home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2166 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.1 KB  |  244 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '0.3'
  5. import Image
  6. import ImageFile
  7. import os
  8. import tempfile
  9. COMPRESSION = {
  10.     1: 'raw',
  11.     5: 'jpeg' }
  12. PAD = chr(0) * 4
  13.  
  14. def i16(c):
  15.     return ord(c[1]) + (ord(c[0]) << 8)
  16.  
  17.  
  18. def i32(c):
  19.     return ord(c[3]) + (ord(c[2]) << 8) + (ord(c[1]) << 16) + (ord(c[0]) << 24)
  20.  
  21.  
  22. def i(c):
  23.     return i32(PAD + c[-4:])
  24.  
  25.  
  26. def dump(c):
  27.     for i in c:
  28.         print '%02x' % ord(i),
  29.     
  30.     print 
  31.  
  32.  
  33. class IptcImageFile(ImageFile.ImageFile):
  34.     format = 'IPTC'
  35.     format_description = 'IPTC/NAA'
  36.     
  37.     def getint(self, key):
  38.         return i(self.info[key])
  39.  
  40.     
  41.     def field(self):
  42.         s = self.fp.read(5)
  43.         if not len(s):
  44.             return (None, 0)
  45.         tag = (ord(s[1]), ord(s[2]))
  46.         if ord(s[0]) != 28 and tag[0] < 1 or tag[0] > 9:
  47.             raise SyntaxError, 'invalid IPTC/NAA file'
  48.         tag[0] > 9
  49.         size = ord(s[3])
  50.         if size > 132:
  51.             raise IOError, 'illegal field length in IPTC/NAA file'
  52.         size > 132
  53.         if size == 128:
  54.             size = 0
  55.         elif size > 128:
  56.             size = i(self.fp.read(size - 128))
  57.         else:
  58.             size = i16(s[3:])
  59.         return (tag, size)
  60.  
  61.     
  62.     def _is_raw(self, offset, size):
  63.         return 0
  64.         self.fp.seek(offset)
  65.         (t, sz) = self.field()
  66.         if sz != size[0]:
  67.             return 0
  68.         y = 1
  69.         while None:
  70.             (t, s) = self.field()
  71.             if t != (8, 10):
  72.                 break
  73.             
  74.             if s != sz:
  75.                 return 0
  76.             y = y + 1
  77.             continue
  78.             return y == size[1]
  79.  
  80.     
  81.     def _open(self):
  82.         while None:
  83.             offset = self.fp.tell()
  84.             (tag, size) = self.field()
  85.             if not tag or tag == (8, 10):
  86.                 break
  87.             
  88.             if size:
  89.                 self.info[tag] = self.fp.read(size)
  90.                 continue
  91.             self.info[tag] = None
  92.             continue
  93.             layers = ord(self.info[(3, 60)][0])
  94.             component = ord(self.info[(3, 60)][1])
  95.             if self.info.has_key((3, 65)):
  96.                 id = ord(self.info[(3, 65)][0]) - 1
  97.             else:
  98.                 id = 0
  99.         if layers == 1 and not component:
  100.             self.mode = 'L'
  101.         elif layers == 3 and component:
  102.             self.mode = 'RGB'[id]
  103.         elif layers == 4 and component:
  104.             self.mode = 'CMYK'[id]
  105.         
  106.         self.size = (self.getint((3, 20)), self.getint((3, 30)))
  107.         
  108.         try:
  109.             compression = COMPRESSION[self.getint((3, 120))]
  110.         except KeyError:
  111.             raise IOError, 'Unknown IPTC image compression'
  112.  
  113.         if tag == (8, 10):
  114.             if compression == 'raw' and self._is_raw(offset, self.size):
  115.                 self.tile = [
  116.                     (compression, (offset, size + 5, -1), (0, 0, self.size[0], self.size[1]))]
  117.             else:
  118.                 self.tile = [
  119.                     ('iptc', (compression, offset), (0, 0, self.size[0], self.size[1]))]
  120.         
  121.  
  122.     
  123.     def load(self):
  124.         if len(self.tile) != 1 or self.tile[0][0] != 'iptc':
  125.             return ImageFile.ImageFile.load(self)
  126.         (type, tile, box) = self.tile[0]
  127.         (encoding, offset) = tile
  128.         self.fp.seek(offset)
  129.         outfile = tempfile.mktemp()
  130.         o = open(outfile, 'wb')
  131.         if encoding == 'raw':
  132.             o.write('P5\n%d %d\n255\n' % self.size)
  133.         
  134.         while None:
  135.             (type, size) = self.field()
  136.             if type != (8, 10):
  137.                 break
  138.             
  139.             while size > 0:
  140.                 s = self.fp.read(min(size, 8192))
  141.                 if not s:
  142.                     break
  143.                 
  144.                 o.write(s)
  145.                 size = size - len(s)
  146.             continue
  147.             
  148.             try:
  149.                 self.im = Image.core.open_ppm(outfile)
  150.             except:
  151.                 im = Image.open(outfile)
  152.                 im.load()
  153.                 self.im = im.im
  154.             finally:
  155.                 
  156.                 try:
  157.                     os.unlink(outfile)
  158.                 except:
  159.                     pass
  160.  
  161.  
  162.             return None
  163.  
  164.  
  165. Image.register_open('IPTC', IptcImageFile)
  166. Image.register_extension('IPTC', '.iim')
  167.  
  168. def getiptcinfo(im):
  169.     import TiffImagePlugin
  170.     import JpegImagePlugin
  171.     import StringIO
  172.     data = None
  173.     if isinstance(im, IptcImageFile):
  174.         return im.info
  175.     if isinstance(im, JpegImagePlugin.JpegImageFile):
  176.         
  177.         try:
  178.             app = im.app['APP13']
  179.             if app[:14] == 'Photoshop 3.0\x00':
  180.                 app = app[14:]
  181.                 offset = 0
  182.                 while app[offset:offset + 4] == '8BIM':
  183.                     offset = offset + 4
  184.                     code = JpegImagePlugin.i16(app, offset)
  185.                     offset = offset + 2
  186.                     name_len = ord(app[offset])
  187.                     name = app[offset + 1:offset + 1 + name_len]
  188.                     offset = 1 + offset + name_len
  189.                     if offset & 1:
  190.                         offset = offset + 1
  191.                     
  192.                     size = JpegImagePlugin.i32(app, offset)
  193.                     offset = offset + 4
  194.                     if code == 1028:
  195.                         data = app[offset:offset + size]
  196.                         break
  197.                     
  198.                     offset = offset + size
  199.                     if offset & 1:
  200.                         offset = offset + 1
  201.                         continue
  202.         except (AttributeError, KeyError):
  203.             isinstance(im, IptcImageFile)
  204.             isinstance(im, IptcImageFile)
  205.         except:
  206.             isinstance(im, IptcImageFile)<EXCEPTION MATCH>(AttributeError, KeyError)
  207.         
  208.  
  209.     isinstance(im, IptcImageFile)
  210.     if isinstance(im, TiffImagePlugin.TiffImageFile):
  211.         
  212.         try:
  213.             (type, data) = im.tag.tagdata[TiffImagePlugin.IPTC_NAA_CHUNK]
  214.         except (AttributeError, KeyError):
  215.             isinstance(im, IptcImageFile)
  216.             isinstance(im, IptcImageFile)
  217.         except:
  218.             isinstance(im, IptcImageFile)<EXCEPTION MATCH>(AttributeError, KeyError)
  219.         
  220.  
  221.     isinstance(im, IptcImageFile)
  222.     if data is None:
  223.         return None
  224.     
  225.     class FakeImage:
  226.         pass
  227.  
  228.     im = FakeImage()
  229.     im.__class__ = IptcImageFile
  230.     im.info = { }
  231.     im.fp = StringIO.StringIO(data)
  232.     
  233.     try:
  234.         im._open()
  235.     except (IndexError, KeyError):
  236.         data is None
  237.         data is None
  238.         isinstance(im, IptcImageFile)
  239.     except:
  240.         data is None
  241.  
  242.     return im.info
  243.  
  244.