Fix for #2258, 2 bit palette images corrupted

This commit is contained in:
wiredfool 2016-12-03 14:08:59 +00:00
parent b3f8b5fa7d
commit 88c43b61b7
2 changed files with 15 additions and 1 deletions

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase
from PIL import ImagePalette
from PIL import ImagePalette, Image
ImagePalette = ImagePalette.ImagePalette
@ -125,6 +125,19 @@ class TestImagePalette(PillowTestCase):
self.assertEqual(rawmode, "RGB")
self.assertEqual(data_in, data_out)
def test_2bit_palette(self):
# issue #2258, 2 bit palettes are corrupted.
outfile = self.tempfile('temp.png')
rgb = b'\x00' * 2 + b'\x01' * 2 + b'\x02' * 2
img = Image.frombytes('P', (6, 1), rgb)
img.putpalette('\xFF\x00\x00' '\x00\xFF\x00' '\x00\x00\xFF') # RGB
img.save(outfile, format='PNG')
reloaded = Image.open(outfile)
self.assert_image_equal(img, reloaded)
if __name__ == '__main__':
unittest.main()

View File

@ -194,6 +194,7 @@ packP2(UINT8* out, const UINT8* in, int pixels)
case 2:
out[0] = (in[0] << 6) |
((in[1] & 3) << 4);
break;
case 1:
out[0] = (in[0] << 6);
}