home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / GhostScript / !GhostScr / 6_01 / lib / viewpcx.ps < prev    next >
Text File  |  2000-03-09  |  5KB  |  153 lines

  1. %    Copyright (C) 1996, 1999 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % $Id: viewpcx.ps,v 1.1 2000/03/09 08:40:40 lpd Exp $
  16. % viewpcx.ps
  17. % Display a PCX file.
  18. % Requires the Level 2 `image' operator (to handle variable pixel widths).
  19. % If SCALE is defined, maps input pixels to output pixels with that scale;
  20. % if SCALE is undefined, scales the image to fit the page.
  21. % ****NOTE: does not handle multi-plane images with palette.
  22.  
  23. /pcxbytes [
  24.   0 1 255 {
  25.     64 string exch 0 1 63 {
  26.       3 copy exch put pop
  27.     } for pop
  28.   } for
  29. ] readonly def
  30. /readpcx {            % - readpcx <str>
  31.   f                % gets replaced
  32.   dup read not {
  33.     pop ()
  34.   } {
  35.     dup 192 lt {
  36.       ( ) dup 0 4 -1 roll put exch pop
  37.     } {
  38.       192 sub //pcxbytes 3 -1 roll read pop get exch 0 exch getinterval
  39.     } ifelse
  40.   } ifelse
  41. } def
  42. /get2                % <string> <index> get2 <int>
  43.  { 2 copy get 3 1 roll 1 add get 8 bitshift add
  44.  } bind def
  45. /dsproc
  46.  { df s readstring pop        % s gets filled in
  47.    s1 () ne { df s1 readstring pop pop } if % discard padding bytes
  48.  } def                % don't bind, must be writable
  49. /viewpcx            % <filename> viewpcx -
  50.  { 100 dict begin
  51.    /fname 1 index def
  52.    /f exch (r) file def
  53.         % Read and unpack the header.
  54.    /header f 128 string readstring pop def
  55.    /version header 1 get def
  56.    /bpp header 3 get def
  57.    /w header 8 get2 header 4 get2 sub 1 add def
  58.    /h header 10 get2 header 6 get2 sub 1 add def
  59.    /nplanes header 65 get def
  60.    /bpl header 66 get2 def
  61.    /palinfo header 68 get2 def
  62.    /nbits bpp nplanes mul def
  63.    version 5 eq
  64.     { nbits 8 le
  65.        { /cspace
  66.        [/Indexed   /DeviceRGB   1 bpp bitshift 1 sub
  67.      f fileposition
  68.      1 nbits bitshift 3 mul string
  69.      fname status pop pop pop exch pop
  70.      1 index length sub f exch setfileposition
  71.      f exch readstring pop
  72.      exch f exch setfileposition
  73.        ] def
  74.      /decode [0 cspace 2 get] def
  75.        }
  76.        { /cspace /DeviceRGB def
  77.      /decode [0 1 0 1 0 1] def
  78.        }
  79.       ifelse
  80.     }
  81.     { /cspace
  82.     [/Indexed   /DeviceRGB   1 bpp bitshift 1 sub
  83.      header 16 1 nbits bitshift 16 min 3 mul getinterval
  84.     ] def
  85.       /decode [0 cspace 2 get] def
  86.     }
  87.    ifelse
  88.         % Set up scaling.
  89.    /SCALE where
  90.     { pop
  91.     % Map pixels SCALE-for-1.  Assume orthogonal transformation.
  92.       w 1 0 dtransform add abs div SCALE mul
  93.       h 0 1 dtransform add abs div SCALE mul
  94.     }
  95.     {    % Scale the image (uniformly) to fit the page.
  96.       clippath pathbbox pop pop translate
  97.       pathbbox min exch pop exch pop ceiling
  98.       dup h w gt { w mul h div exch } { h mul w div } ifelse
  99.     }
  100.    ifelse scale
  101.         % Since the number of bytes per line is always even,
  102.         % it may not match the width specification.
  103.    /wbpl w bpp mul 7 add 8 idiv def
  104.         % Define the data source procedure.
  105.    /s1 bpl wbpl sub string def
  106.    /df /readpcx load copyarray dup 0 f put cvx bind readonly
  107.      0 () /SubFileDecode filter def
  108.    /dsource [ nplanes
  109.     { /dsproc load copyarray
  110.       dup 1 wbpl string put
  111.       cvx bind readonly
  112.     }
  113.    repeat ] def
  114.         % Construct the image dictionary.
  115.    20 dict begin        % image dictionary
  116.      /ImageType 1 def
  117.      /Width w def
  118.      /Height h def
  119.      /ImageMatrix [w 0 0 h neg 0 h] def
  120.      /BitsPerComponent bpp def
  121.      /Decode decode def
  122.      /DataSource dsource dup length 1 gt
  123.       { /MultipleDataSources true def }
  124.       { 0 get }
  125.      ifelse def
  126.    currentdict end
  127.         % Finally, display the image.
  128.    cspace setcolorspace
  129.    image
  130.    showpage
  131.    df closefile
  132.    f closefile
  133.    end
  134.  } bind def
  135.  
  136. % If the program was invoked from the command line, run it now.
  137. [ shellarguments
  138.  { counttomark 1 ge
  139.     { ] { viewpcx } forall
  140.     }
  141.     { cleartomark
  142.       (Usage: gs -- viewpcx.ps filename.pcx ...\n) print
  143.       ( e.g.: gs -- viewpcx.ps my.pcx another.pcx\n) print flush
  144.     }
  145.    ifelse
  146.  }
  147.  { pop
  148.  }
  149. ifelse
  150.