ImageFile.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. from __future__ import annotations
  30. import abc
  31. import io
  32. import itertools
  33. import logging
  34. import os
  35. import struct
  36. from typing import IO, Any, NamedTuple, cast
  37. from . import ExifTags, Image
  38. from ._deprecate import deprecate
  39. from ._util import DeferredError, is_path
  40. TYPE_CHECKING = False
  41. if TYPE_CHECKING:
  42. from ._typing import StrOrBytesPath
  43. logger = logging.getLogger(__name__)
  44. MAXBLOCK = 65536
  45. SAFEBLOCK = 1024 * 1024
  46. LOAD_TRUNCATED_IMAGES = False
  47. """Whether or not to load truncated image files. User code may change this."""
  48. ERRORS = {
  49. -1: "image buffer overrun error",
  50. -2: "decoding error",
  51. -3: "unknown error",
  52. -8: "bad configuration",
  53. -9: "out of memory error",
  54. }
  55. """
  56. Dict of known error codes returned from :meth:`.PyDecoder.decode`,
  57. :meth:`.PyEncoder.encode` :meth:`.PyEncoder.encode_to_pyfd` and
  58. :meth:`.PyEncoder.encode_to_file`.
  59. """
  60. #
  61. # --------------------------------------------------------------------
  62. # Helpers
  63. def _get_oserror(error: int, *, encoder: bool) -> OSError:
  64. try:
  65. msg = Image.core.getcodecstatus(error)
  66. except AttributeError:
  67. msg = ERRORS.get(error)
  68. if not msg:
  69. msg = f"{'encoder' if encoder else 'decoder'} error {error}"
  70. msg += f" when {'writing' if encoder else 'reading'} image file"
  71. return OSError(msg)
  72. def raise_oserror(error: int) -> OSError:
  73. deprecate(
  74. "raise_oserror",
  75. 12,
  76. action="It is only useful for translating error codes returned by a codec's "
  77. "decode() method, which ImageFile already does automatically.",
  78. )
  79. raise _get_oserror(error, encoder=False)
  80. def _tilesort(t: _Tile) -> int:
  81. # sort on offset
  82. return t[2]
  83. class _Tile(NamedTuple):
  84. codec_name: str
  85. extents: tuple[int, int, int, int] | None
  86. offset: int = 0
  87. args: tuple[Any, ...] | str | None = None
  88. #
  89. # --------------------------------------------------------------------
  90. # ImageFile base class
  91. class ImageFile(Image.Image):
  92. """Base class for image file format handlers."""
  93. def __init__(
  94. self, fp: StrOrBytesPath | IO[bytes], filename: str | bytes | None = None
  95. ) -> None:
  96. super().__init__()
  97. self._min_frame = 0
  98. self.custom_mimetype: str | None = None
  99. self.tile: list[_Tile] = []
  100. """ A list of tile descriptors """
  101. self.readonly = 1 # until we know better
  102. self.decoderconfig: tuple[Any, ...] = ()
  103. self.decodermaxblock = MAXBLOCK
  104. if is_path(fp):
  105. # filename
  106. self.fp = open(fp, "rb")
  107. self.filename = os.fspath(fp)
  108. self._exclusive_fp = True
  109. else:
  110. # stream
  111. self.fp = cast(IO[bytes], fp)
  112. self.filename = filename if filename is not None else ""
  113. # can be overridden
  114. self._exclusive_fp = False
  115. try:
  116. try:
  117. self._open()
  118. except (
  119. IndexError, # end of data
  120. TypeError, # end of data (ord)
  121. KeyError, # unsupported mode
  122. EOFError, # got header but not the first frame
  123. struct.error,
  124. ) as v:
  125. raise SyntaxError(v) from v
  126. if not self.mode or self.size[0] <= 0 or self.size[1] <= 0:
  127. msg = "not identified by this driver"
  128. raise SyntaxError(msg)
  129. except BaseException:
  130. # close the file only if we have opened it this constructor
  131. if self._exclusive_fp:
  132. self.fp.close()
  133. raise
  134. def _open(self) -> None:
  135. pass
  136. def _close_fp(self):
  137. if getattr(self, "_fp", False) and not isinstance(self._fp, DeferredError):
  138. if self._fp != self.fp:
  139. self._fp.close()
  140. self._fp = DeferredError(ValueError("Operation on closed image"))
  141. if self.fp:
  142. self.fp.close()
  143. def close(self) -> None:
  144. """
  145. Closes the file pointer, if possible.
  146. This operation will destroy the image core and release its memory.
  147. The image data will be unusable afterward.
  148. This function is required to close images that have multiple frames or
  149. have not had their file read and closed by the
  150. :py:meth:`~PIL.Image.Image.load` method. See :ref:`file-handling` for
  151. more information.
  152. """
  153. try:
  154. self._close_fp()
  155. self.fp = None
  156. except Exception as msg:
  157. logger.debug("Error closing: %s", msg)
  158. super().close()
  159. def get_child_images(self) -> list[ImageFile]:
  160. child_images = []
  161. exif = self.getexif()
  162. ifds = []
  163. if ExifTags.Base.SubIFDs in exif:
  164. subifd_offsets = exif[ExifTags.Base.SubIFDs]
  165. if subifd_offsets:
  166. if not isinstance(subifd_offsets, tuple):
  167. subifd_offsets = (subifd_offsets,)
  168. for subifd_offset in subifd_offsets:
  169. ifds.append((exif._get_ifd_dict(subifd_offset), subifd_offset))
  170. ifd1 = exif.get_ifd(ExifTags.IFD.IFD1)
  171. if ifd1 and ifd1.get(ExifTags.Base.JpegIFOffset):
  172. assert exif._info is not None
  173. ifds.append((ifd1, exif._info.next))
  174. offset = None
  175. for ifd, ifd_offset in ifds:
  176. assert self.fp is not None
  177. current_offset = self.fp.tell()
  178. if offset is None:
  179. offset = current_offset
  180. fp = self.fp
  181. if ifd is not None:
  182. thumbnail_offset = ifd.get(ExifTags.Base.JpegIFOffset)
  183. if thumbnail_offset is not None:
  184. thumbnail_offset += getattr(self, "_exif_offset", 0)
  185. self.fp.seek(thumbnail_offset)
  186. length = ifd.get(ExifTags.Base.JpegIFByteCount)
  187. assert isinstance(length, int)
  188. data = self.fp.read(length)
  189. fp = io.BytesIO(data)
  190. with Image.open(fp) as im:
  191. from . import TiffImagePlugin
  192. if thumbnail_offset is None and isinstance(
  193. im, TiffImagePlugin.TiffImageFile
  194. ):
  195. im._frame_pos = [ifd_offset]
  196. im._seek(0)
  197. im.load()
  198. child_images.append(im)
  199. if offset is not None:
  200. assert self.fp is not None
  201. self.fp.seek(offset)
  202. return child_images
  203. def get_format_mimetype(self) -> str | None:
  204. if self.custom_mimetype:
  205. return self.custom_mimetype
  206. if self.format is not None:
  207. return Image.MIME.get(self.format.upper())
  208. return None
  209. def __getstate__(self) -> list[Any]:
  210. return super().__getstate__() + [self.filename]
  211. def __setstate__(self, state: list[Any]) -> None:
  212. self.tile = []
  213. if len(state) > 5:
  214. self.filename = state[5]
  215. super().__setstate__(state)
  216. def verify(self) -> None:
  217. """Check file integrity"""
  218. # raise exception if something's wrong. must be called
  219. # directly after open, and closes file when finished.
  220. if self._exclusive_fp:
  221. self.fp.close()
  222. self.fp = None
  223. def load(self) -> Image.core.PixelAccess | None:
  224. """Load image data based on tile list"""
  225. if not self.tile and self._im is None:
  226. msg = "cannot load this image"
  227. raise OSError(msg)
  228. pixel = Image.Image.load(self)
  229. if not self.tile:
  230. return pixel
  231. self.map: mmap.mmap | None = None
  232. use_mmap = self.filename and len(self.tile) == 1
  233. readonly = 0
  234. # look for read/seek overrides
  235. if hasattr(self, "load_read"):
  236. read = self.load_read
  237. # don't use mmap if there are custom read/seek functions
  238. use_mmap = False
  239. else:
  240. read = self.fp.read
  241. if hasattr(self, "load_seek"):
  242. seek = self.load_seek
  243. use_mmap = False
  244. else:
  245. seek = self.fp.seek
  246. if use_mmap:
  247. # try memory mapping
  248. decoder_name, extents, offset, args = self.tile[0]
  249. if isinstance(args, str):
  250. args = (args, 0, 1)
  251. if (
  252. decoder_name == "raw"
  253. and isinstance(args, tuple)
  254. and len(args) >= 3
  255. and args[0] == self.mode
  256. and args[0] in Image._MAPMODES
  257. ):
  258. try:
  259. # use mmap, if possible
  260. import mmap
  261. with open(self.filename) as fp:
  262. self.map = mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ)
  263. if offset + self.size[1] * args[1] > self.map.size():
  264. msg = "buffer is not large enough"
  265. raise OSError(msg)
  266. self.im = Image.core.map_buffer(
  267. self.map, self.size, decoder_name, offset, args
  268. )
  269. readonly = 1
  270. # After trashing self.im,
  271. # we might need to reload the palette data.
  272. if self.palette:
  273. self.palette.dirty = 1
  274. except (AttributeError, OSError, ImportError):
  275. self.map = None
  276. self.load_prepare()
  277. err_code = -3 # initialize to unknown error
  278. if not self.map:
  279. # sort tiles in file order
  280. self.tile.sort(key=_tilesort)
  281. # FIXME: This is a hack to handle TIFF's JpegTables tag.
  282. prefix = getattr(self, "tile_prefix", b"")
  283. # Remove consecutive duplicates that only differ by their offset
  284. self.tile = [
  285. list(tiles)[-1]
  286. for _, tiles in itertools.groupby(
  287. self.tile, lambda tile: (tile[0], tile[1], tile[3])
  288. )
  289. ]
  290. for i, (decoder_name, extents, offset, args) in enumerate(self.tile):
  291. seek(offset)
  292. decoder = Image._getdecoder(
  293. self.mode, decoder_name, args, self.decoderconfig
  294. )
  295. try:
  296. decoder.setimage(self.im, extents)
  297. if decoder.pulls_fd:
  298. decoder.setfd(self.fp)
  299. err_code = decoder.decode(b"")[1]
  300. else:
  301. b = prefix
  302. while True:
  303. read_bytes = self.decodermaxblock
  304. if i + 1 < len(self.tile):
  305. next_offset = self.tile[i + 1].offset
  306. if next_offset > offset:
  307. read_bytes = next_offset - offset
  308. try:
  309. s = read(read_bytes)
  310. except (IndexError, struct.error) as e:
  311. # truncated png/gif
  312. if LOAD_TRUNCATED_IMAGES:
  313. break
  314. else:
  315. msg = "image file is truncated"
  316. raise OSError(msg) from e
  317. if not s: # truncated jpeg
  318. if LOAD_TRUNCATED_IMAGES:
  319. break
  320. else:
  321. msg = (
  322. "image file is truncated "
  323. f"({len(b)} bytes not processed)"
  324. )
  325. raise OSError(msg)
  326. b = b + s
  327. n, err_code = decoder.decode(b)
  328. if n < 0:
  329. break
  330. b = b[n:]
  331. finally:
  332. # Need to cleanup here to prevent leaks
  333. decoder.cleanup()
  334. self.tile = []
  335. self.readonly = readonly
  336. self.load_end()
  337. if self._exclusive_fp and self._close_exclusive_fp_after_loading:
  338. self.fp.close()
  339. self.fp = None
  340. if not self.map and not LOAD_TRUNCATED_IMAGES and err_code < 0:
  341. # still raised if decoder fails to return anything
  342. raise _get_oserror(err_code, encoder=False)
  343. return Image.Image.load(self)
  344. def load_prepare(self) -> None:
  345. # create image memory if necessary
  346. if self._im is None:
  347. self.im = Image.core.new(self.mode, self.size)
  348. # create palette (optional)
  349. if self.mode == "P":
  350. Image.Image.load(self)
  351. def load_end(self) -> None:
  352. # may be overridden
  353. pass
  354. # may be defined for contained formats
  355. # def load_seek(self, pos: int) -> None:
  356. # pass
  357. # may be defined for blocked formats (e.g. PNG)
  358. # def load_read(self, read_bytes: int) -> bytes:
  359. # pass
  360. def _seek_check(self, frame: int) -> bool:
  361. if (
  362. frame < self._min_frame
  363. # Only check upper limit on frames if additional seek operations
  364. # are not required to do so
  365. or (
  366. not (hasattr(self, "_n_frames") and self._n_frames is None)
  367. and frame >= getattr(self, "n_frames") + self._min_frame
  368. )
  369. ):
  370. msg = "attempt to seek outside sequence"
  371. raise EOFError(msg)
  372. return self.tell() != frame
  373. class StubHandler(abc.ABC):
  374. def open(self, im: StubImageFile) -> None:
  375. pass
  376. @abc.abstractmethod
  377. def load(self, im: StubImageFile) -> Image.Image:
  378. pass
  379. class StubImageFile(ImageFile, metaclass=abc.ABCMeta):
  380. """
  381. Base class for stub image loaders.
  382. A stub loader is an image loader that can identify files of a
  383. certain format, but relies on external code to load the file.
  384. """
  385. @abc.abstractmethod
  386. def _open(self) -> None:
  387. pass
  388. def load(self) -> Image.core.PixelAccess | None:
  389. loader = self._load()
  390. if loader is None:
  391. msg = f"cannot find loader for this {self.format} file"
  392. raise OSError(msg)
  393. image = loader.load(self)
  394. assert image is not None
  395. # become the other object (!)
  396. self.__class__ = image.__class__ # type: ignore[assignment]
  397. self.__dict__ = image.__dict__
  398. return image.load()
  399. @abc.abstractmethod
  400. def _load(self) -> StubHandler | None:
  401. """(Hook) Find actual image loader."""
  402. pass
  403. class Parser:
  404. """
  405. Incremental image parser. This class implements the standard
  406. feed/close consumer interface.
  407. """
  408. incremental = None
  409. image: Image.Image | None = None
  410. data: bytes | None = None
  411. decoder: Image.core.ImagingDecoder | PyDecoder | None = None
  412. offset = 0
  413. finished = 0
  414. def reset(self) -> None:
  415. """
  416. (Consumer) Reset the parser. Note that you can only call this
  417. method immediately after you've created a parser; parser
  418. instances cannot be reused.
  419. """
  420. assert self.data is None, "cannot reuse parsers"
  421. def feed(self, data: bytes) -> None:
  422. """
  423. (Consumer) Feed data to the parser.
  424. :param data: A string buffer.
  425. :exception OSError: If the parser failed to parse the image file.
  426. """
  427. # collect data
  428. if self.finished:
  429. return
  430. if self.data is None:
  431. self.data = data
  432. else:
  433. self.data = self.data + data
  434. # parse what we have
  435. if self.decoder:
  436. if self.offset > 0:
  437. # skip header
  438. skip = min(len(self.data), self.offset)
  439. self.data = self.data[skip:]
  440. self.offset = self.offset - skip
  441. if self.offset > 0 or not self.data:
  442. return
  443. n, e = self.decoder.decode(self.data)
  444. if n < 0:
  445. # end of stream
  446. self.data = None
  447. self.finished = 1
  448. if e < 0:
  449. # decoding error
  450. self.image = None
  451. raise _get_oserror(e, encoder=False)
  452. else:
  453. # end of image
  454. return
  455. self.data = self.data[n:]
  456. elif self.image:
  457. # if we end up here with no decoder, this file cannot
  458. # be incrementally parsed. wait until we've gotten all
  459. # available data
  460. pass
  461. else:
  462. # attempt to open this file
  463. try:
  464. with io.BytesIO(self.data) as fp:
  465. im = Image.open(fp)
  466. except OSError:
  467. pass # not enough data
  468. else:
  469. flag = hasattr(im, "load_seek") or hasattr(im, "load_read")
  470. if flag or len(im.tile) != 1:
  471. # custom load code, or multiple tiles
  472. self.decode = None
  473. else:
  474. # initialize decoder
  475. im.load_prepare()
  476. d, e, o, a = im.tile[0]
  477. im.tile = []
  478. self.decoder = Image._getdecoder(im.mode, d, a, im.decoderconfig)
  479. self.decoder.setimage(im.im, e)
  480. # calculate decoder offset
  481. self.offset = o
  482. if self.offset <= len(self.data):
  483. self.data = self.data[self.offset :]
  484. self.offset = 0
  485. self.image = im
  486. def __enter__(self) -> Parser:
  487. return self
  488. def __exit__(self, *args: object) -> None:
  489. self.close()
  490. def close(self) -> Image.Image:
  491. """
  492. (Consumer) Close the stream.
  493. :returns: An image object.
  494. :exception OSError: If the parser failed to parse the image file either
  495. because it cannot be identified or cannot be
  496. decoded.
  497. """
  498. # finish decoding
  499. if self.decoder:
  500. # get rid of what's left in the buffers
  501. self.feed(b"")
  502. self.data = self.decoder = None
  503. if not self.finished:
  504. msg = "image was incomplete"
  505. raise OSError(msg)
  506. if not self.image:
  507. msg = "cannot parse this image"
  508. raise OSError(msg)
  509. if self.data:
  510. # incremental parsing not possible; reopen the file
  511. # not that we have all data
  512. with io.BytesIO(self.data) as fp:
  513. try:
  514. self.image = Image.open(fp)
  515. finally:
  516. self.image.load()
  517. return self.image
  518. # --------------------------------------------------------------------
  519. def _save(im: Image.Image, fp: IO[bytes], tile: list[_Tile], bufsize: int = 0) -> None:
  520. """Helper to save image based on tile list
  521. :param im: Image object.
  522. :param fp: File object.
  523. :param tile: Tile list.
  524. :param bufsize: Optional buffer size
  525. """
  526. im.load()
  527. if not hasattr(im, "encoderconfig"):
  528. im.encoderconfig = ()
  529. tile.sort(key=_tilesort)
  530. # FIXME: make MAXBLOCK a configuration parameter
  531. # It would be great if we could have the encoder specify what it needs
  532. # But, it would need at least the image size in most cases. RawEncode is
  533. # a tricky case.
  534. bufsize = max(MAXBLOCK, bufsize, im.size[0] * 4) # see RawEncode.c
  535. try:
  536. fh = fp.fileno()
  537. fp.flush()
  538. _encode_tile(im, fp, tile, bufsize, fh)
  539. except (AttributeError, io.UnsupportedOperation) as exc:
  540. _encode_tile(im, fp, tile, bufsize, None, exc)
  541. if hasattr(fp, "flush"):
  542. fp.flush()
  543. def _encode_tile(
  544. im: Image.Image,
  545. fp: IO[bytes],
  546. tile: list[_Tile],
  547. bufsize: int,
  548. fh: int | None,
  549. exc: BaseException | None = None,
  550. ) -> None:
  551. for encoder_name, extents, offset, args in tile:
  552. if offset > 0:
  553. fp.seek(offset)
  554. encoder = Image._getencoder(im.mode, encoder_name, args, im.encoderconfig)
  555. try:
  556. encoder.setimage(im.im, extents)
  557. if encoder.pushes_fd:
  558. encoder.setfd(fp)
  559. errcode = encoder.encode_to_pyfd()[1]
  560. else:
  561. if exc:
  562. # compress to Python file-compatible object
  563. while True:
  564. errcode, data = encoder.encode(bufsize)[1:]
  565. fp.write(data)
  566. if errcode:
  567. break
  568. else:
  569. # slight speedup: compress to real file object
  570. assert fh is not None
  571. errcode = encoder.encode_to_file(fh, bufsize)
  572. if errcode < 0:
  573. raise _get_oserror(errcode, encoder=True) from exc
  574. finally:
  575. encoder.cleanup()
  576. def _safe_read(fp: IO[bytes], size: int) -> bytes:
  577. """
  578. Reads large blocks in a safe way. Unlike fp.read(n), this function
  579. doesn't trust the user. If the requested size is larger than
  580. SAFEBLOCK, the file is read block by block.
  581. :param fp: File handle. Must implement a <b>read</b> method.
  582. :param size: Number of bytes to read.
  583. :returns: A string containing <i>size</i> bytes of data.
  584. Raises an OSError if the file is truncated and the read cannot be completed
  585. """
  586. if size <= 0:
  587. return b""
  588. if size <= SAFEBLOCK:
  589. data = fp.read(size)
  590. if len(data) < size:
  591. msg = "Truncated File Read"
  592. raise OSError(msg)
  593. return data
  594. blocks: list[bytes] = []
  595. remaining_size = size
  596. while remaining_size > 0:
  597. block = fp.read(min(remaining_size, SAFEBLOCK))
  598. if not block:
  599. break
  600. blocks.append(block)
  601. remaining_size -= len(block)
  602. if sum(len(block) for block in blocks) < size:
  603. msg = "Truncated File Read"
  604. raise OSError(msg)
  605. return b"".join(blocks)
  606. class PyCodecState:
  607. def __init__(self) -> None:
  608. self.xsize = 0
  609. self.ysize = 0
  610. self.xoff = 0
  611. self.yoff = 0
  612. def extents(self) -> tuple[int, int, int, int]:
  613. return self.xoff, self.yoff, self.xoff + self.xsize, self.yoff + self.ysize
  614. class PyCodec:
  615. fd: IO[bytes] | None
  616. def __init__(self, mode: str, *args: Any) -> None:
  617. self.im: Image.core.ImagingCore | None = None
  618. self.state = PyCodecState()
  619. self.fd = None
  620. self.mode = mode
  621. self.init(args)
  622. def init(self, args: tuple[Any, ...]) -> None:
  623. """
  624. Override to perform codec specific initialization
  625. :param args: Tuple of arg items from the tile entry
  626. :returns: None
  627. """
  628. self.args = args
  629. def cleanup(self) -> None:
  630. """
  631. Override to perform codec specific cleanup
  632. :returns: None
  633. """
  634. pass
  635. def setfd(self, fd: IO[bytes]) -> None:
  636. """
  637. Called from ImageFile to set the Python file-like object
  638. :param fd: A Python file-like object
  639. :returns: None
  640. """
  641. self.fd = fd
  642. def setimage(
  643. self,
  644. im: Image.core.ImagingCore,
  645. extents: tuple[int, int, int, int] | None = None,
  646. ) -> None:
  647. """
  648. Called from ImageFile to set the core output image for the codec
  649. :param im: A core image object
  650. :param extents: a 4 tuple of (x0, y0, x1, y1) defining the rectangle
  651. for this tile
  652. :returns: None
  653. """
  654. # following c code
  655. self.im = im
  656. if extents:
  657. (x0, y0, x1, y1) = extents
  658. else:
  659. (x0, y0, x1, y1) = (0, 0, 0, 0)
  660. if x0 == 0 and x1 == 0:
  661. self.state.xsize, self.state.ysize = self.im.size
  662. else:
  663. self.state.xoff = x0
  664. self.state.yoff = y0
  665. self.state.xsize = x1 - x0
  666. self.state.ysize = y1 - y0
  667. if self.state.xsize <= 0 or self.state.ysize <= 0:
  668. msg = "Size cannot be negative"
  669. raise ValueError(msg)
  670. if (
  671. self.state.xsize + self.state.xoff > self.im.size[0]
  672. or self.state.ysize + self.state.yoff > self.im.size[1]
  673. ):
  674. msg = "Tile cannot extend outside image"
  675. raise ValueError(msg)
  676. class PyDecoder(PyCodec):
  677. """
  678. Python implementation of a format decoder. Override this class and
  679. add the decoding logic in the :meth:`decode` method.
  680. See :ref:`Writing Your Own File Codec in Python<file-codecs-py>`
  681. """
  682. _pulls_fd = False
  683. @property
  684. def pulls_fd(self) -> bool:
  685. return self._pulls_fd
  686. def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
  687. """
  688. Override to perform the decoding process.
  689. :param buffer: A bytes object with the data to be decoded.
  690. :returns: A tuple of ``(bytes consumed, errcode)``.
  691. If finished with decoding return -1 for the bytes consumed.
  692. Err codes are from :data:`.ImageFile.ERRORS`.
  693. """
  694. msg = "unavailable in base decoder"
  695. raise NotImplementedError(msg)
  696. def set_as_raw(
  697. self, data: bytes, rawmode: str | None = None, extra: tuple[Any, ...] = ()
  698. ) -> None:
  699. """
  700. Convenience method to set the internal image from a stream of raw data
  701. :param data: Bytes to be set
  702. :param rawmode: The rawmode to be used for the decoder.
  703. If not specified, it will default to the mode of the image
  704. :param extra: Extra arguments for the decoder.
  705. :returns: None
  706. """
  707. if not rawmode:
  708. rawmode = self.mode
  709. d = Image._getdecoder(self.mode, "raw", rawmode, extra)
  710. assert self.im is not None
  711. d.setimage(self.im, self.state.extents())
  712. s = d.decode(data)
  713. if s[0] >= 0:
  714. msg = "not enough image data"
  715. raise ValueError(msg)
  716. if s[1] != 0:
  717. msg = "cannot decode image data"
  718. raise ValueError(msg)
  719. class PyEncoder(PyCodec):
  720. """
  721. Python implementation of a format encoder. Override this class and
  722. add the decoding logic in the :meth:`encode` method.
  723. See :ref:`Writing Your Own File Codec in Python<file-codecs-py>`
  724. """
  725. _pushes_fd = False
  726. @property
  727. def pushes_fd(self) -> bool:
  728. return self._pushes_fd
  729. def encode(self, bufsize: int) -> tuple[int, int, bytes]:
  730. """
  731. Override to perform the encoding process.
  732. :param bufsize: Buffer size.
  733. :returns: A tuple of ``(bytes encoded, errcode, bytes)``.
  734. If finished with encoding return 1 for the error code.
  735. Err codes are from :data:`.ImageFile.ERRORS`.
  736. """
  737. msg = "unavailable in base encoder"
  738. raise NotImplementedError(msg)
  739. def encode_to_pyfd(self) -> tuple[int, int]:
  740. """
  741. If ``pushes_fd`` is ``True``, then this method will be used,
  742. and ``encode()`` will only be called once.
  743. :returns: A tuple of ``(bytes consumed, errcode)``.
  744. Err codes are from :data:`.ImageFile.ERRORS`.
  745. """
  746. if not self.pushes_fd:
  747. return 0, -8 # bad configuration
  748. bytes_consumed, errcode, data = self.encode(0)
  749. if data:
  750. assert self.fd is not None
  751. self.fd.write(data)
  752. return bytes_consumed, errcode
  753. def encode_to_file(self, fh: int, bufsize: int) -> int:
  754. """
  755. :param fh: File handle.
  756. :param bufsize: Buffer size.
  757. :returns: If finished successfully, return 0.
  758. Otherwise, return an error code. Err codes are from
  759. :data:`.ImageFile.ERRORS`.
  760. """
  761. errcode = 0
  762. while errcode == 0:
  763. status, errcode, buf = self.encode(bufsize)
  764. if status > 0:
  765. os.write(fh, buf[status:])
  766. return errcode