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.

137 lines
4.1 KiB

1 month ago
  1. % Copyright (C) 2001-2023 Artifex Software, Inc.
  2. % All Rights Reserved.
  3. %
  4. % This software is provided AS-IS with no warranty, either express or
  5. % implied.
  6. %
  7. % This software is distributed under license and may not be copied,
  8. % modified or distributed except as expressly authorized under the terms
  9. % of the license contained in the file LICENSE in this distribution.
  10. %
  11. % Refer to licensing information at http://www.artifex.com or contact
  12. % Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  13. % CA 94129, USA, for further information.
  14. %
  15. % viewmiff.ps
  16. % Display a MIFF file. You would think the 'display' command would do this,
  17. % but many versions of 'display' either core-dump or require unacceptably
  18. % large amounts of memory.
  19. % FITPAGE is true, it fits the output page size to the image
  20. % Recognize MIFF keywords.
  21. /miffwords mark
  22. /class { cvn /class exch def }
  23. /colors { cvi /colors exch def }
  24. /columns { cvi /Width exch def }
  25. /compression { cvn /compression exch def }
  26. /depth { cvi /depth exch def }
  27. /packets { cvi /packets exch def }
  28. /rows { cvi /Height exch def }
  29. .dicttomark readonly def
  30. % Recognize MIFF image classes.
  31. /miffclasses mark
  32. /DirectClass {
  33. /DeviceRGB setcolorspace
  34. /BitsPerComponent depth def
  35. /Decode [ 0 1 0 1 0 1 ] def
  36. }
  37. /PseudoClass {
  38. [ /Indexed
  39. % The MIFF documentation lies about the size of pixels
  40. % for this case: the pixel size is determined only by
  41. % the number of colors, and is not affected by the image
  42. % depth. Specifically, if there are 256 or fewer colors
  43. % but the depth (of color map entries) is 16, each pixel
  44. % is still only 1 byte, not 2.
  45. currentdict /colors known {
  46. /DeviceRGB colors 1 sub
  47. /BitsPerComponent colors 256 le { 8 } { 16 } ifelse def
  48. colors 3 mul string depth 8 eq {
  49. f exch readstring pop
  50. } {
  51. % 16-bit color map entries: take only the high-order byte.
  52. 0 1 2 index length 1 sub {
  53. f read pop 2 index 3 1 roll put f read pop pop
  54. } for
  55. } ifelse
  56. } {
  57. /colors 256 def
  58. /DeviceGray 255
  59. 256 string 0 1 255 { 1 index exch dup put } for
  60. } ifelse
  61. ] setcolorspace
  62. /Decode [ 0 1 BitsPerComponent bitshift 1 sub ] def
  63. }
  64. .dicttomark readonly def
  65. % Recognize MIFF compression methods.
  66. /rlstring 768 string def
  67. /rlread {
  68. % packets is not reliable -- disregard it.
  69. dup rlstring 0 3 getinterval readstring {
  70. pop read pop 3 mul 3 3 2 index {
  71. rlstring exch rlstring 0 3 getinterval putinterval
  72. } for
  73. rlstring 0 3 -1 roll 3 add getinterval
  74. } {
  75. pop pop ()
  76. } ifelse
  77. } bind def
  78. /miffcompress mark
  79. /Uncompressed { f }
  80. /RunLengthEncoded { { f rlread } }
  81. /Zip { [ f /FlateDecode filter cvlit /rlread cvx ] cvx }
  82. .dicttomark readonly def
  83. % Read a MIFF file and display the image.
  84. /viewmiff { % <filename> viewmiff -
  85. 50 dict begin
  86. /fname 1 index def
  87. /f exch (r) file def
  88. % Set defaults.
  89. /ImageType 1 def
  90. /class /DirectClass def
  91. /compression /Uncompressed def
  92. /depth 8 def
  93. /packets 16#7fffffff def
  94. % Read and parse the header.
  95. { f token pop
  96. dup (:) eq { pop exit } if
  97. dup type /nametype eq {
  98. 1024 string cvs (=) search {
  99. exch pop miffwords exch .knownget { exec } { pop } ifelse
  100. } {
  101. pop % who knows?
  102. } ifelse
  103. } {
  104. pop % probably a comment in braces
  105. } ifelse
  106. } loop
  107. % Read and display the image.
  108. miffclasses class get exec
  109. /DataSource miffcompress compression get exec def
  110. /FITPAGE where
  111. {
  112. /FITPAGE get
  113. {
  114. % we've already set the image color space, so
  115. % push it on the stack, and set it again after
  116. % setting the page size
  117. currentcolorspace
  118. <</PageSize [Width Height] >> setpagedevice
  119. setcolorspace
  120. } if
  121. } if
  122. /ImageMatrix [Width 0 0 Height neg 0 Height] def
  123. currentpagedevice /PageSize get
  124. dup 0 get exch 1 get scale
  125. gsave 0.8 setgray 0 0 1 1 rectfill grestore % provide background
  126. currentdict image
  127. showpage
  128. % Clean up.
  129. f closefile
  130. end
  131. } bind def