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

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