home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / base / imagesize.py < prev    next >
Encoding:
Python Source  |  2006-08-30  |  5.7 KB  |  215 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2006 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20.  
  21. #
  22. # Ported from Perl's Image::Size module by Randy J. Ray
  23. #
  24.  
  25. import os, os.path, re, struct
  26.  
  27.  
  28. xbm_pat = re.compile(r'^\#define\s*\S*\s*(\d+)\s*\n\#define\s*\S*\s*(\d+)', re.IGNORECASE)
  29. xpm_pat = re.compile(r'"\s*(\d+)\s+(\d+)(\s+\d+\s+\d+){1,2}\s*"', re.IGNORECASE)
  30. ppm_pat1 = re.compile(r'^\#.*', re.IGNORECASE | re.MULTILINE)
  31. ppm_pat2 = re.compile(r'^(P[1-6])\s+(\d+)\s+(\d+)', re.IGNORECASE)
  32. ppm_pat3 = re.compile(r'IMGINFO:(\d+)x(\d+)', re.IGNORECASE)
  33. tiff_endian_pat = re.compile(r'II\x2a\x00')
  34.  
  35.  
  36.  
  37. def readin(stream, length, offset=0):
  38.     if offset != 0:
  39.         stream.seek(offset, 0)
  40.  
  41.     return stream.read(length)
  42.  
  43.  
  44. def xbmsize(stream):
  45.     width, height = -1, -1
  46.     match = xbm_pat.match(readin(stream,1024))
  47.  
  48.     try:
  49.         width = int(match.group(1))
  50.         height = int(match.group(2))
  51.     except:
  52.         pass
  53.  
  54.     return width, height
  55.  
  56.  
  57. def xpmsize(stream):
  58.     width, height = -1, -1
  59.     match = re.search(xpm_pat, readin(stream, 1024))
  60.     try:
  61.         width = int(match.group(1))
  62.         height = int(match.group(2))
  63.     except:
  64.         pass
  65.  
  66.     return width, height
  67.  
  68.  
  69. def pngsize(stream): # also does MNG
  70.     width, height = -1, -1
  71.  
  72.     if readin(stream, 4, 12) in ('IHDR', 'MHDR'):
  73.         height, width = struct.unpack("!II", stream.read(8))
  74.  
  75.     return width,height
  76.  
  77.  
  78. def jpegsize(stream):
  79.     width, height = -1, -1
  80.     stream.seek(2)
  81.     while True:
  82.         length = 4
  83.         buffer = readin(stream, length)
  84.         try:
  85.             marker, code, length = struct.unpack("!c c h", buffer)
  86.         except:
  87.             break
  88.  
  89.         if marker != '\xff':
  90.             break
  91.  
  92.         if 0xc0 <= ord(code) <= 0xc3:
  93.             length = 5
  94.             height, width = struct.unpack("!xhh", readin(stream, length))
  95.  
  96.         else:
  97.             readin(stream, length-2)
  98.  
  99.     return width, height
  100.  
  101.  
  102. def ppmsize(stream):
  103.     width, height = -1, -1
  104.     header = re.sub(ppm_pat1, '', readin(stream, 1024))
  105.     match = ppm_pat2.match(header)
  106.     typ = ''
  107.     try:
  108.         typ = match.group(1)
  109.         width = int(match.group(2))
  110.         height = int(match.group(3))
  111.     except:
  112.         pass
  113.  
  114.     if typ == 'P7':
  115.         match = ppm_pat3.match(header)
  116.  
  117.         try:
  118.             width = int(match.group(1))
  119.             height = int(match.group(2))
  120.         except:
  121.             pass
  122.  
  123.     return width, height
  124.  
  125.  
  126. def tiffsize(stream):
  127.     header = readin(stream, 4)
  128.     endian = ">"
  129.     match = tiff_endian_pat.match(header)
  130.  
  131.     if match is not None:
  132.         endian = "<"
  133.  
  134.     input = readin(stream, 4, 4)
  135.     offset = struct.unpack('%si' % endian, input)[0]
  136.     num_dirent = struct.unpack('%sH' % endian, readin(stream, 2, offset))[0]
  137.     offset += 2
  138.     num_dirent = offset+(num_dirent*12)
  139.     width, height = -1, -1
  140.  
  141.     while True:
  142.         ifd = readin(stream, 12, offset)
  143.  
  144.         if ifd == '' or offset > num_dirent:
  145.             break
  146.  
  147.         offset += 12
  148.         tag = struct.unpack('%sH'% endian, ifd[0:2])[0]
  149.         type = struct.unpack('%sH' % endian, ifd[2:4])[0]
  150.  
  151.         if tag == 0x0100:
  152.             width = struct.unpack("%si" % endian, ifd[8:12])[0] 
  153.  
  154.         elif tag == 0x0101:
  155.             height = struct.unpack("%si" % endian, ifd[8:12])[0] 
  156.  
  157.     return width, height
  158.  
  159.  
  160. def bmpsize(stream):
  161.     width, height = struct.unpack("<II", readin(stream, 8, 18))
  162.     return width, height
  163.  
  164.  
  165. def gifsize(stream):
  166.     # since we only care about the printed size of the image
  167.     # we only need to get the logical screen sizes, which are
  168.     # the maximum extents of the image. This code is much simpler
  169.     # than the code from Image::Size
  170.     #width, height = -1, -1
  171.     buf = readin(stream, 7, 6) # LSx, GCTF, etc 
  172.     height, width, flags, bci, par = struct.unpack('<HHBBB', buf)
  173.  
  174.     return width, height
  175.  
  176.  
  177.  
  178.  
  179. TYPE_MAP = {re.compile('^GIF8[7,9]a')              : ('image/gif', gifsize),
  180.              re.compile("^\xFF\xD8")                : ('image/jpeg', jpegsize),
  181.              re.compile("^\x89PNG\x0d\x0a\x1a\x0a") : ('image/png', pngsize),
  182.              re.compile("^P[1-7]")                  : ('image/x-portable-pixmap', ppmsize),
  183.              re.compile('\#define\s+\S+\s+\d+')     : ('image/x-xbitmap', xbmsize),
  184.              re.compile('\/\* XPM \*\/')            : ('image/x-xpixmap', xpmsize),
  185.              re.compile('^MM\x00\x2a')              : ('image/tiff', tiffsize),
  186.              re.compile('^II\*\x00')                : ('image/tiff', tiffsize),
  187.              re.compile('^BM')                      : ('image/x-bitmap', bmpsize),
  188.              re.compile("^\x8aMNG\x0d\x0a\x1a\x0a") : ('image/png', pngsize),
  189.            }
  190.  
  191.  
  192. def imagesize(filename, mime_type=''):
  193.     width, height = -1, -1
  194.  
  195.     f = file(filename, 'r')
  196.     buffer = f.read(4096)
  197.  
  198.     if not mime_type:
  199.         for t in TYPE_MAP:
  200.             match = t.search(buffer)
  201.             if match is not None:
  202.                 mime_type, func = TYPE_MAP[t]
  203.                 break
  204.  
  205.     if mime_type and func:
  206.         f.seek(0)
  207.         width, height = func(f)
  208.     else:
  209.         width, height = -1, -1
  210.  
  211.     f.close()
  212.  
  213.     return height, width, mime_type
  214.     
  215.