add tests for different PCX encoding cases

This commit is contained in:
homm 2016-07-28 05:29:24 +03:00
parent f8912a73e0
commit 978c37d699
2 changed files with 79 additions and 4 deletions

View File

@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase, hopper
from PIL import Image, PcxImagePlugin
from PIL import Image, ImageFile, PcxImagePlugin
class TestFilePcx(PillowTestCase):
@ -46,6 +46,84 @@ class TestFilePcx(PillowTestCase):
# Make sure all pixels are either 0 or 255.
self.assertEqual(im.histogram()[0] + im.histogram()[255], 447*144)
def test_1px_width(self):
im = Image.new('L', (1, 256))
px = im.load()
for y in range(256):
px[0, y] = y
self._roundtrip(im)
def test_large_count(self):
im = Image.new('L', (256, 1))
px = im.load()
for x in range(256):
px[x, 0] = x // 67 * 67
self._roundtrip(im)
def _test_overflow(self, im):
_last = ImageFile.MAXBLOCK
ImageFile.MAXBLOCK = 1024
try:
self._roundtrip(im)
finally:
ImageFile.MAXBLOCK = _last
def test_break_in_count_overflow(self):
im = Image.new('L', (256, 5))
px = im.load()
for y in range(4):
for x in range(256):
px[x, y] = x % 128
self._test_overflow(im)
def test_break_one_in_loop(self):
im = Image.new('L', (256, 5))
px = im.load()
for y in range(5):
for x in range(256):
px[x, y] = x % 128
self._test_overflow(im)
def test_break_many_in_loop(self):
im = Image.new('L', (256, 5))
px = im.load()
for y in range(4):
for x in range(256):
px[x, y] = x % 128
for x in range(8):
px[x, 4] = 16
self._test_overflow(im)
def test_break_one_at_end(self):
im = Image.new('L', (256, 5))
px = im.load()
for y in range(5):
for x in range(256):
px[x, y] = x % 128
px[0, 3] = 128 + 64
self._test_overflow(im)
def test_break_many_at_end(self):
im = Image.new('L', (256, 5))
px = im.load()
for y in range(5):
for x in range(256):
px[x, y] = x % 128
for x in range(4):
px[x * 2, 3] = 128 + 64
px[x + 256 - 4, 3] = 0
self._test_overflow(im)
def test_break_padding(self):
im = Image.new('L', (257, 5))
px = im.load()
for y in range(5):
for x in range(257):
px[x, y] = x % 128
for x in range(5):
px[x, 3] = 0
self._test_overflow(im)
if __name__ == '__main__':
unittest.main()

View File

@ -168,9 +168,6 @@ ImagingPcxEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
bytes -= 2;
}
}
if (bytes < padding) {
return ptr - buf;
}
/* add the padding */
for (i = 0; i < padding; i++) {
ptr[0] = 0;