home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / unix / scribepi.sha / scribepic.ps < prev   
Encoding:
Text File  |  1985-08-29  |  5.0 KB  |  162 lines

  1. %!
  2. % Postscript pre-amble to parse scribepic output.  This is a fairly
  3. % straightforward translation of Peter Rowley's (U Toronto) C code to
  4. % do the same thing. 
  5. %
  6. %   Originally converted to PostScript by J.W. Peterson
  7. %   Arbitrary bitmap sizing added by J. Schimpf
  8. %   Margin & format control added by J. Schimpf
  9. %
  10. %   SEE SCRIBEPIC info sheet for the use of this file
  11. %   YOU SHOULDN'T TRY TO EDIT THIS FILE - IT IS COPIED
  12. %   FOR YOU.
  13. %
  14. %---------------------------------------------------------------------
  15. %
  16. %      HEX FILE FORMAT
  17. %
  18. %      <Invert> <XBYTES> <YSIZE> <Margin> <Height> <Move UP>
  19. %      <Hex Data.......>
  20. %             
  21. %      Where Invert -> 1 For straight through data
  22. %                      0 To invert data
  23. %      Where YSIZE  ->  # Of Scan lines
  24. %            XBYTES  -> # Bytes/scan line
  25. %            Margin  -> Width of the scribe column (in inches)
  26. %            Height  -> Height of picture (in inches)
  27. %            Move UP -> Distance picture is moved up (in inches)
  28. %
  29. %      Hex data is of the form:
  30. %
  31. %      <Count><Data>   -> If <Count> < 0x80 then this is count
  32. %                         of # bytes to read here
  33. %                      -> If <Count> >= 0x80 This is count of # times
  34. %                         next byte is to be repeated
  35. %
  36. %--------------------------------------------------------------------------
  37. %  Open file routine
  38. /infile (%stdin) (r) file def        % This gets around broke "currentfile"?
  39.  
  40. %--------------------------------------------------------------------------
  41. %  Get a byte from file routine
  42. /getbyte 
  43.   { infile (x) readhexstring 
  44.     pop 0 get } def              % Reads 1 byte of data
  45.  
  46. %-------------------------------------------------------------------------
  47. %  Useful routines & definitions
  48. /inch { 72 mul } def            % Conversion inches => points
  49. /inc { 1 add } def            % increment TOS
  50.                                        
  51. %--------------------------------------------------------------------------
  52. %  Read file and form 1 scan line
  53. /parseline
  54. {
  55.   /in_pos 0 def                % output array pos
  56.   {
  57.      in_pos xbytes lt            % while in_pos < xbytes DO...
  58.      {
  59.          /cnt getbyte def        % get run length
  60.   
  61.     cnt 127 gt
  62.       { /cnt cnt 256 sub def }     % if cnt > 128, cnt -= 256;
  63.     if
  64.  
  65.     cnt 0 ge            % if cnt >= 0...
  66.     {  
  67.            cnt inc             % for count+1 times...
  68.       { scanline in_pos getbyte put % scanline[in_pos] = getbyte...
  69.         /in_pos in_pos inc def    % in_pos++
  70.       } repeat            
  71.     }
  72.                      % ELSE
  73.     {
  74.       /data_byte getbyte def    % fetch the run data
  75.       cnt neg 1 add            % -cnt + 1
  76.       { scanline in_pos data_byte put   % scanline[in_pos] = getbyte...
  77.         /in_pos in_pos inc def    % in_pos++
  78.       } repeat
  79.     }
  80.     ifelse
  81.      }
  82.      { exit }                % Exit loop if !(in_pos < xbytes)
  83.      ifelse
  84.  
  85.   }
  86.   loop
  87.   scanline                % leave the data on the stack
  88. } def
  89.  
  90.  
  91. %----------------------------------------------------------------------
  92. %  MAIN ROUTINE
  93. %
  94. % Group all the operations to draw the picture into one procedure, to
  95. % minimize confusion between PostScript commands and hex data...
  96. %
  97. /drawpaint 
  98. {
  99.   gsave                        % Push graphics enviroment
  100.    
  101. %  Read if this is to be inverted or not put onto the stack, then decide
  102. %  If we should invert the data
  103.  
  104.     infile token pop
  105.     1 eq
  106.     {
  107.        /invstat true def               % Don't Invert the data
  108.     }
  109.     {
  110.        /invstat false def              % Invert the data
  111.     }
  112.    ifelse
  113.  
  114. %  Get the X & Y size from the file
  115.  
  116.     /xbytes infile token pop def     % Bytes/scanline
  117.     /ysize infile token pop def      % # Scan lines
  118.  
  119. %  Get the margin, height & upward movement
  120.  
  121.     /margin infile token pop def     % Default usually 6.5 in
  122.     /height infile token pop def     % Default usually 2   in
  123.     /up     infile token pop def     % Default usually 0
  124.  
  125. %  Generate some constants we will need
  126.  
  127.     /scanline xbytes string def      % Create output scan buffer
  128.     /nysize ysize neg def            %  Used in matrix to define size
  129.     /xbits xbytes 8 mul def          % Calculate # pixels/scanline
  130.     /width height xbits ysize div mul def  % Width = height * (x / y)
  131.  
  132. % Generate the transformation to place the "unit square" imagemask uses.
  133.  
  134.   margin width sub 2 div inch    % center on page (with scribe margins)
  135.                   % with x xlate = (margin - width) / 2
  136.   up inch            % Y translate by UP translate
  137.   translate             % Center image on the page.
  138.  
  139.   width inch            % Adjust X size to keep aspect ratio right
  140.   height inch             % convert Y height to inches
  141.   scale 
  142.  
  143. %------------------------------------------------------------------
  144. %  Generate image in unit box
  145.  
  146.        %  The next 5 things are placed on the stack for
  147.        %  the imagemask function
  148.  
  149.        xbits                   % # Pixels/Scanline
  150.        ysize                   % # Scan lines
  151.  
  152.        invstat               % Use invstat to choose pixel black/white
  153.     [ xbits 0 0 nysize 0 ysize ] % Bit map description matrix
  154.     { parseline }                % Routine to read scan lines
  155.   imagemask             % Move bitmap to page
  156.   grestore                       % Restore graphics enviroment
  157. } def
  158.  
  159. % Put the hex data directly after the "drawpaint" command...
  160. drawpaint
  161.