Ticket #11: pkpgcounter-mediawork.patch
File pkpgcounter-mediawork.patch, 13.3 kB (added by jerome, 15 years ago) |
---|
-
bin/pkpgcounter
diff --git a/bin/pkpgcounter b/bin/pkpgcounter index 4ffa10a..ae2d141 100755
a b 81 81 grayscale pages from coloured pages but don't care 82 82 about ink usage per se. 83 83 84 -m, --media 85 Activate the media size, format and duplex detection / 86 tracking code. 87 84 88 -rRESOLUTION, --resolution=RESOLUTION 85 89 The resolution in DPI to use when checking ink usage. 86 90 Lower resolution is faster but less accurate. Default -
pkpgpdls/analyzer.py
diff --git a/pkpgpdls/analyzer.py b/pkpgpdls/analyzer.py index 954aaad..e09f203 100644
a b 3 3 # pkpgcounter : a generic Page Description Language parser 4 4 # 5 5 # (c) 2003-2009 Jerome Alet <alet@librelogiciel.com> 6 # Portions (c) 2009 Trever L. Adams <trever.adams@gmail.com> 7 # 6 8 # This program is free software: you can redistribute it and/or modify 7 9 # it under the terms of the GNU General Public License as published by 8 10 # the Free Software Foundation, either version 3 of the License, or … … 71 73 self.closeFile() 72 74 return size 73 75 76 def getMediaFormat(self) : 77 """Returns the job's media/format.""" 78 result = None 79 self.openFile() 80 try : 81 try : 82 pdlhandler = self.detectPDLHandler() 83 result = pdlhandler.getMediaFormat() 84 except pdlparser.PDLParserError, msg : 85 raise pdlparser.PDLParserError, "Unsupported file format for %s (%s)" % (self.filename, msg) 86 finally : 87 self.closeFile() 88 return result 89 74 90 def getInkCoverage(self, colorspace=None, resolution=None) : 75 91 """Extracts the percents of ink coverage from the input file.""" 76 92 result = None … … 211 227 action="store_true", 212 228 dest="debug", 213 229 help="Activate debug mode.") 230 parser.add_option("-m", "--media", 231 action="store_true", 232 dest="media", 233 help="Activate the detection of various media/format options of the job.") 214 234 parser.add_option("-c", "--colorspace", 215 235 dest="colorspace", 216 236 type="cichoice", … … 236 256 for arg in arguments : 237 257 try : 238 258 parser = PDLAnalyzer(arg, options) 239 if not options.colorspace :259 if not options.colorspace and not options.media : 240 260 totalsize += parser.getJobSize() 241 else :261 if options.colorspace : 242 262 (cspace, pages) = parser.getInkCoverage() 263 if cspace == "BW" : 264 cspace = "K" 265 elif cspace == "GC" : 266 cspace = ["GS", "CR"] 243 267 for page in pages : 244 268 lineparts = [] 245 269 for k in cspace : # NB : this way we preserve the order of the planes 246 270 try : 247 lineparts.append("%s : %s%%" % (k, ("%f" % page[k]).rjust(10))) 271 lineparts.append("%s: %s%%" % (k, ("%f" % page[k]).rjust(10))) 272 except KeyError : 273 pass 274 lines.append(" ".join(lineparts)) 275 if options.media : 276 (mspace, pages) = parser.getMediaFormat() 277 if lines == [] : 278 haveOutput = False 279 else : 280 haveOutput = True 281 i = 0 282 for page in pages : 283 lineparts = [] 284 for k in mspace : # NB : this way we preserve the order of the media format string 285 try : 286 lineparts.append("%s: %s" % (k, ("%s" % page[k]).ljust(10))) 248 287 except KeyError : 249 288 pass 250 lines.append(" ".join(lineparts)) 289 if haveOutput is False : 290 lines.append(" ".join(lineparts)) 291 else : 292 lines[i] = lines[i] + ' ' + ''.join(lineparts) 293 i = i + 1 251 294 except (IOError, pdlparser.PDLParserError), msg : 252 295 sys.stderr.write("ERROR: %s\n" % msg) 253 296 sys.stderr.flush() 254 297 except KeyboardInterrupt : 255 298 sys.stderr.write("WARN: Aborted at user's request.\n") 256 299 sys.stderr.flush() 257 if not options.colorspace :300 if not options.colorspace and not options.media : 258 301 sys.stdout.write("%i\n" % totalsize) 259 302 else : 260 303 sys.stdout.write("%s\n" % ("\n".join(lines))) -
pkpgpdls/inkcoverage.py
diff --git a/pkpgpdls/inkcoverage.py b/pkpgpdls/inkcoverage.py index 900bf07..dc14be0 100644
a b 73 73 for (r, g, b) in img.getdata() : 74 74 if not (r == g == b) : 75 75 # optimize : if a single pixel is not gray the whole page is colored. 76 return { "G " : 0.0, "C" : 100.0 }77 return { "G " : 100.0, "C" : 0.0 }76 return { "GS" : 0.0, "CR" : 100.0 } 77 return { "GS" : 100.0, "CR" : 0.0 } 78 78 79 79 def getPercentBW(img, nbpix) : 80 80 """Extracts the percents of Black from a picture, once converted to gray levels.""" 81 81 if img.mode != "L" : 82 82 img = img.convert("L") 83 return { " B" : 100.0 - getPercent(img, nbpix)["L"] }83 return { "K" : 100.0 - getPercent(img, nbpix)["L"] } 84 84 85 85 def getPercentRGB(img, nbpix) : 86 86 """Extracts the percents of Red, Green, Blue from a picture, once converted to RGB.""" -
pkpgpdls/postscript.py
diff --git a/pkpgpdls/postscript.py b/pkpgpdls/postscript.py index 71b12ca..8fc984b 100644
a b 3 3 # pkpgcounter : a generic Page Description Language parser 4 4 # 5 5 # (c) 2003-2009 Jerome Alet <alet@librelogiciel.com> 6 # (c) 2009 Trever L. Adams <trever.adams@gmail.com> 7 # 6 8 # This program is free software: you can redistribute it and/or modify 7 9 # it under the terms of the GNU General Public License as published by 8 10 # the Free Software Foundation, either version 3 of the License, or … … 23 25 24 26 import sys 25 27 import os 28 import re 26 29 27 30 import pdlparser 28 31 import inkcoverage 29 32 33 findmediatype = re.compile(".*/MediaType [(]?([^)]*).*") 34 findmediasize = re.compile(".*/PageSize \[?([\d.]*) ([\d.]*)\]?.*") 35 findduplexoff = re.compile(r".*/Duplex false.*") 36 findduplexon = re.compile(r".*/Duplex true.*") 37 38 # Any zeros below indicate that the exact postscript size is unknown 39 # Below are some papers that have multiple names. Don't use the Alias(es). 40 # Cannocal Alias 41 # Leder 11x17 42 # Statement half letter 43 PostScriptPaperSizes = \ 44 ("A0", 2384, 3370), \ 45 ("A1", 1684, 2384), \ 46 ("A2", 1191, 1684), \ 47 ("A3", 842, 1191), \ 48 ("A4", 595, 842), \ 49 ("A5", 420, 595), \ 50 ("A6", 297, 420), \ 51 ("A7", 210, 297), \ 52 ("A8", 148, 210), \ 53 ("A9", 105, 148), \ 54 ("A10", 73, 105), \ 55 ("ArchA", 648, 864), \ 56 ("ArchB", 864, 1296), \ 57 ("ArchC", 1296, 1728), \ 58 ("ArchD", 1728, 2592), \ 59 ("ArchE", 2592, 3456), \ 60 ("B0", 2835, 4008), \ 61 ("B1", 2004, 2835), \ 62 ("B2", 1417, 2004), \ 63 ("B3", 1001, 1417), \ 64 ("B4", 709, 1001), \ 65 ("B5", 499, 709), \ 66 ("B6", 354, 499), \ 67 ("C0", 2599, 3677), \ 68 ("C1", 1837, 2599), \ 69 ("C2", 1298, 1837), \ 70 ("C3", 918, 1298), \ 71 ("C4", 649, 918), \ 72 ("C5", 459, 649), \ 73 ("C6", 323, 459), \ 74 ("COM10Envelope", 0, 0), \ 75 ("DL Envelope", 312, 624), \ 76 ("Envelope #9", 279, 639), \ 77 ("Envelope #10", 297, 684), \ 78 ("Envelope #11", 324, 747), \ 79 ("Envelope #12", 288, 792), \ 80 ("Envelope #14", 360, 828), \ 81 ("Executive", 540, 720), \ 82 ("Foolscap", 612, 936), \ 83 ("JDoublePostcard", 0, 0), \ 84 ("JIS16K", 0, 0), \ 85 ("JIS8K", 0, 0), \ 86 ("JISB0", 0, 0), \ 87 ("JISB1", 0, 0), \ 88 ("JISB2", 0, 0), \ 89 ("JISB3", 0, 0), \ 90 ("JISB4", 0, 0), \ 91 ("JISB5", 0, 0), \ 92 ("JISB6", 0, 0), \ 93 ("JISExec", 0, 0), \ 94 ("JPostcard", 0, 0), \ 95 ("Ledger", 792, 1224), \ 96 ("Legal", 612, 1008), \ 97 ("Letter", 612, 792), \ 98 ("MonarchEnvelope", 279, 540), \ 99 ("Statement", 396, 612) 100 30 101 class Parser(pdlparser.PDLParser) : 31 102 """A parser for PostScript documents.""" 32 103 totiffcommands = [ 'gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r"%(dpi)i" -sOutputFile="%(outfname)s" "%(infname)s"' ] … … 77 148 if number > self.pages[pagenum]["copies"] : 78 149 self.pages[pagenum]["copies"] = number 79 150 151 def getpapersize(self, x, y) : 152 for tempsize in PostScriptPaperSizes : 153 if (float(x) == float(tempsize[1])) and (float(y) == float(tempsize[2])) : 154 return tempsize[0] 155 80 156 def natively(self) : 81 157 """Count pages in a DSC compliant PostScript document.""" 82 158 pagecount = 0 … … 87 163 prescribe = False # Kyocera's Prescribe commands 88 164 acrobatmarker = False 89 165 pagescomment = None 166 duplexmode= "N" 167 mediasizelabel = "Letter" 168 mediatypelabel = "Plain" 90 169 for line in self.infile : 91 170 line = line.strip() 92 171 parts = line.split() … … 145 224 if proceed and not notinteger : 146 225 pagecount += 1 147 226 self.pages[pagecount] = { "copies" : self.pages[pagecount-1]["copies"] } 227 self.pages[pagecount]["duplex"] = duplexmode 228 self.pages[pagecount]["mediatype"] = mediatypelabel 229 self.pages[pagecount]["mediasize"] = mediasizelabel 230 elif findduplexon.search(line) : 231 self.pages[pagecount]["duplex"] = "Y" 232 duplexmode=self.pages[pagecount]["duplex"] 233 elif findduplexoff.search(line) : 234 self.pages[pagecount]["duplex"] = "N" 235 duplexmode=self.pages[pagecount]["duplex"] 236 elif findmediatype.search(line) : 237 mediatype = findmediatype.search(line) 238 self.pages[pagecount]["mediatype"] = mediatype.group(1) 239 mediatypelabel = self.pages[pagecount]["mediatype"] 240 elif findmediasize.search(line) : 241 mediasize = findmediasize.search(line) 242 try : 243 self.pages[pagecount]["mediasize"] = self.getpapersize(min(mediasize.group(1), mediasize.group(2)), max(mediasize.group(1), mediasize.group(2))) 244 mediasizelabel = self.pages[pagecount]["mediasize"] 245 except : 246 pass 148 247 elif (not prescribe) \ 149 248 and (parts[:3] == [r"%%BeginResource:", "procset", "pdf"]) \ 150 249 and not acrobatmarker : … … 169 268 self.logdebug("%s * page #%s" % (copies, pnum)) 170 269 171 270 self.logdebug("Internal parser said : %s pages" % pagecount) 172 return (pagecount, notrust )271 return (pagecount, notrust, self.pages) 173 272 174 273 def getJobSize(self) : 175 274 """Count pages in PostScript document.""" 176 275 self.copies = 1 177 (nbpages, notrust ) = self.natively()276 (nbpages, notrust, pages) = self.natively() 178 277 newnbpages = nbpages 179 278 if notrust or not nbpages : 180 279 try : … … 182 281 except pdlparser.PDLParserError, msg : 183 282 self.logdebug(msg) 184 283 return max(nbpages, newnbpages) 284 285 def getMediaFormat(self) : 286 """Discover media fomats/sizes in PostScript document.""" 287 result = [] 288 self.copies = 1 289 (nbpages, notrust, pages) = self.natively() 290 for k in pages : 291 if k > 0 : 292 result.append({"MT" : pages[k]["mediatype"], 293 "MS" : pages[k]["mediasize"], 294 "DU" : pages[k]["duplex"], 295 }) 296 return ((["MT", "MS", "DU"]), result)