1 % Copyright (C) 1996, 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: viewpcx.ps,v 1.5 2002/06/02 12:03:28 mpsuzuki Exp $
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 % ****NOTE: does not handle multi-plane images with palette.
26 64 string exch 0 1 63 {
31 /readpcx { % - readpcx <str>
37 ( ) dup 0 4 -1 roll put exch pop
39 192 sub //pcxbytes 3 -1 roll read pop get exch 0 exch getinterval
43 /get2 % <string> <index> get2 <int>
44 { 2 copy get 3 1 roll 1 add get 8 bitshift add
47 { df s readstring pop % s gets filled in
48 s1 () ne { df s1 readstring pop pop } if % discard padding bytes
49 } def % don't bind, must be writable
50 /viewpcx % <filename> viewpcx -
54 % Read and unpack the header.
55 /header f 128 string readstring pop def
56 /version header 1 get def
58 /w header 8 get2 header 4 get2 sub 1 add def
59 /h header 10 get2 header 6 get2 sub 1 add def
60 /nplanes header 65 get def
61 /bpl header 66 get2 def
62 /palinfo header 68 get2 def
63 /nbits bpp nplanes mul def
67 [/Indexed /DeviceRGB 1 bpp bitshift 1 sub
69 1 nbits bitshift 3 mul string
70 fname status pop pop pop exch pop
71 1 index length sub f exch setfileposition
73 exch f exch setfileposition
75 /decode [0 cspace 2 get] def
77 { /cspace /DeviceRGB def
78 /decode [0 1 0 1 0 1] def
83 [/Indexed /DeviceRGB 1 bpp bitshift 1 sub
84 header 16 1 nbits bitshift 16 .min 3 mul getinterval
86 /decode [0 cspace 2 get] def
92 % Map pixels SCALE-for-1. Assume orthogonal transformation.
93 w 1 0 dtransform add abs div SCALE mul
94 h 0 1 dtransform add abs div SCALE mul
96 { % Scale the image (uniformly) to fit the page.
97 clippath pathbbox pop pop translate
98 pathbbox .min exch pop exch pop ceiling
99 dup h w gt { w mul h div exch } { h mul w div } ifelse
102 % Since the number of bytes per line is always even,
103 % it may not match the width specification.
104 /wbpl w bpp mul 7 add 8 idiv def
105 % Define the data source procedure.
106 /s1 bpl wbpl sub string def
107 /df /readpcx load copyarray dup 0 f put cvx bind readonly
108 0 () /SubFileDecode filter def
110 { /dsproc load copyarray
111 dup 1 wbpl string put
115 % Construct the image dictionary.
116 20 dict begin % image dictionary
120 /ImageMatrix [w 0 0 h neg 0 h] def
121 /BitsPerComponent bpp def
123 /DataSource dsource dup length 1 gt
124 { /MultipleDataSources true def }
128 % Finally, display the image.
137 % If the program was invoked from the command line, run it now.
140 { ] { viewpcx } forall
143 (Usage: gs -- viewpcx.ps filename.pcx ...\n) print
144 ( e.g.: gs -- viewpcx.ps my.pcx another.pcx\n) print flush