2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# DCX file handling
|
|
|
|
#
|
|
|
|
# DCX is a container file format defined by Intel, commonly used
|
|
|
|
# for fax applications. Each DCX file consists of a directory
|
|
|
|
# (a list of file offsets) followed by a set of (usually 1-bit)
|
|
|
|
# PCX files.
|
|
|
|
#
|
|
|
|
# History:
|
|
|
|
# 1995-09-09 fl Created
|
|
|
|
# 1996-03-20 fl Properly derived from PcxImageFile.
|
|
|
|
# 1998-07-15 fl Renamed offset attribute to avoid name clash
|
|
|
|
# 2002-07-30 fl Fixed file handling
|
|
|
|
#
|
|
|
|
# Copyright (c) 1997-98 by Secret Labs AB.
|
|
|
|
# Copyright (c) 1995-96 by Fredrik Lundh.
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
|
|
|
__version__ = "0.2"
|
|
|
|
|
2013-03-07 20:20:28 +04:00
|
|
|
from PIL import Image, _binary
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-03-07 20:20:28 +04:00
|
|
|
from PIL.PcxImagePlugin import PcxImageFile
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-07-17 02:21:56 +04:00
|
|
|
MAGIC = 0x3ADE68B1 # QUIZ: what's this value, then?
|
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
|
|
|
i32 = _binary.i32le
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-07-17 02:21:56 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _accept(prefix):
|
|
|
|
return i32(prefix) == MAGIC
|
|
|
|
|
2014-07-17 02:21:56 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# Image plugin for the Intel DCX format.
|
|
|
|
|
|
|
|
class DcxImageFile(PcxImageFile):
|
|
|
|
|
|
|
|
format = "DCX"
|
|
|
|
format_description = "Intel DCX"
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
|
|
|
|
# Header
|
|
|
|
s = self.fp.read(4)
|
|
|
|
if i32(s) != MAGIC:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("not a DCX file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# Component directory
|
|
|
|
self._offset = []
|
|
|
|
for i in range(1024):
|
|
|
|
offset = i32(self.fp.read(4))
|
|
|
|
if not offset:
|
|
|
|
break
|
|
|
|
self._offset.append(offset)
|
|
|
|
|
|
|
|
self.__fp = self.fp
|
|
|
|
self.seek(0)
|
|
|
|
|
2015-04-15 03:43:05 +03:00
|
|
|
@property
|
|
|
|
def n_frames(self):
|
|
|
|
return len(self._offset)
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def seek(self, frame):
|
|
|
|
if frame >= len(self._offset):
|
|
|
|
raise EOFError("attempt to seek outside DCX directory")
|
|
|
|
self.frame = frame
|
|
|
|
self.fp = self.__fp
|
|
|
|
self.fp.seek(self._offset[frame])
|
|
|
|
PcxImageFile._open(self)
|
|
|
|
|
|
|
|
def tell(self):
|
|
|
|
return self.frame
|
|
|
|
|
|
|
|
|
|
|
|
Image.register_open("DCX", DcxImageFile, _accept)
|
|
|
|
|
|
|
|
Image.register_extension("DCX", ".dcx")
|