viewjpeg.ps 4.8 KB

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