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

  1. %    Copyright (C) 1992, 1995, 1996, 1998, 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: viewpbm.ps,v 1.1 2000/03/09 08:40:40 lpd Exp $
  16. % viewpbm.ps
  17. % Display a PBM/PGM/PPM 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.  
  22. /s 100 string def
  23. /readmaxv {        % <file> readmaxv -
  24.   10 string readline pop cvx exec /maxv exch def
  25. } bind def
  26. /readrow {        % <file> <row> readrow <row>
  27.   0 1 2 index length 1 sub {
  28.     1 index exch 3 index token pop put
  29.   } for exch pop
  30. } bind def
  31. /read01 {        % <file> <count> read01 <byte>
  32.   0 exch {
  33.     1 index read pop 48 xor dup 1 le { exch dup add add } { pop } ifelse
  34.   } repeat
  35. } bind def
  36. /readrow01 {        % <file> <row> readrow01 <row>
  37.   0 1 w 8 idiv {
  38.     1 index exch 3 index 8 read01 put
  39.   } for
  40.   wrem 0 ne {
  41.      dup rsize 1 sub wrem read01 8 wrem sub bitshift put
  42.   } if
  43.   exch pop
  44. } bind def
  45. /readwh {        % <file> readwh <w> <h>
  46.  dup s readline pop        % check for comment
  47.  (#) anchorsearch {
  48.    pop pop dup s readline pop
  49.  } if
  50.  cvx exec
  51. } bind def
  52. /pbmtypes mark
  53. % The procedures in this dictionary are called as
  54. %    <file> Pn <w> <h> <readproc>
  55. /P1 {            % ASCII 1-bit white/black
  56.   /bpc 1 def /maxv 1 def /rsize w 7 add 8 idiv def
  57.   /wrem w 8 mod def
  58.   /ncomp 1 def /invert true def /DeviceGray setcolorspace
  59.   readwh
  60.     { readrow01 }
  61. } bind
  62. /P2 {            % ASCII 8-bit gray
  63.   readwh
  64.   /bpc 8 def 2 index readmaxv /rsize 2 index def
  65.   /ncomp 1 def /invert false def /DeviceGray setcolorspace
  66.     { readrow }
  67. } bind
  68. /P3 {            % ASCII 8-bit RGB
  69.   readwh
  70.   /bpc 8 def 2 index readmaxv /rsize 2 index 3 mul def
  71.   /ncomp 3 def /invert false def /DeviceRGB setcolorspace
  72.     { readrow }
  73. } bind
  74. /P4 {            % Binary 1-bit white/black
  75.   readwh
  76.   /bpc 1 def /maxv 1 def /rsize 2 index 7 add 8 idiv def
  77.   /ncomp 1 def /invert true def /DeviceGray setcolorspace
  78.     { readstring pop }
  79. } bind
  80. /P5 {            % Binary 8-bit gray
  81.   readwh
  82.   /bpc 8 def 2 index readmaxv /rsize 2 index def
  83.   /ncomp 1 def /invert false def /DeviceGray setcolorspace
  84.     { readstring pop }
  85. } bind
  86. /P6 {            % Binary 8-bit RGB
  87.   readwh
  88.   /bpc 8 def 2 index readmaxv /rsize 2 index 3 mul def
  89.   /ncomp 3 def /invert false def /DeviceRGB setcolorspace
  90.     { readstring pop }
  91. } bind
  92. .dicttomark readonly def
  93. /pbmsetup {            % <file> <w> <h> <readproc> runpbm -
  94.    /readproc exch def
  95.    /h exch def
  96.    /w exch def
  97.    /f exch def
  98.    20 dict begin        % image dictionary
  99.      /ImageType 1 def
  100.      /Width w def
  101.      /Height h def
  102.      /ImageMatrix [w 0 0 h neg 0 h] def
  103.      /BitsPerComponent bpc def
  104.      /Decode [ 0 255 maxv div invert { exch } if ncomp 1 sub { 2 copy } repeat ] def
  105.      /DataSource [ f rsize string /readproc load /exec load ] cvx def
  106.    currentdict end
  107. } def
  108. /imagescale {            % <imagedict> imagescale -
  109.   begin
  110.     /SCALE where {
  111.       pop
  112.     % Map pixels SCALE-for-1.  Assume orthogonal transformation.
  113.       Width 1 0 dtransform add abs div SCALE mul
  114.       Height 0 1 dtransform add abs div SCALE mul
  115.     } {
  116.     % Scale the image (uniformly) to fit the page.
  117.       clippath pathbbox pop pop translate
  118.       pathbbox min exch pop exch pop ceiling
  119.       dup Height Width gt {
  120.     Width mul Height div exch
  121.       } {
  122.     Height mul Width div
  123.       } ifelse
  124.     }
  125.     ifelse scale
  126.   end
  127. } def
  128.  
  129. % Image a PBM file page by page.
  130. /viewpbm {            % <filename> viewpbm -
  131.   20 dict begin
  132.     (r) file /pf exch def {
  133.       pf token not { exit } if
  134.       pbmtypes exch get pf exch exec pbmsetup
  135.       dup imagescale image showpage
  136.     } loop
  137.   end
  138. } def
  139.  
  140. % Reassemble a composite PBM file from the CMYK separations.
  141. /viewpsm {
  142.   20 dict begin
  143.     /fname exch def
  144.     /sources [ 0 1 3 {
  145.       /plane exch def 
  146.       /pf fname (r) file def
  147.       pf pbmtypes pf token pop get exec
  148.         % Stack: pf w h readproc
  149.       plane {
  150.     /readproc exch def /h exch def /w exch def pop
  151.     /row rsize string def
  152.     h { pf row readproc pop } repeat
  153.     pf pbmtypes pf token pop get exec
  154.       } repeat
  155.       pbmsetup
  156.     } for ] def
  157.     /datas [ sources { /DataSource get 0 get } forall ] def
  158.     /decode sources 0 get /Decode get
  159.       dup 0 get exch 1 get add cvi 0 exch
  160.       2 copy 4 copy 8 array astore def
  161.     sources 0 get
  162.       dup /MultipleDataSources true put
  163.       dup /DataSource datas put
  164.       dup /Decode decode put
  165.     /DeviceCMYK setcolorspace
  166.     dup imagescale image showpage
  167.   end
  168. } def
  169.  
  170. % If the program was invoked from the command line, run it now.
  171. [ shellarguments
  172.  { counttomark 1 ge
  173.     { ] { viewpbm } forall
  174.     }
  175.     { cleartomark
  176.       (Usage: gs [--] viewpbm.ps filename.p*m ...\n) print
  177.       ( e.g.: gs [--] viewpbm.ps my.ppm another.ppm\n) print flush
  178.     }
  179.    ifelse
  180.  }
  181.  { pop
  182.  }
  183. ifelse
  184.