home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 September / PCWK996.iso / sharewar / 95 / narzedzi / ambra / ambr32a1.exe / FILE / ARCHIVE.Z / ACROBAT / VIEWER32.EXE / PSPROCSET / 10 < prev    next >
Text File  |  1996-02-22  |  15KB  |  436 lines

  1. %%BeginFile: pdfimg1i.prc
  2. %%Copyright: Copyright 1987-1993 Adobe Systems Incorporated. All Rights Reserved.
  3. % PDF Image Operators for Level 1 (Indexed Color)
  4. % This code is based on David Gelphman's emulation of the indexed colorspace.
  5. % It has been expanded to handle DeviceGray and DeviceCMYK colorspaces.
  6.  
  7. PDF /PDFImage get begin
  8.  
  9. Level2? not StartLoad {
  10.     % Utility function: testsystemdict
  11.     % Determines whether the key on the stack first occurs in systemdict
  12.     /testsystemdict    {    
  13.         false exch where { systemdict eq { pop true } if } if
  14.     } bd
  15.  
  16.     % Variable: NDColors
  17.     % Determine the number of device colors
  18.     /NDColors 1 def    % This will be the value if colorimage does not exist
  19.     ColorImage? StartLoad { 
  20.         /NDColors 0        % Define the number of printer colors
  21.         statusdict begin 
  22.             /processcolors where { 
  23.                 pop    pop    processcolors 
  24.             }{
  25.                 /deviceinfo where { 
  26.                     pop deviceinfo /Colors known { 
  27.                         pop deviceinfo /Colors get 
  28.                     } if
  29.                 } if
  30.             } ifelse
  31.         end        % statusdict
  32.         def
  33.         NDColors 0 ne {
  34.             /colorimage testsystemdict
  35.             /setcolortransfer testsystemdict and
  36.             /currentcolortransfer testsystemdict and
  37.             /currentcmykcolor testsystemdict and not {
  38.             % better make sure that lookups on colorimage, setcolortransfer and 
  39.             % currentcolortransfer will result in the systemdict functionality, 
  40.             % else our transfer function strategy will not work. If this is the 
  41.             % case, use slow but reliable technique
  42.                 /NDColors 0 def 
  43.             } if
  44.         } if
  45.         % if NDColors isn't 1,3,4 then it should be 0
  46.         NDColors dup 1 ne exch dup 3 ne exch 4 ne and and { 
  47.             /NDColors 0 def
  48.         }if    
  49.     } EndLoad     % Colorimage?
  50.  
  51.     /BaseSpace? { ColorSpace 1 get } bd
  52.     
  53.     % NDColors is not 0 (Optimized cases)
  54.     % The Gray CLUT code should always be available, even on RGB/CMY and
  55.     % CMYK devices. We want to set the transfer function and then do the image. 
  56.     % Note: we could use color image if it was available, but it may be slower 
  57.     % than converting the CLUT and then doing an image. Might want to test this 
  58.     % in the future.
  59.     NDColors 0 ne StartLoad {
  60.         % *Internal* Name: rgbtogray
  61.         % Convert an RGB value to a gray value.
  62.         /rgbtogray {    % r g b rgbtogray gray
  63.             0.11 mul exch 0.59 mul add exch 0.3 mul add round cvi
  64.         } bd
  65.         
  66.         % *Internal*    Name: ConvRGBtoGray
  67.         % Takes in an RGB CLUT and creates a gray CLUT out of it
  68.         /ConvRGBtoGray {                    % RGB
  69.             dup length 3 idiv                 % RGB #entries
  70.             0 1 3 -1 roll 1 sub {                % RGB loop
  71.                 1 index exch 2 copy 3 mul    % RGB RGB loop RGB idx
  72.                 2 copy get 3 1 roll            % RGB RGB loop R RGB idx
  73.                 2 copy 1 add get 3 1 roll    % RGB RGB loop R G RGB idx
  74.                 2 add get 
  75.                 rgbtogray 
  76.                 put        % RGB
  77.             } for
  78.             dup length 3 idiv 0 exch getinterval    % GrayCLUT
  79.         } bd
  80.         
  81.         % *Internal* Name: cmyktogray
  82.         % Convert a CMYK value to a gray value.
  83.         /cmyktogray {    % c m y k cmyktogray gray
  84.             exch 0.11 mul add exch 0.59 mul add exch 0.3 mul add round cvi
  85.             dup 255 gt { pop 255 } if
  86.             255 exch sub    
  87.         } bd
  88.         
  89.         % *Internal*    Name: ConvCMYKtoGray
  90.         % Takes in an CMYK CLUT and creates a gray CLUT out of it
  91.         /ConvCMYKtoGray {                    % CMYK
  92.             dup length 4 idiv                 % CMYK #entries
  93.             0 1 3 -1 roll 1 sub {            % CMYK loop
  94.                 1 index exch 2 copy 4 mul    % CMYK CMYK loop CMYK idx
  95.                 2 copy get 3 1 roll            % CMYK CMYK loop C CMYK idx
  96.                 2 copy 1 add get 3 1 roll    % CMYK CMYK loop C M CMYK idx
  97.                 2 copy 2 add get 3 1 roll    % CMYK CMYK loop C M Y CMYK idx
  98.                 3 add get cmyktogray put    % CMYK
  99.             } for
  100.             dup length 4 idiv 0 exch getinterval    % GrayCLUT
  101.         } bd
  102.  
  103.         % Utility function: setupgrayclut
  104.         % Create gray subcluts.
  105.         /setupgrayclut {
  106.             /CIbit1x CIgclut length 1 sub pv        
  107.         } bd
  108.     
  109.         % Utility function: 1compute
  110.         % This proc converts a CLUT index into a grayscale value.
  111.         /1compute { 
  112.             exch 2 BitsPerComponent exp 1 sub mul round cvi 
  113.             dup CIbit1x gt { pop pop 0 } { get 255 div } ifelse
  114.         } bd
  115.  
  116.         /DoGrayCLUT {
  117.             % Set up the transfer function.
  118.             ColorSpace 3 get /CIgclut exch pv setupgrayclut
  119.             [ { CIgclut 1compute } /exec load dup currenttransfer exch ] 
  120.             cvx settransfer
  121.             % Do the B/W image
  122.             Width Height BitsPerComponent ImageMatrix ImageFilter image
  123.         } bd
  124.     } EndLoad
  125.  
  126.     % NDColors is 1
  127.     % We're dealing strictly with a BW device.
  128.     NDColors 1 eq StartLoad {
  129.         % Utility function: Indexed
  130.         % This proc will be run at image time, it does the indexed color stuff
  131.         /Indexed [ 1 
  132.             {
  133.                 % Convert the CLUT if necessary.
  134.                 BaseSpace? /DeviceRGB eq { 
  135.                     ColorSpace dup 3 get ConvRGBtoGray 3 exch put
  136.                 } {
  137.                     BaseSpace? /DeviceCMYK eq { 
  138.                         ColorSpace dup 3 get ConvCMYKtoGray 3 exch put
  139.                     } if
  140.                 } ifelse
  141.                 
  142.                 DoGrayCLUT
  143.             }
  144.         ] InstallImage
  145.     } EndLoad
  146.     
  147.     % What follows are some utility functions that are used in both the 3 or 4 
  148.     % color cases.
  149.     NDColors dup 3 eq exch 4 eq or StartLoad {
  150.  
  151.         /NULLPROC {} bd    
  152.  
  153.         % Utility function: concatutil
  154.         % This proc is helps to concatenate transfer functions 
  155.         /concatutil {                     % a b c d [ newtransferfunct
  156.             /exec load                     % a b c d [ newtransferfunct exec
  157.             7 -1 roll /exec load         % b c d [ newtransferfunct exec a exec
  158.         } bd
  159.     
  160.         % Utility function: spconcattransfer
  161.         % This proc is designed specifically to handle the problem of 
  162.         % concatenating COLOR transfer functions as compactly as possible and 
  163.         % to share the code between the 3 and 4 color cases
  164.         /spconcattransfer { % key RorCtable GorMtable BorYtable GRorKtable
  165.             /Dclut exch pv
  166.             /Cclut exch pv
  167.             /Bclut exch pv
  168.             /Aclut exch pv
  169.             /ncompute exch load pv
  170.     
  171.             currentcolortransfer                    % r g b gr
  172.             
  173.             [ { Aclut ncompute } concatutil ] cvx      % g b gr {r'}
  174.             [ { Bclut ncompute } concatutil ] cvx     % b gr {r'} {g'}
  175.             [ { Cclut ncompute } concatutil ] cvx    % gr {r'} {g'} {b'}
  176.             [ { Dclut ncompute } concatutil ] cvx     % {r'} {g'} {b'} {gr'}
  177.             setcolortransfer
  178.         } bd
  179.     
  180.         % Utility function: pvsubclut
  181.         % Utility function to help create subcluts
  182.         /pvsubclut {    % key str index length-1                
  183.             1 add getinterval pv
  184.         } bd
  185.     } EndLoad
  186.     
  187.     % NDColors is 3
  188.     % We're dealing with either a RGB or a CMY device. In this case, we want to 
  189.     % set the color transfer functions and then do a colorimage.
  190.     NDColors 3 eq StartLoad {    
  191.         % Utility function: cmyktorgb
  192.         % Convert a CMYK value to an rgb value.
  193.         /cmyktorgb {    % c m y k cmyktorgb r g b
  194.             dup 5 -1 roll add 255 exch sub 4 1 roll        % r=255-(k+c) m y k
  195.             dup 4 -1 roll add 255 exch sub 3 1 roll     % r g=255-(k+g) y k
  196.             add 255 exch sub                            % r g b=255-(y+k)
  197.             3 { dup 0 lt { pop 0 } if cvi 3 1 roll } repeat
  198.         } bd
  199.         
  200.         % Utility function: ConvCMYKtoRGB
  201.         % Takes in an CMYK CLUT and creates a RGB CLUT out of it
  202.         % It looks like we're building the RGB CLUT backwards, but we're not.
  203.         /ConvCMYKtoRGB {                            % CLUT
  204.             dup length 4 idiv                         % CLUT #entries
  205.             0 1 3 -1 roll 1 sub {                    % CLUT loop
  206.                 dup 3 mul exch 4 mul                % CLUT RGBidx CMYKidx
  207.                 2 index exch 4 getinterval { } forall    % CLUT RGBidx C M Y K
  208.                 cmyktorgb                            % CLUT RGBidx R G B
  209.                 5 -2 roll 2 copy 7 -1 roll put        % G B CLUT RGBidx
  210.                 2 copy 1 add 6 -1 roll put            % B CLUT RGBidx
  211.                 2 copy 2 add 5 -1 roll put            % CLUT RGBidx
  212.                 pop                                    % CLUT
  213.             } bind for
  214.             dup length 4 idiv 3 mul 0 exch getinterval    % RGBCLUT
  215.         } bd
  216.  
  217.         % Utility function: setuprgbcluts
  218.         % Utility function to create subcluts
  219.         /setuprgbcluts {
  220.             /CIbit3x CIrgbclut length 3 sub pv        
  221.             /CIrclut CIrgbclut pv % make substr of clut for easy indexing
  222.             /CIgclut CIrclut 1 CIbit3x pvsubclut    % green = red + 1
  223.             /CIbclut CIrclut 2 CIbit3x pvsubclut    % blue  = red + 2
  224.         } bd
  225.     
  226.         % Utility function: 3compute
  227.         % Utility function to convert CLUT index into a color value
  228.         /3compute {            % value color_clut
  229.             exch 2 BitsPerComponent exp 3 mul 3 sub
  230.             mul             % color_clut clut_index
  231.             round            % round so that we get to nearest int
  232.             cvi             % must be an int to index into clut
  233.             dup CIbit3x gt { pop pop 0 } { get 255 div } ifelse
  234.         } bd                
  235.     
  236.         /DoRGBCLUT {
  237.             % Set up the color transfer function
  238.             ColorSpace 3 get /CIrgbclut exch pv setuprgbcluts
  239.             /3compute CIrclut CIgclut CIbclut dup spconcattransfer
  240.             % Do the color image
  241.             Width Height BitsPerComponent ImageMatrix ImageFilter
  242.             [ exch /exec load /dup load dup ] cvx /NULLPROC load dup true 3 
  243.             colorimage
  244.         } bd
  245.  
  246.         % Install the image Indexed colorspace handler (3 color device).
  247.         /Indexed [ 1
  248.             {
  249.                 % Convert the CLUT if necessary.
  250.                 BaseSpace? /DeviceCMYK eq { 
  251.                     ColorSpace dup 3 get ConvCMYKtoRGB 3 exch put
  252.                 } if 
  253.  
  254.                 BaseSpace? /DeviceGray eq { DoGrayCLUT } 
  255.                     { DoRGBCLUT } ifelse
  256.             } bind 
  257.         ] InstallImage
  258.     } EndLoad
  259.     
  260.     % NDColors is 4
  261.     % We're dealing with a CMYK device. In this case, we want to set the color
  262.     % transfer functions and then do a colorimage.
  263.     NDColors 4 eq StartLoad
  264.     {
  265.         % Utility function: rgbtocmyk
  266.         % Convert RGB to CMYK
  267.         /rgbtocmyk { % r g b rgbtocmyk c m y k
  268.             3 { 255 div 3 1 roll } repeat
  269.             setrgbcolor currentcmykcolor
  270.             4 { 1 exch sub 255 mul round cvi 4 1 roll } repeat
  271.         } bd
  272.         
  273.         % Utility function: ConvRGBtoCMYK
  274.         % Takes in an RGB CLUT and creates a CMYK CLUT out of it
  275.         % It looks like we're building the CMYK CLUT backwards, but we're not.
  276.         /ConvRGBtoCMYK {                            % RGB
  277.             dup length 3 idiv                         % RGB #entries
  278.             dup 4 mul string 3 1 roll                % CMYK RGB #entries
  279.             0 1 3 -1 roll 1 sub {                    % CMYK RGB loop
  280.                 dup 4 mul 3 index length 4 sub exch sub % CMYK RGB loop CMYKidx
  281.                 3 1 roll 3 mul                        % CMYK CMYKidx RGB RGBidx
  282.                 1 index exch 3 getinterval {} forall % CMYK CMYKidx RGB R G B
  283.                 rgbtocmyk                            % CMYK CMYKidx RGB C M Y K
  284.                 7 -2 roll 2 copy 8 -1 roll put        % RGB M Y K CMYK CMYKidx
  285.                 2 copy 1 add 7 -1 roll put            % RGB Y K CMYK CMYKidx
  286.                 2 copy 2 add 6 -1 roll put            % RGB K CMYK CMYKidx
  287.                 2 copy 3 add 5 -1 roll put            % RGB CMYK CMYKidx
  288.                 pop exch                            % CMYK RGB
  289.             } bind for
  290.             pop                                        % CMYK
  291.         } bd
  292.  
  293.         % Utility function: setupcmykcluts
  294.         /setupcmykcluts {
  295.             /CIbit4x CIcmykclut length 4 sub pv        
  296.             /CIcclut CIcmykclut pv % Make substr of CLUT for easy indexing
  297.             /CImclut CIcclut 1 CIbit4x pvsubclut    % magenta = cyan + 1
  298.             /CIyclut CIcclut 2 CIbit4x pvsubclut    % yellow  = cyan + 2
  299.             /CIkclut CIcclut 3 CIbit4x pvsubclut    % black   = cyan + 3
  300.         } bd
  301.     
  302.         % Utility function: 4compute
  303.         % Convert CLUT index into a color value
  304.         /4compute { 
  305.             exch 2 BitsPerComponent exp 4 mul 4 sub mul round cvi 
  306.             dup CIbit4x gt { pop pop 0 } { get 255 div } ifelse
  307.         } bd
  308.     
  309.         /DoCMYKCLUT {
  310.             % Set up the color transfer function
  311.             ColorSpace 3 get /CIcmykclut exch pv setupcmykcluts
  312.             /4compute CIcclut CImclut CIyclut CIkclut spconcattransfer
  313.             % Do the color image
  314.             Width Height BitsPerComponent ImageMatrix ImageFilter
  315.             [ exch /exec load /dup load dup dup ] cvx 
  316.             /NULLPROC load dup dup true 4 colorimage
  317.         } bd
  318.         
  319.         % Install the image Indexed colorspace handler (4 color device).
  320.         /Indexed [ 1
  321.             { 
  322.                 % Convert the CLUT if necessary.
  323.                 BaseSpace? /DeviceRGB eq { 
  324.                     ColorSpace dup 3 get ConvRGBtoCMYK 3 exch put
  325.                 } if 
  326.  
  327.                 BaseSpace? /DeviceGray eq {    DoGrayCLUT } { DoCMYKCLUT } ifelse
  328.             } bind 
  329.         ] InstallImage
  330.     } EndLoad
  331.     
  332.     % NDColors is 0
  333.     % This is the ultra-conservative case. We're not sure what the number of 
  334.     % device colors is and/or we may be performing a separation. In this case,
  335.     % do a lookup in the CLUT for every sample.
  336.     % Gray Limits:
  337.     % 65535/4 = 16384 BYTES = 131072 BITS = 65536 SAMPLES WIDE @ 2 BPC
  338.     % 65535/2 =    32768 BYTES = 262144 BITS = 65536 SAMPLES WIDE @ 4 BPC
  339.     % RGB Limits:
  340.     % 65535/12 = 6561 BYTES = 43690 BITS = 21845 SAMPLES WIDE @ 2 BPC
  341.     % 65535/6 =    10922 BYTES = 87380 BITS = 21845 SAMPLES WIDE @ 4 BPC
  342.     % 65535/3 =    21845 BYTES = 174760 BITS = 21845 SAMPLES WIDE @ 8 BPC
  343.     % CMYK Limits:
  344.     % 65535/16 = 4096 BYTES = 32768 BITS = 16384 SAMPLES WIDE @ 2 BPC
  345.     % 65535/8 =     8192 BYTES = 65536 BITS = 16384 SAMPLES WIDE @ 4 BPC
  346.     % 65535/4 =    16384 BYTES = 131072 BITS = 16384 SAMPLES WIDE @ 8 BPC
  347.     % at 300 dpi, 11 inch wide paper has room for 3300 samples
  348.     % at 1500 dpi, 11 wide paper has room for 16500 samples
  349.     % at 1985 dpi, 11 wide paper has room for 21835 samples
  350.     % This means we're likely to have problems with CMYK images at 1985 dpi
  351.     % or higher.
  352.     NDColors 0 eq StartLoad {
  353.         /CICSLookup 3 dict begin
  354.             /DeviceGray {
  355.                 get                 % expstr j gclut expstr j value  
  356.                 put exch             % expstr gclut j
  357.                 1 add exch 3 copy    % expstr j+1 gclut expstr j+1 gclut
  358.             } bd
  359.             
  360.             /DeviceRGB {
  361.                 3 mul         % expstr j rgbclut expstr j rgbclut 3xstrelement(i)
  362.                 3 getinterval     % expstr j rgbclut expstr j 3elementstring  
  363.                 putinterval exch     % expstr rgbclut j
  364.                 3 add exch 3 copy    % expstr j+3 rgbclut expstr j+3 rgbclut
  365.             } bd
  366.             
  367.             /DeviceCMYK {
  368.                 4 mul     % expstr j cmykclut expstr j rgbclut 4xstrelement(i)
  369.                 4 getinterval     % expstr j cmykclut expstr j 4elementstring  
  370.                 putinterval exch     % expstr cmykclut j
  371.                 4 add exch 3 copy    % expstr j+4 cmykclut  expstr j+4 cmykclut
  372.             } bd
  373.         currentdict end pv
  374.         /CIBLookup 3 dict begin
  375.             8 { lookupandstore } bd
  376.         
  377.             4 {    % expstr j rgbclut expstr j rgbclut stringelement(i)
  378.                 /shiftbyte 1 index pv
  379.                 -4 bitshift            % left 4 bits of string element
  380.                 lookupandstore        % lookup and put into expstr
  381.                 shiftbyte 15 and    % right 4 bits of string element
  382.                 lookupandstore        % lookup and put into expstr
  383.             } bd
  384.     
  385.             2 {    % expstr j rgbclut expstr j rgbclut stringelement(i)
  386.                 /shiftbyte 1 index pv
  387.                 -6 bitshift            % left 2 bits of string element
  388.                 lookupandstore        % lookup and put into expstr
  389.                 shiftbyte -4 bitshift    
  390.                 3 and                % next 2 bits of string element
  391.                 lookupandstore        % lookup and put into expstr
  392.                 shiftbyte -2 bitshift    
  393.                 3 and                % next 2 bits of string element
  394.                 lookupandstore        % lookup and put into expstr
  395.                 shiftbyte 3 and        % right 2 bits of string element
  396.                 lookupandstore        % lookup and put into expstr
  397.             } bd
  398.         currentdict end pv
  399.     
  400.         /colorexpand {    % unexpandedstr
  401.             BufferExp 0 CLUT        % unexpstr expstr j CLUT 
  402.             3 copy                     % unexpstr expstr j CLUT expstr j CLUT
  403.             7 -1 roll                 % expstr j CLUT expstr j CLUT unexpstr
  404.                                     % expstr j CLUT expstr j CLUT strelement(i)    
  405.             /BitLookup load            % proc on stack for 'forall'
  406.             forall 
  407.             pop pop pop pop pop
  408.         } bd
  409.                 
  410.         % Install the image Indexed colorspace handler (conservative case).
  411.         /Indexed [ 1
  412.             {
  413.                 /CLUT ColorSpace 3 get pv
  414.                 /BitLookup CIBLookup BitsPerComponent get pv
  415.                 /lookupandstore BaseSpace? CICSLookup exch get pv
  416.                 /BufferExp CIBuffer length 8 mul BitsPerComponent idiv 
  417.                 BaseSpace? ColorComps? mul string pv
  418.                 
  419.                 Width Height 8 ImageMatrix 
  420.                 [
  421.                     ImageFilter /exec load /colorexpand load /exec load 0 
  422.                     Width BaseSpace? ColorComps? mul /getinterval load /exec 
  423.                     load 
  424.                 ] cvx 
  425.                 BaseSpace? ColorComps? ColorImage
  426.             } bind 
  427.         ] InstallImage
  428.     } EndLoad
  429.  
  430. currentdict readonly pop
  431.  
  432. } EndLoad % Level2? not
  433.  
  434. end
  435. %%EndFile
  436.