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

Added rounding when converting P and PA #5824

Merged
merged 1 commit into from Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion Tests/test_image_convert.py
Expand Up @@ -93,7 +93,7 @@ def test_trns_p(tmp_path):
f = str(tmp_path / "temp.png")

im_l = im.convert("L")
assert im_l.info["transparency"] == 0 # undone
assert im_l.info["transparency"] == 1 # undone
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first palette entry is (0, 1, 2). (0*299 + 1*587 + 2*114) / 1000 = 0.815, so this is now rounded to 1.

im_l.save(f)

im_rgb = im.convert("RGB")
Expand Down Expand Up @@ -170,6 +170,20 @@ def test_trns_RGB(tmp_path):
im_p.save(f)


@pytest.mark.parametrize("convert_mode", ("L", "LA", "I"))
def test_l_macro_rounding(convert_mode):
for mode in ("P", "PA"):
im = Image.new(mode, (1, 1))
im.palette.getcolor((0, 1, 2))

converted_im = im.convert(convert_mode)
px = converted_im.load()
converted_color = px[0, 0]
if convert_mode == "LA":
converted_color = converted_color[0]
assert converted_color == 1


def test_gif_with_rgba_palette_to_p():
# See https://github.com/python-pillow/Pillow/issues/2433
with Image.open("Tests/images/hopper.gif") as im:
Expand Down
12 changes: 6 additions & 6 deletions src/libImaging/Convert.c
Expand Up @@ -1013,7 +1013,7 @@ p2l(UINT8 *out, const UINT8 *in, int xsize, const UINT8 *palette) {
int x;
/* FIXME: precalculate greyscale palette? */
for (x = 0; x < xsize; x++) {
*out++ = L(&palette[in[x] * 4]) / 1000;
*out++ = L24(&palette[in[x] * 4]) >> 16;
}
}

Expand All @@ -1022,7 +1022,7 @@ pa2l(UINT8 *out, const UINT8 *in, int xsize, const UINT8 *palette) {
int x;
/* FIXME: precalculate greyscale palette? */
for (x = 0; x < xsize; x++, in += 4) {
*out++ = L(&palette[in[0] * 4]) / 1000;
*out++ = L24(&palette[in[0] * 4]) >> 16;
}
}

Expand All @@ -1044,7 +1044,7 @@ p2la(UINT8 *out, const UINT8 *in, int xsize, const UINT8 *palette) {
/* FIXME: precalculate greyscale palette? */
for (x = 0; x < xsize; x++, out += 4) {
const UINT8 *rgba = &palette[*in++ * 4];
out[0] = out[1] = out[2] = L(rgba) / 1000;
out[0] = out[1] = out[2] = L24(rgba) >> 16;
out[3] = rgba[3];
}
}
Expand All @@ -1054,7 +1054,7 @@ pa2la(UINT8 *out, const UINT8 *in, int xsize, const UINT8 *palette) {
int x;
/* FIXME: precalculate greyscale palette? */
for (x = 0; x < xsize; x++, in += 4, out += 4) {
out[0] = out[1] = out[2] = L(&palette[in[0] * 4]) / 1000;
out[0] = out[1] = out[2] = L24(&palette[in[0] * 4]) >> 16;
out[3] = in[3];
}
}
Expand All @@ -1063,7 +1063,7 @@ static void
p2i(UINT8 *out_, const UINT8 *in, int xsize, const UINT8 *palette) {
int x;
for (x = 0; x < xsize; x++, out_ += 4) {
INT32 v = L(&palette[in[x] * 4]) / 1000;
INT32 v = L24(&palette[in[x] * 4]) >> 16;
memcpy(out_, &v, sizeof(v));
}
}
Expand All @@ -1073,7 +1073,7 @@ pa2i(UINT8 *out_, const UINT8 *in, int xsize, const UINT8 *palette) {
int x;
INT32 *out = (INT32 *)out_;
for (x = 0; x < xsize; x++, in += 4) {
*out++ = L(&palette[in[0] * 4]) / 1000;
*out++ = L24(&palette[in[0] * 4]) >> 16;
}
}

Expand Down