From 270ae862460a9a5f6757af8439e7c8ce42b1f34b Mon Sep 17 00:00:00 2001 From: Yay295 Date: Tue, 11 Jul 2023 20:33:19 +1000 Subject: [PATCH 01/11] Add image equality tests --- Tests/test_image_equality.py | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Tests/test_image_equality.py diff --git a/Tests/test_image_equality.py b/Tests/test_image_equality.py new file mode 100644 index 00000000000..3b5dcaab494 --- /dev/null +++ b/Tests/test_image_equality.py @@ -0,0 +1,66 @@ +from __future__ import annotations + +import itertools + +import pytest + +from PIL import Image + + +@pytest.mark.parametrize("mode", Image.MODES) +def test_equal(mode): + num_img_bytes = len(Image.new(mode, (2, 2)).tobytes()) + data = bytes(range(ord("A"), ord("A") + num_img_bytes)) + img_a = Image.frombytes(mode, (2, 2), data) + img_b = Image.frombytes(mode, (2, 2), data) + assert img_a.tobytes() == img_b.tobytes() + assert img_a.im == img_b.im + + +# With mode "1" different bytes can map to the same value, +# so we have to be more specific with the values we use. +@pytest.mark.parametrize( + "bytes_a, bytes_b", + itertools.permutations( + (bytes(x) for x in itertools.product(b"\x00\xff", repeat=4)), 2 + ), +) +def test_not_equal_mode_1(bytes_a, bytes_b): + # Use rawmode "1;8" so that each full byte is interpreted as a value + # instead of the bits in the bytes being interpreted as values. + img_a = Image.frombytes("1", (2, 2), bytes_a, "raw", "1;8") + img_b = Image.frombytes("1", (2, 2), bytes_b, "raw", "1;8") + assert img_a.tobytes() != img_b.tobytes() + assert img_a.im != img_b.im + + +@pytest.mark.parametrize("mode", [mode for mode in Image.MODES if mode != "1"]) +def test_not_equal(mode): + num_img_bytes = len(Image.new(mode, (2, 2)).tobytes()) + data_a = bytes(range(ord("A"), ord("A") + num_img_bytes)) + data_b = bytes(range(ord("Z"), ord("Z") - num_img_bytes, -1)) + img_a = Image.frombytes(mode, (2, 2), data_a) + img_b = Image.frombytes(mode, (2, 2), data_b) + assert img_a.tobytes() != img_b.tobytes() + assert img_a.im != img_b.im + + +@pytest.mark.parametrize("mode", ("RGB", "YCbCr", "HSV", "LAB")) +def test_equal_three_channels_four_bytes(mode): + # The "A" and "B" values in LAB images are signed values from -128 to 127, + # but we store them as unsigned values from 0 to 255, so we need to use + # slightly different input bytes for LAB to get the same output. + img_a = Image.new(mode, (1, 1), 0x00B3B231 if mode == "LAB" else 0x00333231) + img_b = Image.new(mode, (1, 1), 0xFFB3B231 if mode == "LAB" else 0xFF333231) + assert img_a.tobytes() == b"123" + assert img_b.tobytes() == b"123" + assert img_a.im == img_b.im + + +@pytest.mark.parametrize("mode", ("LA", "La", "PA")) +def test_equal_two_channels_four_bytes(mode): + img_a = Image.new(mode, (1, 1), 0x32000031) + img_b = Image.new(mode, (1, 1), 0x32FFFF31) + assert img_a.tobytes() == b"12" + assert img_b.tobytes() == b"12" + assert img_a.im == img_b.im From f85c600ea3284471350911604a5ac250345a1f36 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sun, 20 Sep 2026 22:18:07 +0300 Subject: [PATCH 02/11] Adjust equality tests * de-parametrize test_not_equal_mode_1 * add type annotations * test the Image's behavior, not the Core's --- Tests/test_image_equality.py | 50 +++++++++++++++++------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/Tests/test_image_equality.py b/Tests/test_image_equality.py index 3b5dcaab494..6e5a7c8c212 100644 --- a/Tests/test_image_equality.py +++ b/Tests/test_image_equality.py @@ -8,59 +8,55 @@ @pytest.mark.parametrize("mode", Image.MODES) -def test_equal(mode): +def test_equal(mode: str) -> None: num_img_bytes = len(Image.new(mode, (2, 2)).tobytes()) data = bytes(range(ord("A"), ord("A") + num_img_bytes)) img_a = Image.frombytes(mode, (2, 2), data) img_b = Image.frombytes(mode, (2, 2), data) assert img_a.tobytes() == img_b.tobytes() - assert img_a.im == img_b.im + assert img_a == img_b -# With mode "1" different bytes can map to the same value, -# so we have to be more specific with the values we use. -@pytest.mark.parametrize( - "bytes_a, bytes_b", - itertools.permutations( +def test_not_equal_mode_1() -> None: + # With mode "1" different bytes can map to the same value, + # so we have to be more specific with the values we use. + for bytes_a, bytes_b in itertools.permutations( (bytes(x) for x in itertools.product(b"\x00\xff", repeat=4)), 2 - ), -) -def test_not_equal_mode_1(bytes_a, bytes_b): - # Use rawmode "1;8" so that each full byte is interpreted as a value - # instead of the bits in the bytes being interpreted as values. - img_a = Image.frombytes("1", (2, 2), bytes_a, "raw", "1;8") - img_b = Image.frombytes("1", (2, 2), bytes_b, "raw", "1;8") - assert img_a.tobytes() != img_b.tobytes() - assert img_a.im != img_b.im + ): + # Use rawmode "1;8" so that each full byte is interpreted as a value + # instead of the bits in the bytes being interpreted as values. + img_a = Image.frombytes("1", (2, 2), bytes_a, "raw", "1;8") + img_b = Image.frombytes("1", (2, 2), bytes_b, "raw", "1;8") + assert img_a.tobytes() != img_b.tobytes() + assert img_a != img_b @pytest.mark.parametrize("mode", [mode for mode in Image.MODES if mode != "1"]) -def test_not_equal(mode): +def test_not_equal(mode: str) -> None: num_img_bytes = len(Image.new(mode, (2, 2)).tobytes()) data_a = bytes(range(ord("A"), ord("A") + num_img_bytes)) data_b = bytes(range(ord("Z"), ord("Z") - num_img_bytes, -1)) img_a = Image.frombytes(mode, (2, 2), data_a) img_b = Image.frombytes(mode, (2, 2), data_b) assert img_a.tobytes() != img_b.tobytes() - assert img_a.im != img_b.im + assert img_a != img_b @pytest.mark.parametrize("mode", ("RGB", "YCbCr", "HSV", "LAB")) -def test_equal_three_channels_four_bytes(mode): +def test_equal_three_channels_four_bytes(mode: str) -> None: # The "A" and "B" values in LAB images are signed values from -128 to 127, # but we store them as unsigned values from 0 to 255, so we need to use # slightly different input bytes for LAB to get the same output. img_a = Image.new(mode, (1, 1), 0x00B3B231 if mode == "LAB" else 0x00333231) img_b = Image.new(mode, (1, 1), 0xFFB3B231 if mode == "LAB" else 0xFF333231) - assert img_a.tobytes() == b"123" - assert img_b.tobytes() == b"123" - assert img_a.im == img_b.im - + assert img_a.tobytes() == img_b.tobytes() == b"123" + assert img_a == img_b @pytest.mark.parametrize("mode", ("LA", "La", "PA")) -def test_equal_two_channels_four_bytes(mode): +def test_equal_two_channels_four_bytes(mode: str) -> None: + # Test that for LA/La/PA modes, where the data is stored in the 1st and 4th + # byte of each pixel, the middle bytes are masked off for comparison. img_a = Image.new(mode, (1, 1), 0x32000031) img_b = Image.new(mode, (1, 1), 0x32FFFF31) - assert img_a.tobytes() == b"12" - assert img_b.tobytes() == b"12" - assert img_a.im == img_b.im + assert img_a.tobytes() == img_b.tobytes() == b"12" + assert img_a == img_b From 75451d35888a7f461dbe6f9f59244e9570b740e1 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sun, 20 Sep 2026 22:32:25 +0300 Subject: [PATCH 03/11] Add more tests for equality --- Tests/test_image_equality.py | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/Tests/test_image_equality.py b/Tests/test_image_equality.py index 6e5a7c8c212..98cb516154b 100644 --- a/Tests/test_image_equality.py +++ b/Tests/test_image_equality.py @@ -1,12 +1,28 @@ from __future__ import annotations import itertools +from unittest.mock import Mock import pytest from PIL import Image +def _make_rows(width: int, height: int) -> list[bytes]: + """Build test image data of width x height.""" + return [ + bytes((y * (width - 1) + x) % 256 for x in range(width)) for y in range(height) + ] + + +def _frombuffer( + mode: str, size: tuple[int, int], data: bytes, *, stride: int = 0, ystep: int = 1 +) -> Image.Image: + im = Image.frombuffer(mode, size, data, "raw", mode, stride, ystep) + assert im.readonly # Sanity check (that we took the map_buffer path) + return im + + @pytest.mark.parametrize("mode", Image.MODES) def test_equal(mode: str) -> None: num_img_bytes = len(Image.new(mode, (2, 2)).tobytes()) @@ -52,6 +68,7 @@ def test_equal_three_channels_four_bytes(mode: str) -> None: assert img_a.tobytes() == img_b.tobytes() == b"123" assert img_a == img_b + @pytest.mark.parametrize("mode", ("LA", "La", "PA")) def test_equal_two_channels_four_bytes(mode: str) -> None: # Test that for LA/La/PA modes, where the data is stored in the 1st and 4th @@ -60,3 +77,61 @@ def test_equal_two_channels_four_bytes(mode: str) -> None: img_b = Image.new(mode, (1, 1), 0x32FFFF31) assert img_a.tobytes() == img_b.tobytes() == b"12" assert img_a == img_b + + +def test_not_equal_rgbx_padding() -> None: + # Ensure RGBX's last byte is compared, even if it has no image meaning. + img_a = Image.frombytes("RGBX", (1, 1), b"1234") + img_b = Image.frombytes("RGBX", (1, 1), b"123\xff") + assert img_a.tobytes() != img_b.tobytes() + assert img_a != img_b + + +def test_compare_with_other_type() -> None: + im = Image.new("L", (1, 1)) + assert im.im == im.im + assert im.im != 42 + # Check that the other object's comparison method is called. + x = Mock(__eq__=Mock(return_value=True)) + assert im.im == x + assert x.__eq__.called # type: ignore[attr-defined] + + +def test_equal_frombuffer_stride() -> None: + # Test that a buffer-mapped image's padding bytes (stride) + # are not part of the comparison. + width, height, stride = 3, 4, 8 + rows = _make_rows(width, height) + buffer_a = b"".join(row + b"\x00" * (stride - width) for row in rows) + buffer_b = b"".join(row + b"\xff" * (stride - width) for row in rows) + assert buffer_a != buffer_b + img_a = _frombuffer("L", (width, height), buffer_a, stride=stride) + img_b = _frombuffer("L", (width, height), buffer_b, stride=stride) + assert img_a.tobytes() == img_b.tobytes() # Padding bytes disappear + assert img_a == img_b + + +def test_not_equal_frombuffer_stride() -> None: + # Test that differences within strided rows are found, + # even if padding matches. + width, height, stride = 3, 4, 8 + rows = _make_rows(width, height) + padding = b"\xab" * (stride - width) + buffer_a = b"".join(row + padding for row in rows) + rows[height - 1] = bytes(42) + rows[height - 1][1:] + buffer_b = b"".join(row + padding for row in rows) + + img_a = _frombuffer("L", (width, height), buffer_a, stride=stride) + img_b = _frombuffer("L", (width, height), buffer_b, stride=stride) + assert img_a.tobytes() != img_b.tobytes() + assert img_a != img_b + + +def test_equal_frombuffer_ystep() -> None: + # Test that ystep=-1 (rows in reverse order) is handled correctly. + width, height = 3, 4 + rows = _make_rows(width, height) + img_a = _frombuffer("L", (width, height), b"".join(rows)) + img_b = _frombuffer("L", (width, height), b"".join(reversed(rows)), ystep=-1) + assert img_a.tobytes() == img_b.tobytes() + assert img_a == img_b From 336b7deac2cf12a04258b8128416a89b4d57a892 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sun, 20 Sep 2026 16:41:16 +0300 Subject: [PATCH 04/11] Add tp_richcompare handler for Imaging_Type/ImagingCore Co-authored-by: Yay295 Co-authored-by: Andrew Murray --- docs/releasenotes/13.1.0.rst | 55 ++++++++++++++ src/PIL/Image.py | 20 +++-- src/_imaging.c | 139 +++++++++++++++++++++++++++++++++++ 3 files changed, 207 insertions(+), 7 deletions(-) create mode 100644 docs/releasenotes/13.1.0.rst diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst new file mode 100644 index 00000000000..5cbaa7cd6a5 --- /dev/null +++ b/docs/releasenotes/13.1.0.rst @@ -0,0 +1,55 @@ +13.1.0 +------ + +Security +======== + +TODO +^^^^ + +TODO + +:cve:`YYYY-XXXXX`: TODO +^^^^^^^^^^^^^^^^^^^^^^^ + +TODO + +Backwards incompatible changes +============================== + +TODO +^^^^ + +TODO + +Deprecations +============ + +TODO +^^^^ + +TODO + +API changes +=========== + +TODO +^^^^ + +TODO + +API additions +============= + +TODO +^^^^ + +TODO + +Other changes +============= + +Faster image comparison +^^^^^^^^^^^^^^^^^^^^^^^ + +Comparing two images with ``==`` was made faster by reducing data copying. \ No newline at end of file diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 40720c5d438..48555b03eba 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -754,13 +754,19 @@ def __eq__(self, other: object) -> bool: if self.__class__ is not other.__class__: return False assert isinstance(other, Image) - return ( - self.mode == other.mode - and self.size == other.size - and self.info == other.info - and self.getpalette() == other.getpalette() - and self.tobytes() == other.tobytes() - ) + if self is other: + return True + # Early-out before loading. + # ImagingCore does check for mode and size. + if ( + self.mode != other.mode + or self.size != other.size + or self.info != other.info + ): + return False + self.load() + other.load() + return self.im == other.im def __repr__(self) -> str: return ( diff --git a/src/_imaging.c b/src/_imaging.c index 6557649edef..2409278441c 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -4468,6 +4468,144 @@ static PySequenceMethods image_as_sequence = { (ssizessizeobjargproc)NULL, /*sq_ass_slice*/ }; +/** + * Compare two pixel arrays in the given mode for equality. + * + * @param mode The mode of the pixel arrays. + * @param ysize The number of rows in the pixel arrays. + * @param linesize The number of bytes in each row of the pixel arrays. + * @param pixels_a Pointer to the first array of pixel row arrays. + * @param pixels_b Pointer to the second array of pixel row arrays. + * @return 0 if the pixel arrays are equal, 1 if they are not equal. + */ +static int +_compare_pixels( + const ModeID mode, + const int ysize, + const int linesize, + const char *const *pixels_a, + const char *const *pixels_b +) { + if (ysize <= 0 || linesize <= 0) { + // Pixel arrays without size are always equal. + // Since rows may not have been allocated at all, + // early-out now. + return 0; + } + + // Fortunately, all of the modes that have extra bytes in their pixels + // use four bytes for their pixels. RGBX is deliberately not one of them: + // its fourth byte is compared just like Image.tobytes() includes it. + UINT32 mask = 0xffffffff; + switch (mode) { + case IMAGING_MODE_RGB: + case IMAGING_MODE_YCbCr: + case IMAGING_MODE_HSV: + case IMAGING_MODE_LAB: + // These modes have three channels in four bytes, + // so we have to ignore the last byte. +#ifdef WORDS_BIGENDIAN + mask = 0xffffff00; +#else + mask = 0x00ffffff; +#endif + break; + case IMAGING_MODE_LA: + case IMAGING_MODE_La: + case IMAGING_MODE_PA: + // These modes have two channels in four bytes, + // so we have to ignore the middle two bytes. + mask = 0xff0000ff; + break; + default: + break; + } + + if (mask == 0xffffffff) { + // If we aren't masking anything we can use memcmp. + for (int y = 0; y < ysize; y++) { + if (memcmp(pixels_a[y], pixels_b[y], linesize)) { + return 1; + } + } + } else { + const int xsize = linesize / 4; + for (int y = 0; y < ysize; y++) { + const UINT32 *line_a = (const UINT32 *)pixels_a[y]; + const UINT32 *line_b = (const UINT32 *)pixels_b[y]; + for (int x = 0; x < xsize; x++, line_a++, line_b++) { + if ((*line_a & mask) != (*line_b & mask)) { + return 1; + } + } + } + } + return 0; +} + +/** + * Compare two palettes for equality. + * + * @param palette_a Pointer to the first palette. May be NULL. + * @param palette_b Pointer to the second palette. May be NULL. + * @return 0 if the palettes are equal, 1 if they are not equal. + */ +static int +_compare_palette(const ImagingPalette palette_a, const ImagingPalette palette_b) { + if (palette_a == NULL && palette_b == NULL) { + return 0; + } + if (palette_a == NULL || palette_b == NULL) { + return 1; + } + if (palette_a->size != palette_b->size || palette_a->mode != palette_b->mode) { + return 1; + } + + const char *palette_a_data = (const char *)palette_a->palette; + const char *palette_b_data = (const char *)palette_b->palette; + // 1024 is the hard-coded maximum size of a palette + int linesize = MIN(palette_a->size * 4, 1024); + return _compare_pixels( + palette_a->mode, 1, linesize, &palette_a_data, &palette_b_data + ); +} + +static PyObject * +image_richcompare(PyObject *self, PyObject *other, int op) { + if (op != Py_EQ && op != Py_NE) { + Py_RETURN_NOTIMPLEMENTED; + } + + if (!PyImaging_Check(other)) { + Py_RETURN_NOTIMPLEMENTED; + } + + const Imaging img_a = ((ImagingObject *)self)->image; + const Imaging img_b = ((ImagingObject *)other)->image; + + if (img_a == NULL || img_b == NULL) { + Py_RETURN_NOTIMPLEMENTED; + } + + const int equal = + // Mode and size comparison (cheap) + img_a->mode == img_b->mode && img_a->xsize == img_b->xsize && + img_a->ysize == img_b->ysize && + // Palette comparison + _compare_palette(img_a->palette, img_b->palette) == 0 && + // Pixel comparison + _compare_pixels( + img_a->mode, + img_a->ysize, + img_a->linesize, + (const char *const *)img_a->image, + (const char *const *)img_b->image + ) == 0; + + return PyBool_FromLong(op == Py_EQ ? equal : !equal); +} + /* type description */ static PyTypeObject Imaging_Type = { @@ -4475,6 +4613,7 @@ static PyTypeObject Imaging_Type = { .tp_basicsize = sizeof(ImagingObject), .tp_dealloc = (destructor)_dealloc, .tp_as_sequence = &image_as_sequence, + .tp_richcompare = image_richcompare, .tp_methods = methods, .tp_getset = getsetters, }; From ea0f2991e500cf087e69a66943d322189f4f6b68 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sun, 20 Sep 2026 23:29:52 +0300 Subject: [PATCH 05/11] Use vectorisable form of masked comparison loop --- src/_imaging.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/_imaging.c b/src/_imaging.c index 2409278441c..bc8ec30ca64 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -4529,14 +4529,30 @@ _compare_pixels( } } } else { + // All modes where we use a mask store four bytes per pixel. const int xsize = linesize / 4; for (int y = 0; y < ysize; y++) { const UINT32 *line_a = (const UINT32 *)pixels_a[y]; const UINT32 *line_b = (const UINT32 *)pixels_b[y]; - for (int x = 0; x < xsize; x++, line_a++, line_b++) { - if ((*line_a & mask) != (*line_b & mask)) { - return 1; - } + // Check the first pixel separately; if it's different, + // we don't need to enter the loop at all. + if ((line_a[0] ^ line_b[0]) & mask) { + return 1; + } + // Autovectorizable form: compute one row at a time, + // and OR together all the differences in that row, + // then see if we saw any differences at all. + // This loop will not bail out in the middle of a row + // (the common case of "entirely different images, + // same size and mode" will have been taken care of + // above in the first-pixel check), but it measures + // to be faster anyhow on modern CPUs. + UINT32 diff = 0; + for (int x = 0; x < xsize; x++) { + diff |= (line_a[x] ^ line_b[x]) & mask; + } + if (diff) { + return 1; } } } From bebf0ab134fa661105578cb02bac8cddc5046d8c Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sun, 11 Oct 2026 22:12:56 +0900 Subject: [PATCH 06/11] Add tests to verify palette comparison behavior --- Tests/test_image_equality.py | 45 ++++++++++++++++++++++++++++++++++++ docs/releasenotes/13.1.0.rst | 10 +++++--- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Tests/test_image_equality.py b/Tests/test_image_equality.py index 98cb516154b..85d9e6d70c1 100644 --- a/Tests/test_image_equality.py +++ b/Tests/test_image_equality.py @@ -135,3 +135,48 @@ def test_equal_frombuffer_ystep() -> None: img_b = _frombuffer("L", (width, height), b"".join(reversed(rows)), ystep=-1) assert img_a.tobytes() == img_b.tobytes() assert img_a == img_b + + +def _make_palette_image(mode: str, data: bytes, rawmode: str) -> Image.Image: + im = Image.new(mode, (2, 2)) + im.putpalette(data, rawmode) + return im + + +@pytest.mark.parametrize("mode", ("P", "PA")) +def test_equal_rgba_palette(mode: str) -> None: + img_a = _make_palette_image(mode, b"\x01\x02\x03\x80" * 256, "RGBA") + img_b = _make_palette_image(mode, b"\x01\x02\x03\x80" * 256, "RGBA") + assert img_a == img_b + + +@pytest.mark.parametrize("mode", ("P", "PA")) +def test_not_equal_palette_alpha(mode: str) -> None: + # Palettes that differ only in alpha are not equal. + img_a = _make_palette_image(mode, b"\x01\x02\x03\xff" * 256, "RGBA") + img_b = _make_palette_image(mode, b"\x01\x02\x03\x00" * 256, "RGBA") + assert img_a.getpalette() == img_b.getpalette() # implicitly mode=RGB, so same + assert img_a != img_b + + +@pytest.mark.parametrize("mode", ("P", "PA")) +def test_not_equal_palette_mode(mode: str) -> None: + # An RGB palette is not the same thing as an otherwise identical opaque + # RGBA palette, since they e.g. save differently. + img_a = _make_palette_image(mode, b"\x01\x02\x03" * 256, "RGB") + img_b = _make_palette_image(mode, b"\x01\x02\x03\xff" * 256, "RGBA") + assert img_a.getpalette() == img_b.getpalette() # implicitly mode=RGB, so same + assert img_a != img_b + + +@pytest.mark.parametrize("mode", ("P", "PA")) +def test_equal_rgb_palette_padding(mode: str) -> None: + # For RGB palettes, the unused fourth byte is not part of the comparison. + img_a = _make_palette_image(mode, b"\x01\x02\x03\x00" * 256, "RGBX") + img_b = _make_palette_image(mode, b"\x01\x02\x03\xff" * 256, "RGBX") + palette_a = img_a.palette + palette_b = img_b.palette + assert palette_a + assert palette_b + assert palette_a.mode == palette_b.mode == "RGB" + assert img_a == img_b diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 5cbaa7cd6a5..e84caf66421 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -49,7 +49,11 @@ TODO Other changes ============= -Faster image comparison -^^^^^^^^^^^^^^^^^^^^^^^ +Faster and more accurate image comparison +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Comparing two images with ``==`` was made faster by reducing data copying. -Comparing two images with ``==`` was made faster by reducing data copying. \ No newline at end of file +For ``P`` and ``PA`` images, comparison now also takes palette mode and alpha +into account. Previously palettes that differed only in alpha, or an RGB palette +identical with a fully opaque RGBA palette compared equal. From 9dca7bc673adeefbe429d6708ac9176cb51c288c Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Mon, 12 Oct 2026 15:30:44 +0900 Subject: [PATCH 07/11] Add special case for mode-1 comparison --- Tests/test_image_equality.py | 8 ++++++++ src/_imaging.c | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Tests/test_image_equality.py b/Tests/test_image_equality.py index 85d9e6d70c1..de9d142142f 100644 --- a/Tests/test_image_equality.py +++ b/Tests/test_image_equality.py @@ -47,6 +47,14 @@ def test_not_equal_mode_1() -> None: assert img_a != img_b +def test_equal_mode_1_nonzero_values() -> None: + # Any nonzero byte is a set pixel in mode "1", so 1 and 255 are equal. + img_a = Image.new("1", (1, 1), 1) + img_b = Image.new("1", (1, 1), 255) + assert img_a.tobytes() == img_b.tobytes() + assert img_a == img_b + + @pytest.mark.parametrize("mode", [mode for mode in Image.MODES if mode != "1"]) def test_not_equal(mode: str) -> None: num_img_bytes = len(Image.new(mode, (2, 2)).tobytes()) diff --git a/src/_imaging.c b/src/_imaging.c index bc8ec30ca64..a1bb454b1fc 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -4493,6 +4493,28 @@ _compare_pixels( return 0; } + if (mode == IMAGING_MODE_1) { + // Mode 1, special case: compare the truthiness of each value. + for (int y = 0; y < ysize; y++) { + const UINT8 *line_a = (const UINT8 *)pixels_a[y]; + const UINT8 *line_b = (const UINT8 *)pixels_b[y]; + // Check the first pixel separately; if it's different, + // we don't need to enter the loop at all. + if ((line_a[0] == 0) != (line_b[0] == 0)) { + return 1; + } + // Autovectorizable (see the masked loop below) + UINT8 diff = 0; + for (int x = 0; x < linesize; x++) { + diff |= (line_a[x] == 0) ^ (line_b[x] == 0); + } + if (diff) { + return 1; + } + } + return 0; + } + // Fortunately, all of the modes that have extra bytes in their pixels // use four bytes for their pixels. RGBX is deliberately not one of them: // its fourth byte is compared just like Image.tobytes() includes it. From 0c6d67add20b8a7bfdbd8c4383f6568c7687254a Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Mon, 2 Nov 2026 10:21:05 +0200 Subject: [PATCH 08/11] Avoid unittest.mock --- Tests/test_image_equality.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Tests/test_image_equality.py b/Tests/test_image_equality.py index de9d142142f..323da09392e 100644 --- a/Tests/test_image_equality.py +++ b/Tests/test_image_equality.py @@ -1,7 +1,6 @@ from __future__ import annotations import itertools -from unittest.mock import Mock import pytest @@ -99,10 +98,17 @@ def test_compare_with_other_type() -> None: im = Image.new("L", (1, 1)) assert im.im == im.im assert im.im != 42 - # Check that the other object's comparison method is called. - x = Mock(__eq__=Mock(return_value=True)) + + class EqMock: + eq_called = False + + def __eq__(self, other: object) -> bool: + self.eq_called = True + return True + + x = EqMock() assert im.im == x - assert x.__eq__.called # type: ignore[attr-defined] + assert x.eq_called def test_equal_frombuffer_stride() -> None: From 4bec1baf0d2ad1ac3aa8ce5681c1272a5d732062 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Mon, 2 Nov 2026 10:22:40 +0200 Subject: [PATCH 09/11] Clarify comment --- src/_imaging.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/_imaging.c b/src/_imaging.c index 52ac1a9b165..e3ba28426cf 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -4600,7 +4600,8 @@ _compare_palette(const ImagingPalette palette_a, const ImagingPalette palette_b) const char *palette_a_data = (const char *)palette_a->palette; const char *palette_b_data = (const char *)palette_b->palette; - // 1024 is the hard-coded maximum size of a palette + // 1024 is the maximum size of a palette hard-coded in Imaging.h's + // ImagingPaletteInstance struct. int linesize = MIN(palette_a->size * 4, 1024); return _compare_pixels( palette_a->mode, 1, linesize, &palette_a_data, &palette_b_data From 108b42fad9ddf0767bf94df44edfd092870933be Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 17 Nov 2026 11:25:00 +1100 Subject: [PATCH 10/11] Exclude padding byte in RGBX --- Tests/test_image_equality.py | 21 --------------------- src/_imaging.c | 4 ++-- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/Tests/test_image_equality.py b/Tests/test_image_equality.py index 323da09392e..98fa043bbbc 100644 --- a/Tests/test_image_equality.py +++ b/Tests/test_image_equality.py @@ -86,14 +86,6 @@ def test_equal_two_channels_four_bytes(mode: str) -> None: assert img_a == img_b -def test_not_equal_rgbx_padding() -> None: - # Ensure RGBX's last byte is compared, even if it has no image meaning. - img_a = Image.frombytes("RGBX", (1, 1), b"1234") - img_b = Image.frombytes("RGBX", (1, 1), b"123\xff") - assert img_a.tobytes() != img_b.tobytes() - assert img_a != img_b - - def test_compare_with_other_type() -> None: im = Image.new("L", (1, 1)) assert im.im == im.im @@ -181,16 +173,3 @@ def test_not_equal_palette_mode(mode: str) -> None: img_b = _make_palette_image(mode, b"\x01\x02\x03\xff" * 256, "RGBA") assert img_a.getpalette() == img_b.getpalette() # implicitly mode=RGB, so same assert img_a != img_b - - -@pytest.mark.parametrize("mode", ("P", "PA")) -def test_equal_rgb_palette_padding(mode: str) -> None: - # For RGB palettes, the unused fourth byte is not part of the comparison. - img_a = _make_palette_image(mode, b"\x01\x02\x03\x00" * 256, "RGBX") - img_b = _make_palette_image(mode, b"\x01\x02\x03\xff" * 256, "RGBX") - palette_a = img_a.palette - palette_b = img_b.palette - assert palette_a - assert palette_b - assert palette_a.mode == palette_b.mode == "RGB" - assert img_a == img_b diff --git a/src/_imaging.c b/src/_imaging.c index acb7d9fade6..ca95e0d193c 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -4575,11 +4575,11 @@ _compare_pixels( } // Fortunately, all of the modes that have extra bytes in their pixels - // use four bytes for their pixels. RGBX is deliberately not one of them: - // its fourth byte is compared just like Image.tobytes() includes it. + // use four bytes for their pixels. UINT32 mask = 0xffffffff; switch (mode) { case IMAGING_MODE_RGB: + case IMAGING_MODE_RGBX: case IMAGING_MODE_YCbCr: case IMAGING_MODE_HSV: case IMAGING_MODE_LAB: From 47c923c859ccd66d2d1290b9d03843e4b3954599 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 17 Nov 2026 13:14:53 +1100 Subject: [PATCH 11/11] Use band count, rather than listing individual modes --- Tests/test_image_equality.py | 117 +++++++++------------------------- docs/releasenotes/13.1.0.rst | 9 --- src/PIL/Image.py | 2 - src/_imaging.c | 119 ++++++++++++++--------------------- 4 files changed, 77 insertions(+), 170 deletions(-) diff --git a/Tests/test_image_equality.py b/Tests/test_image_equality.py index 98fa043bbbc..45f2851cfb2 100644 --- a/Tests/test_image_equality.py +++ b/Tests/test_image_equality.py @@ -1,64 +1,45 @@ from __future__ import annotations -import itertools - import pytest from PIL import Image -def _make_rows(width: int, height: int) -> list[bytes]: - """Build test image data of width x height.""" +def _make_rows(size: tuple[int, int]) -> list[bytes]: + # Build test image data return [ - bytes((y * (width - 1) + x) % 256 for x in range(width)) for y in range(height) + bytes((y * (size[0] - 1) + x) % 256 for x in range(size[0])) + for y in range(size[1]) ] -def _frombuffer( - mode: str, size: tuple[int, int], data: bytes, *, stride: int = 0, ystep: int = 1 -) -> Image.Image: - im = Image.frombuffer(mode, size, data, "raw", mode, stride, ystep) - assert im.readonly # Sanity check (that we took the map_buffer path) - return im - - @pytest.mark.parametrize("mode", Image.MODES) def test_equal(mode: str) -> None: num_img_bytes = len(Image.new(mode, (2, 2)).tobytes()) - data = bytes(range(ord("A"), ord("A") + num_img_bytes)) + data = bytes(range(num_img_bytes)) img_a = Image.frombytes(mode, (2, 2), data) img_b = Image.frombytes(mode, (2, 2), data) assert img_a.tobytes() == img_b.tobytes() assert img_a == img_b -def test_not_equal_mode_1() -> None: - # With mode "1" different bytes can map to the same value, - # so we have to be more specific with the values we use. - for bytes_a, bytes_b in itertools.permutations( - (bytes(x) for x in itertools.product(b"\x00\xff", repeat=4)), 2 - ): - # Use rawmode "1;8" so that each full byte is interpreted as a value - # instead of the bits in the bytes being interpreted as values. - img_a = Image.frombytes("1", (2, 2), bytes_a, "raw", "1;8") - img_b = Image.frombytes("1", (2, 2), bytes_b, "raw", "1;8") - assert img_a.tobytes() != img_b.tobytes() - assert img_a != img_b - - def test_equal_mode_1_nonzero_values() -> None: - # Any nonzero byte is a set pixel in mode "1", so 1 and 255 are equal. + # 1 mode pixels are either empty (0) or not (1-255) img_a = Image.new("1", (1, 1), 1) img_b = Image.new("1", (1, 1), 255) assert img_a.tobytes() == img_b.tobytes() assert img_a == img_b -@pytest.mark.parametrize("mode", [mode for mode in Image.MODES if mode != "1"]) +@pytest.mark.parametrize("mode", Image.MODES) def test_not_equal(mode: str) -> None: num_img_bytes = len(Image.new(mode, (2, 2)).tobytes()) - data_a = bytes(range(ord("A"), ord("A") + num_img_bytes)) - data_b = bytes(range(ord("Z"), ord("Z") - num_img_bytes, -1)) + if mode == "1": + data_a = bytes((0, 255)) + data_b = bytes((255, 0)) + else: + data_a = bytes(range(num_img_bytes)) + data_b = bytes(range(255, 255 - num_img_bytes, -1)) img_a = Image.frombytes(mode, (2, 2), data_a) img_b = Image.frombytes(mode, (2, 2), data_b) assert img_a.tobytes() != img_b.tobytes() @@ -67,11 +48,11 @@ def test_not_equal(mode: str) -> None: @pytest.mark.parametrize("mode", ("RGB", "YCbCr", "HSV", "LAB")) def test_equal_three_channels_four_bytes(mode: str) -> None: - # The "A" and "B" values in LAB images are signed values from -128 to 127, - # but we store them as unsigned values from 0 to 255, so we need to use - # slightly different input bytes for LAB to get the same output. - img_a = Image.new(mode, (1, 1), 0x00B3B231 if mode == "LAB" else 0x00333231) - img_b = Image.new(mode, (1, 1), 0xFFB3B231 if mode == "LAB" else 0xFF333231) + color_a = 0x00B3B231 if mode == "LAB" else 0x00333231 + color_b = color_a | 0xFF000000 + + img_a = Image.new(mode, (1, 1), color_a) + img_b = Image.new(mode, (1, 1), color_b) assert img_a.tobytes() == img_b.tobytes() == b"123" assert img_a == img_b @@ -79,14 +60,14 @@ def test_equal_three_channels_four_bytes(mode: str) -> None: @pytest.mark.parametrize("mode", ("LA", "La", "PA")) def test_equal_two_channels_four_bytes(mode: str) -> None: # Test that for LA/La/PA modes, where the data is stored in the 1st and 4th - # byte of each pixel, the middle bytes are masked off for comparison. + # byte of each pixel, the middle bytes are ignored. img_a = Image.new(mode, (1, 1), 0x32000031) img_b = Image.new(mode, (1, 1), 0x32FFFF31) assert img_a.tobytes() == img_b.tobytes() == b"12" assert img_a == img_b -def test_compare_with_other_type() -> None: +def test_types() -> None: im = Image.new("L", (1, 1)) assert im.im == im.im assert im.im != 42 @@ -103,46 +84,6 @@ def __eq__(self, other: object) -> bool: assert x.eq_called -def test_equal_frombuffer_stride() -> None: - # Test that a buffer-mapped image's padding bytes (stride) - # are not part of the comparison. - width, height, stride = 3, 4, 8 - rows = _make_rows(width, height) - buffer_a = b"".join(row + b"\x00" * (stride - width) for row in rows) - buffer_b = b"".join(row + b"\xff" * (stride - width) for row in rows) - assert buffer_a != buffer_b - img_a = _frombuffer("L", (width, height), buffer_a, stride=stride) - img_b = _frombuffer("L", (width, height), buffer_b, stride=stride) - assert img_a.tobytes() == img_b.tobytes() # Padding bytes disappear - assert img_a == img_b - - -def test_not_equal_frombuffer_stride() -> None: - # Test that differences within strided rows are found, - # even if padding matches. - width, height, stride = 3, 4, 8 - rows = _make_rows(width, height) - padding = b"\xab" * (stride - width) - buffer_a = b"".join(row + padding for row in rows) - rows[height - 1] = bytes(42) + rows[height - 1][1:] - buffer_b = b"".join(row + padding for row in rows) - - img_a = _frombuffer("L", (width, height), buffer_a, stride=stride) - img_b = _frombuffer("L", (width, height), buffer_b, stride=stride) - assert img_a.tobytes() != img_b.tobytes() - assert img_a != img_b - - -def test_equal_frombuffer_ystep() -> None: - # Test that ystep=-1 (rows in reverse order) is handled correctly. - width, height = 3, 4 - rows = _make_rows(width, height) - img_a = _frombuffer("L", (width, height), b"".join(rows)) - img_b = _frombuffer("L", (width, height), b"".join(reversed(rows)), ystep=-1) - assert img_a.tobytes() == img_b.tobytes() - assert img_a == img_b - - def _make_palette_image(mode: str, data: bytes, rawmode: str) -> Image.Image: im = Image.new(mode, (2, 2)) im.putpalette(data, rawmode) @@ -150,7 +91,11 @@ def _make_palette_image(mode: str, data: bytes, rawmode: str) -> Image.Image: @pytest.mark.parametrize("mode", ("P", "PA")) -def test_equal_rgba_palette(mode: str) -> None: +def test_equal_palette(mode: str) -> None: + img_a = _make_palette_image(mode, b"\x01\x02\x03" * 256, "RGB") + img_b = _make_palette_image(mode, b"\x01\x02\x03" * 256, "RGB") + assert img_a == img_b + img_a = _make_palette_image(mode, b"\x01\x02\x03\x80" * 256, "RGBA") img_b = _make_palette_image(mode, b"\x01\x02\x03\x80" * 256, "RGBA") assert img_a == img_b @@ -161,15 +106,13 @@ def test_not_equal_palette_alpha(mode: str) -> None: # Palettes that differ only in alpha are not equal. img_a = _make_palette_image(mode, b"\x01\x02\x03\xff" * 256, "RGBA") img_b = _make_palette_image(mode, b"\x01\x02\x03\x00" * 256, "RGBA") - assert img_a.getpalette() == img_b.getpalette() # implicitly mode=RGB, so same + assert img_a.getpalette() == img_b.getpalette() # implicit RGB rawmode + assert img_a.getpalette(None) != img_b.getpalette(None) assert img_a != img_b - -@pytest.mark.parametrize("mode", ("P", "PA")) -def test_not_equal_palette_mode(mode: str) -> None: - # An RGB palette is not the same thing as an otherwise identical opaque - # RGBA palette, since they e.g. save differently. + # An RGB palette is not the same as an otherwise identical opaque RGBA palette img_a = _make_palette_image(mode, b"\x01\x02\x03" * 256, "RGB") img_b = _make_palette_image(mode, b"\x01\x02\x03\xff" * 256, "RGBA") - assert img_a.getpalette() == img_b.getpalette() # implicitly mode=RGB, so same + assert img_a.getpalette() == img_b.getpalette() # implicit RGB rawmode + assert img_a.getpalette(None) != img_b.getpalette(None) assert img_a != img_b diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 35cecc07bea..44729c75bad 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -81,15 +81,6 @@ Scaling empty text :py:meth:`~PIL.ImageText.Text.wrap` now raises :py:exc:`ValueError` for empty or whitespace-only text with ``scaling``, before adjusting the font size. -Faster and more accurate image comparison -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -Comparing two images with ``==`` was made faster by reducing data copying. - -For ``P`` and ``PA`` images, comparison now also takes palette mode and alpha -into account. Previously palettes that differed only in alpha, or an RGB palette -identical with a fully opaque RGBA palette compared equal. - Writing to read-only images ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 8dc5b7e1b14..0c3cdb8bfbd 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -748,8 +748,6 @@ def __eq__(self, other: object) -> bool: assert isinstance(other, Image) if self is other: return True - # Early-out before loading. - # ImagingCore does check for mode and size. if ( self.mode != other.mode or self.size != other.size diff --git a/src/_imaging.c b/src/_imaging.c index ca95e0d193c..36270cb0146 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -4543,17 +4543,17 @@ _compare_pixels( const int ysize, const int linesize, const char *const *pixels_a, - const char *const *pixels_b + const char *const *pixels_b, + const UINT32 mask ) { if (ysize <= 0 || linesize <= 0) { - // Pixel arrays without size are always equal. - // Since rows may not have been allocated at all, - // early-out now. + // Pixel arrays without size are always equal return 0; } if (mode == IMAGING_MODE_1) { - // Mode 1, special case: compare the truthiness of each value. + // 1 mode pixels are either empty (0) or not (1-255), + // so compare the truthiness of each value for (int y = 0; y < ysize; y++) { const UINT8 *line_a = (const UINT8 *)pixels_a[y]; const UINT8 *line_b = (const UINT8 *)pixels_b[y]; @@ -4574,36 +4574,8 @@ _compare_pixels( return 0; } - // Fortunately, all of the modes that have extra bytes in their pixels - // use four bytes for their pixels. - UINT32 mask = 0xffffffff; - switch (mode) { - case IMAGING_MODE_RGB: - case IMAGING_MODE_RGBX: - case IMAGING_MODE_YCbCr: - case IMAGING_MODE_HSV: - case IMAGING_MODE_LAB: - // These modes have three channels in four bytes, - // so we have to ignore the last byte. -#ifdef WORDS_BIGENDIAN - mask = 0xffffff00; -#else - mask = 0x00ffffff; -#endif - break; - case IMAGING_MODE_LA: - case IMAGING_MODE_La: - case IMAGING_MODE_PA: - // These modes have two channels in four bytes, - // so we have to ignore the middle two bytes. - mask = 0xff0000ff; - break; - default: - break; - } - if (mask == 0xffffffff) { - // If we aren't masking anything we can use memcmp. + // We aren't masking anything for (int y = 0; y < ysize; y++) { if (memcmp(pixels_a[y], pixels_b[y], linesize)) { return 1; @@ -4623,11 +4595,10 @@ _compare_pixels( // Autovectorizable form: compute one row at a time, // and OR together all the differences in that row, // then see if we saw any differences at all. - // This loop will not bail out in the middle of a row - // (the common case of "entirely different images, - // same size and mode" will have been taken care of - // above in the first-pixel check), but it measures - // to be faster anyhow on modern CPUs. + // This loop will not bail out in the middle of a row (the common case of + // "entirely different images, same size and mode" + // will have been taken care of above in the first-pixel check), + // but regardless, it measures to be faster on modern CPUs. UINT32 diff = 0; for (int x = 0; x < xsize; x++) { diff |= (line_a[x] ^ line_b[x]) & mask; @@ -4643,30 +4614,22 @@ _compare_pixels( /** * Compare two palettes for equality. * - * @param palette_a Pointer to the first palette. May be NULL. - * @param palette_b Pointer to the second palette. May be NULL. + * @param a Pointer to the first palette. May be NULL. + * @param b Pointer to the second palette. May be NULL. * @return 0 if the palettes are equal, 1 if they are not equal. */ static int -_compare_palette(const ImagingPalette palette_a, const ImagingPalette palette_b) { - if (palette_a == NULL && palette_b == NULL) { +_compare_palette(const ImagingPalette a, const ImagingPalette b) { + if (a == NULL && b == NULL) { return 0; } - if (palette_a == NULL || palette_b == NULL) { - return 1; - } - if (palette_a->size != palette_b->size || palette_a->mode != palette_b->mode) { + if (a == NULL || b == NULL || a->size != b->size || a->mode != b->mode) { return 1; } - const char *palette_a_data = (const char *)palette_a->palette; - const char *palette_b_data = (const char *)palette_b->palette; - // 1024 is the maximum size of a palette hard-coded in Imaging.h's - // ImagingPaletteInstance struct. - int linesize = MIN(palette_a->size * 4, 1024); - return _compare_pixels( - palette_a->mode, 1, linesize, &palette_a_data, &palette_b_data - ); + const char *a_data = (const char *)a->palette; + const char *b_data = (const char *)b->palette; + return _compare_pixels(a->mode, 1, a->size * 4, &a_data, &b_data, 0xffffffff); } static PyObject * @@ -4679,28 +4642,40 @@ image_richcompare(PyObject *self, PyObject *other, int op) { Py_RETURN_NOTIMPLEMENTED; } - const Imaging img_a = ((ImagingObject *)self)->image; - const Imaging img_b = ((ImagingObject *)other)->image; + const Imaging a = ((ImagingObject *)self)->image; + const Imaging b = ((ImagingObject *)other)->image; - if (img_a == NULL || img_b == NULL) { + if (a == NULL || b == NULL) { Py_RETURN_NOTIMPLEMENTED; } - const int equal = - // Mode and size comparison (cheap) - img_a->mode == img_b->mode && img_a->xsize == img_b->xsize && - img_a->ysize == img_b->ysize && - // Palette comparison - _compare_palette(img_a->palette, img_b->palette) == 0 && - // Pixel comparison - _compare_pixels( - img_a->mode, - img_a->ysize, - img_a->linesize, - (const char *const *)img_a->image, - (const char *const *)img_b->image - ) == 0; + if (a->mode != b->mode || a->xsize != b->xsize || a->ysize != b->ysize || + _compare_palette(a->palette, b->palette) != 0) { + return op == Py_EQ ? Py_False : Py_True; + } + // All of the modes with padding bytes use four bytes for their pixels + UINT32 mask = 0xffffffff; + if (a->bands == 3) { + // Three channels in four bytes, so ignore the last byte +#ifdef WORDS_BIGENDIAN + mask = 0xffffff00; +#else + mask = 0x00ffffff; +#endif + } else if (a->bands == 2) { + // Two channels in four bytes, so ignore the middle two bytes + mask = 0xff0000ff; + } + + const int equal = _compare_pixels( + a->mode, + a->ysize, + a->linesize, + (const char *const *)a->image, + (const char *const *)b->image, + mask + ) == 0; return PyBool_FromLong(op == Py_EQ ? equal : !equal); }