home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gs333ini / gs3.33 / viewjpeg.ps < prev    next >
Text File  |  1995-12-09  |  4KB  |  136 lines

  1. %! viewjpeg.ps   Copyright (C) Thomas Merz 1994
  2. %
  3. % View JPEG files with Ghostscript
  4. %
  5. % This PostScript code relies on level 2 features.
  6. %
  7. % Only JPEG baseline and extended sequential 
  8. % coded images are supported (as defined in PostScript level 2)
  9. %
  10. % Author's address:
  11. % ------------------------------+
  12. % {(pstack exec quit) = flush } |    Thomas Merz
  13. % pstack exec quit              |    InterFace Connection GmbH
  14. % ------------------------------+    phone: +49/89/6 10 49-211
  15. % Leipziger Str. 16                  fax:   +49/89/6 10 49-85
  16. % D-82008 Unterhaching, FRG          internet: thomas@ifconnection.de
  17.  
  18. /languagelevel where {pop languagelevel 2 lt}{true} ifelse {
  19.   (JPEG needs PostScript Level 2!\n) print flush stop
  20. } if
  21.  
  22. /JPEGdict 20 dict def
  23. JPEGdict begin
  24.  
  25. /NoParamMarkers [    % JPEG markers without additional parameters
  26.     16#D0 16#D1 16#D2 16#D3 16#D4 16#D5 16#D6 16#D7 16#D8 16#01
  27. ] def
  28.  
  29. /NotSupportedMarkers [     % JPEG markers not supported by PostScript level 2
  30.     16#C1 16#C2 16#C3 16#C5 16#C6 16#C7 16#C8 
  31.     16#C9 16#CA 16#CB 16#CD 16#CE 16#CF
  32. ] def
  33.  
  34. % Names of color spaces
  35. /ColorSpaceNames << /1 /DeviceGray /3 /DeviceRGB /4 /DeviceCMYK >> def
  36.  
  37. % read one byte from file F
  38. % - ==> int --or-- stop context
  39. /NextByte { 
  40.     F read not { (Read error in ViewJPEG!\n) print flush stop } if
  41. } bind def
  42.  
  43. /SkipSegment {    % read two bytes and skip that much data
  44.     NextByte 8 bitshift NextByte add 2 sub { NextByte pop } repeat
  45. } bind def
  46.  
  47. % read width, height, and # of components from JPEG markers
  48. % and store in dict
  49. /readJPEGmarkers {    % - ==> dict --or-- stop context
  50.     5 dict begin
  51.  
  52.     { % loop: read JPEG marker segments until we find baseline/ext. seq.
  53.       % markers or EOF
  54.     NextByte
  55.     16#FF eq {                % found marker
  56.         /markertype NextByte def
  57.         % baseline or extended sequential?
  58.         markertype dup 16#C0 eq exch 16#C1 eq or {
  59.         NextByte pop NextByte pop    % segment length
  60.         NextByte 8 ne {
  61.             (Error: not 8 bits per component!\n) print flush stop 
  62.         } if
  63.  
  64.         % Read crucial image parameters
  65.         /height NextByte 8 bitshift NextByte add def
  66.         /width NextByte 8 bitshift NextByte add def
  67.         /colors NextByte def
  68.  
  69.         DEBUG { currentdict { exch == == } forall flush } if
  70.         exit
  71.         } if
  72.  
  73.         % detect several segment types which are not compatible with PS
  74.         NotSupportedMarkers {
  75.         markertype eq {
  76.             (Marker ) print markertype == 
  77.             (not supported!\n) print flush stop
  78.         } if 
  79.         } forall 
  80.  
  81.         % Skip segment if marker has parameters associated with it
  82.         true NoParamMarkers { markertype eq {pop false exit} if } forall 
  83.         { SkipSegment } if
  84.     } if
  85.     } loop
  86.  
  87.     currentdict dup /markertype undef
  88.     end
  89. } bind def
  90.  
  91. end    % JPEGdict
  92.  
  93. % read image parameters from JPEG file and display the image
  94. /viewJPEG {        % <file|string> ==> -
  95.     save 
  96.     JPEGdict begin
  97.     /saved exch def
  98.     /scratch 1 string def
  99.     dup type /stringtype eq { (r) file } if
  100.     /F exch def
  101.  
  102.     readJPEGmarkers begin
  103.     F 0 setfileposition        % reset file pointer
  104.  
  105.     % We use the whole clipping area for the image (at least in one dimension)
  106.     gsave clippath pathbbox grestore
  107.     /ury exch def /urx exch def
  108.     /lly exch def /llx exch def
  109.  
  110.     llx lly translate
  111.     width height scale
  112.  
  113.     % use whole width or height, whichever is appropriate
  114.     urx llx sub width div ury lly sub height div
  115.     2 copy gt { exch } if pop        % min
  116.     dup scale
  117.     ColorSpaceNames colors scratch cvs get setcolorspace
  118.  
  119.     % prepare image dictionary
  120.     << /ImageType 1
  121.        /Width width
  122.        /Height height
  123.        /ImageMatrix [ width 0 0 height neg 0 height ]
  124.        /BitsPerComponent 8
  125.        /Decode [ colors { 0 1 } repeat ]
  126.        /DataSource F /DCTDecode filter
  127.     >> image
  128.  
  129.     end        % image parameter dictionary
  130.  
  131.     saved end restore
  132. } bind def
  133.  
  134. % usage example:
  135. % (jpeg-5a/testimg.jpg) viewJPEG
  136.