]> git.lizzy.rs Git - plan9front.git/blob - sys/lib/ghostscript/viewpbm.ps
etherbcm: handle 64-bit host addresses, use PCIWADDR() instead of PADDR()
[plan9front.git] / sys / lib / ghostscript / viewpbm.ps
1 %    Copyright (C) 1992, 1995, 1996, 1998, 1999 Aladdin Enterprises.  All rights reserved.
2
3 % This software is provided AS-IS with no warranty, either express or
4 % implied.
5
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.
9
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.
15
16 % $Id: viewpbm.ps,v 1.5 2002/06/02 12:03:28 mpsuzuki Exp $
17 % viewpbm.ps
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.
22
23 /s 100 string def
24 /readmaxv {             % <file> readmaxv -
25   10 string readline pop cvx exec /maxv exch def
26 } bind def
27 /readrow {              % <file> <row> readrow <row>
28   0 1 2 index length 1 sub {
29     1 index exch 3 index token pop put
30   } for exch pop
31 } bind def
32 /read01 {               % <file> <count> read01 <byte>
33   0 exch {
34     1 index read pop 48 xor dup 1 le { exch dup add add } { pop } ifelse
35   } repeat
36 } bind def
37 /readrow01 {            % <file> <row> readrow01 <row>
38   0 1 w 8 idiv {
39     1 index exch 3 index 8 read01 put
40   } for
41   wrem 0 ne {
42      dup rsize 1 sub wrem read01 8 wrem sub bitshift put
43   } if
44   exch pop
45 } bind def
46 /readwh {               % <file> readwh <w> <h>
47  dup s readline pop             % check for comment
48  (#) anchorsearch {
49    pop pop dup s readline pop
50  } if
51  cvx exec
52 } bind def
53 /pbmtypes mark
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
58   /wrem w 8 mod def
59   /ncomp 1 def /invert true def /DeviceGray setcolorspace
60   readwh
61         { readrow01 }
62 } bind
63 /P2 {                   % ASCII 8-bit gray
64   readwh
65   /bpc 8 def 2 index readmaxv /rsize 2 index def
66   /ncomp 1 def /invert false def /DeviceGray setcolorspace
67         { readrow }
68 } bind
69 /P3 {                   % ASCII 8-bit RGB
70   readwh
71   /bpc 8 def 2 index readmaxv /rsize 2 index 3 mul def
72   /ncomp 3 def /invert false def /DeviceRGB setcolorspace
73         { readrow }
74 } bind
75 /P4 {                   % Binary 1-bit white/black
76   readwh
77   /bpc 1 def /maxv 1 def /rsize 2 index 7 add 8 idiv def
78   /ncomp 1 def /invert true def /DeviceGray setcolorspace
79         { readstring pop }
80 } bind
81 /P5 {                   % Binary 8-bit gray
82   readwh
83   /bpc 8 def 2 index readmaxv /rsize 2 index def
84   /ncomp 1 def /invert false def /DeviceGray setcolorspace
85         { readstring pop }
86 } bind
87 /P6 {                   % Binary 8-bit RGB
88   readwh
89   /bpc 8 def 2 index readmaxv /rsize 2 index 3 mul def
90   /ncomp 3 def /invert false def /DeviceRGB setcolorspace
91         { readstring pop }
92 } bind
93 .dicttomark readonly def
94 /pbmsetup {                     % <file> <w> <h> <readproc> runpbm -
95    /readproc exch def
96    /h exch def
97    /w exch def
98    /f exch def
99    20 dict begin                % image dictionary
100      /ImageType 1 def
101      /Width w def
102      /Height h def
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
107    currentdict end
108 } def
109 /imagescale {                   % <imagedict> imagescale -
110   begin
111     /SCALE where {
112       pop
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
116     } {
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
122       } {
123         Height mul Width div
124       } ifelse
125     }
126     ifelse scale
127   end
128 } def
129
130 % Image a PBM file page by page.
131 /viewpbm {                      % <filename> viewpbm -
132   20 dict begin
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
137     } loop
138   end
139 } def
140
141 % Reassemble a composite PBM file from the CMYK separations.
142 /viewpsm {
143   20 dict begin
144     /fname exch def
145     /sources [ 0 1 3 {
146       /plane exch def 
147       /pf fname (r) file def
148       pf pbmtypes pf token pop get exec
149                 % Stack: pf w h readproc
150       plane {
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
155       } repeat
156       pbmsetup
157     } for ] def
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
162     sources 0 get
163       dup /MultipleDataSources true put
164       dup /DataSource datas put
165       dup /Decode decode put
166     /DeviceCMYK setcolorspace
167     dup imagescale image showpage
168   end
169 } def
170
171 % If the program was invoked from the command line, run it now.
172 [ shellarguments
173  { counttomark 1 ge
174     { ] { viewpbm } forall
175     }
176     { cleartomark
177       (Usage: gs [--] viewpbm.ps filename.p*m ...\n) print
178       ( e.g.: gs [--] viewpbm.ps my.ppm another.ppm\n) print flush
179     }
180    ifelse
181  }
182  { pop
183  }
184 ifelse