Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pillow-10.4.0-py3.8-linux-x86_64.egg/PIL/GimpPaletteFile.py: 29%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

28 statements  

1# 

2# Python Imaging Library 

3# $Id$ 

4# 

5# stuff to read GIMP palette files 

6# 

7# History: 

8# 1997-08-23 fl Created 

9# 2004-09-07 fl Support GIMP 2.0 palette files. 

10# 

11# Copyright (c) Secret Labs AB 1997-2004. All rights reserved. 

12# Copyright (c) Fredrik Lundh 1997-2004. 

13# 

14# See the README file for information on usage and redistribution. 

15# 

16from __future__ import annotations 

17 

18import re 

19from typing import IO 

20 

21from ._binary import o8 

22 

23 

24class GimpPaletteFile: 

25 """File handler for GIMP's palette format.""" 

26 

27 rawmode = "RGB" 

28 

29 def __init__(self, fp: IO[bytes]) -> None: 

30 palette = [o8(i) * 3 for i in range(256)] 

31 

32 if fp.readline()[:12] != b"GIMP Palette": 

33 msg = "not a GIMP palette file" 

34 raise SyntaxError(msg) 

35 

36 for i in range(256): 

37 s = fp.readline() 

38 if not s: 

39 break 

40 

41 # skip fields and comment lines 

42 if re.match(rb"\w+:|#", s): 

43 continue 

44 if len(s) > 100: 

45 msg = "bad palette file" 

46 raise SyntaxError(msg) 

47 

48 v = tuple(map(int, s.split()[:3])) 

49 if len(v) != 3: 

50 msg = "bad palette entry" 

51 raise ValueError(msg) 

52 

53 palette[i] = o8(v[0]) + o8(v[1]) + o8(v[2]) 

54 

55 self.palette = b"".join(palette) 

56 

57 def getpalette(self) -> tuple[bytes, str]: 

58 return self.palette, self.rawmode