Pillow/Tests/test_file_pcx.py

128 lines
3.9 KiB
Python
Raw Normal View History

from PIL import Image, ImageFile, PcxImagePlugin
from .helper import PillowTestCase, assert_image_equal, hopper
2014-06-10 13:10:47 +04:00
class TestFilePcx(PillowTestCase):
def _roundtrip(self, im):
f = self.tempfile("temp.pcx")
im.save(f)
2019-11-25 23:03:23 +03:00
with Image.open(f) as im2:
self.assertEqual(im2.mode, im.mode)
self.assertEqual(im2.size, im.size)
self.assertEqual(im2.format, "PCX")
self.assertEqual(im2.get_format_mimetype(), "image/x-pcx")
assert_image_equal(im2, im)
2014-06-10 13:10:47 +04:00
def test_sanity(self):
2019-06-13 18:54:11 +03:00
for mode in ("1", "L", "P", "RGB"):
2014-09-05 13:36:24 +04:00
self._roundtrip(hopper(mode))
2014-06-10 13:10:47 +04:00
2017-03-01 12:20:18 +03:00
# Test an unsupported mode
f = self.tempfile("temp.pcx")
2017-09-01 14:05:40 +03:00
im = hopper("RGBA")
self.assertRaises(ValueError, im.save, f)
2017-03-01 12:20:18 +03:00
2015-07-03 08:03:25 +03:00
def test_invalid_file(self):
2015-07-03 09:22:56 +03:00
invalid_file = "Tests/images/flower.jpg"
2019-06-13 18:54:11 +03:00
self.assertRaises(SyntaxError, PcxImagePlugin.PcxImageFile, invalid_file)
2015-07-03 08:03:25 +03:00
2014-06-10 13:10:47 +04:00
def test_odd(self):
# see issue #523, odd sized images should have a stride that's even.
# not that imagemagick or gimp write pcx that way.
# we were not handling properly.
2019-06-13 18:54:11 +03:00
for mode in ("1", "L", "P", "RGB"):
2014-06-10 13:10:47 +04:00
# larger, odd sized images are better here to ensure that
# we handle interrupted scan lines properly.
2014-09-05 13:36:24 +04:00
self._roundtrip(hopper(mode).resize((511, 511)))
2014-06-10 13:10:47 +04:00
def test_pil184(self):
# Check reading of files where xmin/xmax is not zero.
2015-04-24 11:24:52 +03:00
test_file = "Tests/images/pil184.pcx"
2019-11-25 23:03:23 +03:00
with Image.open(test_file) as im:
self.assertEqual(im.size, (447, 144))
self.assertEqual(im.tile[0][1], (0, 0, 447, 144))
2014-06-10 13:10:47 +04:00
2019-11-25 23:03:23 +03:00
# Make sure all pixels are either 0 or 255.
self.assertEqual(im.histogram()[0] + im.histogram()[255], 447 * 144)
2014-06-10 13:10:47 +04:00
def test_1px_width(self):
2019-06-13 18:54:11 +03:00
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):
2019-06-13 18:54:11 +03:00
im = Image.new("L", (256, 1))
px = im.load()
for x in range(256):
px[x, 0] = x // 67 * 67
self._roundtrip(im)
2016-07-29 12:47:36 +03:00
def _test_buffer_overflow(self, im, size=1024):
_last = ImageFile.MAXBLOCK
2016-07-29 12:47:36 +03:00
ImageFile.MAXBLOCK = size
try:
self._roundtrip(im)
finally:
ImageFile.MAXBLOCK = _last
def test_break_in_count_overflow(self):
2019-06-13 18:54:11 +03:00
im = Image.new("L", (256, 5))
px = im.load()
for y in range(4):
for x in range(256):
px[x, y] = x % 128
2016-07-29 12:47:36 +03:00
self._test_buffer_overflow(im)
def test_break_one_in_loop(self):
2019-06-13 18:54:11 +03:00
im = Image.new("L", (256, 5))
px = im.load()
for y in range(5):
for x in range(256):
px[x, y] = x % 128
2016-07-29 12:47:36 +03:00
self._test_buffer_overflow(im)
def test_break_many_in_loop(self):
2019-06-13 18:54:11 +03:00
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
2016-07-29 12:47:36 +03:00
self._test_buffer_overflow(im)
def test_break_one_at_end(self):
2019-06-13 18:54:11 +03:00
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
2016-07-29 12:47:36 +03:00
self._test_buffer_overflow(im)
def test_break_many_at_end(self):
2019-06-13 18:54:11 +03:00
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
2016-07-29 12:47:36 +03:00
self._test_buffer_overflow(im)
def test_break_padding(self):
2019-06-13 18:54:11 +03:00
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
2016-07-29 12:47:36 +03:00
self._test_buffer_overflow(im)