shithub: riscv

ref: 678be8399090b9eb90f983b1b4105495893f57f1
dir: /sys/src/cmd/gs/lib/gs_sepr.ps/

View raw version
%    Copyright (C) 2001, 2002 Aladdin Enterprises.  All rights reserved.
% 
% This software is provided AS-IS with no warranty, either express or
% implied.
% 
% This software is distributed under license and may not be copied,
% modified or distributed except as expressly authorized under the terms
% of the license contained in the file LICENSE in this distribution.
% 
% For more information about licensing, please refer to
% http://www.ghostscript.com/licensing/. For information on
% commercial licensing, go to http://www.artifex.com/licensing/ or
% contact Artifex Software, Inc., 101 Lucas Valley Road #110,
% San Rafael, CA  94903, U.S.A., +1(415)492-9861.

% $Id: gs_sepr.ps,v 1.3 2003/04/26 18:40:16 dan Exp $
% Separation color space method dictionary


% verify that Separation color spaces are supported
/.setseparationspace where
  { pop }
  { currentfile closefile }
ifelse

.currentglobal true .setglobal
.cspace_util begin


%
%   <c1>  <array>   apply_sepr_tint_xform   <c1>' ... <cn>'  <array>
%
% Apply the tint transform for a Separation color space color intensity
% value.
%
/apply_sepr_tint_xform
  {
    exch //bound_0_1 exec
    mark exch 2 index 3 get exec
    counttomark 2 add -2 roll pop
  }
bind def


%
%  <cspace_array>   build_tintxform_function   <cspace_array>  <proc>
%
% Convert a tint transformation into a function, if possible. If this is
% successful, <proc> will be a function procedure (a two-element,
% execute-only, executable array whose first element is the function
% data structure and whose second element is the operator %execfunction.
% If it is not successful, <proc> will be the same as the tint transform
% in <cspace_array>.
%
% Note that, for PDF files, the tint transform will likely already be a
% function-array. In this case, <proc> will be this array, and thus will
% also be identical to the original tint transform.
%
% This procedure is used for both Separation and DeviceN color spaces.
%
/build_tintxform_function
  {
    dup 3 get dup .isencapfunction not
      {
        % Try a FunctionType 4 function first; build the function dictionary
        % in local VM.
        .currentglobal exch //false .setglobal
	% Make the dictionary a couple of elements large in case this fails
	% and we need the dictionary to build a sampled function.
        6 dict begin
        /FunctionType 4 def
        /Function exch def
        /Domain 2 index //.cs_get_range exec def
        /Range 2 index 2 get //.cs_get_range exec def
        currentdict end
        { .buildfunction }
        .internalstopped	% If .buildfunction fails then not type 4 func.
        {			% If not then try building a sampled (type 0) func.
	  % We are using the dictionary which was created before .buildfunction
          dup /Order 3 put
          dup /BitsPerSample 16 put
          { .buildsampledfunction }
          .internalstopped	% If .buildsamplefunction fails then invalid func.
          {			% Failed - Create a dummy tint transform function
	    % We are using the dictionary which was created before .buildfunction
    	    dup /Function {} put    % Replace invalid tint transform with dummy
    	    .buildfunction
    	  }
          if		% Check status from .buildsamplefunction/.internalstopped
	}
	if		% check status from .buildfunction/.intermalstopped
        % restore the VM mode
        exch .setglobal
      }
    if
  }
bind def

%
%  <array1>  <array2>   converttinttransform   <array1>  <array2'>
%
% Convert a Separation/DeviceN color space to use a function as a tint
% transformation, if this is possible. Possible outcomes are:
%
%  1. The tint transform already is a function, or is a procedure that
%     cannot be converted to a function. In either case, <array2> is
%     left unchanged (<array2> == <array2'>).
%
%  2. The tint transform is not already a function but can be converted
%     to a function, and <array1> != <array2>. In this case, <array2>
%     is modified directly.
%
%  3. The tint transform is not already a function but can be converted
%     to a function, and <array1> == <array2>. In this case, <array2>
%     is copied, and the copy is modified (i.e., after the operation
%     <array1> != <array2>
%
% This slightly complex approach avoids creating an extra color space
% array unnecessarily.
%
/converttinttransform
  {
    % convert the tint transform to a fucntion
    //build_tintxform_function exec

    % see if anything needs to be modified
    1 index 3 get 2 copy eq
      { pop pop }
      {
        pop

        % see if the color space must be copied
        3 copy pop eq
          {
            % copy the array into local VM
            .currentglobal //false .setglobal
            3 -1 roll dup length array copy 3 1 roll
            .setglobal
          }
        if
        1 index exch 3 exch put
      }
    ifelse
  }
bind def


colorspacedict
/Separation
  mark
    /cs_potential_indexed_base true
    /cs_potential_pattern_base true
    /cs_potential_alternate false
    /cs_potential_icc_alternate false
    /cs_get_ncomps //ncomps_1
    /cs_get_range //get_range_1
    /cs_get_default_color { pop 1 } bind

    /cs_get_currentgray
      { //apply_sepr_tint_xform exec 2 get //.cs_get_currentgray exec }
    bind
    /cs_get_currentrgb
      { //apply_sepr_tint_xform exec 2 get //.cs_get_currentrgb exec }
    bind
    /cs_get_currentcmyk
      { //apply_sepr_tint_xform exec 2 get //.cs_get_currentcmyk exec }
    bind

    % a lot of validation is done by the cs_validate method
    /cs_validate
      {
        //check_array exec
        dup 1 get type dup /nametype ne exch /stringtype ne and
          //setcspace_typecheck
        if
        dup 2 get //.cs_validate exec //.cs_potential_alternate exec not
          //setcspace_rangecheck
        if
        dup 3 get //check_array exec xcheck not
          //setcspace_typecheck
        if
      }
    bind

    % substitute the base space if appropriate
    /cs_substitute
      {
        dup 2 get //.cs_substitute exec 2 copy eq
          { pop pop dup }
          {
            % retain only the new alternate space
            exch pop

            % build all new structures in local VM
            .currentglobal 3 1 roll //false .setglobal

            % construct a new array and insert the new base color space
            1 index dup length array copy dup 2 4 -1 roll put

            % restore VM mode
            3 -1 roll .setglobal
          }
        ifelse
      }
    bind

    %
    % The Ghostscript interpreter works better when tinttransform procedures
    % are translated into functions. Attempt to do that here.
    %
    /cs_prepare //converttinttransform

    %
    % Install the current color space.
    %
    % The current Ghostscript color space implementation requires that
    % color spaces that provide a base or alternative color space set
    % that base/alternative color space to be the current color space
    % before attempting to set the original color space.
    %
    /cs_install
      {
        % save the current color space
        currentcolorspace

        % set the base color space as the current color space
        1 index 2 get //forcesetcolorspace

        % set the indexed color space; restore the earlier space on error
        mark 2 index
          { .setseparationspace }
        stopped
          { cleartomark setcolorspace stop }
          { pop pop pop }
        ifelse
      }
    bind

    /cs_prepare_color //validate_1

    %
    % If a Separation color space is not supported in native mode by
    % the current process color model, Adobe implementations will always
    % execute the tint transform procedure when setcolor is invoked.
    % Ghostscript may have turned this transform into a sampled function,
    % and even if this is not the case, will have sampled the transform
    % when the color space is first set. Some applications may depend
    % on the Adobe behavior, so we implement it via the
    % cs_complete_setcolor method.
    %
    /cs_complete_setcolor
      {
        .usealternate
          {
            currentcolor exch 3 get exec
            currentcolorspace 2 get //clear_setcolor_operands exec
          }
          { pop }
        ifelse
      }
    bind

  .dicttomark
put

end     % .cspace_util
.setglobal