root / pkpgcounter / trunk / pkpgpdls / tiff.py @ 372

Revision 372, 3.6 kB (checked in by jerome, 18 years ago)

Now uses the standard mechanism for the TIFF conversion, even if none has to occur for this particular file format.

  • 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, 2006 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
24"""This modules implements a page counter for TIFF documents."""
25
26import sys
27import os
28import mmap
29from struct import unpack
30
31import pdlparser
32
33class Parser(pdlparser.PDLParser) :
34    """A parser for TIFF documents."""
35    totiffcommand = "cat >%(fname)s"
36    def isValid(self) :       
37        """Returns 1 if data is TIFF, else 0."""
38        littleendian = (chr(0x49)*2) + chr(0x2a) + chr(0)
39        bigendian = (chr(0x4d)*2) + chr(0) + chr(0x2a)
40        if self.firstblock[:4] in (littleendian, bigendian) :
41            self.logdebug("DEBUG: Input file is in the TIFF format.")
42            return 1
43        else :   
44            return 0
45   
46    def getJobSize(self) :
47        """Counts pages in a TIFF document.
48       
49           Algorithm by Jerome Alet.
50           
51           The documentation used for this was :
52           
53           http://www.ee.cooper.edu/courses/course_pages/past_courses/EE458/TIFF/
54        """
55        infileno = self.infile.fileno()
56        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
57        pagecount = 0
58        littleendian = (chr(0x49)*2) + chr(0x2a) + chr(0)
59        bigendian = (chr(0x4d)*2) + chr(0) + chr(0x2a)
60        if minfile[:4] == littleendian :
61            integerbyteorder = "<I"
62            shortbyteorder = "<H"
63        elif minfile[:4] == bigendian :
64            integerbyteorder = ">I"
65            shortbyteorder = ">H"
66        else :   
67            raise pdlparser.PDLParserError, "Unknown file endianness."
68        pos = 4   
69        try :   
70            nextifdoffset = unpack(integerbyteorder, minfile[pos : pos + 4])[0]
71            while nextifdoffset :
72                direntrycount = unpack(shortbyteorder, minfile[nextifdoffset : nextifdoffset + 2])[0]
73                pos = nextifdoffset + 2 + (direntrycount * 12)
74                nextifdoffset = unpack(integerbyteorder, minfile[pos : pos + 4])[0]
75                pagecount += 1
76        except IndexError :           
77            pass
78        minfile.close()
79        return pagecount
80       
81def test() :       
82    """Test function."""
83    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
84        sys.argv.append("-")
85    totalsize = 0   
86    for arg in sys.argv[1:] :
87        if arg == "-" :
88            infile = sys.stdin
89            mustclose = 0
90        else :   
91            infile = open(arg, "rb")
92            mustclose = 1
93        try :
94            parser = Parser(infile, debug=1)
95            totalsize += parser.getJobSize()
96        except pdlparser.PDLParserError, msg :   
97            sys.stderr.write("ERROR: %s\n" % msg)
98            sys.stderr.flush()
99        if mustclose :   
100            infile.close()
101    print "%s" % totalsize
102   
103if __name__ == "__main__" :   
104    test()
Note: See TracBrowser for help on using the browser.