Commits

Christian Tismer committed 9736584

finished support for multi-strip tiff! (started at EuroPython with no success)

Comments (0)

Files changed (3)

tiffany/from-pil-py3/TiffImagePlugin.py

                 if typ == 5:
                     count = count // 2        # adjust for rational data field
                 append((tag, typ, count, o32(offset), data))
+                if (tag == STRIPOFFSETS) and data:
+                    strip_tableofs = offset
                 offset = offset + len(data)
                 if offset & 1:
                     offset = offset + 1 # word padding
         # update strip offset data to point beyond auxiliary data
         if stripoffsets is not None:
             tag, typ, count, value, data = directory[stripoffsets]
-            assert not data, "multistrip support not yet implemented"
-            value = o32(self.i32(value) + offset)
+            if data:
+                value = o32(strip_tableofs)
+            else:
+                value = o32(self.i32(value) + offset)
             directory[stripoffsets] = tag, typ, count, value, data
 
         # pass 2: write directory to file
             if len(data) & 1:
                 fp.write(b"\0")
 
+        # pass 4: write strip offset data for multi-strip
+        if stripoffsets is not None:
+            tag, typ, count, value, data = directory[stripoffsets]
+            if data:
+                stripstart = fp.tell()
+                lis = list(self.i32(data[i:i+4]) for i in range(0, count*4, 4))
+                data = ''.join(self.o32(it + stripstart) for it in lis)
+                fp.seek(strip_tableofs)
+                fp.write(data)
+                fp.seek(stripstart)
+            directory[stripoffsets] = tag, typ, count, value, data
+
         return offset
 
 ##

tiffany/monkeypil.py

             elif tag == STRIPOFFSETS:
                 # don't carry over the old offsets
                 stripoffsets = value
-                value = (0,)
+                # re-adjust the offsets to zero
+                minvalue = min(value)
+                value = tuple(v - minvalue for v in value) 
             elif tag == STRIPBYTECOUNTS:
                 stripbytecounts = value
             elif tag == 293:  # T6Options
     return Tiffany(im)
 
 
-def inline_test():
+def inline_test(fname, altpage):
 
     # reading from a file
-    imfile = os.path.join(rootpath, 'test/data/recipe_1.tiff')
-    outfile = os.path.join(os.path.dirname(imfile), 'look_page2.tiff')
+    imfile = os.path.join(rootpath, 'test/data', fname)
+    if not os.path.exists(imfile):
+        print('warning: %s not found' % fname)
+        return
+    outfile = os.path.join(os.path.dirname(imfile), 'look_' + fname)
     im = open(imfile)
 
     with io.open(outfile, 'wb') as imf:
-        for i in range(5):
-            im.seek(1)
+        for i in range(2):
+            im.seek(altpage)
             im.save(imf)
             im.seek(0)
             im.save(imf)
     im = open(im)
 
     with io.open(outfile, 'wb') as imf:
-        for i in range(5):
-            im.seek(1)
+        for i in range(2):
+            im.seek(altpage)
             im.save(imf)
             im.seek(0)
             im.save(imf)
 
     # writing to a buffer
     imf = io.BytesIO()
-    for i in range(5):
-        im.seek(1)
+    for i in range(2):
+        im.seek(altpage)
         im.save(imf)
         im.seek(0)
         im.save(imf)
 
 if __name__ == '__main__':
-    inline_test()
+    inline_test('lzw_pon.tiff', 0)
+    inline_test('recipe_1.tiff', 1)

tiffany/test/data/lzw_pon.tiff

Added
New image