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

131 lines
3.1 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00:00
#
# The Python Imaging Library.
# $Id$
#
# XPM File handling
#
# History:
# 1996-12-29 fl Created
# 2001-02-17 fl Use 're' instead of 'regex' (Python 2.1) (0.7)
#
# Copyright (c) Secret Labs AB 1997-2001.
# Copyright (c) Fredrik Lundh 1996-2001.
#
# See the README file for information on usage and redistribution.
#
2014-09-30 16:15:32 +00:00
import re
from PIL import Image, ImageFile, ImagePalette
from PIL._binary import i8, o8
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
__version__ = "0.2"
2013-10-11 17:28:32 +00:00
# XPM header
2014-09-30 16:15:32 +00:00
xpm_head = re.compile(b"\"([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)")
2013-10-11 17:28:32 +00:00
def _accept(prefix):
2014-09-30 16:15:32 +00:00
return prefix[:9] == b"/* XPM */"
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
2013-10-11 17:28:32 +00:00
##
# Image plugin for X11 pixel maps.
class XpmImageFile(ImageFile.ImageFile):
format = "XPM"
format_description = "X11 Pixel Map"
def _open(self):
if not _accept(self.fp.read(9)):
2014-09-30 16:15:32 +00:00
raise SyntaxError("not an XPM file")
2013-10-11 17:28:32 +00:00
# skip forward to next string
2014-09-30 16:15:32 +00:00
while True:
2013-10-11 17:28:32 +00:00
s = self.fp.readline()
if not s:
2014-09-30 16:15:32 +00:00
raise SyntaxError("broken XPM file")
2013-10-11 17:28:32 +00:00
m = xpm_head.match(s)
if m:
break
self.size = int(m.group(1)), int(m.group(2))
pal = int(m.group(3))
bpp = int(m.group(4))
if pal > 256 or bpp != 1:
2014-09-30 16:15:32 +00:00
raise ValueError("cannot read this XPM file")
2013-10-11 17:28:32 +00:00
#
# load palette description
2014-09-30 16:15:32 +00:00
palette = [b"\0\0\0"] * 256
2013-10-11 17:28:32 +00:00
for i in range(pal):
s = self.fp.readline()
2014-09-30 16:15:32 +00:00
if s[-2:] == b'\r\n':
2013-10-11 17:28:32 +00:00
s = s[:-2]
2014-09-30 16:15:32 +00:00
elif s[-1:] in b'\r\n':
2013-10-11 17:28:32 +00:00
s = s[:-1]
2014-09-30 16:15:32 +00:00
c = i8(s[1])
s = s[2:-2].split()
2013-10-11 17:28:32 +00:00
for i in range(0, len(s), 2):
2014-09-30 16:15:32 +00:00
if s[i] == b"c":
2013-10-11 17:28:32 +00:00
# process colour key
rgb = s[i+1]
2014-09-30 16:15:32 +00:00
if rgb == b"None":
2013-10-11 17:28:32 +00:00
self.info["transparency"] = c
2014-09-30 16:15:32 +00:00
elif rgb[0:1] == b"#":
2013-10-11 17:28:32 +00:00
# FIXME: handle colour names (see ImagePalette.py)
2014-09-30 16:15:32 +00:00
rgb = int(rgb[1:], 16)
2015-11-25 01:25:01 +00:00
palette[c] = (o8((rgb >> 16) & 255) +
o8((rgb >> 8) & 255) +
o8(rgb & 255))
2013-10-11 17:28:32 +00:00
else:
# unknown colour
2014-09-30 16:15:32 +00:00
raise ValueError("cannot read this XPM file")
2013-10-11 17:28:32 +00:00
break
else:
# missing colour key
2014-09-30 16:15:32 +00:00
raise ValueError("cannot read this XPM file")
2013-10-11 17:28:32 +00:00
self.mode = "P"
2014-09-30 16:15:32 +00:00
self.palette = ImagePalette.raw("RGB", b"".join(palette))
2013-10-11 17:28:32 +00:00
self.tile = [("raw", (0, 0)+self.size, self.fp.tell(), ("P", 0, 1))]
def load_read(self, bytes):
#
# load all image data in one chunk
xsize, ysize = self.size
s = [None] * ysize
for i in range(ysize):
2014-09-30 16:15:32 +00:00
s[i] = self.fp.readline()[1:xsize+1].ljust(xsize)
2013-10-11 17:28:32 +00:00
self.fp = None
2014-09-30 16:15:32 +00:00
return b"".join(s)
2013-10-11 17:28:32 +00:00
#
# Registry
2015-11-25 01:25:01 +00:00
Image.register_open(XpmImageFile.format, XpmImageFile, _accept)
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
Image.register_extension(XpmImageFile.format, ".xpm")
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
Image.register_mime(XpmImageFile.format, "image/xpm")