viewjpeg.ps 4.1 KB

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