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

Revision 3474, 2.8 kB (checked in by jerome, 15 years ago)

Changed copyright years.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[3410]1# -*- coding: utf-8 -*-
[217]2#
3# pkpgcounter : a generic Page Description Language parser
4#
[3474]5# (c) 2003-2009 Jerome Alet <alet@librelogiciel.com>
[463]6# This program is free software: you can redistribute it and/or modify
[217]7# it under the terms of the GNU General Public License as published by
[463]8# the Free Software Foundation, either version 3 of the License, or
[217]9# (at your option) any later version.
[3436]10#
[217]11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
[3436]15#
[217]16# You should have received a copy of the GNU General Public License
[463]17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[217]18#
19# $Id$
20#
21
[354]22"""This modules implements a page counter for TIFF documents."""
23
[217]24import sys
25import os
26import mmap
27from struct import unpack
28
[235]29import pdlparser
[217]30
[220]31class Parser(pdlparser.PDLParser) :
[217]32    """A parser for TIFF documents."""
[527]33    totiffcommands = [ 'cp "%(infname)s" "%(outfname)s"' ]
34    required = [ "cp" ]
[555]35    format = "TIFF"
[3436]36    def isValid(self) :
[387]37        """Returns True if data is TIFF, else False."""
[220]38        littleendian = (chr(0x49)*2) + chr(0x2a) + chr(0)
39        bigendian = (chr(0x4d)*2) + chr(0) + chr(0x2a)
[522]40        if self.firstblock[:4] in (littleendian, bigendian) :
[387]41            return True
[3436]42        else :
[387]43            return False
[3436]44
[217]45    def getJobSize(self) :
46        """Counts pages in a TIFF document.
[3436]47
[219]48           Algorithm by Jerome Alet.
[3436]49
[219]50           The documentation used for this was :
[3436]51
[219]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"
[3436]65        else :
[354]66            raise pdlparser.PDLParserError, "Unknown file endianness."
[3436]67        pos = 4
[489]68        try :
[3436]69            try :
[219]70                nextifdoffset = unpack(integerbyteorder, minfile[pos : pos + 4])[0]
[489]71                while nextifdoffset :
72                    direntrycount = unpack(shortbyteorder, minfile[nextifdoffset : nextifdoffset + 2])[0]
73                    pos = nextifdoffset + 2 + (direntrycount * 12)
74                    nextifdoffset = unpack(integerbyteorder, minfile[pos : pos + 4])[0]
75                    pagecount += 1
[3436]76            except IndexError :
[489]77                pass
[3436]78        finally :
[489]79            minfile.close()
[217]80        return pagecount
Note: See TracBrowser for help on using the browser.