If we can't read a file due to unsupported compression, raise an error

This commit is contained in:
hugovk 2014-07-20 01:50:05 +03:00
parent bc34bda2b8
commit fef3ceb2c0
2 changed files with 8 additions and 20 deletions

View File

@ -45,7 +45,7 @@ class SgiImageFile(ImageFile.ImageFile):
# HEAD # HEAD
s = self.fp.read(512) s = self.fp.read(512)
if i16(s) != 474: if i16(s) != 474:
raise SyntaxError("not an SGI image file") raise ValueError("Not an SGI image file")
# relevant header entries # relevant header entries
compression = i8(s[2]) compression = i8(s[2])
@ -61,7 +61,7 @@ class SgiImageFile(ImageFile.ImageFile):
elif layout == (1, 3, 4): elif layout == (1, 3, 4):
self.mode = "RGBA" self.mode = "RGBA"
else: else:
raise SyntaxError("unsupported SGI image mode") raise ValueError("Unsupported SGI image mode")
# size # size
self.size = i16(s[6:]), i16(s[8:]) self.size = i16(s[6:]), i16(s[8:])
@ -76,8 +76,7 @@ class SgiImageFile(ImageFile.ImageFile):
("raw", (0, 0)+self.size, offset, (layer, 0, -1))) ("raw", (0, 0)+self.size, offset, (layer, 0, -1)))
offset = offset + pagesize offset = offset + pagesize
elif compression == 1: elif compression == 1:
self.tile = [ raise ValueError("SGI RLE encoding not supported")
("sgi_rle", (0, 0)+self.size, 512, (self.mode, 0, -1))]
# #
# registry # registry
@ -87,5 +86,6 @@ Image.register_open("SGI", SgiImageFile, _accept)
Image.register_extension("SGI", ".bw") Image.register_extension("SGI", ".bw")
Image.register_extension("SGI", ".rgb") Image.register_extension("SGI", ".rgb")
Image.register_extension("SGI", ".rgba") Image.register_extension("SGI", ".rgba")
Image.register_extension("SGI", ".sgi") Image.register_extension("SGI", ".sgi")
# End of file

View File

@ -5,44 +5,32 @@ from PIL import Image
class TestFileSgi(PillowTestCase): class TestFileSgi(PillowTestCase):
def sanity(self, filename, expected_mode, expected_size=(128, 128)):
# Act
im = Image.open(filename)
# Assert
self.assertEqual(im.mode, expected_mode)
self.assertEqual(im.size, expected_size)
def test_rgb(self): def test_rgb(self):
# Arrange # Arrange
# Created with ImageMagick then renamed: # Created with ImageMagick then renamed:
# convert lena.ppm lena.sgi # convert lena.ppm lena.sgi
test_file = "Tests/images/lena.rgb" test_file = "Tests/images/lena.rgb"
expected_mode = "RGB"
# Act / Assert # Act / Assert
self.sanity(test_file, expected_mode) self.assertRaises(ValueError, lambda: Image.open(test_file))
def test_l(self): def test_l(self):
# Arrange # Arrange
# Created with ImageMagick then renamed: # Created with ImageMagick then renamed:
# convert lena.ppm -monochrome lena.sgi # convert lena.ppm -monochrome lena.sgi
test_file = "Tests/images/lena.bw" test_file = "Tests/images/lena.bw"
expected_mode = "L"
# Act / Assert # Act / Assert
self.sanity(test_file, expected_mode) self.assertRaises(ValueError, lambda: Image.open(test_file))
def test_rgba(self): def test_rgba(self):
# Arrange # Arrange
# Created with ImageMagick: # Created with ImageMagick:
# convert transparent.png transparent.sgi # convert transparent.png transparent.sgi
test_file = "Tests/images/transparent.sgi" test_file = "Tests/images/transparent.sgi"
expected_mode = "RGBA"
expected_size = (200, 150)
# Act / Assert # Act / Assert
self.sanity(test_file, expected_mode, expected_size) self.assertRaises(ValueError, lambda: Image.open(test_file))
if __name__ == '__main__': if __name__ == '__main__':