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

Revision 219, 3.1 kB (checked in by jerome, 19 years ago)

Documented where the file format is documented

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