openmedialibrary_platform/Darwin/lib/python3.5/site-packages/PIL/PaletteFile.py

56 lines
1.1 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00:00
#
# Python Imaging Library
# $Id$
#
# stuff to read simple, teragon-style palette files
#
# History:
# 97-08-23 fl Created
#
# Copyright (c) Secret Labs AB 1997.
# Copyright (c) Fredrik Lundh 1997.
#
# See the README file for information on usage and redistribution.
#
2014-09-30 16:15:32 +00:00
from PIL._binary import o8
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
2013-10-11 17:28:32 +00:00
##
# File handler for Teragon-style palette files.
2015-11-25 01:25:01 +00:00
class PaletteFile(object):
2013-10-11 17:28:32 +00:00
rawmode = "RGB"
def __init__(self, fp):
2014-09-30 16:15:32 +00:00
self.palette = [(i, i, i) for i in range(256)]
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
while True:
2013-10-11 17:28:32 +00:00
s = fp.readline()
if not s:
break
2014-09-30 16:15:32 +00:00
if s[0:1] == b"#":
2013-10-11 17:28:32 +00:00
continue
if len(s) > 100:
2014-09-30 16:15:32 +00:00
raise SyntaxError("bad palette file")
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
v = [int(x) for x in s.split()]
2013-10-11 17:28:32 +00:00
try:
[i, r, g, b] = v
except ValueError:
[i, r] = v
g = b = r
if 0 <= i <= 255:
2014-09-30 16:15:32 +00:00
self.palette[i] = o8(r) + o8(g) + o8(b)
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
self.palette = b"".join(self.palette)
2013-10-11 17:28:32 +00:00
def getpalette(self):
return self.palette, self.rawmode