]> git.lizzy.rs Git - plan9front.git/blob - sys/lib/ghostscript/viewjpeg.ps
update colemak kbmap, simpler version with scroll working (thanks jeremy)
[plan9front.git] / sys / lib / ghostscript / viewjpeg.ps
1 %! viewjpeg.ps   Copyright (C) 1994 Thomas Merz <tm@pdflib.com>
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: viewjpeg.ps,v 1.6 2003/04/12 18:08:18 ray Exp $
17 % View JPEG files with Ghostscript
18 %
19 % This PostScript code relies on level 2 features.
20 %
21 % Only JPEG baseline, extended sequential, and progressive files
22 % are supported.  Note that Adobe PostScript level 2 does not include
23 % progressive-JPEG support.  Ghostscript with IJG JPEG v6 or later
24 % will decode progressive JPEG, but only if you edit gsjmorec.h to
25 % enable that feature.
26 %
27 % Author's address:
28 % ------------------------------+
29 % {(pstack exec quit) = flush } |    Thomas Merz, Munich
30 % pstack exec quit              |    voice +49/89/29160728
31 % ------------------------------+    tm@muc.de  http://www.muc.de/~tm/
32 %
33 % $Id: viewjpeg.ps,v 1.6 2003/04/12 18:08:18 ray Exp $
34 % Updated by L. Peter Deutsch 20-May-1997:
35 %   move the usage example to the beginning
36 % Updates by Tom Lane 6-Sep-1995
37
38 % Usage example:
39 %       (jpeg-6/testimg.jpg) viewJPEG
40
41 /languagelevel where {pop languagelevel 2 lt}{true} ifelse {
42   (JPEG needs PostScript Level 2!\n) print flush stop
43 } if
44
45 /JPEGdict 20 dict def
46 JPEGdict begin
47
48 /NoParamMarkers [       % JPEG markers without additional parameters
49     16#D0 16#D1 16#D2 16#D3 16#D4 16#D5 16#D6 16#D7 16#D8 16#01
50 ] def
51
52 /NotSupportedMarkers [  % JPEG markers not supported by PostScript level 2
53     16#C3 16#C5 16#C6 16#C7 16#C8 16#C9 16#CA 16#CB 16#CD 16#CE 16#CF
54 ] def
55
56 % Names of color spaces
57 /ColorSpaceNames << /1 /DeviceGray /3 /DeviceRGB /4 /DeviceCMYK >> def
58
59 % read one byte from file F
60 % - ==> int --or-- stop context
61 /NextByte { 
62     F read not { (Read error in ViewJPEG!\n) print flush stop } if
63 } bind def
64
65 /SkipSegment {  % read two bytes and skip that much data
66     NextByte 8 bitshift NextByte add 2 sub { NextByte pop } repeat
67 } bind def
68
69 % read width, height, and # of components from JPEG markers
70 % and store in dict
71 /readJPEGmarkers {      % - ==> dict --or-- stop context
72     5 dict begin
73
74     { % loop: read JPEG marker segments until we find SOFn marker or EOF
75         NextByte
76         16#FF eq {                              % found marker
77             /markertype NextByte def
78             % Is it S0F0=baseline, SOF1=extended sequential, SOF2=progressive ?
79             markertype dup 16#C0 ge exch 16#C2 le and {
80                 NextByte pop NextByte pop       % segment length
81                 % Ghostscript and Adobe PS accept only data precision 8
82                 NextByte 8 ne {
83                     (Error: not 8 bits per component!\n) print flush stop 
84                 } if
85
86                 % Read crucial image parameters
87                 /height NextByte 8 bitshift NextByte add def
88                 /width NextByte 8 bitshift NextByte add def
89                 /colors NextByte def
90
91                 VJPGDEBUG { currentdict { exch == == } forall flush } if
92                 exit
93             } if
94
95             % detect several segment types which are not compatible with PS
96             NotSupportedMarkers {
97                 markertype eq {
98                     (Marker ) print markertype == 
99                     (not supported!\n) print flush stop
100                 } if 
101             } forall 
102
103             % Skip segment if marker has parameters associated with it
104             true NoParamMarkers { markertype eq {pop false exit} if } forall 
105             { SkipSegment } if
106         } if
107     } loop
108
109     currentdict dup /markertype undef
110     end
111 } bind def
112
113 end     % JPEGdict
114
115 % read image parameters from JPEG file and display the image
116 /viewJPEG {             % <file|string> ==> -
117     save 
118     JPEGdict begin
119     /saved exch def
120     /scratch 1 string def
121     dup type /stringtype eq { (r) file } if
122     /F exch def
123
124     readJPEGmarkers begin
125     F 0 setfileposition         % reset file pointer
126
127     % We use the whole clipping area for the image (at least in one dimension)
128     gsave clippath pathbbox grestore
129     /ury exch def /urx exch def
130     /lly exch def /llx exch def
131
132     llx lly translate
133     width height scale
134
135     % use whole width or height, whichever is appropriate
136     urx llx sub width div ury lly sub height div
137     2 copy gt { exch } if pop           % min
138     dup scale
139     ColorSpaceNames colors scratch cvs get setcolorspace
140
141     % prepare image dictionary
142     << /ImageType 1
143        /Width width
144        /Height height
145        /ImageMatrix [ width 0 0 height neg 0 height ]
146        /BitsPerComponent 8
147        % If 4-component (CMYK), assume data is inverted per Adobe Photoshop
148        colors 4 eq {
149          /Decode [ colors { 1 0 } repeat ]
150        } {
151          /Decode [ colors { 0 1 } repeat ]
152        } ifelse
153        /DataSource F /DCTDecode filter
154     >> image
155
156     end         % image parameter dictionary
157
158     saved end restore
159 } bind def