home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 Applications 1996 August / Silicon_Graphics_812-0542-002.iso / dist / impr_base.idb / usr / lib / print / rgb2ps_prolog.z / rgb2ps_prolog
Text File  |  1996-08-03  |  11KB  |  385 lines

  1. %!
  2. % A prolog file for the future tops. Written originally by Tektronix
  3. % folks and then adopted into rgb2ps. -suresh 8/16/91
  4. %
  5. % Originating with some postscript by Chris Shaver,
  6. % modified by some code from Rod Belshee for color table lookup,
  7. % all merged, and made to work with binary data by Sean Kamath.
  8. %
  9. %    width height bits/primary x-size y-size orientation LUTlen
  10. %
  11. %    width        - width of one image raster in pixels (integer)
  12. %    height        - number of rasters (integer)
  13. %    bits/primary    - number of bits per primary (1, 2, 4 or 8)
  14. %    zoom        - zoom factor 0 to 1.0
  15. %                    0 = do not zoom
  16. %                    1 = zoom to fit page
  17. %                    .5= zoom to fit half page
  18. %    orientation    - 0 = as needed, 1 = portrait, 2 = landscape (integer)
  19. %    Binary        - 0 = hex, 1 = binary
  20. %    image        - 0 = bw image, 1 = color
  21. %    flip         - 0 = no flip, 1 = flip 
  22. %    angle        - angle of rotation 
  23. %    presolution    - printer resolution
  24. %    ppi        - Pixels Per Inch
  25. %    Triplets    - 0 = CLUT, 1 = RGB triplets
  26. %    [LUTlen]    - length of color map (in triplets, only if CLUT).
  27. %
  28. %    If orientation = 0, the output will be portrait if height >= width
  29. %    or landscape if height < width.
  30. %
  31. %    If flip is true, then the image would be flipped. Used for 
  32. %    Transperancy media.
  33. %
  34. %    The image will always be rotated by user specified angle. 
  35. %
  36. %       The LUT is in RGB triplet form (three bytes per index) and the
  37. %    image data folows (without breaks!)
  38. %
  39. %       There must be NO blank lines after the line containing this data!!!
  40. %       The binary image data must imediately follow this line with only a
  41. %    single intervening CR/LF (or just a CR or just LF) line terminator.
  42. /do_image {
  43.  
  44.  
  45.     % to snarf tokens.
  46.     /readnum {
  47.         currentfile token pop
  48.     } bind def
  49.  
  50.     % Define proc to do the image and print it.
  51.  
  52.     /I_width     readnum def        % Width (in pixels)
  53.     /I_height    readnum def        % Height (in pixels)
  54.     /I_bpp       readnum def        % Bits per pixel
  55.     /Z_factor    readnum def        % Specified zoom 
  56.     /Orientation readnum def        % Specified Orientation
  57.     /Binary      readnum def        % If it's Binary or Hex
  58.     /Image         readnum def        % If it's bw or color
  59.     /Flip         readnum def        % Flip the image
  60.     /Angle         readnum def        % rotate angle
  61.     /Ppi         readnum def        % PixelsPerInch
  62.     /Presolution readnum def        % Printer Resolution
  63.     /Triplets    readnum def        % If it's in triplets
  64.                         % (1 = RGB)
  65.                         % (0 = CLUT)
  66.         
  67.             % set the default Resolution to be 300, if it is
  68.             % not defined.
  69.     Presolution 0 eq { /Presolution 300 def } if
  70.  
  71.     % define mygetbyte to read either ascii or direct bytes
  72.     Binary 0 eq { /mygetbyte { readhexstring } bind def }
  73.             { /mygetbyte { readstring } bind def }
  74.     ifelse
  75.  
  76.     Flip 1 eq { 1 neg 1 scale } if        % Flip the image
  77.     Angle neg rotate            % rotate the image by Angle    
  78.     16 16 neg scale                % Lets work in inches
  79.                         % added neg to mirror
  80.  
  81.     newpath clippath pathbbox        % Get imageable area
  82.     /URy exch def
  83.     /URx exch def
  84.     /LLy exch def
  85.     /LLx exch def
  86.  
  87.     /Width  URx LLx sub 0.005 sub def    % avoid clipping!
  88.     /Height URy LLy sub 0.005 sub def
  89.  
  90.     Ppi 0 gt {                % ppi is set
  91.  
  92.     /X_size Presolution Ppi div I_width mul 72 div  def
  93.     /Y_size Presolution Ppi div I_height mul 72 div def    
  94.     LLx LLy translate        % Position of LL corner
  95.     } % if Ppi is set
  96.     {
  97.     /angle Angle abs def
  98.     /angle_mod angle cvi 90 mod def
  99.     angle_mod 0 eq  {
  100.     Z_factor 0 eq
  101.     {                     % Set X,Y for nonzoom option
  102.           /X_size I_width 72 div def
  103.           /Y_size I_height 72 div  def
  104.     } if
  105.     Z_factor 1 eq                % Zoom the image to fit the page
  106.     {    /X_size 0 def /Y_size 0 def } if
  107.         Z_factor 0 ne Z_factor 1 ne and
  108.         {                                       % Zoom the image by user 
  109.                                                 % requested zoom factor 
  110.                 I_width I_height lt 
  111.                 { /Y_size Height Z_factor mul def /X_size 0 def }
  112.                 { /X_size Width Z_factor mul def /Y_size 0 def  } ifelse
  113.                 
  114.         } if
  115.  
  116.     LLx LLy translate            % Position of LL corner
  117.                         % of imageable area
  118.  
  119.     % If zero Rotate Angle is requested then only determine
  120.     % the Orientation.
  121.     Angle 0 eq {
  122.         % If Orientation was *not* specifed, determine best fit
  123.         Orientation 0 eq             % Determine orientation
  124.         {
  125.             I_height I_width ge
  126.             { /Orientation 1 def }
  127.             { /Orientation 2 def } ifelse
  128.         } if
  129.  
  130.         % Orientation specified as Landscape
  131.         Orientation 2 eq            % Transform to landscape
  132.         {                    %  if necessary
  133.             90 rotate
  134.             0 Width neg translate
  135.             Width Height /Width exch def /Height exch def
  136.         } if
  137.     } if                    % if anlgle = 0
  138.     % default to portrait orientation
  139.  
  140.  
  141.     % neither x or y was requested (i.e., make it as large as
  142.     % possible
  143.     X_size 0 eq  Y_size 0 eq and        % Calculate an X or Y size
  144.     {                    %  for largest possible image
  145.         Width I_width div
  146.         Height I_height div gt
  147.         { /Y_size Height def }
  148.         { /X_size Width def  } ifelse
  149.     } if
  150.  
  151.     % if Y was given, but no X, determine correct X size
  152.     X_size 0 eq                % Calculate X sixe for given
  153.     {                    %  Y size
  154.         I_width I_height gt
  155.         { /X_size I_height I_width div Y_size mul def }
  156.         { /X_size I_width I_height div Y_size mul def } ifelse
  157.         
  158.     } if
  159.  
  160.     % if X was given, but no Y, determine correct Y size
  161.     Y_size 0 eq                % Calculate Y sixe for given
  162.     {                    %  X size
  163.         I_width I_height gt
  164.         { /Y_size I_height I_width div X_size mul def }
  165.         { /Y_size I_width I_height div X_size mul def } ifelse
  166.     } if
  167.  
  168.     } if         % if angle 90 or 270 or 180 or 360
  169.  
  170.     % If non-orthogonal rotation is requested then calculate a new
  171.     % bounding box for the rotated image and a new scaling factor.
  172.     angle 90 ne angle 180 ne and  
  173.     { angle 270 ne angle 360 ne and 
  174.        { angle 0 ne
  175.           {
  176.  
  177.         /I_height I_height 1 sub def
  178.         /I_width I_width 1 sub def
  179.         /NX1 0 def
  180.           /NY1 0 def
  181.     
  182.         /NX2 I_height Angle sin mul def
  183.         /NY2 I_height Angle cos mul def
  184.     
  185.         /NX3_A I_width Angle cos mul def
  186.         /NX3_B I_height Angle sin mul def
  187.         /NX3 NX3_A NX3_B add def
  188.         /NY3_A I_width Angle sin mul def
  189.         /NY3_B I_height Angle cos mul def
  190.         /NY3 NY3_A neg NY3_B add def
  191.         
  192.         /NX4 I_width Angle cos mul def
  193.         /NY4 I_width Angle sin mul neg def
  194.     
  195.         NX1 NX2 gt { /MAX_X1 NX1 def } { /MAX_X1 NX2 def } ifelse
  196.         NX3 NX4 gt { /MAX_X2 NX3 def } { /MAX_X2 NX4 def } ifelse
  197.         MAX_X1 MAX_X2 gt { /MAX_X MAX_X1 def } { /MAX_X MAX_X2 def } ifelse
  198.         
  199.         NY1 NY2 gt { /MAX_Y1 NY1 def } { /MAX_Y1 NY2 def } ifelse
  200.         NY3 NY4 gt { /MAX_Y2 NY3 def } { /MAX_Y2 NY4 def } ifelse
  201.         MAX_Y1 MAX_Y2 gt { /MAX_Y MAX_Y1 def } { /MAX_Y MAX_Y2 def } ifelse
  202.     
  203.         NX1 NX2 lt { /MIN_X1 NX1 def } { /MIN_X1 NX2 def } ifelse
  204.         NX3 NX4 lt { /MIN_X2 NX3 def } { /MIN_X2 NX4 def } ifelse
  205.         MIN_X1 MIN_X2 lt { /MIN_X MIN_X1 def } { /MIN_X MIN_X2 def } ifelse
  206.         
  207.         NY1 NY2 lt { /MIN_Y1 NY1 def } { /MIN_Y1 NY2 def } ifelse
  208.         NY3 NY4 lt { /MIN_Y2 NY3 def } { /MIN_Y2 NY4 def } ifelse
  209.         MIN_Y1 MIN_Y2 lt { /MIN_Y MIN_Y1 def } { /MIN_Y MIN_Y2 def } ifelse
  210.         
  211.         /NI_width MAX_X MIN_X sub abs def
  212.         /NI_height MAX_Y MIN_Y sub abs def
  213.     
  214.         /ScaleX I_width NI_width div def
  215.         /ScaleY I_height NI_height div def
  216.     
  217.         /I_height I_height 1 add def
  218.         /I_width I_width 1 add def
  219.         Z_factor 0 eq
  220.         {                 % Set X,Y for nonzoom option
  221.           /X_size NI_width 72 div def
  222.           /Y_size NI_height 72 div  def
  223.         } if
  224.         Z_factor 1 eq            % Zoom the image to fit the page
  225.         {    /X_size 0 def /Y_size 0 def } if
  226.             Z_factor 0 ne Z_factor 1 ne and
  227.             {                      % Zoom the image by user 
  228.                                                 % requested zoom factor 
  229.                 NI_width NI_height lt 
  230.                   { /Y_size Height Z_factor mul def /X_size 0 def }
  231.                   { /X_size Width Z_factor mul def /Y_size 0 def  } ifelse
  232.             } if
  233.  
  234.         LLx LLy translate        % Position of LL corner
  235.                         % of imageable area
  236.  
  237.         % If zero Rotate Angle is requested then only determine
  238.         % the Orientation.
  239.         Angle 0 eq {
  240.         % If Orientation was *not* specifed, determine best fit
  241.         Orientation 0 eq     % Determine orientation
  242.         {
  243.             NI_height NI_width ge
  244.             { /Orientation 1 def }
  245.             { /Orientation 2 def } ifelse
  246.         } if
  247.  
  248.         % Orientation specified as Landscape
  249.         Orientation 2 eq            % Transform to landscape
  250.           {                    %  if necessary
  251.             90 rotate
  252.             0 Width neg translate
  253.             Width Height /Width exch def /Height exch def
  254.            } if
  255.         } if    % if anlgle = 0
  256.         % default to portrait orientation
  257.  
  258.  
  259.         % neither x or y was requested (i.e., make it as large as
  260.         % possible
  261.         X_size 0 eq  Y_size 0 eq and    % Calculate an X or Y size
  262.         {                %  for largest possible image
  263.         Width NI_width div
  264.         Height NI_height div gt
  265.           { /Y_size Height def }
  266.           { /X_size Width def  } ifelse
  267.         } if
  268.  
  269.         % if Y was given, but no X, determine correct X size
  270.         X_size 0 eq            % Calculate X sixe for given
  271.         {                %  Y size
  272.         NI_width NI_height gt
  273.           { /X_size NI_height NI_width div Y_size mul def }
  274.           { /X_size NI_width NI_height div Y_size mul def } ifelse
  275.         } if
  276.  
  277.         % if X was given, but no Y, determine correct Y size
  278.         Y_size 0 eq            % Calculate Y sixe for given
  279.         {                %  X size
  280.         NI_width NI_height gt
  281.           { /Y_size NI_height NI_width div X_size mul def }
  282.           { /Y_size NI_width NI_height div X_size mul def } ifelse
  283.         } if
  284.         /X_size X_size ScaleX mul 0.62 mul def
  285.         /Y_size Y_size ScaleY mul 0.62 mul def
  286.           } if
  287.        } if
  288.     } if    % if angle ne orthogonal
  289.  
  290.     } ifelse         % not ppi    
  291.     % Center the image on the page
  292.  
  293.     Width  X_size sub 2 div
  294.     Height Y_size sub 2 div
  295.     translate
  296.  
  297.     % Setup for and do the image
  298.  
  299.  
  300. Image 1 eq
  301.     {
  302.     Triplets 0 eq 
  303.     {
  304.         % Color Lookup Table ---------------------------------------
  305.  
  306.         /cmap_len readnum def    % color index lookup table length
  307.         /LUT cmap_len array def  % color index lookup table
  308.  
  309.         cmap_len { currentfile 3 string mygetbyte pop } repeat
  310.     
  311.         LUT astore pop
  312.  
  313.         % Define procedure for colorimage to lookup indicies
  314.         /I_inbuf I_width string def
  315.         /I_outbuf I_width 3 mul string def     % Allocate output raster storage
  316.  
  317.         %--------------------------------------------------
  318.     } % else, handle it further down during the read
  319.     { /I_str I_width 3 mul string def }
  320.     ifelse
  321.     } if        % If only color image
  322.     
  323.     % Start colorimage operator...
  324.     % if ! Triplets
  325.     %   Insert the image data after the colorimage operator, as
  326.     %   one binary bytes per index, with no separators.
  327.     % else
  328.     %   Inset RGB data right after colorimage operator.
  329.  
  330.     X_size Y_size scale            % Scale properly
  331.  
  332.     I_width I_height 8 % I_bpp         % Image size
  333.     [ I_width 0 0 I_height neg 0 I_height ] % Image matrix
  334.  
  335.     % this is the image proc.  It depends on how the data is sent,
  336.     % i.e., if it's RGB triplets (triplets=1) or Color Lookup
  337.     % Table.
  338.  
  339.     Image 1 eq    
  340.     {
  341.     Triplets 0 eq
  342.     {
  343.         % It's a RGB Triplet data stream
  344.         { 
  345.         % /readindex
  346.                 % reads a scan line of indicies into inbuf, looks up the
  347.             % RGB triplets in the LUT, and assembles the triplets for
  348.             % the scan line into outbuf, which is left on the stack
  349.  
  350.             currentfile I_inbuf mygetbyte pop
  351.  
  352.             % for 0..width
  353.             pop
  354.             0 1 I_width 1 sub {
  355.                 I_outbuf           % destination for putinterval
  356.                 1 index            % inbuf index
  357.                 dup 3 mul          % outbuf index
  358.                 LUT              % source string for get
  359.                 I_inbuf
  360.                 4 -1 roll
  361.                 get                % get color index from inbuf
  362.                 get                % get RGB triplet from LUT at index
  363.                 putinterval        % put RGB triplet into outbuf at 3*index
  364.                 pop
  365.             }
  366.             for
  367.             I_outbuf
  368.         } bind                    % Image proc
  369.     } % else
  370.     {
  371.         { currentfile I_str mygetbyte pop } bind
  372.     }
  373.     ifelse
  374.     false 3                    % Image color information
  375.     colorimage
  376.     } 
  377.     { /picstr I_width string def
  378.       { currentfile picstr mygetbyte pop } 
  379.       image
  380.     } ifelse
  381.  
  382.     showpage
  383. } bind def
  384. do_image
  385.