From 36d4f5a9b4380ca46b5820d850b0ade17c6fab33 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Fri, 1 Jan 2016 05:30:40 -0800 Subject: [PATCH] Tests for permitted libtiff metadata --- PIL/TiffTags.py | 15 ++++++++++- Tests/test_file_libtiff.py | 55 +++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/PIL/TiffTags.py b/PIL/TiffTags.py index 4e47871d8..6bb2409f3 100644 --- a/PIL/TiffTags.py +++ b/PIL/TiffTags.py @@ -386,4 +386,17 @@ LIBTIFF_CORE = set ([255, 256, 257, 258, 259, 262, 263, 266, 274, 277, 269 # this has been in our tests forever, and works ]) -LIBTIFF_CORE.remove(320) # Arrays error? +LIBTIFF_CORE.remove(320) # Array of short, crashes +LIBTIFF_CORE.remove(301) # Array of short, crashes +LIBTIFF_CORE.remove(532) # Array of long, crashes + +LIBTIFF_CORE.remove(255) # We don't have support for subfiletypes +LIBTIFF_CORE.remove(322) # We don't have support for tiled images in libtiff +LIBTIFF_CORE.remove(323) # Tiled images +LIBTIFF_CORE.remove(333) # Ink Names either + +# Note to advanced users: There may be combinations of these +# parameters and values that when added properly, will work and +# produce valid tiff images that may work in your application. +# It is safe to add and remove tags from this set from Pillow's point +# of view so long as you test against libtiff. diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 8d7ef072f..50e6dcc23 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -7,7 +7,7 @@ import logging import itertools import os -from PIL import Image, TiffImagePlugin +from PIL import Image, TiffImagePlugin, TiffTags logger = logging.getLogger(__name__) @@ -172,6 +172,59 @@ class TestFileLibTiff(LibTiffTestCase): for field in requested_fields: self.assertTrue(field in reloaded, "%s not in metadata" %field) + def test_additional_metadata(self): + # these should not crash. Seriously dummy data, most of it doesn't make + # any sense, so we're running up against limits where we're asking + # libtiff to do stupid things. + + # Get the list of the ones that we should be able to write + + core_items = dict((tag, info) for tag, info in [(s,TiffTags.lookup(s)) for s + in TiffTags.LIBTIFF_CORE] + if info.type is not None) + + # Exclude ones that have special meaning that we're already testing them + im = Image.open('Tests/images/hopper_g4.tif') + for tag in im.tag_v2.keys(): + try: + del(core_items[tag]) + except: pass + + # Type codes: + # 2: "ascii", + # 3: "short", + # 4: "long", + # 5: "rational", + # 12: "double", + # type: dummy value + values = { 2: 'test', + 3: 1, + 4: 2**20, + 5: TiffImagePlugin.IFDRational(100,1), + 12: 1.05 } + + + new_ifd = TiffImagePlugin.ImageFileDirectory_v2() + for tag, info in core_items.items(): + if info.length == 1: + new_ifd[tag] = values[info.type] + if info.length == 0: + new_ifd[tag] = tuple(values[info.type] for _ in range(3)) + else: + new_ifd[tag] = tuple(values[info.type] for _ in range(info.length)) + + # Extra samples really doesn't make sense in this application. + del(new_ifd[338]) + + out = self.tempfile("temp.tif") + TiffImagePlugin.WRITE_LIBTIFF = True + + im.save(out, tiffinfo=new_ifd) + + TiffImagePlugin.WRITE_LIBTIFF = False + + + def test_g3_compression(self): i = Image.open('Tests/images/hopper_g4_500.tif') out = self.tempfile("temp.tif")