From 1428e4140ae87de42a937e63ae20f01ab630dafe Mon Sep 17 00:00:00 2001 From: wiredfool Date: Tue, 7 Mar 2017 01:52:31 -0800 Subject: [PATCH] Test for identical handling of getheader/getdata --- PIL/GifImagePlugin.py | 6 +++--- Tests/images/gif_header_data.pkl | 17 +++++++++++++++++ Tests/test_file_gif.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 Tests/images/gif_header_data.pkl diff --git a/PIL/GifImagePlugin.py b/PIL/GifImagePlugin.py index e55460f2c..087b2d1c3 100644 --- a/PIL/GifImagePlugin.py +++ b/PIL/GifImagePlugin.py @@ -685,13 +685,13 @@ def _get_global_header(im, info): return [ b"GIF"+version + # signature + version o16(im.size[0]) + # canvas width - o16(im.size[1]) + # canvas height + o16(im.size[1]), # canvas height # Logical Screen Descriptor # size of global color table + global color table flag - o8(color_table_size + 128) + # packed fields + o8(color_table_size + 128), # packed fields # background + reserved/aspect - o8(background) + o8(0) + + o8(background) + o8(0), # Global Color Table _get_header_palette(palette_bytes) diff --git a/Tests/images/gif_header_data.pkl b/Tests/images/gif_header_data.pkl new file mode 100644 index 000000000..b08e7a5fe --- /dev/null +++ b/Tests/images/gif_header_data.pkl @@ -0,0 +1,17 @@ +(((lp1 +S'GIF87a\x10\x00\x10\x00' +p2 +aS'\x87' +aS'\x00\x00' +p3 +aS'\xff\xff\xff\xfe\xfe\xfe\xfd\xfd\xfd\xfc\xfc\xfc\xfb\xfb\xfb\xfa\xfa\xfa\xf9\xf9\xf9\xf8\xf8\xf8\xf7\xf7\xf7\xf6\xf6\xf6\xf5\xf5\xf5\xf4\xf4\xf4\xf3\xf3\xf3\xf2\xf2\xf2\xf1\xf1\xf1\xf0\xf0\xf0\xef\xef\xef\xee\xee\xee\xed\xed\xed\xec\xec\xec\xeb\xeb\xeb\xea\xea\xea\xe9\xe9\xe9\xe8\xe8\xe8\xe7\xe7\xe7\xe6\xe6\xe6\xe5\xe5\xe5\xe4\xe4\xe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\x89\x89\x89\x88\x88\x88\x87\x87\x87\x86\x86\x86\x85\x85\x85\x84\x84\x84\x83\x83\x83\x82\x82\x82\x81\x81\x81\x80\x80\x80\x7f\x7f\x7f~~~}}}|||{{{zzzyyyxxxwwwvvvuuutttsssrrrqqqpppooonnnmmmlllkkkjjjiiihhhgggfffeeedddcccbbbaaa```___^^^]]]\\\\\\[[[ZZZYYYXXXWWWVVVUUUTTTSSSRRRQQQPPPOOONNNMMMLLLKKKJJJIIIHHHGGGFFFEEEDDDCCCBBBAAA@@@???>>>===<<<;;;:::999888777666555444333222111000///...---,,,+++***)))(((\'\'\'&&&%%%$$$###"""!!! \x1f\x1f\x1f\x1e\x1e\x1e\x1d\x1d\x1d\x1c\x1c\x1c\x1b\x1b\x1b\x1a\x1a\x1a\x19\x19\x19\x18\x18\x18\x17\x17\x17\x16\x16\x16\x15\x15\x15\x14\x14\x14\x13\x13\x13\x12\x12\x12\x11\x11\x11\x10\x10\x10\x0f\x0f\x0f\x0e\x0e\x0e\r\r\r\x0c\x0c\x0c\x0b\x0b\x0b\n\n\n\t\t\t\x08\x08\x08\x07\x07\x07\x06\x06\x06\x05\x05\x05\x04\x04\x04\x03\x03\x03\x02\x02\x02\x01\x01\x01\x00\x00\x00' +p4 +aNtp5 +(lp6 +S',\x00\x00\x00\x00\x10\x00\x10\x00\x00' +p7 +aS'\x08' +aS'o\x00\x11\x08\x1cH\xb0 \x02\x0c\x08\x13*\\\x88\x01\x85\xc3\x87\x10#\xa2\xc0A\xb1\xa2\xc5\x8b8\x90h\xdc\xc8\xb1#\x12, C\x8a\x1c\x89\x05\x8d\xc9\x93(S\xa2\xc1\xc3\xb2\xa5\xcb\x97x\x10\xc9\x9cI\xb3&"L8s\xea\xdc\x89\t\x95\xcf\x9f@\x83\xa2\xc2E\xb4\xa8\xd1\xa3\xb8\x90)]\xca\xb4)2lP\xa3J\x9d\x8a\r\x9d\xd5\xabX\xb3\xa2\xc3\xc7\xb5\xab\xd7\xaf\xf8\x02\x02' +p8 +aS'\x00' +at. \ No newline at end of file diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 482a0a472..05a28995c 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -505,6 +505,35 @@ class TestFileGif(PillowTestCase): reloaded = Image.open(out) self.assert_image_equal(reloaded.convert('L'), im.convert('L')) + def test_getdata(self): + # test getheader/getdata against legacy values + # Create a 'P' image with holes in the palette + im = Image._wedge().resize((16,16)) + im.putpalette(ImagePalette.ImagePalette('RGB')) + im.info = {'background':0} + + passed_palette = bytes(bytearray([255-i//3 for i in range(768)])) + + GifImagePlugin._FORCE_OPTIMIZE = True + try: + h = GifImagePlugin.getheader(im, passed_palette) + d = GifImagePlugin.getdata(im) + + import cPickle as pickle + # Enable to get target values on pre-refactor version + # with open('Tests/images/gif_header_data.pkl', 'wb') as f: + # f.write(pickle.dumps((h,d))) + with open('Tests/images/gif_header_data.pkl', 'rb') as f: + (h_target,d_target) = pickle.loads(f.read()) + + self.assertEqual(h, h_target) + self.assertEqual(d, d_target) + finally: + GifImagePlugin._FORCE_OPTIMIZE = False + + + + if __name__ == '__main__': unittest.main()