2010-07-31 06:52:47 +04: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.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2012-10-11 02:11:13 +04:00
|
|
|
import re
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2017-01-17 16:22:18 +03:00
|
|
|
from . import Image, ImageFile, ImagePalette
|
2020-05-08 19:48:02 +03:00
|
|
|
from ._binary import o8
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# XPM header
|
2019-03-21 16:28:20 +03:00
|
|
|
xpm_head = re.compile(b'"([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)')
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
def _accept(prefix):
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return prefix[:9] == b"/* XPM */"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-07-17 03:40:14 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# Image plugin for X11 pixel maps.
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
class XpmImageFile(ImageFile.ImageFile):
|
|
|
|
|
|
|
|
format = "XPM"
|
|
|
|
format_description = "X11 Pixel Map"
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
|
|
|
|
if not _accept(self.fp.read(9)):
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("not an XPM file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# skip forward to next string
|
2012-10-17 07:39:56 +04:00
|
|
|
while True:
|
2010-07-31 06:52:47 +04:00
|
|
|
s = self.fp.readline()
|
|
|
|
if not s:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("broken XPM file")
|
2010-07-31 06:52:47 +04:00
|
|
|
m = xpm_head.match(s)
|
|
|
|
if m:
|
|
|
|
break
|
|
|
|
|
2018-09-30 05:58:02 +03:00
|
|
|
self._size = int(m.group(1)), int(m.group(2))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
pal = int(m.group(3))
|
|
|
|
bpp = int(m.group(4))
|
|
|
|
|
|
|
|
if pal > 256 or bpp != 1:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise ValueError("cannot read this XPM file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# load palette description
|
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
palette = [b"\0\0\0"] * 256
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2022-04-10 19:52:38 +03:00
|
|
|
for _ in range(pal):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
s = self.fp.readline()
|
2019-03-21 16:28:20 +03:00
|
|
|
if s[-2:] == b"\r\n":
|
2010-07-31 06:52:47 +04:00
|
|
|
s = s[:-2]
|
2019-03-21 16:28:20 +03:00
|
|
|
elif s[-1:] in b"\r\n":
|
2010-07-31 06:52:47 +04:00
|
|
|
s = s[:-1]
|
|
|
|
|
2020-05-08 19:48:02 +03:00
|
|
|
c = s[1]
|
2012-10-11 02:11:13 +04:00
|
|
|
s = s[2:-2].split()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
for i in range(0, len(s), 2):
|
|
|
|
|
2012-10-25 17:12:13 +04:00
|
|
|
if s[i] == b"c":
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# process colour key
|
2019-03-21 16:28:20 +03:00
|
|
|
rgb = s[i + 1]
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if rgb == b"None":
|
2010-07-31 06:52:47 +04:00
|
|
|
self.info["transparency"] = c
|
2022-04-25 15:50:08 +03:00
|
|
|
elif rgb[:1] == b"#":
|
2010-07-31 06:52:47 +04:00
|
|
|
# FIXME: handle colour names (see ImagePalette.py)
|
2012-10-11 02:11:13 +04:00
|
|
|
rgb = int(rgb[1:], 16)
|
2019-03-21 16:28:20 +03:00
|
|
|
palette[c] = (
|
|
|
|
o8((rgb >> 16) & 255) + o8((rgb >> 8) & 255) + o8(rgb & 255)
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
else:
|
|
|
|
# unknown colour
|
2012-10-11 07:52:53 +04:00
|
|
|
raise ValueError("cannot read this XPM file")
|
2010-07-31 06:52:47 +04:00
|
|
|
break
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# missing colour key
|
2012-10-11 07:52:53 +04:00
|
|
|
raise ValueError("cannot read this XPM file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
self.mode = "P"
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
self.palette = ImagePalette.raw("RGB", b"".join(palette))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), ("P", 0, 1))]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def load_read(self, bytes):
|
|
|
|
|
|
|
|
#
|
|
|
|
# load all image data in one chunk
|
|
|
|
|
|
|
|
xsize, ysize = self.size
|
|
|
|
|
|
|
|
s = [None] * ysize
|
|
|
|
|
|
|
|
for i in range(ysize):
|
2019-03-21 16:28:20 +03:00
|
|
|
s[i] = self.fp.readline()[1 : xsize + 1].ljust(xsize)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return b"".join(s)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# Registry
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_open(XpmImageFile.format, XpmImageFile, _accept)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_extension(XpmImageFile.format, ".xpm")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_mime(XpmImageFile.format, "image/xpm")
|