386BSD 0.1 development
[unix-history] / usr / othersrc / public / ghostscript-2.4.1 / ps2image.ps
CommitLineData
c04c7506
WJ
1% Copyright (C) 1990, 1991 Aladdin Enterprises. All rights reserved.
2% Distributed by Free Software Foundation, Inc.
3%
4% This file is part of Ghostscript.
5%
6% Ghostscript is distributed in the hope that it will be useful, but
7% WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
8% to anyone for the consequences of using it or for whether it serves any
9% particular purpose or works at all, unless he says so in writing. Refer
10% to the Ghostscript General Public License for full details.
11%
12% Everyone is granted permission to copy, modify and redistribute
13% Ghostscript, but only under the conditions described in the Ghostscript
14% General Public License. A copy of this license is supposed to have been
15% given to you along with Ghostscript so you can know your rights and
16% responsibilities. It should be in a file named COPYING. Among other
17% things, the copyright notice and this notice must be preserved on all
18% copies.
19
20% Convert a .ps file to another .ps file containing only a bit image.
21% Usage:
22% (filename) ps2image
23% This replaces the current device, writing the output on the file
24% instead of to the device. To display the image at a later time,
25% simply run the file that was written.
26
27% Initialize, and redefine copypage and showpage.
28/ps2idict 25 dict def
29ps2idict begin
30 % Save the showpage operator
31 /realshowpage /showpage load def
32 % Define a monochrome palette
33 /monopalette <00 ff> def
34 % The main procedure
35 /ps2image
36 { % Open the file
37 (w) file /myfile exch def
38 myfile (/readimage ) writestring
39 myfile /readimage load write==only
40 myfile ( bind def\n) writestring
41 % Get the device parameters
42 currentdevice getdeviceprops dicttomark
43 dup /HWSize get aload pop
44 /devheight exch def
45 /devwidth exch def
46 /InitialMatrix get
47 /devmatrix exch def
48 % Make a corresponding memory device
49 devmatrix devwidth devheight monopalette
50 makeimagedevice
51 /mydevice exch def
52 mydevice setdevice % (does an erasepage)
53 /rowwidth devwidth 7 add 8 idiv def
54 /row rowwidth 7 add 8 idiv 8 mul string def % pad for findcommon
55 /prevrow row length string def
56 % Replace the definition of showpage
57 userdict /showpage { ps2idict begin myshowpage end } bind put
58 } def
59 % Procedure for reading and displaying
60 % the rendered image.
61 % <width> <height> readimage
62 /readimage
63 { gsave matrix setmatrix
64 1 matrix
65 3 index 7 add 8 idiv string currentfile exch
66 % At each iteration of the loop,
67 % the stack contains <file> <buffer>
68 { 2 copy
69 1 index token pop % starting index
70 2 index token pop % count
71 getinterval
72 readhexstring pop pop dup }
73 3 1 roll
74 7 2 roll
75 image pop pop
76 grestore showpage
77 } def
78/findcommon % prevrow row -> stopindex startindex
79 { 2 copy eq
80 { 0 0 }
81 { dup length
82 { 8 sub 2 copy 8 getinterval 3 index 2 index 8 getinterval
83 ne { 8 add exit } if
84 } loop
85 { 1 sub 2 copy get 3 index 2 index get
86 ne { 1 add exit } if
87 } loop
88 0
89 { 3 index 1 index 8 getinterval 3 index 2 index 8 getinterval
90 ne { exit } if
91 8 add
92 } loop
93 { 3 index 1 index get 3 index 2 index get
94 ne { exit } if
95 1 add
96 } loop
97 }
98 ifelse
99 4 2 roll pop pop
100 } bind def
101 % Write the image on the file
102 /myshowpage
103 { myfile devwidth write==only myfile ( ) writestring
104 myfile devheight write==only myfile ( readimage\n) writestring
105 0 1 prevrow length 1 sub { prevrow exch 0 put } for
106 % Write the hex data
107 0 1 devheight 1 sub
108 { mydevice exch row 0 rowwidth getinterval copyscanlines
109 prevrow row findcommon
110 % stack now has stop index, start index
111 exch 1 index sub 2 copy exch % start, length, length, start
112 myfile exch write==only myfile ( ) writestring
113 myfile exch write==only myfile ( ) writestring
114 getinterval myfile exch writehexstring
115 row prevrow copy pop
116 myfile (\n) writestring
117 } for
118 myfile flushfile
119 erasepage initgraphics
120 } bind def
121
122end
123
124/ps2image { ps2idict begin ps2image end } bind def