home *** CD-ROM | disk | FTP | other *** search
/ jppd.dyndns.org / jppd.dyndns.org.tar / jppd.dyndns.org / QUERYPRO / Actualizar / Impressora_PDF / converter.exe / GPLGS / gs_icc.ps < prev    next >
Text File  |  2003-12-13  |  11KB  |  336 lines

  1. %    Copyright (C) 2001, 2002 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: gs_icc.ps,v 1.7.2.1 2003/12/12 22:07:58 giles Exp $
  14. % ICCBased color space method dictionaries.
  15. % This assumes gs_ciecs2.ps has already been processed.
  16.  
  17. %
  18. % Note that the substitution procedure in this routine will dynamically
  19. % check for support of ICCBased color space. If such support is not
  20. % provided, the alternative color space will be used.
  21. %
  22. % The validation routine in dictionary (cs_validate) performs a more
  23. % extensive validation than is done for other color spaces, because
  24. % .seticcspace does less checking than most other color space setting
  25. % operators.
  26. %
  27.  
  28.  
  29. .currentglobal true .setglobal
  30. .cspace_util begin
  31.  
  32. %
  33. % A dictionary for mapping the number of components of an ICCBased color
  34. % space to the appropriate alternative color space. This is used only
  35. % if an alternative color space is not specifically provided.
  36. %
  37. /icc_comp_map_dict
  38.   mark 1 /DeviceGray 3 /DeviceRGB 4 /DeviceCMYK .dicttomark
  39. def
  40.  
  41. %
  42. %   <array1>   get_icc_alternative_space   <name | array2>
  43. %
  44. % Get the alternative color space for an ICCBased color space.
  45. %
  46. /get_icc_alternative_space
  47.   {
  48.     1 get dup /Alternate .knownget
  49.       { exch pop }
  50.       { /N get //icc_comp_map_dict exch get }
  51.     ifelse
  52.   }
  53. bind def
  54.  
  55.  
  56. colorspacedict
  57. /ICCBased
  58.   mark
  59.     /cs_potential_indexed_base true
  60.     /cs_potential_pattern_base true
  61.     /cs_potential_alternate true
  62.     /cs_potential_icc_alternate false
  63.     /cs_get_ncomps { 1 get /N get } bind
  64.  
  65.     /cs_get_range
  66.       {
  67.         1 get dup /Range .knownget
  68.           { exch pop }
  69.           { /N get 2 mul //dflt_range_4 exch 0 exch getinterval }
  70.         ifelse
  71.       }
  72.     bind
  73.  
  74.     /cs_get_default_color { 1 get /N get { 0 } repeat } bind
  75.  
  76.     %
  77.     % For generating a gray, RGB, or CMYK equivalent color, we will
  78.     % assume that the alternative color space provides reasonable
  79.     % mapping.
  80.     /cs_get_currentgray
  81.       { //get_icc_alternative_space exec //.cs_get_currentgray exec }
  82.     bind
  83.     /cs_get_currentrgb
  84.       { //get_icc_alternative_space exec //.cs_get_currentrgb exec }
  85.     bind
  86.     /cs_get_currentcmyk
  87.       { //get_icc_alternative_space exec //.cs_get_currentcmyk exec }
  88.     bind
  89.  
  90.     % a lot of validation is done by the cs_validate method
  91.     /cs_validate
  92.       {
  93.         //check_cie_cspace exec
  94.         dup 1 get
  95.         dup /N get
  96.         dup type /integertype ne
  97.           //setcspace_typecheck
  98.         if
  99.         //icc_comp_map_dict exch known not
  100.           //setcspace_rangecheck
  101.         if
  102.         dup /DataSource get
  103.         dup type dup /stringtype ne exch /filetype ne and
  104.           //setcspace_typecheck
  105.         if
  106.         rcheck not
  107.           //setcspace_invalidaccess
  108.         if
  109.         dup /Range .knownget
  110.           {
  111.             //check_array exec
  112.               {
  113.                 type dup /integertype ne exch /realtype ne and
  114.                   //setcspace_typecheck
  115.                 if
  116.               }
  117.             forall
  118.           }
  119.         if
  120.         /Alternate .knownget
  121.           {
  122.             //.cs_validate exec
  123.             //.cs_potential_icc_alternate exec not
  124.               //setcspace_rangecheck
  125.             if
  126.           }
  127.         if
  128.       }
  129.     bind
  130.  
  131.     % substitute the Alternate space, if appropriate
  132.     /cs_substitute
  133.       {
  134.         %
  135.         % A design problem in the Ghostscript graphic library color
  136.         % space code prevents an ICCBased color space from having an
  137.         % ICCBased alternative color space. This situation actually
  138.         % arises fairly frequently in PDF, as an ICCBased color space
  139.         % is used as the substitute color for a Device{Gray|RGB|CMYK}
  140.         % color space, which in turn are used as the alternative color
  141.         % space to another (or possibly even the same) ICCBased color
  142.         % space.
  143.         %
  144.         % This situation causes no fundamental problems, as
  145.         % Ghostscript nominally supports ICCBased color spaces, so the
  146.         % Alternate color space is not used. Where this is not true
  147.         % (primarily because the NOCIE option is selected), the code
  148.         % would (except for the design flaw noted above) select the
  149.         % Alternate of the Alternate color space.
  150.         %
  151.         % The code below works around this problem by suprressing
  152.         % color space substitution for alternative color spaces if
  153.         % the substituting space is an ICCBased color space.
  154.         %
  155.         dup //get_icc_alternative_space exec
  156.         //.cs_substitute exec
  157.         2 copy eq
  158.         1 index //.cs_potential_icc_alternate exec not
  159.         or
  160.           { pop pop dup }
  161.           {
  162.             % retain just the new Alternate space
  163.             exch pop
  164.  
  165.             % build all new structures in local VM
  166.             .currentglobal 3 1 roll //false .setglobal
  167.  
  168.             % copy the original ICCBased color space array
  169.             1 index dup length array copy
  170.  
  171.             % copy the ICCBased dictionary
  172.             dup 1 2 copy get dup length dict copy
  173.  
  174.             % insert the new alterante color space
  175.             dup /Alternate 7 -1 roll put
  176.  
  177.             % insert the new dictionary into the arra
  178.             put
  179.  
  180.             % restore the VM mode
  181.             3 -1 roll .setglobal
  182.           }
  183.         ifelse
  184.       }
  185.     bind
  186.  
  187.     %
  188.     % The current implementation of ICCBased color spaces requires the
  189.     % DataSource to be a file.
  190.     %
  191.     /cs_prepare
  192.       {
  193.         % make DataSource a file
  194.         dup 1 get /DataSource get type /stringtype eq
  195.           {
  196.             % build all new structures in local VM
  197.             .currentglobal exch //false .setglobal
  198.  
  199.             % check if we need to copy the color space and dictionary
  200.             2 copy eq
  201.               {
  202.                 dup length array copy
  203.                 dup 1 2 copy get dup length dict copy put
  204.               }
  205.             if
  206.  
  207.             % fetch DataSource, setting up stack for multiple puts
  208.             dup 1 2 copy get dup /DataSource 2 copy get
  209.  
  210.             % convert the string into a file
  211.             /ReusableStreamDecode filter
  212.  
  213.             % put the file into the dictioary, the dictionary into the array
  214.             put put
  215.  
  216.             % restore the VM mode
  217.             exch .setglobal
  218.           }
  219.         if
  220.       }
  221.     bind
  222.  
  223.     %
  224.     % Install the current color space.
  225.     %
  226.     % The current Ghostscript color space implementation requires that
  227.     % color spaces that provide a base or alternative color space set
  228.     % that base/alternative color space to be the current color space
  229.     % before attempting to set the original color space. This can cause
  230.     % difficulty if an ICCBased color space is being used as a substitute
  231.     % color space for a device-specific color space, and uses that same
  232.     % device-specific color space as an alternative space. For this
  233.     % reason, a special _setcolorspace_nosub operator is provided.
  234.     %
  235.     /cs_install
  236.       {
  237.         % set the alternative color space to be the current color space
  238.         dup //get_icc_alternative_space exec //_setcolorspace_nosub exec
  239.  
  240.         % check for native support
  241.         /.seticcspace where
  242.           { pop //false }
  243.           { //true }
  244.         ifelse
  245.         NOCIE or
  246.           //pop_1       % do nothing
  247.           {
  248.             % Acrobat Reader silently ignores errors with ICC profiles
  249.             % and uses the alternate color space -- do the same.
  250.             mark exch 1 get
  251.               { .seticcspace }
  252.             .internalstopped
  253.             cleartomark
  254.           }
  255.         ifelse
  256.       }
  257.     bind
  258.  
  259.     % for now, the alternative spaces for an ICCBased color space do 
  260.     % not require special preparation
  261.     /cs_prepare_color { dup 1 get /N get //check_num_stack exec pop } bind
  262.     /cs_complete_setcolor //pop_1
  263.  
  264.   .dicttomark
  265. put
  266.  
  267. end     % .cspace_util
  268.  
  269. NOPSICC { (%END PSICC) .skipeof } if
  270. % Now set up ICC profile loading for PostScript %%BeginICCProfile sections.
  271.  
  272. systemdict begin
  273. /.ProcessICCcomment { % file comment --  file comment
  274.   dup
  275.   (%%BeginICCProfile) anchorsearch {
  276.     pop pop
  277.     DEBUG { (.ProcessICCcomment found %%BeginICCProfile) print flush } if
  278.  
  279.     % load an ICC profile defined as comments (hex encoded).
  280.     % Ends with %%End at the start of a line. Read the data into
  281.     % a bytestring to allow seeking. This string can be used as a
  282.     % seekable ReusableStreamDecode filter source by the ICC logic.
  283.     %
  284.     % Since .bigstring needs to know the size, we first read an array of
  285.     % strings each 64000 max length.
  286.     %
  287.     % stack: --file-- (%%BeginICCProfile: ...) 
  288.     1 index 0 (%%EndICCProfile) /SubFileDecode filter
  289.     [ { counttomark 1 add index
  290.         64000 string readhexstring
  291.         not { exit } if
  292.       } loop
  293.     ] exch closefile
  294.     0 1 index { length add } forall
  295.     .bigstring
  296.     exch 0 exch {
  297.       % stack: --file-- (%%BeginICCProfile: ...) --bytestring-- cur_index --string--
  298.       2 copy length add     % calculate next string start point
  299.       3 1 roll 3 index 3 1 roll putinterval
  300.     } forall
  301.     pop                % discard length of bytestring
  302.     % make a seekable -file- out of the bytestring
  303.     mark /AsyncRead true .dicttomark /ReusableStreamDecode filter
  304.     % stack: --file-- (%%BeginICCProfile: ...) --icc_subfile-- 
  305.     /DeviceCMYK setcolorspace
  306.     << /DataSource 3 -1 roll
  307.        /N 4        % Try CMYK first
  308.     >> { .seticcspace } stopped {
  309.       /DeviceRGB setcolorspace
  310.       dup /N 3 put  { .seticcspace } stopped {
  311.         /DeviceGray setcolorspace
  312.         dup /N 1 put { .seticcspace } stopped {    % last choice
  313.           QUIET not { (   *** Unable to load ICC profile from PostScript DSC comments ***) = flush } if
  314.           pop
  315.         } if
  316.       } if
  317.     } if
  318.   } {
  319.     pop % Not interested in this DSC comment
  320.   } ifelse
  321. } bind def
  322.  
  323. % Merge ProcessICCcomment with existing handler
  324. /.ProcessICCcomment load /exec load
  325. currentuserparams /ProcessDSCComment get 
  326. dup null eq {pop {pop pop}} if /exec load
  327. 4 array astore cvx readonly
  328. << /ProcessDSCComment 3 -1 roll >> setuserparams
  329.  
  330. end    % systemdict
  331. %END PSICC
  332. .setglobal
  333.