You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.7 KiB

1 month ago
  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., 39 Mesa Street, Suite 108A, San Francisco,
  14. % CA 94129, USA.
  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. % Updated by L. Peter Deutsch 20-May-1997:
  32. % move the usage example to the beginning
  33. % Updates by Tom Lane 6-Sep-1995
  34. % Usage example:
  35. % (jpeg-6/testimg.jpg) viewJPEG
  36. % From version 9.50 you must supply permissions for this program
  37. % to read the input file(s) either by using -dNOSAFER or by
  38. % supplying --permit-file-read=<filename>
  39. /languagelevel where {pop languagelevel 2 lt}{true} ifelse {
  40. (JPEG needs PostScript Level 2!\n) print flush stop
  41. } if
  42. /JPEGdict 20 dict def
  43. JPEGdict begin
  44. /NoParamMarkers [ % JPEG markers without additional parameters
  45. 16#D0 16#D1 16#D2 16#D3 16#D4 16#D5 16#D6 16#D7 16#D8 16#01
  46. ] def
  47. /NotSupportedMarkers [ % JPEG markers not supported by PostScript level 2
  48. 16#C3 16#C5 16#C6 16#C7 16#C8 16#C9 16#CA 16#CB 16#CD 16#CE 16#CF
  49. ] def
  50. % Names of color spaces
  51. /ColorSpaceNames << /1 /DeviceGray /3 /DeviceRGB /4 /DeviceCMYK >> def
  52. % read one byte from file F
  53. % - ==> int --or-- stop context
  54. /NextByte {
  55. F read not { (Read error in ViewJPEG!\n) print flush stop } if
  56. } bind def
  57. /SkipSegment { % read two bytes and skip that much data
  58. NextByte 8 bitshift NextByte add 2 sub { NextByte pop } repeat
  59. } bind def
  60. % read width, height, and # of components from JPEG markers
  61. % and store in dict
  62. /readJPEGmarkers { % - ==> dict --or-- stop context
  63. 5 dict begin
  64. { % loop: read JPEG marker segments until we find SOFn marker or EOF
  65. NextByte
  66. 16#FF eq { % found marker
  67. /markertype NextByte def
  68. % Is it S0F0=baseline, SOF1=extended sequential, SOF2=progressive ?
  69. markertype dup 16#C0 ge exch 16#C2 le and {
  70. NextByte pop NextByte pop % segment length
  71. % Ghostscript and Adobe PS accept only data precision 8
  72. NextByte 8 ne {
  73. (Error: not 8 bits per component!\n) print flush stop
  74. } if
  75. % Read crucial image parameters
  76. /height NextByte 8 bitshift NextByte add def
  77. /width NextByte 8 bitshift NextByte add def
  78. /colors NextByte def
  79. VJPGDEBUG { currentdict { exch == == } forall flush } if
  80. exit
  81. } if
  82. % detect several segment types which are not compatible with PS
  83. NotSupportedMarkers {
  84. markertype eq {
  85. (Marker ) print markertype ==
  86. (not supported!\n) print flush stop
  87. } if
  88. } forall
  89. % Skip segment if marker has parameters associated with it
  90. true NoParamMarkers { markertype eq {pop false exit} if } forall
  91. { SkipSegment } if
  92. } if
  93. } loop
  94. currentdict dup /markertype undef
  95. end
  96. } bind def
  97. end % JPEGdict
  98. % read image parameters from JPEG file and display the image
  99. /viewJPEG { % <file|string> ==> -
  100. save
  101. JPEGdict begin
  102. /saved exch def
  103. /scratch 1 string def
  104. dup type /stringtype eq { (r) file } if
  105. /F exch def
  106. readJPEGmarkers begin
  107. F 0 setfileposition % reset file pointer
  108. % We use the whole clipping area for the image (at least in one dimension)
  109. gsave clippath pathbbox grestore
  110. /ury exch def /urx exch def
  111. /lly exch def /llx exch def
  112. llx lly translate
  113. width height scale
  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. % 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. % If 4-component (CMYK), assume data is inverted per Adobe Photoshop
  126. colors 4 eq {
  127. /Decode [ colors { 1 0 } repeat ]
  128. } {
  129. /Decode [ colors { 0 1 } repeat ]
  130. } ifelse
  131. /DataSource F /DCTDecode filter
  132. >> image
  133. end % image parameter dictionary
  134. saved end restore
  135. } bind def
  136. % This lets you do stuff on the command line like:
  137. % gs -sDEVICE=pdfwrite -o stuff%03d.pdf viewjpeg.ps -c "(image.jpg) << /PageSize 2 index viewJPEGgetsize 2 array astore /HWResolution [ 72 72 ] >> setpagedevice viewJPEG"
  138. % so the output size matches the original image.
  139. /viewJPEGgetsize { % <file|string> ==> width height
  140. save
  141. JPEGdict begin
  142. /saved exch def
  143. /scratch 1 string def
  144. dup type /stringtype eq { (r) file } if
  145. /F exch def
  146. readJPEGmarkers begin
  147. F 0 setfileposition % reset file pointer
  148. width height
  149. saved end restore
  150. } bind def