Merge pull request #5824 from radarhere/l_macro

Added rounding when converting P and PA
This commit is contained in:
Hugo van Kemenade 2021-12-28 10:02:36 +02:00 committed by GitHub
commit e7b53259d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 7 deletions

View File

@ -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:

View File

@ -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;
}
}