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

Revision 220, 3.5 kB (checked in by jerome, 19 years ago)

Big improvements on readability + maintainability

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