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

  1. %    Copyright (C) 1996, 1999 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: viewpcx.ps,v $ $Revision: 1.2.2.1 $
  17. % viewpcx.ps
  18. % Display a PCX file.
  19. % Requires the Level 2 `image' operator (to handle variable pixel widths).
  20. % If SCALE is defined, maps input pixels to output pixels with that scale;
  21. % if SCALE is undefined, scales the image to fit the page.
  22. % ****NOTE: does not handle multi-plane images with palette.
  23.  
  24. /pcxbytes [
  25.   0 1 255 {
  26.     64 string exch 0 1 63 {
  27.       3 copy exch put pop
  28.     } for pop
  29.   } for
  30. ] readonly def
  31. /readpcx {            % - readpcx <str>
  32.   f                % gets replaced
  33.   dup read not {
  34.     pop ()
  35.   } {
  36.     dup 192 lt {
  37.       ( ) dup 0 4 -1 roll put exch pop
  38.     } {
  39.       192 sub //pcxbytes 3 -1 roll read pop get exch 0 exch getinterval
  40.     } ifelse
  41.   } ifelse
  42. } def
  43. /get2                % <string> <index> get2 <int>
  44.  { 2 copy get 3 1 roll 1 add get 8 bitshift add
  45.  } bind def
  46. /dsproc
  47.  { df s readstring pop        % s gets filled in
  48.    s1 () ne { df s1 readstring pop pop } if % discard padding bytes
  49.  } def                % don't bind, must be writable
  50. /viewpcx            % <filename> viewpcx -
  51.  { 100 dict begin
  52.    /fname 1 index def
  53.    /f exch (r) file def
  54.         % Read and unpack the header.
  55.    /header f 128 string readstring pop def
  56.    /version header 1 get def
  57.    /bpp header 3 get def
  58.    /w header 8 get2 header 4 get2 sub 1 add def
  59.    /h header 10 get2 header 6 get2 sub 1 add def
  60.    /nplanes header 65 get def
  61.    /bpl header 66 get2 def
  62.    /palinfo header 68 get2 def
  63.    /nbits bpp nplanes mul def
  64.    version 5 eq
  65.     { nbits 8 le
  66.        { /cspace
  67.        [/Indexed   /DeviceRGB   1 bpp bitshift 1 sub
  68.      f fileposition
  69.      1 nbits bitshift 3 mul string
  70.      fname status pop pop pop exch pop
  71.      1 index length sub f exch setfileposition
  72.      f exch readstring pop
  73.      exch f exch setfileposition
  74.        ] def
  75.      /decode [0 cspace 2 get] def
  76.        }
  77.        { /cspace /DeviceRGB def
  78.      /decode [0 1 0 1 0 1] def
  79.        }
  80.       ifelse
  81.     }
  82.     { /cspace
  83.     [/Indexed   /DeviceRGB   1 bpp bitshift 1 sub
  84.      header 16 1 nbits bitshift 16 min 3 mul getinterval
  85.     ] def
  86.       /decode [0 cspace 2 get] def
  87.     }
  88.    ifelse
  89.         % Set up scaling.
  90.    /SCALE where
  91.     { pop
  92.     % Map pixels SCALE-for-1.  Assume orthogonal transformation.
  93.       w 1 0 dtransform add abs div SCALE mul
  94.       h 0 1 dtransform add abs div SCALE mul
  95.     }
  96.     {    % Scale the image (uniformly) to fit the page.
  97.       clippath pathbbox pop pop translate
  98.       pathbbox min exch pop exch pop ceiling
  99.       dup h w gt { w mul h div exch } { h mul w div } ifelse
  100.     }
  101.    ifelse scale
  102.         % Since the number of bytes per line is always even,
  103.         % it may not match the width specification.
  104.    /wbpl w bpp mul 7 add 8 idiv def
  105.         % Define the data source procedure.
  106.    /s1 bpl wbpl sub string def
  107.    /df /readpcx load copyarray dup 0 f put cvx bind readonly
  108.      0 () /SubFileDecode filter def
  109.    /dsource [ nplanes
  110.     { /dsproc load copyarray
  111.       dup 1 wbpl string put
  112.       cvx bind readonly
  113.     }
  114.    repeat ] def
  115.         % Construct the image dictionary.
  116.    20 dict begin        % image dictionary
  117.      /ImageType 1 def
  118.      /Width w def
  119.      /Height h def
  120.      /ImageMatrix [w 0 0 h neg 0 h] def
  121.      /BitsPerComponent bpp def
  122.      /Decode decode def
  123.      /DataSource dsource dup length 1 gt
  124.       { /MultipleDataSources true def }
  125.       { 0 get }
  126.      ifelse def
  127.    currentdict end
  128.         % Finally, display the image.
  129.    cspace setcolorspace
  130.    image
  131.    showpage
  132.    df closefile
  133.    f closefile
  134.    end
  135.  } bind def
  136.  
  137. % If the program was invoked from the command line, run it now.
  138. [ shellarguments
  139.  { counttomark 1 ge
  140.     { ] { viewpcx } forall
  141.     }
  142.     { cleartomark
  143.       (Usage: gs -- viewpcx.ps filename.pcx ...\n) print
  144.       ( e.g.: gs -- viewpcx.ps my.pcx another.pcx\n) print flush
  145.     }
  146.    ifelse
  147.  }
  148.  { pop
  149.  }
  150. ifelse
  151.