From 921df53b527e4ba79ac291c4e95f0f2cb40189bf Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 7 Oct 2023 14:07:06 +1100 Subject: [PATCH] This is not a bug. 4 byte mask cannot be applied to 333 byte data --- Tests/test_lib_image.py | 10 +++++----- src/_imaging.c | 12 +++--------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index 7d77f096393..308162f6eb7 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -26,12 +26,12 @@ "BGR;24", ) + @pytest.mark.parametrize("mode", mode_names) 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) + num_img_bytes = len(Image.new(mode, (4, 2)).tobytes()) + data = b"0\xd2\xf9\x92Db\xbd]\x84\x86fd%\xfeR\x1b0\xd2\xf9\x92Db\xbd]\x84\x86fd%\xfeR\x1b" + img_a = Image.frombytes(mode, (4, 2), data) + img_b = Image.frombytes(mode, (4, 2), data) assert img_a.tobytes() == img_b.tobytes() assert img_a.im == img_b.im diff --git a/src/_imaging.c b/src/_imaging.c index 637995aa2eb..aeb4b3ab2fd 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -3760,7 +3760,7 @@ _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, "BGR;24") || + !strcmp(mode, "RGB") || !strcmp(mode, "YCbCr") || !strcmp(mode, "HSV") || !strcmp(mode, "LAB") ) { // These modes have three channels in four bytes, @@ -3769,14 +3769,6 @@ _compare_pixels( mask = 0xffffff00; #else mask = 0x00ffffff; -#endif - } else if (!strcmp(mode, "BGR;15") || !strcmp(mode, "BGR;16")) { - // These modes have two channels in four bytes, - // so we have to ignore the last two bytes. -#ifdef WORDS_BIGENDIAN - mask = 0xffff0000; -#else - mask = 0x0000ffff; #endif } else if (!strcmp(mode, "LA") || !strcmp(mode, "La") || !strcmp(mode, "PA")) { // These modes have two channels in four bytes, @@ -3799,7 +3791,9 @@ _compare_pixels( UINT32 *line_a = (UINT32*)pixels_a[y]; UINT32 *line_b = (UINT32*)pixels_b[y]; for (x = 0; x < xsize; x++, line_a++, line_b++) { + printf("mask x %d y %d linea %u lineb %u mask %u lineamask %u linebmask %u\n", x, y, *line_a, *line_b, mask, *line_a & mask, *line_b & mask); if ((*line_a & mask) != (*line_b & mask)) { + printf("fail"); return 1; } }