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