home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gs333ini / gs3.33 / ps2epsi.ps < prev    next >
Text File  |  1995-12-09  |  6KB  |  190 lines

  1. %    Copyright (C) 1990, 1991, 1993, 1994 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 an arbitrary PostScript file to an EPSI file.
  16. %
  17. %    NOTE: this file was contributed by a user: please contact
  18. %    George Cameron <george@bio-medical-physics.aberdeen.ac.uk>
  19. %    if you have questions.
  20. %
  21.  
  22. % Initialize, and redefine copypage and showpage.
  23. /ps2edict 25 dict def
  24. ps2edict begin
  25.  
  26.                 % The main procedure
  27.   /ps2epsi
  28.    {                % Open the file
  29.      outfile (w) file /epsifile exch def
  30.                     % Get the device parameters
  31.      currentdevice getdeviceprops .dicttomark
  32.      /HWSize get aload pop
  33.        /devheight exch def
  34.        /devwidth exch def
  35.      matrix defaultmatrix
  36.        /devmatrix exch def
  37.                 % Make a corresponding memory device
  38.      devmatrix devwidth devheight <ff 00>
  39.      makeimagedevice
  40.      /arraydevice exch def
  41.      arraydevice setdevice    % (does an erasepage)
  42.      /rowwidth devwidth 7 add 8 idiv def
  43.      /row rowwidth string def
  44.                 % Replace the definition of showpage
  45.      userdict /showpage { ps2edict begin epsipage end } bind put
  46.    } def
  47.  
  48.  /margintest
  49.  {
  50.       { dup arraydevice exch row copyscanlines pop
  51.     -1 0 1 rowwidth 1 sub
  52.     { dup row exch get 0 ne { exit }{ pop } ifelse
  53.     } for
  54.     dup -1 ne { exch pop } if
  55.     -1 ne { exit } if pop
  56.       } for
  57.  } bind def
  58.  
  59.   /epsipage
  60.    {
  61.      /loopcount devheight 1 sub def
  62.  
  63.      % Find top margin
  64.      -1 0 1 loopcount margintest
  65.      dup -1 eq { (blank page!!\n) print 0 .quit }{ exch pop } ifelse 
  66.      /tm exch def
  67.  
  68.      % Find bottom margin
  69.      loopcount -1 0 margintest
  70.      /bm exch def
  71.      
  72.      % Initialise limit variables
  73.      /loopcount rowwidth 1 sub def
  74.      /lm loopcount def /lmb 0 def
  75.      /rm 0 def /rmb 0 def
  76.  
  77.      % Find left and right boundaries of image
  78.      tm 1 bm
  79.       { % Get more data
  80.     arraydevice exch row copyscanlines pop
  81.     % Scan from left to find first non-zero element
  82.     % We save first the element, then the index
  83.     -1 0 1 loopcount
  84.     { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  85.     } for
  86.     % If we found -1, row is blank ..
  87.     dup -1 ne 
  88.     { % Find the leftmost index
  89.           dup lm lt
  90.           % If the new index is less, we save index and element
  91.           { /lm exch def /lmb exch def }
  92.           % If the index is equal, we or the bits together
  93.           { lm eq { lmb or /lmb exch def }{ pop } ifelse
  94.           } ifelse
  95.       % Now find the rightmost index
  96.       loopcount -1 0
  97.           { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  98.           } for
  99.       dup rm gt
  100.           % If the new index is greater, we save index and element
  101.           { /rm exch def /rmb exch def }
  102.           % If the index is equal, or the bits
  103.           { rm eq { rmb or /rmb exch def } { pop } ifelse
  104.           } ifelse
  105.     } if
  106.     pop
  107.       } for
  108.  
  109.      % Now we find the real left & right bit positions
  110.      256 0 1 7
  111.      { exch 2 div dup lmb le { pop exit }{ exch pop } ifelse
  112.      } for
  113.      /lmb exch def
  114.  
  115.      1 7 -1 0
  116.      { exch dup dup rmb and eq { pop exit }{ 2 mul exch pop } ifelse
  117.      } for
  118.      /rmb exch def
  119.  
  120.      % Calculate the bounding box values
  121.      /llx lm 8 mul lmb add def
  122.      /lly devheight bm sub def
  123.      /urx rm 8 mul rmb add def
  124.      /ury devheight tm sub def
  125.  
  126.      % Write out the magic string and bounding box information
  127.      epsifile (%!PS-Adobe-2.0 EPSF-1.2\n) writestring
  128.      epsifile (%%BoundingBox: ) writestring
  129.      epsifile llx write==only epsifile ( ) writestring
  130.      epsifile lly write==only epsifile ( ) writestring
  131.      epsifile urx write==only epsifile ( ) writestring
  132.      epsifile ury write==
  133.      epsifile (%%BeginPreview: ) writestring
  134.      epsifile urx llx sub 1 add write==only epsifile ( ) writestring
  135.      epsifile bm tm sub 1 add write==only epsifile ( 1 ) writestring
  136.      epsifile bm tm sub 1 add write==
  137.  
  138.      % Define character and bit widths for the output line buffer
  139.      /cwidth rm lm sub 1 add 8 mul 7 add 8 idiv def
  140.      /bwidth cwidth 8 mul def
  141.      /owidth urx llx sub 1 add 7 add 8 idiv def
  142.      /out cwidth string def
  143.  
  144.      % Create a 1-bit-high device for bitblt to align with the bbox
  145.      gsave
  146.      matrix cwidth 8 mul 1 <00 ff> makeimagedevice setdevice
  147.  
  148.      % 'image' a zero string to clear the line device
  149.      bwidth 1 1 matrix cwidth string image
  150.  
  151.      tm 1 bm
  152.       { % Get a scan line interval from the array device
  153.     arraydevice exch row copyscanlines lm cwidth getinterval
  154.     lmb 0 gt
  155.     { % 'image' it into the line device with the lmb offset
  156.       bwidth 1 1 [1 0 0 1 lmb 0] 5 -1 roll image
  157.       % Now we get the modified scan line
  158.       currentdevice 0 out copyscanlines 0 owidth getinterval
  159.     } if
  160.     % Write out the hex data
  161.     epsifile (% ) writestring 
  162.     epsifile exch writehexstring
  163.     epsifile (\n) writestring
  164.       } for
  165.  
  166.      epsifile (%%EndImage\n) writestring
  167.      epsifile (%%EndPreview\n) writestring
  168.      epsifile flushfile
  169.      grestore
  170.      erasepage initgraphics
  171.  
  172.      DonePage 0 1 put
  173.    } bind def
  174.  
  175.  
  176. (outfile) getenv
  177.   { /outfile exch def 
  178.     ps2epsi
  179.  
  180.     /DonePage 1 string def
  181.     (%stdin) (r) file cvx execute0
  182.     DonePage 0 get 0 eq { showpage } if
  183.   } if
  184.  
  185. end
  186. quit
  187.