Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/PaletteFile.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

31 statements  

1# 

2# Python Imaging Library 

3# $Id$ 

4# 

5# stuff to read simple, teragon-style palette files 

6# 

7# History: 

8# 97-08-23 fl Created 

9# 

10# Copyright (c) Secret Labs AB 1997. 

11# Copyright (c) Fredrik Lundh 1997. 

12# 

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

14# 

15from __future__ import annotations 

16 

17__lazy_modules__ = {"PIL._binary"} 

18 

19from ._binary import o8 

20 

21TYPE_CHECKING = False 

22if TYPE_CHECKING: 

23 from typing import IO 

24 

25 

26class PaletteFile: 

27 """File handler for Teragon-style palette files.""" 

28 

29 rawmode = "RGB" 

30 

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

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

33 

34 while True: 

35 s = fp.readline() 

36 

37 if not s: 

38 break 

39 if s.startswith(b"#"): 

40 continue 

41 if len(s) > 100: 

42 msg = "bad palette file" 

43 raise SyntaxError(msg) 

44 

45 v = [int(x) for x in s.split()] 

46 try: 

47 [i, r, g, b] = v 

48 except ValueError: 

49 [i, r] = v 

50 g = b = r 

51 

52 if 0 <= i <= 255: 

53 palette[i] = o8(r) + o8(g) + o8(b) 

54 

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

56 

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

58 return self.palette, self.rawmode