home *** CD-ROM | disk | FTP | other *** search
/ jppd.dyndns.org / jppd.dyndns.org.tar / jppd.dyndns.org / QUERYPRO / Actualizar / Impressora_PDF / converter.exe / GPLGS / viewjpeg.ps < prev    next >
Text File  |  2003-04-13  |  5KB  |  160 lines

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