From bb6212a332785ab3d07321811d2ab0d7d4677926 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 10 Nov 2021 08:00:58 +1100 Subject: [PATCH] Added rounding when converting P and PA --- Tests/test_image_convert.py | 16 +++++++++++++++- src/libImaging/Convert.c | 12 ++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 436a417d1..f12516952 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -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 im_l.save(f) im_rgb = im.convert("RGB") @@ -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: diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 9012cfcd7..517a4dbe3 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -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; } } @@ -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; } } @@ -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]; } } @@ -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]; } } @@ -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)); } } @@ -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; } }