[217] | 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 | |
---|
| 24 | import sys |
---|
| 25 | import os |
---|
| 26 | import mmap |
---|
| 27 | from struct import unpack |
---|
| 28 | |
---|
| 29 | from pdlanalyzer import pdlparser |
---|
| 30 | |
---|
[220] | 31 | class Parser(pdlparser.PDLParser) : |
---|
[217] | 32 | """A parser for TIFF documents.""" |
---|
[220] | 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 | |
---|
[217] | 44 | def getJobSize(self) : |
---|
| 45 | """Counts pages in a TIFF document. |
---|
[219] | 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/ |
---|
[217] | 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 : |
---|
[219] | 66 | nextifdoffset = unpack(integerbyteorder, minfile[pos : pos + 4])[0] |
---|
[217] | 67 | while nextifdoffset : |
---|
[219] | 68 | direntrycount = unpack(shortbyteorder, minfile[nextifdoffset : nextifdoffset + 2])[0] |
---|
[217] | 69 | pos = nextifdoffset + 2 + (direntrycount * 12) |
---|
[219] | 70 | nextifdoffset = unpack(integerbyteorder, minfile[pos : pos + 4])[0] |
---|
[217] | 71 | pagecount += 1 |
---|
| 72 | except IndexError : |
---|
| 73 | pass |
---|
| 74 | minfile.close() |
---|
| 75 | return pagecount |
---|
| 76 | |
---|
| 77 | def 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 : |
---|
[220] | 90 | parser = Parser(infile, debug=1) |
---|
[217] | 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 | |
---|
| 99 | if __name__ == "__main__" : |
---|
| 100 | test() |
---|