From 12685ef7c13b54761f01553ee42890039076c3da Mon Sep 17 00:00:00 2001 From: Diederik Veeze Date: Sat, 25 Jun 2016 23:14:21 +0200 Subject: [PATCH 1/3] Convert P to LA without deforming image --- libImaging/Convert.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libImaging/Convert.c b/libImaging/Convert.c index 99d5ae10d..995ba36a9 100644 --- a/libImaging/Convert.c +++ b/libImaging/Convert.c @@ -903,6 +903,18 @@ p2l(UINT8* out, const UINT8* in, int xsize, const UINT8* palette) *out++ = L(&palette[in[x]*4]) / 1000; } +static void +p2la(UINT8* out, const UINT8* in, int xsize, const UINT8* palette) +{ + int x; + /* FIXME: precalculate greyscale palette? */ + for (x = 0; x < xsize; x++, out+=4) { + const UINT8* rgba = &palette[*in++ * 4]; + out[0] = L(rgba) / 1000; + out[3] = rgba[3]; + } +} + static void pa2la(UINT8* out, const UINT8* in, int xsize, const UINT8* palette) { @@ -1005,7 +1017,7 @@ frompalette(Imaging imOut, Imaging imIn, const char *mode) else if (strcmp(mode, "L") == 0) convert = p2l; else if (strcmp(mode, "LA") == 0) - convert = (alpha) ? pa2la : p2l; + convert = (alpha) ? pa2la : p2la; else if (strcmp(mode, "I") == 0) convert = p2i; else if (strcmp(mode, "F") == 0) From 95de0b6d0576469501529599758d40e74304a999 Mon Sep 17 00:00:00 2001 From: Diederik Veeze Date: Sun, 26 Jun 2016 12:50:38 +0200 Subject: [PATCH 2/3] Testing for p2la --- Tests/test_image_convert.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 8197a66e0..900be8bab 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -123,6 +123,26 @@ class TestImageConvert(PillowTestCase): self.assertNotIn('transparency', p.info) p.save(f) + # ref https://github.com/python-pillow/Pillow/issues/1979 + + def test_p_la(self): + def L(rgb): + return int((rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000) + + def convert_to_gray(im, pixel): + if im.mode == 'P': + color_id = im.getpixel(pixel) + color = im.getpalette()[color_id * 3:(color_id+1)*3] + return L(color) + elif im.mode == 'LA': + color = im.getpixel(pixel) + return color[0] + + im_p = hopper('P') + im_la = im_p.convert('LA') + self.assertEqual(convert_to_gray(im_p, (5, 5)), + convert_to_gray(im_la, (5, 5))) + if __name__ == '__main__': unittest.main() From ca69de192b4969ad550474385d8f932575d79be7 Mon Sep 17 00:00:00 2001 From: Diederik Veeze Date: Sun, 26 Jun 2016 12:51:48 +0200 Subject: [PATCH 3/3] Assign to unused layers as well, jtbs --- libImaging/Convert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libImaging/Convert.c b/libImaging/Convert.c index 995ba36a9..a73d7aecd 100644 --- a/libImaging/Convert.c +++ b/libImaging/Convert.c @@ -910,7 +910,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] = L(rgba) / 1000; + out[0] = out[1] = out[2] = L(rgba) / 1000; out[3] = rgba[3]; } }