Skip to content

Commit

Permalink
Merge pull request #2883 from uploadcare/tiff-wrong-bitspersample
Browse files Browse the repository at this point in the history
Fix count of BITSPERSAMPLE items in broken TIFF files
  • Loading branch information
wiredfool committed Dec 9, 2017
2 parents eccf9f6 + ce151bc commit 9ed4ea9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
21 changes: 16 additions & 5 deletions PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,12 +1186,23 @@ def _setup(self):
# for more exotic images.
sampleFormat = (1,)

bps_tuple = self.tag_v2.get(BITSPERSAMPLE, (1,))
extra_tuple = self.tag_v2.get(EXTRASAMPLES, ())
if photo in (2, 6, 8): # RGB, YCbCr, LAB
bps_count = 3
elif photo == 5: # CMYK
bps_count = 4
else:
bps_count = 1
bps_count += len(extra_tuple)
# Some files have only one value in bps_tuple,
# while should have more. Fix it
if bps_count > len(bps_tuple) and len(bps_tuple) == 1:
bps_tuple = bps_tuple * bps_count

# mode: check photometric interpretation and bits per pixel
key = (
self.tag_v2.prefix, photo, sampleFormat, fillorder,
self.tag_v2.get(BITSPERSAMPLE, (1,)),
self.tag_v2.get(EXTRASAMPLES, ())
)
key = (self.tag_v2.prefix, photo, sampleFormat, fillorder,
bps_tuple, extra_tuple)
if DEBUG:
print("format key:", key)
try:
Expand Down
Binary file added Tests/images/tiff_wrong_bits_per_sample.tiff
Binary file not shown.
8 changes: 8 additions & 0 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def test_16bit_RGBa_tiff(self):
self.assertEqual(im.tile, [('tiff_lzw', (0, 0, 100, 40), 50, 'RGBa;16B')])
im.load()

def test_wrong_bits_per_sample(self):
im = Image.open("Tests/images/tiff_wrong_bits_per_sample.tiff")

self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (52, 53))
self.assertEqual(im.tile, [('raw', (0, 0, 52, 53), 160, ('RGBA', 0, 1))])
im.load()

def test_gimp_tiff(self):
# Read TIFF JPEG images from GIMP [@PIL168]

Expand Down

0 comments on commit 9ed4ea9

Please sign in to comment.