home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
pypil112.zip
/
PIL-1.1.2.zip
/
Lib
/
site-packages
/
PIL
/
ImageFile.pyc
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2001-12-25
|
11KB
|
341 lines
# Source Generated with Decompyle++
# File: in.pyc (Python 2.2)
import Image
import traceback
import sys
MAXBLOCK = 65536
MAPMODES = ('L', 'P', 'RGBX', 'RGBA', 'CMYK', 'I;16', 'I;16B')
def _tilesort(t1, t2):
return cmp(t1[2], t2[2])
class ImageFile(Image.Image):
'''Base class for image file format handlers.'''
def __init__(self, fp = None, filename = None):
Image.Image.__init__(self)
self.tile = None
self.readonly = 1
self.decoderconfig = ()
self.decodermaxblock = MAXBLOCK
if type(fp) == type(''):
self.fp = open(fp, 'rb')
self.filename = fp
else:
self.fp = fp
self.filename = filename
try:
self._open()
except IndexError:
v = None
if Image.DEBUG > 1:
traceback.print_exc()
raise SyntaxError, v
except TypeError:
v = None
if Image.DEBUG > 1:
traceback.print_exc()
raise SyntaxError, v
except KeyError:
v = None
if Image.DEBUG > 1:
traceback.print_exc()
raise SyntaxError, v
if not (self.mode) or self.size[0] <= 0:
raise SyntaxError, 'not identified by this driver'
def draft(self, mode, size):
'''Set draft mode'''
pass
def verify(self):
'''Check file integrity'''
self.fp = None
def load(self):
'''Load image data based on tile list'''
Image.Image.load(self)
if self.tile is None:
raise IOError, 'cannot load this image'
if not (self.tile):
return None
self.map = None
readonly = 0
if self.filename and len(self.tile) == 1:
(d, e, o, a) = self.tile[0]
if d == 'raw' and a[0] == self.mode and a[0] in MAPMODES:
try:
self.map = Image.core.map(self.filename)
self.map.seek(o)
self.im = self.map.readimage(self.mode, self.size, a[1], a[2])
readonly = 1
except (AttributeError, IOError):
self.map = None
self.load_prepare()
if not (self.map):
self.tile.sort(_tilesort)
try:
prefix = self.tile_prefix
except AttributeError:
prefix = ''
for d, e, o, a in self.tile:
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
self.load_seek(o)
try:
d.setimage(self.im, e)
except ValueError:
continue
b = prefix
t = len(b)
while 1:
s = self.load_read(self.decodermaxblock)
if not s:
self.tile = []
raise IOError, 'image file is truncated, %d bytes left in buffer' % len(b)
b = b + s
(n, e) = d.decode(b)
if n < 0:
break
b = b[n:]
t = t + n
self.tile = []
self.readonly = readonly
self.fp = None
if not (self.map) and e < 0:
raise IOError, 'decoder error %d when reading image file' % e
if hasattr(self, 'tile_post_rotate'):
self.im = self.im.rotate(self.tile_post_rotate)
self.size = self.im.size
self.load_end()
def load_prepare(self):
if not (self.im) and self.im.mode != self.mode or self.im.size != self.size:
self.im = Image.core.new(self.mode, self.size)
if self.mode == 'P':
Image.Image.load(self)
def load_end(self):
pass
def load_seek(self, pos):
self.fp.seek(pos)
def load_read(self, bytes):
return self.fp.read(bytes)
class _ParserFile:
def __init__(self, data):
self.data = data
self.offset = 0
def close(self):
self.data = self.offset = None
def tell(self):
return self.offset
def seek(self, offset, whence = 0):
if whence == 0:
self.offset = offset
elif whence == 1:
self.offset = self.offset + offset
else:
raise IOError, 'illegal argument to seek'
def read(self, bytes = 0):
pos = self.offset
if bytes:
data = self.data[pos:pos + bytes]
else:
data = self.data[pos:]
self.offset = pos + len(data)
return data
def readline(self):
s = ''
while 1:
c = self.read(1)
if not c:
break
s = s + c
if c == '\n':
break
return s
class Parser:
image = None
data = None
decoder = None
finished = 0
def reset(self):
if not __debug__ and self.data is None:
raise AssertionError, 'cannot reuse parsers'
def feed(self, data):
if self.finished:
return None
if self.data is None:
self.data = data
else:
self.data = self.data + data
if self.decoder:
if self.offset > 0:
skip = min(len(self.data), self.offset)
self.data = self.data[skip:]
self.offset = self.offset - skip
if self.offset > 0 or not (self.data):
return None
(n, e) = self.decoder.decode(self.data)
if n < 0:
self.data = None
self.finished = 1
if e < 0:
self.image = None
raise IOError, 'decoder error %d when reading image file' % e
else:
return None
self.data = self.data[n:]
else:
try:
try:
fp = _ParserFile(self.data)
im = Image.open(fp)
finally:
fp.close()
except IOError:
pass
if len(im.tile) != 1:
raise IOError, 'cannot parse this image'
im.load_prepare()
(d, e, o, a) = im.tile[0]
im.tile = []
self.decoder = Image._getdecoder(im.mode, d, a, im.decoderconfig)
self.decoder.setimage(im.im, e)
self.offset = o
if self.offset <= len(self.data):
self.data = self.data[self.offset:]
self.offset = 0
self.image = im
def close(self):
if self.decoder:
self.feed('')
self.data = self.decoder = None
if not (self.finished):
raise IOError, 'image was incomplete'
if not (self.image):
raise IOError, 'cannot parse this image'
return self.image
def _save(im, fp, tile):
'''Helper to save image based on tile list'''
im.load()
if not hasattr(im, 'encoderconfig'):
im.encoderconfig = ()
tile.sort(_tilesort)
bufsize = max(MAXBLOCK, im.size[0] * 4)
try:
fh = fp.fileno()
fp.flush()
except AttributeError:
for e, b, o, a in tile:
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
if o > 0:
fp.seek(o, 0)
e.setimage(im.im, b)
while 1:
(l, s, d) = e.encode(bufsize)
fp.write(d)
if s:
break
if s < 0:
raise IOError, 'encoder error %d when writing image file' % s
for e, b, o, a in tile:
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
if o > 0:
fp.seek(o, 0)
e.setimage(im.im, b)
s = e.encode_to_file(fh, bufsize)
if s < 0:
raise IOError, 'encoder error %d when writing image file' % s
try:
fp.flush()
except:
pass