Repository: python-pillow/Pillow

Issue:
Some TIFF G4 images are not well decoded
TIFF G4 with a FillOrder Tag (266) indicating a value of 2 (pixels are arranged within a byte such that pixels with lower column values are stored in the lower-order bits of the byte.) are not well decoded even if it seems that code to treat them is available.
Unfortunately I can't join such Tiff files because only PNG, GIF or JPG are accepted.



Discussion:
I'd like to see the image that fails. Can you either email it to me, put it in a new repo on github, or put it on dropbox?

I attached 2 Tiff G4 files that have a FillOrder equal to 2. 

----- Mail original -----

| I'd like to see the image that fails. Can you either email it to me,
| put it in a new repo on github, or put it on dropbox?
| —
| Reply to this email directly or view it on GitHub .

Looks like those got stripped by github, can you email me direct @ eric-github@soroos.net .

Ok, that's a reproduced bug here. 
![garbled](https://f.cloud.github.com/assets/19792/769589/f346261a-e8b8-11e2-8e38-093e6d2227df.png)
![not_garbled](https://f.cloud.github.com/assets/19792/769647/09c66e30-e8ba-11e2-958f-48e3ef2574fd.png)

I'll dig on it. 


Resolving patch:
diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py
--- a/PIL/TiffImagePlugin.py
+++ b/PIL/TiffImagePlugin.py
@@ -782,6 +782,23 @@ def _setup(self):
                     # it doesn't use a file descriptor.
                     fp = False
 
+                # libtiff handles the fillmode for us, so 1;IR should
+                # actually be 1;I. Including the R double reverses the
+                # bits, so stripes of the image are reversed.  See
+                # https://github.com/python-imaging/Pillow/issues/279
+                if fillorder == 2:
+                    key = (
+                        self.tag.prefix, photo, format, 1,
+                        self.tag.get(BITSPERSAMPLE, (1,)),
+                        self.tag.get(EXTRASAMPLES, ())
+                        )
+                    if Image.DEBUG:
+                        print("format key:", key)
+                    # this should always work, since all the
+                    # fillorder==2 modes have a corresponding
+                    # fillorder=1 mode
+                    self.mode, rawmode = OPEN_INFO[key]
+
                 # Offset in the tile tuple is 0, we go from 0,0 to
                 # w,h, and we only do this once -- eds
                 a = (rawmode, self._compression, fp )


Test patch:
diff --git a/Tests/images/g4-fillorder-test.png b/Tests/images/g4-fillorder-test.png
new file mode 100644
Binary files /dev/null and b/Tests/images/g4-fillorder-test.png differ
diff --git a/Tests/images/g4-fillorder-test.tif b/Tests/images/g4-fillorder-test.tif
new file mode 100644
Binary files /dev/null and b/Tests/images/g4-fillorder-test.tif differ
diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py
--- a/Tests/test_file_libtiff.py
+++ b/Tests/test_file_libtiff.py
@@ -70,6 +70,14 @@ def test_g4_eq_png():
 
     assert_image_equal(g4, png)
 
+# see https://github.com/python-imaging/Pillow/issues/279
+def test_g4_fillorder_eq_png():
+    """ Checking that we're actually getting the data that we expect"""
+    png = Image.open('Tests/images/g4-fillorder-test.png')
+    g4 = Image.open('Tests/images/g4-fillorder-test.tif')
+
+    assert_image_equal(g4, png)
+
 def test_g4_write():
     """Checking to see that the saved image is the same as what we wrote"""
     file = "Tests/images/lena_g4_500.tif"
@@ -95,3 +103,5 @@ def test_adobe_deflate_tiff():
     assert_equal(im.size, (278, 374))
     assert_equal(im.tile[0][:3], ('tiff_adobe_deflate', (0, 0, 278, 374), 0))
     assert_no_exception(lambda: im.load())
+
+