Pillow/src/PIL/ImageFile.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

833 lines
26 KiB
Python
Raw Normal View History

2010-07-31 06:52:47 +04:00
#
# The Python Imaging Library.
# $Id$
#
# base class for image file handlers
#
# history:
# 1995-09-09 fl Created
# 1996-03-11 fl Fixed load mechanism.
# 1996-04-15 fl Added pcx/xbm decoders.
# 1996-04-30 fl Added encoders.
# 1996-12-14 fl Added load helpers
# 1997-01-11 fl Use encode_to_file where possible
# 1997-08-27 fl Flush output in _save
# 1998-03-05 fl Use memory mapping for some modes
# 1999-02-04 fl Use memory mapping also for "I;16" and "I;16B"
# 1999-05-31 fl Added image parser
# 2000-10-12 fl Set readonly flag on memory-mapped images
# 2002-03-20 fl Use better messages for common decoder errors
# 2003-04-21 fl Fall back on mmap/map_buffer if map is not available
# 2003-10-30 fl Added StubImageFile class
# 2004-02-25 fl Made incremental parser more robust
#
# Copyright (c) 1997-2004 by Secret Labs AB
# Copyright (c) 1995-2004 by Fredrik Lundh
#
# See the README file for information on usage and redistribution.
#
2023-10-12 19:09:28 +03:00
from __future__ import annotations
2010-07-31 06:52:47 +04:00
2024-05-18 09:06:50 +03:00
import abc
import io
import itertools
2024-08-21 01:05:02 +03:00
import os
2015-07-31 01:20:51 +03:00
import struct
import sys
2024-08-29 15:15:43 +03:00
from typing import IO, TYPE_CHECKING, Any, NamedTuple, cast
from . import Image
from ._deprecate import deprecate
from ._util import is_path
2019-03-12 02:27:43 +03:00
2024-08-29 15:51:15 +03:00
if TYPE_CHECKING:
from ._typing import StrOrBytesPath
2010-07-31 06:52:47 +04:00
MAXBLOCK = 65536
SAFEBLOCK = 1024 * 1024
LOAD_TRUNCATED_IMAGES = False
2020-09-01 20:16:46 +03:00
"""Whether or not to load truncated image files. User code may change this."""
2010-07-31 06:52:47 +04:00
ERRORS = {
-1: "image buffer overrun error",
-2: "decoding error",
-3: "unknown error",
-8: "bad configuration",
-9: "out of memory error",
}
2022-02-25 08:07:01 +03:00
"""
Dict of known error codes returned from :meth:`.PyDecoder.decode`,
:meth:`.PyEncoder.encode` :meth:`.PyEncoder.encode_to_pyfd` and
:meth:`.PyEncoder.encode_to_file`.
"""
2010-07-31 06:52:47 +04:00
2014-08-26 17:47:10 +04:00
#
# --------------------------------------------------------------------
# Helpers
2024-06-24 14:04:33 +03:00
def _get_oserror(error: int, *, encoder: bool) -> OSError:
2010-07-31 06:52:47 +04:00
try:
msg = Image.core.getcodecstatus(error)
2010-07-31 06:52:47 +04:00
except AttributeError:
msg = ERRORS.get(error)
if not msg:
msg = f"{'encoder' if encoder else 'decoder'} error {error}"
msg += f" when {'writing' if encoder else 'reading'} image file"
return OSError(msg)
2024-06-24 14:04:33 +03:00
def raise_oserror(error: int) -> OSError:
deprecate(
"raise_oserror",
12,
action="It is only useful for translating error codes returned by a codec's "
"decode() method, which ImageFile already does automatically.",
)
raise _get_oserror(error, encoder=False)
2010-07-31 06:52:47 +04:00
2014-08-26 17:47:10 +04:00
2024-07-30 13:20:09 +03:00
def _tilesort(t: _Tile) -> int:
2010-07-31 06:52:47 +04:00
# sort on offset
return t[2]
2010-07-31 06:52:47 +04:00
2014-08-26 17:47:10 +04:00
2023-12-01 17:24:29 +03:00
class _Tile(NamedTuple):
2024-02-02 15:54:31 +03:00
codec_name: str
extents: tuple[int, int, int, int] | None
2023-12-01 17:24:29 +03:00
offset: int
2023-12-27 02:41:30 +03:00
args: tuple[Any, ...] | str | None
2023-12-01 17:24:29 +03:00
2010-07-31 06:52:47 +04:00
#
# --------------------------------------------------------------------
# ImageFile base class
2019-03-21 16:28:20 +03:00
2010-07-31 06:52:47 +04:00
class ImageFile(Image.Image):
"""Base class for image file format handlers."""
2010-07-31 06:52:47 +04:00
2024-08-29 15:51:15 +03:00
def __init__(
2024-08-29 15:15:43 +03:00
self, fp: StrOrBytesPath | IO[bytes], filename: str | bytes | None = None
2024-08-29 15:51:15 +03:00
) -> None:
super().__init__()
2010-07-31 06:52:47 +04:00
2017-09-06 06:23:50 +03:00
self._min_frame = 0
2024-08-29 15:51:15 +03:00
self.custom_mimetype: str | None = None
2024-08-29 15:51:15 +03:00
self.tile: list[_Tile] = []
2020-07-23 15:40:02 +03:00
""" A list of tile descriptors, or ``None`` """
2010-07-31 06:52:47 +04:00
self.readonly = 1 # until we know better
2024-08-29 15:51:15 +03:00
self.decoderconfig: tuple[Any, ...] = ()
2010-07-31 06:52:47 +04:00
self.decodermaxblock = MAXBLOCK
if is_path(fp):
2010-07-31 06:52:47 +04:00
# filename
self.fp = open(fp, "rb")
2024-08-29 15:51:15 +03:00
self.filename = os.path.realpath(os.fspath(fp))
2016-07-17 06:29:36 +03:00
self._exclusive_fp = True
2010-07-31 06:52:47 +04:00
else:
# stream
2024-08-29 15:15:43 +03:00
self.fp = cast(IO[bytes], fp)
2024-08-29 15:51:15 +03:00
self.filename = filename if filename is not None else ""
# can be overridden
2024-08-29 15:51:15 +03:00
self._exclusive_fp = False
2010-07-31 06:52:47 +04:00
try:
Improve handling of file resources Follow Python's file object semantics. User code is responsible for closing resources (usually through a context manager) in a deterministic way. To achieve this, remove __del__ functions. These functions used to closed open file handlers in an attempt to silence Python ResourceWarnings. However, using __del__ has the following drawbacks: - __del__ isn't called until the object's reference count reaches 0. Therefore, resource handlers remain open or in use longer than necessary. - The __del__ method isn't guaranteed to execute on system exit. See the Python documentation: https://docs.python.org/3/reference/datamodel.html#object.__del__ > It is not guaranteed that __del__() methods are called for objects > that still exist when the interpreter exits. - Exceptions that occur inside __del__ are ignored instead of raised. This has the potential of hiding bugs. This is also in the Python documentation: > Warning: Due to the precarious circumstances under which __del__() > methods are invoked, exceptions that occur during their execution > are ignored, and a warning is printed to sys.stderr instead. Instead, always close resource handlers when they are no longer in use. This will close the file handler at a specified point in the user's code and not wait until the interpreter chooses to. It is always guaranteed to run. And, if an exception occurs while closing the file handler, the bug will not be ignored. Now, when code receives a ResourceWarning, it will highlight an area that is mishandling resources. It should not simply be silenced, but fixed by closing resources with a context manager. All warnings that were emitted during tests have been cleaned up. To enable warnings, I passed the `-Wa` CLI option to Python. This exposed some mishandling of resources in ImageFile.__init__() and SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
try:
self._open()
except (
IndexError, # end of data
TypeError, # end of data (ord)
KeyError, # unsupported mode
EOFError, # got header but not the first frame
struct.error,
) as v:
raise SyntaxError(v) from v
Improve handling of file resources Follow Python's file object semantics. User code is responsible for closing resources (usually through a context manager) in a deterministic way. To achieve this, remove __del__ functions. These functions used to closed open file handlers in an attempt to silence Python ResourceWarnings. However, using __del__ has the following drawbacks: - __del__ isn't called until the object's reference count reaches 0. Therefore, resource handlers remain open or in use longer than necessary. - The __del__ method isn't guaranteed to execute on system exit. See the Python documentation: https://docs.python.org/3/reference/datamodel.html#object.__del__ > It is not guaranteed that __del__() methods are called for objects > that still exist when the interpreter exits. - Exceptions that occur inside __del__ are ignored instead of raised. This has the potential of hiding bugs. This is also in the Python documentation: > Warning: Due to the precarious circumstances under which __del__() > methods are invoked, exceptions that occur during their execution > are ignored, and a warning is printed to sys.stderr instead. Instead, always close resource handlers when they are no longer in use. This will close the file handler at a specified point in the user's code and not wait until the interpreter chooses to. It is always guaranteed to run. And, if an exception occurs while closing the file handler, the bug will not be ignored. Now, when code receives a ResourceWarning, it will highlight an area that is mishandling resources. It should not simply be silenced, but fixed by closing resources with a context manager. All warnings that were emitted during tests have been cleaned up. To enable warnings, I passed the `-Wa` CLI option to Python. This exposed some mishandling of resources in ImageFile.__init__() and SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
if not self.mode or self.size[0] <= 0 or self.size[1] <= 0:
msg = "not identified by this driver"
raise SyntaxError(msg)
Improve handling of file resources Follow Python's file object semantics. User code is responsible for closing resources (usually through a context manager) in a deterministic way. To achieve this, remove __del__ functions. These functions used to closed open file handlers in an attempt to silence Python ResourceWarnings. However, using __del__ has the following drawbacks: - __del__ isn't called until the object's reference count reaches 0. Therefore, resource handlers remain open or in use longer than necessary. - The __del__ method isn't guaranteed to execute on system exit. See the Python documentation: https://docs.python.org/3/reference/datamodel.html#object.__del__ > It is not guaranteed that __del__() methods are called for objects > that still exist when the interpreter exits. - Exceptions that occur inside __del__ are ignored instead of raised. This has the potential of hiding bugs. This is also in the Python documentation: > Warning: Due to the precarious circumstances under which __del__() > methods are invoked, exceptions that occur during their execution > are ignored, and a warning is printed to sys.stderr instead. Instead, always close resource handlers when they are no longer in use. This will close the file handler at a specified point in the user's code and not wait until the interpreter chooses to. It is always guaranteed to run. And, if an exception occurs while closing the file handler, the bug will not be ignored. Now, when code receives a ResourceWarning, it will highlight an area that is mishandling resources. It should not simply be silenced, but fixed by closing resources with a context manager. All warnings that were emitted during tests have been cleaned up. To enable warnings, I passed the `-Wa` CLI option to Python. This exposed some mishandling of resources in ImageFile.__init__() and SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
except BaseException:
2016-07-17 06:29:36 +03:00
# close the file only if we have opened it this constructor
if self._exclusive_fp:
self.fp.close()
Improve handling of file resources Follow Python's file object semantics. User code is responsible for closing resources (usually through a context manager) in a deterministic way. To achieve this, remove __del__ functions. These functions used to closed open file handlers in an attempt to silence Python ResourceWarnings. However, using __del__ has the following drawbacks: - __del__ isn't called until the object's reference count reaches 0. Therefore, resource handlers remain open or in use longer than necessary. - The __del__ method isn't guaranteed to execute on system exit. See the Python documentation: https://docs.python.org/3/reference/datamodel.html#object.__del__ > It is not guaranteed that __del__() methods are called for objects > that still exist when the interpreter exits. - Exceptions that occur inside __del__ are ignored instead of raised. This has the potential of hiding bugs. This is also in the Python documentation: > Warning: Due to the precarious circumstances under which __del__() > methods are invoked, exceptions that occur during their execution > are ignored, and a warning is printed to sys.stderr instead. Instead, always close resource handlers when they are no longer in use. This will close the file handler at a specified point in the user's code and not wait until the interpreter chooses to. It is always guaranteed to run. And, if an exception occurs while closing the file handler, the bug will not be ignored. Now, when code receives a ResourceWarning, it will highlight an area that is mishandling resources. It should not simply be silenced, but fixed by closing resources with a context manager. All warnings that were emitted during tests have been cleaned up. To enable warnings, I passed the `-Wa` CLI option to Python. This exposed some mishandling of resources in ImageFile.__init__() and SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
raise
2010-07-31 06:52:47 +04:00
2024-08-29 15:51:15 +03:00
def _open(self) -> None:
pass
2024-06-24 14:04:33 +03:00
def get_format_mimetype(self) -> str | None:
2019-01-05 23:00:00 +03:00
if self.custom_mimetype:
return self.custom_mimetype
if self.format is not None:
return Image.MIME.get(self.format.upper())
2024-06-24 14:04:33 +03:00
return None
2024-07-30 13:20:09 +03:00
def __setstate__(self, state: list[Any]) -> None:
self.tile = []
super().__setstate__(state)
2024-04-29 13:19:05 +03:00
def verify(self) -> None:
2019-02-03 07:58:24 +03:00
"""Check file integrity"""
2010-07-31 06:52:47 +04:00
# raise exception if something's wrong. must be called
# directly after open, and closes file when finished.
if self._exclusive_fp:
self.fp.close()
2010-07-31 06:52:47 +04:00
self.fp = None
2024-08-05 06:32:59 +03:00
def load(self) -> Image.core.PixelAccess | None:
2019-02-03 07:58:24 +03:00
"""Load image data based on tile list"""
2010-07-31 06:52:47 +04:00
2024-08-29 15:51:15 +03:00
if not self.tile and self._im is None:
msg = "cannot load this image"
raise OSError(msg)
2020-04-17 11:29:45 +03:00
pixel = Image.Image.load(self)
2010-07-31 06:52:47 +04:00
if not self.tile:
return pixel
2024-08-05 06:32:59 +03:00
self.map: mmap.mmap | None = None
use_mmap = self.filename and len(self.tile) == 1
# As of pypy 2.1.0, memory mapping was failing here.
use_mmap = use_mmap and not hasattr(sys, "pypy_version_info")
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
readonly = 0
# look for read/seek overrides
2024-08-05 06:32:59 +03:00
if hasattr(self, "load_read"):
read = self.load_read
# don't use mmap if there are custom read/seek functions
use_mmap = False
2024-08-05 06:32:59 +03:00
else:
read = self.fp.read
2024-08-05 06:32:59 +03:00
if hasattr(self, "load_seek"):
seek = self.load_seek
use_mmap = False
2024-08-05 06:32:59 +03:00
else:
seek = self.fp.seek
if use_mmap:
2010-07-31 06:52:47 +04:00
# try memory mapping
decoder_name, extents, offset, args = self.tile[0]
if isinstance(args, str):
args = (args, 0, 1)
2018-06-24 15:32:25 +03:00
if (
decoder_name == "raw"
2024-08-29 15:51:15 +03:00
and isinstance(args, tuple)
2018-06-24 15:32:25 +03:00
and len(args) >= 3
and args[0] == self.mode
and args[0] in Image._MAPMODES
):
2010-07-31 06:52:47 +04:00
try:
# use mmap, if possible
import mmap
with open(self.filename) as fp:
self.map = mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ)
if offset + self.size[1] * args[1] > self.map.size():
2023-10-19 10:42:41 +03:00
msg = "buffer is not large enough"
raise OSError(msg)
self.im = Image.core.map_buffer(
self.map, self.size, decoder_name, offset, args
)
2010-07-31 06:52:47 +04:00
readonly = 1
2018-06-24 15:32:25 +03:00
# After trashing self.im,
# we might need to reload the palette data.
if self.palette:
self.palette.dirty = 1
except (AttributeError, OSError, ImportError):
2010-07-31 06:52:47 +04:00
self.map = None
self.load_prepare()
err_code = -3 # initialize to unknown error
2010-07-31 06:52:47 +04:00
if not self.map:
# sort tiles in file order
self.tile.sort(key=_tilesort)
2010-07-31 06:52:47 +04:00
2024-08-05 06:32:59 +03:00
# FIXME: This is a hack to handle TIFF's JpegTables tag.
prefix = getattr(self, "tile_prefix", b"")
2010-07-31 06:52:47 +04:00
# Remove consecutive duplicates that only differ by their offset
self.tile = [
list(tiles)[-1]
for _, tiles in itertools.groupby(
self.tile, lambda tile: (tile[0], tile[1], tile[3])
)
]
for decoder_name, extents, offset, args in self.tile:
seek(offset)
decoder = Image._getdecoder(
2017-04-20 14:14:23 +03:00
self.mode, decoder_name, args, self.decoderconfig
)
try:
decoder.setimage(self.im, extents)
if decoder.pulls_fd:
decoder.setfd(self.fp)
2022-03-08 11:51:24 +03:00
err_code = decoder.decode(b"")[1]
else:
b = prefix
while True:
try:
s = read(self.decodermaxblock)
except (IndexError, struct.error) as e:
2018-06-24 15:32:25 +03:00
# truncated png/gif
if LOAD_TRUNCATED_IMAGES:
break
else:
msg = "image file is truncated"
raise OSError(msg) from e
if not s: # truncated jpeg
if LOAD_TRUNCATED_IMAGES:
break
else:
msg = (
"image file is truncated "
f"({len(b)} bytes not processed)"
2018-06-24 15:32:25 +03:00
)
raise OSError(msg)
b = b + s
n, err_code = decoder.decode(b)
if n < 0:
break
b = b[n:]
finally:
# Need to cleanup here to prevent leaks
decoder.cleanup()
2010-07-31 06:52:47 +04:00
self.tile = []
self.readonly = readonly
self.load_end()
if self._exclusive_fp and self._close_exclusive_fp_after_loading:
self.fp.close()
self.fp = None
2010-07-31 06:52:47 +04:00
if not self.map and not LOAD_TRUNCATED_IMAGES and err_code < 0:
2013-01-11 17:40:02 +04:00
# still raised if decoder fails to return anything
raise _get_oserror(err_code, encoder=False)
2010-07-31 06:52:47 +04:00
return Image.Image.load(self)
2024-05-04 13:51:54 +03:00
def load_prepare(self) -> None:
2010-07-31 06:52:47 +04:00
# create image memory if necessary
2024-09-18 13:27:35 +03:00
if self._im is None:
2010-07-31 06:52:47 +04:00
self.im = Image.core.new(self.mode, self.size)
# create palette (optional)
if self.mode == "P":
Image.Image.load(self)
2024-05-04 13:51:54 +03:00
def load_end(self) -> None:
2010-07-31 06:52:47 +04:00
# may be overridden
pass
# may be defined for contained formats
2024-05-15 13:19:09 +03:00
# def load_seek(self, pos: int) -> None:
2010-07-31 06:52:47 +04:00
# pass
# may be defined for blocked formats (e.g. PNG)
2024-05-15 13:19:09 +03:00
# def load_read(self, read_bytes: int) -> bytes:
2010-07-31 06:52:47 +04:00
# pass
2024-07-12 14:16:56 +03:00
def _seek_check(self, frame: int) -> bool:
2017-09-06 06:23:50 +03:00
if (
frame < self._min_frame
# Only check upper limit on frames if additional seek operations
# are not required to do so
or (
not (hasattr(self, "_n_frames") and self._n_frames is None)
2024-07-12 14:16:56 +03:00
and frame >= getattr(self, "n_frames") + self._min_frame
2019-03-21 16:28:20 +03:00
)
2017-09-06 06:23:50 +03:00
):
msg = "attempt to seek outside sequence"
raise EOFError(msg)
return self.tell() != frame
2010-07-31 06:52:47 +04:00
2024-05-18 09:06:50 +03:00
class StubHandler:
def open(self, im: StubImageFile) -> None:
pass
@abc.abstractmethod
def load(self, im: StubImageFile) -> Image.Image:
pass
2010-07-31 06:52:47 +04:00
class StubImageFile(ImageFile):
"""
Base class for stub image loaders.
A stub loader is an image loader that can identify files of a
certain format, but relies on external code to load the file.
"""
2010-07-31 06:52:47 +04:00
2024-06-24 14:04:33 +03:00
def _open(self) -> None:
msg = "StubImageFile subclass must implement _open"
raise NotImplementedError(msg)
2010-07-31 06:52:47 +04:00
2024-07-12 14:16:56 +03:00
def load(self) -> Image.core.PixelAccess | None:
2010-07-31 06:52:47 +04:00
loader = self._load()
if loader is None:
msg = f"cannot find loader for this {self.format} file"
raise OSError(msg)
2010-07-31 06:52:47 +04:00
image = loader.load(self)
assert image is not None
# become the other object (!)
2024-07-12 14:16:56 +03:00
self.__class__ = image.__class__ # type: ignore[assignment]
2010-07-31 06:52:47 +04:00
self.__dict__ = image.__dict__
return image.load()
2010-07-31 06:52:47 +04:00
2024-06-24 14:04:33 +03:00
def _load(self) -> StubHandler | None:
2019-02-03 07:58:24 +03:00
"""(Hook) Find actual image loader."""
msg = "StubImageFile subclass must implement _load"
raise NotImplementedError(msg)
2010-07-31 06:52:47 +04:00
class Parser:
"""
Incremental image parser. This class implements the standard
feed/close consumer interface.
"""
2019-03-21 16:28:20 +03:00
2010-07-31 06:52:47 +04:00
incremental = None
2024-03-02 05:12:17 +03:00
image: Image.Image | None = None
2024-07-12 14:16:56 +03:00
data: bytes | None = None
decoder: Image.core.ImagingDecoder | PyDecoder | None = None
2015-04-24 11:24:52 +03:00
offset = 0
2010-07-31 06:52:47 +04:00
finished = 0
2024-05-04 13:51:54 +03:00
def reset(self) -> None:
"""
(Consumer) Reset the parser. Note that you can only call this
method immediately after you've created a parser; parser
instances cannot be reused.
"""
2010-07-31 06:52:47 +04:00
assert self.data is None, "cannot reuse parsers"
2024-07-12 14:16:56 +03:00
def feed(self, data: bytes) -> None:
"""
(Consumer) Feed data to the parser.
2013-10-13 00:57:27 +04:00
:param data: A string buffer.
:exception OSError: If the parser failed to parse the image file.
"""
2010-07-31 06:52:47 +04:00
# collect data
if self.finished:
return
if self.data is None:
self.data = data
else:
self.data = self.data + data
# parse what we have
if self.decoder:
if self.offset > 0:
# skip header
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
n, e = self.decoder.decode(self.data)
if n < 0:
# end of stream
self.data = None
self.finished = 1
if e < 0:
# decoding error
self.image = None
raise _get_oserror(e, encoder=False)
2010-07-31 06:52:47 +04:00
else:
# end of image
return
self.data = self.data[n:]
elif self.image:
# if we end up here with no decoder, this file cannot
# be incrementally parsed. wait until we've gotten all
# available data
pass
else:
# attempt to open this file
try:
2017-05-13 01:48:03 +03:00
with io.BytesIO(self.data) as fp:
2010-07-31 06:52:47 +04:00
im = Image.open(fp)
except OSError:
2010-07-31 06:52:47 +04:00
pass # not enough data
else:
flag = hasattr(im, "load_seek") or hasattr(im, "load_read")
if flag or len(im.tile) != 1:
# custom load code, or multiple tiles
self.decode = None
else:
# initialize decoder
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)
# calculate decoder offset
self.offset = o
if self.offset <= len(self.data):
self.data = self.data[self.offset :]
self.offset = 0
self.image = im
2024-07-08 13:09:45 +03:00
def __enter__(self) -> Parser:
2017-10-07 15:18:23 +03:00
return self
2024-06-11 16:26:00 +03:00
def __exit__(self, *args: object) -> None:
2017-10-07 15:18:23 +03:00
self.close()
2024-07-12 14:16:56 +03:00
def close(self) -> Image.Image:
"""
(Consumer) Close the stream.
:returns: An image object.
:exception OSError: If the parser failed to parse the image file either
2013-10-13 00:57:27 +04:00
because it cannot be identified or cannot be
decoded.
"""
2010-07-31 06:52:47 +04:00
# finish decoding
if self.decoder:
# get rid of what's left in the buffers
self.feed(b"")
2010-07-31 06:52:47 +04:00
self.data = self.decoder = None
if not self.finished:
msg = "image was incomplete"
raise OSError(msg)
2010-07-31 06:52:47 +04:00
if not self.image:
msg = "cannot parse this image"
raise OSError(msg)
2010-07-31 06:52:47 +04:00
if self.data:
# incremental parsing not possible; reopen the file
# not that we have all data
2017-05-13 01:48:03 +03:00
with io.BytesIO(self.data) as fp:
try:
self.image = Image.open(fp)
finally:
self.image.load()
2010-07-31 06:52:47 +04:00
return self.image
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
# --------------------------------------------------------------------
2019-03-21 16:28:20 +03:00
def _save(im: Image.Image, fp: IO[bytes], tile: list[_Tile], bufsize: int = 0) -> None:
"""Helper to save image based on tile list
:param im: Image object.
:param fp: File object.
:param tile: Tile list.
:param bufsize: Optional buffer size
"""
2010-07-31 06:52:47 +04:00
im.load()
if not hasattr(im, "encoderconfig"):
im.encoderconfig = ()
tile.sort(key=_tilesort)
2010-07-31 06:52:47 +04:00
# FIXME: make MAXBLOCK a configuration parameter
# It would be great if we could have the encoder specify what it needs
# But, it would need at least the image size in most cases. RawEncode is
# a tricky case.
bufsize = max(MAXBLOCK, bufsize, im.size[0] * 4) # see RawEncode.c
2010-07-31 06:52:47 +04:00
try:
fh = fp.fileno()
fp.flush()
2022-07-22 00:59:30 +03:00
_encode_tile(im, fp, tile, bufsize, fh)
except (AttributeError, io.UnsupportedOperation) as exc:
_encode_tile(im, fp, tile, bufsize, None, exc)
if hasattr(fp, "flush"):
fp.flush()
2024-07-15 12:23:36 +03:00
def _encode_tile(
2024-07-30 13:20:09 +03:00
im: Image.Image,
fp: IO[bytes],
tile: list[_Tile],
bufsize: int,
2024-08-21 01:05:02 +03:00
fh: int | None,
2024-07-30 13:20:09 +03:00
exc: BaseException | None = None,
2024-07-15 12:23:36 +03:00
) -> None:
for encoder_name, extents, offset, args in tile:
if offset > 0:
fp.seek(offset)
encoder = Image._getencoder(im.mode, encoder_name, args, im.encoderconfig)
2022-02-28 05:59:52 +03:00
try:
encoder.setimage(im.im, extents)
2022-02-28 05:59:52 +03:00
if encoder.pushes_fd:
encoder.setfd(fp)
errcode = encoder.encode_to_pyfd()[1]
else:
2022-02-28 05:59:52 +03:00
if exc:
# compress to Python file-compatible object
while True:
errcode, data = encoder.encode(bufsize)[1:]
fp.write(data)
if errcode:
break
else:
2022-02-28 05:59:52 +03:00
# slight speedup: compress to real file object
2024-08-21 01:05:02 +03:00
assert fh is not None
errcode = encoder.encode_to_file(fh, bufsize)
if errcode < 0:
raise _get_oserror(errcode, encoder=True) from exc
2022-02-28 05:59:52 +03:00
finally:
encoder.cleanup()
2010-07-31 06:52:47 +04:00
2024-07-08 13:09:45 +03:00
def _safe_read(fp: IO[bytes], size: int) -> bytes:
"""
Reads large blocks in a safe way. Unlike fp.read(n), this function
doesn't trust the user. If the requested size is larger than
SAFEBLOCK, the file is read block by block.
:param fp: File handle. Must implement a <b>read</b> method.
:param size: Number of bytes to read.
:returns: A string containing <i>size</i> bytes of data.
2021-04-01 17:41:46 +03:00
Raises an OSError if the file is truncated and the read cannot be completed
"""
2010-07-31 06:52:47 +04:00
if size <= 0:
return b""
2010-07-31 06:52:47 +04:00
if size <= SAFEBLOCK:
data = fp.read(size)
if len(data) < size:
msg = "Truncated File Read"
raise OSError(msg)
return data
2024-07-08 13:09:45 +03:00
blocks: list[bytes] = []
remaining_size = size
while remaining_size > 0:
block = fp.read(min(remaining_size, SAFEBLOCK))
2010-07-31 06:52:47 +04:00
if not block:
break
2024-07-08 13:09:45 +03:00
blocks.append(block)
remaining_size -= len(block)
2024-07-08 13:09:45 +03:00
if sum(len(block) for block in blocks) < size:
msg = "Truncated File Read"
raise OSError(msg)
2024-07-08 13:09:45 +03:00
return b"".join(blocks)
2016-05-31 11:10:01 +03:00
class PyCodecState:
2024-05-04 13:51:54 +03:00
def __init__(self) -> None:
2016-05-31 11:10:01 +03:00
self.xsize = 0
self.ysize = 0
self.xoff = 0
self.yoff = 0
2024-06-24 14:04:33 +03:00
def extents(self) -> tuple[int, int, int, int]:
2022-04-10 19:25:40 +03:00
return self.xoff, self.yoff, self.xoff + self.xsize, self.yoff + self.ysize
2016-05-31 11:10:01 +03:00
2017-04-20 14:14:23 +03:00
2022-02-25 08:07:01 +03:00
class PyCodec:
2024-02-13 14:26:23 +03:00
fd: IO[bytes] | None
2024-01-17 11:22:45 +03:00
2024-07-12 14:16:56 +03:00
def __init__(self, mode: str, *args: Any) -> None:
self.im: Image.core.ImagingCore | None = None
2016-05-31 11:10:01 +03:00
self.state = PyCodecState()
self.fd = None
self.mode = mode
self.init(args)
2024-07-12 14:16:56 +03:00
def init(self, args: tuple[Any, ...]) -> None:
2016-07-08 12:52:30 +03:00
"""
2022-02-25 08:07:01 +03:00
Override to perform codec specific initialization
2016-07-08 12:52:30 +03:00
2024-07-12 14:16:56 +03:00
:param args: Tuple of arg items from the tile entry
2016-07-08 12:52:30 +03:00
:returns: None
"""
2016-05-31 11:10:01 +03:00
self.args = args
2017-04-20 14:14:23 +03:00
2024-05-04 13:51:54 +03:00
def cleanup(self) -> None:
2016-07-08 12:52:30 +03:00
"""
2022-02-25 08:07:01 +03:00
Override to perform codec specific cleanup
2016-07-08 12:52:30 +03:00
:returns: None
"""
2016-05-31 11:10:01 +03:00
pass
2024-07-15 12:23:36 +03:00
def setfd(self, fd: IO[bytes]) -> None:
2016-07-08 12:52:30 +03:00
"""
2022-02-25 08:07:01 +03:00
Called from ImageFile to set the Python file-like object
2016-07-08 12:52:30 +03:00
2022-02-25 08:07:01 +03:00
:param fd: A Python file-like object
2016-07-08 12:52:30 +03:00
:returns: None
"""
2016-05-31 11:10:01 +03:00
self.fd = fd
2017-04-20 14:14:23 +03:00
2024-07-30 13:20:09 +03:00
def setimage(
self,
im: Image.core.ImagingCore,
extents: tuple[int, int, int, int] | None = None,
) -> None:
2016-07-08 12:52:30 +03:00
"""
2022-02-25 08:07:01 +03:00
Called from ImageFile to set the core output image for the codec
2016-07-08 12:52:30 +03:00
:param im: A core image object
:param extents: a 4 tuple of (x0, y0, x1, y1) defining the rectangle
for this tile
:returns: None
"""
2017-04-20 14:14:23 +03:00
2016-05-31 11:10:01 +03:00
# following c code
self.im = im
if extents:
(x0, y0, x1, y1) = extents
else:
(x0, y0, x1, y1) = (0, 0, 0, 0)
2017-02-23 13:20:25 +03:00
if x0 == 0 and x1 == 0:
2016-05-31 11:10:01 +03:00
self.state.xsize, self.state.ysize = self.im.size
else:
self.state.xoff = x0
self.state.yoff = y0
self.state.xsize = x1 - x0
self.state.ysize = y1 - y0
if self.state.xsize <= 0 or self.state.ysize <= 0:
msg = "Size cannot be negative"
raise ValueError(msg)
2017-04-20 14:14:23 +03:00
2016-05-31 11:10:01 +03:00
if (
self.state.xsize + self.state.xoff > self.im.size[0]
2017-04-20 14:14:23 +03:00
or self.state.ysize + self.state.yoff > self.im.size[1]
):
msg = "Tile cannot extend outside image"
raise ValueError(msg)
2017-04-20 14:14:23 +03:00
2022-02-25 08:07:01 +03:00
class PyDecoder(PyCodec):
"""
Python implementation of a format decoder. Override this class and
add the decoding logic in the :meth:`decode` method.
See :ref:`Writing Your Own File Codec in Python<file-codecs-py>`
"""
_pulls_fd = False
@property
2024-06-24 14:04:33 +03:00
def pulls_fd(self) -> bool:
2022-02-25 08:07:01 +03:00
return self._pulls_fd
2024-09-06 08:16:59 +03:00
def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]:
2022-02-25 08:07:01 +03:00
"""
Override to perform the decoding process.
:param buffer: A bytes object with the data to be decoded.
:returns: A tuple of ``(bytes consumed, errcode)``.
2022-03-08 11:48:58 +03:00
If finished with decoding return -1 for the bytes consumed.
2022-02-25 08:07:01 +03:00
Err codes are from :data:`.ImageFile.ERRORS`.
"""
2023-10-19 10:42:41 +03:00
msg = "unavailable in base decoder"
raise NotImplementedError(msg)
2022-02-25 08:07:01 +03:00
2024-08-31 11:48:16 +03:00
def set_as_raw(
self, data: bytes, rawmode: str | None = None, extra: tuple[Any, ...] = ()
) -> None:
2016-07-08 12:52:30 +03:00
"""
Convenience method to set the internal image from a stream of raw data
:param data: Bytes to be set
2018-06-24 15:32:25 +03:00
:param rawmode: The rawmode to be used for the decoder.
If not specified, it will default to the mode of the image
2024-08-31 11:48:16 +03:00
:param extra: Extra arguments for the decoder.
2016-07-08 12:52:30 +03:00
:returns: None
"""
2017-04-20 14:14:23 +03:00
2016-05-31 11:10:01 +03:00
if not rawmode:
rawmode = self.mode
2024-08-31 11:48:16 +03:00
d = Image._getdecoder(self.mode, "raw", rawmode, extra)
2024-06-24 14:04:33 +03:00
assert self.im is not None
2016-05-31 11:10:01 +03:00
d.setimage(self.im, self.state.extents())
s = d.decode(data)
2017-04-20 14:14:23 +03:00
2016-05-31 11:10:01 +03:00
if s[0] >= 0:
msg = "not enough image data"
raise ValueError(msg)
2016-05-31 11:10:01 +03:00
if s[1] != 0:
msg = "cannot decode image data"
raise ValueError(msg)
2022-02-25 08:07:01 +03:00
class PyEncoder(PyCodec):
"""
Python implementation of a format encoder. Override this class and
add the decoding logic in the :meth:`encode` method.
2022-02-28 00:27:39 +03:00
See :ref:`Writing Your Own File Codec in Python<file-codecs-py>`
2022-02-25 08:07:01 +03:00
"""
_pushes_fd = False
@property
2024-06-24 14:04:33 +03:00
def pushes_fd(self) -> bool:
2022-02-25 08:07:01 +03:00
return self._pushes_fd
2024-06-10 07:15:28 +03:00
def encode(self, bufsize: int) -> tuple[int, int, bytes]:
2022-02-25 08:07:01 +03:00
"""
Override to perform the encoding process.
:param bufsize: Buffer size.
:returns: A tuple of ``(bytes encoded, errcode, bytes)``.
If finished with encoding return 1 for the error code.
Err codes are from :data:`.ImageFile.ERRORS`.
"""
2023-10-19 10:42:41 +03:00
msg = "unavailable in base encoder"
raise NotImplementedError(msg)
2022-02-25 08:07:01 +03:00
2024-06-24 14:04:33 +03:00
def encode_to_pyfd(self) -> tuple[int, int]:
2022-02-25 08:07:01 +03:00
"""
If ``pushes_fd`` is ``True``, then this method will be used,
and ``encode()`` will only be called once.
2022-02-25 08:07:01 +03:00
:returns: A tuple of ``(bytes consumed, errcode)``.
Err codes are from :data:`.ImageFile.ERRORS`.
"""
if not self.pushes_fd:
return 0, -8 # bad configuration
bytes_consumed, errcode, data = self.encode(0)
if data:
2024-06-24 14:04:33 +03:00
assert self.fd is not None
2022-02-25 08:07:01 +03:00
self.fd.write(data)
return bytes_consumed, errcode
2024-08-21 01:05:02 +03:00
def encode_to_file(self, fh: int, bufsize: int) -> int:
2022-02-25 08:07:01 +03:00
"""
:param fh: File handle.
:param bufsize: Buffer size.
:returns: If finished successfully, return 0.
Otherwise, return an error code. Err codes are from
:data:`.ImageFile.ERRORS`.
"""
errcode = 0
while errcode == 0:
status, errcode, buf = self.encode(bufsize)
if status > 0:
2024-08-21 01:05:02 +03:00
os.write(fh, buf[status:])
2022-02-25 08:07:01 +03:00
return errcode