diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index 31548bbc91f..28b75193de3 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -1,9 +1,39 @@ from __future__ import annotations +import secrets + import pytest from PIL import Image +mode_names_not_bgr = ( + "1", + "L", + "LA", + "La", + "P", + "PA", + "F", + "I", + "I;16", + "I;16L", + "I;16B", + "I;16N", + "RGB", + "RGBA", + "RGBa", + "RGBX", + # Image.frombytes() doesn't work with BGR modes: + # unknown raw mode for given image mode + # "BGR;15", + # "BGR;16", + # "BGR;24", + "CMYK", + "YCbCr", + "HSV", + "LAB", +) + def test_setmode() -> None: im = Image.new("L", (1, 1), 255) @@ -32,3 +62,59 @@ def test_setmode() -> None: im.im.setmode("L") with pytest.raises(ValueError): im.im.setmode("RGBABCDE") + + +@pytest.mark.parametrize("mode", mode_names_not_bgr) +def test_equal(mode): + num_img_bytes = len(Image.new(mode, (2, 2)).tobytes()) + # alternatively, random.randbytes() in Python 3.9 + data = secrets.token_bytes(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. +def test_not_equal_mode_1(): + data_a = data_b = bytes(secrets.choice(b"\x00\xff") for i in range(4)) + while data_a == data_b: + data_b = bytes(secrets.choice(b"\x00\xff") for i in range(4)) + # We need to 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), data_a, "raw", "1;8") + img_b = Image.frombytes("1", (2, 2), data_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 mode_names_not_bgr if mode != "1"]) +def test_not_equal(mode): + num_img_bytes = len(Image.new(mode, (2, 2)).tobytes()) + # alternatively, random.randbytes() in Python 3.9 + data_a = data_b = secrets.token_bytes(num_img_bytes) + while data_a == data_b: + data_b = secrets.token_bytes(num_img_bytes) + 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): + 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 diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 958b95e3b0f..4638144a6bb 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -644,14 +644,18 @@ def _dump( return filename def __eq__(self, other): - return ( + if self is other: + return True + if not ( self.__class__ is other.__class__ and self.mode == other.mode and self.size == other.size and self.info == other.info - and self.getpalette() == other.getpalette() - and self.tobytes() == other.tobytes() - ) + ): + return False + self.load() + other.load() + return self.im == other.im def __repr__(self) -> str: return "<%s.%s image mode=%s size=%dx%d at 0x%X>" % ( diff --git a/src/_imaging.c b/src/_imaging.c index c565c21bb15..7081edf94aa 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3793,6 +3793,129 @@ static PySequenceMethods image_as_sequence = { (ssizessizeobjargproc)NULL, /*sq_ass_slice*/ }; +/* +Returns 0 if all of the pixels are the same, otherwise 1. +Skips unused bytes based on the given mode. +*/ +static int +_compare_pixels( + const char *mode, + const int ysize, + const int linesize, + const UINT8 **pixels_a, + const UINT8 **pixels_b) { + // Fortunately, all of the modes that have extra bytes in their pixels use four + // bytes for their pixels. + UINT32 mask = 0xffffffff; + if (!strcmp(mode, "RGB") || !strcmp(mode, "YCbCr") || !strcmp(mode, "HSV") || + !strcmp(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 + } else if (!strcmp(mode, "LA") || !strcmp(mode, "La") || !strcmp(mode, "PA")) { + // These modes have two channels in four bytes, + // so we have to ignore the middle two bytes. + mask = 0xff0000ff; + } + + if (mask == 0xffffffff) { + // If we aren't masking anything we can use memcmp. + int y; + for (y = 0; y < ysize; y++) { + if (memcmp(pixels_a[y], pixels_b[y], linesize)) { + return 1; + } + } + } else { + const int xsize = linesize / 4; + int y, x; + for (y = 0; y < ysize; y++) { + UINT32 *line_a = (UINT32 *)pixels_a[y]; + UINT32 *line_b = (UINT32 *)pixels_b[y]; + for (x = 0; x < xsize; x++, line_a++, line_b++) { + if ((*line_a & mask) != (*line_b & mask)) { + return 1; + } + } + } + } + return 0; +} + +static PyObject * +image_richcompare(const ImagingObject *self, const PyObject *other, const int op) { + if (op != Py_EQ && op != Py_NE) { + Py_RETURN_NOTIMPLEMENTED; + } + + // If the other object is not an ImagingObject. + if (!PyImaging_Check(other)) { + if (op == Py_EQ) { + Py_RETURN_FALSE; + } else { + Py_RETURN_TRUE; + } + } + + const Imaging img_a = self->image; + const Imaging img_b = ((ImagingObject *)other)->image; + + if (strcmp(img_a->mode, img_b->mode) || img_a->xsize != img_b->xsize || + img_a->ysize != img_b->ysize) { + if (op == Py_EQ) { + Py_RETURN_FALSE; + } else { + Py_RETURN_TRUE; + } + } + + const ImagingPalette palette_a = img_a->palette; + const ImagingPalette palette_b = img_b->palette; + if (palette_a || palette_b) { + const UINT8 *palette_a_data = palette_a->palette; + const UINT8 *palette_b_data = palette_b->palette; + const UINT8 **palette_a_data_ptr = &palette_a_data; + const UINT8 **palette_b_data_ptr = &palette_b_data; + if (!palette_a || !palette_b || palette_a->size != palette_b->size || + strcmp(palette_a->mode, palette_b->mode) || + _compare_pixels( + palette_a->mode, + 1, + palette_a->size * 4, + palette_a_data_ptr, + palette_b_data_ptr)) { + if (op == Py_EQ) { + Py_RETURN_FALSE; + } else { + Py_RETURN_TRUE; + } + } + } + + if (_compare_pixels( + img_a->mode, + img_a->ysize, + img_a->linesize, + (const UINT8 **)img_a->image, + (const UINT8 **)img_b->image)) { + if (op == Py_EQ) { + Py_RETURN_FALSE; + } else { + Py_RETURN_TRUE; + } + } else { + if (op == Py_EQ) { + Py_RETURN_TRUE; + } else { + Py_RETURN_FALSE; + } + } +} + /* type description */ static PyTypeObject Imaging_Type = { @@ -3800,32 +3923,32 @@ static PyTypeObject Imaging_Type = { sizeof(ImagingObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ /* methods */ - (destructor)_dealloc, /*tp_dealloc*/ - 0, /*tp_vectorcall_offset*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_as_async*/ - 0, /*tp_repr*/ - 0, /*tp_as_number*/ - &image_as_sequence, /*tp_as_sequence*/ - 0, /*tp_as_mapping*/ - 0, /*tp_hash*/ - 0, /*tp_call*/ - 0, /*tp_str*/ - 0, /*tp_getattro*/ - 0, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT, /*tp_flags*/ - 0, /*tp_doc*/ - 0, /*tp_traverse*/ - 0, /*tp_clear*/ - 0, /*tp_richcompare*/ - 0, /*tp_weaklistoffset*/ - 0, /*tp_iter*/ - 0, /*tp_iternext*/ - methods, /*tp_methods*/ - 0, /*tp_members*/ - getsetters, /*tp_getset*/ + (destructor)_dealloc, /*tp_dealloc*/ + 0, /*tp_vectorcall_offset*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_as_async*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + &image_as_sequence, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + 0, /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + (richcmpfunc)image_richcompare, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + methods, /*tp_methods*/ + 0, /*tp_members*/ + getsetters, /*tp_getset*/ }; #ifdef WITH_IMAGEDRAW