From 318ff7d332ed47fbb25ec5f2c5dc805b7bce2765 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Sat, 19 Nov 2016 19:43:43 -0800 Subject: [PATCH] fixed support for hopper.ras, and other RGB sun raster files --- PIL/SunImagePlugin.py | 37 ++++++++++++++++++++++++++++++------- Tests/test_file_sun.py | 6 +++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/PIL/SunImagePlugin.py b/PIL/SunImagePlugin.py index af63144f2..07aa37eeb 100644 --- a/PIL/SunImagePlugin.py +++ b/PIL/SunImagePlugin.py @@ -48,17 +48,20 @@ class SunImageFile(ImageFile.ImageFile): self.size = i32(s[4:8]), i32(s[8:12]) depth = i32(s[12:16]) + file_type = i32(s[20:24]) + if depth == 1: self.mode, rawmode = "1", "1;I" elif depth == 8: self.mode = rawmode = "L" elif depth == 24: - self.mode, rawmode = "RGB", "BGR" + if file_type == 3: + self.mode, rawmode = "RGB", "RGB" + else: + self.mode, rawmode = "RGB", "BGR" else: raise SyntaxError("unsupported mode") - compression = i32(s[20:24]) - if i32(s[24:28]) != 0: length = i32(s[28:32]) offset = offset + length @@ -68,11 +71,31 @@ class SunImageFile(ImageFile.ImageFile): stride = (((self.size[0] * depth + 7) // 8) + 3) & (~3) - if compression == 1: - self.tile = [("raw", (0, 0)+self.size, offset, (rawmode, stride))] - elif compression == 2: - self.tile = [("sun_rle", (0, 0)+self.size, offset, rawmode)] + # file type: Type is the version (or flavor) of the bitmap + # file. The following values are typically found in the Type + # field: + # 0000h Old + # 0001h Standard + # 0002h Byte-encoded + # 0003h RGB format + # 0004h TIFF format + # 0005h IFF format + # FFFFh Experimental + # Old and standard are the same, except for the length tag. + # byte-encoded is run-length-encoded + # RGB looks similar to standard, but RGB byte order + # TIFF and IFF mean that they were converted from T/IFF + # Experimental means that it's something else. + # (http://www.fileformat.info/format/sunraster/egff.htm) + + if file_type in (0, 1, 3, 4, 5): + self.tile = [("raw", (0, 0)+self.size, offset, (rawmode, stride))] + elif file_type == 2: + self.tile = [("sun_rle", (0, 0)+self.size, offset, rawmode)] + else: + raise SyntaxError('Unsupported Sun Raster file type') + # # registry diff --git a/Tests/test_file_sun.py b/Tests/test_file_sun.py index 55afced16..d0ff87b84 100644 --- a/Tests/test_file_sun.py +++ b/Tests/test_file_sun.py @@ -1,4 +1,4 @@ -from helper import unittest, PillowTestCase +from helper import unittest, PillowTestCase, hopper from PIL import Image, SunImagePlugin @@ -16,12 +16,12 @@ class TestFileSun(PillowTestCase): # Assert self.assertEqual(im.size, (128, 128)) + self.assert_image_similar(im, hopper(), 5) # visually verified + invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, lambda: SunImagePlugin.SunImageFile(invalid_file)) - - def test_im1(self): im = Image.open('Tests/images/sunraster.im1') target = Image.open('Tests/images/sunraster.im1.png')