Merge pull request #1986 from didrix/fix-1979-p2la

Fix issue converting P mode to LA
This commit is contained in:
Alexander Karpinsky 2016-06-30 17:43:53 +04:00 committed by GitHub
commit 92660a1a05
2 changed files with 33 additions and 1 deletions

View File

@ -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()

View File

@ -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] = out[1] = out[2] = 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)