I have a need to determine if a postscript file was sent to the printer in color or black and white. While there are many solutions out there, they mostly seem to requires rendering the document and then checking each page individually using ghostscript, which can be a bit slow for larger documents. I was wondering if the following grep would give me the same info or if there are other types of colors missing?
grep 'setrgbcolor\|setcymkcolor\|colorimage' foo.prn
You are a very long way short of the full range of colour spaces available in PostScript. You don't have setcolorspace
and setcolorspace takes a complex array of arguments which can include /DeviceGray (so not colour).
You also aren't accounting for CIEBasedA nor the fact that C=M=Y=0, K is grayscale even though it is expressed in CMYK space, and of course R=G=B is s shade of gray expressed in RGB space. Similarly /Separataion /Black will draw in a shade of gray.
In addition, PostScript is a programming language; it's very common to include a prolog which defines operations in a way convenient to the author of the PostScript-generating application. The prolog can include functions which are not used in this particular program. So something like:
/rg /setrgbcolor load def
would trigger your grep, even if the PostScript program didn't then use rg
.
Then there are images. colorimage
is not the only way to draw an image in PostScript, the image
operator takes a dictionary operand, and that can include a colour space. As above, it's possible to have a gray image expressed in a different colour space (inefficient, possibly, but legal).
You can write a PostScript program which 'hooks' the definition of the colour operators and have that code interrogate the parameters to decide if the specification is a gray space or not, images are much harder to deal with since, if you want reliability, you need to look at the image sample data to see whether it's actually just a gray image.
It's possible to write a Ghostscript device which could do the job, because it will receive all the operations. It still won't be entirely fast as it has to interpret the entire file in order to detect every operation. Even then such a tool could be defeated in particular applications. For example the code could interrogate the underlying device and take a different code path if the device colour space was Gray, RGB or CMYK. But that's highly unlikely to happen in any real world PostScript program.