Skip to content

Commit

Permalink
This is not a bug. 4 byte mask cannot be applied to 333 byte data
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Oct 7, 2023
1 parent 9a7bb52 commit 921df53
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
10 changes: 5 additions & 5 deletions Tests/test_lib_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 3 additions & 9 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 921df53

Please sign in to comment.