Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/PaletteFile.py: 25%
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
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
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
17from typing import IO
19from ._binary import o8
22class PaletteFile:
23 """File handler for Teragon-style palette files."""
25 rawmode = "RGB"
27 def __init__(self, fp: IO[bytes]) -> None:
28 palette = [o8(i) * 3 for i in range(256)]
30 while True:
31 s = fp.readline()
33 if not s:
34 break
35 if s.startswith(b"#"):
36 continue
37 if len(s) > 100:
38 msg = "bad palette file"
39 raise SyntaxError(msg)
41 v = [int(x) for x in s.split()]
42 try:
43 [i, r, g, b] = v
44 except ValueError:
45 [i, r] = v
46 g = b = r
48 if 0 <= i <= 255:
49 palette[i] = o8(r) + o8(g) + o8(b)
51 self.palette = b"".join(palette)
53 def getpalette(self) -> tuple[bytes, str]:
54 return self.palette, self.rawmode