viewmiff.ps 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. % Copyright (C) 1998 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This file is part of AFPL Ghostscript.
  4. %
  5. % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  6. % distributor accepts any responsibility for the consequences of using it, or
  7. % for whether it serves any particular purpose or works at all, unless he or
  8. % she says so in writing. Refer to the Aladdin Free Public License (the
  9. % "License") for full details.
  10. %
  11. % Every copy of AFPL Ghostscript must include a copy of the License, normally
  12. % in a plain ASCII text file named PUBLIC. The License grants you the right
  13. % to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14. % conditions described in the License. Among other things, the License
  15. % requires that the copyright notice and this notice be preserved on all
  16. % copies.
  17. % $Id: viewmiff.ps,v 1.2 2000/09/19 18:29:11 lpd Exp $
  18. % viewmiff.ps
  19. % Display a MIFF file. You would think the 'display' command would do this,
  20. % but many versions of 'display' either core-dump or require unacceptably
  21. % large amounts of memory.
  22. % Recognize MIFF keywords.
  23. /miffwords mark
  24. /class { cvn /class exch def }
  25. /colors { cvi /colors exch def }
  26. /columns { cvi /Width exch def }
  27. /compression { cvn /compression exch def }
  28. /depth { cvi /depth exch def }
  29. /packets { cvi /packets exch def }
  30. /rows { cvi /Height exch def }
  31. .dicttomark readonly def
  32. % Recognize MIFF image classes.
  33. /miffclasses mark
  34. /DirectClass {
  35. /DeviceRGB setcolorspace
  36. /BitsPerComponent depth def
  37. /Decode [ 0 1 0 1 0 1 ] def
  38. }
  39. /PseudoClass {
  40. [ /Indexed
  41. % The MIFF documentation lies about the size of pixels
  42. % for this case: the pixel size is determined only by
  43. % the number of colors, and is not affected by the image
  44. % depth. Specifically, if there are 256 or fewer colors
  45. % but the depth (of color map entries) is 16, each pixel
  46. % is still only 1 byte, not 2.
  47. currentdict /colors known {
  48. /DeviceRGB colors 1 sub
  49. /BitsPerComponent colors 256 le { 8 } { 16 } ifelse def
  50. colors 3 mul string depth 8 eq {
  51. f exch readstring pop
  52. } {
  53. % 16-bit color map entries: take only the high-order byte.
  54. 0 1 2 index length 1 sub {
  55. f read pop 2 index 3 1 roll put f read pop pop
  56. } for
  57. } ifelse
  58. } {
  59. /colors 256 def
  60. /DeviceGray 255
  61. 256 string 0 1 255 { 1 index exch dup put } for
  62. } ifelse
  63. ] setcolorspace
  64. /Decode [ 0 1 BitsPerComponent bitshift 1 sub ] def
  65. }
  66. .dicttomark readonly def
  67. % Recognize MIFF compression methods.
  68. /rlstring 768 string def
  69. /rlread {
  70. % packets is not reliable -- disregard it.
  71. dup rlstring 0 3 getinterval readstring {
  72. pop read pop 3 mul 3 3 2 index {
  73. rlstring exch rlstring 0 3 getinterval putinterval
  74. } for
  75. rlstring 0 3 -1 roll 3 add getinterval
  76. } {
  77. pop pop ()
  78. } ifelse
  79. } bind def
  80. /miffcompress mark
  81. /Uncompressed { f }
  82. /RunLengthEncoded { { f rlread } }
  83. /Zip { [ f /FlateDecode filter cvlit /rlread cvx ] cvx }
  84. .dicttomark readonly def
  85. % Read a MIFF file and display the image.
  86. /viewmiff { % <filename> viewmiff -
  87. 50 dict begin
  88. /fname 1 index def
  89. /f exch (r) file def
  90. % Set defaults.
  91. /ImageType 1 def
  92. /class /DirectClass def
  93. /compression /Uncompressed def
  94. /depth 8 def
  95. /packets 16#7fffffff def
  96. % Read and parse the header.
  97. { f token pop
  98. dup (:) eq { pop exit } if
  99. dup type /nametype eq {
  100. .namestring (=) search {
  101. exch pop miffwords exch .knownget { exec } { pop } ifelse
  102. } {
  103. pop % who knows?
  104. } ifelse
  105. } {
  106. pop % probably a comment in braces
  107. } ifelse
  108. } loop
  109. % Read and display the image.
  110. miffclasses class get exec
  111. /DataSource miffcompress compression get exec def
  112. /ImageMatrix [Width 0 0 Height neg 0 Height] def
  113. currentpagedevice /PageSize get
  114. dup 0 get exch 1 get scale
  115. gsave 0.8 setgray 0 0 1 1 rectfill grestore % provide background
  116. currentdict image
  117. showpage
  118. % Clean up.
  119. f closefile
  120. end
  121. } bind def