ImageFile.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # base class for image file handlers
  6. #
  7. # history:
  8. # 1995-09-09 fl Created
  9. # 1996-03-11 fl Fixed load mechanism.
  10. # 1996-04-15 fl Added pcx/xbm decoders.
  11. # 1996-04-30 fl Added encoders.
  12. # 1996-12-14 fl Added load helpers
  13. # 1997-01-11 fl Use encode_to_file where possible
  14. # 1997-08-27 fl Flush output in _save
  15. # 1998-03-05 fl Use memory mapping for some modes
  16. # 1999-02-04 fl Use memory mapping also for "I;16" and "I;16B"
  17. # 1999-05-31 fl Added image parser
  18. # 2000-10-12 fl Set readonly flag on memory-mapped images
  19. # 2002-03-20 fl Use better messages for common decoder errors
  20. # 2003-04-21 fl Fall back on mmap/map_buffer if map is not available
  21. # 2003-10-30 fl Added StubImageFile class
  22. # 2004-02-25 fl Made incremental parser more robust
  23. #
  24. # Copyright (c) 1997-2004 by Secret Labs AB
  25. # Copyright (c) 1995-2004 by Fredrik Lundh
  26. #
  27. # See the README file for information on usage and redistribution.
  28. #
  29. import io
  30. import struct
  31. import sys
  32. from . import Image
  33. from ._util import isPath
  34. MAXBLOCK = 65536
  35. SAFEBLOCK = 1024 * 1024
  36. LOAD_TRUNCATED_IMAGES = False
  37. ERRORS = {
  38. -1: "image buffer overrun error",
  39. -2: "decoding error",
  40. -3: "unknown error",
  41. -8: "bad configuration",
  42. -9: "out of memory error",
  43. }
  44. def raise_ioerror(error):
  45. try:
  46. message = Image.core.getcodecstatus(error)
  47. except AttributeError:
  48. message = ERRORS.get(error)
  49. if not message:
  50. message = "decoder error %d" % error
  51. raise IOError(message + " when reading image file")
  52. #
  53. # --------------------------------------------------------------------
  54. # Helpers
  55. def _tilesort(t):
  56. # sort on offset
  57. return t[2]
  58. #
  59. # --------------------------------------------------------------------
  60. # ImageFile base class
  61. class ImageFile(Image.Image):
  62. "Base class for image file format handlers."
  63. def __init__(self, fp=None, filename=None):
  64. Image.Image.__init__(self)
  65. self._min_frame = 0
  66. self.custom_mimetype = None
  67. self.tile = None
  68. self.readonly = 1 # until we know better
  69. self.decoderconfig = ()
  70. self.decodermaxblock = MAXBLOCK
  71. if isPath(fp):
  72. # filename
  73. self.fp = open(fp, "rb")
  74. self.filename = fp
  75. self._exclusive_fp = True
  76. else:
  77. # stream
  78. self.fp = fp
  79. self.filename = filename
  80. # can be overridden
  81. self._exclusive_fp = None
  82. try:
  83. self._open()
  84. except (
  85. IndexError, # end of data
  86. TypeError, # end of data (ord)
  87. KeyError, # unsupported mode
  88. EOFError, # got header but not the first frame
  89. struct.error,
  90. ) as v:
  91. # close the file only if we have opened it this constructor
  92. if self._exclusive_fp:
  93. self.fp.close()
  94. raise SyntaxError(v)
  95. if not self.mode or self.size[0] <= 0:
  96. raise SyntaxError("not identified by this driver")
  97. def draft(self, mode, size):
  98. """Set draft mode"""
  99. pass
  100. def get_format_mimetype(self):
  101. if self.custom_mimetype:
  102. return self.custom_mimetype
  103. if self.format is not None:
  104. return Image.MIME.get(self.format.upper())
  105. def verify(self):
  106. """Check file integrity"""
  107. # raise exception if something's wrong. must be called
  108. # directly after open, and closes file when finished.
  109. if self._exclusive_fp:
  110. self.fp.close()
  111. self.fp = None
  112. def load(self):
  113. """Load image data based on tile list"""
  114. pixel = Image.Image.load(self)
  115. if self.tile is None:
  116. raise IOError("cannot load this image")
  117. if not self.tile:
  118. return pixel
  119. self.map = None
  120. use_mmap = self.filename and len(self.tile) == 1
  121. # As of pypy 2.1.0, memory mapping was failing here.
  122. use_mmap = use_mmap and not hasattr(sys, "pypy_version_info")
  123. readonly = 0
  124. # look for read/seek overrides
  125. try:
  126. read = self.load_read
  127. # don't use mmap if there are custom read/seek functions
  128. use_mmap = False
  129. except AttributeError:
  130. read = self.fp.read
  131. try:
  132. seek = self.load_seek
  133. use_mmap = False
  134. except AttributeError:
  135. seek = self.fp.seek
  136. if use_mmap:
  137. # try memory mapping
  138. decoder_name, extents, offset, args = self.tile[0]
  139. if (
  140. decoder_name == "raw"
  141. and len(args) >= 3
  142. and args[0] == self.mode
  143. and args[0] in Image._MAPMODES
  144. ):
  145. try:
  146. if hasattr(Image.core, "map"):
  147. # use built-in mapper WIN32 only
  148. self.map = Image.core.map(self.filename)
  149. self.map.seek(offset)
  150. self.im = self.map.readimage(
  151. self.mode, self.size, args[1], args[2]
  152. )
  153. else:
  154. # use mmap, if possible
  155. import mmap
  156. with open(self.filename, "r") as fp:
  157. self.map = mmap.mmap(
  158. fp.fileno(), 0, access=mmap.ACCESS_READ
  159. )
  160. self.im = Image.core.map_buffer(
  161. self.map, self.size, decoder_name, extents, offset, args
  162. )
  163. readonly = 1
  164. # After trashing self.im,
  165. # we might need to reload the palette data.
  166. if self.palette:
  167. self.palette.dirty = 1
  168. except (AttributeError, EnvironmentError, ImportError):
  169. self.map = None
  170. self.load_prepare()
  171. err_code = -3 # initialize to unknown error
  172. if not self.map:
  173. # sort tiles in file order
  174. self.tile.sort(key=_tilesort)
  175. try:
  176. # FIXME: This is a hack to handle TIFF's JpegTables tag.
  177. prefix = self.tile_prefix
  178. except AttributeError:
  179. prefix = b""
  180. for decoder_name, extents, offset, args in self.tile:
  181. decoder = Image._getdecoder(
  182. self.mode, decoder_name, args, self.decoderconfig
  183. )
  184. try:
  185. seek(offset)
  186. decoder.setimage(self.im, extents)
  187. if decoder.pulls_fd:
  188. decoder.setfd(self.fp)
  189. status, err_code = decoder.decode(b"")
  190. else:
  191. b = prefix
  192. while True:
  193. try:
  194. s = read(self.decodermaxblock)
  195. except (IndexError, struct.error):
  196. # truncated png/gif
  197. if LOAD_TRUNCATED_IMAGES:
  198. break
  199. else:
  200. raise IOError("image file is truncated")
  201. if not s: # truncated jpeg
  202. if LOAD_TRUNCATED_IMAGES:
  203. break
  204. else:
  205. raise IOError(
  206. "image file is truncated "
  207. "(%d bytes not processed)" % len(b)
  208. )
  209. b = b + s
  210. n, err_code = decoder.decode(b)
  211. if n < 0:
  212. break
  213. b = b[n:]
  214. finally:
  215. # Need to cleanup here to prevent leaks
  216. decoder.cleanup()
  217. self.tile = []
  218. self.readonly = readonly
  219. self.load_end()
  220. if self._exclusive_fp and self._close_exclusive_fp_after_loading:
  221. self.fp.close()
  222. self.fp = None
  223. if not self.map and not LOAD_TRUNCATED_IMAGES and err_code < 0:
  224. # still raised if decoder fails to return anything
  225. raise_ioerror(err_code)
  226. return Image.Image.load(self)
  227. def load_prepare(self):
  228. # create image memory if necessary
  229. if not self.im or self.im.mode != self.mode or self.im.size != self.size:
  230. self.im = Image.core.new(self.mode, self.size)
  231. # create palette (optional)
  232. if self.mode == "P":
  233. Image.Image.load(self)
  234. def load_end(self):
  235. # may be overridden
  236. pass
  237. # may be defined for contained formats
  238. # def load_seek(self, pos):
  239. # pass
  240. # may be defined for blocked formats (e.g. PNG)
  241. # def load_read(self, bytes):
  242. # pass
  243. def _seek_check(self, frame):
  244. if (
  245. frame < self._min_frame
  246. # Only check upper limit on frames if additional seek operations
  247. # are not required to do so
  248. or (
  249. not (hasattr(self, "_n_frames") and self._n_frames is None)
  250. and frame >= self.n_frames + self._min_frame
  251. )
  252. ):
  253. raise EOFError("attempt to seek outside sequence")
  254. return self.tell() != frame
  255. class StubImageFile(ImageFile):
  256. """
  257. Base class for stub image loaders.
  258. A stub loader is an image loader that can identify files of a
  259. certain format, but relies on external code to load the file.
  260. """
  261. def _open(self):
  262. raise NotImplementedError("StubImageFile subclass must implement _open")
  263. def load(self):
  264. loader = self._load()
  265. if loader is None:
  266. raise IOError("cannot find loader for this %s file" % self.format)
  267. image = loader.load(self)
  268. assert image is not None
  269. # become the other object (!)
  270. self.__class__ = image.__class__
  271. self.__dict__ = image.__dict__
  272. def _load(self):
  273. """(Hook) Find actual image loader."""
  274. raise NotImplementedError("StubImageFile subclass must implement _load")
  275. class Parser(object):
  276. """
  277. Incremental image parser. This class implements the standard
  278. feed/close consumer interface.
  279. """
  280. incremental = None
  281. image = None
  282. data = None
  283. decoder = None
  284. offset = 0
  285. finished = 0
  286. def reset(self):
  287. """
  288. (Consumer) Reset the parser. Note that you can only call this
  289. method immediately after you've created a parser; parser
  290. instances cannot be reused.
  291. """
  292. assert self.data is None, "cannot reuse parsers"
  293. def feed(self, data):
  294. """
  295. (Consumer) Feed data to the parser.
  296. :param data: A string buffer.
  297. :exception IOError: If the parser failed to parse the image file.
  298. """
  299. # collect data
  300. if self.finished:
  301. return
  302. if self.data is None:
  303. self.data = data
  304. else:
  305. self.data = self.data + data
  306. # parse what we have
  307. if self.decoder:
  308. if self.offset > 0:
  309. # skip header
  310. skip = min(len(self.data), self.offset)
  311. self.data = self.data[skip:]
  312. self.offset = self.offset - skip
  313. if self.offset > 0 or not self.data:
  314. return
  315. n, e = self.decoder.decode(self.data)
  316. if n < 0:
  317. # end of stream
  318. self.data = None
  319. self.finished = 1
  320. if e < 0:
  321. # decoding error
  322. self.image = None
  323. raise_ioerror(e)
  324. else:
  325. # end of image
  326. return
  327. self.data = self.data[n:]
  328. elif self.image:
  329. # if we end up here with no decoder, this file cannot
  330. # be incrementally parsed. wait until we've gotten all
  331. # available data
  332. pass
  333. else:
  334. # attempt to open this file
  335. try:
  336. with io.BytesIO(self.data) as fp:
  337. im = Image.open(fp)
  338. except IOError:
  339. # traceback.print_exc()
  340. pass # not enough data
  341. else:
  342. flag = hasattr(im, "load_seek") or hasattr(im, "load_read")
  343. if flag or len(im.tile) != 1:
  344. # custom load code, or multiple tiles
  345. self.decode = None
  346. else:
  347. # initialize decoder
  348. im.load_prepare()
  349. d, e, o, a = im.tile[0]
  350. im.tile = []
  351. self.decoder = Image._getdecoder(im.mode, d, a, im.decoderconfig)
  352. self.decoder.setimage(im.im, e)
  353. # calculate decoder offset
  354. self.offset = o
  355. if self.offset <= len(self.data):
  356. self.data = self.data[self.offset :]
  357. self.offset = 0
  358. self.image = im
  359. def __enter__(self):
  360. return self
  361. def __exit__(self, *args):
  362. self.close()
  363. def close(self):
  364. """
  365. (Consumer) Close the stream.
  366. :returns: An image object.
  367. :exception IOError: If the parser failed to parse the image file either
  368. because it cannot be identified or cannot be
  369. decoded.
  370. """
  371. # finish decoding
  372. if self.decoder:
  373. # get rid of what's left in the buffers
  374. self.feed(b"")
  375. self.data = self.decoder = None
  376. if not self.finished:
  377. raise IOError("image was incomplete")
  378. if not self.image:
  379. raise IOError("cannot parse this image")
  380. if self.data:
  381. # incremental parsing not possible; reopen the file
  382. # not that we have all data
  383. with io.BytesIO(self.data) as fp:
  384. try:
  385. self.image = Image.open(fp)
  386. finally:
  387. self.image.load()
  388. return self.image
  389. # --------------------------------------------------------------------
  390. def _save(im, fp, tile, bufsize=0):
  391. """Helper to save image based on tile list
  392. :param im: Image object.
  393. :param fp: File object.
  394. :param tile: Tile list.
  395. :param bufsize: Optional buffer size
  396. """
  397. im.load()
  398. if not hasattr(im, "encoderconfig"):
  399. im.encoderconfig = ()
  400. tile.sort(key=_tilesort)
  401. # FIXME: make MAXBLOCK a configuration parameter
  402. # It would be great if we could have the encoder specify what it needs
  403. # But, it would need at least the image size in most cases. RawEncode is
  404. # a tricky case.
  405. bufsize = max(MAXBLOCK, bufsize, im.size[0] * 4) # see RawEncode.c
  406. if fp == sys.stdout:
  407. fp.flush()
  408. return
  409. try:
  410. fh = fp.fileno()
  411. fp.flush()
  412. except (AttributeError, io.UnsupportedOperation):
  413. # compress to Python file-compatible object
  414. for e, b, o, a in tile:
  415. e = Image._getencoder(im.mode, e, a, im.encoderconfig)
  416. if o > 0:
  417. fp.seek(o)
  418. e.setimage(im.im, b)
  419. if e.pushes_fd:
  420. e.setfd(fp)
  421. l, s = e.encode_to_pyfd()
  422. else:
  423. while True:
  424. l, s, d = e.encode(bufsize)
  425. fp.write(d)
  426. if s:
  427. break
  428. if s < 0:
  429. raise IOError("encoder error %d when writing image file" % s)
  430. e.cleanup()
  431. else:
  432. # slight speedup: compress to real file object
  433. for e, b, o, a in tile:
  434. e = Image._getencoder(im.mode, e, a, im.encoderconfig)
  435. if o > 0:
  436. fp.seek(o)
  437. e.setimage(im.im, b)
  438. if e.pushes_fd:
  439. e.setfd(fp)
  440. l, s = e.encode_to_pyfd()
  441. else:
  442. s = e.encode_to_file(fh, bufsize)
  443. if s < 0:
  444. raise IOError("encoder error %d when writing image file" % s)
  445. e.cleanup()
  446. if hasattr(fp, "flush"):
  447. fp.flush()
  448. def _safe_read(fp, size):
  449. """
  450. Reads large blocks in a safe way. Unlike fp.read(n), this function
  451. doesn't trust the user. If the requested size is larger than
  452. SAFEBLOCK, the file is read block by block.
  453. :param fp: File handle. Must implement a <b>read</b> method.
  454. :param size: Number of bytes to read.
  455. :returns: A string containing up to <i>size</i> bytes of data.
  456. """
  457. if size <= 0:
  458. return b""
  459. if size <= SAFEBLOCK:
  460. return fp.read(size)
  461. data = []
  462. while size > 0:
  463. block = fp.read(min(size, SAFEBLOCK))
  464. if not block:
  465. break
  466. data.append(block)
  467. size -= len(block)
  468. return b"".join(data)
  469. class PyCodecState(object):
  470. def __init__(self):
  471. self.xsize = 0
  472. self.ysize = 0
  473. self.xoff = 0
  474. self.yoff = 0
  475. def extents(self):
  476. return (self.xoff, self.yoff, self.xoff + self.xsize, self.yoff + self.ysize)
  477. class PyDecoder(object):
  478. """
  479. Python implementation of a format decoder. Override this class and
  480. add the decoding logic in the `decode` method.
  481. See :ref:`Writing Your Own File Decoder in Python<file-decoders-py>`
  482. """
  483. _pulls_fd = False
  484. def __init__(self, mode, *args):
  485. self.im = None
  486. self.state = PyCodecState()
  487. self.fd = None
  488. self.mode = mode
  489. self.init(args)
  490. def init(self, args):
  491. """
  492. Override to perform decoder specific initialization
  493. :param args: Array of args items from the tile entry
  494. :returns: None
  495. """
  496. self.args = args
  497. @property
  498. def pulls_fd(self):
  499. return self._pulls_fd
  500. def decode(self, buffer):
  501. """
  502. Override to perform the decoding process.
  503. :param buffer: A bytes object with the data to be decoded.
  504. :returns: A tuple of (bytes consumed, errcode).
  505. If finished with decoding return <0 for the bytes consumed.
  506. Err codes are from `ERRORS`
  507. """
  508. raise NotImplementedError()
  509. def cleanup(self):
  510. """
  511. Override to perform decoder specific cleanup
  512. :returns: None
  513. """
  514. pass
  515. def setfd(self, fd):
  516. """
  517. Called from ImageFile to set the python file-like object
  518. :param fd: A python file-like object
  519. :returns: None
  520. """
  521. self.fd = fd
  522. def setimage(self, im, extents=None):
  523. """
  524. Called from ImageFile to set the core output image for the decoder
  525. :param im: A core image object
  526. :param extents: a 4 tuple of (x0, y0, x1, y1) defining the rectangle
  527. for this tile
  528. :returns: None
  529. """
  530. # following c code
  531. self.im = im
  532. if extents:
  533. (x0, y0, x1, y1) = extents
  534. else:
  535. (x0, y0, x1, y1) = (0, 0, 0, 0)
  536. if x0 == 0 and x1 == 0:
  537. self.state.xsize, self.state.ysize = self.im.size
  538. else:
  539. self.state.xoff = x0
  540. self.state.yoff = y0
  541. self.state.xsize = x1 - x0
  542. self.state.ysize = y1 - y0
  543. if self.state.xsize <= 0 or self.state.ysize <= 0:
  544. raise ValueError("Size cannot be negative")
  545. if (
  546. self.state.xsize + self.state.xoff > self.im.size[0]
  547. or self.state.ysize + self.state.yoff > self.im.size[1]
  548. ):
  549. raise ValueError("Tile cannot extend outside image")
  550. def set_as_raw(self, data, rawmode=None):
  551. """
  552. Convenience method to set the internal image from a stream of raw data
  553. :param data: Bytes to be set
  554. :param rawmode: The rawmode to be used for the decoder.
  555. If not specified, it will default to the mode of the image
  556. :returns: None
  557. """
  558. if not rawmode:
  559. rawmode = self.mode
  560. d = Image._getdecoder(self.mode, "raw", (rawmode))
  561. d.setimage(self.im, self.state.extents())
  562. s = d.decode(data)
  563. if s[0] >= 0:
  564. raise ValueError("not enough image data")
  565. if s[1] != 0:
  566. raise ValueError("cannot decode image data")