]> git.lizzy.rs Git - plan9front.git/blob - sys/lib/ghostscript/viewmiff.ps
merge
[plan9front.git] / sys / lib / ghostscript / viewmiff.ps
1 %    Copyright (C) 1998 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: viewmiff.ps,v 1.4 2002/02/21 21:49:28 giles Exp $
17 % viewmiff.ps
18 % Display a MIFF file.  You would think the 'display' command would do this,
19 % but many versions of 'display' either core-dump or require unacceptably
20 % large amounts of memory.
21
22 % Recognize MIFF keywords.
23 /miffwords mark
24   /class { cvn /class exch def }
25   /colors { cvi /colors exch def }
26   /columns { cvi /Width exch def }
27   /compression { cvn /compression exch def }
28   /depth { cvi /depth exch def }
29   /packets { cvi /packets exch def }
30   /rows { cvi /Height exch def }
31 .dicttomark readonly def
32
33 % Recognize MIFF image classes.
34 /miffclasses mark
35   /DirectClass {
36     /DeviceRGB setcolorspace
37     /BitsPerComponent depth def
38     /Decode [ 0 1 0 1 0 1 ] def
39   }
40   /PseudoClass {
41     [ /Indexed
42                 % The MIFF documentation lies about the size of pixels
43                 % for this case: the pixel size is determined only by
44                 % the number of colors, and is not affected by the image
45                 % depth.  Specifically, if there are 256 or fewer colors
46                 % but the depth (of color map entries) is 16, each pixel
47                 % is still only 1 byte, not 2.
48       currentdict /colors known {
49         /DeviceRGB colors 1 sub
50         /BitsPerComponent colors 256 le { 8 } { 16 } ifelse def
51         colors 3 mul string depth 8 eq {
52           f exch readstring pop
53         } {
54                 % 16-bit color map entries: take only the high-order byte.
55           0 1 2 index length 1 sub {
56             f read pop 2 index 3 1 roll put f read pop pop
57           } for
58         } ifelse
59       } {
60         /colors 256 def
61         /DeviceGray 255
62         256 string 0 1 255 { 1 index exch dup put } for
63       } ifelse
64     ] setcolorspace
65     /Decode [ 0 1 BitsPerComponent bitshift 1 sub ] def
66   }
67 .dicttomark readonly def
68
69 % Recognize MIFF compression methods.
70 /rlstring 768 string def
71 /rlread {
72                 % packets is not reliable -- disregard it.
73   dup rlstring 0 3 getinterval readstring {
74     pop read pop 3 mul 3 3 2 index {
75       rlstring exch rlstring 0 3 getinterval putinterval
76     } for
77     rlstring 0 3 -1 roll 3 add getinterval
78   } {
79     pop pop ()
80   } ifelse
81 } bind def
82 /miffcompress mark
83   /Uncompressed { f }
84   /RunLengthEncoded { { f rlread } }
85   /Zip { [ f /FlateDecode filter cvlit /rlread cvx ] cvx }
86 .dicttomark readonly def
87
88 % Read a MIFF file and display the image.
89 /viewmiff {             % <filename> viewmiff -
90   50 dict begin
91   /fname 1 index def
92   /f exch (r) file def
93                 % Set defaults.
94   /ImageType 1 def
95   /class /DirectClass def
96   /compression /Uncompressed def
97   /depth 8 def
98   /packets 16#7fffffff def
99                 % Read and parse the header.
100   { f token pop
101     dup (:) eq { pop exit } if
102     dup type /nametype eq {
103       .namestring (=) search {
104         exch pop miffwords exch .knownget { exec } { pop } ifelse
105       } {
106         pop     % who knows?
107       } ifelse
108     } {
109       pop       % probably a comment in braces
110     } ifelse
111   } loop
112                 % Read and display the image.
113   miffclasses class get exec
114   /DataSource miffcompress compression get exec def
115   /ImageMatrix [Width 0 0 Height neg 0 Height] def
116   currentpagedevice /PageSize get
117     dup 0 get exch 1 get scale
118   gsave 0.8 setgray 0 0 1 1 rectfill grestore   % provide background
119   currentdict image
120   showpage
121                 % Clean up.
122   f closefile
123   end
124 } bind def