[comp.lang.postscript] Color PS->PS

bijal@hpopd.pwd.hp.com (Bijal Shah) (01/21/91)

	Is there any program available that will convert a color PS document
	to a grey scales or ordinary PS document suitable for a Laserwriter?

	Thanks in advance

				BJ

glenn@heaven.woodside.ca.us (Glenn Reid) (01/22/91)

In article <60410001@hpopd.pwd.hp.com> bijal@hpopd.pwd.hp.com (Bijal Shah) writes:
>
>
>	Is there any program available that will convert a color PS document
>	to a grey scales or ordinary PS document suitable for a Laserwriter?
>
>	Thanks in advance
>
>				BJ

Try prepending the following file to your color PS file and send it to
your Laserwriter; I think it's available from the ps-file-server as well:

%!
%%Title: adobe_colorops.ps
%%CreationDate: Mon Nov 14 14:23:27 1988
%%Creator: Glenn Reid, Adobe Systems Incorporated
%%EndComments

% Copyright (c) 1988, 1989 Adobe Systems Incorporated.  All rights reserved.

%%VMUsage: 1536

%%BeginProcSet: adobe_install 1.0
/installprocset { %def		% /name (endflag)
    where { %ifelse
	pop
	(flushing ... ) print
	save 256 string
	    { %loop
		currentfile 1 index readline not { pop pop exit }{ %ifelse
		    3 index eq { pop exit } if
		} ifelse
	    } loop
	restore	pop (done) =
    }{ pop } ifelse
} bind def
%%EndProcSet: adobe_install 1.0

(%%EndProcSet: adobe_colorops 1.0)	% flush to this flag if found
/colorimage				% test for this operator
installprocset

% This file implements backward-compatibility packages for many of the
% new color PostScript operators. These are most useful when a document
% is to be sent to a printer which does not have any of these
% language extensions.  These packages should permit the file to print
% correctly on most or all PostScript printers.

% The routines are broken into procsets and delimited with comments.
%
% SUPPORTED:
%    colorimage
%    setcmykcolor		currentcmykcolor
%    setblackgeneration		currentblackgeneration
%    setcolorscreen		currentcolorscreen
%    setcolortransfer		currentcolortransfer
%    setundercolorremoval	currentundercolorremoval

% Caveats:  these procedures, in order to keep them reasonable,
% do not do extensive error checking, and may not report the same
% errors that the original operators would.  However, if called with
% appropriate arguments, they should perform reasonably.

%%BeginProcSet: adobe_ifnotdef 1.0
/ifnotdef { %def
    1 index where { pop pop pop }{ def } ifelse
} bind def
%%EndProcSet: ifnotdef 1.0

%%BeginProcSet: adobe_packing 1.0
/setpacking { %def
    pop
} bind ifnotdef
/currentpacking { %
    false
} bind ifnotdef
%%EndProcSet: adobe_packing 1.0

%%BeginProcSet: adobe_colorops 1.0
/adobe_colorops 1.0 def		% version number

% w h bits matrix proc0 [...procncolors-1] multiproc ncolors colorimage -

% The "colorimage" operator takes composite color data and renders it to
% a full-color device.  In order to simulate this on a grayscale device,
% the "image" operator must be used.  The basic approach is to discard
% a great deal of the data.  In the RGB case, we throw out 2/3 of the data.
% in the CMYK case, we throw out 3/4 of the data.  The remaining data is
% used as the grayscale argument to "image".  The data is thrown away in
% as reasonable a fashion as can be implemented.  For the multi-proc
% cases, it keeps alternating every scan line, throwing away red, then
% green, then blue, etc.  For the single-proc case, the interleaved data
% is not extracted in some reasonable fashion, and the image may not even
% resemble the original.  It will, however, execute correctly and image
% in the correct area on the page.

/colorimage { %def
    (trying to find "ncolors" proc) exch
    dummy exch get
    exch pop exec	%if no errors
} bind def

currentpacking true setpacking

3 dict begin	% the "ncolors" choices are stored as procs in this dict
    1 { %def	% gray scale case
	image
    } bind def
    3 { %def	% RGB case
	/savelevel save def 10 dict begin
	    { %ifelse (multiproc)
		/Bproc exch def /Gproc exch def /Rproc exch def
		1 5 1 roll	% leave "1" on stack
		{ 1 add Rproc Gproc Bproc 3 4 index roll pop pop }
		image pop
	    }{ %else
		/RGBproc exch def
		/mtx exch def /bits exch def /height exch def /width exch def
		width height bits mtx
		{ RGBproc dup length 3 idiv 0 exch getinterval } image
	    } ifelse
	end savelevel restore
    } bind def
    4 { %def	% CMYK case
	/savelevel save def 10 dict begin
	    { %ifelse (multiproc)
		/Kproc exch def /Yproc exch def /Mproc exch def /Cproc exch def
		1 5 1 roll	% leave "1" on stack
		{ 1 add Cproc Mproc Yproc Kproc 4 5 index roll pop pop pop }
		image pop
	    }{ %else
		/CMYKproc exch def
		/mtx exch def /bits exch def /height exch def /width exch def
		width height bits mtx
		{ CMYKproc dup length 4 idiv 0 exch getinterval } image
	    } ifelse
	end savelevel restore
    } bind def
currentdict end
/colorimage load exch 2 exch put	% replace "dummy" with this dict

/setblackgeneration { %def
    pop
} bind def

/currentblackgeneration { %def
    {}    % use null proc
} bind def

/setcmykcolor { %def
    1 sub 4 1 roll
    3 { %repeat
	3 index add neg dup 0 lt {pop 0} if 3 1 roll
    } repeat setrgbcolor pop
} bind def

/currentcmykcolor { %def
    currentrgbcolor	% R G B
    3 { %repeat
	1 exch sub 3 1 roll
    } repeat
    0
} bind def

/setcolorscreen { %def
    setscreen 9 { pop } repeat
} bind def

/currentcolorscreen { %def
    currentscreen 3 copy 6 copy
} bind def

/setcolortransfer { %def
    settransfer pop pop pop
} bind def

/currentcolortransfer { %def
    currenttransfer dup dup dup
} bind def

/setundercolorremoval { %def
    pop
} bind def

/currentundercolorremoval { %def
    {}
} bind def

setpacking	% uses value on stack from currentpacking
%%EndProcSet: adobe_colorops 1.0


-- 
 Glenn Reid				RightBrain Software
 glenn@heaven.woodside.ca.us		NeXT/PostScript developers
 ..{adobe,next}!heaven!glenn		415-851-1785 (fax 851-1470)

brp@howitt.dog.oz.au (Bruce Perkins) (01/24/91)

In article <60410001@hpopd.pwd.hp.com>, bijal@hpopd.pwd.hp.com (Bijal Shah) writes:
> 
> 
> 	Is there any program available that will convert a color PS document
> 	to a grey scales or ordinary PS document suitable for a Laserwriter?
> 


You should find that a color postscript file will print on a standard
PS printer such as a Laserwriter. There is a builtin algorithm for
mapping the colors to grayscales, but I have yet to see how you can
control that mapping (it may not be possible at all!). However.....,

One of the great things about postscript is you can redefine commands.

You could simply convert all set color commands to "black" by inserting 
the following line before any setrgbcolor commands:-

/setrgbcolor {pop pop pop 0 setgray} def

Alternatively you might want to use the RGB components (the top 3
items on the stack) in some algorithm to come up with a grayscale.
An algorithm I have seen is:- 0.3*Red + 0.59*Green + 0.11*Blue

Implementing this could be done by:-

/setrgbcolor {0.11 mul exch 0.59 mul add exch 0.3 mul add setgray} def

I have found occasion where I have needed to do the reverse of what
you want. The code I use for that is similar to:-

%! Pseudocolor for postscript grayscales
%% This postscript code assigns a "pseudo-colormap" to a
%% grayscale postcript file. The entries defined for the
%% postscript array "cmap" can be modified and/or extended 
%% as required. In this example a postscript "0 setgray"
%% is mapped to "0 0 0 setrgbcolor" and "1 setgray" is
%% mapped to "1 1 1 setrgbcolor" 
/cmap[
[0 0 0] [1 0 0] [1 1 0] [0 1 0] [0 0 1] [0 1 1] [1 1 1]
] def cmap length 1 sub /cmapsize exch def
/setgray {cmapsize mul cvi cmap exch get aload pop setrgbcolor} bind def


Hope this helps

Bruce Perkins
CSIRO Division of Geomechanics
Melbourne, Australia

brp@dogmelb.oz