home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / inne / gs300ini / ps2epsi.ps < prev    next >
Encoding:
Text File  |  1994-08-02  |  6.0 KB  |  192 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 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.      dup /HWSize get aload pop
  33.        /devheight exch def
  34.        /devwidth exch def
  35.      /InitialMatrix get
  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.      matrix cwidth 8 mul 1 <00 ff> makeimagedevice 
  146.      /linedevice exch def
  147.  
  148.      % 'image' a zero string to clear the line device
  149.      bwidth 1 1 matrix cwidth string linedevice setdevice image
  150.      arraydevice setdevice
  151.  
  152.      tm 1 bm
  153.       { % Get a scan line interval from the array device
  154.     arraydevice exch row copyscanlines lm cwidth getinterval
  155.     lmb 0 gt
  156.     { % 'image' it into the line device with the lmb offset
  157.       bwidth 1 1 [1 0 0 1 lmb 0] 5 -1 roll linedevice setdevice image
  158.       % Now we get the modified scan line
  159.       linedevice 0 out copyscanlines 0 owidth getinterval
  160.       % Reset the current device
  161.       arraydevice setdevice
  162.     } if
  163.     % Write out the hex data
  164.     epsifile (% ) writestring 
  165.     epsifile exch writehexstring
  166.     epsifile (\n) writestring
  167.       } for
  168.  
  169.      epsifile (%%EndImage\n) writestring
  170.      epsifile (%%EndPreview\n) writestring
  171.      epsifile flushfile
  172.      erasepage initgraphics
  173.  
  174.      DonePage 0 1 put
  175.    } bind def
  176.  
  177.  
  178. (outfile) getenv
  179.   { /outfile exch def 
  180.     ps2epsi
  181.  
  182.     /DonePage 1 string def
  183.     (%stdin) (r) file cvx execute0
  184.     DonePage 0 get 0 eq { showpage } if
  185.   } if
  186.  
  187. end
  188. quit
  189.