root / pkpgcounter / trunk / pdlanalyzer / tiff.py @ 217

Revision 217, 2.8 kB (checked in by jerome, 19 years ago)

Added basic support for the TIFF format (which can handle
multiple pages in the same file)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
6# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21# $Id$
22#
23
24import sys
25import os
26import mmap
27from struct import unpack
28
29from pdlanalyzer import pdlparser
30
31class TIFFParser(pdlparser.PDLParser) :
32    """A parser for TIFF documents."""
33    def getJobSize(self) :
34        """Counts pages in a TIFF document.
35        """
36        infileno = self.infile.fileno()
37        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
38        pagecount = 0
39        littleendian = (chr(0x49)*2) + chr(0x2a) + chr(0)
40        bigendian = (chr(0x4d)*2) + chr(0) + chr(0x2a)
41        if minfile[:4] == littleendian :
42            integerbyteorder = "<I"
43            shortbyteorder = "<H"
44        else :   
45            integerbyteorder = ">I"
46            shortbyteorder = ">H"
47        pos = 4   
48        try :   
49            nextifdoffset = unpack(integerbyteorder, minfile[pos:pos+4])[0]
50            while nextifdoffset :
51                direntrycount = unpack(shortbyteorder, minfile[nextifdoffset:nextifdoffset+2])[0]
52                pos = nextifdoffset + 2 + (direntrycount * 12)
53                nextifdoffset = unpack(integerbyteorder, minfile[pos:pos+4])[0]
54                pagecount += 1
55        except IndexError :           
56            pass
57        minfile.close()
58        return pagecount
59       
60def test() :       
61    """Test function."""
62    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
63        sys.argv.append("-")
64    totalsize = 0   
65    for arg in sys.argv[1:] :
66        if arg == "-" :
67            infile = sys.stdin
68            mustclose = 0
69        else :   
70            infile = open(arg, "rb")
71            mustclose = 1
72        try :
73            parser = TIFFParser(infile, debug=1)
74            totalsize += parser.getJobSize()
75        except pdlparser.PDLParserError, msg :   
76            sys.stderr.write("ERROR: %s\n" % msg)
77            sys.stderr.flush()
78        if mustclose :   
79            infile.close()
80    print "%s" % totalsize
81   
82if __name__ == "__main__" :   
83    test()
Note: See TracBrowser for help on using the browser.