home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / inne / gs300ini / ps2image.ps < prev    next >
Encoding:
Text File  |  1994-08-02  |  5.8 KB  |  172 lines

  1. %    Copyright (C) 1990, 1991, 1993 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. % Convert a .ps file to another .ps file containing only a bit image.
  16. % After loading ps2image.ps into Ghostscript, invoke
  17. %    (output_file.ps) ps2image
  18. % This replaces the current device, writing the output on the file
  19. % instead of to the device.  Then invoke
  20. %    (your_input_file.ps) run
  21. % To display the image at a later time, simply run the file that was
  22. % written (output_file.ps).
  23.  
  24. % Initialize, and redefine copypage and showpage.
  25. /ps2idict 25 dict def
  26. ps2idict begin
  27.  
  28.         % Save the showpage operator.
  29.   /realshowpage /showpage load def
  30.  
  31.         % Define a monochrome palette.
  32.   /monopalette <00 ff> def
  33.  
  34.         % The following string gets copied to the output file,
  35.         % and also gets executed now.
  36.  
  37. /initstring
  38. (%!
  39.   /maxrep 31 def        % max repeat count
  40.   /maxrep1 maxrep 1 sub def
  41.         % Initialize the strings for filling runs.
  42.      /.ImageFills [
  43.      0 1 255
  44.       { maxrep string dup 0 1 maxrep1 { 3 index put dup } for
  45.     pop exch pop readonly
  46.       } for
  47.      ] readonly def
  48.         % Initialize the procedure table for input dispatching.
  49.      /.ImageProcs [
  50.      33 { { pop .ImageItem } } repeat
  51.      32 { {    % 0x21-0x40: (N-0x20) data bytes follow
  52.       32 sub 3 index exch 0 exch getinterval 2 index exch
  53.       readhexstring pop exch pop dup
  54.      } bind } repeat
  55.      31 { {    % 0x41-0x5f: repeat last data byte (N-0x40) times
  56.       64 sub .ImageFills 2 index dup length 1 sub get get
  57.       exch 0 exch getinterval
  58.      } bind } repeat
  59.      160 { { pop .ImageItem } } repeat
  60.      ] readonly def
  61.         % Read one item from a compressed image.
  62.         % Stack contents: <buffer> <file> <previous>
  63.   /.ImageItem
  64.    { 1 index read pop dup .ImageProcs exch get exec
  65.    } bind def
  66.         % Read and print an entire compressed image,
  67.         % scaling it uniformly in X and Y to fill the page.
  68.         % Arguments: <width> <height>
  69.   /.ImageRead
  70.    { gsave 1 [
  71.      clippath pathbbox pop pop translate
  72.      pathbbox newpath 4 -2 roll pop pop
  73.      dup 3 1 roll abs 5 index exch div exch abs 6 index exch div
  74.      2 copy lt { exch } if pop        % (definition of max)
  75.      0 0 2 index neg 0 4 index 7 -1 roll mul
  76.      ] { .ImageItem }
  77.      4 index 7 add 8 idiv string currentfile ()
  78.      8 3 roll
  79.      image pop pop pop
  80.      grestore showpage
  81.    } bind def
  82. ) def
  83. initstring cvx exec        % Execute it now too.
  84.  
  85.         % Write a repeat command on the file.
  86.   /writerepeat        % <count> writerepeat -
  87.    {  { dup 31 le { exit } if myfile (_) writestring 31 sub } loop
  88.      dup 0 ne { 64 add myfile exch write } { pop } ifelse
  89.    } bind def
  90.  
  91.         % Write a data command on the file.
  92.   /writedata        % <string> writedata -
  93.    {  { dup length 0 eq { exit } if
  94.     dup length 32 min
  95.     dup 16#20 add myfile exch write
  96.     1 index 0 2 index getinterval myfile exch writehexstring
  97.     myfile (\n) writestring
  98.     1 index length 1 index sub getinterval
  99.       }
  100.      loop pop
  101.    } bind def
  102.  
  103.         % Write an entire data string on the file.
  104.   /writedatastring        % <string> writedatastring -
  105.    {  { dup length 4 lt { exit } if
  106.         % Detect a maximal run of non-repeated data.
  107.     dup length 4 sub 0 exch 1 exch
  108.      { 2 copy 3 getinterval 2 index 2 index 1 add 3 getinterval
  109.        eq { exit } if pop
  110.      }
  111.     for
  112.     dup type /stringtype eq { exit } if    % no repetition found
  113.     1 add
  114.     1 index 0 2 index getinterval writedata
  115.     1 index length 1 index sub getinterval
  116.         % Detect a maximal run of repeated data.
  117.         % We know there are at least 3 repeated bytes.
  118.     dup 0 get exch
  119.     dup length exch 3 1 3 index 1 sub
  120.      {        % Stack: <byte> <length> <string> <index>
  121.        2 copy get 4 index ne { 3 -1 roll pop exch exit } { pop } ifelse
  122.      }
  123.     for        % Stack: <byte> <length> <string>
  124.     exch dup writerepeat
  125.     1 index length 1 index sub getinterval
  126.     exch pop
  127.       }
  128.      loop writedata
  129.    } bind def
  130.  
  131.         % The main procedure.
  132.   /ps2image
  133.    {                % Open the file
  134.      (w) file /myfile exch def
  135.      myfile initstring writestring
  136.                     % Get the device parameters
  137.      currentdevice getdeviceprops .dicttomark
  138.      dup /HWSize get aload pop
  139.        /devheight exch def
  140.        /devwidth exch def
  141.      /InitialMatrix get
  142.        /devmatrix exch def
  143.                 % Make a corresponding memory device
  144.      devmatrix devwidth devheight monopalette
  145.      makeimagedevice
  146.      /mydevice exch def
  147.      mydevice setdevice        % (does an erasepage)
  148.      /rowwidth devwidth 7 add 8 idiv def
  149.      /row rowwidth 7 add 8 idiv 8 mul string def    % pad for scanning
  150.                 % Replace the definition of showpage
  151.      userdict /showpage { ps2idict begin myshowpage end } bind put
  152.    } def
  153.                 % Write the image on the file
  154.   /myshowpage
  155.    { myfile devwidth write==only   myfile ( ) writestring
  156.      myfile devheight write==only   myfile ( .ImageRead\n) writestring
  157.                  % Write the hex data
  158.      0 1 devheight 1 sub
  159.       { mydevice exch row 0 rowwidth getinterval copyscanlines
  160.     writedatastring
  161.       } for
  162.      myfile flushfile
  163.      erasepage initgraphics
  164.    } bind def
  165.  
  166. end
  167.  
  168. /ps2image { ps2idict begin ps2image end } bind def
  169.