From: ab@mentor.cc.purdue.edu (Allen Braunsdorf)
Subject: Re: SUMMARY: How can I tell if a PS file is in color?
Summary: solution in PS
Organization: Purdue UNIX Group
Date: Mon, 25 Jan 1993 15:59:04 GMT



Here's the promised bit of code to test for color being used in a
PostScript job.  The instructions are for using it with GhostScript.
You could use it in any two-way situation, but the procedure's a little
different.

It hasn't been tested a lot, but it's based on a piece of code I use
often, so it should be OK.  Tell me if you have any trouble.  As often
with my stuff, the documentation is longer than the program. :-)

ab

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	README
#	colorpdict.ps
#	level2
# This archive created: Mon Jan 25 10:47:49 1993
# By:	Allen Braunsdorf (Purdue UNIX Group)
echo shar: extracting README '(3686 characters)'
sed 's/^XX//' << \SHAR_EOF > README
XXWhat's Here
XX
XXEnclosed you should find this file (README) the Level 1 version
XX(colorpdict.ps) and the Level 2 version (level2).  If you aren't
XXsure which version to use, just use the Level 1 one.
XX
XXMove whichever one you want to colorpdict.ps in your PostScript
XXtools directory.  (What you don't have one? :-) )
XX
XX
XXHow to Use It
XX
XXTo use this from GhostScript, run gs without any arguments (so it
XXgoes interactive).
XX
XXThen you need to load the new dictionary:
XX
XX	(colorpdict.ps) run
XX
XXThe operand to run is the pathname to the PostScript file that came
XXwith this.  If it's not in the current directory, you'll need to
XXsay where it is.
XX
XXThen you need to install the new functions:
XX
XX	colorpdict begin please
XX
XXThen run the code you want to check:
XX
XX	(foo.ps) run
XX
XXIf it ever tries to draw in color, you'll see the message "Non-gray
XXcolor encountered" in the interactive window.  If you didn't, it
XXdidn't.  The message will only be printed once to save time.
XX
XXIn fact, when the message is printed, the checking routine is
XXneutered.  It's still there, it just doesn't work.  To re-enable
XXit, type:
XX
XX	please
XX
XXwhich will make it work again.  That's why you had to type please
XXwhen you began the dictionary.  (Not just 'cause we're so polite.)
XX
XXTo get rid of the checking functions:
XX
XX	end
XX
XXWhich will put you back in the state you were after you loaded the
XXdictionary.  If you begin it again, be sure to say please. :-)
XX
XX
XXHow It Works
XX
XXYou might be wondering how this thing works.  It's not very hard,
XXbut you need to understand some basic PostScript concepts.  If you
XXfind you don't really grok something, please consult a reference
XXtext.
XX
XXFirst you'll see that we construct an array full of literal names.
XXThese are the names that we want to redefine.
XX
XXThen we build a dictionary with enough entries for those new
XXfunctions, 2 other functions, and (in the Level 1 version) as many
XXentries as userdict has.  The last is so the program we run has
XXroom to work if it needs it.  In Level 2 PostScript, dictionaries
XXwon't fill up, so that's not necessary.
XX
XXWe begin this dictionary so all our definitions will be in it.
XX
XXNext we do a forall on the array we made above.  This makes a
XXprocedure for all the names in the array.  That procedure performs
XXcolorp before it does whatever that name is currently assigned to.
XXIt'll work if they aren't operators as well.  To see a sample
XXdefinition, type:
XX
XX	/stroke load ==
XX
XXwhen the dictionary is in effect.  Load the dictionary again if
XXyou want to see what happens if it redefines things twice.
XX
XXThen we define please, which installs the checking function as
XXcolorp.  Note that colorp destroys itself if it sees color.  This
XXkeeps it from printing a zillion messages on the screen.
XX
XX
XXBUGS
XX
XXThis version doesn't trap image calls.  In Level 1, images should
XXbe grayscale anyway.  In general, there should be few images in a
XXdocument and you can probably scope those out by hand.  Maybe I'll
XXfix that later.
XX
XXThe prototype function insists on using the systemdict version of
XXexec.  This was to get around worrying about how to bind exec if
XXit wasn't an operator.  (Who redefines exec anyway? :-) )
XX
XXWhen please is executed, colorp is defined in the current dictionary.
XXThat's probably OK.  When colorp redefines itself, it also does it in
XXthe current dictionary, which might be a problem.  In general, this
XXwill only cause extra messages to be printed and should not make it
XXfail.
XX
XXOf course this won't work if the tested program has its own definition
XXfor colorp or insists on using graphics routines from systemdict.
XX
XX
XXQuestions?
XX
XXQuestions, problems, or (heaven forfend!) bugs can be sent to me
XXat the address below.  Have fun!
XX
XXAllen B (ab@nova.cc.purdue.edu)
SHAR_EOF
if test 3686 -ne "`wc -c README`"
then
echo shar: error transmitting README '(should have been 3686 characters)'
fi
echo shar: extracting colorpdict.ps '(728 characters)'
sed 's/^XX//' << \SHAR_EOF > colorpdict.ps
XX%!
XX%%Title: colorpdict 1.0 (for Level 1 PS)
XX%%Creator: ab@nova.cc.purdue.edu
XX%%EndComments
XX
XX% put operators with graphic output here:
XX[
XX	/fill
XX	/eofill
XX	/stroke
XX	/imagemask
XX	/show
XX	/ashow
XX	/widthshow
XX	/awidthshow
XX	/kshow
XX]
XX% When encoutered, the currentrgbcolor will be checked
XX% (You should only include operators that actually mark the page.)
XX
XXdup length 2 add userdict maxlength add dict /colorpdict exch def
XXcolorpdict begin
XX
XX{
XX	[
XX		/colorp cvx
XX		2 index load
XX		dup type /arraytype eq {
XX			systemdict begin
XX			/exec load
XX			end
XX		} if
XX	] cvx def
XX} forall
XX
XX/please
XX{
XX	/colorp
XX	{
XX		currentrgbcolor 2 index ne 3 1 roll ne or {
XX			(Non-gray color encountered\n) print flush
XX			/colorp null cvx def
XX		} if
XX	} bind def
XX} def
XX
XXend
SHAR_EOF
if test 728 -ne "`wc -c colorpdict.ps`"
then
echo shar: error transmitting colorpdict.ps '(should have been 728 characters)'
fi
echo shar: extracting level2 '(802 characters)'
sed 's/^XX//' << \SHAR_EOF > level2
XX%!
XX%%Title: colorpdict 1.0 (for Level 2 PS)
XX%%Creator: ab@nova.cc.purdue.edu
XX%%EndComments
XX
XX% put operators with graphic output here:
XX[
XX	/fill
XX	/eofill
XX	/stroke
XX	/ufill
XX	/ueofill
XX	/ustroke
XX	/rectfill
XX	/rectstroke
XX	/imagemask
XX	/show
XX	/ashow
XX	/widthshow
XX	/awidthshow
XX	/xshow
XX	/xyshow
XX	/yshow
XX	/glyphshow
XX	/cshow
XX	/kshow
XX]
XX% When encoutered, the currentrgbcolor will be checked
XX% (You should only include operators that actually mark the page.)
XX
XXdup length 2 add dict /colorpdict exch def
XXcolorpdict begin
XX
XX{
XX	[
XX		/colorp cvx
XX		2 index load
XX		dup type /arraytype eq {
XX			systemdict begin
XX			/exec load
XX			end
XX		} if
XX	] cvx def
XX} forall
XX
XX/please
XX{
XX	/colorp
XX	{
XX		currentrgbcolor 2 index ne 3 1 roll ne or {
XX			(Non-gray color encountered\n) print flush
XX			/colorp null cvx def
XX		} if
XX	} bind def
XX} def
XX
XXend
SHAR_EOF
if test 802 -ne "`wc -c level2`"
then
echo shar: error transmitting level2 '(should have been 802 characters)'
fi
#	End of shell archive
exit 0