ImageFile.py 29 KB

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