Repository: python-pillow/Pillow

Issue:
TIF old style jpeg compression
### What did you do?
I am trying to open and save a tif file. The problem with the file is that it uses old jpeg compression. You can replicate the issue with the one provided at https://github.com/openpreserve/format-corpus/tree/master/tiff-examples/old-style-jpeg-compression.

### What did you expect to happen?
Showing and saving the tif correctly.

### What actually happened?
With libtiff < 4.0.9 Pillow completely freezes and never returns anything or does not throw any exception. With libtiff 4.09 I receive the error ```OSError: -2``` and ```<PIL.TiffImagePlugin.TiffImageFile image mode=YCbCr size=4160x870 at 0x7FCF45E6C7F0>```.

### What versions of Pillow and Python are you using?

Python 3.6
Pillow 5.1.0
Libtiff 4.0.9

```python
from PIL import Image
img = Image.open('old-style-jpeg-compression.tif')
img
```

Identifying it in linux gives me 

```
identify: Unknown field with tag 33000 (0x80e8) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/881.
identify: Depreciated and troublesome old-style JPEG compression mode, please convert to new-style JPEG compression and notify vendor of writing software. `OJPEGSetupDecode' @ warning/tiff.c/TIFFWarnings/881.
```

Converting it with imagemagick gives me the same warnings, but successfully converts it to e.g., PNG which I can also read.



Discussion:

Resolving patch:
diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py
--- a/src/PIL/TiffImagePlugin.py
+++ b/src/PIL/TiffImagePlugin.py
@@ -263,10 +263,10 @@
     (II, 5, (1,), 1, (8, 8, 8, 8, 8, 8), (0, 0)): ("CMYK", "CMYKXX"),
     (MM, 5, (1,), 1, (8, 8, 8, 8, 8, 8), (0, 0)): ("CMYK", "CMYKXX"),
 
-    # JPEG compressed images handled by LibTiff and auto-converted to RGB
+    # JPEG compressed images handled by LibTiff and auto-converted to RGBX
     # Minimal Baseline TIFF requires YCbCr images to have 3 SamplesPerPixel
-    (II, 6, (1,), 1, (8, 8, 8), ()): ("RGB", "RGB"),
-    (MM, 6, (1,), 1, (8, 8, 8), ()): ("RGB", "RGB"),
+    (II, 6, (1,), 1, (8, 8, 8), ()): ("RGB", "RGBX"),
+    (MM, 6, (1,), 1, (8, 8, 8), ()): ("RGB", "RGBX"),
 
     (II, 8, (1,), 1, (8, 8, 8), ()): ("LAB", "LAB"),
     (MM, 8, (1,), 1, (8, 8, 8), ()): ("LAB", "LAB"),
@@ -1191,6 +1191,10 @@ def _setup(self):
         # the specification
         photo = self.tag_v2.get(PHOTOMETRIC_INTERPRETATION, 0)
 
+        # old style jpeg compression images most certainly are YCbCr
+        if self._compression == "tiff_jpeg":
+            photo = 6
+
         fillorder = self.tag_v2.get(FILLORDER, 1)
 
         if DEBUG:


Test patch:
diff --git a/Tests/images/old-style-jpeg-compression.png b/Tests/images/old-style-jpeg-compression.png
new file mode 100644
Binary files /dev/null and b/Tests/images/old-style-jpeg-compression.png differ
diff --git a/Tests/images/old-style-jpeg-compression.tif b/Tests/images/old-style-jpeg-compression.tif
new file mode 100644
Binary files /dev/null and b/Tests/images/old-style-jpeg-compression.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
@@ -716,3 +716,10 @@ def test_tiled_ycbcr_jpeg_2x2_sampling(self):
         im = Image.open(infile)
 
         self.assert_image_similar_tofile(im, "Tests/images/flower.jpg", 0.5)
+
+    def test_old_style_jpeg(self):
+        infile = "Tests/images/old-style-jpeg-compression.tif"
+        im = Image.open(infile)
+
+        self.assert_image_equal_tofile(im,
+                                       "Tests/images/old-style-jpeg-compression.png")