Merge branch 'main' into type_hints

This commit is contained in:
Andrew Murray 2024-05-31 08:06:42 +10:00 committed by GitHub
commit 6affb123c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
110 changed files with 1115 additions and 853 deletions

View File

@ -1 +1 @@
cibuildwheel==2.17.0 cibuildwheel==2.18.1

View File

@ -9,6 +9,7 @@ BinPackParameters: false
BreakBeforeBraces: Attach BreakBeforeBraces: Attach
ColumnLimit: 88 ColumnLimit: 88
DerivePointerAlignment: false DerivePointerAlignment: false
IndentGotoLabels: false
IndentWidth: 4 IndentWidth: 4
Language: Cpp Language: Cpp
PointerAlignment: Right PointerAlignment: Right

View File

@ -12,7 +12,7 @@ elif [ "${AUDITWHEEL_POLICY::9}" == "musllinux" ]; then
else else
yum install -y fribidi yum install -y fribidi
fi fi
if [ "${AUDITWHEEL_POLICY::9}" != "musllinux" ]; then if [ "${AUDITWHEEL_POLICY::9}" != "musllinux" ] && !([[ "$OSTYPE" == "darwin"* ]] && [[ $(python3 --version) == *"3.13."* ]]); then
python3 -m pip install numpy python3 -m pip install numpy
fi fi

View File

@ -46,6 +46,7 @@ jobs:
- cp310 - cp310
- cp311 - cp311
- cp312 - cp312
- cp313
spec: spec:
- manylinux2014 - manylinux2014
- manylinux_2_28 - manylinux_2_28
@ -80,6 +81,7 @@ jobs:
CIBW_ARCHS: "aarch64" CIBW_ARCHS: "aarch64"
# Likewise, select only one Python version per job to speed this up. # Likewise, select only one Python version per job to speed this up.
CIBW_BUILD: "${{ matrix.python-version }}-${{ matrix.spec == 'musllinux' && 'musllinux' || 'manylinux' }}*" CIBW_BUILD: "${{ matrix.python-version }}-${{ matrix.spec == 'musllinux' && 'musllinux' || 'manylinux' }}*"
CIBW_PRERELEASE_PYTHONS: True
# Extra options for manylinux. # Extra options for manylinux.
CIBW_MANYLINUX_AARCH64_IMAGE: ${{ matrix.spec }} CIBW_MANYLINUX_AARCH64_IMAGE: ${{ matrix.spec }}
CIBW_MANYLINUX_PYPY_AARCH64_IMAGE: ${{ matrix.spec }} CIBW_MANYLINUX_PYPY_AARCH64_IMAGE: ${{ matrix.spec }}
@ -133,6 +135,7 @@ jobs:
CIBW_BUILD: ${{ matrix.build }} CIBW_BUILD: ${{ matrix.build }}
CIBW_MANYLINUX_PYPY_X86_64_IMAGE: ${{ matrix.manylinux }} CIBW_MANYLINUX_PYPY_X86_64_IMAGE: ${{ matrix.manylinux }}
CIBW_MANYLINUX_X86_64_IMAGE: ${{ matrix.manylinux }} CIBW_MANYLINUX_X86_64_IMAGE: ${{ matrix.manylinux }}
CIBW_PRERELEASE_PYTHONS: True
CIBW_SKIP: pp38-* CIBW_SKIP: pp38-*
CIBW_TEST_SKIP: cp38-macosx_arm64 CIBW_TEST_SKIP: cp38-macosx_arm64
MACOSX_DEPLOYMENT_TARGET: ${{ matrix.macosx_deployment_target }} MACOSX_DEPLOYMENT_TARGET: ${{ matrix.macosx_deployment_target }}
@ -204,6 +207,7 @@ jobs:
CIBW_ARCHS: ${{ matrix.cibw_arch }} CIBW_ARCHS: ${{ matrix.cibw_arch }}
CIBW_BEFORE_ALL: "{package}\\winbuild\\build\\build_dep_all.cmd" CIBW_BEFORE_ALL: "{package}\\winbuild\\build\\build_dep_all.cmd"
CIBW_CACHE_PATH: "C:\\cibw" CIBW_CACHE_PATH: "C:\\cibw"
CIBW_PRERELEASE_PYTHONS: True
CIBW_SKIP: pp38-* CIBW_SKIP: pp38-*
CIBW_TEST_SKIP: "*-win_arm64" CIBW_TEST_SKIP: "*-win_arm64"
CIBW_TEST_COMMAND: 'docker run --rm CIBW_TEST_COMMAND: 'docker run --rm

View File

@ -1,12 +1,12 @@
repos: repos:
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4 rev: v0.4.3
hooks: hooks:
- id: ruff - id: ruff
args: [--exit-non-zero-on-fix] args: [--exit-non-zero-on-fix]
- repo: https://github.com/psf/black-pre-commit-mirror - repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.3.0 rev: 24.4.2
hooks: hooks:
- id: black - id: black
@ -23,13 +23,20 @@ repos:
- id: remove-tabs - id: remove-tabs
exclude: (Makefile$|\.bat$|\.cmake$|\.eps$|\.fits$|\.gd$|\.opt$) exclude: (Makefile$|\.bat$|\.cmake$|\.eps$|\.fits$|\.gd$|\.opt$)
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v18.1.4
hooks:
- id: clang-format
types: [c]
exclude: ^src/thirdparty/
- repo: https://github.com/pre-commit/pygrep-hooks - repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0 rev: v1.10.0
hooks: hooks:
- id: rst-backticks - id: rst-backticks
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0 rev: v4.6.0
hooks: hooks:
- id: check-executables-have-shebangs - id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable - id: check-shebang-scripts-are-executable
@ -43,7 +50,7 @@ repos:
exclude: ^.github/.*TEMPLATE|^Tests/(fonts|images)/ exclude: ^.github/.*TEMPLATE|^Tests/(fonts|images)/
- repo: https://github.com/python-jsonschema/check-jsonschema - repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.28.1 rev: 0.28.2
hooks: hooks:
- id: check-github-workflows - id: check-github-workflows
- id: check-readthedocs - id: check-readthedocs
@ -55,7 +62,7 @@ repos:
- id: sphinx-lint - id: sphinx-lint
- repo: https://github.com/tox-dev/pyproject-fmt - repo: https://github.com/tox-dev/pyproject-fmt
rev: 1.7.0 rev: 1.8.0
hooks: hooks:
- id: pyproject-fmt - id: pyproject-fmt

View File

@ -5,6 +5,15 @@ Changelog (Pillow)
10.4.0 (unreleased) 10.4.0 (unreleased)
------------------- -------------------
- Added ImageDraw circle() #8085
[void4, hugovk, radarhere]
- Add mypy target to Makefile #8077
[Yay295]
- Added more modes to Image.MODES #7984
[radarhere]
- Deprecate BGR;15, BGR;16 and BGR;24 modes #7978 - Deprecate BGR;15, BGR;16 and BGR;24 modes #7978
[radarhere, hugovk] [radarhere, hugovk]

View File

@ -118,3 +118,8 @@ lint-fix:
python3 -m black . python3 -m black .
python3 -c "import ruff" > /dev/null 2>&1 || python3 -m pip install ruff python3 -c "import ruff" > /dev/null 2>&1 || python3 -m pip install ruff
python3 -m ruff --fix . python3 -m ruff --fix .
.PHONY: mypy
mypy:
python3 -c "import tox" > /dev/null 2>&1 || python3 -m pip install tox
python3 -m tox -e mypy

View File

@ -29,33 +29,6 @@ elif "GITHUB_ACTIONS" in os.environ:
uploader = "github_actions" uploader = "github_actions"
modes = (
"1",
"L",
"LA",
"La",
"P",
"PA",
"F",
"I",
"I;16",
"I;16L",
"I;16B",
"I;16N",
"RGB",
"RGBA",
"RGBa",
"RGBX",
"BGR;15",
"BGR;16",
"BGR;24",
"CMYK",
"YCbCr",
"HSV",
"LAB",
)
def upload(a: Image.Image, b: Image.Image) -> str | None: def upload(a: Image.Image, b: Image.Image) -> str | None:
if uploader == "show": if uploader == "show":
# local img.show for errors. # local img.show for errors.
@ -201,12 +174,13 @@ def skip_unless_feature(feature: str) -> pytest.MarkDecorator:
def skip_unless_feature_version( def skip_unless_feature_version(
feature: str, required: str, reason: str | None = None feature: str, required: str, reason: str | None = None
) -> pytest.MarkDecorator: ) -> pytest.MarkDecorator:
if not features.check(feature): version = features.version(feature)
if version is None:
return pytest.mark.skip(f"{feature} not available") return pytest.mark.skip(f"{feature} not available")
if reason is None: if reason is None:
reason = f"{feature} is older than {required}" reason = f"{feature} is older than {required}"
version_required = parse_version(required) version_required = parse_version(required)
version_available = parse_version(features.version(feature)) version_available = parse_version(version)
return pytest.mark.skipif(version_available < version_required, reason=reason) return pytest.mark.skipif(version_available < version_required, reason=reason)
@ -216,12 +190,13 @@ def mark_if_feature_version(
version_blacklist: str, version_blacklist: str,
reason: str | None = None, reason: str | None = None,
) -> pytest.MarkDecorator: ) -> pytest.MarkDecorator:
if not features.check(feature): version = features.version(feature)
if version is None:
return pytest.mark.pil_noop_mark() return pytest.mark.pil_noop_mark()
if reason is None: if reason is None:
reason = f"{feature} is {version_blacklist}" reason = f"{feature} is {version_blacklist}"
version_required = parse_version(version_blacklist) version_required = parse_version(version_blacklist)
version_available = parse_version(features.version(feature)) version_available = parse_version(version)
if ( if (
version_available.major == version_required.major version_available.major == version_required.major
and version_available.minor == version_required.minor and version_available.minor == version_required.minor
@ -247,16 +222,11 @@ class PillowLeakTestCase:
from resource import RUSAGE_SELF, getrusage from resource import RUSAGE_SELF, getrusage
mem = getrusage(RUSAGE_SELF).ru_maxrss mem = getrusage(RUSAGE_SELF).ru_maxrss
if sys.platform == "darwin":
# man 2 getrusage: # man 2 getrusage:
# ru_maxrss # ru_maxrss
# This is the maximum resident set size utilized (in bytes). # This is the maximum resident set size utilized
return mem / 1024 # Kb # in bytes on macOS, in kilobytes on Linux
# linux return mem / 1024 if sys.platform == "darwin" else mem
# man 2 getrusage
# ru_maxrss (since Linux 2.6.32)
# This is the maximum resident set size used (in kilobytes).
return mem # Kb
def _test_leak(self, core: Callable[[], None]) -> None: def _test_leak(self, core: Callable[[], None]) -> None:
start_mem = self._get_mem_usage() start_mem = self._get_mem_usage()

View File

@ -12,8 +12,9 @@ from Tests.helper import skip_unless_feature
if sys.platform.startswith("win32"): if sys.platform.startswith("win32"):
pytest.skip("Fuzzer is linux only", allow_module_level=True) pytest.skip("Fuzzer is linux only", allow_module_level=True)
if features.check("libjpeg_turbo"): libjpeg_turbo_version = features.version("libjpeg_turbo")
version = packaging.version.parse(features.version("libjpeg_turbo")) if libjpeg_turbo_version is not None:
version = packaging.version.parse(libjpeg_turbo_version)
if version.major == 2 and version.minor == 0: if version.major == 2 and version.minor == 0:
pytestmark = pytest.mark.valgrind_known_error( pytestmark = pytest.mark.valgrind_known_error(
reason="Known failing with libjpeg_turbo 2.0" reason="Known failing with libjpeg_turbo 2.0"

View File

@ -30,7 +30,7 @@ def test_version() -> None:
# Check the correctness of the convenience function # Check the correctness of the convenience function
# and the format of version numbers # and the format of version numbers
def test(name: str, function: Callable[[str], bool]) -> None: def test(name: str, function: Callable[[str], str | None]) -> None:
version = features.version(name) version = features.version(name)
if not features.check(name): if not features.check(name):
assert version is None assert version is None
@ -67,12 +67,16 @@ def test_webp_anim() -> None:
@skip_unless_feature("libjpeg_turbo") @skip_unless_feature("libjpeg_turbo")
def test_libjpeg_turbo_version() -> None: def test_libjpeg_turbo_version() -> None:
assert re.search(r"\d+\.\d+\.\d+$", features.version("libjpeg_turbo")) version = features.version("libjpeg_turbo")
assert version is not None
assert re.search(r"\d+\.\d+\.\d+$", version)
@skip_unless_feature("libimagequant") @skip_unless_feature("libimagequant")
def test_libimagequant_version() -> None: def test_libimagequant_version() -> None:
assert re.search(r"\d+\.\d+\.\d+$", features.version("libimagequant")) version = features.version("libimagequant")
assert version is not None
assert re.search(r"\d+\.\d+\.\d+$", version)
@pytest.mark.parametrize("feature", features.modules) @pytest.mark.parametrize("feature", features.modules)

View File

@ -336,9 +336,7 @@ def test_readline_psfile(tmp_path: Path) -> None:
strings = ["something", "else", "baz", "bif"] strings = ["something", "else", "baz", "bif"]
def _test_readline(t: EpsImagePlugin.PSFile, ending: str) -> None: def _test_readline(t: EpsImagePlugin.PSFile, ending: str) -> None:
ending = "Failure with line ending: %s" % ( ending = f"Failure with line ending: {''.join(str(ord(s)) for s in ending)}"
"".join("%s" % ord(s) for s in ending)
)
assert t.readline().strip("\r\n") == "something", ending assert t.readline().strip("\r\n") == "something", ending
assert t.readline().strip("\r\n") == "else", ending assert t.readline().strip("\r\n") == "else", ending
assert t.readline().strip("\r\n") == "baz", ending assert t.readline().strip("\r\n") == "baz", ending

View File

@ -70,7 +70,9 @@ class TestFileJpeg:
def test_sanity(self) -> None: def test_sanity(self) -> None:
# internal version number # internal version number
assert re.search(r"\d+\.\d+$", features.version_codec("jpg")) version = features.version_codec("jpg")
assert version is not None
assert re.search(r"\d+\.\d+$", version)
with Image.open(TEST_FILE) as im: with Image.open(TEST_FILE) as im:
im.load() im.load()

View File

@ -48,7 +48,9 @@ def roundtrip(im: Image.Image, **options: Any) -> Image.Image:
def test_sanity() -> None: def test_sanity() -> None:
# Internal version number # Internal version number
assert re.search(r"\d+\.\d+\.\d+$", features.version_codec("jpg_2000")) version = features.version_codec("jpg_2000")
assert version is not None
assert re.search(r"\d+\.\d+\.\d+$", version)
with Image.open("Tests/images/test-card-lossless.jp2") as im: with Image.open("Tests/images/test-card-lossless.jp2") as im:
px = im.load() px = im.load()

View File

@ -52,7 +52,9 @@ class LibTiffTestCase:
class TestFileLibTiff(LibTiffTestCase): class TestFileLibTiff(LibTiffTestCase):
def test_version(self) -> None: def test_version(self) -> None:
assert re.search(r"\d+\.\d+\.\d+$", features.version_codec("libtiff")) version = features.version_codec("libtiff")
assert version is not None
assert re.search(r"\d+\.\d+\.\d+$", version)
def test_g4_tiff(self, tmp_path: Path) -> None: def test_g4_tiff(self, tmp_path: Path) -> None:
"""Test the ordinary file path load path""" """Test the ordinary file path load path"""

39
Tests/test_file_mpeg.py Normal file
View File

@ -0,0 +1,39 @@
from __future__ import annotations
from io import BytesIO
import pytest
from PIL import Image, MpegImagePlugin
def test_identify() -> None:
# Arrange
b = BytesIO(b"\x00\x00\x01\xb3\x01\x00\x01")
# Act
with Image.open(b) as im:
# Assert
assert im.format == "MPEG"
assert im.mode == "RGB"
assert im.size == (16, 1)
def test_invalid_file() -> None:
# Arrange
invalid_file = "Tests/images/flower.jpg"
# Act / Assert
with pytest.raises(SyntaxError):
MpegImagePlugin.MpegImageFile(invalid_file)
def test_load() -> None:
# Arrange
b = BytesIO(b"\x00\x00\x01\xb3\x01\x00\x01")
with Image.open(b) as im:
# Act / Assert: cannot load
with pytest.raises(OSError):
im.load()

View File

@ -85,9 +85,9 @@ class TestFilePng:
def test_sanity(self, tmp_path: Path) -> None: def test_sanity(self, tmp_path: Path) -> None:
# internal version number # internal version number
assert re.search( version = features.version_codec("zlib")
r"\d+(\.\d+){1,3}(\.zlib\-ng)?$", features.version_codec("zlib") assert version is not None
) assert re.search(r"\d+(\.\d+){1,3}(\.zlib\-ng)?$", version)
test_file = str(tmp_path / "temp.png") test_file = str(tmp_path / "temp.png")

View File

@ -49,7 +49,9 @@ class TestFileWebp:
def test_version(self) -> None: def test_version(self) -> None:
_webp.WebPDecoderVersion() _webp.WebPDecoderVersion()
_webp.WebPDecoderBuggyAlpha() _webp.WebPDecoderBuggyAlpha()
assert re.search(r"\d+\.\d+\.\d+$", features.version_module("webp")) version = features.version_module("webp")
assert version is not None
assert re.search(r"\d+\.\d+\.\d+$", version)
def test_read_rgb(self) -> None: def test_read_rgb(self) -> None:
""" """

View File

@ -52,8 +52,9 @@ def test_write_animation_L(tmp_path: Path) -> None:
assert_image_similar(im, orig.convert("RGBA"), 32.9) assert_image_similar(im, orig.convert("RGBA"), 32.9)
if is_big_endian(): if is_big_endian():
webp = parse_version(features.version_module("webp")) version = features.version_module("webp")
if webp < parse_version("1.2.2"): assert version is not None
if parse_version(version) < parse_version("1.2.2"):
pytest.skip("Fails with libwebp earlier than 1.2.2") pytest.skip("Fails with libwebp earlier than 1.2.2")
orig.seek(orig.n_frames - 1) orig.seek(orig.n_frames - 1)
im.seek(im.n_frames - 1) im.seek(im.n_frames - 1)
@ -78,8 +79,9 @@ def test_write_animation_RGB(tmp_path: Path) -> None:
# Compare second frame to original # Compare second frame to original
if is_big_endian(): if is_big_endian():
webp = parse_version(features.version_module("webp")) version = features.version_module("webp")
if webp < parse_version("1.2.2"): assert version is not None
if parse_version(version) < parse_version("1.2.2"):
pytest.skip("Fails with libwebp earlier than 1.2.2") pytest.skip("Fails with libwebp earlier than 1.2.2")
im.seek(1) im.seek(1)
im.load() im.load()

View File

@ -31,7 +31,6 @@ from .helper import (
is_big_endian, is_big_endian,
is_win32, is_win32,
mark_if_feature_version, mark_if_feature_version,
modes,
skip_unless_feature, skip_unless_feature,
) )
@ -46,7 +45,7 @@ def helper_image_new(mode: str, size: tuple[int, int]) -> Image.Image:
class TestImage: class TestImage:
@pytest.mark.parametrize("mode", modes) @pytest.mark.parametrize("mode", Image.MODES + ["BGR;15", "BGR;16", "BGR;24"])
def test_image_modes_success(self, mode: str) -> None: def test_image_modes_success(self, mode: str) -> None:
helper_image_new(mode, (1, 1)) helper_image_new(mode, (1, 1))
@ -1027,7 +1026,7 @@ class TestImage:
class TestImageBytes: class TestImageBytes:
@pytest.mark.parametrize("mode", modes) @pytest.mark.parametrize("mode", Image.MODES + ["BGR;15", "BGR;16", "BGR;24"])
def test_roundtrip_bytes_constructor(self, mode: str) -> None: def test_roundtrip_bytes_constructor(self, mode: str) -> None:
im = hopper(mode) im = hopper(mode)
source_bytes = im.tobytes() source_bytes = im.tobytes()
@ -1039,7 +1038,7 @@ class TestImageBytes:
reloaded = Image.frombytes(mode, im.size, source_bytes) reloaded = Image.frombytes(mode, im.size, source_bytes)
assert reloaded.tobytes() == source_bytes assert reloaded.tobytes() == source_bytes
@pytest.mark.parametrize("mode", modes) @pytest.mark.parametrize("mode", Image.MODES + ["BGR;15", "BGR;16", "BGR;24"])
def test_roundtrip_bytes_method(self, mode: str) -> None: def test_roundtrip_bytes_method(self, mode: str) -> None:
im = hopper(mode) im = hopper(mode)
source_bytes = im.tobytes() source_bytes = im.tobytes()
@ -1048,7 +1047,7 @@ class TestImageBytes:
reloaded.frombytes(source_bytes) reloaded.frombytes(source_bytes)
assert reloaded.tobytes() == source_bytes assert reloaded.tobytes() == source_bytes
@pytest.mark.parametrize("mode", modes) @pytest.mark.parametrize("mode", Image.MODES + ["BGR;15", "BGR;16", "BGR;24"])
def test_getdata_putdata(self, mode: str) -> None: def test_getdata_putdata(self, mode: str) -> None:
if is_big_endian() and mode == "BGR;15": if is_big_endian() and mode == "BGR;15":
pytest.xfail("Known failure of BGR;15 on big-endian") pytest.xfail("Known failure of BGR;15 on big-endian")

View File

@ -10,7 +10,7 @@ import pytest
from PIL import Image from PIL import Image
from .helper import assert_image_equal, hopper, is_win32, modes from .helper import assert_image_equal, hopper, is_win32
# CFFI imports pycparser which doesn't support PYTHONOPTIMIZE=2 # CFFI imports pycparser which doesn't support PYTHONOPTIMIZE=2
# https://github.com/eliben/pycparser/pull/198#issuecomment-317001670 # https://github.com/eliben/pycparser/pull/198#issuecomment-317001670
@ -205,12 +205,13 @@ class TestImageGetPixel(AccessTest):
with pytest.raises(error): with pytest.raises(error):
im.getpixel((-1, -1)) im.getpixel((-1, -1))
@pytest.mark.parametrize("mode", modes) @pytest.mark.parametrize("mode", Image.MODES)
def test_basic(self, mode: str) -> None: def test_basic(self, mode: str) -> None:
if mode.startswith("BGR;"):
with pytest.warns(DeprecationWarning):
self.check(mode) self.check(mode)
else:
@pytest.mark.parametrize("mode", ("BGR;15", "BGR;16", "BGR;24"))
def test_deprecated(self, mode: str) -> None:
with pytest.warns(DeprecationWarning):
self.check(mode) self.check(mode)
def test_list(self) -> None: def test_list(self) -> None:
@ -409,13 +410,14 @@ class TestEmbeddable:
from setuptools.command import build_ext from setuptools.command import build_ext
with open("embed_pil.c", "w", encoding="utf-8") as fh: with open("embed_pil.c", "w", encoding="utf-8") as fh:
home = sys.prefix.replace("\\", "\\\\")
fh.write( fh.write(
""" f"""
#include "Python.h" #include "Python.h"
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {{
char *home = "%s"; char *home = "{home}";
wchar_t *whome = Py_DecodeLocale(home, NULL); wchar_t *whome = Py_DecodeLocale(home, NULL);
Py_SetPythonHome(whome); Py_SetPythonHome(whome);
@ -430,9 +432,8 @@ int main(int argc, char* argv[])
PyMem_RawFree(whome); PyMem_RawFree(whome);
return 0; return 0;
} }}
""" """
% sys.prefix.replace("\\", "\\\\")
) )
compiler = getattr(build_ext, "new_compiler")() compiler = getattr(build_ext, "new_compiler")()

View File

@ -24,8 +24,9 @@ def test_sanity() -> None:
def test_libimagequant_quantize() -> None: def test_libimagequant_quantize() -> None:
image = hopper() image = hopper()
if is_ppc64le(): if is_ppc64le():
libimagequant = parse_version(features.version_feature("libimagequant")) version = features.version_feature("libimagequant")
if libimagequant < parse_version("4"): assert version is not None
if parse_version(version) < parse_version("4"):
pytest.skip("Fails with libimagequant earlier than 4.0.0 on ppc64le") pytest.skip("Fails with libimagequant earlier than 4.0.0 on ppc64le")
converted = image.quantize(100, Image.Quantize.LIBIMAGEQUANT) converted = image.quantize(100, Image.Quantize.LIBIMAGEQUANT)
assert converted.mode == "P" assert converted.mode == "P"

View File

@ -102,7 +102,7 @@ def test_unsupported_modes(mode: str) -> None:
def get_image(mode: str) -> Image.Image: def get_image(mode: str) -> Image.Image:
mode_info = ImageMode.getmode(mode) mode_info = ImageMode.getmode(mode)
if mode_info.basetype == "L": if mode_info.basetype == "L":
bands = [gradients_image] bands: list[Image.Image] = [gradients_image]
for _ in mode_info.bands[1:]: for _ in mode_info.bands[1:]:
# rotate previous image # rotate previous image
band = bands[-1].transpose(Image.Transpose.ROTATE_90) band = bands[-1].transpose(Image.Transpose.ROTATE_90)

View File

@ -60,10 +60,13 @@ def test_sanity() -> None:
assert list(map(type, v)) == [str, str, str, str] assert list(map(type, v)) == [str, str, str, str]
# internal version number # internal version number
assert re.search(r"\d+\.\d+(\.\d+)?$", features.version_module("littlecms2")) version = features.version_module("littlecms2")
assert version is not None
assert re.search(r"\d+\.\d+(\.\d+)?$", version)
skip_missing() skip_missing()
i = ImageCms.profileToProfile(hopper(), SRGB, SRGB) i = ImageCms.profileToProfile(hopper(), SRGB, SRGB)
assert i is not None
assert_image(i, "RGB", (128, 128)) assert_image(i, "RGB", (128, 128))
i = hopper() i = hopper()
@ -72,23 +75,27 @@ def test_sanity() -> None:
t = ImageCms.buildTransform(SRGB, SRGB, "RGB", "RGB") t = ImageCms.buildTransform(SRGB, SRGB, "RGB", "RGB")
i = ImageCms.applyTransform(hopper(), t) i = ImageCms.applyTransform(hopper(), t)
assert i is not None
assert_image(i, "RGB", (128, 128)) assert_image(i, "RGB", (128, 128))
with hopper() as i: with hopper() as i:
t = ImageCms.buildTransform(SRGB, SRGB, "RGB", "RGB") t = ImageCms.buildTransform(SRGB, SRGB, "RGB", "RGB")
ImageCms.applyTransform(hopper(), t, inPlace=True) ImageCms.applyTransform(hopper(), t, inPlace=True)
assert i is not None
assert_image(i, "RGB", (128, 128)) assert_image(i, "RGB", (128, 128))
p = ImageCms.createProfile("sRGB") p = ImageCms.createProfile("sRGB")
o = ImageCms.getOpenProfile(SRGB) o = ImageCms.getOpenProfile(SRGB)
t = ImageCms.buildTransformFromOpenProfiles(p, o, "RGB", "RGB") t = ImageCms.buildTransformFromOpenProfiles(p, o, "RGB", "RGB")
i = ImageCms.applyTransform(hopper(), t) i = ImageCms.applyTransform(hopper(), t)
assert i is not None
assert_image(i, "RGB", (128, 128)) assert_image(i, "RGB", (128, 128))
t = ImageCms.buildProofTransform(SRGB, SRGB, SRGB, "RGB", "RGB") t = ImageCms.buildProofTransform(SRGB, SRGB, SRGB, "RGB", "RGB")
assert t.inputMode == "RGB" assert t.inputMode == "RGB"
assert t.outputMode == "RGB" assert t.outputMode == "RGB"
i = ImageCms.applyTransform(hopper(), t) i = ImageCms.applyTransform(hopper(), t)
assert i is not None
assert_image(i, "RGB", (128, 128)) assert_image(i, "RGB", (128, 128))
# test PointTransform convenience API # test PointTransform convenience API
@ -260,7 +267,7 @@ def test_simple_lab() -> None:
t = ImageCms.buildTransform(psRGB, pLab, "RGB", "LAB") t = ImageCms.buildTransform(psRGB, pLab, "RGB", "LAB")
i_lab = ImageCms.applyTransform(i, t) i_lab = ImageCms.applyTransform(i, t)
assert i_lab is not None
assert i_lab.mode == "LAB" assert i_lab.mode == "LAB"
k = i_lab.getpixel((0, 0)) k = i_lab.getpixel((0, 0))
@ -284,6 +291,7 @@ def test_lab_color() -> None:
# Need to add a type mapping for some PIL type to TYPE_Lab_8 in findLCMSType, and # Need to add a type mapping for some PIL type to TYPE_Lab_8 in findLCMSType, and
# have that mapping work back to a PIL mode (likely RGB). # have that mapping work back to a PIL mode (likely RGB).
i = ImageCms.applyTransform(hopper(), t) i = ImageCms.applyTransform(hopper(), t)
assert i is not None
assert_image(i, "LAB", (128, 128)) assert_image(i, "LAB", (128, 128))
# i.save('temp.lab.tif') # visually verified vs PS. # i.save('temp.lab.tif') # visually verified vs PS.
@ -298,6 +306,7 @@ def test_lab_srgb() -> None:
with Image.open("Tests/images/hopper.Lab.tif") as img: with Image.open("Tests/images/hopper.Lab.tif") as img:
img_srgb = ImageCms.applyTransform(img, t) img_srgb = ImageCms.applyTransform(img, t)
assert img_srgb is not None
# img_srgb.save('temp.srgb.tif') # visually verified vs ps. # img_srgb.save('temp.srgb.tif') # visually verified vs ps.
@ -317,11 +326,11 @@ def test_lab_roundtrip() -> None:
t2 = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB") t2 = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB")
i = ImageCms.applyTransform(hopper(), t) i = ImageCms.applyTransform(hopper(), t)
assert i is not None
assert i.info["icc_profile"] == ImageCmsProfile(pLab).tobytes() assert i.info["icc_profile"] == ImageCmsProfile(pLab).tobytes()
out = ImageCms.applyTransform(i, t2) out = ImageCms.applyTransform(i, t2)
assert out is not None
assert_image_similar(hopper(), out, 2) assert_image_similar(hopper(), out, 2)
@ -657,7 +666,7 @@ def test_auxiliary_channels_isolated() -> None:
reference_image = ImageCms.applyTransform( reference_image = ImageCms.applyTransform(
source_image.convert(src_format[2]), reference_transform source_image.convert(src_format[2]), reference_transform
) )
assert reference_image is not None
assert_image_equal(test_image.convert(dst_format[2]), reference_image) assert_image_equal(test_image.convert(dst_format[2]), reference_image)

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import contextlib import contextlib
import os.path import os.path
from typing import Sequence
import pytest import pytest
@ -265,6 +266,21 @@ def test_chord_too_fat() -> None:
assert_image_equal_tofile(im, "Tests/images/imagedraw_chord_too_fat.png") assert_image_equal_tofile(im, "Tests/images/imagedraw_chord_too_fat.png")
@pytest.mark.parametrize("mode", ("RGB", "L"))
@pytest.mark.parametrize("xy", ((W / 2, H / 2), [W / 2, H / 2]))
def test_circle(mode: str, xy: Sequence[float]) -> None:
# Arrange
im = Image.new(mode, (W, H))
draw = ImageDraw.Draw(im)
expected = f"Tests/images/imagedraw_ellipse_{mode}.png"
# Act
draw.circle(xy, 25, fill="green", outline="blue")
# Assert
assert_image_similar_tofile(im, expected, 1)
@pytest.mark.parametrize("mode", ("RGB", "L")) @pytest.mark.parametrize("mode", ("RGB", "L"))
@pytest.mark.parametrize("bbox", BBOX) @pytest.mark.parametrize("bbox", BBOX)
def test_ellipse(mode: str, bbox: Coords) -> None: def test_ellipse(mode: str, bbox: Coords) -> None:

View File

@ -202,6 +202,8 @@ class TestImageFile:
class MockPyDecoder(ImageFile.PyDecoder): class MockPyDecoder(ImageFile.PyDecoder):
last: MockPyDecoder
def __init__(self, mode: str, *args: Any) -> None: def __init__(self, mode: str, *args: Any) -> None:
MockPyDecoder.last = self MockPyDecoder.last = self
@ -213,6 +215,8 @@ class MockPyDecoder(ImageFile.PyDecoder):
class MockPyEncoder(ImageFile.PyEncoder): class MockPyEncoder(ImageFile.PyEncoder):
last: MockPyEncoder | None
def __init__(self, mode: str, *args: Any) -> None: def __init__(self, mode: str, *args: Any) -> None:
MockPyEncoder.last = self MockPyEncoder.last = self
@ -315,6 +319,7 @@ class TestPyEncoder(CodecsTest):
im, fp, [("MOCK", (xoff, yoff, xoff + xsize, yoff + ysize), 0, "RGB")] im, fp, [("MOCK", (xoff, yoff, xoff + xsize, yoff + ysize), 0, "RGB")]
) )
assert MockPyEncoder.last
assert MockPyEncoder.last.state.xoff == xoff assert MockPyEncoder.last.state.xoff == xoff
assert MockPyEncoder.last.state.yoff == yoff assert MockPyEncoder.last.state.yoff == yoff
assert MockPyEncoder.last.state.xsize == xsize assert MockPyEncoder.last.state.xsize == xsize
@ -329,6 +334,7 @@ class TestPyEncoder(CodecsTest):
fp = BytesIO() fp = BytesIO()
ImageFile._save(im, fp, [("MOCK", None, 0, "RGB")]) ImageFile._save(im, fp, [("MOCK", None, 0, "RGB")])
assert MockPyEncoder.last
assert MockPyEncoder.last.state.xoff == 0 assert MockPyEncoder.last.state.xoff == 0
assert MockPyEncoder.last.state.yoff == 0 assert MockPyEncoder.last.state.yoff == 0
assert MockPyEncoder.last.state.xsize == 200 assert MockPyEncoder.last.state.xsize == 200

View File

@ -34,7 +34,9 @@ pytestmark = skip_unless_feature("freetype2")
def test_sanity() -> None: def test_sanity() -> None:
assert re.search(r"\d+\.\d+\.\d+$", features.version_module("freetype2")) version = features.version_module("freetype2")
assert version is not None
assert re.search(r"\d+\.\d+\.\d+$", version)
@pytest.fixture( @pytest.fixture(
@ -547,11 +549,10 @@ def test_find_font(
def loadable_font( def loadable_font(
filepath: str, size: int, index: int, encoding: str, *args: Any filepath: str, size: int, index: int, encoding: str, *args: Any
): ):
_freeTypeFont = getattr(ImageFont, "_FreeTypeFont")
if filepath == path_to_fake: if filepath == path_to_fake:
return ImageFont._FreeTypeFont( return _freeTypeFont(FONT_PATH, size, index, encoding, *args)
FONT_PATH, size, index, encoding, *args return _freeTypeFont(filepath, size, index, encoding, *args)
)
return ImageFont._FreeTypeFont(filepath, size, index, encoding, *args)
m.setattr(ImageFont, "FreeTypeFont", loadable_font) m.setattr(ImageFont, "FreeTypeFont", loadable_font)
font = ImageFont.truetype(fontname) font = ImageFont.truetype(fontname)
@ -630,7 +631,9 @@ def test_complex_font_settings() -> None:
def test_variation_get(font: ImageFont.FreeTypeFont) -> None: def test_variation_get(font: ImageFont.FreeTypeFont) -> None:
freetype = parse_version(features.version_module("freetype2")) version = features.version_module("freetype2")
assert version is not None
freetype = parse_version(version)
if freetype < parse_version("2.9.1"): if freetype < parse_version("2.9.1"):
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
font.get_variation_names() font.get_variation_names()
@ -700,7 +703,9 @@ def _check_text(font: ImageFont.FreeTypeFont, path: str, epsilon: float) -> None
def test_variation_set_by_name(font: ImageFont.FreeTypeFont) -> None: def test_variation_set_by_name(font: ImageFont.FreeTypeFont) -> None:
freetype = parse_version(features.version_module("freetype2")) version = features.version_module("freetype2")
assert version is not None
freetype = parse_version(version)
if freetype < parse_version("2.9.1"): if freetype < parse_version("2.9.1"):
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
font.set_variation_by_name("Bold") font.set_variation_by_name("Bold")
@ -725,7 +730,9 @@ def test_variation_set_by_name(font: ImageFont.FreeTypeFont) -> None:
def test_variation_set_by_axes(font: ImageFont.FreeTypeFont) -> None: def test_variation_set_by_axes(font: ImageFont.FreeTypeFont) -> None:
freetype = parse_version(features.version_module("freetype2")) version = features.version_module("freetype2")
assert version is not None
freetype = parse_version(version)
if freetype < parse_version("2.9.1"): if freetype < parse_version("2.9.1"):
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
font.set_variation_by_axes([100]) font.set_variation_by_axes([100])

View File

@ -4,11 +4,11 @@ from typing import Generator
import pytest import pytest
from PIL import Image, ImageFilter from PIL import Image, ImageFile, ImageFilter
@pytest.fixture @pytest.fixture
def test_images() -> Generator[dict[str, Image.Image], None, None]: def test_images() -> Generator[dict[str, ImageFile.ImageFile], None, None]:
ims = { ims = {
"im": Image.open("Tests/images/hopper.ppm"), "im": Image.open("Tests/images/hopper.ppm"),
"snakes": Image.open("Tests/images/color_snakes.png"), "snakes": Image.open("Tests/images/color_snakes.png"),
@ -20,7 +20,7 @@ def test_images() -> Generator[dict[str, Image.Image], None, None]:
im.close() im.close()
def test_filter_api(test_images: dict[str, Image.Image]) -> None: def test_filter_api(test_images: dict[str, ImageFile.ImageFile]) -> None:
im = test_images["im"] im = test_images["im"]
test_filter = ImageFilter.GaussianBlur(2.0) test_filter = ImageFilter.GaussianBlur(2.0)
@ -34,7 +34,7 @@ def test_filter_api(test_images: dict[str, Image.Image]) -> None:
assert i.size == (128, 128) assert i.size == (128, 128)
def test_usm_formats(test_images: dict[str, Image.Image]) -> None: def test_usm_formats(test_images: dict[str, ImageFile.ImageFile]) -> None:
im = test_images["im"] im = test_images["im"]
usm = ImageFilter.UnsharpMask usm = ImageFilter.UnsharpMask
@ -52,7 +52,7 @@ def test_usm_formats(test_images: dict[str, Image.Image]) -> None:
im.convert("YCbCr").filter(usm) im.convert("YCbCr").filter(usm)
def test_blur_formats(test_images: dict[str, Image.Image]) -> None: def test_blur_formats(test_images: dict[str, ImageFile.ImageFile]) -> None:
im = test_images["im"] im = test_images["im"]
blur = ImageFilter.GaussianBlur blur = ImageFilter.GaussianBlur
@ -70,7 +70,7 @@ def test_blur_formats(test_images: dict[str, Image.Image]) -> None:
im.convert("YCbCr").filter(blur) im.convert("YCbCr").filter(blur)
def test_usm_accuracy(test_images: dict[str, Image.Image]) -> None: def test_usm_accuracy(test_images: dict[str, ImageFile.ImageFile]) -> None:
snakes = test_images["snakes"] snakes = test_images["snakes"]
src = snakes.convert("RGB") src = snakes.convert("RGB")
@ -79,7 +79,7 @@ def test_usm_accuracy(test_images: dict[str, Image.Image]) -> None:
assert i.tobytes() == src.tobytes() assert i.tobytes() == src.tobytes()
def test_blur_accuracy(test_images: dict[str, Image.Image]) -> None: def test_blur_accuracy(test_images: dict[str, ImageFile.ImageFile]) -> None:
snakes = test_images["snakes"] snakes = test_images["snakes"]
i = snakes.filter(ImageFilter.GaussianBlur(0.4)) i = snakes.filter(ImageFilter.GaussianBlur(0.4))

View File

@ -227,6 +227,18 @@ Methods
.. versionadded:: 5.3.0 .. versionadded:: 5.3.0
.. py:method:: ImageDraw.circle(xy, radius, fill=None, outline=None, width=1)
Draws a circle with a given radius centering on a point.
.. versionadded:: 10.4.0
:param xy: The point for the center of the circle, e.g. ``(x, y)``.
:param radius: Radius of the circle.
:param outline: Color to use for the outline.
:param fill: Color to use for the fill.
:param width: The line width, in pixels.
.. py:method:: ImageDraw.ellipse(xy, fill=None, outline=None, width=1) .. py:method:: ImageDraw.ellipse(xy, fill=None, outline=None, width=1)
Draws an ellipse inside the given bounding box. Draws an ellipse inside the given bounding box.

View File

@ -45,6 +45,13 @@ TODO
API Additions API Additions
============= =============
ImageDraw.circle
^^^^^^^^^^^^^^^^
Added :py:meth:`~PIL.ImageDraw.ImageDraw.circle`. It provides the same functionality as
:py:meth:`~PIL.ImageDraw.ImageDraw.ellipse`, but instead of taking a bounding box, it
takes a center point and radius.
TODO TODO
^^^^ ^^^^
@ -53,7 +60,9 @@ TODO
Other Changes Other Changes
============= =============
TODO Python 3.13 beta
^^^^ ^^^^^^^^^^^^^^^^
TODO To help others prepare for Python 3.13, wheels have been built against the 3.13 beta as
a preview. This is not official support for Python 3.13, but simply an opportunity for
users to test how Pillow works with the beta and report any problems.

View File

@ -165,9 +165,9 @@ if __name__ == "__main__":
print("Running selftest:") print("Running selftest:")
status = doctest.testmod(sys.modules[__name__]) status = doctest.testmod(sys.modules[__name__])
if status[0]: if status[0]:
print("*** %s tests of %d failed." % status) print(f"*** {status[0]} tests of {status[1]} failed.")
exit_status = 1 exit_status = 1
else: else:
print("--- %s tests passed." % status[1]) print(f"--- {status[1]} tests passed.")
sys.exit(exit_status) sys.exit(exit_status)

View File

@ -23,8 +23,7 @@ from setuptools.command.build_ext import build_ext
def get_version(): def get_version():
version_file = "src/PIL/_version.py" version_file = "src/PIL/_version.py"
with open(version_file, encoding="utf-8") as f: with open(version_file, encoding="utf-8") as f:
exec(compile(f.read(), version_file, "exec")) return f.read().split('"')[1]
return locals()["__version__"]
configuration = {} configuration = {}

View File

@ -253,7 +253,7 @@ class BlpImageFile(ImageFile.ImageFile):
format = "BLP" format = "BLP"
format_description = "Blizzard Mipmap Format" format_description = "Blizzard Mipmap Format"
def _open(self): def _open(self) -> None:
self.magic = self.fp.read(4) self.magic = self.fp.read(4)
self.fp.seek(5, os.SEEK_CUR) self.fp.seek(5, os.SEEK_CUR)
@ -333,7 +333,7 @@ class _BLPBaseDecoder(ImageFile.PyDecoder):
class BLP1Decoder(_BLPBaseDecoder): class BLP1Decoder(_BLPBaseDecoder):
def _load(self): def _load(self) -> None:
if self._blp_compression == Format.JPEG: if self._blp_compression == Format.JPEG:
self._decode_jpeg_stream() self._decode_jpeg_stream()
@ -418,7 +418,7 @@ class BLP2Decoder(_BLPBaseDecoder):
class BLPEncoder(ImageFile.PyEncoder): class BLPEncoder(ImageFile.PyEncoder):
_pushes_fd = True _pushes_fd = True
def _write_palette(self): def _write_palette(self) -> bytes:
data = b"" data = b""
palette = self.im.getpalette("RGBA", "RGBA") palette = self.im.getpalette("RGBA", "RGBA")
for i in range(len(palette) // 4): for i in range(len(palette) // 4):

View File

@ -283,7 +283,7 @@ class BmpImageFile(ImageFile.ImageFile):
) )
] ]
def _open(self): def _open(self) -> None:
"""Open file, check magic number and read header""" """Open file, check magic number and read header"""
# read 14 bytes: magic number, filesize, reserved, header final offset # read 14 bytes: magic number, filesize, reserved, header final offset
head_data = self.fp.read(14) head_data = self.fp.read(14)
@ -376,7 +376,7 @@ class DibImageFile(BmpImageFile):
format = "DIB" format = "DIB"
format_description = "Windows Bitmap" format_description = "Windows Bitmap"
def _open(self): def _open(self) -> None:
self._bitmap() self._bitmap()

View File

@ -37,7 +37,7 @@ class BufrStubImageFile(ImageFile.StubImageFile):
format = "BUFR" format = "BUFR"
format_description = "BUFR" format_description = "BUFR"
def _open(self): def _open(self) -> None:
offset = self.fp.tell() offset = self.fp.tell()
if not _accept(self.fp.read(4)): if not _accept(self.fp.read(4)):

View File

@ -37,7 +37,7 @@ class CurImageFile(BmpImagePlugin.BmpImageFile):
format = "CUR" format = "CUR"
format_description = "Windows Cursor" format_description = "Windows Cursor"
def _open(self): def _open(self) -> None:
offset = self.fp.tell() offset = self.fp.tell()
# check magic # check magic

View File

@ -63,7 +63,7 @@ class DcxImageFile(PcxImageFile):
self.is_animated = self.n_frames > 1 self.is_animated = self.n_frames > 1
self.seek(0) self.seek(0)
def seek(self, frame): def seek(self, frame: int) -> None:
if not self._seek_check(frame): if not self._seek_check(frame):
return return
self.frame = frame self.frame = frame
@ -71,7 +71,7 @@ class DcxImageFile(PcxImageFile):
self.fp.seek(self._offset[frame]) self.fp.seek(self._offset[frame])
PcxImageFile._open(self) PcxImageFile._open(self)
def tell(self): def tell(self) -> int:
return self.frame return self.frame

View File

@ -331,7 +331,7 @@ class DdsImageFile(ImageFile.ImageFile):
format = "DDS" format = "DDS"
format_description = "DirectDraw Surface" format_description = "DirectDraw Surface"
def _open(self): def _open(self) -> None:
if not _accept(self.fp.read(4)): if not _accept(self.fp.read(4)):
msg = "not a DDS file" msg = "not a DDS file"
raise SyntaxError(msg) raise SyntaxError(msg)
@ -472,7 +472,7 @@ class DdsImageFile(ImageFile.ImageFile):
else: else:
self.tile = [ImageFile._Tile("raw", extents, 0, rawmode or self.mode)] self.tile = [ImageFile._Tile("raw", extents, 0, rawmode or self.mode)]
def load_seek(self, pos): def load_seek(self, pos: int) -> None:
pass pass

View File

@ -42,7 +42,7 @@ gs_binary: str | bool | None = None
gs_windows_binary = None gs_windows_binary = None
def has_ghostscript(): def has_ghostscript() -> bool:
global gs_binary, gs_windows_binary global gs_binary, gs_windows_binary
if gs_binary is None: if gs_binary is None:
if sys.platform.startswith("win"): if sys.platform.startswith("win"):
@ -178,7 +178,7 @@ class PSFile:
self.char = None self.char = None
self.fp.seek(offset, whence) self.fp.seek(offset, whence)
def readline(self): def readline(self) -> str:
s = [self.char or b""] s = [self.char or b""]
self.char = None self.char = None
@ -212,7 +212,7 @@ class EpsImageFile(ImageFile.ImageFile):
mode_map = {1: "L", 2: "LAB", 3: "RGB", 4: "CMYK"} mode_map = {1: "L", 2: "LAB", 3: "RGB", 4: "CMYK"}
def _open(self): def _open(self) -> None:
(length, offset) = self._find_offset(self.fp) (length, offset) = self._find_offset(self.fp)
# go to offset - start of "%!PS" # go to offset - start of "%!PS"
@ -404,7 +404,7 @@ class EpsImageFile(ImageFile.ImageFile):
self.tile = [] self.tile = []
return Image.Image.load(self) return Image.Image.load(self)
def load_seek(self, pos): def load_seek(self, pos: int) -> None:
# we can't incrementally load, so force ImageFile.parser to # we can't incrementally load, so force ImageFile.parser to
# use our custom load method by defining this method. # use our custom load method by defining this method.
pass pass

View File

@ -123,7 +123,7 @@ class FliImageFile(ImageFile.ImageFile):
palette[i] = (r, g, b) palette[i] = (r, g, b)
i += 1 i += 1
def seek(self, frame): def seek(self, frame: int) -> None:
if not self._seek_check(frame): if not self._seek_check(frame):
return return
if frame < self.__frame: if frame < self.__frame:
@ -132,7 +132,7 @@ class FliImageFile(ImageFile.ImageFile):
for f in range(self.__frame + 1, frame + 1): for f in range(self.__frame + 1, frame + 1):
self._seek(f) self._seek(f)
def _seek(self, frame): def _seek(self, frame: int) -> None:
if frame == 0: if frame == 0:
self.__frame = -1 self.__frame = -1
self._fp.seek(self.__rewind) self._fp.seek(self.__rewind)
@ -162,7 +162,7 @@ class FliImageFile(ImageFile.ImageFile):
self.__offset += framesize self.__offset += framesize
def tell(self): def tell(self) -> int:
return self.__frame return self.__frame

View File

@ -237,7 +237,7 @@ class FpxImageFile(ImageFile.ImageFile):
return ImageFile.ImageFile.load(self) return ImageFile.ImageFile.load(self)
def close(self): def close(self) -> None:
self.ole.close() self.ole.close()
super().close() super().close()

View File

@ -71,7 +71,7 @@ class FtexImageFile(ImageFile.ImageFile):
format = "FTEX" format = "FTEX"
format_description = "Texture File Format (IW2:EOC)" format_description = "Texture File Format (IW2:EOC)"
def _open(self): def _open(self) -> None:
if not _accept(self.fp.read(4)): if not _accept(self.fp.read(4)):
msg = "not an FTEX file" msg = "not an FTEX file"
raise SyntaxError(msg) raise SyntaxError(msg)
@ -103,7 +103,7 @@ class FtexImageFile(ImageFile.ImageFile):
self.fp.close() self.fp.close()
self.fp = BytesIO(data) self.fp = BytesIO(data)
def load_seek(self, pos): def load_seek(self, pos: int) -> None:
pass pass

View File

@ -41,7 +41,7 @@ class GbrImageFile(ImageFile.ImageFile):
format = "GBR" format = "GBR"
format_description = "GIMP brush file" format_description = "GIMP brush file"
def _open(self): def _open(self) -> None:
header_size = i32(self.fp.read(4)) header_size = i32(self.fp.read(4))
if header_size < 20: if header_size < 20:
msg = "not a GIMP brush" msg = "not a GIMP brush"

View File

@ -30,6 +30,7 @@ import math
import os import os
import subprocess import subprocess
from enum import IntEnum from enum import IntEnum
from functools import cached_property
from . import ( from . import (
Image, Image,
@ -76,19 +77,19 @@ class GifImageFile(ImageFile.ImageFile):
global_palette = None global_palette = None
def data(self): def data(self) -> bytes | None:
s = self.fp.read(1) s = self.fp.read(1)
if s and s[0]: if s and s[0]:
return self.fp.read(s[0]) return self.fp.read(s[0])
return None return None
def _is_palette_needed(self, p): def _is_palette_needed(self, p: bytes) -> bool:
for i in range(0, len(p), 3): for i in range(0, len(p), 3):
if not (i // 3 == p[i] == p[i + 1] == p[i + 2]): if not (i // 3 == p[i] == p[i + 1] == p[i + 2]):
return True return True
return False return False
def _open(self): def _open(self) -> None:
# Screen # Screen
s = self.fp.read(13) s = self.fp.read(13)
if not _accept(s): if not _accept(s):
@ -112,8 +113,7 @@ class GifImageFile(ImageFile.ImageFile):
self._fp = self.fp # FIXME: hack self._fp = self.fp # FIXME: hack
self.__rewind = self.fp.tell() self.__rewind = self.fp.tell()
self._n_frames = None self._n_frames: int | None = None
self._is_animated = None
self._seek(0) # get ready to read first frame self._seek(0) # get ready to read first frame
@property @property
@ -128,26 +128,25 @@ class GifImageFile(ImageFile.ImageFile):
self.seek(current) self.seek(current)
return self._n_frames return self._n_frames
@property @cached_property
def is_animated(self): def is_animated(self) -> bool:
if self._is_animated is None:
if self._n_frames is not None: if self._n_frames is not None:
self._is_animated = self._n_frames != 1 return self._n_frames != 1
else:
current = self.tell() current = self.tell()
if current: if current:
self._is_animated = True return True
else:
try: try:
self._seek(1, False) self._seek(1, False)
self._is_animated = True is_animated = True
except EOFError: except EOFError:
self._is_animated = False is_animated = False
self.seek(current) self.seek(current)
return self._is_animated return is_animated
def seek(self, frame): def seek(self, frame: int) -> None:
if not self._seek_check(frame): if not self._seek_check(frame):
return return
if frame < self.__frame: if frame < self.__frame:
@ -417,7 +416,7 @@ class GifImageFile(ImageFile.ImageFile):
elif k in self.info: elif k in self.info:
del self.info[k] del self.info[k]
def load_prepare(self): def load_prepare(self) -> None:
temp_mode = "P" if self._frame_palette else "L" temp_mode = "P" if self._frame_palette else "L"
self._prev_im = None self._prev_im = None
if self.__frame == 0: if self.__frame == 0:
@ -437,7 +436,7 @@ class GifImageFile(ImageFile.ImageFile):
super().load_prepare() super().load_prepare()
def load_end(self): def load_end(self) -> None:
if self.__frame == 0: if self.__frame == 0:
if self.mode == "P" and LOADING_STRATEGY == LoadingStrategy.RGB_ALWAYS: if self.mode == "P" and LOADING_STRATEGY == LoadingStrategy.RGB_ALWAYS:
if self._frame_transparency is not None: if self._frame_transparency is not None:
@ -463,7 +462,7 @@ class GifImageFile(ImageFile.ImageFile):
else: else:
self.im.paste(frame_im, self.dispose_extent) self.im.paste(frame_im, self.dispose_extent)
def tell(self): def tell(self) -> int:
return self.__frame return self.__frame
@ -474,7 +473,7 @@ class GifImageFile(ImageFile.ImageFile):
RAWMODE = {"1": "L", "L": "L", "P": "P"} RAWMODE = {"1": "L", "L": "L", "P": "P"}
def _normalize_mode(im): def _normalize_mode(im: Image.Image) -> Image.Image:
""" """
Takes an image (or frame), returns an image in a mode that is appropriate Takes an image (or frame), returns an image in a mode that is appropriate
for saving in a Gif. for saving in a Gif.
@ -887,7 +886,7 @@ def _get_optimize(im, info):
return used_palette_colors return used_palette_colors
def _get_color_table_size(palette_bytes): def _get_color_table_size(palette_bytes: bytes) -> int:
# calculate the palette size for the header # calculate the palette size for the header
if not palette_bytes: if not palette_bytes:
return 0 return 0
@ -897,7 +896,7 @@ def _get_color_table_size(palette_bytes):
return math.ceil(math.log(len(palette_bytes) // 3, 2)) - 1 return math.ceil(math.log(len(palette_bytes) // 3, 2)) - 1
def _get_header_palette(palette_bytes): def _get_header_palette(palette_bytes: bytes) -> bytes:
""" """
Returns the palette, null padded to the next power of 2 (*3) bytes Returns the palette, null padded to the next power of 2 (*3) bytes
suitable for direct inclusion in the GIF header suitable for direct inclusion in the GIF header
@ -915,7 +914,7 @@ def _get_header_palette(palette_bytes):
return palette_bytes return palette_bytes
def _get_palette_bytes(im): def _get_palette_bytes(im: Image.Image) -> bytes:
""" """
Gets the palette for inclusion in the gif header Gets the palette for inclusion in the gif header

View File

@ -53,5 +53,5 @@ class GimpPaletteFile:
self.palette = b"".join(self.palette) self.palette = b"".join(self.palette)
def getpalette(self): def getpalette(self) -> tuple[bytes, str]:
return self.palette, self.rawmode return self.palette, self.rawmode

View File

@ -37,7 +37,7 @@ class GribStubImageFile(ImageFile.StubImageFile):
format = "GRIB" format = "GRIB"
format_description = "GRIB" format_description = "GRIB"
def _open(self): def _open(self) -> None:
offset = self.fp.tell() offset = self.fp.tell()
if not _accept(self.fp.read(8)): if not _accept(self.fp.read(8)):

View File

@ -37,7 +37,7 @@ class HDF5StubImageFile(ImageFile.StubImageFile):
format = "HDF5" format = "HDF5"
format_description = "HDF5" format_description = "HDF5"
def _open(self): def _open(self) -> None:
offset = self.fp.tell() offset = self.fp.tell()
if not _accept(self.fp.read(8)): if not _accept(self.fp.read(8)):

View File

@ -252,7 +252,7 @@ class IcnsImageFile(ImageFile.ImageFile):
format = "ICNS" format = "ICNS"
format_description = "Mac OS icns resource" format_description = "Mac OS icns resource"
def _open(self): def _open(self) -> None:
self.icns = IcnsFile(self.fp) self.icns = IcnsFile(self.fp)
self._mode = "RGBA" self._mode = "RGBA"
self.info["sizes"] = self.icns.itersizes() self.info["sizes"] = self.icns.itersizes()

View File

@ -302,7 +302,7 @@ class IcoImageFile(ImageFile.ImageFile):
format = "ICO" format = "ICO"
format_description = "Windows Icon" format_description = "Windows Icon"
def _open(self): def _open(self) -> None:
self.ico = IcoFile(self.fp) self.ico = IcoFile(self.fp)
self.info["sizes"] = self.ico.sizes() self.info["sizes"] = self.ico.sizes()
self.size = self.ico.entry[0]["dim"] self.size = self.ico.entry[0]["dim"]
@ -341,7 +341,7 @@ class IcoImageFile(ImageFile.ImageFile):
self.size = im.size self.size = im.size
def load_seek(self, pos): def load_seek(self, pos: int) -> None:
# Flag the ImageFile.Parser so that it # Flag the ImageFile.Parser so that it
# just does all the decode at the end. # just does all the decode at the end.
pass pass

View File

@ -119,7 +119,7 @@ class ImImageFile(ImageFile.ImageFile):
format_description = "IFUNC Image Memory" format_description = "IFUNC Image Memory"
_close_exclusive_fp_after_loading = False _close_exclusive_fp_after_loading = False
def _open(self): def _open(self) -> None:
# Quick rejection: if there's not an LF among the first # Quick rejection: if there's not an LF among the first
# 100 bytes, this is (probably) not a text header. # 100 bytes, this is (probably) not a text header.
@ -271,14 +271,14 @@ class ImImageFile(ImageFile.ImageFile):
self.tile = [("raw", (0, 0) + self.size, offs, (self.rawmode, 0, -1))] self.tile = [("raw", (0, 0) + self.size, offs, (self.rawmode, 0, -1))]
@property @property
def n_frames(self): def n_frames(self) -> int:
return self.info[FRAMES] return self.info[FRAMES]
@property @property
def is_animated(self): def is_animated(self) -> bool:
return self.info[FRAMES] > 1 return self.info[FRAMES] > 1
def seek(self, frame): def seek(self, frame: int) -> None:
if not self._seek_check(frame): if not self._seek_check(frame):
return return
@ -296,7 +296,7 @@ class ImImageFile(ImageFile.ImageFile):
self.tile = [("raw", (0, 0) + self.size, offs, (self.rawmode, 0, -1))] self.tile = [("raw", (0, 0) + self.size, offs, (self.rawmode, 0, -1))]
def tell(self): def tell(self) -> int:
return self.frame return self.frame

View File

@ -258,7 +258,28 @@ def _conv_type_shape(im):
return shape, m.typestr return shape, m.typestr
MODES = ["1", "CMYK", "F", "HSV", "I", "L", "LAB", "P", "RGB", "RGBA", "RGBX", "YCbCr"] MODES = [
"1",
"CMYK",
"F",
"HSV",
"I",
"I;16",
"I;16B",
"I;16L",
"I;16N",
"L",
"LA",
"La",
"LAB",
"P",
"PA",
"RGB",
"RGBA",
"RGBa",
"RGBX",
"YCbCr",
]
# raw modes that may be memory mapped. NOTE: if you change this, you # raw modes that may be memory mapped. NOTE: if you change this, you
# may have to modify the stride calculation in map.c too! # may have to modify the stride calculation in map.c too!
@ -1333,7 +1354,10 @@ class Image:
self.load() self.load()
return self._new(self.im.expand(xmargin, ymargin)) return self._new(self.im.expand(xmargin, ymargin))
def filter(self, filter): if TYPE_CHECKING:
from . import ImageFilter
def filter(self, filter: ImageFilter.Filter | type[ImageFilter.Filter]) -> Image:
""" """
Filters this image using the given filter. For a list of Filters this image using the given filter. For a list of
available filters, see the :py:mod:`~PIL.ImageFilter` module. available filters, see the :py:mod:`~PIL.ImageFilter` module.
@ -1345,7 +1369,7 @@ class Image:
self.load() self.load()
if isinstance(filter, Callable): if callable(filter):
filter = filter() filter = filter()
if not hasattr(filter, "filter"): if not hasattr(filter, "filter"):
msg = "filter argument should be ImageFilter.Filter instance or class" msg = "filter argument should be ImageFilter.Filter instance or class"

View File

@ -34,7 +34,7 @@ from __future__ import annotations
import math import math
import numbers import numbers
import struct import struct
from typing import Sequence, cast from typing import TYPE_CHECKING, Sequence, cast
from . import Image, ImageColor from . import Image, ImageColor
from ._typing import Coords from ._typing import Coords
@ -92,7 +92,10 @@ class ImageDraw:
self.fontmode = "L" # aliasing is okay for other modes self.fontmode = "L" # aliasing is okay for other modes
self.fill = False self.fill = False
def getfont(self): if TYPE_CHECKING:
from . import ImageFont
def getfont(self) -> ImageFont.FreeTypeFont | ImageFont.ImageFont:
""" """
Get the current default font. Get the current default font.
@ -178,6 +181,13 @@ class ImageDraw:
if ink is not None and ink != fill and width != 0: if ink is not None and ink != fill and width != 0:
self.draw.draw_ellipse(xy, ink, 0, width) self.draw.draw_ellipse(xy, ink, 0, width)
def circle(
self, xy: Sequence[float], radius: float, fill=None, outline=None, width=1
) -> None:
"""Draw a circle given center coordinates and a radius."""
ellipse_xy = (xy[0] - radius, xy[1] - radius, xy[0] + radius, xy[1] + radius)
self.ellipse(ellipse_xy, fill, outline, width)
def line(self, xy: Coords, fill=None, width=0, joint=None) -> None: def line(self, xy: Coords, fill=None, width=0, joint=None) -> None:
"""Draw a line, or a connected sequence of line segments.""" """Draw a line, or a connected sequence of line segments."""
ink = self._getink(fill)[0] ink = self._getink(fill)[0]

View File

@ -311,7 +311,7 @@ class ImageFile(Image.Image):
return Image.Image.load(self) return Image.Image.load(self)
def load_prepare(self): def load_prepare(self) -> None:
# create image memory if necessary # create image memory if necessary
if not self.im or self.im.mode != self.mode or self.im.size != self.size: if not self.im or self.im.mode != self.mode or self.im.size != self.size:
self.im = Image.core.new(self.mode, self.size) self.im = Image.core.new(self.mode, self.size)
@ -319,16 +319,16 @@ class ImageFile(Image.Image):
if self.mode == "P": if self.mode == "P":
Image.Image.load(self) Image.Image.load(self)
def load_end(self): def load_end(self) -> None:
# may be overridden # may be overridden
pass pass
# may be defined for contained formats # may be defined for contained formats
# def load_seek(self, pos): # def load_seek(self, pos: int) -> None:
# pass # pass
# may be defined for blocked formats (e.g. PNG) # may be defined for blocked formats (e.g. PNG)
# def load_read(self, read_bytes): # def load_read(self, read_bytes: int) -> bytes:
# pass # pass
def _seek_check(self, frame): def _seek_check(self, frame):
@ -390,7 +390,7 @@ class Parser:
offset = 0 offset = 0
finished = 0 finished = 0
def reset(self): def reset(self) -> None:
""" """
(Consumer) Reset the parser. Note that you can only call this (Consumer) Reset the parser. Note that you can only call this
method immediately after you've created a parser; parser method immediately after you've created a parser; parser
@ -605,7 +605,7 @@ def _safe_read(fp, size):
class PyCodecState: class PyCodecState:
def __init__(self): def __init__(self) -> None:
self.xsize = 0 self.xsize = 0
self.ysize = 0 self.ysize = 0
self.xoff = 0 self.xoff = 0
@ -634,7 +634,7 @@ class PyCodec:
""" """
self.args = args self.args = args
def cleanup(self): def cleanup(self) -> None:
""" """
Override to perform codec specific cleanup Override to perform codec specific cleanup

View File

@ -16,10 +16,13 @@
# #
from __future__ import annotations from __future__ import annotations
import abc
import functools import functools
class Filter: class Filter:
@abc.abstractmethod
def filter(self, image):
pass pass
@ -541,7 +544,7 @@ class Color3DLUT(MultibandFilter):
_copy_table=False, _copy_table=False,
) )
def __repr__(self): def __repr__(self) -> str:
r = [ r = [
f"{self.__class__.__name__} from {self.table.__class__.__name__}", f"{self.__class__.__name__} from {self.table.__class__.__name__}",
"size={:d}x{:d}x{:d}".format(*self.size), "size={:d}x{:d}x{:d}".format(*self.size),

View File

@ -160,10 +160,6 @@ class ImageFont:
.. versionadded:: 9.2.0 .. versionadded:: 9.2.0
:param text: Text to render. :param text: Text to render.
:param mode: Used by some graphics drivers to indicate what mode the
driver prefers; if empty, the renderer may return either
mode. Note that the mode is always a string, to simplify
C-level implementations.
:return: ``(left, top, right, bottom)`` bounding box :return: ``(left, top, right, bottom)`` bounding box
""" """

View File

@ -66,7 +66,7 @@ class ImagePalette:
def colors(self, colors): def colors(self, colors):
self._colors = colors self._colors = colors
def copy(self): def copy(self) -> ImagePalette:
new = ImagePalette() new = ImagePalette()
new.mode = self.mode new.mode = self.mode
@ -77,7 +77,7 @@ class ImagePalette:
return new return new
def getdata(self): def getdata(self) -> tuple[str, bytes]:
""" """
Get palette contents in format suitable for the low-level Get palette contents in format suitable for the low-level
``im.putpalette`` primitive. ``im.putpalette`` primitive.
@ -88,7 +88,7 @@ class ImagePalette:
return self.rawmode, self.palette return self.rawmode, self.palette
return self.mode, self.tobytes() return self.mode, self.tobytes()
def tobytes(self): def tobytes(self) -> bytes:
"""Convert palette to bytes. """Convert palette to bytes.
.. warning:: This method is experimental. .. warning:: This method is experimental.

View File

@ -128,7 +128,7 @@ class PhotoImage:
if image: if image:
self.paste(image) self.paste(image)
def __del__(self): def __del__(self) -> None:
name = self.__photo.name name = self.__photo.name
self.__photo.name = None self.__photo.name = None
try: try:
@ -136,7 +136,7 @@ class PhotoImage:
except Exception: except Exception:
pass # ignore internal errors pass # ignore internal errors
def __str__(self): def __str__(self) -> str:
""" """
Get the Tkinter photo image identifier. This method is automatically Get the Tkinter photo image identifier. This method is automatically
called by Tkinter whenever a PhotoImage object is passed to a Tkinter called by Tkinter whenever a PhotoImage object is passed to a Tkinter
@ -146,7 +146,7 @@ class PhotoImage:
""" """
return str(self.__photo) return str(self.__photo)
def width(self): def width(self) -> int:
""" """
Get the width of the image. Get the width of the image.
@ -154,7 +154,7 @@ class PhotoImage:
""" """
return self.__size[0] return self.__size[0]
def height(self): def height(self) -> int:
""" """
Get the height of the image. Get the height of the image.
@ -219,7 +219,7 @@ class BitmapImage:
kw["data"] = image.tobitmap() kw["data"] = image.tobitmap()
self.__photo = tkinter.BitmapImage(**kw) self.__photo = tkinter.BitmapImage(**kw)
def __del__(self): def __del__(self) -> None:
name = self.__photo.name name = self.__photo.name
self.__photo.name = None self.__photo.name = None
try: try:
@ -227,7 +227,7 @@ class BitmapImage:
except Exception: except Exception:
pass # ignore internal errors pass # ignore internal errors
def width(self): def width(self) -> int:
""" """
Get the width of the image. Get the width of the image.
@ -235,7 +235,7 @@ class BitmapImage:
""" """
return self.__size[0] return self.__size[0]
def height(self): def height(self) -> int:
""" """
Get the height of the image. Get the height of the image.
@ -243,7 +243,7 @@ class BitmapImage:
""" """
return self.__size[1] return self.__size[1]
def __str__(self): def __str__(self) -> str:
""" """
Get the Tkinter bitmap image identifier. This method is automatically Get the Tkinter bitmap image identifier. This method is automatically
called by Tkinter whenever a BitmapImage object is passed to a Tkinter called by Tkinter whenever a BitmapImage object is passed to a Tkinter

View File

@ -204,7 +204,7 @@ class Window:
def ui_handle_damage(self, x0, y0, x1, y1): def ui_handle_damage(self, x0, y0, x1, y1):
pass pass
def ui_handle_destroy(self): def ui_handle_destroy(self) -> None:
pass pass
def ui_handle_repair(self, dc, x0, y0, x1, y1): def ui_handle_repair(self, dc, x0, y0, x1, y1):
@ -213,7 +213,7 @@ class Window:
def ui_handle_resize(self, width, height): def ui_handle_resize(self, width, height):
pass pass
def mainloop(self): def mainloop(self) -> None:
Image.core.eventloop() Image.core.eventloop()

View File

@ -57,7 +57,7 @@ def dump(c: Sequence[int | bytes]) -> None:
""".. deprecated:: 10.2.0""" """.. deprecated:: 10.2.0"""
deprecate("IptcImagePlugin.dump", 12) deprecate("IptcImagePlugin.dump", 12)
for i in c: for i in c:
print("%02x" % _i8(i), end=" ") print(f"{_i8(i):02x}", end=" ")
print() print()

View File

@ -63,12 +63,12 @@ class BoxReader:
data = self._read_bytes(size) data = self._read_bytes(size)
return struct.unpack(field_format, data) return struct.unpack(field_format, data)
def read_boxes(self): def read_boxes(self) -> BoxReader:
size = self.remaining_in_box size = self.remaining_in_box
data = self._read_bytes(size) data = self._read_bytes(size)
return BoxReader(io.BytesIO(data), size) return BoxReader(io.BytesIO(data), size)
def has_next_box(self): def has_next_box(self) -> bool:
if self.has_length: if self.has_length:
return self.fp.tell() + self.remaining_in_box < self.length return self.fp.tell() + self.remaining_in_box < self.length
else: else:
@ -215,7 +215,7 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
format = "JPEG2000" format = "JPEG2000"
format_description = "JPEG 2000 (ISO 15444)" format_description = "JPEG 2000 (ISO 15444)"
def _open(self): def _open(self) -> None:
sig = self.fp.read(4) sig = self.fp.read(4)
if sig == b"\xff\x4f\xff\x51": if sig == b"\xff\x4f\xff\x51":
self.codec = "j2k" self.codec = "j2k"
@ -267,7 +267,7 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
) )
] ]
def _parse_comment(self): def _parse_comment(self) -> None:
hdr = self.fp.read(2) hdr = self.fp.read(2)
length = _binary.i16be(hdr) length = _binary.i16be(hdr)
self.fp.seek(length - 2, os.SEEK_CUR) self.fp.seek(length - 2, os.SEEK_CUR)

View File

@ -408,7 +408,7 @@ class JpegImageFile(ImageFile.ImageFile):
msg = "no marker found" msg = "no marker found"
raise SyntaxError(msg) raise SyntaxError(msg)
def load_read(self, read_bytes): def load_read(self, read_bytes: int) -> bytes:
""" """
internal: read more image data internal: read more image data
For premature EOF and LOAD_TRUNCATED_IMAGES adds EOI marker For premature EOF and LOAD_TRUNCATED_IMAGES adds EOI marker
@ -462,7 +462,7 @@ class JpegImageFile(ImageFile.ImageFile):
box = (0, 0, original_size[0] / scale, original_size[1] / scale) box = (0, 0, original_size[0] / scale, original_size[1] / scale)
return self.mode, box return self.mode, box
def load_djpeg(self): def load_djpeg(self) -> None:
# ALTERNATIVE: handle JPEGs via the IJG command line utilities # ALTERNATIVE: handle JPEGs via the IJG command line utilities
f, path = tempfile.mkstemp() f, path = tempfile.mkstemp()

View File

@ -38,7 +38,7 @@ class MicImageFile(TiffImagePlugin.TiffImageFile):
format_description = "Microsoft Image Composer" format_description = "Microsoft Image Composer"
_close_exclusive_fp_after_loading = False _close_exclusive_fp_after_loading = False
def _open(self): def _open(self) -> None:
# read the OLE directory and see if this is a likely # read the OLE directory and see if this is a likely
# to be a Microsoft Image Composer file # to be a Microsoft Image Composer file
@ -88,7 +88,7 @@ class MicImageFile(TiffImagePlugin.TiffImageFile):
def tell(self): def tell(self):
return self.frame return self.frame
def close(self): def close(self) -> None:
self.__fp.close() self.__fp.close()
self.ole.close() self.ole.close()
super().close() super().close()

View File

@ -53,6 +53,10 @@ class BitStream:
return v return v
def _accept(prefix: bytes) -> bool:
return prefix[:4] == b"\x00\x00\x01\xb3"
## ##
# Image plugin for MPEG streams. This plugin can identify a stream, # Image plugin for MPEG streams. This plugin can identify a stream,
# but it cannot read it. # but it cannot read it.
@ -77,7 +81,7 @@ class MpegImageFile(ImageFile.ImageFile):
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# Registry stuff # Registry stuff
Image.register_open(MpegImageFile.format, MpegImageFile) Image.register_open(MpegImageFile.format, MpegImageFile, _accept)
Image.register_extensions(MpegImageFile.format, [".mpg", ".mpeg"]) Image.register_extensions(MpegImageFile.format, [".mpg", ".mpeg"])

View File

@ -100,7 +100,7 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
format_description = "MPO (CIPA DC-007)" format_description = "MPO (CIPA DC-007)"
_close_exclusive_fp_after_loading = False _close_exclusive_fp_after_loading = False
def _open(self): def _open(self) -> None:
self.fp.seek(0) # prep the fp in order to pass the JPEG test self.fp.seek(0) # prep the fp in order to pass the JPEG test
JpegImagePlugin.JpegImageFile._open(self) JpegImagePlugin.JpegImageFile._open(self)
self._after_jpeg_open() self._after_jpeg_open()
@ -124,10 +124,10 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
# for now we can only handle reading and individual frame extraction # for now we can only handle reading and individual frame extraction
self.readonly = 1 self.readonly = 1
def load_seek(self, pos): def load_seek(self, pos: int) -> None:
self._fp.seek(pos) self._fp.seek(pos)
def seek(self, frame): def seek(self, frame: int) -> None:
if not self._seek_check(frame): if not self._seek_check(frame):
return return
self.fp = self._fp self.fp = self._fp
@ -149,7 +149,7 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
self.tile = [("jpeg", (0, 0) + self.size, self.offset, self.tile[0][-1])] self.tile = [("jpeg", (0, 0) + self.size, self.offset, self.tile[0][-1])]
self.__frame = frame self.__frame = frame
def tell(self): def tell(self) -> int:
return self.__frame return self.__frame
@staticmethod @staticmethod

View File

@ -54,7 +54,7 @@ class PSDraw:
self.fp.write(b"%%EndProlog\n") self.fp.write(b"%%EndProlog\n")
self.isofont = {} self.isofont = {}
def end_document(self): def end_document(self) -> None:
"""Ends printing. (Write PostScript DSC footer.)""" """Ends printing. (Write PostScript DSC footer.)"""
self.fp.write(b"%%EndDocument\nrestore showpage\n%%End\n") self.fp.write(b"%%EndDocument\nrestore showpage\n%%End\n")
if hasattr(self.fp, "flush"): if hasattr(self.fp, "flush"):

View File

@ -87,10 +87,10 @@ class IndirectReferenceTuple(NamedTuple):
class IndirectReference(IndirectReferenceTuple): class IndirectReference(IndirectReferenceTuple):
def __str__(self): def __str__(self) -> str:
return f"{self.object_id} {self.generation} R" return f"{self.object_id} {self.generation} R"
def __bytes__(self): def __bytes__(self) -> bytes:
return self.__str__().encode("us-ascii") return self.__str__().encode("us-ascii")
def __eq__(self, other): def __eq__(self, other):
@ -108,7 +108,7 @@ class IndirectReference(IndirectReferenceTuple):
class IndirectObjectDef(IndirectReference): class IndirectObjectDef(IndirectReference):
def __str__(self): def __str__(self) -> str:
return f"{self.object_id} {self.generation} obj" return f"{self.object_id} {self.generation} obj"
@ -150,7 +150,7 @@ class XrefTable:
def __contains__(self, key): def __contains__(self, key):
return key in self.existing_entries or key in self.new_entries return key in self.existing_entries or key in self.new_entries
def __len__(self): def __len__(self) -> int:
return len( return len(
set(self.existing_entries.keys()) set(self.existing_entries.keys())
| set(self.new_entries.keys()) | set(self.new_entries.keys())
@ -211,7 +211,7 @@ class PdfName:
else: else:
self.name = name.encode("us-ascii") self.name = name.encode("us-ascii")
def name_as_str(self): def name_as_str(self) -> str:
return self.name.decode("us-ascii") return self.name.decode("us-ascii")
def __eq__(self, other): def __eq__(self, other):
@ -222,7 +222,7 @@ class PdfName:
def __hash__(self): def __hash__(self):
return hash(self.name) return hash(self.name)
def __repr__(self): def __repr__(self) -> str:
return f"{self.__class__.__name__}({repr(self.name)})" return f"{self.__class__.__name__}({repr(self.name)})"
@classmethod @classmethod
@ -231,7 +231,7 @@ class PdfName:
allowed_chars = set(range(33, 127)) - {ord(c) for c in "#%/()<>[]{}"} allowed_chars = set(range(33, 127)) - {ord(c) for c in "#%/()<>[]{}"}
def __bytes__(self): def __bytes__(self) -> bytes:
result = bytearray(b"/") result = bytearray(b"/")
for b in self.name: for b in self.name:
if b in self.allowed_chars: if b in self.allowed_chars:
@ -242,7 +242,7 @@ class PdfName:
class PdfArray(List[Any]): class PdfArray(List[Any]):
def __bytes__(self): def __bytes__(self) -> bytes:
return b"[ " + b" ".join(pdf_repr(x) for x in self) + b" ]" return b"[ " + b" ".join(pdf_repr(x) for x in self) + b" ]"
@ -286,7 +286,7 @@ class PdfDict(_DictBase):
value = time.gmtime(calendar.timegm(value) + offset) value = time.gmtime(calendar.timegm(value) + offset)
return value return value
def __bytes__(self): def __bytes__(self) -> bytes:
out = bytearray(b"<<") out = bytearray(b"<<")
for key, value in self.items(): for key, value in self.items():
if value is None: if value is None:
@ -304,7 +304,7 @@ class PdfBinary:
def __init__(self, data): def __init__(self, data):
self.data = data self.data = data
def __bytes__(self): def __bytes__(self) -> bytes:
return b"<%s>" % b"".join(b"%02X" % b for b in self.data) return b"<%s>" % b"".join(b"%02X" % b for b in self.data)
@ -409,28 +409,28 @@ class PdfParser:
self.close() self.close()
return False # do not suppress exceptions return False # do not suppress exceptions
def start_writing(self): def start_writing(self) -> None:
self.close_buf() self.close_buf()
self.seek_end() self.seek_end()
def close_buf(self): def close_buf(self) -> None:
try: try:
self.buf.close() self.buf.close()
except AttributeError: except AttributeError:
pass pass
self.buf = None self.buf = None
def close(self): def close(self) -> None:
if self.should_close_buf: if self.should_close_buf:
self.close_buf() self.close_buf()
if self.f is not None and self.should_close_file: if self.f is not None and self.should_close_file:
self.f.close() self.f.close()
self.f = None self.f = None
def seek_end(self): def seek_end(self) -> None:
self.f.seek(0, os.SEEK_END) self.f.seek(0, os.SEEK_END)
def write_header(self): def write_header(self) -> None:
self.f.write(b"%PDF-1.4\n") self.f.write(b"%PDF-1.4\n")
def write_comment(self, s): def write_comment(self, s):
@ -450,7 +450,7 @@ class PdfParser:
) )
return self.root_ref return self.root_ref
def rewrite_pages(self): def rewrite_pages(self) -> None:
pages_tree_nodes_to_delete = [] pages_tree_nodes_to_delete = []
for i, page_ref in enumerate(self.orig_pages): for i, page_ref in enumerate(self.orig_pages):
page_info = self.cached_objects[page_ref] page_info = self.cached_objects[page_ref]
@ -529,7 +529,7 @@ class PdfParser:
f.write(b"endobj\n") f.write(b"endobj\n")
return ref return ref
def del_root(self): def del_root(self) -> None:
if self.root_ref is None: if self.root_ref is None:
return return
del self.xref_table[self.root_ref.object_id] del self.xref_table[self.root_ref.object_id]
@ -547,7 +547,7 @@ class PdfParser:
except ValueError: # cannot mmap an empty file except ValueError: # cannot mmap an empty file
return b"" return b""
def read_pdf_info(self): def read_pdf_info(self) -> None:
self.file_size_total = len(self.buf) self.file_size_total = len(self.buf)
self.file_size_this = self.file_size_total - self.start_offset self.file_size_this = self.file_size_total - self.start_offset
self.read_trailer() self.read_trailer()
@ -823,11 +823,10 @@ class PdfParser:
m = cls.re_stream_start.match(data, offset) m = cls.re_stream_start.match(data, offset)
if m: if m:
try: try:
stream_len = int(result[b"Length"]) stream_len_str = result.get(b"Length")
except (TypeError, KeyError, ValueError) as e: stream_len = int(stream_len_str)
msg = "bad or missing Length in stream dict (%r)" % result.get( except (TypeError, ValueError) as e:
b"Length", None msg = f"bad or missing Length in stream dict ({stream_len_str})"
)
raise PdfFormatError(msg) from e raise PdfFormatError(msg) from e
stream_data = data[m.end() : m.end() + stream_len] stream_data = data[m.end() : m.end() + stream_len]
m = cls.re_stream_end.match(data, m.end() + stream_len) m = cls.re_stream_end.match(data, m.end() + stream_len)

View File

@ -39,6 +39,7 @@ import struct
import warnings import warnings
import zlib import zlib
from enum import IntEnum from enum import IntEnum
from typing import IO
from . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence from . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence
from ._binary import i16be as i16 from ._binary import i16be as i16
@ -149,14 +150,15 @@ def _crc32(data, seed=0):
class ChunkStream: class ChunkStream:
def __init__(self, fp): def __init__(self, fp: IO[bytes]) -> None:
self.fp = fp self.fp: IO[bytes] | None = fp
self.queue = [] self.queue: list[tuple[bytes, int, int]] | None = []
def read(self): def read(self) -> tuple[bytes, int, int]:
"""Fetch a new chunk. Returns header information.""" """Fetch a new chunk. Returns header information."""
cid = None cid = None
assert self.fp is not None
if self.queue: if self.queue:
cid, pos, length = self.queue.pop() cid, pos, length = self.queue.pop()
self.fp.seek(pos) self.fp.seek(pos)
@ -173,16 +175,17 @@ class ChunkStream:
return cid, pos, length return cid, pos, length
def __enter__(self): def __enter__(self) -> ChunkStream:
return self return self
def __exit__(self, *args): def __exit__(self, *args):
self.close() self.close()
def close(self): def close(self) -> None:
self.queue = self.fp = None self.queue = self.fp = None
def push(self, cid, pos, length): def push(self, cid: bytes, pos: int, length: int) -> None:
assert self.queue is not None
self.queue.append((cid, pos, length)) self.queue.append((cid, pos, length))
def call(self, cid, pos, length): def call(self, cid, pos, length):
@ -191,7 +194,7 @@ class ChunkStream:
logger.debug("STREAM %r %s %s", cid, pos, length) logger.debug("STREAM %r %s %s", cid, pos, length)
return getattr(self, f"chunk_{cid.decode('ascii')}")(pos, length) return getattr(self, f"chunk_{cid.decode('ascii')}")(pos, length)
def crc(self, cid, data): def crc(self, cid: bytes, data: bytes) -> None:
"""Read and verify checksum""" """Read and verify checksum"""
# Skip CRC checks for ancillary chunks if allowed to load truncated # Skip CRC checks for ancillary chunks if allowed to load truncated
@ -201,6 +204,7 @@ class ChunkStream:
self.crc_skip(cid, data) self.crc_skip(cid, data)
return return
assert self.fp is not None
try: try:
crc1 = _crc32(data, _crc32(cid)) crc1 = _crc32(data, _crc32(cid))
crc2 = i32(self.fp.read(4)) crc2 = i32(self.fp.read(4))
@ -211,12 +215,13 @@ class ChunkStream:
msg = f"broken PNG file (incomplete checksum in {repr(cid)})" msg = f"broken PNG file (incomplete checksum in {repr(cid)})"
raise SyntaxError(msg) from e raise SyntaxError(msg) from e
def crc_skip(self, cid, data): def crc_skip(self, cid: bytes, data: bytes) -> None:
"""Read checksum""" """Read checksum"""
assert self.fp is not None
self.fp.read(4) self.fp.read(4)
def verify(self, endchunk=b"IEND"): def verify(self, endchunk: bytes = b"IEND") -> list[bytes]:
# Simple approach; just calculate checksum for all remaining # Simple approach; just calculate checksum for all remaining
# blocks. Must be called directly after open. # blocks. Must be called directly after open.
@ -361,7 +366,7 @@ class PngStream(ChunkStream):
self.text_memory = 0 self.text_memory = 0
def check_text_memory(self, chunklen): def check_text_memory(self, chunklen: int) -> None:
self.text_memory += chunklen self.text_memory += chunklen
if self.text_memory > MAX_TEXT_MEMORY: if self.text_memory > MAX_TEXT_MEMORY:
msg = ( msg = (
@ -370,19 +375,19 @@ class PngStream(ChunkStream):
) )
raise ValueError(msg) raise ValueError(msg)
def save_rewind(self): def save_rewind(self) -> None:
self.rewind_state = { self.rewind_state = {
"info": self.im_info.copy(), "info": self.im_info.copy(),
"tile": self.im_tile, "tile": self.im_tile,
"seq_num": self._seq_num, "seq_num": self._seq_num,
} }
def rewind(self): def rewind(self) -> None:
self.im_info = self.rewind_state["info"].copy() self.im_info = self.rewind_state["info"].copy()
self.im_tile = self.rewind_state["tile"] self.im_tile = self.rewind_state["tile"]
self._seq_num = self.rewind_state["seq_num"] self._seq_num = self.rewind_state["seq_num"]
def chunk_iCCP(self, pos, length): def chunk_iCCP(self, pos: int, length: int) -> bytes:
# ICC profile # ICC profile
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
# according to PNG spec, the iCCP chunk contains: # according to PNG spec, the iCCP chunk contains:
@ -409,7 +414,7 @@ class PngStream(ChunkStream):
self.im_info["icc_profile"] = icc_profile self.im_info["icc_profile"] = icc_profile
return s return s
def chunk_IHDR(self, pos, length): def chunk_IHDR(self, pos: int, length: int) -> bytes:
# image header # image header
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
if length < 13: if length < 13:
@ -446,14 +451,14 @@ class PngStream(ChunkStream):
msg = "end of PNG image" msg = "end of PNG image"
raise EOFError(msg) raise EOFError(msg)
def chunk_PLTE(self, pos, length): def chunk_PLTE(self, pos: int, length: int) -> bytes:
# palette # palette
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
if self.im_mode == "P": if self.im_mode == "P":
self.im_palette = "RGB", s self.im_palette = "RGB", s
return s return s
def chunk_tRNS(self, pos, length): def chunk_tRNS(self, pos: int, length: int) -> bytes:
# transparency # transparency
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
if self.im_mode == "P": if self.im_mode == "P":
@ -473,13 +478,13 @@ class PngStream(ChunkStream):
self.im_info["transparency"] = i16(s), i16(s, 2), i16(s, 4) self.im_info["transparency"] = i16(s), i16(s, 2), i16(s, 4)
return s return s
def chunk_gAMA(self, pos, length): def chunk_gAMA(self, pos: int, length: int) -> bytes:
# gamma setting # gamma setting
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
self.im_info["gamma"] = i32(s) / 100000.0 self.im_info["gamma"] = i32(s) / 100000.0
return s return s
def chunk_cHRM(self, pos, length): def chunk_cHRM(self, pos: int, length: int) -> bytes:
# chromaticity, 8 unsigned ints, actual value is scaled by 100,000 # chromaticity, 8 unsigned ints, actual value is scaled by 100,000
# WP x,y, Red x,y, Green x,y Blue x,y # WP x,y, Red x,y, Green x,y Blue x,y
@ -488,7 +493,7 @@ class PngStream(ChunkStream):
self.im_info["chromaticity"] = tuple(elt / 100000.0 for elt in raw_vals) self.im_info["chromaticity"] = tuple(elt / 100000.0 for elt in raw_vals)
return s return s
def chunk_sRGB(self, pos, length): def chunk_sRGB(self, pos: int, length: int) -> bytes:
# srgb rendering intent, 1 byte # srgb rendering intent, 1 byte
# 0 perceptual # 0 perceptual
# 1 relative colorimetric # 1 relative colorimetric
@ -504,7 +509,7 @@ class PngStream(ChunkStream):
self.im_info["srgb"] = s[0] self.im_info["srgb"] = s[0]
return s return s
def chunk_pHYs(self, pos, length): def chunk_pHYs(self, pos: int, length: int) -> bytes:
# pixels per unit # pixels per unit
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
if length < 9: if length < 9:
@ -521,7 +526,7 @@ class PngStream(ChunkStream):
self.im_info["aspect"] = px, py self.im_info["aspect"] = px, py
return s return s
def chunk_tEXt(self, pos, length): def chunk_tEXt(self, pos: int, length: int) -> bytes:
# text # text
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
try: try:
@ -540,7 +545,7 @@ class PngStream(ChunkStream):
return s return s
def chunk_zTXt(self, pos, length): def chunk_zTXt(self, pos: int, length: int) -> bytes:
# compressed text # compressed text
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
try: try:
@ -574,7 +579,7 @@ class PngStream(ChunkStream):
return s return s
def chunk_iTXt(self, pos, length): def chunk_iTXt(self, pos: int, length: int) -> bytes:
# international text # international text
r = s = ImageFile._safe_read(self.fp, length) r = s = ImageFile._safe_read(self.fp, length)
try: try:
@ -614,13 +619,13 @@ class PngStream(ChunkStream):
return s return s
def chunk_eXIf(self, pos, length): def chunk_eXIf(self, pos: int, length: int) -> bytes:
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
self.im_info["exif"] = b"Exif\x00\x00" + s self.im_info["exif"] = b"Exif\x00\x00" + s
return s return s
# APNG chunks # APNG chunks
def chunk_acTL(self, pos, length): def chunk_acTL(self, pos: int, length: int) -> bytes:
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
if length < 8: if length < 8:
if ImageFile.LOAD_TRUNCATED_IMAGES: if ImageFile.LOAD_TRUNCATED_IMAGES:
@ -640,7 +645,7 @@ class PngStream(ChunkStream):
self.im_custom_mimetype = "image/apng" self.im_custom_mimetype = "image/apng"
return s return s
def chunk_fcTL(self, pos, length): def chunk_fcTL(self, pos: int, length: int) -> bytes:
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
if length < 26: if length < 26:
if ImageFile.LOAD_TRUNCATED_IMAGES: if ImageFile.LOAD_TRUNCATED_IMAGES:
@ -669,7 +674,7 @@ class PngStream(ChunkStream):
self.im_info["blend"] = s[25] self.im_info["blend"] = s[25]
return s return s
def chunk_fdAT(self, pos, length): def chunk_fdAT(self, pos: int, length: int) -> bytes:
if length < 4: if length < 4:
if ImageFile.LOAD_TRUNCATED_IMAGES: if ImageFile.LOAD_TRUNCATED_IMAGES:
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
@ -701,7 +706,7 @@ class PngImageFile(ImageFile.ImageFile):
format = "PNG" format = "PNG"
format_description = "Portable network graphics" format_description = "Portable network graphics"
def _open(self): def _open(self) -> None:
if not _accept(self.fp.read(8)): if not _accept(self.fp.read(8)):
msg = "not a PNG file" msg = "not a PNG file"
raise SyntaxError(msg) raise SyntaxError(msg)
@ -711,8 +716,8 @@ class PngImageFile(ImageFile.ImageFile):
# #
# Parse headers up to the first IDAT or fDAT chunk # Parse headers up to the first IDAT or fDAT chunk
self.private_chunks = [] self.private_chunks: list[tuple[bytes, bytes] | tuple[bytes, bytes, bool]] = []
self.png = PngStream(self.fp) self.png: PngStream | None = PngStream(self.fp)
while True: while True:
# #
@ -793,6 +798,7 @@ class PngImageFile(ImageFile.ImageFile):
# back up to beginning of IDAT block # back up to beginning of IDAT block
self.fp.seek(self.tile[0][2] - 8) self.fp.seek(self.tile[0][2] - 8)
assert self.png is not None
self.png.verify() self.png.verify()
self.png.close() self.png.close()
@ -800,7 +806,7 @@ class PngImageFile(ImageFile.ImageFile):
self.fp.close() self.fp.close()
self.fp = None self.fp = None
def seek(self, frame): def seek(self, frame: int) -> None:
if not self._seek_check(frame): if not self._seek_check(frame):
return return
if frame < self.__frame: if frame < self.__frame:
@ -909,10 +915,10 @@ class PngImageFile(ImageFile.ImageFile):
else: else:
self.dispose = None self.dispose = None
def tell(self): def tell(self) -> int:
return self.__frame return self.__frame
def load_prepare(self): def load_prepare(self) -> None:
"""internal: prepare to read PNG file""" """internal: prepare to read PNG file"""
if self.info.get("interlace"): if self.info.get("interlace"):
@ -921,9 +927,10 @@ class PngImageFile(ImageFile.ImageFile):
self.__idat = self.__prepare_idat # used by load_read() self.__idat = self.__prepare_idat # used by load_read()
ImageFile.ImageFile.load_prepare(self) ImageFile.ImageFile.load_prepare(self)
def load_read(self, read_bytes): def load_read(self, read_bytes: int) -> bytes:
"""internal: read more image data""" """internal: read more image data"""
assert self.png is not None
while self.__idat == 0: while self.__idat == 0:
# end of chunk, skip forward to next one # end of chunk, skip forward to next one
@ -954,8 +961,9 @@ class PngImageFile(ImageFile.ImageFile):
return self.fp.read(read_bytes) return self.fp.read(read_bytes)
def load_end(self): def load_end(self) -> None:
"""internal: finished reading image data""" """internal: finished reading image data"""
assert self.png is not None
if self.__idat != 0: if self.__idat != 0:
self.fp.read(self.__idat) self.fp.read(self.__idat)
while True: while True:
@ -1042,22 +1050,22 @@ class PngImageFile(ImageFile.ImageFile):
# PNG writer # PNG writer
_OUTMODES = { _OUTMODES = {
# supported PIL modes, and corresponding rawmodes/bits/color combinations # supported PIL modes, and corresponding rawmode, bit depth and color type
"1": ("1", b"\x01\x00"), "1": ("1", b"\x01", b"\x00"),
"L;1": ("L;1", b"\x01\x00"), "L;1": ("L;1", b"\x01", b"\x00"),
"L;2": ("L;2", b"\x02\x00"), "L;2": ("L;2", b"\x02", b"\x00"),
"L;4": ("L;4", b"\x04\x00"), "L;4": ("L;4", b"\x04", b"\x00"),
"L": ("L", b"\x08\x00"), "L": ("L", b"\x08", b"\x00"),
"LA": ("LA", b"\x08\x04"), "LA": ("LA", b"\x08", b"\x04"),
"I": ("I;16B", b"\x10\x00"), "I": ("I;16B", b"\x10", b"\x00"),
"I;16": ("I;16B", b"\x10\x00"), "I;16": ("I;16B", b"\x10", b"\x00"),
"I;16B": ("I;16B", b"\x10\x00"), "I;16B": ("I;16B", b"\x10", b"\x00"),
"P;1": ("P;1", b"\x01\x03"), "P;1": ("P;1", b"\x01", b"\x03"),
"P;2": ("P;2", b"\x02\x03"), "P;2": ("P;2", b"\x02", b"\x03"),
"P;4": ("P;4", b"\x04\x03"), "P;4": ("P;4", b"\x04", b"\x03"),
"P": ("P", b"\x08\x03"), "P": ("P", b"\x08", b"\x03"),
"RGB": ("RGB", b"\x08\x02"), "RGB": ("RGB", b"\x08", b"\x02"),
"RGBA": ("RGBA", b"\x08\x06"), "RGBA": ("RGBA", b"\x08", b"\x06"),
} }
@ -1079,7 +1087,7 @@ class _idat:
self.fp = fp self.fp = fp
self.chunk = chunk self.chunk = chunk
def write(self, data): def write(self, data: bytes) -> None:
self.chunk(self.fp, b"IDAT", data) self.chunk(self.fp, b"IDAT", data)
@ -1091,7 +1099,7 @@ class _fdat:
self.chunk = chunk self.chunk = chunk
self.seq_num = seq_num self.seq_num = seq_num
def write(self, data): def write(self, data: bytes) -> None:
self.chunk(self.fp, b"fdAT", o32(self.seq_num), data) self.chunk(self.fp, b"fdAT", o32(self.seq_num), data)
self.seq_num += 1 self.seq_num += 1
@ -1286,7 +1294,7 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False):
# get the corresponding PNG mode # get the corresponding PNG mode
try: try:
rawmode, mode = _OUTMODES[mode] rawmode, bit_depth, color_type = _OUTMODES[mode]
except KeyError as e: except KeyError as e:
msg = f"cannot write mode {mode} as PNG" msg = f"cannot write mode {mode} as PNG"
raise OSError(msg) from e raise OSError(msg) from e
@ -1301,7 +1309,8 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False):
b"IHDR", b"IHDR",
o32(size[0]), # 0: size o32(size[0]), # 0: size
o32(size[1]), o32(size[1]),
mode, # 8: depth/type bit_depth,
color_type,
b"\0", # 10: compression b"\0", # 10: compression
b"\0", # 11: filter category b"\0", # 11: filter category
b"\0", # 12: interlace flag b"\0", # 12: interlace flag
@ -1436,10 +1445,10 @@ def getchunks(im, **params):
class collector: class collector:
data = [] data = []
def write(self, data): def write(self, data: bytes) -> None:
pass pass
def append(self, chunk): def append(self, chunk: bytes) -> None:
self.data.append(chunk) self.data.append(chunk)
def append(fp, cid, *data): def append(fp, cid, *data):

View File

@ -57,7 +57,7 @@ class PsdImageFile(ImageFile.ImageFile):
format_description = "Adobe Photoshop" format_description = "Adobe Photoshop"
_close_exclusive_fp_after_loading = False _close_exclusive_fp_after_loading = False
def _open(self): def _open(self) -> None:
read = self.fp.read read = self.fp.read
# #
@ -141,23 +141,22 @@ class PsdImageFile(ImageFile.ImageFile):
self.frame = 1 self.frame = 1
self._min_frame = 1 self._min_frame = 1
def seek(self, layer): def seek(self, layer: int) -> None:
if not self._seek_check(layer): if not self._seek_check(layer):
return return
# seek to given layer (1..max) # seek to given layer (1..max)
try: try:
name, mode, bbox, tile = self.layers[layer - 1] _, mode, _, tile = self.layers[layer - 1]
self._mode = mode self._mode = mode
self.tile = tile self.tile = tile
self.frame = layer self.frame = layer
self.fp = self._fp self.fp = self._fp
return name, bbox
except IndexError as e: except IndexError as e:
msg = "no such layer" msg = "no such layer"
raise EOFError(msg) from e raise EOFError(msg) from e
def tell(self): def tell(self) -> int:
# return layer number (0=image, 1..max=layers) # return layer number (0=image, 1..max=layers)
return self.frame return self.frame

View File

@ -21,7 +21,7 @@ class QoiImageFile(ImageFile.ImageFile):
format = "QOI" format = "QOI"
format_description = "Quite OK Image" format_description = "Quite OK Image"
def _open(self): def _open(self) -> None:
if not _accept(self.fp.read(4)): if not _accept(self.fp.read(4)):
msg = "not a QOI file" msg = "not a QOI file"
raise SyntaxError(msg) raise SyntaxError(msg)

View File

@ -37,6 +37,7 @@ from __future__ import annotations
import os import os
import struct import struct
import sys import sys
from typing import TYPE_CHECKING
from . import Image, ImageFile from . import Image, ImageFile
@ -97,7 +98,7 @@ class SpiderImageFile(ImageFile.ImageFile):
format_description = "Spider 2D image" format_description = "Spider 2D image"
_close_exclusive_fp_after_loading = False _close_exclusive_fp_after_loading = False
def _open(self): def _open(self) -> None:
# check header # check header
n = 27 * 4 # read 27 float values n = 27 * 4 # read 27 float values
f = self.fp.read(n) f = self.fp.read(n)
@ -157,21 +158,21 @@ class SpiderImageFile(ImageFile.ImageFile):
self._fp = self.fp # FIXME: hack self._fp = self.fp # FIXME: hack
@property @property
def n_frames(self): def n_frames(self) -> int:
return self._nimages return self._nimages
@property @property
def is_animated(self): def is_animated(self) -> bool:
return self._nimages > 1 return self._nimages > 1
# 1st image index is zero (although SPIDER imgnumber starts at 1) # 1st image index is zero (although SPIDER imgnumber starts at 1)
def tell(self): def tell(self) -> int:
if self.imgnumber < 1: if self.imgnumber < 1:
return 0 return 0
else: else:
return self.imgnumber - 1 return self.imgnumber - 1
def seek(self, frame): def seek(self, frame: int) -> None:
if self.istack == 0: if self.istack == 0:
msg = "attempt to seek in a non-stack file" msg = "attempt to seek in a non-stack file"
raise EOFError(msg) raise EOFError(msg)
@ -191,8 +192,11 @@ class SpiderImageFile(ImageFile.ImageFile):
b = -m * minimum b = -m * minimum
return self.point(lambda i, m=m, b=b: i * m + b).convert("L") return self.point(lambda i, m=m, b=b: i * m + b).convert("L")
if TYPE_CHECKING:
from . import ImageTk
# returns a ImageTk.PhotoImage object, after rescaling to 0..255 # returns a ImageTk.PhotoImage object, after rescaling to 0..255
def tkPhotoImage(self): def tkPhotoImage(self) -> ImageTk.PhotoImage:
from . import ImageTk from . import ImageTk
return ImageTk.PhotoImage(self.convert2byte(), palette=256) return ImageTk.PhotoImage(self.convert2byte(), palette=256)

View File

@ -381,7 +381,7 @@ class IFDRational(Rational):
f = self._val.limit_denominator(max_denominator) f = self._val.limit_denominator(max_denominator)
return f.numerator, f.denominator return f.numerator, f.denominator
def __repr__(self): def __repr__(self) -> str:
return str(float(self._val)) return str(float(self._val))
def __hash__(self): def __hash__(self):
@ -603,7 +603,7 @@ class ImageFileDirectory_v2(_IFDv2Base):
self._next = None self._next = None
self._offset = None self._offset = None
def __str__(self): def __str__(self) -> str:
return str(dict(self)) return str(dict(self))
def named(self): def named(self):
@ -617,7 +617,7 @@ class ImageFileDirectory_v2(_IFDv2Base):
for code, value in self.items() for code, value in self.items()
} }
def __len__(self): def __len__(self) -> int:
return len(set(self._tagdata) | set(self._tags_v2)) return len(set(self._tagdata) | set(self._tags_v2))
def __getitem__(self, tag): def __getitem__(self, tag):
@ -1041,7 +1041,7 @@ class ImageFileDirectory_v1(ImageFileDirectory_v2):
ifd.next = original.next # an indicator for multipage tiffs ifd.next = original.next # an indicator for multipage tiffs
return ifd return ifd
def to_v2(self): def to_v2(self) -> ImageFileDirectory_v2:
"""Returns an """Returns an
:py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v2` :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v2`
instance with the same data as is contained in the original instance with the same data as is contained in the original
@ -1061,7 +1061,7 @@ class ImageFileDirectory_v1(ImageFileDirectory_v2):
def __contains__(self, tag): def __contains__(self, tag):
return tag in self._tags_v1 or tag in self._tagdata return tag in self._tags_v1 or tag in self._tagdata
def __len__(self): def __len__(self) -> int:
return len(set(self._tagdata) | set(self._tags_v1)) return len(set(self._tagdata) | set(self._tags_v1))
def __iter__(self): def __iter__(self):
@ -1143,7 +1143,7 @@ class TiffImageFile(ImageFile.ImageFile):
self.seek(current) self.seek(current)
return self._n_frames return self._n_frames
def seek(self, frame): def seek(self, frame: int) -> None:
"""Select a given frame as current image""" """Select a given frame as current image"""
if not self._seek_check(frame): if not self._seek_check(frame):
return return
@ -1154,7 +1154,7 @@ class TiffImageFile(ImageFile.ImageFile):
Image._decompression_bomb_check(self.size) Image._decompression_bomb_check(self.size)
self.im = Image.core.new(self.mode, self.size) self.im = Image.core.new(self.mode, self.size)
def _seek(self, frame): def _seek(self, frame: int) -> None:
self.fp = self._fp self.fp = self._fp
# reset buffered io handle in case fp # reset buffered io handle in case fp
@ -1198,7 +1198,7 @@ class TiffImageFile(ImageFile.ImageFile):
self.__frame = frame self.__frame = frame
self._setup() self._setup()
def tell(self): def tell(self) -> int:
"""Return the current frame number""" """Return the current frame number"""
return self.__frame return self.__frame
@ -1237,7 +1237,7 @@ class TiffImageFile(ImageFile.ImageFile):
return self._load_libtiff() return self._load_libtiff()
return super().load() return super().load()
def load_end(self): def load_end(self) -> None:
# allow closing if we're on the first frame, there's no next # allow closing if we're on the first frame, there's no next
# This is the ImageFile.load path only, libtiff specific below. # This is the ImageFile.load path only, libtiff specific below.
if not self.is_animated: if not self.is_animated:
@ -1942,7 +1942,7 @@ class AppendingTiffWriter:
self.beginning = self.f.tell() self.beginning = self.f.tell()
self.setup() self.setup()
def setup(self): def setup(self) -> None:
# Reset everything. # Reset everything.
self.f.seek(self.beginning, os.SEEK_SET) self.f.seek(self.beginning, os.SEEK_SET)
@ -1967,7 +1967,7 @@ class AppendingTiffWriter:
self.skipIFDs() self.skipIFDs()
self.goToEnd() self.goToEnd()
def finalize(self): def finalize(self) -> None:
if self.isFirst: if self.isFirst:
return return
@ -1990,7 +1990,7 @@ class AppendingTiffWriter:
self.f.seek(ifd_offset) self.f.seek(ifd_offset)
self.fixIFD() self.fixIFD()
def newFrame(self): def newFrame(self) -> None:
# Call this to finish a frame. # Call this to finish a frame.
self.finalize() self.finalize()
self.setup() self.setup()
@ -2003,7 +2003,7 @@ class AppendingTiffWriter:
self.close() self.close()
return False return False
def tell(self): def tell(self) -> int:
return self.f.tell() - self.offsetOfNewPage return self.f.tell() - self.offsetOfNewPage
def seek(self, offset, whence=io.SEEK_SET): def seek(self, offset, whence=io.SEEK_SET):
@ -2013,7 +2013,7 @@ class AppendingTiffWriter:
self.f.seek(offset, whence) self.f.seek(offset, whence)
return self.tell() return self.tell()
def goToEnd(self): def goToEnd(self) -> None:
self.f.seek(0, os.SEEK_END) self.f.seek(0, os.SEEK_END)
pos = self.f.tell() pos = self.f.tell()
@ -2029,7 +2029,7 @@ class AppendingTiffWriter:
self.shortFmt = f"{self.endian}H" self.shortFmt = f"{self.endian}H"
self.tagFormat = f"{self.endian}HHL" self.tagFormat = f"{self.endian}HHL"
def skipIFDs(self): def skipIFDs(self) -> None:
while True: while True:
ifd_offset = self.readLong() ifd_offset = self.readLong()
if ifd_offset == 0: if ifd_offset == 0:
@ -2084,11 +2084,11 @@ class AppendingTiffWriter:
msg = f"wrote only {bytes_written} bytes but wanted 4" msg = f"wrote only {bytes_written} bytes but wanted 4"
raise RuntimeError(msg) raise RuntimeError(msg)
def close(self): def close(self) -> None:
self.finalize() self.finalize()
self.f.close() self.f.close()
def fixIFD(self): def fixIFD(self) -> None:
num_tags = self.readShort() num_tags = self.readShort()
for i in range(num_tags): for i in range(num_tags):

View File

@ -32,7 +32,7 @@ class WalImageFile(ImageFile.ImageFile):
format = "WAL" format = "WAL"
format_description = "Quake2 Texture" format_description = "Quake2 Texture"
def _open(self): def _open(self) -> None:
self._mode = "P" self._mode = "P"
# read header fields # read header fields

View File

@ -43,7 +43,7 @@ class WebPImageFile(ImageFile.ImageFile):
__loaded = 0 __loaded = 0
__logical_frame = 0 __logical_frame = 0
def _open(self): def _open(self) -> None:
if not _webp.HAVE_WEBPANIM: if not _webp.HAVE_WEBPANIM:
# Legacy mode # Legacy mode
data, width, height, self._mode, icc_profile, exif = _webp.WebPDecode( data, width, height, self._mode, icc_profile, exif = _webp.WebPDecode(
@ -109,7 +109,7 @@ class WebPImageFile(ImageFile.ImageFile):
""" """
return self._getxmp(self.info["xmp"]) if "xmp" in self.info else {} return self._getxmp(self.info["xmp"]) if "xmp" in self.info else {}
def seek(self, frame): def seek(self, frame: int) -> None:
if not self._seek_check(frame): if not self._seek_check(frame):
return return
@ -144,7 +144,7 @@ class WebPImageFile(ImageFile.ImageFile):
timestamp -= duration timestamp -= duration
return data, timestamp, duration return data, timestamp, duration
def _seek(self, frame): def _seek(self, frame: int) -> None:
if self.__physical_frame == frame: if self.__physical_frame == frame:
return # Nothing to do return # Nothing to do
if frame < self.__physical_frame: if frame < self.__physical_frame:
@ -171,10 +171,10 @@ class WebPImageFile(ImageFile.ImageFile):
return super().load() return super().load()
def load_seek(self, pos): def load_seek(self, pos: int) -> None:
pass pass
def tell(self): def tell(self) -> int:
if not _webp.HAVE_WEBPANIM: if not _webp.HAVE_WEBPANIM:
return super().tell() return super().tell()

View File

@ -79,7 +79,7 @@ class WmfStubImageFile(ImageFile.StubImageFile):
format = "WMF" format = "WMF"
format_description = "Windows Metafile" format_description = "Windows Metafile"
def _open(self): def _open(self) -> None:
self._inch = None self._inch = None
# check placable header # check placable header

View File

@ -36,7 +36,7 @@ class XpmImageFile(ImageFile.ImageFile):
format = "XPM" format = "XPM"
format_description = "X11 Pixel Map" format_description = "X11 Pixel Map"
def _open(self): def _open(self) -> None:
if not _accept(self.fp.read(9)): if not _accept(self.fp.read(9)):
msg = "not an XPM file" msg = "not an XPM file"
raise SyntaxError(msg) raise SyntaxError(msg)
@ -103,16 +103,13 @@ class XpmImageFile(ImageFile.ImageFile):
self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), ("P", 0, 1))] self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), ("P", 0, 1))]
def load_read(self, read_bytes): def load_read(self, read_bytes: int) -> bytes:
# #
# load all image data in one chunk # load all image data in one chunk
xsize, ysize = self.size xsize, ysize = self.size
s = [None] * ysize s = [self.fp.readline()[1 : xsize + 1].ljust(xsize) for i in range(ysize)]
for i in range(ysize):
s[i] = self.fp.readline()[1 : xsize + 1].ljust(xsize)
return b"".join(s) return b"".join(s)

View File

@ -18,7 +18,7 @@ modules = {
} }
def check_module(feature): def check_module(feature: str) -> bool:
""" """
Checks if a module is available. Checks if a module is available.
@ -42,7 +42,7 @@ def check_module(feature):
return False return False
def version_module(feature): def version_module(feature: str) -> str | None:
""" """
:param feature: The module to check for. :param feature: The module to check for.
:returns: :returns:
@ -54,13 +54,10 @@ def version_module(feature):
module, ver = modules[feature] module, ver = modules[feature]
if ver is None:
return None
return getattr(__import__(module, fromlist=[ver]), ver) return getattr(__import__(module, fromlist=[ver]), ver)
def get_supported_modules(): def get_supported_modules() -> list[str]:
""" """
:returns: A list of all supported modules. :returns: A list of all supported modules.
""" """
@ -75,7 +72,7 @@ codecs = {
} }
def check_codec(feature): def check_codec(feature: str) -> bool:
""" """
Checks if a codec is available. Checks if a codec is available.
@ -92,7 +89,7 @@ def check_codec(feature):
return f"{codec}_encoder" in dir(Image.core) return f"{codec}_encoder" in dir(Image.core)
def version_codec(feature): def version_codec(feature: str) -> str | None:
""" """
:param feature: The codec to check for. :param feature: The codec to check for.
:returns: :returns:
@ -113,7 +110,7 @@ def version_codec(feature):
return version return version
def get_supported_codecs(): def get_supported_codecs() -> list[str]:
""" """
:returns: A list of all supported codecs. :returns: A list of all supported codecs.
""" """
@ -133,7 +130,7 @@ features = {
} }
def check_feature(feature): def check_feature(feature: str) -> bool | None:
""" """
Checks if a feature is available. Checks if a feature is available.
@ -157,7 +154,7 @@ def check_feature(feature):
return None return None
def version_feature(feature): def version_feature(feature: str) -> str | None:
""" """
:param feature: The feature to check for. :param feature: The feature to check for.
:returns: The version number as a string, or ``None`` if not available. :returns: The version number as a string, or ``None`` if not available.
@ -174,14 +171,14 @@ def version_feature(feature):
return getattr(__import__(module, fromlist=[ver]), ver) return getattr(__import__(module, fromlist=[ver]), ver)
def get_supported_features(): def get_supported_features() -> list[str]:
""" """
:returns: A list of all supported features. :returns: A list of all supported features.
""" """
return [f for f in features if check_feature(f)] return [f for f in features if check_feature(f)]
def check(feature): def check(feature: str) -> bool | None:
""" """
:param feature: A module, codec, or feature name. :param feature: A module, codec, or feature name.
:returns: :returns:
@ -199,7 +196,7 @@ def check(feature):
return False return False
def version(feature): def version(feature: str) -> str | None:
""" """
:param feature: :param feature:
The module, codec, or feature to check for. The module, codec, or feature to check for.
@ -215,7 +212,7 @@ def version(feature):
return None return None
def get_supported(): def get_supported() -> list[str]:
""" """
:returns: A list of all supported modules, features, and codecs. :returns: A list of all supported modules, features, and codecs.
""" """

View File

@ -128,14 +128,7 @@ PyImagingPhotoPut(
block.pixelPtr = (unsigned char *)im->block; block.pixelPtr = (unsigned char *)im->block;
TK_PHOTO_PUT_BLOCK( TK_PHOTO_PUT_BLOCK(
interp, interp, photo, &block, 0, 0, block.width, block.height, TK_PHOTO_COMPOSITE_SET);
photo,
&block,
0,
0,
block.width,
block.height,
TK_PHOTO_COMPOSITE_SET);
return TCL_OK; return TCL_OK;
} }
@ -400,8 +393,8 @@ _func_loader(void *lib) {
return 1; return 1;
} }
return ( return (
(TK_PHOTO_PUT_BLOCK = (TK_PHOTO_PUT_BLOCK = (Tk_PhotoPutBlock_t)_dfunc(lib, "Tk_PhotoPutBlock")) ==
(Tk_PhotoPutBlock_t)_dfunc(lib, "Tk_PhotoPutBlock")) == NULL); NULL);
} }
int int

View File

@ -533,7 +533,9 @@ getink(PyObject *color, Imaging im, char *ink) {
/* unsigned integer, single layer */ /* unsigned integer, single layer */
if (rIsInt != 1) { if (rIsInt != 1) {
if (tupleSize != 1) { if (tupleSize != 1) {
PyErr_SetString(PyExc_TypeError, "color must be int or single-element tuple"); PyErr_SetString(
PyExc_TypeError,
"color must be int or single-element tuple");
return NULL; return NULL;
} else if (!PyArg_ParseTuple(color, "L", &r)) { } else if (!PyArg_ParseTuple(color, "L", &r)) {
return NULL; return NULL;
@ -552,7 +554,9 @@ getink(PyObject *color, Imaging im, char *ink) {
a = 255; a = 255;
if (im->bands == 2) { if (im->bands == 2) {
if (tupleSize != 1 && tupleSize != 2) { if (tupleSize != 1 && tupleSize != 2) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or two elements"); PyErr_SetString(
PyExc_TypeError,
"color must be int, or tuple of one or two elements");
return NULL; return NULL;
} else if (!PyArg_ParseTuple(color, "L|i", &r, &a)) { } else if (!PyArg_ParseTuple(color, "L|i", &r, &a)) {
return NULL; return NULL;
@ -560,7 +564,10 @@ getink(PyObject *color, Imaging im, char *ink) {
g = b = r; g = b = r;
} else { } else {
if (tupleSize != 3 && tupleSize != 4) { if (tupleSize != 3 && tupleSize != 4) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one, three or four elements"); PyErr_SetString(
PyExc_TypeError,
"color must be int, or tuple of one, three or four "
"elements");
return NULL; return NULL;
} else if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a)) { } else if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a)) {
return NULL; return NULL;
@ -599,7 +606,9 @@ getink(PyObject *color, Imaging im, char *ink) {
g = (UINT8)(r >> 8); g = (UINT8)(r >> 8);
r = (UINT8)r; r = (UINT8)r;
} else if (tupleSize != 3) { } else if (tupleSize != 3) {
PyErr_SetString(PyExc_TypeError, "color must be int, or tuple of one or three elements"); PyErr_SetString(
PyExc_TypeError,
"color must be int, or tuple of one or three elements");
return NULL; return NULL;
} else if (!PyArg_ParseTuple(color, "iiL", &b, &g, &r)) { } else if (!PyArg_ParseTuple(color, "iiL", &b, &g, &r)) {
return NULL; return NULL;
@ -1596,8 +1605,10 @@ if (PySequence_Check(op)) { \
value = value * scale + offset; value = value * scale + offset;
} }
if (image->type == IMAGING_TYPE_SPECIAL) { if (image->type == IMAGING_TYPE_SPECIAL) {
image->image8[y][x * 2 + (bigendian ? 1 : 0)] = CLIP8((int)value % 256); image->image8[y][x * 2 + (bigendian ? 1 : 0)] =
image->image8[y][x * 2 + (bigendian ? 0 : 1)] = CLIP8((int)value >> 8); CLIP8((int)value % 256);
image->image8[y][x * 2 + (bigendian ? 0 : 1)] =
CLIP8((int)value >> 8);
} else { } else {
image->image8[y][x] = (UINT8)CLIP8(value); image->image8[y][x] = (UINT8)CLIP8(value);
} }
@ -1639,8 +1650,7 @@ if (PySequence_Check(op)) { \
for (i = x = y = 0; i < n; i++) { for (i = x = y = 0; i < n; i++) {
double value; double value;
set_value_to_item(seq, i); set_value_to_item(seq, i);
IMAGING_PIXEL_INT32(image, x, y) = IMAGING_PIXEL_INT32(image, x, y) = (INT32)(value * scale + offset);
(INT32)(value * scale + offset);
if (++x >= (int)image->xsize) { if (++x >= (int)image->xsize) {
x = 0, y++; x = 0, y++;
} }
@ -2785,8 +2795,8 @@ _font_getmask(ImagingFontObject *self, PyObject *args) {
glyph = &self->glyphs[text[i]]; glyph = &self->glyphs[text[i]];
if (i == 0 || text[i] != text[i - 1]) { if (i == 0 || text[i] != text[i - 1]) {
ImagingDelete(bitmap); ImagingDelete(bitmap);
bitmap = bitmap = ImagingCrop(
ImagingCrop(self->bitmap, glyph->sx0, glyph->sy0, glyph->sx1, glyph->sy1); self->bitmap, glyph->sx0, glyph->sy0, glyph->sx1, glyph->sy1);
if (!bitmap) { if (!bitmap) {
goto failed; goto failed;
} }
@ -3315,7 +3325,8 @@ _draw_polygon(ImagingDrawObject *self, PyObject *args) {
free(xy); free(xy);
if (ImagingDrawPolygon(self->image->image, n, ixy, &ink, fill, width, self->blend) < 0) { if (ImagingDrawPolygon(self->image->image, n, ixy, &ink, fill, width, self->blend) <
0) {
free(ixy); free(ixy);
return NULL; return NULL;
} }
@ -4411,7 +4422,8 @@ setup_module(PyObject *m) {
PyModule_AddObject(m, "HAVE_XCB", have_xcb); PyModule_AddObject(m, "HAVE_XCB", have_xcb);
PyObject *pillow_version = PyUnicode_FromString(version); PyObject *pillow_version = PyUnicode_FromString(version);
PyDict_SetItemString(d, "PILLOW_VERSION", pillow_version ? pillow_version : Py_None); PyDict_SetItemString(
d, "PILLOW_VERSION", pillow_version ? pillow_version : Py_None);
Py_XDECREF(pillow_version); Py_XDECREF(pillow_version);
return 0; return 0;

View File

@ -213,11 +213,8 @@ cms_transform_dealloc(CmsTransformObject *self) {
static cmsUInt32Number static cmsUInt32Number
findLCMStype(char *PILmode) { findLCMStype(char *PILmode) {
if ( if (strcmp(PILmode, "RGB") == 0 || strcmp(PILmode, "RGBA") == 0 ||
strcmp(PILmode, "RGB") == 0 || strcmp(PILmode, "RGBX") == 0) {
strcmp(PILmode, "RGBA") == 0 ||
strcmp(PILmode, "RGBX") == 0
) {
return TYPE_RGBA_8; return TYPE_RGBA_8;
} }
if (strcmp(PILmode, "RGBA;16B") == 0) { if (strcmp(PILmode, "RGBA;16B") == 0) {
@ -232,10 +229,7 @@ findLCMStype(char *PILmode) {
if (strcmp(PILmode, "L;16B") == 0) { if (strcmp(PILmode, "L;16B") == 0) {
return TYPE_GRAY_16_SE; return TYPE_GRAY_16_SE;
} }
if ( if (strcmp(PILmode, "YCCA") == 0 || strcmp(PILmode, "YCC") == 0) {
strcmp(PILmode, "YCCA") == 0 ||
strcmp(PILmode, "YCC") == 0
) {
return TYPE_YCbCr_8; return TYPE_YCbCr_8;
} }
if (strcmp(PILmode, "LAB") == 0) { if (strcmp(PILmode, "LAB") == 0) {
@ -391,7 +385,7 @@ _buildTransform(
iRenderingIntent, iRenderingIntent,
cmsFLAGS); cmsFLAGS);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS;
if (!hTransform) { if (!hTransform) {
PyErr_SetString(PyExc_ValueError, "cannot build transform"); PyErr_SetString(PyExc_ValueError, "cannot build transform");
@ -425,7 +419,7 @@ _buildProofTransform(
iProofIntent, iProofIntent,
cmsFLAGS); cmsFLAGS);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS;
if (!hTransform) { if (!hTransform) {
PyErr_SetString(PyExc_ValueError, "cannot build proof transform"); PyErr_SetString(PyExc_ValueError, "cannot build proof transform");
@ -622,7 +616,7 @@ cms_profile_is_intent_supported(CmsProfileObject *self, PyObject *args) {
static PyObject * static PyObject *
cms_get_display_profile_win32(PyObject *self, PyObject *args) { cms_get_display_profile_win32(PyObject *self, PyObject *args) {
char filename[MAX_PATH]; char filename[MAX_PATH];
cmsUInt32Number filename_size; DWORD filename_size;
BOOL ok; BOOL ok;
HANDLE handle = 0; HANDLE handle = 0;

View File

@ -490,8 +490,7 @@ text_layout(
size_t count; size_t count;
#ifdef HAVE_RAQM #ifdef HAVE_RAQM
if (have_raqm && self->layout_engine == LAYOUT_RAQM) { if (have_raqm && self->layout_engine == LAYOUT_RAQM) {
count = text_layout_raqm( count = text_layout_raqm(string, self, dir, features, lang, glyph_info);
string, self, dir, features, lang, glyph_info);
} else } else
#endif #endif
{ {
@ -550,7 +549,17 @@ font_getlength(FontObject *self, PyObject *args) {
} }
static int static int
bounding_box_and_anchors(FT_Face face, const char *anchor, int horizontal_dir, GlyphInfo *glyph_info, size_t count, int load_flags, int *width, int *height, int *x_offset, int *y_offset) { bounding_box_and_anchors(
FT_Face face,
const char *anchor,
int horizontal_dir,
GlyphInfo *glyph_info,
size_t count,
int load_flags,
int *width,
int *height,
int *x_offset,
int *y_offset) {
int position; /* pen position along primary axis, in 26.6 precision */ int position; /* pen position along primary axis, in 26.6 precision */
int advanced; /* pen position along primary axis, in pixels */ int advanced; /* pen position along primary axis, in pixels */
int px, py; /* position of current glyph, in pixels */ int px, py; /* position of current glyph, in pixels */
@ -654,8 +663,7 @@ bounding_box_and_anchors(FT_Face face, const char *anchor, int horizontal_dir, G
break; break;
case 'm': // middle (ascender + descender) / 2 case 'm': // middle (ascender + descender) / 2
y_anchor = PIXEL( y_anchor = PIXEL(
(face->size->metrics.ascender + (face->size->metrics.ascender + face->size->metrics.descender) /
face->size->metrics.descender) /
2); 2);
break; break;
case 's': // horizontal baseline case 's': // horizontal baseline
@ -758,7 +766,17 @@ font_getsize(FontObject *self, PyObject *args) {
load_flags |= FT_LOAD_COLOR; load_flags |= FT_LOAD_COLOR;
} }
error = bounding_box_and_anchors(self->face, anchor, horizontal_dir, glyph_info, count, load_flags, &width, &height, &x_offset, &y_offset); error = bounding_box_and_anchors(
self->face,
anchor,
horizontal_dir,
glyph_info,
count,
load_flags,
&width,
&height,
&x_offset,
&y_offset);
if (glyph_info) { if (glyph_info) {
PyMem_Free(glyph_info); PyMem_Free(glyph_info);
glyph_info = NULL; glyph_info = NULL;
@ -767,12 +785,7 @@ font_getsize(FontObject *self, PyObject *args) {
return NULL; return NULL;
} }
return Py_BuildValue( return Py_BuildValue("(ii)(ii)", width, height, x_offset, y_offset);
"(ii)(ii)",
width,
height,
x_offset,
y_offset);
} }
static PyObject * static PyObject *
@ -869,7 +882,17 @@ font_render(FontObject *self, PyObject *args) {
horizontal_dir = dir && strcmp(dir, "ttb") == 0 ? 0 : 1; horizontal_dir = dir && strcmp(dir, "ttb") == 0 ? 0 : 1;
error = bounding_box_and_anchors(self->face, anchor, horizontal_dir, glyph_info, count, load_flags, &width, &height, &x_offset, &y_offset); error = bounding_box_and_anchors(
self->face,
anchor,
horizontal_dir,
glyph_info,
count,
load_flags,
&width,
&height,
&x_offset,
&y_offset);
if (error) { if (error) {
PyMem_Del(glyph_info); PyMem_Del(glyph_info);
return NULL; return NULL;
@ -1066,17 +1089,26 @@ font_render(FontObject *self, PyObject *args) {
/* paste only if source has data */ /* paste only if source has data */
if (src_alpha > 0) { if (src_alpha > 0) {
/* unpremultiply BGRa */ /* unpremultiply BGRa */
int src_red = CLIP8((255 * (int)source[k * 4 + 2]) / src_alpha); int src_red =
int src_green = CLIP8((255 * (int)source[k * 4 + 1]) / src_alpha); CLIP8((255 * (int)source[k * 4 + 2]) / src_alpha);
int src_blue = CLIP8((255 * (int)source[k * 4 + 0]) / src_alpha); int src_green =
CLIP8((255 * (int)source[k * 4 + 1]) / src_alpha);
int src_blue =
CLIP8((255 * (int)source[k * 4 + 0]) / src_alpha);
/* blend required if target has data */ /* blend required if target has data */
if (target[k * 4 + 3] > 0) { if (target[k * 4 + 3] > 0) {
/* blend RGBA colors */ /* blend RGBA colors */
target[k * 4 + 0] = BLEND(src_alpha, target[k * 4 + 0], src_red, tmp); target[k * 4 + 0] =
target[k * 4 + 1] = BLEND(src_alpha, target[k * 4 + 1], src_green, tmp); BLEND(src_alpha, target[k * 4 + 0], src_red, tmp);
target[k * 4 + 2] = BLEND(src_alpha, target[k * 4 + 2], src_blue, tmp); target[k * 4 + 1] =
target[k * 4 + 3] = CLIP8(src_alpha + MULDIV255(target[k * 4 + 3], (255 - src_alpha), tmp)); BLEND(src_alpha, target[k * 4 + 1], src_green, tmp);
target[k * 4 + 2] =
BLEND(src_alpha, target[k * 4 + 2], src_blue, tmp);
target[k * 4 + 3] = CLIP8(
src_alpha +
MULDIV255(
target[k * 4 + 3], (255 - src_alpha), tmp));
} else { } else {
/* paste unpremultiplied RGBA values */ /* paste unpremultiplied RGBA values */
target[k * 4 + 0] = src_red; target[k * 4 + 0] = src_red;
@ -1093,10 +1125,16 @@ font_render(FontObject *self, PyObject *args) {
unsigned int src_alpha = source[k] * convert_scale; unsigned int src_alpha = source[k] * convert_scale;
if (src_alpha > 0) { if (src_alpha > 0) {
if (target[k * 4 + 3] > 0) { if (target[k * 4 + 3] > 0) {
target[k * 4 + 0] = BLEND(src_alpha, target[k * 4 + 0], ink[0], tmp); target[k * 4 + 0] = BLEND(
target[k * 4 + 1] = BLEND(src_alpha, target[k * 4 + 1], ink[1], tmp); src_alpha, target[k * 4 + 0], ink[0], tmp);
target[k * 4 + 2] = BLEND(src_alpha, target[k * 4 + 2], ink[2], tmp); target[k * 4 + 1] = BLEND(
target[k * 4 + 3] = CLIP8(src_alpha + MULDIV255(target[k * 4 + 3], (255 - src_alpha), tmp)); src_alpha, target[k * 4 + 1], ink[1], tmp);
target[k * 4 + 2] = BLEND(
src_alpha, target[k * 4 + 2], ink[2], tmp);
target[k * 4 + 3] = CLIP8(
src_alpha +
MULDIV255(
target[k * 4 + 3], (255 - src_alpha), tmp));
} else { } else {
target[k * 4 + 0] = ink[0]; target[k * 4 + 0] = ink[0];
target[k * 4 + 1] = ink[1]; target[k * 4 + 1] = ink[1];
@ -1109,7 +1147,13 @@ font_render(FontObject *self, PyObject *args) {
for (k = x0; k < x1; k++) { for (k = x0; k < x1; k++) {
unsigned int src_alpha = source[k] * convert_scale; unsigned int src_alpha = source[k] * convert_scale;
if (src_alpha > 0) { if (src_alpha > 0) {
target[k] = target[k] > 0 ? CLIP8(src_alpha + MULDIV255(target[k], (255 - src_alpha), tmp)) : src_alpha; target[k] =
target[k] > 0
? CLIP8(
src_alpha +
MULDIV255(
target[k], (255 - src_alpha), tmp))
: src_alpha;
} }
} }
} }
@ -1249,7 +1293,8 @@ font_getvaraxes(FontObject *self) {
if (name.name_id == axis.strid) { if (name.name_id == axis.strid) {
axis_name = Py_BuildValue("y#", name.string, name.string_len); axis_name = Py_BuildValue("y#", name.string, name.string_len);
PyDict_SetItemString(list_axis, "name", axis_name ? axis_name : Py_None); PyDict_SetItemString(
list_axis, "name", axis_name ? axis_name : Py_None);
Py_XDECREF(axis_name); Py_XDECREF(axis_name);
break; break;
} }

View File

@ -716,7 +716,7 @@ PyImaging_DrawWmf(PyObject *self, PyObject *args) {
HDC dc; HDC dc;
RECT rect; RECT rect;
PyObject *buffer = NULL; PyObject *buffer = NULL;
char *ptr; void *ptr;
char *data; char *data;
Py_ssize_t datasize; Py_ssize_t datasize;

View File

@ -1163,8 +1163,10 @@ PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) {
((JPEGENCODERSTATE *)encoder->state.context)->streamtype = streamtype; ((JPEGENCODERSTATE *)encoder->state.context)->streamtype = streamtype;
((JPEGENCODERSTATE *)encoder->state.context)->xdpi = xdpi; ((JPEGENCODERSTATE *)encoder->state.context)->xdpi = xdpi;
((JPEGENCODERSTATE *)encoder->state.context)->ydpi = ydpi; ((JPEGENCODERSTATE *)encoder->state.context)->ydpi = ydpi;
((JPEGENCODERSTATE *)encoder->state.context)->restart_marker_blocks = restart_marker_blocks; ((JPEGENCODERSTATE *)encoder->state.context)->restart_marker_blocks =
((JPEGENCODERSTATE *)encoder->state.context)->restart_marker_rows = restart_marker_rows; restart_marker_blocks;
((JPEGENCODERSTATE *)encoder->state.context)->restart_marker_rows =
restart_marker_rows;
((JPEGENCODERSTATE *)encoder->state.context)->comment = comment; ((JPEGENCODERSTATE *)encoder->state.context)->comment = comment;
((JPEGENCODERSTATE *)encoder->state.context)->comment_size = comment_size; ((JPEGENCODERSTATE *)encoder->state.context)->comment_size = comment_size;
((JPEGENCODERSTATE *)encoder->state.context)->extra = extra; ((JPEGENCODERSTATE *)encoder->state.context)->extra = extra;
@ -1333,9 +1335,7 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) {
if (comment && comment_size > 0) { if (comment && comment_size > 0) {
/* Size is stored as as an uint16, subtract 4 bytes for the header */ /* Size is stored as as an uint16, subtract 4 bytes for the header */
if (comment_size >= 65532) { if (comment_size >= 65532) {
PyErr_SetString( PyErr_SetString(PyExc_ValueError, "JPEG 2000 comment is too long");
PyExc_ValueError,
"JPEG 2000 comment is too long");
Py_DECREF(encoder); Py_DECREF(encoder);
return NULL; return NULL;
} }

View File

@ -83,7 +83,6 @@ decode_bc1_color(rgba *dst, const UINT8 *src, int separate_alpha) {
g1 = p[1].g; g1 = p[1].g;
b1 = p[1].b; b1 = p[1].b;
/* NOTE: BC2 and BC3 reuse BC1 color blocks but always act like c0 > c1 */ /* NOTE: BC2 and BC3 reuse BC1 color blocks but always act like c0 > c1 */
if (col.c0 > col.c1 || separate_alpha) { if (col.c0 > col.c1 || separate_alpha) {
p[2].r = (2 * r0 + 1 * r1) / 3; p[2].r = (2 * r0 + 1 * r1) / 3;
@ -354,8 +353,7 @@ decode_bc7_block(rgba *col, const UINT8 *src) {
} }
return; return;
} }
while (!(mode & (1 << bit++))) while (!(mode & (1 << bit++)));
;
mode = bit - 1; mode = bit - 1;
info = &bc7_modes[mode]; info = &bc7_modes[mode];
/* color selection bits: {subset}{endpoint} */ /* color selection bits: {subset}{endpoint} */
@ -826,7 +824,13 @@ put_block(Imaging im, ImagingCodecState state, const char *col, int sz, int C) {
static int static int
decode_bcn( decode_bcn(
Imaging im, ImagingCodecState state, const UINT8 *src, int bytes, int N, int C, char *pixel_format) { Imaging im,
ImagingCodecState state,
const UINT8 *src,
int bytes,
int N,
int C,
char *pixel_format) {
int ymax = state->ysize + state->yoff; int ymax = state->ysize + state->yoff;
const UINT8 *ptr = src; const UINT8 *ptr = src;
switch (N) { switch (N) {
@ -849,8 +853,7 @@ decode_bcn(
DECODE_LOOP(2, 16, rgba); DECODE_LOOP(2, 16, rgba);
DECODE_LOOP(3, 16, rgba); DECODE_LOOP(3, 16, rgba);
DECODE_LOOP(4, 8, lum); DECODE_LOOP(4, 8, lum);
case 5: case 5: {
{
int sign = strcmp(pixel_format, "BC5S") == 0 ? 1 : 0; int sign = strcmp(pixel_format, "BC5S") == 0 ? 1 : 0;
while (bytes >= 16) { while (bytes >= 16) {
rgba col[16]; rgba col[16];
@ -865,8 +868,7 @@ decode_bcn(
} }
break; break;
} }
case 6: case 6: {
{
int sign = strcmp(pixel_format, "BC6HS") == 0 ? 1 : 0; int sign = strcmp(pixel_format, "BC6HS") == 0 ? 1 : 0;
while (bytes >= 16) { while (bytes >= 16) {
rgba col[16]; rgba col[16];

View File

@ -313,12 +313,12 @@ _gaussian_blur_radius(float radius, int passes) {
} }
Imaging Imaging
ImagingGaussianBlur(Imaging imOut, Imaging imIn, float xradius, float yradius, int passes) { ImagingGaussianBlur(
Imaging imOut, Imaging imIn, float xradius, float yradius, int passes) {
return ImagingBoxBlur( return ImagingBoxBlur(
imOut, imOut,
imIn, imIn,
_gaussian_blur_radius(xradius, passes), _gaussian_blur_radius(xradius, passes),
_gaussian_blur_radius(yradius, passes), _gaussian_blur_radius(yradius, passes),
passes passes);
);
} }

View File

@ -518,8 +518,8 @@ rgba2rgb_(UINT8 *out, const UINT8 *in, int xsize) {
/* /*
* Conversion of RGB + single transparent color either to * Conversion of RGB + single transparent color either to
* RGBA or LA, where any pixel matching the color will have the alpha channel set to 0, or * RGBA or LA, where any pixel matching the color will have the alpha channel set to 0,
* RGBa or La, where any pixel matching the color will have all channels set to 0 * or RGBa or La, where any pixel matching the color will have all channels set to 0
*/ */
static void static void
@ -1676,7 +1676,8 @@ convert(
return (Imaging)ImagingError_ValueError("conversion not supported"); return (Imaging)ImagingError_ValueError("conversion not supported");
#else #else
static char buf[100]; static char buf[100];
snprintf(buf, 100, "conversion from %.10s to %.10s not supported", imIn->mode, mode); snprintf(
buf, 100, "conversion from %.10s to %.10s not supported", imIn->mode, mode);
return (Imaging)ImagingError_ValueError(buf); return (Imaging)ImagingError_ValueError(buf);
#endif #endif
} }
@ -1720,25 +1721,24 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) {
return (Imaging)ImagingError_ModeError(); return (Imaging)ImagingError_ModeError();
} }
if (strcmp(imIn->mode, "RGB") == 0 && (strcmp(mode, "RGBA") == 0 || strcmp(mode, "RGBa") == 0)) { if (strcmp(imIn->mode, "RGB") == 0 &&
(strcmp(mode, "RGBA") == 0 || strcmp(mode, "RGBa") == 0)) {
convert = rgb2rgba; convert = rgb2rgba;
if (strcmp(mode, "RGBa") == 0) { if (strcmp(mode, "RGBa") == 0) {
premultiplied = 1; premultiplied = 1;
} }
} else if (strcmp(imIn->mode, "RGB") == 0 && (strcmp(mode, "LA") == 0 || strcmp(mode, "La") == 0)) { } else if (
strcmp(imIn->mode, "RGB") == 0 &&
(strcmp(mode, "LA") == 0 || strcmp(mode, "La") == 0)) {
convert = rgb2la; convert = rgb2la;
source_transparency = 1; source_transparency = 1;
if (strcmp(mode, "La") == 0) { if (strcmp(mode, "La") == 0) {
premultiplied = 1; premultiplied = 1;
} }
} else if ((strcmp(imIn->mode, "1") == 0 || } else if (
strcmp(imIn->mode, "I") == 0 || (strcmp(imIn->mode, "1") == 0 || strcmp(imIn->mode, "I") == 0 ||
strcmp(imIn->mode, "I;16") == 0 || strcmp(imIn->mode, "I;16") == 0 || strcmp(imIn->mode, "L") == 0) &&
strcmp(imIn->mode, "L") == 0 (strcmp(mode, "RGBA") == 0 || strcmp(mode, "LA") == 0)) {
) && (
strcmp(mode, "RGBA") == 0 ||
strcmp(mode, "LA") == 0
)) {
if (strcmp(imIn->mode, "1") == 0) { if (strcmp(imIn->mode, "1") == 0) {
convert = bit2rgb; convert = bit2rgb;
} else if (strcmp(imIn->mode, "I") == 0) { } else if (strcmp(imIn->mode, "I") == 0) {

View File

@ -94,8 +94,8 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) {
return (ImagingDIB)ImagingError_MemoryError(); return (ImagingDIB)ImagingError_MemoryError();
} }
dib->bitmap = dib->bitmap = CreateDIBSection(
CreateDIBSection(dib->dc, dib->info, DIB_RGB_COLORS, &dib->bits, NULL, 0); dib->dc, dib->info, DIB_RGB_COLORS, (void **)&dib->bits, NULL, 0);
if (!dib->bitmap) { if (!dib->bitmap) {
free(dib->info); free(dib->info);
free(dib); free(dib);

View File

@ -439,7 +439,14 @@ draw_horizontal_lines(
* Filled polygon draw function using scan line algorithm. * Filled polygon draw function using scan line algorithm.
*/ */
static inline int static inline int
polygon_generic(Imaging im, int n, Edge *e, int ink, int eofill, hline_handler hline, int hasAlpha) { polygon_generic(
Imaging im,
int n,
Edge *e,
int ink,
int eofill,
hline_handler hline,
int hasAlpha) {
Edge **edge_table; Edge **edge_table;
float *xx; float *xx;
int edge_count = 0; int edge_count = 0;
@ -510,23 +517,38 @@ polygon_generic(Imaging im, int n, Edge *e, int ink, int eofill, hline_handler h
// Check if the two edges join to make a corner // Check if the two edges join to make a corner
if (((ymin == current->ymin && ymin == other_edge->ymin) || if (((ymin == current->ymin && ymin == other_edge->ymin) ||
(ymin == current->ymax && ymin == other_edge->ymax)) && (ymin == current->ymax && ymin == other_edge->ymax)) &&
xx[j-1] == (ymin - other_edge->y0) * other_edge->dx + other_edge->x0) { xx[j - 1] == (ymin - other_edge->y0) * other_edge->dx +
other_edge->x0) {
// Determine points from the edges on the next row // Determine points from the edges on the next row
// Or if this is the last row, check the previous row // Or if this is the last row, check the previous row
int offset = ymin == ymax ? -1 : 1; int offset = ymin == ymax ? -1 : 1;
adjacent_line_x = (ymin + offset - current->y0) * current->dx + current->x0; adjacent_line_x =
adjacent_line_x_other_edge = (ymin + offset - other_edge->y0) * other_edge->dx + other_edge->x0; (ymin + offset - current->y0) * current->dx +
current->x0;
adjacent_line_x_other_edge =
(ymin + offset - other_edge->y0) * other_edge->dx +
other_edge->x0;
if (ymin == current->ymax) { if (ymin == current->ymax) {
if (current->dx > 0) { if (current->dx > 0) {
xx[k] = fmax(adjacent_line_x, adjacent_line_x_other_edge) + 1; xx[k] = fmax(
adjacent_line_x,
adjacent_line_x_other_edge) +
1;
} else { } else {
xx[k] = fmin(adjacent_line_x, adjacent_line_x_other_edge) - 1; xx[k] = fmin(
adjacent_line_x,
adjacent_line_x_other_edge) -
1;
} }
} else { } else {
if (current->dx > 0) { if (current->dx > 0) {
xx[k] = fmin(adjacent_line_x, adjacent_line_x_other_edge); xx[k] = fmin(
adjacent_line_x, adjacent_line_x_other_edge);
} else { } else {
xx[k] = fmax(adjacent_line_x, adjacent_line_x_other_edge) + 1; xx[k] = fmax(
adjacent_line_x,
adjacent_line_x_other_edge) +
1;
} }
} }
break; break;
@ -552,7 +574,8 @@ polygon_generic(Imaging im, int n, Edge *e, int ink, int eofill, hline_handler h
int x_start = ROUND_UP(xx[i - 1]); int x_start = ROUND_UP(xx[i - 1]);
if (x_pos > x_start) { if (x_pos > x_start) {
// Line would be partway through x_pos, so increase the starting point // Line would be partway through x_pos, so increase the starting
// point
x_start = x_pos; x_start = x_pos;
if (x_end < x_start) { if (x_end < x_start) {
// Line would now end before it started // Line would now end before it started
@ -776,7 +799,8 @@ ImagingDrawRectangle(
} }
int int
ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, int width, int op) { ImagingDrawPolygon(
Imaging im, int count, int *xy, const void *ink_, int fill, int width, int op) {
int i, n, x0, y0, x1, y1; int i, n, x0, y0, x1, y1;
DRAW *draw; DRAW *draw;
INT32 ink; INT32 ink;
@ -826,14 +850,24 @@ ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, i
/* Outline */ /* Outline */
if (width == 1) { if (width == 1) {
for (i = 0; i < count - 1; i++) { for (i = 0; i < count - 1; i++) {
draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[i * 2 + 2], xy[i * 2 + 3], ink); draw->line(
im, xy[i * 2], xy[i * 2 + 1], xy[i * 2 + 2], xy[i * 2 + 3], ink);
} }
draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], ink); draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], ink);
} else { } else {
for (i = 0; i < count - 1; i++) { for (i = 0; i < count - 1; i++) {
ImagingDrawWideLine(im, xy[i * 2], xy[i * 2 + 1], xy[i * 2 + 2], xy[i * 2 + 3], ink_, width, op); ImagingDrawWideLine(
im,
xy[i * 2],
xy[i * 2 + 1],
xy[i * 2 + 2],
xy[i * 2 + 3],
ink_,
width,
op);
} }
ImagingDrawWideLine(im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], ink_, width, op); ImagingDrawWideLine(
im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], ink_, width, op);
} }
} }

View File

@ -225,9 +225,8 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) {
void void
ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) { ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) {
#define KERNEL1x5(in0, x, kernel, d) \ #define KERNEL1x5(in0, x, kernel, d) \
(_i2f(in0[x - d - d]) * (kernel)[0] + \ (_i2f(in0[x - d - d]) * (kernel)[0] + _i2f(in0[x - d]) * (kernel)[1] + \
_i2f(in0[x - d]) * (kernel)[1] + _i2f(in0[x]) * (kernel)[2] + \ _i2f(in0[x]) * (kernel)[2] + _i2f(in0[x + d]) * (kernel)[3] + \
_i2f(in0[x + d]) * (kernel)[3] + \
_i2f(in0[x + d + d]) * (kernel)[4]) _i2f(in0[x + d + d]) * (kernel)[4])
int x = 0, y = 0; int x = 0, y = 0;

View File

@ -231,8 +231,9 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
} }
/* Note, have to check Data + size, not just ptr + size) */ /* Note, have to check Data + size, not just ptr + size) */
if (data + (state->xsize * state->ysize) > ptr + bytes) { if (data + (state->xsize * state->ysize) > ptr + bytes) {
/* not enough data for frame */ // not enough data for frame
/* UNDONE Unclear that we're actually going to leave the buffer at the right place. */ // UNDONE Unclear that we're actually going to leave the buffer at
// the right place.
return ptr - buf; /* bytes consumed */ return ptr - buf; /* bytes consumed */
} }
for (y = 0; y < state->ysize; y++) { for (y = 0; y < state->ysize; y++) {

View File

@ -58,11 +58,11 @@ ImagingGetBBox(Imaging im, int bbox[4], int alpha_only) {
INT32 mask = 0xffffffff; INT32 mask = 0xffffffff;
if (im->bands == 3) { if (im->bands == 3) {
((UINT8 *)&mask)[3] = 0; ((UINT8 *)&mask)[3] = 0;
} else if (alpha_only && ( } else if (
strcmp(im->mode, "RGBa") == 0 || strcmp(im->mode, "RGBA") == 0 || alpha_only &&
(strcmp(im->mode, "RGBa") == 0 || strcmp(im->mode, "RGBA") == 0 ||
strcmp(im->mode, "La") == 0 || strcmp(im->mode, "LA") == 0 || strcmp(im->mode, "La") == 0 || strcmp(im->mode, "LA") == 0 ||
strcmp(im->mode, "PA") == 0 strcmp(im->mode, "PA") == 0)) {
)) {
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
mask = 0x000000ff; mask = 0x000000ff;
#else #else

View File

@ -42,20 +42,28 @@ enum { INIT, ENCODE, FINISH };
#define CODE_LIMIT 4096 #define CODE_LIMIT 4096
/* Values of entry_state */ /* Values of entry_state */
enum { LZW_INITIAL, LZW_TRY_IN1, LZW_TRY_IN2, LZW_TRY_OUT1, LZW_TRY_OUT2, enum {
LZW_FINISHED }; LZW_INITIAL,
LZW_TRY_IN1,
LZW_TRY_IN2,
LZW_TRY_OUT1,
LZW_TRY_OUT2,
LZW_FINISHED
};
/* Values of control_state */ /* Values of control_state */
enum { PUT_HEAD, PUT_INIT_CLEAR, PUT_CLEAR, PUT_LAST_HEAD, PUT_END }; enum { PUT_HEAD, PUT_INIT_CLEAR, PUT_CLEAR, PUT_LAST_HEAD, PUT_END };
static void glzwe_reset(GIFENCODERSTATE *st) { static void
glzwe_reset(GIFENCODERSTATE *st) {
st->next_code = st->end_code + 1; st->next_code = st->end_code + 1;
st->max_code = 2 * st->clear_code - 1; st->max_code = 2 * st->clear_code - 1;
st->code_width = st->bits + 1; st->code_width = st->bits + 1;
memset(st->codes, 0, sizeof(st->codes)); memset(st->codes, 0, sizeof(st->codes));
} }
static void glzwe_init(GIFENCODERSTATE *st) { static void
glzwe_init(GIFENCODERSTATE *st) {
st->clear_code = 1 << st->bits; st->clear_code = 1 << st->bits;
st->end_code = st->clear_code + 1; st->end_code = st->clear_code + 1;
glzwe_reset(st); glzwe_reset(st);
@ -64,11 +72,15 @@ static void glzwe_init(GIFENCODERSTATE *st) {
st->code_buffer = 0; st->code_buffer = 0;
} }
static int glzwe(GIFENCODERSTATE *st, const UINT8 *in_ptr, UINT8 *out_ptr, static int
UINT32 *in_avail, UINT32 *out_avail, glzwe(
GIFENCODERSTATE *st,
const UINT8 *in_ptr,
UINT8 *out_ptr,
UINT32 *in_avail,
UINT32 *out_avail,
UINT32 end_of_data) { UINT32 end_of_data) {
switch (st->entry_state) { switch (st->entry_state) {
case LZW_TRY_IN1: case LZW_TRY_IN1:
get_first_byte: get_first_byte:
if (!*in_avail) { if (!*in_avail) {
@ -100,13 +112,12 @@ encode_loop:
/* This works ONLY with TABLE_SIZE a power of 2. */ /* This works ONLY with TABLE_SIZE a power of 2. */
st->probe = ((st->head ^ (st->tail << 6)) * 31) & (TABLE_SIZE - 1); st->probe = ((st->head ^ (st->tail << 6)) * 31) & (TABLE_SIZE - 1);
while (st->codes[st->probe]) { while (st->codes[st->probe]) {
if ((st->codes[st->probe] & 0xFFFFF) == if ((st->codes[st->probe] & 0xFFFFF) == ((st->head << 8) | st->tail)) {
((st->head << 8) | st->tail)) {
st->head = st->codes[st->probe] >> 20; st->head = st->codes[st->probe] >> 20;
goto encode_loop; goto encode_loop;
} else { } else {
/* Reprobe decrement must be non-zero and relatively prime to table // Reprobe decrement must be non-zero and relatively prime to table
* size. So, any odd positive number for power-of-2 size. */ // size. So, any odd positive number for power-of-2 size.
if ((st->probe -= ((st->tail << 2) | 1)) < 0) { if ((st->probe -= ((st->tail << 2) | 1)) < 0) {
st->probe += TABLE_SIZE; st->probe += TABLE_SIZE;
} }
@ -118,8 +129,8 @@ encode_loop:
goto put_code; goto put_code;
insert_code_or_clear: /* jump here after put_code */ insert_code_or_clear: /* jump here after put_code */
if (st->next_code < CODE_LIMIT) { if (st->next_code < CODE_LIMIT) {
st->codes[st->probe] = (st->next_code << 20) | st->codes[st->probe] =
(st->head << 8) | st->tail; (st->next_code << 20) | (st->head << 8) | st->tail;
if (st->next_code > st->max_code) { if (st->next_code > st->max_code) {
st->max_code = st->max_code * 2 + 1; st->max_code = st->max_code * 2 + 1;
st->code_width++; st->code_width++;
@ -155,10 +166,9 @@ check_buf_bits:
st->buf_bits_left = 8; st->buf_bits_left = 8;
} }
/* code bits to pack */ /* code bits to pack */
UINT32 n = st->buf_bits_left < st->code_bits_left UINT32 n = st->buf_bits_left < st->code_bits_left ? st->buf_bits_left
? st->buf_bits_left : st->code_bits_left; : st->code_bits_left;
st->code_buffer |= st->code_buffer |= (st->code & ((1 << n) - 1)) << (8 - st->buf_bits_left);
(st->code & ((1 << n) - 1)) << (8 - st->buf_bits_left);
st->code >>= n; st->code >>= n;
st->buf_bits_left -= n; st->buf_bits_left -= n;
st->code_bits_left -= n; st->code_bits_left -= n;
@ -186,7 +196,6 @@ end_of_data:
goto put_code; goto put_code;
flush_code_buffer: /* jump here after put_code */ flush_code_buffer: /* jump here after put_code */
if (st->buf_bits_left < 8) { if (st->buf_bits_left < 8) {
case LZW_TRY_OUT2: case LZW_TRY_OUT2:
if (!*out_avail) { if (!*out_avail) {
st->entry_state = LZW_TRY_OUT2; st->entry_state = LZW_TRY_OUT2;
@ -278,9 +287,9 @@ ImagingGifEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) {
return ptr - buf; return ptr - buf;
} }
sub_block_ptr = ptr; sub_block_ptr = ptr;
sub_block_limit = sub_block_ptr + sub_block_limit =
(256 < buf_limit - sub_block_ptr ? sub_block_ptr +
256 : buf_limit - sub_block_ptr); (256 < buf_limit - sub_block_ptr ? 256 : buf_limit - sub_block_ptr);
*ptr++ = 0; *ptr++ = 0;
} }
@ -302,8 +311,8 @@ ImagingGifEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) {
state->shuffle( state->shuffle(
state->buffer, state->buffer,
(UINT8 *)im->image[state->y + state->yoff] + (UINT8 *)im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->xsize state->xoff * im->pixelsize,
); state->xsize);
state->x = 0; state->x = 0;
/* step forward, according to the interlace settings */ /* step forward, according to the interlace settings */
@ -333,8 +342,13 @@ ImagingGifEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes) {
in_avail = state->xsize - state->x; /* bytes left in line */ in_avail = state->xsize - state->x; /* bytes left in line */
out_avail = sub_block_limit - ptr; /* bytes left in sub-block */ out_avail = sub_block_limit - ptr; /* bytes left in sub-block */
r = glzwe(context, &state->buffer[state->x], ptr, &in_avail, r = glzwe(
&out_avail, state->state == FINISH); context,
&state->buffer[state->x],
ptr,
&in_avail,
&out_avail,
state->state == FINISH);
out_used = sub_block_limit - ptr - out_avail; out_used = sub_block_limit - ptr - out_avail;
*sub_block_ptr += out_used; *sub_block_ptr += out_used;
ptr += out_used; ptr += out_used;

View File

@ -309,7 +309,8 @@ ImagingFlipLeftRight(Imaging imOut, Imaging imIn);
extern Imaging extern Imaging
ImagingFlipTopBottom(Imaging imOut, Imaging imIn); ImagingFlipTopBottom(Imaging imOut, Imaging imIn);
extern Imaging extern Imaging
ImagingGaussianBlur(Imaging imOut, Imaging imIn, float xradius, float yradius, int passes); ImagingGaussianBlur(
Imaging imOut, Imaging imIn, float xradius, float yradius, int passes);
extern Imaging extern Imaging
ImagingGetBand(Imaging im, int band); ImagingGetBand(Imaging im, int band);
extern Imaging extern Imaging
@ -487,7 +488,8 @@ ImagingDrawPieslice(
extern int extern int
ImagingDrawPoint(Imaging im, int x, int y, const void *ink, int op); ImagingDrawPoint(Imaging im, int x, int y, const void *ink, int op);
extern int extern int
ImagingDrawPolygon(Imaging im, int points, int *xy, const void *ink, int fill, int width, int op); ImagingDrawPolygon(
Imaging im, int points, int *xy, const void *ink, int fill, int width, int op);
extern int extern int
ImagingDrawRectangle( ImagingDrawRectangle(
Imaging im, Imaging im,

View File

@ -383,8 +383,7 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) {
float *pq; float *pq;
if (len > 0) { if (len > 0) {
if ((size_t)len > if ((size_t)len > sizeof(params.tcp_rates) / sizeof(params.tcp_rates[0])) {
sizeof(params.tcp_rates) / sizeof(params.tcp_rates[0])) {
len = sizeof(params.tcp_rates) / sizeof(params.tcp_rates[0]); len = sizeof(params.tcp_rates) / sizeof(params.tcp_rates[0]);
} }
@ -464,7 +463,8 @@ j2k_encode_entry(Imaging im, ImagingCodecState state) {
} }
if (!context->num_resolutions) { if (!context->num_resolutions) {
while (tile_width < (1U << (params.numresolution - 1U)) || tile_height < (1U << (params.numresolution - 1U))) { while (tile_width < (1U << (params.numresolution - 1U)) ||
tile_height < (1U << (params.numresolution - 1U))) {
params.numresolution -= 1; params.numresolution -= 1;
} }
} }

View File

@ -305,7 +305,11 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
case 4: case 4:
if (context->comment) { if (context->comment) {
jpeg_write_marker(&context->cinfo, JPEG_COM, (unsigned char *)context->comment, context->comment_size); jpeg_write_marker(
&context->cinfo,
JPEG_COM,
(unsigned char *)context->comment,
context->comment_size);
} }
state->state++; state->state++;

View File

@ -432,10 +432,9 @@ fill_mask_L(
} }
} else { } else {
int alpha_channel = strcmp(imOut->mode, "RGBa") == 0 || int alpha_channel =
strcmp(imOut->mode, "RGBA") == 0 || strcmp(imOut->mode, "RGBa") == 0 || strcmp(imOut->mode, "RGBA") == 0 ||
strcmp(imOut->mode, "La") == 0 || strcmp(imOut->mode, "La") == 0 || strcmp(imOut->mode, "LA") == 0 ||
strcmp(imOut->mode, "LA") == 0 ||
strcmp(imOut->mode, "PA") == 0; strcmp(imOut->mode, "PA") == 0;
for (y = 0; y < ysize; y++) { for (y = 0; y < ysize; y++) {
UINT8 *out = (UINT8 *)imOut->image[y + dy] + dx * pixelsize; UINT8 *out = (UINT8 *)imOut->image[y + dy] + dx * pixelsize;

View File

@ -260,8 +260,7 @@ mergesort_pixels(PixelList *head, int i) {
return head; return head;
} }
for (c = t = head; c && t; for (c = t = head; c && t;
c = c->next[i], t = (t->next[i]) ? t->next[i]->next[i] : NULL) c = c->next[i], t = (t->next[i]) ? t->next[i]->next[i] : NULL);
;
if (c) { if (c) {
if (c->prev[i]) { if (c->prev[i]) {
c->prev[i]->next[i] = NULL; c->prev[i]->next[i] = NULL;
@ -354,12 +353,10 @@ splitlists(
for (_i = 0; _i < 3; _i++) { for (_i = 0; _i < 3; _i++) {
for (_nextCount[_i] = 0, _nextTest = h[_i]; for (_nextCount[_i] = 0, _nextTest = h[_i];
_nextTest && _nextTest->next[_i]; _nextTest && _nextTest->next[_i];
_nextTest = _nextTest->next[_i], _nextCount[_i]++) _nextTest = _nextTest->next[_i], _nextCount[_i]++);
;
for (_prevCount[_i] = 0, _prevTest = t[_i]; for (_prevCount[_i] = 0, _prevTest = t[_i];
_prevTest && _prevTest->prev[_i]; _prevTest && _prevTest->prev[_i];
_prevTest = _prevTest->prev[_i], _prevCount[_i]++) _prevTest = _prevTest->prev[_i], _prevCount[_i]++);
;
if (_nextTest != t[_i]) { if (_nextTest != t[_i]) {
printf("next-list of axis %d does not end at tail\n", _i); printf("next-list of axis %d does not end at tail\n", _i);
exit(1); exit(1);
@ -368,10 +365,8 @@ splitlists(
printf("prev-list of axis %d does not end at head\n", _i); printf("prev-list of axis %d does not end at head\n", _i);
exit(1); exit(1);
} }
for (; _nextTest && _nextTest->prev[_i]; _nextTest = _nextTest->prev[_i]) for (; _nextTest && _nextTest->prev[_i]; _nextTest = _nextTest->prev[_i]);
; for (; _prevTest && _prevTest->next[_i]; _prevTest = _prevTest->next[_i]);
for (; _prevTest && _prevTest->next[_i]; _prevTest = _prevTest->next[_i])
;
if (_nextTest != h[_i]) { if (_nextTest != h[_i]) {
printf("next-list of axis %d does not loop back to head\n", _i); printf("next-list of axis %d does not loop back to head\n", _i);
exit(1); exit(1);
@ -548,22 +543,18 @@ split(BoxNode *node) {
for (_i = 0; _i < 3; _i++) { for (_i = 0; _i < 3; _i++) {
for (_nextCount[_i] = 0, _nextTest = node->head[_i]; for (_nextCount[_i] = 0, _nextTest = node->head[_i];
_nextTest && _nextTest->next[_i]; _nextTest && _nextTest->next[_i];
_nextTest = _nextTest->next[_i], _nextCount[_i]++) _nextTest = _nextTest->next[_i], _nextCount[_i]++);
;
for (_prevCount[_i] = 0, _prevTest = node->tail[_i]; for (_prevCount[_i] = 0, _prevTest = node->tail[_i];
_prevTest && _prevTest->prev[_i]; _prevTest && _prevTest->prev[_i];
_prevTest = _prevTest->prev[_i], _prevCount[_i]++) _prevTest = _prevTest->prev[_i], _prevCount[_i]++);
;
if (_nextTest != node->tail[_i]) { if (_nextTest != node->tail[_i]) {
printf("next-list of axis %d does not end at tail\n", _i); printf("next-list of axis %d does not end at tail\n", _i);
} }
if (_prevTest != node->head[_i]) { if (_prevTest != node->head[_i]) {
printf("prev-list of axis %d does not end at head\n", _i); printf("prev-list of axis %d does not end at head\n", _i);
} }
for (; _nextTest && _nextTest->prev[_i]; _nextTest = _nextTest->prev[_i]) for (; _nextTest && _nextTest->prev[_i]; _nextTest = _nextTest->prev[_i]);
; for (; _prevTest && _prevTest->next[_i]; _prevTest = _prevTest->next[_i]);
for (; _prevTest && _prevTest->next[_i]; _prevTest = _prevTest->next[_i])
;
if (_nextTest != node->head[_i]) { if (_nextTest != node->head[_i]) {
printf("next-list of axis %d does not loop back to head\n", _i); printf("next-list of axis %d does not loop back to head\n", _i);
} }
@ -668,8 +659,7 @@ median_cut(PixelList *hl[3], uint32_t imPixelCount, int nPixels) {
return NULL; return NULL;
} }
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
for (tl[i] = hl[i]; tl[i] && tl[i]->next[i]; tl[i] = tl[i]->next[i]) for (tl[i] = hl[i]; tl[i] && tl[i]->next[i]; tl[i] = tl[i]->next[i]);
;
root->head[i] = hl[i]; root->head[i] = hl[i];
root->tail[i] = tl[i]; root->tail[i] = tl[i];
} }
@ -832,16 +822,9 @@ build_distance_tables(
} }
for (i = 0; i < nEntries; i++) { for (i = 0; i < nEntries; i++) {
for (j = 0; j < nEntries; j++) { for (j = 0; j < nEntries; j++) {
dwi[j] = (DistanceWithIndex){ dwi[j] = (DistanceWithIndex){&(avgDist[i * nEntries + j]), j};
&(avgDist[i * nEntries + j]),
j
};
} }
qsort( qsort(dwi, nEntries, sizeof(DistanceWithIndex), _distance_index_cmp);
dwi,
nEntries,
sizeof(DistanceWithIndex),
_distance_index_cmp);
for (j = 0; j < nEntries; j++) { for (j = 0; j < nEntries; j++) {
avgDistSortKey[i * nEntries + j] = dwi[j].distance; avgDistSortKey[i * nEntries + j] = dwi[j].distance;
} }
@ -1452,15 +1435,17 @@ quantize(
hashtable_insert(h2, pixelData[i], bestmatch); hashtable_insert(h2, pixelData[i], bestmatch);
} }
if (qp[i] != bestmatch) { if (qp[i] != bestmatch) {
printf ("discrepancy in matching algorithms pixel %d [%d %d] %f %f\n", printf(
i,qp[i],bestmatch, "discrepancy in matching algorithms pixel %d [%d %d] %f %f\n",
i,
qp[i],
bestmatch,
sqrt((double)(_SQR(pixelData[i].c.r - p[qp[i]].c.r) + sqrt((double)(_SQR(pixelData[i].c.r - p[qp[i]].c.r) +
_SQR(pixelData[i].c.g - p[qp[i]].c.g) + _SQR(pixelData[i].c.g - p[qp[i]].c.g) +
_SQR(pixelData[i].c.b - p[qp[i]].c.b))), _SQR(pixelData[i].c.b - p[qp[i]].c.b))),
sqrt((double)(_SQR(pixelData[i].c.r - p[bestmatch].c.r) + sqrt((double)(_SQR(pixelData[i].c.r - p[bestmatch].c.r) +
_SQR(pixelData[i].c.g - p[bestmatch].c.g) + _SQR(pixelData[i].c.g - p[bestmatch].c.g) +
_SQR(pixelData[i].c.b-p[bestmatch].c.b))) _SQR(pixelData[i].c.b - p[bestmatch].c.b))));
);
} }
} }
hashtable_free(h2); hashtable_free(h2);

View File

@ -113,7 +113,8 @@ expandrow(UINT8 *dest, UINT8 *src, int n, int z, int xsize, UINT8 *end_of_buffer
} }
static int static int
expandrow2(UINT8 *dest, const UINT8 *src, int n, int z, int xsize, UINT8 *end_of_buffer) { expandrow2(
UINT8 *dest, const UINT8 *src, int n, int z, int xsize, UINT8 *end_of_buffer) {
UINT8 pixel, count; UINT8 pixel, count;
int x = 0; int x = 0;
@ -197,7 +198,6 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t
return -1; return -1;
} }
/* decoder initialization */ /* decoder initialization */
state->count = 0; state->count = 0;
state->y = 0; state->y = 0;
@ -268,7 +268,6 @@ ImagingSgiRleDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t
} else if (status == 1) { } else if (status == 1) {
goto sgi_finish_decode; goto sgi_finish_decode;
} }
} }
/* store decompressed data in image */ /* store decompressed data in image */

View File

@ -418,8 +418,7 @@ ImagingAllocateArray(Imaging im, int dirty, int block_size) {
} }
im->blocks[current_block] = block; im->blocks[current_block] = block;
/* Bulletproof code from libc _int_memalign */ /* Bulletproof code from libc _int_memalign */
aligned_ptr = (char *)( aligned_ptr = (char *)(((size_t)(block.ptr + arena->alignment - 1)) &
((size_t) (block.ptr + arena->alignment - 1)) &
-((Py_ssize_t)arena->alignment)); -((Py_ssize_t)arena->alignment));
} }

View File

@ -60,7 +60,11 @@ _tiffReadProc(thandle_t hdata, tdata_t buf, tsize_t size) {
dump_state(state); dump_state(state);
if (state->loc > state->eof) { if (state->loc > state->eof) {
TIFFError("_tiffReadProc", "Invalid Read at loc %" PRIu64 ", eof: %" PRIu64, state->loc, state->eof); TIFFError(
"_tiffReadProc",
"Invalid Read at loc %" PRIu64 ", eof: %" PRIu64,
state->loc,
state->eof);
return 0; return 0;
} }
to_read = min(size, min(state->size, (tsize_t)state->eof) - (tsize_t)state->loc); to_read = min(size, min(state->size, (tsize_t)state->eof) - (tsize_t)state->loc);
@ -217,7 +221,12 @@ ImagingLibTiffInit(ImagingCodecState state, int fp, uint32_t offset) {
} }
int int
_pickUnpackers(Imaging im, ImagingCodecState state, TIFF *tiff, uint16_t planarconfig, ImagingShuffler *unpackers) { _pickUnpackers(
Imaging im,
ImagingCodecState state,
TIFF *tiff,
uint16_t planarconfig,
ImagingShuffler *unpackers) {
// if number of bands is 1, there is no difference with contig case // if number of bands is 1, there is no difference with contig case
if (planarconfig == PLANARCONFIG_SEPARATE && im->bands > 1) { if (planarconfig == PLANARCONFIG_SEPARATE && im->bands > 1) {
uint16_t bits_per_sample = 8; uint16_t bits_per_sample = 8;
@ -232,10 +241,14 @@ _pickUnpackers(Imaging im, ImagingCodecState state, TIFF *tiff, uint16_t planarc
// We'll pick appropriate set of unpackers depending on planar_configuration // We'll pick appropriate set of unpackers depending on planar_configuration
// It does not matter if data is RGB(A), CMYK or LUV really, // It does not matter if data is RGB(A), CMYK or LUV really,
// we just copy it plane by plane // we just copy it plane by plane
unpackers[0] = ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "R;16N" : "R", NULL); unpackers[0] =
unpackers[1] = ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "G;16N" : "G", NULL); ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "R;16N" : "R", NULL);
unpackers[2] = ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "B;16N" : "B", NULL); unpackers[1] =
unpackers[3] = ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "A;16N" : "A", NULL); ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "G;16N" : "G", NULL);
unpackers[2] =
ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "B;16N" : "B", NULL);
unpackers[3] =
ImagingFindUnpacker("RGBA", bits_per_sample == 16 ? "A;16N" : "A", NULL);
return im->bands; return im->bands;
} else { } else {
@ -247,10 +260,10 @@ _pickUnpackers(Imaging im, ImagingCodecState state, TIFF *tiff, uint16_t planarc
int int
_decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) { _decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) {
// To avoid dealing with YCbCr subsampling and other complications, let libtiff handle it // To avoid dealing with YCbCr subsampling and other complications, let libtiff
// Use a TIFFRGBAImage wrapping the tiff image, and let libtiff handle // handle it Use a TIFFRGBAImage wrapping the tiff image, and let libtiff handle all
// all of the conversion. Metadata read from the TIFFRGBAImage could // of the conversion. Metadata read from the TIFFRGBAImage could be different from
// be different from the metadata that the base tiff returns. // the metadata that the base tiff returns.
INT32 current_row; INT32 current_row;
UINT8 *new_data; UINT8 *new_data;
@ -259,13 +272,12 @@ _decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) {
TIFFRGBAImage img; TIFFRGBAImage img;
char emsg[1024] = ""; char emsg[1024] = "";
// Since using TIFFRGBAImage* functions, we can read whole tiff into rastrr in one call // Since using TIFFRGBAImage* functions, we can read whole tiff into rastrr in one
// Let's select smaller block size. Multiplying image width by (tile length OR rows per strip) // call Let's select smaller block size. Multiplying image width by (tile length OR
// gives us manageable block size in pixels // rows per strip) gives us manageable block size in pixels
if (TIFFIsTiled(tiff)) { if (TIFFIsTiled(tiff)) {
ret = TIFFGetFieldDefaulted(tiff, TIFFTAG_TILELENGTH, &rows_per_block); ret = TIFFGetFieldDefaulted(tiff, TIFFTAG_TILELENGTH, &rows_per_block);
} } else {
else {
ret = TIFFGetFieldDefaulted(tiff, TIFFTAG_ROWSPERSTRIP, &rows_per_block); ret = TIFFGetFieldDefaulted(tiff, TIFFTAG_ROWSPERSTRIP, &rows_per_block);
} }
@ -357,7 +369,12 @@ decodergba_err:
} }
int int
_decodeTile(Imaging im, ImagingCodecState state, TIFF *tiff, int planes, ImagingShuffler *unpackers) { _decodeTile(
Imaging im,
ImagingCodecState state,
TIFF *tiff,
int planes,
ImagingShuffler *unpackers) {
INT32 x, y, tile_y, current_tile_length, current_tile_width; INT32 x, y, tile_y, current_tile_length, current_tile_width;
UINT32 tile_width, tile_length; UINT32 tile_width, tile_length;
tsize_t tile_bytes_size, row_byte_size; tsize_t tile_bytes_size, row_byte_size;
@ -396,8 +413,8 @@ _decodeTile(Imaging im, ImagingCodecState state, TIFF *tiff, int planes, Imaging
if (tile_bytes_size > ((tile_length * state->bits / planes + 7) / 8) * tile_width) { if (tile_bytes_size > ((tile_length * state->bits / planes + 7) / 8) * tile_width) {
// If the tile size as expected by LibTiff isn't what we're expecting, abort. // If the tile size as expected by LibTiff isn't what we're expecting, abort.
// man: TIFFTileSize returns the equivalent size for a tile of data as it would be returned in a // man: TIFFTileSize returns the equivalent size for a tile of data as it
// call to TIFFReadTile ... // would be returned in a call to TIFFReadTile ...
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
return -1; return -1;
} }
@ -432,15 +449,20 @@ _decodeTile(Imaging im, ImagingCodecState state, TIFF *tiff, int planes, Imaging
current_tile_length = min((INT32)tile_length, state->ysize - y); current_tile_length = min((INT32)tile_length, state->ysize - y);
// iterate over each line in the tile and stuff data into image // iterate over each line in the tile and stuff data into image
for (tile_y = 0; tile_y < current_tile_length; tile_y++) { for (tile_y = 0; tile_y < current_tile_length; tile_y++) {
TRACE(("Writing tile data at %dx%d using tile_width: %d; \n", tile_y + y, x, current_tile_width)); TRACE(
("Writing tile data at %dx%d using tile_width: %d; \n",
tile_y + y,
x,
current_tile_width));
// UINT8 * bbb = state->buffer + tile_y * row_byte_size; // UINT8 * bbb = state->buffer + tile_y * row_byte_size;
// TRACE(("chars: %x%x%x%x\n", ((UINT8 *)bbb)[0], ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3])); // TRACE(("chars: %x%x%x%x\n", ((UINT8 *)bbb)[0], ((UINT8 *)bbb)[1],
// ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3]));
shuffler((UINT8*) im->image[tile_y + y] + x * im->pixelsize, shuffler(
(UINT8 *)im->image[tile_y + y] + x * im->pixelsize,
state->buffer + tile_y * row_byte_size, state->buffer + tile_y * row_byte_size,
current_tile_width current_tile_width);
);
} }
} }
} }
@ -450,7 +472,12 @@ _decodeTile(Imaging im, ImagingCodecState state, TIFF *tiff, int planes, Imaging
} }
int int
_decodeStrip(Imaging im, ImagingCodecState state, TIFF *tiff, int planes, ImagingShuffler *unpackers) { _decodeStrip(
Imaging im,
ImagingCodecState state,
TIFF *tiff,
int planes,
ImagingShuffler *unpackers) {
INT32 strip_row = 0; INT32 strip_row = 0;
UINT8 *new_data; UINT8 *new_data;
UINT32 rows_per_strip; UINT32 rows_per_strip;
@ -478,8 +505,8 @@ _decodeStrip(Imaging im, ImagingCodecState state, TIFF *tiff, int planes, Imagin
unpacker_row_byte_size = (state->xsize * state->bits / planes + 7) / 8; unpacker_row_byte_size = (state->xsize * state->bits / planes + 7) / 8;
if (strip_size > (unpacker_row_byte_size * rows_per_strip)) { if (strip_size > (unpacker_row_byte_size * rows_per_strip)) {
// If the strip size as expected by LibTiff isn't what we're expecting, abort. // If the strip size as expected by LibTiff isn't what we're expecting, abort.
// man: TIFFStripSize returns the equivalent size for a strip of data as it would be returned in a // man: TIFFStripSize returns the equivalent size for a strip of data as it
// call to TIFFReadEncodedStrip ... // would be returned in a call to TIFFReadEncodedStrip ...
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
return -1; return -1;
} }
@ -513,8 +540,13 @@ _decodeStrip(Imaging im, ImagingCodecState state, TIFF *tiff, int planes, Imagin
int plane; int plane;
for (plane = 0; plane < planes; plane++) { for (plane = 0; plane < planes; plane++) {
ImagingShuffler shuffler = unpackers[plane]; ImagingShuffler shuffler = unpackers[plane];
if (TIFFReadEncodedStrip(tiff, TIFFComputeStrip(tiff, state->y, plane), (tdata_t)state->buffer, strip_size) == -1) { if (TIFFReadEncodedStrip(
TRACE(("Decode Error, strip %d\n", TIFFComputeStrip(tiff, state->y, 0))); tiff,
TIFFComputeStrip(tiff, state->y, plane),
(tdata_t)state->buffer,
strip_size) == -1) {
TRACE(
("Decode Error, strip %d\n", TIFFComputeStrip(tiff, state->y, 0)));
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
return -1; return -1;
} }
@ -527,8 +559,9 @@ _decodeStrip(Imaging im, ImagingCodecState state, TIFF *tiff, int planes, Imagin
strip_row++) { strip_row++) {
TRACE(("Writing data into line %d ; \n", state->y + strip_row)); TRACE(("Writing data into line %d ; \n", state->y + strip_row));
// UINT8 * bbb = state->buffer + strip_row * (state->bytes / rows_per_strip); // UINT8 * bbb = state->buffer + strip_row * (state->bytes /
// TRACE(("chars: %x %x %x %x\n", ((UINT8 *)bbb)[0], ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3])); // rows_per_strip); TRACE(("chars: %x %x %x %x\n", ((UINT8 *)bbb)[0],
// ((UINT8 *)bbb)[1], ((UINT8 *)bbb)[2], ((UINT8 *)bbb)[3]));
shuffler( shuffler(
(UINT8 *)im->image[state->y + state->yoff + strip_row] + (UINT8 *)im->image[state->y + state->yoff + strip_row] +
@ -666,7 +699,6 @@ ImagingLibTiffDecode(
goto decode_err; goto decode_err;
} }
TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric); TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &photometric);
TIFFGetField(tiff, TIFFTAG_COMPRESSION, &compression); TIFFGetField(tiff, TIFFTAG_COMPRESSION, &compression);
TIFFGetFieldDefaulted(tiff, TIFFTAG_PLANARCONFIG, &planarconfig); TIFFGetFieldDefaulted(tiff, TIFFTAG_PLANARCONFIG, &planarconfig);
@ -675,16 +707,17 @@ ImagingLibTiffDecode(
// Let LibTiff read them as RGBA // Let LibTiff read them as RGBA
readAsRGBA = photometric == PHOTOMETRIC_YCBCR; readAsRGBA = photometric == PHOTOMETRIC_YCBCR;
if (readAsRGBA && compression == COMPRESSION_JPEG && planarconfig == PLANARCONFIG_CONTIG) { if (readAsRGBA && compression == COMPRESSION_JPEG &&
// If using new JPEG compression, let libjpeg do RGB conversion for performance reasons planarconfig == PLANARCONFIG_CONTIG) {
// If using new JPEG compression, let libjpeg do RGB conversion for performance
// reasons
TIFFSetField(tiff, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); TIFFSetField(tiff, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
readAsRGBA = 0; readAsRGBA = 0;
} }
if (readAsRGBA) { if (readAsRGBA) {
_decodeAsRGBA(im, state, tiff); _decodeAsRGBA(im, state, tiff);
} } else {
else {
planes = _pickUnpackers(im, state, tiff, planarconfig, unpackers); planes = _pickUnpackers(im, state, tiff, planarconfig, unpackers);
if (planes <= 0) { if (planes <= 0) {
goto decode_err; goto decode_err;
@ -692,8 +725,7 @@ ImagingLibTiffDecode(
if (TIFFIsTiled(tiff)) { if (TIFFIsTiled(tiff)) {
_decodeTile(im, state, tiff, planes, unpackers); _decodeTile(im, state, tiff, planes, unpackers);
} } else {
else {
_decodeStrip(im, state, tiff, planes, unpackers); _decodeStrip(im, state, tiff, planes, unpackers);
} }
@ -706,11 +738,11 @@ ImagingLibTiffDecode(
ImagingShuffler shuffle; ImagingShuffler shuffle;
INT32 y; INT32 y;
TIFFGetFieldDefaulted(tiff, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo); TIFFGetFieldDefaulted(
tiff, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo);
if (extrasamples >= 1 && if (extrasamples >= 1 && (sampleinfo[0] == EXTRASAMPLE_UNSPECIFIED ||
(sampleinfo[0] == EXTRASAMPLE_UNSPECIFIED || sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA) sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA)) {
) {
shuffle = ImagingFindUnpacker("RGBA", "RGBa", NULL); shuffle = ImagingFindUnpacker("RGBA", "RGBa", NULL);
for (y = state->yoff; y < state->ysize; y++) { for (y = state->yoff; y < state->ysize; y++) {
@ -973,7 +1005,8 @@ ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8 *buffer, int byt
} }
if (state->state == 1 && !clientstate->fp) { if (state->state == 1 && !clientstate->fp) {
int read = (int)_tiffReadProc(clientstate, (tdata_t)buffer, (tsize_t)bytes); int read =
(int)_tiffReadProc((thandle_t)clientstate, (tdata_t)buffer, (tsize_t)bytes);
TRACE( TRACE(
("Buffer: %p: %c%c%c%c\n", ("Buffer: %p: %c%c%c%c\n",
buffer, buffer,

View File

@ -1437,90 +1437,90 @@ band3I(UINT8 *out, const UINT8 *in, int pixels) {
} }
static void static void
band016B(UINT8* out, const UINT8* in, int pixels) band016B(UINT8 *out, const UINT8 *in, int pixels) {
{
int i; int i;
/* band 0 only, big endian */ /* band 0 only, big endian */
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
out[0] = in[0]; out[0] = in[0];
out += 4; in += 2; out += 4;
in += 2;
} }
} }
static void static void
band116B(UINT8* out, const UINT8* in, int pixels) band116B(UINT8 *out, const UINT8 *in, int pixels) {
{
int i; int i;
/* band 1 only, big endian */ /* band 1 only, big endian */
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
out[1] = in[0]; out[1] = in[0];
out += 4; in += 2; out += 4;
in += 2;
} }
} }
static void static void
band216B(UINT8* out, const UINT8* in, int pixels) band216B(UINT8 *out, const UINT8 *in, int pixels) {
{
int i; int i;
/* band 2 only, big endian */ /* band 2 only, big endian */
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
out[2] = in[0]; out[2] = in[0];
out += 4; in += 2; out += 4;
in += 2;
} }
} }
static void static void
band316B(UINT8* out, const UINT8* in, int pixels) band316B(UINT8 *out, const UINT8 *in, int pixels) {
{
int i; int i;
/* band 3 only, big endian */ /* band 3 only, big endian */
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
out[3] = in[0]; out[3] = in[0];
out += 4; in += 2; out += 4;
in += 2;
} }
} }
static void static void
band016L(UINT8* out, const UINT8* in, int pixels) band016L(UINT8 *out, const UINT8 *in, int pixels) {
{
int i; int i;
/* band 0 only, little endian */ /* band 0 only, little endian */
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
out[0] = in[1]; out[0] = in[1];
out += 4; in += 2; out += 4;
in += 2;
} }
} }
static void static void
band116L(UINT8* out, const UINT8* in, int pixels) band116L(UINT8 *out, const UINT8 *in, int pixels) {
{
int i; int i;
/* band 1 only, little endian */ /* band 1 only, little endian */
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
out[1] = in[1]; out[1] = in[1];
out += 4; in += 2; out += 4;
in += 2;
} }
} }
static void static void
band216L(UINT8* out, const UINT8* in, int pixels) band216L(UINT8 *out, const UINT8 *in, int pixels) {
{
int i; int i;
/* band 2 only, little endian */ /* band 2 only, little endian */
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
out[2] = in[1]; out[2] = in[1];
out += 4; in += 2; out += 4;
in += 2;
} }
} }
static void static void
band316L(UINT8* out, const UINT8* in, int pixels) band316L(UINT8 *out, const UINT8 *in, int pixels) {
{
int i; int i;
/* band 3 only, little endian */ /* band 3 only, little endian */
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
out[3] = in[1]; out[3] = in[1];
out += 4; in += 2; out += 4;
in += 2;
} }
} }
@ -1687,7 +1687,6 @@ static struct {
{"RGB", "G;16N", 16, band116L}, {"RGB", "G;16N", 16, band116L},
{"RGB", "B;16N", 16, band216L}, {"RGB", "B;16N", 16, band216L},
{"RGBA", "R;16N", 16, band016L}, {"RGBA", "R;16N", 16, band016L},
{"RGBA", "G;16N", 16, band116L}, {"RGBA", "G;16N", 16, band116L},
{"RGBA", "B;16N", 16, band216L}, {"RGBA", "B;16N", 16, band216L},