home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2002 February / INTERNET88.ISO / pc / software / windows / bits / pdf995 / data1.cab / Program_Executable_Files / res / viewmiff.ps < prev    next >
Encoding:
Text File  |  2001-12-08  |  4.0 KB  |  128 lines

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