home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / os2 / gspm25.zip / ps2image.ps < prev    next >
Text File  |  1992-07-17  |  4KB  |  127 lines

  1. %    Copyright (C) 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2. %    Distributed by Free Software Foundation, Inc.
  3. %
  4. % This file is part of Ghostscript.
  5. %
  6. % Ghostscript is distributed in the hope that it will be useful, but
  7. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. % to anyone for the consequences of using it or for whether it serves any
  9. % particular purpose or works at all, unless he says so in writing.  Refer
  10. % to the Ghostscript General Public License for full details.
  11. %
  12. % Everyone is granted permission to copy, modify and redistribute
  13. % Ghostscript, but only under the conditions described in the Ghostscript
  14. % General Public License.  A copy of this license is supposed to have been
  15. % given to you along with Ghostscript so you can know your rights and
  16. % responsibilities.  It should be in a file named COPYING.  Among other
  17. % things, the copyright notice and this notice must be preserved on all
  18. % copies.
  19.  
  20. % Convert a .ps file to another .ps file containing only a bit image.
  21. % After loading ps2image.ps into Ghostscript, invoke
  22. %    (output_file.ps) ps2image
  23. % This replaces the current device, writing the output on the file
  24. % instead of to the device.  Then invoke
  25. %    (your_input_file.ps) run
  26. % To display the image at a later time, simply run the file that was
  27. % written (output_file.ps).
  28.  
  29. % Initialize, and redefine copypage and showpage.
  30. /ps2idict 25 dict def
  31. ps2idict begin
  32.                 % Save the showpage operator
  33.   /realshowpage /showpage load def
  34.                 % Define a monochrome palette
  35.   /monopalette <00 ff> def
  36.                 % The main procedure
  37.   /ps2image
  38.    {                % Open the file
  39.      (w) file /myfile exch def
  40.      myfile (/readimage ) writestring
  41.      myfile /readimage load write==only
  42.      myfile ( bind def\n) writestring
  43.                     % Get the device parameters
  44.      currentdevice getdeviceprops dicttomark
  45.      dup /HWSize get aload pop
  46.        /devheight exch def
  47.        /devwidth exch def
  48.      /InitialMatrix get
  49.        /devmatrix exch def
  50.                 % Make a corresponding memory device
  51.      devmatrix devwidth devheight monopalette
  52.      makeimagedevice
  53.      /mydevice exch def
  54.      mydevice setdevice        % (does an erasepage)
  55.      /rowwidth devwidth 7 add 8 idiv def
  56.      /row rowwidth 7 add 8 idiv 8 mul string def    % pad for findcommon
  57.      /prevrow row length string def
  58.                 % Replace the definition of showpage
  59.      userdict /showpage { ps2idict begin myshowpage end } bind put
  60.    } def
  61.                 % Procedure for reading and displaying
  62.                 % the rendered image.
  63.                 % <width> <height> readimage
  64.   /readimage
  65.    { gsave matrix setmatrix
  66.      1 matrix
  67.      3 index 7 add 8 idiv string currentfile exch
  68.                      % At each iteration of the loop,
  69.                 % the stack contains <file> <buffer>
  70.       { 2 copy
  71.         1 index token pop    % starting index
  72.         2 index token pop    % count
  73.     getinterval
  74.         readhexstring pop pop dup }
  75.      3 1 roll
  76.      7 2 roll
  77.      image pop pop
  78.      grestore showpage
  79.    } def
  80. /findcommon        % prevrow row -> stopindex startindex
  81.  { 2 copy eq
  82.     { 0 0 }
  83.     { dup length
  84.        { 8 sub 2 copy 8 getinterval 3 index 2 index 8 getinterval
  85.          ne { 8 add exit } if
  86.        } loop
  87.        { 1 sub 2 copy get 3 index 2 index get
  88.          ne { 1 add exit } if
  89.        } loop
  90.       0
  91.        { 3 index 1 index 8 getinterval 3 index 2 index 8 getinterval
  92.          ne { exit } if
  93.      8 add
  94.        } loop
  95.        { 3 index 1 index get 3 index 2 index get
  96.          ne { exit } if
  97.      1 add
  98.        } loop
  99.     }
  100.    ifelse
  101.    4 2 roll pop pop
  102.  } bind def
  103.                 % Write the image on the file
  104.   /myshowpage
  105.    { myfile devwidth write==only   myfile ( ) writestring
  106.      myfile devheight write==only   myfile ( readimage\n) writestring
  107.      0 1 prevrow length 1 sub { prevrow exch 0 put } for
  108.                  % Write the hex data
  109.      0 1 devheight 1 sub
  110.       { mydevice exch row 0 rowwidth getinterval copyscanlines
  111.         prevrow row findcommon
  112.                    % stack now has stop index, start index
  113.     exch 1 index sub 2 copy exch    % start, length, length, start
  114.     myfile exch write==only   myfile ( ) writestring
  115.     myfile exch write==only   myfile ( ) writestring
  116.     getinterval myfile exch writehexstring
  117.     row prevrow copy pop
  118.     myfile (\n) writestring
  119.       } for
  120.      myfile flushfile
  121.      erasepage initgraphics
  122.    } bind def
  123.  
  124. end
  125.  
  126. /ps2image { ps2idict begin ps2image end } bind def
  127.