Skip to content

Commit

Permalink
Merge pull request #5824 from radarhere/l_macro
Browse files Browse the repository at this point in the history
Added rounding when converting P and PA
  • Loading branch information
hugovk committed Dec 28, 2021
2 parents 44bd03f + bb6212a commit e7b5325
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
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
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

0 comments on commit e7b5325

Please sign in to comment.