% Copyright (C) 1990, 1991 Aladdin Enterprises. All rights reserved.
% Distributed by Free Software Foundation, Inc.
% This file is part of Ghostscript.
% Ghostscript is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
% to anyone for the consequences of using it or for whether it serves any
% particular purpose or works at all, unless he says so in writing. Refer
% to the Ghostscript General Public License for full details.
% Everyone is granted permission to copy, modify and redistribute
% Ghostscript, but only under the conditions described in the Ghostscript
% General Public License. A copy of this license is supposed to have been
% given to you along with Ghostscript so you can know your rights and
% responsibilities. It should be in a file named COPYING. Among other
% things, the copyright notice and this notice must be preserved on all
% Convert a .ps file to another .ps file containing only a bit image.
% This replaces the current device, writing the output on the file
% instead of to the device. To display the image at a later time,
% simply run the file that was written.
% Initialize, and redefine copypage and showpage.
% Save the showpage operator
/realshowpage /showpage load def
% Define a monochrome palette
(w) file /myfile exch def
myfile (/readimage ) writestring
myfile /readimage load write==only
myfile ( bind def\n) writestring
% Get the device parameters
currentdevice getdeviceprops dicttomark
dup /HWSize get aload pop
% Make a corresponding memory device
devmatrix devwidth devheight monopalette
mydevice setdevice % (does an erasepage)
/rowwidth devwidth 7 add 8 idiv def
/row rowwidth 7 add 8 idiv 8 mul string def % pad for findcommon
/prevrow row length string def
% Replace the definition of showpage
userdict /showpage { ps2idict begin myshowpage end } bind put
% Procedure for reading and displaying
% <width> <height> readimage
3 index 7 add 8 idiv string currentfile exch
% At each iteration of the loop,
% the stack contains <file> <buffer>
1 index token pop % starting index
2 index token pop % count
readhexstring pop pop dup }
/findcommon % prevrow row -> stopindex startindex
{ 8 sub 2 copy 8 getinterval 3 index 2 index 8 getinterval
{ 1 sub 2 copy get 3 index 2 index get
{ 3 index 1 index 8 getinterval 3 index 2 index 8 getinterval
{ 3 index 1 index get 3 index 2 index get
% Write the image on the file
{ myfile devwidth write==only myfile ( ) writestring
myfile devheight write==only myfile ( readimage\n) writestring
0 1 prevrow length 1 sub { prevrow exch 0 put } for
{ mydevice exch row 0 rowwidth getinterval copyscanlines
% stack now has stop index, start index
exch 1 index sub 2 copy exch % start, length, length, start
myfile exch write==only myfile ( ) writestring
myfile exch write==only myfile ( ) writestring
getinterval myfile exch writehexstring
/ps2image { ps2idict begin ps2image end } bind def