1 % Copyright (C) 1992, 1995, 1996, 1998, 1999 Aladdin Enterprises. All rights reserved.
3 % This software is provided AS-IS with no warranty, either express or
6 % This software is distributed under license and may not be copied,
7 % modified or distributed except as expressly authorized under the terms
8 % of the license contained in the file LICENSE in this distribution.
10 % For more information about licensing, please refer to
11 % http://www.ghostscript.com/licensing/. For information on
12 % commercial licensing, go to http://www.artifex.com/licensing/ or
13 % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14 % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
16 % $Id: viewpbm.ps,v 1.5 2002/06/02 12:03:28 mpsuzuki Exp $
18 % Display a PBM/PGM/PPM file.
19 % Requires the Level 2 `image' operator (to handle variable pixel widths).
20 % If SCALE is defined, maps input pixels to output pixels with that scale;
21 % if SCALE is undefined, scales the image to fit the page.
24 /readmaxv { % <file> readmaxv -
25 10 string readline pop cvx exec /maxv exch def
27 /readrow { % <file> <row> readrow <row>
28 0 1 2 index length 1 sub {
29 1 index exch 3 index token pop put
32 /read01 { % <file> <count> read01 <byte>
34 1 index read pop 48 xor dup 1 le { exch dup add add } { pop } ifelse
37 /readrow01 { % <file> <row> readrow01 <row>
39 1 index exch 3 index 8 read01 put
42 dup rsize 1 sub wrem read01 8 wrem sub bitshift put
46 /readwh { % <file> readwh <w> <h>
47 dup s readline pop % check for comment
49 pop pop dup s readline pop
54 % The procedures in this dictionary are called as
55 % <file> Pn <w> <h> <readproc>
56 /P1 { % ASCII 1-bit white/black
57 /bpc 1 def /maxv 1 def /rsize w 7 add 8 idiv def
59 /ncomp 1 def /invert true def /DeviceGray setcolorspace
63 /P2 { % ASCII 8-bit gray
65 /bpc 8 def 2 index readmaxv /rsize 2 index def
66 /ncomp 1 def /invert false def /DeviceGray setcolorspace
69 /P3 { % ASCII 8-bit RGB
71 /bpc 8 def 2 index readmaxv /rsize 2 index 3 mul def
72 /ncomp 3 def /invert false def /DeviceRGB setcolorspace
75 /P4 { % Binary 1-bit white/black
77 /bpc 1 def /maxv 1 def /rsize 2 index 7 add 8 idiv def
78 /ncomp 1 def /invert true def /DeviceGray setcolorspace
81 /P5 { % Binary 8-bit gray
83 /bpc 8 def 2 index readmaxv /rsize 2 index def
84 /ncomp 1 def /invert false def /DeviceGray setcolorspace
87 /P6 { % Binary 8-bit RGB
89 /bpc 8 def 2 index readmaxv /rsize 2 index 3 mul def
90 /ncomp 3 def /invert false def /DeviceRGB setcolorspace
93 .dicttomark readonly def
94 /pbmsetup { % <file> <w> <h> <readproc> runpbm -
99 20 dict begin % image dictionary
103 /ImageMatrix [w 0 0 h neg 0 h] def
104 /BitsPerComponent bpc def
105 /Decode [ 0 255 maxv div invert { exch } if ncomp 1 sub { 2 copy } repeat ] def
106 /DataSource [ f rsize string /readproc load /exec load ] cvx def
109 /imagescale { % <imagedict> imagescale -
113 % Map pixels SCALE-for-1. Assume orthogonal transformation.
114 Width 1 0 dtransform add abs div SCALE mul
115 Height 0 1 dtransform add abs div SCALE mul
117 % Scale the image (uniformly) to fit the page.
118 clippath pathbbox pop pop translate
119 pathbbox .min exch pop exch pop ceiling
120 dup Height Width gt {
121 Width mul Height div exch
130 % Image a PBM file page by page.
131 /viewpbm { % <filename> viewpbm -
133 (r) file /pf exch def {
134 pf token not { exit } if
135 pbmtypes exch get pf exch exec pbmsetup
136 dup imagescale image showpage
141 % Reassemble a composite PBM file from the CMYK separations.
147 /pf fname (r) file def
148 pf pbmtypes pf token pop get exec
149 % Stack: pf w h readproc
151 /readproc exch def /h exch def /w exch def pop
152 /row rsize string def
153 h { pf row readproc pop } repeat
154 pf pbmtypes pf token pop get exec
158 /datas [ sources { /DataSource get 0 get } forall ] def
159 /decode sources 0 get /Decode get
160 dup 0 get exch 1 get add cvi 0 exch
161 2 copy 4 copy 8 array astore def
163 dup /MultipleDataSources true put
164 dup /DataSource datas put
165 dup /Decode decode put
166 /DeviceCMYK setcolorspace
167 dup imagescale image showpage
171 % If the program was invoked from the command line, run it now.
174 { ] { viewpbm } forall
177 (Usage: gs [--] viewpbm.ps filename.p*m ...\n) print
178 ( e.g.: gs [--] viewpbm.ps my.ppm another.ppm\n) print flush