Commits

Christian Tismer  committed ffcfbd3

PIL hack is enforced. No more temp files. Importer is activated. Basic painting works. I'm tired .-)

  • Participants
  • Parent commits c7727ae

Comments (0)

Files changed (3)

File playground/piltest.py

-import sys, os
-from ddc import rootpath
-
-"""
-Fake Imaging without PIL decoder
---------------------------------
-
-based upon information from
-http://www.awaresystems.be/imaging/tiff/faq.html
-
-This little hack finally enables the usage of PIL for the DDC
-project without compiling CCITT G4 support at all.
-
-The idea is simply to avoid any recoding of the tiff file.
-Instead, the position of the first IFD is changed to another
-one (the second by default). No other changes are made to the tiff
-data. Fortunately Qt is reading the IFP data correctly.
-
-"""
-
-def check_pil_fake():
-    try:
-        import Image
-    except ImportError:
-        if warning:
-            warn('PIL is not installed')
-        return False
-
-    imfile = os.path.join(rootpath, 'ddc/config/data/demo-g4-2pages.tiff')
-    im = Image.open(imfile)
-    pos = im._TiffImageFile__first
-    next = im._TiffImageFile__next
-    fp = im._TiffImageFile__fp
-    fp.seek(0)
-    data = fp.read()
-    ifd = next#-4
-    import struct
-    ifdbytes = struct.pack('I', ifd)
-    data = data[:4] + ifdbytes + data[8:]
-    file('look_page2.tiff', 'wb').write(data)
-    
-
-if __name__ == '__main__':
-    check_pil_fake()

File tool/tiff_reader/__init__.py

 import os
 __path__[0] = os.path.realpath(__path__[0])
 __file__ = os.path.realpath(__file__)
-__path__.append(os.path.join(__path__[0], 'from-pil'))
+__path__.append(os.path.join(__path__[0], 'from-pil'))
+
+from .implementation import read_tiff_page

File tool/tiff_reader/implementation.py

+"""
+Fake Imaging without PIL decoder
+--------------------------------
+
+based upon information from
+http://www.awaresystems.be/imaging/tiff/faq.html
+
+This little hack finally enables the usage of PIL for the DDC
+project without compiling CCITT G4 support at all.
+
+The idea is simply to avoid any recoding of the tiff file.
+Instead, the position of the first IFD is changed to another
+one (the second by default). No other changes are made to the tiff
+data. Fortunately Qt is reading the IFP data correctly.
+
+"""
+
+from StringIO import StringIO as sio
+from ddc.tool.tiff_reader import Image
+from ddc.tool.tiff_reader.TiffImagePlugin import ImageFileDirectory
+import struct
+
+# it is a problem to derive a class here. Why?
+
+def read_tiff_page(filename, file_or_buffer=None, pageno=1):
+    f = file_or_buffer
+    if f and not hasattr(f, 'seek'):
+        f = sio(f)
+    else:
+        f = file(filename, 'rb')
+    im = Image.open(f)
+    pos = im._TiffImageFile__first
+    fp = im._TiffImageFile__fp
+    fp.seek(0)
+    data = fp.read()
+    if pageno > 0:
+        o32 = ImageFileDirectory(data[:2]).o32
+        try:
+            im.seek(pageno-1)
+        except:
+            import warnings
+            warnings.warn('could nor read ' + filename)
+            return filename, None
+        next = im._TiffImageFile__next
+        ifd = next
+        ifdbytes = o32(ifd)
+        data = data[:4] + ifdbytes + data[8:]
+    return filename, data