1 %! viewjpeg.ps Copyright (C) 1994 Thomas Merz <tm@pdflib.com>
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: viewjpeg.ps,v 1.6 2003/04/12 18:08:18 ray Exp $
17 % View JPEG files with Ghostscript
19 % This PostScript code relies on level 2 features.
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.
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/
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
39 % (jpeg-6/testimg.jpg) viewJPEG
41 /languagelevel where {pop languagelevel 2 lt}{true} ifelse {
42 (JPEG needs PostScript Level 2!\n) print flush stop
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
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
56 % Names of color spaces
57 /ColorSpaceNames << /1 /DeviceGray /3 /DeviceRGB /4 /DeviceCMYK >> def
59 % read one byte from file F
60 % - ==> int --or-- stop context
62 F read not { (Read error in ViewJPEG!\n) print flush stop } if
65 /SkipSegment { % read two bytes and skip that much data
66 NextByte 8 bitshift NextByte add 2 sub { NextByte pop } repeat
69 % read width, height, and # of components from JPEG markers
71 /readJPEGmarkers { % - ==> dict --or-- stop context
74 { % loop: read JPEG marker segments until we find SOFn marker or EOF
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
83 (Error: not 8 bits per component!\n) print flush stop
86 % Read crucial image parameters
87 /height NextByte 8 bitshift NextByte add def
88 /width NextByte 8 bitshift NextByte add def
91 VJPGDEBUG { currentdict { exch == == } forall flush } if
95 % detect several segment types which are not compatible with PS
98 (Marker ) print markertype ==
99 (not supported!\n) print flush stop
103 % Skip segment if marker has parameters associated with it
104 true NoParamMarkers { markertype eq {pop false exit} if } forall
109 currentdict dup /markertype undef
115 % read image parameters from JPEG file and display the image
116 /viewJPEG { % <file|string> ==> -
120 /scratch 1 string def
121 dup type /stringtype eq { (r) file } if
124 readJPEGmarkers begin
125 F 0 setfileposition % reset file pointer
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
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
139 ColorSpaceNames colors scratch cvs get setcolorspace
141 % prepare image dictionary
145 /ImageMatrix [ width 0 0 height neg 0 height ]
147 % If 4-component (CMYK), assume data is inverted per Adobe Photoshop
149 /Decode [ colors { 1 0 } repeat ]
151 /Decode [ colors { 0 1 } repeat ]
153 /DataSource F /DCTDecode filter
156 end % image parameter dictionary