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