home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / gs403osk.tgz / gs403osk.tar / viewpbm.ps < prev    next >
Text File  |  1996-09-22  |  4KB  |  136 lines

  1. %    Copyright (C) 1992, 1995, 1996 Aladdin Enterprises.  All rights reserved.
  2. %
  3. % This file is part of Ghostscript.
  4. %
  5. % Ghostscript is distributed in the hope that it will be useful, but
  6. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. % to anyone for the consequences of using it or for whether it serves any
  8. % particular purpose or works at all, unless he says so in writing.  Refer
  9. % to the Ghostscript General Public License for full details.
  10. %
  11. % Everyone is granted permission to copy, modify and redistribute
  12. % Ghostscript, but only under the conditions described in the Ghostscript
  13. % General Public License.  A copy of this license is supposed to have been
  14. % given to you along with Ghostscript so you can know your rights and
  15. % responsibilities.  It should be in a file named COPYING.  Among other
  16. % things, the copyright notice and this notice must be preserved on all
  17. % copies.
  18.  
  19. % viewpbm.ps
  20. % Display a PBM/PGM/PPM file.
  21. % Requires the Level 2 `image' operator (to handle variable pixel widths).
  22. % If SCALE is defined, maps input pixels to output pixels with that scale;
  23. % if SCALE is undefined, scales the image to fit the page.
  24.  
  25. /s 100 string def
  26. /readmaxv
  27.  { f s readline pop cvx exec /maxv exch def
  28.  } bind def
  29. /readrow
  30.  { 0 1 row length 1 sub { row exch f token pop put } for
  31.  } bind def
  32. /read01         % <count> read01 <byte>
  33.  { 0 exch { f read pop 48 xor dup 1 le { exch dup add add } { pop } ifelse } repeat
  34.  } bind def
  35. /P1         % ASCII 1-bit white/black
  36.  {  { /bpc 1 def /maxv 1 def /rsize w 7 add 8 idiv def
  37.       /wrem w 8 mod def
  38.           /ncomp 1 def /invert true def
  39.     }
  40.     { 0 1 w 8 idiv { row exch 8 read01 put } for
  41.       wrem 0 ne
  42.        { row rsize 1 sub wrem read01 8 wrem sub bitshift put
  43.        } if
  44.       row
  45.     }
  46.    runpbm
  47.  } bind def
  48. /P2         % ASCII 8-bit gray
  49.  {  { /bpc 8 def readmaxv /rsize w def
  50.       /ncomp 1 def /invert false def
  51.     }
  52.     { readrow row }
  53.    runpbm
  54.  } bind def
  55. /P3         % ASCII 8-bit RGB
  56.  {  { /bpc 8 def readmaxv /rsize w 3 mul def
  57.           /ncomp 3 def /invert false def /DeviceRGB setcolorspace
  58.     }
  59.     { readrow row }
  60.    runpbm
  61.  } bind def
  62. /P4         % Binary 1-bit white/black
  63.  {  { /bpc 1 def /maxv 1 def /rsize w 7 add 8 idiv def
  64.       /ncomp 1 def /invert true def
  65.     }
  66.     { f row readstring pop }
  67.    runpbm
  68.  } bind def
  69. /P5         % Binary 8-bit gray
  70.  {  { /bpc 8 def readmaxv /rsize w def
  71.       /ncomp 1 def /invert false def
  72.     }
  73.     { f row readstring pop }
  74.    runpbm
  75.  } bind def
  76. /P6         % Binary 8-bit RGB
  77.  {  { /bpc 8 def readmaxv /rsize w 3 mul def
  78.       /ncomp 3 def /invert false def /DeviceRGB setcolorspace
  79.     }
  80.     { f row readstring pop }
  81.    runpbm
  82.  } bind def
  83. /viewpbm { run } def
  84. /runpbm             % <initproc> <readproc> runpbm -
  85.  { /readproc exch def
  86.    /initproc exch def
  87.    currentfile /f exch def
  88.    f s readline pop     % check for comment
  89.    (#) anchorsearch
  90.     { pop pop f s readline pop }
  91.    if
  92.    cvx exec /h exch def /w exch def
  93.    erasepage
  94.    /DeviceGray setcolorspace
  95.    initproc         % sets bpc, maxv, rsize, ncomp, invert
  96.    /row rsize string def
  97.    /SCALE where
  98.     { pop
  99.     % Map pixels SCALE-for-1.  Assume orthogonal transformation.
  100.       w 1 0 dtransform add abs div SCALE mul
  101.       h 0 1 dtransform add abs div SCALE mul
  102.     }
  103.     {   % Scale the image (uniformly) to fit the page.
  104.       clippath pathbbox pop pop translate
  105.       pathbbox min exch pop exch pop ceiling
  106.       dup h mul w div
  107.     }
  108.    ifelse scale
  109.    20 dict begin        % image dictionary
  110.      /ImageType 1 def
  111.      /Width w def
  112.      /Height h def
  113.      /ImageMatrix [w 0 0 h neg 0 h] def
  114.      /BitsPerComponent bpc def
  115.      /Decode [ 0 255 maxv div invert { exch } if ncomp 1 sub { 2 copy } repeat ] def
  116.      /DataSource /readproc load def
  117.    currentdict end
  118.    image
  119.    showpage
  120.  } def
  121.  
  122. % If the program was invoked from the command line, run it now.
  123. [ shellarguments
  124.  { counttomark 1 ge
  125.     { ] { viewpbm } forall
  126.     }
  127.     { cleartomark
  128.       (Usage: gs [--] viewpbm.ps filename.p*m ...\n) print
  129.       ( e.g.: gs [--] viewpbm.ps my.ppm another.ppm\n) print flush
  130.     }
  131.    ifelse
  132.  }
  133.  { pop
  134.  }
  135. ifelse
  136.