Index: /pkpgcounter/trunk/pkpgpdls/postscript.py
===================================================================
--- /pkpgcounter/trunk/pkpgpdls/postscript.py (revision 359)
+++ /pkpgcounter/trunk/pkpgpdls/postscript.py (revision 363)
@@ -177,39 +177,25 @@
         return self.natively() or self.throughGhostScript()
         
-    def throughTiffMultiPage24NC(self, dpi) :
+    def convertToTiffMultiPage24NC(self, fname, dpi) :
         """Converts the input file to TIFF format, X dpi, 24 bits per pixel, uncompressed.
-           Returns percents of ink coverage and number of pages.
+           Writes TIFF datas to the outputfile file object.
         """   
-        self.logdebug("Converting input datas to TIFF...")
-        result = None    
-        self.infile.seek(0)
-        (handle, filename) = tempfile.mkstemp(".tmp", "pkpgcounter")    
-        os.close(handle)
-        command = 'gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%i -sOutputFile="%s" -' % (dpi, filename)
-        try :
-            child = popen2.Popen4(command)
-            try :
-                data = self.infile.read(pdlparser.MEGABYTE)    
-                while data :
-                    child.tochild.write(data)
-                    data = self.infile.read(pdlparser.MEGABYTE)
-                child.tochild.flush()
-                child.tochild.close()    
-            except (IOError, OSError), msg :    
-                raise pdlparser.PDLParserError, "Problem during conversion to TIFF : %s" % msg
-                
-            child.fromchild.close()
-            try :
-                child.wait()
-            except OSError, msg :    
-                raise pdlparser.PDLParserError, "Problem during conversion to TIFF : %s" % msg
-                
-            result = inkcoverage.getPercents(filename)    
-        finally :    
-            try :
-                os.remove(filename)
-            except OSError :    
-                pass
-        return result    
+        command = 'gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%i -sOutputFile="%s" -' % (dpi, fname)
+        child = popen2.Popen4(command)
+        try :
+            data = self.infile.read(pdlparser.MEGABYTE)    
+            while data :
+                child.tochild.write(data)
+                data = self.infile.read(pdlparser.MEGABYTE)
+            child.tochild.flush()
+            child.tochild.close()    
+        except (IOError, OSError), msg :    
+            raise pdlparser.PDLParserError, "Problem during conversion to TIFF : %s" % msg
+            
+        child.fromchild.close()
+        try :
+            child.wait()
+        except OSError, msg :    
+            raise pdlparser.PDLParserError, "Problem during conversion to TIFF : %s" % msg
         
 def test() :        
Index: /pkpgcounter/trunk/pkpgpdls/pdlparser.py
===================================================================
--- /pkpgcounter/trunk/pkpgpdls/pdlparser.py (revision 362)
+++ /pkpgcounter/trunk/pkpgpdls/pdlparser.py (revision 363)
@@ -83,7 +83,7 @@
         raise RuntimeError, "Not implemented !"
         
-    def convertToTiffMultiPage24NC(self, outputfile, dpi) :
+    def convertToTiffMultiPage24NC(self, fname, dpi) :
         """Converts the input file to TIFF format, X dpi, 24 bits per pixel, uncompressed.
-           Writes TIFF datas to the outputfile file object.
+           Writes TIFF datas to the file named by fname.
         """   
         raise RuntimeError, "Not implemented !"
Index: /pkpgcounter/trunk/pkpgpdls/inkcoverage.py
===================================================================
--- /pkpgcounter/trunk/pkpgpdls/inkcoverage.py (revision 361)
+++ /pkpgcounter/trunk/pkpgpdls/inkcoverage.py (revision 363)
@@ -28,4 +28,15 @@
 from PIL import Image
 
+def getPercent(img, nbpix) :
+    """Extracts the percents per color component from a picture.
+      
+       Faster without Psyco on my own machine.
+    """
+    result = {}     
+    bands = img.split()
+    for (i, bandname) in enumerate(img.getbands()) :
+        result[bandname] = 100.0 * (reduce(lambda current, next: current + (next[1] * next[0]), enumerate(bands[i].histogram()), 0) / 255.0) / nbpix
+    return result    
+    
 def getPercentCMYK(img, nbpix) :
     """Extracts the percents of Cyan, Magenta, Yellow, and Black from a picture.
@@ -51,20 +62,9 @@
            }
         
-def getPercent(img, nbpix) :
-    """Extracts the percents per color component from a picture.
-      
-       Faster without Psyco on my own machine.
-    """
-    result = {}     
-    bands = img.split()
-    for (i, bandname) in enumerate(img.getbands()) :
-        result[bandname] = 100.0 * (reduce(lambda current, next: current + (next[1] * next[0]), enumerate(bands[i].histogram()), 0) / 255.0) / nbpix
-    return result    
-        
-def getPercentBlack(img, nbpix) :
+def getPercentBW(img, nbpix) :
     """Extracts the percents of Black from a picture, once converted to gray levels."""
     if img.mode != "L" :
         img = img.convert("L")
-    return { "L" : 100.0 - getPercent(img, nbpix)["L"] }
+    return { "B" : 100.0 - getPercent(img, nbpix)["L"] }
     
 def getPercentRGB(img, nbpix) :
@@ -82,13 +82,20 @@
            }
     
-def getPercents(fname) :
-    """Extracts the ink percentages from an image."""
-    try :
-        import psyco
-    except ImportError :    
-        pass
-    else :    
-        psyco.bind(getPercentCMYK)
+def getInkCoverage(fname, colorspace) :
+    """Returns a list of dictionnaries containing for each page, 
+       for each color component, the percent of ink coverage on 
+       that particular page.
+    """
     result = []
+    colorspace = colorspace.upper()
+    computation = globals()["getPercent%s" % colorspace]
+    if colorspace == "CMYK" : # faster with psyco on my machine
+        try :
+            import psyco
+        except ImportError :    
+            pass
+        else :    
+            psyco.bind(computation)
+    
     index = 0
     image = Image.open(fname)
@@ -96,10 +103,5 @@
         while 1 :
             nbpixels = image.size[0] * image.size[1]
-            result.append((image.size, \
-                           { "BLACK" : getPercentBlack(image, nbpixels), \
-                             "RGB" : getPercentRGB(image, nbpixels), \
-                             "CMY" : getPercentCMY(image, nbpixels), \
-                             "CMYK" : getPercentCMYK(image, nbpixels), \
-                           }))
+            result.append({ colorspace : computation(image, nbpixels) })
             index += 1              
             image.seek(index)
@@ -110,3 +112,3 @@
 if __name__ == "__main__" :
     # NB : length of result gives number of pages !
-    print getPercents(sys.argv[1])
+    print getInkCoverage(sys.argv[1], "CMYK")
Index: /pkpgcounter/trunk/pkpgpdls/analyzer.py
===================================================================
--- /pkpgcounter/trunk/pkpgpdls/analyzer.py (revision 358)
+++ /pkpgcounter/trunk/pkpgpdls/analyzer.py (revision 363)
@@ -26,9 +26,10 @@
 
 import sys
+import os
 import tempfile
 
 import version, pdlparser, postscript, pdf, pcl345, pclxl, \
        escp2, dvi, tiff, ooo, zjstream
-
+import inkcoverage
 
 class AnalyzerOptions :
@@ -71,4 +72,40 @@
                 self.closeFile()
             return size
+            
+    def getInkCoverage(self, colorspace=None, resolution=None) :
+        """Extracts the percents of ink coverage from the input file."""
+        result = None
+        cspace = colorspace or self.options.colorspace
+        res = resolution or self.options.resolution
+        if (not cspace) or (not res) :
+            raise ValueError, "Invalid colorspace (%s) or resolution (%s)" % (cspace, res)
+        self.openFile()
+        try :
+            pdlhandler = self.detectPDLHandler()
+        except pdlparser.PDLParserError, msg :    
+            self.closeFile()
+            raise pdlparser.PDLParserError, "Unknown file format for %s (%s)" % (self.filename, msg)
+        else :
+            try :
+                tiffname = self.convertToTiffMultiPage24NC(pdlhandler)
+                result = inkcoverage.getInkCoverage(tiffname, cspace)
+                try :
+                    os.remove(tiffname)
+                except OSError :
+                    sys.stderr.write("Problem when trying to remove temporary file %s\n" % tiffname)
+            finally :    
+                self.closeFile()
+        return result
+        
+    def convertToTiffMultiPage24NC(self, handler) :    
+        """Converts the input file to TIFF format, X dpi, 24 bits per pixel, uncompressed.
+           Returns a temporary filename which names a file containing the TIFF datas.
+           The temporary file has to be deleted by the caller.
+        """   
+        self.infile.seek(0)
+        (handle, filename) = tempfile.mkstemp(".tmp", "pkpgcounter")    
+        os.close(handle)
+        handler.convertToTiffMultiPage24NC(filename, self.options.resolution)
+        return filename
         
     def openFile(self) :    
@@ -180,6 +217,6 @@
                             dest="colorspace",
                             type="cichoice",
-                            cichoices=["bw", "cmyk", "cmy", "all"],
-                            help="Activate the computation of ink usage, and defines the colorspace to use. Supported values are 'BW', 'CMYK', 'CMY' and 'ALL'.")
+                            cichoices=["bw", "rgb", "cmyk", "cmy"],
+                            help="Activate the computation of ink usage, and defines the colorspace to use. Supported values are 'BW', 'RGB', 'CMYK', and 'CMY'.")
     parser.add_option("-r", "--resolution", 
                             type="int", 
@@ -201,5 +238,10 @@
                 try :
                     parser = PDLAnalyzer(arg, options)
-                    totalsize += parser.getJobSize()
+                    if not options.colorspace :
+                        totalsize += parser.getJobSize()
+                    else :
+                        result = parser.getInkCoverage()
+                        totalsize += len(result)
+                        print result
                 except (IOError, pdlparser.PDLParserError), msg :    
                     sys.stderr.write("ERROR: %s\n" % msg)
Index: /pkpgcounter/trunk/NEWS
===================================================================
--- /pkpgcounter/trunk/NEWS (revision 343)
+++ /pkpgcounter/trunk/NEWS (revision 363)
@@ -22,4 +22,11 @@
 pkpgcounter News :
 
+  * 1.84alpha :
+  
+    - Added initial support for the computation of ink coverage.
+      See python analyzer.py --help for details. The ame command
+      line options work for pkpgcounter, although it's not
+      documented yet.
+    
   * 1.83 :
   
