[217] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # pkpgcounter : a generic Page Description Language parser |
---|
| 5 | # |
---|
[303] | 6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
[217] | 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 | |
---|
[354] | 24 | """This modules implements a page counter for TIFF documents.""" |
---|
| 25 | |
---|
[217] | 26 | import sys |
---|
| 27 | import os |
---|
| 28 | import mmap |
---|
| 29 | from struct import unpack |
---|
| 30 | |
---|
[235] | 31 | import pdlparser |
---|
[217] | 32 | |
---|
[220] | 33 | class Parser(pdlparser.PDLParser) : |
---|
[217] | 34 | """A parser for TIFF documents.""" |
---|
[220] | 35 | def isValid(self) : |
---|
| 36 | """Returns 1 if data is TIFF, else 0.""" |
---|
| 37 | littleendian = (chr(0x49)*2) + chr(0x2a) + chr(0) |
---|
| 38 | bigendian = (chr(0x4d)*2) + chr(0) + chr(0x2a) |
---|
| 39 | if self.firstblock[:4] in (littleendian, bigendian) : |
---|
[252] | 40 | self.logdebug("DEBUG: Input file is in the TIFF format.") |
---|
[220] | 41 | return 1 |
---|
| 42 | else : |
---|
| 43 | return 0 |
---|
| 44 | |
---|
[217] | 45 | def getJobSize(self) : |
---|
| 46 | """Counts pages in a TIFF document. |
---|
[219] | 47 | |
---|
| 48 | Algorithm by Jerome Alet. |
---|
| 49 | |
---|
| 50 | The documentation used for this was : |
---|
| 51 | |
---|
| 52 | http://www.ee.cooper.edu/courses/course_pages/past_courses/EE458/TIFF/ |
---|
[217] | 53 | """ |
---|
| 54 | infileno = self.infile.fileno() |
---|
| 55 | minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) |
---|
| 56 | pagecount = 0 |
---|
| 57 | littleendian = (chr(0x49)*2) + chr(0x2a) + chr(0) |
---|
| 58 | bigendian = (chr(0x4d)*2) + chr(0) + chr(0x2a) |
---|
| 59 | if minfile[:4] == littleendian : |
---|
| 60 | integerbyteorder = "<I" |
---|
| 61 | shortbyteorder = "<H" |
---|
[354] | 62 | elif minfile[:4] == bigendian : |
---|
[217] | 63 | integerbyteorder = ">I" |
---|
| 64 | shortbyteorder = ">H" |
---|
[354] | 65 | else : |
---|
| 66 | raise pdlparser.PDLParserError, "Unknown file endianness." |
---|
[217] | 67 | pos = 4 |
---|
| 68 | try : |
---|
[219] | 69 | nextifdoffset = unpack(integerbyteorder, minfile[pos : pos + 4])[0] |
---|
[217] | 70 | while nextifdoffset : |
---|
[219] | 71 | direntrycount = unpack(shortbyteorder, minfile[nextifdoffset : nextifdoffset + 2])[0] |
---|
[217] | 72 | pos = nextifdoffset + 2 + (direntrycount * 12) |
---|
[219] | 73 | nextifdoffset = unpack(integerbyteorder, minfile[pos : pos + 4])[0] |
---|
[217] | 74 | pagecount += 1 |
---|
| 75 | except IndexError : |
---|
| 76 | pass |
---|
| 77 | minfile.close() |
---|
| 78 | return pagecount |
---|
| 79 | |
---|
[365] | 80 | def convertToTiffMultiPage24NC(self, fname, dpi) : |
---|
| 81 | """Converts the input file to TIFF format, X dpi, 24 bits per pixel, uncompressed. |
---|
| 82 | Writes TIFF datas to the outputfile file object. |
---|
| 83 | """ |
---|
| 84 | # TODO : There's NO conversion done, so dpi is ignored, and TIFF files |
---|
| 85 | # TODO : that can't be opened with PIL can't be used... |
---|
| 86 | out = open(fname, "wb") |
---|
| 87 | while 1 : |
---|
| 88 | data = self.infile.read(pdlparser.MEGABYTE) |
---|
| 89 | if not data : |
---|
| 90 | break |
---|
| 91 | out.write(data) |
---|
| 92 | out.close() |
---|
| 93 | |
---|
[217] | 94 | def test() : |
---|
| 95 | """Test function.""" |
---|
| 96 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
| 97 | sys.argv.append("-") |
---|
| 98 | totalsize = 0 |
---|
| 99 | for arg in sys.argv[1:] : |
---|
| 100 | if arg == "-" : |
---|
| 101 | infile = sys.stdin |
---|
| 102 | mustclose = 0 |
---|
| 103 | else : |
---|
| 104 | infile = open(arg, "rb") |
---|
| 105 | mustclose = 1 |
---|
| 106 | try : |
---|
[220] | 107 | parser = Parser(infile, debug=1) |
---|
[217] | 108 | totalsize += parser.getJobSize() |
---|
| 109 | except pdlparser.PDLParserError, msg : |
---|
| 110 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
| 111 | sys.stderr.flush() |
---|
| 112 | if mustclose : |
---|
| 113 | infile.close() |
---|
| 114 | print "%s" % totalsize |
---|
| 115 | |
---|
| 116 | if __name__ == "__main__" : |
---|
| 117 | test() |
---|