From 276915e5f6e7e810bb5faefa1798e7988872b22c Mon Sep 17 00:00:00 2001 From: Yay295 Date: Tue, 4 Jul 2023 01:43:46 -0500 Subject: [PATCH 01/12] add tp_richcompare handler for Imaging_Type/ImagingCore --- Tests/test_lib_image.py | 82 +++++++++++++++++++++++ src/PIL/Image.py | 12 ++-- src/_imaging.c | 142 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 231 insertions(+), 5 deletions(-) diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index f6818be46dc..1f3f3e11300 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -1,7 +1,37 @@ +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(): im = Image.new("L", (1, 1), 255) @@ -30,3 +60,55 @@ def test_setmode(): 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 + + +@pytest.mark.parametrize("mode", mode_names_not_bgr) +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.skip(reason="no way to directly set C bytes from Python") +@pytest.mark.parametrize("mode", ("RGB", "RGBX", "YCbCr", "HSV", "LAB")) +def test_equal_three_channels_four_bytes(mode): + img_a = Image.frombytes("RGBA", (2, 2), b"ABC1DEF2GHI3JKL4") + img_b = Image.frombytes("RGBA", (2, 2), b"ABC5DEF6GHI7JKL8") + # this only sets the mode in Python, not C + img_a.mode = mode + img_b.mode = mode + assert img_a.tobytes() == b"ABCDEFGHIJKL" + assert img_b.tobytes() == b"ABCDEFGHIJKL" + # this fails because the C code still thinks the mode is RGBA + assert img_a.im == img_b.im + + +@pytest.mark.skip(reason="no way to directly set C bytes from Python") +@pytest.mark.parametrize("mode", ("LA", "La", "PA")) +def test_equal_two_channels_four_bytes(mode): + img_a = Image.frombytes("RGBA", (2, 2), b"1AB23CD45EF67GH8") + img_b = Image.frombytes("RGBA", (2, 2), b"1IJ23KL45MN67OP8") + # this only sets the mode in Python, not C + img_a.mode = mode + img_b.mode = mode + assert img_a.tobytes() == b"12345678" + assert img_b.tobytes() == b"12345678" + # this fails because the C code still thinks the mode is RGBA + assert img_a.im == img_b.im diff --git a/src/PIL/Image.py b/src/PIL/Image.py index a519a28af36..856a4150345 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -597,14 +597,18 @@ def _dump(self, file=None, format=None, **options): 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): return "<%s.%s image mode=%s size=%dx%d at 0x%X>" % ( diff --git a/src/_imaging.c b/src/_imaging.c index e15cb89fcea..381c041d0e4 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3721,6 +3721,146 @@ 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, "RGBX") || + !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. + mask ^= 0xff; + } 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; + } + + PyErr_Format(PyExc_Exception, "mode mask : %s 0x%08x", mode, mask); + + 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++) { + UINT32 *line_a = (UINT32*)pixels_a[y]; + UINT32 *line_b = (UINT32*)pixels_b[y]; + for (int x = 0; x < xsize; x++, line_a++, line_b++) { + if ((*line_a & mask) != (*line_b & mask)) { + PyErr_Format( + PyExc_Exception, + "line %i index %i value a b mask : 0x%08x 0x%08x 0x%08x", + y, + x, + *line_a, + *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)) { + return op == Py_EQ ? Py_False : Py_True; + } + + const Imaging img_a = self->image; + const Imaging img_b = ((ImagingObject*)other)->image; + + PyErr_Format( + PyExc_Exception, + "a mode, b mode, diff : %s %s %i", + img_a->mode, + img_b->mode, + strcmp(img_a->mode, img_a->mode) + ); + + if ( + strcmp(img_a->mode, img_b->mode) + || img_a->xsize != img_b->xsize + || img_a->ysize != img_b->ysize + ) { + return op == Py_EQ ? Py_False : Py_True; + } + + const ImagingPalette palette_a = img_a->palette; + const ImagingPalette palette_b = img_b->palette; + if (palette_a || palette_b) { + PyErr_Format( + PyExc_Exception, + "pa mode, pb mode, diff : %s %s %i", + palette_a->mode, + palette_b->mode, + strcmp(palette_a->mode, palette_b->mode) + ); + + 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 + ) + ) { + return op == Py_EQ ? Py_False : Py_True; + } + } + + PyErr_Format( + PyExc_Exception, + "linesize a b : %i %i", + img_a->linesize, + img_b->linesize + ); + + if (_compare_pixels( + img_a->mode, + img_a->ysize, + img_a->linesize, + (const UINT8 **)img_a->image, + (const UINT8 **)img_b->image) + ) { + PyErr_Clear(); + return op == Py_EQ ? Py_False : Py_True; + } else { + PyErr_Clear(); + return op == Py_EQ ? Py_True : Py_False; + } +} + /* type description */ static PyTypeObject Imaging_Type = { @@ -3747,7 +3887,7 @@ static PyTypeObject Imaging_Type = { 0, /*tp_doc*/ 0, /*tp_traverse*/ 0, /*tp_clear*/ - 0, /*tp_richcompare*/ + (richcmpfunc)image_richcompare, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ 0, /*tp_iter*/ 0, /*tp_iternext*/ From 181803227d63ddfbb4d39f8963a6f473969ec6c9 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Tue, 4 Jul 2023 01:46:33 -0500 Subject: [PATCH 02/12] remove debugging messages --- src/_imaging.c | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/src/_imaging.c b/src/_imaging.c index 381c041d0e4..ed2c040d504 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3745,8 +3745,6 @@ _compare_pixels( mask = 0xff0000ff; } - PyErr_Format(PyExc_Exception, "mode mask : %s 0x%08x", mode, mask); - if (mask == 0xffffffff) { // If we aren't masking anything we can use memcmp. for (int y = 0; y < ysize; y++) { @@ -3761,15 +3759,6 @@ _compare_pixels( UINT32 *line_b = (UINT32*)pixels_b[y]; for (int x = 0; x < xsize; x++, line_a++, line_b++) { if ((*line_a & mask) != (*line_b & mask)) { - PyErr_Format( - PyExc_Exception, - "line %i index %i value a b mask : 0x%08x 0x%08x 0x%08x", - y, - x, - *line_a, - *line_b, - mask - ); return 1; } } @@ -3792,14 +3781,6 @@ image_richcompare(const ImagingObject *self, const PyObject *other, const int op const Imaging img_a = self->image; const Imaging img_b = ((ImagingObject*)other)->image; - PyErr_Format( - PyExc_Exception, - "a mode, b mode, diff : %s %s %i", - img_a->mode, - img_b->mode, - strcmp(img_a->mode, img_a->mode) - ); - if ( strcmp(img_a->mode, img_b->mode) || img_a->xsize != img_b->xsize @@ -3811,14 +3792,6 @@ image_richcompare(const ImagingObject *self, const PyObject *other, const int op const ImagingPalette palette_a = img_a->palette; const ImagingPalette palette_b = img_b->palette; if (palette_a || palette_b) { - PyErr_Format( - PyExc_Exception, - "pa mode, pb mode, diff : %s %s %i", - palette_a->mode, - palette_b->mode, - strcmp(palette_a->mode, palette_b->mode) - ); - 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; @@ -3839,13 +3812,6 @@ image_richcompare(const ImagingObject *self, const PyObject *other, const int op } } - PyErr_Format( - PyExc_Exception, - "linesize a b : %i %i", - img_a->linesize, - img_b->linesize - ); - if (_compare_pixels( img_a->mode, img_a->ysize, @@ -3853,10 +3819,8 @@ image_richcompare(const ImagingObject *self, const PyObject *other, const int op (const UINT8 **)img_a->image, (const UINT8 **)img_b->image) ) { - PyErr_Clear(); return op == Py_EQ ? Py_False : Py_True; } else { - PyErr_Clear(); return op == Py_EQ ? Py_True : Py_False; } } From fedd5da415a7e54627eb458af54123422df50af9 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Tue, 4 Jul 2023 16:55:03 -0500 Subject: [PATCH 03/12] extract mode "1" to its own test --- Tests/test_lib_image.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index 1f3f3e11300..57b0f25be24 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -73,7 +73,19 @@ def test_equal(mode): assert img_a.im == img_b.im -@pytest.mark.parametrize("mode", mode_names_not_bgr) +# 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 = "".join(secrets.choice(0x00, 0xFF) for i in range(4)) + while data_a == data_b: + data_b = "".join(secrets.choice(0x00, 0xFF) for i in range(4)) + img_a = Image.frombytes("1", (2, 2), data_a) + img_b = Image.frombytes("1", (2, 2), data_b) + 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 From d1d0a87627b8fb5cb66dc77d67bc4ab81b8b2574 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Tue, 4 Jul 2023 17:41:45 -0500 Subject: [PATCH 04/12] fix bytes generation --- Tests/test_lib_image.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index 57b0f25be24..2d553612c79 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -76,9 +76,9 @@ def test_equal(mode): # 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 = "".join(secrets.choice(0x00, 0xFF) for i in range(4)) + data_a = data_b = bytes(secrets.choice(b"\x00\xff") for i in range(4)) while data_a == data_b: - data_b = "".join(secrets.choice(0x00, 0xFF) for i in range(4)) + data_b = bytes(secrets.choice(b"\x00\xff") for i in range(4)) img_a = Image.frombytes("1", (2, 2), data_a) img_b = Image.frombytes("1", (2, 2), data_b) assert img_a.tobytes() != img_b.tobytes() From d3a92260676164dada220b8af8c064a6602bcd05 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Tue, 4 Jul 2023 22:23:15 -0500 Subject: [PATCH 05/12] use rawmode "1;8" when creating mode "1" image from bytes --- Tests/test_lib_image.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index 2d553612c79..6fe4d501bc2 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -79,8 +79,10 @@ 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)) - img_a = Image.frombytes("1", (2, 2), data_a) - img_b = Image.frombytes("1", (2, 2), data_b) + # 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 From 8b2321563578d480742139baff1925569a95ec3f Mon Sep 17 00:00:00 2001 From: Yay295 Date: Tue, 4 Jul 2023 23:09:36 -0500 Subject: [PATCH 06/12] declare variables before loop --- src/_imaging.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/_imaging.c b/src/_imaging.c index ed2c040d504..a63fcd4167d 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3747,17 +3747,19 @@ _compare_pixels( if (mask == 0xffffffff) { // If we aren't masking anything we can use memcmp. - for (int y = 0; y < ysize; y++) { + 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; - for (int y = 0; y < ysize; y++) { + int y, x; + for (y = 0; y < ysize; y++) { UINT32 *line_a = (UINT32*)pixels_a[y]; UINT32 *line_b = (UINT32*)pixels_b[y]; - for (int x = 0; x < xsize; x++, line_a++, line_b++) { + for (x = 0; x < xsize; x++, line_a++, line_b++) { if ((*line_a & mask) != (*line_b & mask)) { return 1; } From ae1f9e9e60ed699630c3a9a5098aefe9616983be Mon Sep 17 00:00:00 2001 From: Yay295 Date: Wed, 5 Jul 2023 08:33:46 -0500 Subject: [PATCH 07/12] use Py_RETURN_* macros for Py_True/Py_False --- src/_imaging.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/_imaging.c b/src/_imaging.c index a63fcd4167d..13197bf09ea 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3777,7 +3777,11 @@ image_richcompare(const ImagingObject *self, const PyObject *other, const int op // If the other object is not an ImagingObject. if (!PyImaging_Check(other)) { - return op == Py_EQ ? Py_False : Py_True; + if (op == Py_EQ) { + Py_RETURN_FALSE; + } else { + Py_RETURN_TRUE; + } } const Imaging img_a = self->image; @@ -3788,7 +3792,11 @@ image_richcompare(const ImagingObject *self, const PyObject *other, const int op || img_a->xsize != img_b->xsize || img_a->ysize != img_b->ysize ) { - return op == Py_EQ ? Py_False : Py_True; + if (op == Py_EQ) { + Py_RETURN_FALSE; + } else { + Py_RETURN_TRUE; + } } const ImagingPalette palette_a = img_a->palette; @@ -3810,20 +3818,34 @@ image_richcompare(const ImagingObject *self, const PyObject *other, const int op palette_b_data_ptr ) ) { - return op == Py_EQ ? Py_False : Py_True; + if (op == Py_EQ) { + Py_RETURN_FALSE; + } else { + Py_RETURN_TRUE; + } } } - if (_compare_pixels( + if ( + _compare_pixels( img_a->mode, img_a->ysize, img_a->linesize, (const UINT8 **)img_a->image, - (const UINT8 **)img_b->image) + (const UINT8 **)img_b->image + ) ) { - return op == Py_EQ ? Py_False : Py_True; + if (op == Py_EQ) { + Py_RETURN_FALSE; + } else { + Py_RETURN_TRUE; + } } else { - return op == Py_EQ ? Py_True : Py_False; + if (op == Py_EQ) { + Py_RETURN_TRUE; + } else { + Py_RETURN_FALSE; + } } } From bac58a63226112d6939df2c17cba8983d17af3f5 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Thu, 6 Jul 2023 10:19:55 -0500 Subject: [PATCH 08/12] fix ImagingCore.tp_richcompare test for RGB and YCbCr also, RGBX technically has 4 channels, so it shouldn't be masked. also, fix the mask for modes with three channels. --- Tests/test_lib_image.py | 18 +++++++++--------- src/_imaging.c | 10 +++++++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index 6fe4d501bc2..d493844a93a 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -100,17 +100,17 @@ def test_not_equal(mode): assert img_a.im != img_b.im -@pytest.mark.skip(reason="no way to directly set C bytes from Python") -@pytest.mark.parametrize("mode", ("RGB", "RGBX", "YCbCr", "HSV", "LAB")) -def test_equal_three_channels_four_bytes(mode): - img_a = Image.frombytes("RGBA", (2, 2), b"ABC1DEF2GHI3JKL4") - img_b = Image.frombytes("RGBA", (2, 2), b"ABC5DEF6GHI7JKL8") - # this only sets the mode in Python, not C - img_a.mode = mode - img_b.mode = mode +@pytest.mark.parametrize( + ("mode", "rawmode"), + (("RGB", "RGBX"), ("YCbCr", "YCbCrX"), ("HSV", None), ("LAB", None)), +) +def test_equal_three_channels_four_bytes(mode, rawmode): + if rawmode is None: + pytest.skip("no 4-byte rawmode for " + mode) + img_a = Image.frombytes(mode, (2, 2), b"ABC1DEF2GHI3JKL4", "raw", rawmode) + img_b = Image.frombytes(mode, (2, 2), b"ABC5DEF6GHI7JKL8", "raw", rawmode) assert img_a.tobytes() == b"ABCDEFGHIJKL" assert img_b.tobytes() == b"ABCDEFGHIJKL" - # this fails because the C code still thinks the mode is RGBA assert img_a.im == img_b.im diff --git a/src/_imaging.c b/src/_imaging.c index 13197bf09ea..04c12188e64 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3733,12 +3733,16 @@ _compare_pixels( // 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, "RGBX") || - !strcmp(mode, "YCbCr") || !strcmp(mode, "HSV") || !strcmp(mode, "LAB") + !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. - mask ^= 0xff; +#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. From 1713f5919485a5b746e1e6232e0ebbe3283e40e1 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Thu, 6 Jul 2023 20:54:22 -0500 Subject: [PATCH 09/12] add Image.im.set_internal_pixel_bytes() for testing --- Tests/test_lib_image.py | 26 ++++++++++---------------- src/_imaging.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index d493844a93a..f24391680ec 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -100,29 +100,23 @@ def test_not_equal(mode): assert img_a.im != img_b.im -@pytest.mark.parametrize( - ("mode", "rawmode"), - (("RGB", "RGBX"), ("YCbCr", "YCbCrX"), ("HSV", None), ("LAB", None)), -) -def test_equal_three_channels_four_bytes(mode, rawmode): - if rawmode is None: - pytest.skip("no 4-byte rawmode for " + mode) - img_a = Image.frombytes(mode, (2, 2), b"ABC1DEF2GHI3JKL4", "raw", rawmode) - img_b = Image.frombytes(mode, (2, 2), b"ABC5DEF6GHI7JKL8", "raw", rawmode) +@pytest.mark.parametrize("mode", ("RGB", "YCbCr", "HSV", "LAB")) +def test_equal_three_channels_four_bytes(mode): + img_a = Image.new(mode, (2, 2)) + img_b = Image.new(mode, (2, 2)) + img_a.im.set_internal_pixel_bytes(b"ABC1DEF2GHI3JKL4") + img_b.im.set_internal_pixel_bytes(b"ABC5DEF6GHI7JKL8") assert img_a.tobytes() == b"ABCDEFGHIJKL" assert img_b.tobytes() == b"ABCDEFGHIJKL" assert img_a.im == img_b.im -@pytest.mark.skip(reason="no way to directly set C bytes from Python") @pytest.mark.parametrize("mode", ("LA", "La", "PA")) def test_equal_two_channels_four_bytes(mode): - img_a = Image.frombytes("RGBA", (2, 2), b"1AB23CD45EF67GH8") - img_b = Image.frombytes("RGBA", (2, 2), b"1IJ23KL45MN67OP8") - # this only sets the mode in Python, not C - img_a.mode = mode - img_b.mode = mode + img_a = Image.new(mode, (2, 2)) + img_b = Image.new(mode, (2, 2)) + img_a.im.set_internal_pixel_bytes(b"1AB23CD45EF67GH8") + img_b.im.set_internal_pixel_bytes(b"1IJ23KL45MN67OP8") assert img_a.tobytes() == b"12345678" assert img_b.tobytes() == b"12345678" - # this fails because the C code still thinks the mode is RGBA assert img_a.im == img_b.im diff --git a/src/_imaging.c b/src/_imaging.c index 04c12188e64..965d5932a91 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3534,6 +3534,34 @@ _save_ppm(ImagingObject *self, PyObject *args) { return Py_None; } +static PyObject * +_set_internal_pixel_bytes(ImagingObject *self, PyObject *args) { + char *bytes; + Py_ssize_t length; + + if (!PyArg_ParseTuple(args, "y#", &bytes, &length)) { + return NULL; + } + + const int ysize = self->image->ysize; + const int linesize = self->image->linesize; + const Py_ssize_t expected_bytes = ysize * linesize; + if (length != expected_bytes) { + return PyErr_Format( + PyExc_ValueError, "Expected %i bytes but got %i.", + expected_bytes, length + ); + } + + int y; + char **image_bytes = self->image->image; + for (y = 0; y < ysize; y++, bytes += linesize) { + memcpy(image_bytes[y], bytes, linesize); + } + + Py_RETURN_NONE; +} + /* -------------------------------------------------------------------- */ /* methods */ @@ -3636,6 +3664,8 @@ static struct PyMethodDef methods[] = { {"save_ppm", (PyCFunction)_save_ppm, METH_VARARGS}, + {"set_internal_pixel_bytes", (PyCFunction)_set_internal_pixel_bytes, METH_VARARGS}, + {NULL, NULL} /* sentinel */ }; From 40597e7e53f78188009dacfc48bf3d9c46eda807 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Thu, 6 Jul 2023 21:22:48 -0500 Subject: [PATCH 10/12] fix bytes for LAB --- Tests/test_lib_image.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index f24391680ec..f37a43ea2f2 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -104,8 +104,14 @@ def test_not_equal(mode): def test_equal_three_channels_four_bytes(mode): img_a = Image.new(mode, (2, 2)) img_b = Image.new(mode, (2, 2)) - img_a.im.set_internal_pixel_bytes(b"ABC1DEF2GHI3JKL4") - img_b.im.set_internal_pixel_bytes(b"ABC5DEF6GHI7JKL8") + if mode != "LAB": + img_a.im.set_internal_pixel_bytes(b"ABC1DEF2GHI3JKL4") + img_b.im.set_internal_pixel_bytes(b"ABC5DEF6GHI7JKL8") + else: + # 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. + img_a.im.set_internal_pixel_bytes(b"A\xc2\xc31D\xc5\xc62G\xc8\xc93J\xcb\xcc4") + img_b.im.set_internal_pixel_bytes(b"A\xc2\xc35D\xc5\xc66G\xc8\xc97J\xcb\xcc8") assert img_a.tobytes() == b"ABCDEFGHIJKL" assert img_b.tobytes() == b"ABCDEFGHIJKL" assert img_a.im == img_b.im From 6bbb4deee2e534f069bf6a1ebe2f71fa1a308131 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 11 Jul 2023 20:33:19 +1000 Subject: [PATCH 11/12] Use single integer color instead of adding set_internal_pixel_bytes() --- Tests/test_lib_image.py | 26 ++++++++------------------ src/_imaging.c | 30 ------------------------------ 2 files changed, 8 insertions(+), 48 deletions(-) diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index f37a43ea2f2..d599b625551 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -102,27 +102,17 @@ def test_not_equal(mode): @pytest.mark.parametrize("mode", ("RGB", "YCbCr", "HSV", "LAB")) def test_equal_three_channels_four_bytes(mode): - img_a = Image.new(mode, (2, 2)) - img_b = Image.new(mode, (2, 2)) - if mode != "LAB": - img_a.im.set_internal_pixel_bytes(b"ABC1DEF2GHI3JKL4") - img_b.im.set_internal_pixel_bytes(b"ABC5DEF6GHI7JKL8") - else: - # 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. - img_a.im.set_internal_pixel_bytes(b"A\xc2\xc31D\xc5\xc62G\xc8\xc93J\xcb\xcc4") - img_b.im.set_internal_pixel_bytes(b"A\xc2\xc35D\xc5\xc66G\xc8\xc97J\xcb\xcc8") - assert img_a.tobytes() == b"ABCDEFGHIJKL" - assert img_b.tobytes() == b"ABCDEFGHIJKL" + 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, (2, 2)) - img_b = Image.new(mode, (2, 2)) - img_a.im.set_internal_pixel_bytes(b"1AB23CD45EF67GH8") - img_b.im.set_internal_pixel_bytes(b"1IJ23KL45MN67OP8") - assert img_a.tobytes() == b"12345678" - assert img_b.tobytes() == b"12345678" + 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/_imaging.c b/src/_imaging.c index 965d5932a91..04c12188e64 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3534,34 +3534,6 @@ _save_ppm(ImagingObject *self, PyObject *args) { return Py_None; } -static PyObject * -_set_internal_pixel_bytes(ImagingObject *self, PyObject *args) { - char *bytes; - Py_ssize_t length; - - if (!PyArg_ParseTuple(args, "y#", &bytes, &length)) { - return NULL; - } - - const int ysize = self->image->ysize; - const int linesize = self->image->linesize; - const Py_ssize_t expected_bytes = ysize * linesize; - if (length != expected_bytes) { - return PyErr_Format( - PyExc_ValueError, "Expected %i bytes but got %i.", - expected_bytes, length - ); - } - - int y; - char **image_bytes = self->image->image; - for (y = 0; y < ysize; y++, bytes += linesize) { - memcpy(image_bytes[y], bytes, linesize); - } - - Py_RETURN_NONE; -} - /* -------------------------------------------------------------------- */ /* methods */ @@ -3664,8 +3636,6 @@ static struct PyMethodDef methods[] = { {"save_ppm", (PyCFunction)_save_ppm, METH_VARARGS}, - {"set_internal_pixel_bytes", (PyCFunction)_set_internal_pixel_bytes, METH_VARARGS}, - {NULL, NULL} /* sentinel */ }; From 79a2d2ce3015c80a970110deb390bad4783cee63 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 May 2024 10:13:09 +0000 Subject: [PATCH 12/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_imaging.c | 99 +++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 54 deletions(-) diff --git a/src/_imaging.c b/src/_imaging.c index bd466f24b56..7081edf94aa 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3799,15 +3799,16 @@ 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. + 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") - ) { + 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 @@ -3833,8 +3834,8 @@ _compare_pixels( 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]; + 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; @@ -3861,13 +3862,10 @@ image_richcompare(const ImagingObject *self, const PyObject *other, const int op } const Imaging img_a = self->image; - const Imaging img_b = ((ImagingObject*)other)->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 (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 { @@ -3882,18 +3880,14 @@ image_richcompare(const ImagingObject *self, const PyObject *other, const int op 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( + 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 - ) - ) { + palette_b_data_ptr)) { if (op == Py_EQ) { Py_RETURN_FALSE; } else { @@ -3902,15 +3896,12 @@ image_richcompare(const ImagingObject *self, const PyObject *other, const int op } } - if ( - _compare_pixels( + if (_compare_pixels( img_a->mode, img_a->ysize, img_a->linesize, (const UINT8 **)img_a->image, - (const UINT8 **)img_b->image - ) - ) { + (const UINT8 **)img_b->image)) { if (op == Py_EQ) { Py_RETURN_FALSE; } else { @@ -3932,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*/ + (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*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + methods, /*tp_methods*/ + 0, /*tp_members*/ + getsetters, /*tp_getset*/ }; #ifdef WITH_IMAGEDRAW