root / pkpgcounter / trunk / pkpgpdls / tiff.py @ 463

Revision 463, 2.9 kB (checked in by jerome, 17 years ago)

Licensing terms changed to GNU GPL v3.0 or higher.
Removed old PCL3/4/5 parser which for a long time now wasn't used
anymore, and for which I was not the original copyright owner.
Version number bumped to 3.00alpha to reflect licensing changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[217]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
[443]6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[463]7# This program is free software: you can redistribute it and/or modify
[217]8# it under the terms of the GNU General Public License as published by
[463]9# the Free Software Foundation, either version 3 of the License, or
[217]10# (at your option) any later version.
[463]11#
[217]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
[463]18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[217]19#
20# $Id$
21#
22
[354]23"""This modules implements a page counter for TIFF documents."""
24
[217]25import sys
26import os
27import mmap
28from struct import unpack
29
[235]30import pdlparser
[217]31
[220]32class Parser(pdlparser.PDLParser) :
[217]33    """A parser for TIFF documents."""
[428]34    totiffcommands = [ "cat >%(fname)s" ]
[220]35    def isValid(self) :       
[387]36        """Returns True if data is TIFF, else False."""
[220]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.")
[387]41            return True
[220]42        else :   
[387]43            return False
[220]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       
80if __name__ == "__main__" :   
[415]81    pdlparser.test(Parser)
Note: See TracBrowser for help on using the browser.