home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / base / imagesize.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  5.3 KB  |  178 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import os
  5. import os.path as os
  6. import re
  7. import struct
  8. xbm_pat = re.compile('^\\#define\\s*\\S*\\s*(\\d+)\\s*\\n\\#define\\s*\\S*\\s*(\\d+)', re.IGNORECASE)
  9. xpm_pat = re.compile('"\\s*(\\d+)\\s+(\\d+)(\\s+\\d+\\s+\\d+){1,2}\\s*"', re.IGNORECASE)
  10. ppm_pat1 = re.compile('^\\#.*', re.IGNORECASE | re.MULTILINE)
  11. ppm_pat2 = re.compile('^(P[1-6])\\s+(\\d+)\\s+(\\d+)', re.IGNORECASE)
  12. ppm_pat3 = re.compile('IMGINFO:(\\d+)x(\\d+)', re.IGNORECASE)
  13. tiff_endian_pat = re.compile('II\\x2a\\x00')
  14.  
  15. def readin(stream, length, offset = 0):
  16.     if offset != 0:
  17.         stream.seek(offset, 0)
  18.     
  19.     return stream.read(length)
  20.  
  21.  
  22. def xbmsize(stream):
  23.     (width, height) = (-1, -1)
  24.     match = xbm_pat.match(readin(stream, 1024))
  25.     
  26.     try:
  27.         width = int(match.group(1))
  28.         height = int(match.group(2))
  29.     except:
  30.         pass
  31.  
  32.     return (width, height)
  33.  
  34.  
  35. def xpmsize(stream):
  36.     (width, height) = (-1, -1)
  37.     match = re.search(xpm_pat, readin(stream, 1024))
  38.     
  39.     try:
  40.         width = int(match.group(1))
  41.         height = int(match.group(2))
  42.     except:
  43.         pass
  44.  
  45.     return (width, height)
  46.  
  47.  
  48. def pngsize(stream):
  49.     (width, height) = (-1, -1)
  50.     if readin(stream, 4, 12) in ('IHDR', 'MHDR'):
  51.         (height, width) = struct.unpack('!II', stream.read(8))
  52.     
  53.     return (width, height)
  54.  
  55.  
  56. def jpegsize(stream):
  57.     (width, height) = (-1, -1)
  58.     stream.seek(2)
  59.     while True:
  60.         length = 4
  61.         buffer = readin(stream, length)
  62.         
  63.         try:
  64.             (marker, code, length) = struct.unpack('!c c h', buffer)
  65.         except:
  66.             break
  67.  
  68.         if marker != '\xff':
  69.             break
  70.         
  71.         if ord(code) <= ord(code):
  72.             pass
  73.         elif ord(code) <= 195:
  74.             length = 5
  75.             (height, width) = struct.unpack('!xhh', readin(stream, length))
  76.             continue
  77.         readin(stream, length - 2)
  78.     return (width, height)
  79.  
  80.  
  81. def ppmsize(stream):
  82.     (width, height) = (-1, -1)
  83.     header = re.sub(ppm_pat1, '', readin(stream, 1024))
  84.     match = ppm_pat2.match(header)
  85.     typ = ''
  86.     
  87.     try:
  88.         typ = match.group(1)
  89.         width = int(match.group(2))
  90.         height = int(match.group(3))
  91.     except:
  92.         pass
  93.  
  94.     if typ == 'P7':
  95.         match = ppm_pat3.match(header)
  96.         
  97.         try:
  98.             width = int(match.group(1))
  99.             height = int(match.group(2))
  100.  
  101.     
  102.     return (width, height)
  103.  
  104.  
  105. def tiffsize(stream):
  106.     header = readin(stream, 4)
  107.     endian = '>'
  108.     match = tiff_endian_pat.match(header)
  109.     if match is not None:
  110.         endian = '<'
  111.     
  112.     input = readin(stream, 4, 4)
  113.     offset = struct.unpack('%si' % endian, input)[0]
  114.     num_dirent = struct.unpack('%sH' % endian, readin(stream, 2, offset))[0]
  115.     offset += 2
  116.     num_dirent = offset + num_dirent * 12
  117.     (width, height) = (-1, -1)
  118.     while True:
  119.         ifd = readin(stream, 12, offset)
  120.         if ifd == '' or offset > num_dirent:
  121.             break
  122.         
  123.         offset += 12
  124.         tag = struct.unpack('%sH' % endian, ifd[0:2])[0]
  125.         type = struct.unpack('%sH' % endian, ifd[2:4])[0]
  126.         if tag == 256:
  127.             width = struct.unpack('%si' % endian, ifd[8:12])[0]
  128.             continue
  129.         if tag == 257:
  130.             height = struct.unpack('%si' % endian, ifd[8:12])[0]
  131.             continue
  132.     return (width, height)
  133.  
  134.  
  135. def bmpsize(stream):
  136.     (width, height) = struct.unpack('<II', readin(stream, 8, 18))
  137.     return (width, height)
  138.  
  139.  
  140. def gifsize(stream):
  141.     buf = readin(stream, 7, 6)
  142.     (height, width, flags, bci, par) = struct.unpack('<HHBBB', buf)
  143.     return (width, height)
  144.  
  145. TYPE_MAP = {
  146.     re.compile('^GIF8[7,9]a'): ('image/gif', gifsize),
  147.     re.compile('^\xff\xd8'): ('image/jpeg', jpegsize),
  148.     re.compile('^\x89PNG\r\n\x1a\n'): ('image/png', pngsize),
  149.     re.compile('^P[1-7]'): ('image/x-portable-pixmap', ppmsize),
  150.     re.compile('\\#define\\s+\\S+\\s+\\d+'): ('image/x-xbitmap', xbmsize),
  151.     re.compile('\\/\\* XPM \\*\\/'): ('image/x-xpixmap', xpmsize),
  152.     re.compile('^MM\x00*'): ('image/tiff', tiffsize),
  153.     re.compile('^II\\*\x00'): ('image/tiff', tiffsize),
  154.     re.compile('^BM'): ('image/x-bitmap', bmpsize),
  155.     re.compile('^\x8aMNG\r\n\x1a\n'): ('image/png', pngsize) }
  156.  
  157. def imagesize(filename, mime_type = ''):
  158.     (width, height) = (-1, -1)
  159.     f = file(filename, 'r')
  160.     buffer = f.read(4096)
  161.     if not mime_type:
  162.         for t in TYPE_MAP:
  163.             match = t.search(buffer)
  164.             if match is not None:
  165.                 (mime_type, func) = TYPE_MAP[t]
  166.                 break
  167.                 continue
  168.         
  169.     
  170.     if mime_type and func:
  171.         f.seek(0)
  172.         (width, height) = func(f)
  173.     else:
  174.         (width, height) = (-1, -1)
  175.     f.close()
  176.     return (height, width, mime_type)
  177.  
  178.