Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix putdata() for I;16N on big-endian #7209

Merged
merged 16 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,11 @@ def hopper(mode: str | None = None, cache: dict[str, Image.Image] = {}) -> Image
# (for fast, isolated, repeatable tests).
im = cache.get(mode)
if im is None:
if mode == "F":
if mode == "RGBa":
im = hopper("RGBA").convert(mode)
elif mode == "La":
im = hopper("LA").convert(mode)
elif mode == "F":
im = hopper("L").convert(mode)
elif mode[:4] == "I;16":
im = hopper("I").convert(mode)
Expand Down
105 changes: 77 additions & 28 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,38 @@
skip_unless_feature,
)

# name, number of bands, pixel size
image_modes = (
("1", 1, 1),
("L", 1, 1),
("LA", 2, 4),
("La", 2, 4),
("P", 1, 1),
("PA", 2, 4),
("F", 1, 4),
("I", 1, 4),
("I;16", 1, 2),
("I;16L", 1, 2),
("I;16B", 1, 2),
("I;16N", 1, 2),
("RGB", 3, 4),
("RGBA", 4, 4),
("RGBa", 4, 4),
("RGBX", 4, 4),
("BGR;15", 3, 2),
("BGR;16", 3, 2),
("BGR;24", 3, 3),
("CMYK", 4, 4),
("YCbCr", 3, 4),
("HSV", 3, 4),
("LAB", 3, 4),
)

image_mode_names = [name for name, num_bands, pixelsize in image_modes]
Yay295 marked this conversation as resolved.
Show resolved Hide resolved


class TestImage:
@pytest.mark.parametrize(
"mode",
(
"1",
"P",
"PA",
"L",
"LA",
"La",
"F",
"I",
"I;16",
"I;16L",
"I;16B",
"I;16N",
"RGB",
"RGBX",
"RGBA",
"RGBa",
"BGR;15",
"BGR;16",
"BGR;24",
"CMYK",
"YCbCr",
"LAB",
"HSV",
),
)
@pytest.mark.parametrize("mode", image_mode_names)
def test_image_modes_success(self, mode: str) -> None:
Image.new(mode, (1, 1))

Expand Down Expand Up @@ -1042,6 +1044,53 @@ def test_close_graceful(self, caplog: pytest.LogCaptureFixture) -> None:
assert im.fp is None


class TestImageBytes:
sample_bytes = bytes(
range(2 * 2 * max(pixelsize for mode, num_bands, pixelsize in image_modes))
)

@pytest.mark.parametrize("mode", image_mode_names)
def test_roundtrip_bytes_constructor(self, mode: str):
source_image = hopper(mode)
source_bytes = source_image.tobytes()
copy_image = Image.frombytes(mode, source_image.size, source_bytes)
assert copy_image.tobytes() == source_bytes

@pytest.mark.parametrize("mode", image_mode_names)
def test_roundtrip_bytes_method(self, mode: str):
source_image = hopper(mode)
source_bytes = source_image.tobytes()
copy_image = Image.new(mode, source_image.size)
copy_image.frombytes(source_bytes)
assert copy_image.tobytes() == source_bytes

@pytest.mark.parametrize(("mode", "num_bands", "pixelsize"), image_modes)
def test_pixels_after_getdata_putdata(
self, mode: str, num_bands: int, pixelsize: int
):
image_byte_size = 2 * 2 * pixelsize
start_bytes = self.sample_bytes[:image_byte_size]
image = Image.frombytes(mode, (2, 2), start_bytes)

start_pixels = (
image.getpixel((0, 0)),
image.getpixel((0, 1)),
image.getpixel((1, 0)),
image.getpixel((1, 1)),
)

image.putdata(image.getdata())

end_pixels = (
image.getpixel((0, 0)),
image.getpixel((0, 1)),
image.getpixel((1, 0)),
image.getpixel((1, 1)),
)

assert start_pixels == end_pixels


class MockEncoder(ImageFile.PyEncoder):
pass

Expand Down
12 changes: 11 additions & 1 deletion src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,17 @@ if (PySequence_Check(op)) { \
int bigendian = 0;
if (image->type == IMAGING_TYPE_SPECIAL) {
// I;16*
bigendian = strcmp(image->mode, "I;16B") == 0;
if (strcmp(image->mode, "I;16N") == 0) {
#ifdef WORDS_BIGENDIAN
bigendian = 1;
#else
bigendian = 0;
#endif
} else if (strcmp(image->mode, "I;16B") == 0) {
bigendian = 1;
} else {
bigendian = 0;
}
}
for (i = x = y = 0; i < n; i++) {
set_value_to_item(seq, i);
Expand Down
Loading