[gs-cvs] gs/lib

Henry Stiles henrys at casper.ghostscript.com
Thu Aug 22 00:12:30 PDT 2002


Update of /cvs/ghostscript/gs/lib
In directory casper:/tmp/cvs-serv7660/lib

Modified Files:
	gs_dpnxt.ps gs_icc.ps gs_init.ps gs_lev2.ps gs_ll3.ps 
	gs_pdfwr.ps gs_res.ps gs_setpd.ps pdf_draw.ps pdf_main.ps 
Added Files:
	gs_ciecs2.ps gs_ciecs3.ps gs_devcs.ps gs_devn.ps gs_devpxl.ps 
	gs_img.ps gs_indxd.ps gs_patrn.ps gs_sepr.ps 
Log Message:
DeviceN.


--- NEW FILE: gs_ciecs2.ps ---
%    Copyright (C) 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_ciecs2.ps,v 1.1 2002/08/22 07:12:28 henrys Exp $
% Level 2 CIEBased color space method dictionaries.

%
% The validation routines in these method dictionaries perform only
% partial validations; the .setcie* operators will perform the rest.
%

% verify that at least one of the CIEBased color spaces is supported
true
  { /.setcieaspace /.setcieabcspace /.setciedefspace /.setciedefgspace }
  {
    where
      { pop not exit }
    if
  }
forall
  { currentfile closefile }
if


.currentglobal true .setglobal
.cspace_util begin

%
%   <obj>   check_cie_cspace   <obj>
%
% Perform a very basic check that an object is a CIEBased color space
% array.
%
/check_cie_cspace
  {
    //check_array exec
    dup 1 get type /dicttype ne
      //setcspace_typecheck
    if
  }
bind def



/.setcieaspace where
  {
    pop
    colorspacedict
    /CIEBasedA
      mark
        /cs_potential_indexed_base true
        /cs_potential_pattern_base true
        /cs_potential_alternate true
        /cs_potential_icc_alternate true
        /cs_get_ncomps //ncomps_1

        /cs_get_range
          {
            1 get /RangeA .knownget not
              { //dflt_range_1 }
            if
          }
        bind

        /cs_get_default_color { pop 0 } bind
        /cs_get_currentgray //no_currentgray
        /cs_get_currentrgb //no_currentrgb
        /cs_get_currentcmyk //no_currentcmyk
        /cs_validate //check_cie_cspace
        /cs_substitute //dup_1
        /cs_prepare {}

        /cs_install
          {
            NOCIE
              { pop /DeviceGray //.cs_install exec }
              { 1 get .setcieaspace }
            ifelse
          }
        bind

        /cs_prepare_color //validate_1
        /cs_complete_setcolor //pop_1
      .dicttomark
    put
  }
if

/.setcieabcspace where
  {
    pop
    colorspacedict
    /CIEBasedABC
      mark
        /cs_potential_indexed_base true
        /cs_potential_pattern_base true
        /cs_potential_alternate true
        /cs_potential_icc_alternate true
        /cs_get_ncomps //ncomps_3

        /cs_get_range
          {
            1 get /RangeABC .knownget not
              { //dflt_range_3 }
            if
          }
        bind

        /cs_get_default_color { pop 0 0 0 } bind
        /cs_get_currentgray //no_currentgray
        /cs_get_currentrgb //no_currentrgb
        /cs_get_currentcmyk //no_currentcmyk
        /cs_validate //check_cie_cspace
        /cs_substitute //dup_1
        /cs_prepare {}

        /cs_install
          {
            NOCIE
              { pop /DeviceRGB //.cs_install exec }
              { 1 get .setcieabcspace }
            ifelse
          }
        bind

        /cs_prepare_color //validate_3
        /cs_complete_setcolor //pop_1
      .dicttomark
    put
  }
if


end     % .cspace_util
.setglobal

--- NEW FILE: gs_ciecs3.ps ---
%    Copyright (C) 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_ciecs3.ps,v 1.1 2002/08/22 07:12:28 henrys Exp $
% Level 3 CIEBased color space method dictionaries.
% This assumes gs_ciecs2.ps has already been processed.

%
% The validation routines in these method dictionaries perform only
% partial validations; the .setcie* operators will perform the rest.
%


.currentglobal true .setglobal
.cspace_util begin


/.setciedefspace where
  {
    pop
    colorspacedict
    /CIEBasedDEF
      mark
        /cs_potential_indexed_base true
        /cs_potential_pattern_base true
        /cs_potential_alternate true
        /cs_potential_icc_alternate true
        /cs_get_ncomps //ncomps_3

        /cs_get_range
          {
            1 get /RangeDEF .knownget not
              { //dflt_range_3 }
            if
          }
        bind

        /cs_get_default_color { pop 0 0 0 } bind
        /cs_get_currentgray //no_currentgray
        /cs_get_currentrgb //no_currentrgb
        /cs_get_currentcmyk //no_currentcmyk
        /cs_validate //check_cie_cspace
        /cs_substitute //dup_1
        /cs_prepare {}

        /cs_install
          {
            NOCIE
              { pop /DeviceRGB //.cs_install exec }
              { 1 get .setciedefspace }
            ifelse
          }
        bind

        /cs_prepare_color //validate_3
        /cs_complete_setcolor //pop_1
      .dicttomark
    put
  }
if


/.setciedefgspace where
  {
    pop
    colorspacedict
    /CIEBasedDEFG
      mark
        /cs_potential_indexed_base true
        /cs_potential_pattern_base true
        /cs_potential_alternate true
        /cs_potential_icc_alternate true
        /cs_get_ncomps //ncomps_4

        /cs_get_range
          {
            1 get /RangeDEFG .knownget not
              { //dflt_range_4 }
            if
          }
        bind

        /cs_get_default_color { pop 0 0 0 0 } bind
        /cs_get_currentgray //no_currentgray
        /cs_get_currentrgb //no_currentrgb
        /cs_get_currentcmyk //no_currentcmyk
        /cs_validate //check_cie_cspace
        /cs_substitute //dup_1
        /cs_prepare {}

        % the use of the DeviceCMYK color space is questionable:
        % it will likely have the wrong polarity
        /cs_install
          {
            NOCIE
              { pop /DeviceCMYK //.cs_install exec }
              { 1 get .setciedefgspace }
            ifelse
          }
        bind

        /cs_prepare_color //validate_4
        /cs_complete_setcolor //pop_1
      .dicttomark
    put
  }
if


end     % .cspace_util
.setglobal

--- NEW FILE: gs_devcs.ps ---
%    Copyright (C) 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_devcs.ps,v 1.1 2002/08/22 07:12:28 henrys Exp $
% Device-specific color space method dictionaries.

%
% This file implements the method dictionaries for the Device-specific
% color spaces. See gs_cspace.ps for information.
%
% Note that, because these color spaces are parameter-less, no color
% space validation is required: if we can get to the color space methods,
% we know the color space is legitimate.
%
% The colorspace substitution routines for these color spaces
% (cs_substitute) will fail in a Level 1 system, but this is not a
% problem as .getuseciecolor will always return false for such systems.
%
.currentglobal true .setglobal
.cspace_util begin

%
%   <r>  <g>  <b>   rgb_2_gray   <gray>
%
% Convert RGB colors to gray. This includes a special check for
% r == g == b, and avoids roundoff error if this is the case.
%
/rgb_2_gray
  {
    3 copy 1 index eq 3 1 roll eq and
      { pop pop }
      { .11 mul exch .59 mul add exch .3 mul add }
    ifelse
  }
bind def


colorspacedict

dup
/DeviceGray
  mark
    /cs_potential_indexed_base true
    /cs_potential_pattern_base true
    /cs_potential_alternate true
    /cs_potential_icc_alternate true
    /cs_get_ncomps //ncomps_1
    /cs_get_range  //get_range_1
    /cs_get_default_color { pop 0 } bind
    /cs_get_currentgray //pop_1
    /cs_get_currentrgb { pop dup dup } bind
    /cs_get_currentcmyk { pop 1 exch sub 0 0 0 4 -1 roll } bind
    /cs_validate {}

    /cs_substitute
      {
        .getuseciecolor
          { /DefaultGray /ColorSpace findresource }
          { dup }
        ifelse
      }
    bind

    /cs_prepare {}
    /cs_install { pop 0 .setdevcspace } bind
    /cs_prepare_color //validate_1
    /cs_complete_setcolor //pop_1
  .dicttomark
put


/DeviceRGB
  mark
    /cs_potential_indexed_base true
    /cs_potential_pattern_base true
    /cs_potential_alternate true
    /cs_potential_icc_alternate true
    /cs_get_ncomps //ncomps_3
    /cs_get_range  //get_range_3
    /cs_get_default_color { pop 0 0 0 } bind
    /cs_get_currentgray { pop //rgb_2_gray exec } bind
    /cs_get_currentrgb //pop_1

    % to convert to cmyk use blackgeneration and undercolorremoval
    /cs_get_currentcmyk
      {
        pop

        % convert to subtractive (CMY) color space
        3
          { 1 exch sub 3 1 roll }
        repeat

        % find the minimum (initial k component)
        3 copy
        2
          {
            2 copy gt
              { exch }
            if
            pop
          }
        repeat

        % apply undercolorremoval
        dup 5 1 roll currentundercolorremoval exec 4 1 roll
        3
          { 3 index sub //bound_0_1 exec 3 1 roll }
        repeat

        % apply blackgeneration
        5 3 roll pop currentblackgeneration exec //bound_0_1 exec
      }
    bind

    /cs_validate {}

    /cs_substitute
      {
        .getuseciecolor
          { /DefaultRGB /ColorSpace findresource }
          { dup }
        ifelse
      }
    bind

    /cs_prepare {}
    /cs_install { pop 1 .setdevcspace } bind
    /cs_prepare_color //validate_3
    /cs_complete_setcolor //pop_1
  .dicttomark
put

end     % .cspace_util
.setglobal


% Only create the DeviceCMYK color space if setcolorscreen is present
/setcolorscreen where
  { pop }
  { currentfile closefile }
ifelse


.currentglobal true .setglobal
.cspace_util begin

colorspacedict
/DeviceCMYK
  mark
    /cs_potential_indexed_base true
    /cs_potential_pattern_base true
    /cs_potential_alternate true
    /cs_potential_icc_alternate true
    /cs_get_ncomps //ncomps_4
    /cs_get_range  //get_range_4
    /cs_get_default_color { pop 0 0 0 1 } bind

    /cs_get_currentgray
      { pop 4 1 roll //rgb_2_gray exec add 1 exch sub //bound_0_1 exec }
    bind

    /cs_get_currentrgb 
      {
        pop
        4 1 roll 3
          { 3 index add  1 exch sub //bound_0_1 exec 3 1 roll }
        repeat
        4 -1 roll pop
      }
    bind

    /cs_get_currentcmyk //pop_1

    /cs_validate {}

    /cs_substitute
      {
        .getuseciecolor
          { /DefaultCMYK /ColorSpace findresource }
          { dup }
        ifelse
      }
    bind

    /cs_prepare {}
    /cs_install { pop 2 .setdevcspace } bind
    /cs_prepare_color //validate_4
    /cs_complete_setcolor //pop_1
  .dicttomark
put

end     % .cspace_util
.setglobal


--- NEW FILE: gs_devn.ps ---
%    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_devn.ps,v 1.1 2002/08/22 07:12:28 henrys Exp $
% DeviceN color space method dictionary; this depends on gs_sepr.ps


% verify that DeviceN and Separation color spaces are supported
/.setdevicenspace where
  {
    pop
    /.setseparationspace where
      { pop //false }
      { //true }
    ifelse
  }
  { //true }
ifelse
  { currentfile closefile }
if

.currentglobal true .setglobal
.cspace_util begin


%
%   <c1> ... <cm>  <array>   apply_devn_tint_xform   <c1>' ... <cn>'  <array>
%
% Apply the tint transformation for the DeviceN color intensity values.
/apply_devn_tint_xform
  {
    dup 1 get length 1 add exch
    mark 2 index 2 add 2 roll
    index 3 get exec
    counttomark 2 add -2 roll pop
  }
bind def



colorspacedict
/DeviceN
  mark
    /cs_potential_indexed_base true
    /cs_potential_pattern_base true
    /cs_potential_alternate false
    /cs_potential_icc_alternate false
    /cs_get_ncomps { 1 get length } bind
    /cs_get_range { 1 get length [ exch { 0 1 } repeat ] } bind
    /cs_get_default_color { 1 get length { 1 } repeat } bind

    /cs_get_currentgray
      { //apply_devn_tint_xform exec 2 get //.cs_get_currentgray exec }
    bind
    /cs_get_currentrgb
      { //apply_devn_tint_xform exec 2 get //.cs_get_currentrgb exec }
    bind
    /cs_get_currentcmyk
      { //apply_devn_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 //check_array exec
          {
            type dup /nametype ne exch /stringtype ne and
              //setcspace_typecheck
            if
          }
        forall
        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
          { .setdevicenspace }
        stopped
          { cleartomark setcolorspace stop }
          { pop pop pop }
        ifelse
      }
    bind

    /cs_prepare_color { dup 1 get length //check_num_stack exec pop } bind

    %
    % If a DeviceN 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 depend on
    % the Adobe behavior, so we implement it via the cs_complete_setcolor
    % method.
    %
    /cs_complete_setcolor
      {
        .usealternate
          {
            pop currentcolor
            currentcolorspace 3 get exec
            currentcolorspace 2 get
            //clear_setcolor_operands exec
          }
          { pop }
        ifelse
      }
    bind

  .dicttomark
put

end     % .cspace_util
.setglobal

--- NEW FILE: gs_devpxl.ps ---
%    Copyright (C) 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_devpxl.ps,v 1.1 2002/08/22 07:12:28 henrys Exp $
% DevicePixel color space method dictionaries.

%
% This file implements the DevicePixel color space. See gs_cspace.ps
% for information.
%
% The DevicePixel color space includes a single parameter, the bit
% depth of the device color representation. Color is expressed as
% single integers in an opaque, device-specific format.
%

% verify that the DevicePixel color space is supported
/.setdevicepixelspace where
  { pop }
  { currentfile closefile }
ifelse


.currentglobal true .setglobal

.cspace_util begin


colorspacedict
/DevicePixel
  mark
    /cs_potential_indexed_base true
    /cs_potential_pattern_base true
    /cs_potential_alternate true
    /cs_potential_icc_alternate true
    /cs_get_ncomps //ncomps_1
    /cs_get_range { [ exch 1 get 1 exch bitshift 1 sub ] } bind
    /cs_get_default_color { pop 0 } bind    % no good default
    /cs_get_currentgray //no_currentgray
    /cs_get_currentrgb //no_currentrgb
    /cs_get_currentcmyk //no_currentcmyk

    /cs_validate
      {
        //check_array exec
        dup 1 get dup type /integertype ne
          //setcspace_typecheck
        if
        dup 0 lt
          //setcspace_rangecheck
        if
        31 gt   % 31 bits is an implementation limit
          { /setcolorspace cvx /limitcheck signalerror }
        if
      }
    bind

    /cs_substitute //dup_1
    /cs_prepare {}
    /cs_install { .setdevicepixelspace } bind
    /cs_prepare_color //validate_1
    /cs_complete_setcolor //pop_1
  .dicttomark
put

end     % .cspace_util
.setglobal

--- NEW FILE: gs_img.ps ---
% (C) 2002 Artifex, Inc.  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_img.ps,v 1.1 2002/08/22 07:12:28 henrys Exp $
% image, colorimage, and imagemask implementation

%
% The design of the overprint facility in Ghostscript requires that color
% specifications include the color space from which they were expressed,
% even after conversion to the device color model. Directly including this
% information in color specifications is usually not efficient, and is
% difficult to integrate into the existing code structure. The alternative
% approach taken is to extend a state mechanism through the device
% interface, and make the current color space, or more specifically,
% certain information about the current color space, a property of this
% state.
%
% For such a mechanism to work, it is necessary to identify all changes
% to the current color space. This is accomplished in the graphic library
% by funneling all changes to the current color space through the
% gs_setcolorspace procedure. At the PostScript interpreter level, this
% result is achieved by forcing color space changes through the
% setcolorspace operator.
%
% Aside from explicit use of setcolorspace, PostScript provides a few
% implicit methods of changing the current color space. The setgray,
% setrgbcolor, and setcmykcolor operators implicitly set the color space
% while explicitly setting the current color. Similarly, the colorimage
% operator and the traditional form of the image operator (5 operands)
% both temporarily modify the current color space while an image is
% being processed. The current file is concerned with the implementation
% of these two operators. In addition, the traditional form of the
% imagemask operator (5 operands), while it does not affect the current
% color space, is closely related to the image operator and thus is
% implemented in this file as well.
%
% In this implementation, all sampled objects are passed through one of
% the internal operators .image1, .imagemask1, .image1alpha, .image2,
% .image3, or .image4, each of which handles a specific ImageType value.
%
% The procedures in this file are responsible for constructing
% image dictionaries from a set of stack entries. This is, in principle,
% a trivial exercise. In practice it appears to be far more complex,
% primarily due to the need to reconstruct the original state in the
% event of an error. This is a particular problem for operators such as
% image, which include data source objects that may, directly or
% indirectly, be procedures. When these procedures are executed, the
% image operator's operands must have been cleared from the operand
% stack. Hence, the operand stack cannot be used to store state
% information. Similarly, the dictionary stack also cannot be used to
% store state information, as the data source procedures may depend on
% a particular dictionary being on the top of this stack.
%
% Adobe's PostScript implementations determine the extent to which the
% interpreter state is restored in the event of an error by the point at
% which the error is detected. Errors in the image/colorimage/imagemask
% operators that are detected before the data source procedures are
% executed restore the state in effect before the image was processed.
% Those that are detected as part of running the data source procedures
% only attempt to restore the state to that in effect at the start of
% the operator that failed (or at the conclusion of the data source
% procedure, if this procedure failed to push a string).
%
% The implementation given here follows the Adobe convention. The
% mechanism used is as follows:
%
%   1. Check that the stack has a sufficient number of operands, and
%      that enough of them have the proper type to allow construction
%      of the image dictionary. Any errors at this point are handled
%      in the conventional manner.
%
%   2. Build the image dictionary, in the process clearing the image/
%      colorimage/imagemask operands from the stack. No errors can
%      occur during this process.
%
%      (Special precautions could be taken during this step to handle
%      a limitcheck or VMError during the building of the image
%      dictionary, but this essentially never occurs in practice and, if
%      it did, is very unlikely to leave a useable state. Hence, we don't
%      bother with this possibility.)
%
%   3. The .image operator is executed in a stopped context. If it
%      returns abnormally, a check is made to see if the uppermost
%      operand on the stack is a color image dictionary. If so, the
%      original stack is created anew using this dictionary. (Because
%      the image operand works via colorimage, some additional special
%      handling is required in this case.)
%


%
% Create a dictionary of operators for specific image and image mask types.
% Each of these will always handle ImageType 1. Additional types are added
% as they are supported in specific interpreter levels or versions.
%
% These dictionaries are in systemdict for historical reasons.
%
.currentglobal true .setglobal
systemdict begin
/.imagetypes
  5 dict
  dup 1 /.image1 load put
def
/.imagemasktypes
  5 dict
  dup 1 /.imagemask1 load put
def
end
.setglobal

% 
% Build a dictionary of utility procedures and constants for use in
% impelementing the image operators. This dictionary is in global VM but
% is maintained (during initialization) in userdict. It should be pushed
% onto the dictionary stack when constructing image-related procedures
% and pseudo-operators.
%
% This dictionary is removed from userdict when initialization is
% completed.
%
.currentglobal true .setglobal
userdict /img_utils_dict 30 dict put
img_utils_dict begin


%
% Some useful local data structures:
%
%   img_csary maps the number of components in an image to the implied
%       color space.
%
%   img_decary is a prototype Decode array; subintervals of this array
%       may be used for fewer than 4 color components.
%
%   img_params_ary is a list of the parameters to be built in the image
%       dictionary for a colorimage invocation. ImageType is given a
%       fixed value; the other parameters are in stack order (IMG_NComps
%       is the number of components).
%
%   img_mask_params_ary is the equivalent of img_params_ary for imagemask
%       invocations. Polarity is a proxy for Decode, and is replaced
%       by the Decode key in the image dictionary.
%
%   img_mask_check_ary is the set of parameters that must be present in
%       an image dictionary generated by an imagemask invocation. This
%       differs from img_mask_params_ary in that Decode replaces Polarity.
%
/img_csary [ null /DeviceGray null /DeviceRGB /DeviceCMYK ] def
/img_decary [ 0 1  0 1  0 1  0 1 ] def

/img_params_ary
  [
    /ImageType  /IMG_NComps  /MultipleDataSources  /DataSource
    /ImageMatrix  /BitsPerComponent  /Height  /Width   /Decode
  ]
def
/img_check_ary //img_params_ary def
/img_unbuild_ary
 //img_params_ary 1 1 index length 2 sub getinterval
def

/img_mask_params_ary
  [ /ImageType  /DataSource  /ImageMatrix  /Polarity  /Height  /Width ]
def
/img_mask_check_ary
  [
    /ImageType  /BitsPerComponent
    /DataSource  /ImageMatrix  /Decode  /Height  /Width
  ]
def
/img_mask_unbuild_ary
 //img_mask_check_ary 2 1 index length 2 sub getinterval
def


%
%   <?any?>  <array>   img_check_keys   <?any?>  <bool>
%
% Verify that:
%   that there are at least two entries on the stack, and
%   the second (lower) entry is a dictionary, and
%   that dictionary contains all of the keys in the array
%
% If any one of these conditions does not hold, pop the array and push
% false; otherwise pop the array and push true. This utility is used by
% the colorimage and imagematrix procedures to determine if .image left
% the image dictionary on the stack after an abnormal return.
%
/img_check_keys
  {
    count 2 ge
      {
        1 index type /dicttype eq
          {
            true exch
              {
                2 index exch known and
                dup not
                  { exit }
                if
              }
            forall
          }
          { pop //false }
        ifelse
      }
      { pop //false }
    ifelse
  }
.bind def

%
% Procedures to convert a set of stack entries to a dictionary. There is
% a procedure associated with each key, though most keys use the same
% procedure. The dictionary to be built is at the top of the dictionary
% stack. Stack handling for the procedures is:
%
%   <?val0?> ... <?val(n - 1)?>  <key>   proc   -
%
% Parameters are handle in inverse-stack order, so inter-parameter
% dependencies that on the stack can generally be used here.
%
/img_params_dict
  mark
    /ImageType { 1 def } .bind

    /IMG_NComps { exch def } .bind      % number of components
    /MultipleDataSources 1 index
    /Width 1 index
    /Height 1 index
    /ImageMatrix 1 index
    /BitsPerComponent 1 index
    /DataSource 1 index

    % Polarity is a proxy for Decode; it never appears in a dictionary
    /Polarity
      {
        pop
          { { 1 0 } }
          { { 0 1 } }
        ifelse
        /Decode exch cvlit def
      }
    .bind

    % the definition of Decode is based on the number of components
    /Decode { //img_decary 0 IMG_NComps 2 mul getinterval def } .bind
  .dicttomark
def

%
%    <oper_0>  ...  <oper_n>  <array>   img_build_dict   <dict>
%
% Build a dictionary. This will always be done in local VM. The array is
% a list of the keys to be associated with operands on the stack, in
% inverse stack order (topmost element first). The caller should verify
% that the dictionary can be built successfully (except for a possible
% VMerror) before calling this routine.
%
/img_build_dict
  {
    % build the dictionary in local VM; all for 2 extra entries
    .currentglobal false .setglobal
    1 index length 2 add dict
    exch .setglobal
    begin

    % process all keys in the array
      { //img_params_dict 1 index get exec }
    forall

    % if BitsPerComponent is not yet defined, define it to be 1
    currentdict /BitsPerComponent known not
      { /BitsPerComponent 1 def }
    if

    currentdict end
  }
.bind def

%
%   <dict>  <array>   img_unbuild_dict   <oper_0>  ...  <oper_n>
%
% "Unbuild" a dictionary: spread the contents the dictionary back onto the
% stack, in the inverse of the order indicated in the array (inverse is
% used as this order is more convenient for img_build_dict, which is
% expected to be invoked far more frequently).
%
/img_unbuild_dict
  {
    exch begin
    dup length 1 sub -1 0
      { 1 index exch get load exch }
    for
    pop
    end
  }
.bind def


%
%   <width>  <height>  <bits/component>  <matrix>  <dsrc0> ...
%   <multi>  <ncomp>  <has_alpha>
%   img_build_image_dict
%   <dict>  <has_alpha>
%
% Build the dictionary corresponding to a colorimage operand stack. This
% routine will check just enough of the stack to verify that the
% dictionary can be built, and will generate the appropriate error if this
% is not the case.
%
% The <has_alpha> boolean is used to support the Next alphaimage extension.
%
% At the first level, errors in this procedure are reported as colorimage
% errors. The error actually reported will usually be determined by the
% pseudo-operator which invokes this routine.
%
/img_build_image_dict
  {
    % Veify that at least 8 operands are available, and that the top three
    % operands have the expected types
    count 8 lt
      { /.colorimage cvx /stackunderflow signalerror }
    if
    3 copy
    type /booleantype ne exch
    type /integertype ne or exch
    type /booleantype ne or
      { /.colorimage cvx /typecheck signalerror }
    if

    % verify that the number of components is 1, 3, or 4
    1 index 1 lt 2 index 2 eq or 2 index 4 gt or
      { /.colorimage cvx /rangecheck signalerror }
    if

    % Verify that the required number of operands are present if multiple
    % data sources are being used. If this test is successful, convert
    % the data sources to an array (in local VM).
    2 index
      {
        % if an alpha component is present, this adds one more component
        2 copy
          { 1 add }
        if
        dup count 9 sub gt
          { /.colorimage cvx /stackunderflow signalerror }
        if

        % build the DataSource array in local VM
        dup .currentglobal false .setglobal exch array exch .setglobal

        % stack: <w> <h> <bps> <mtx> <d0> ... <multi> <n> <alpha> <n'> <array>
        5 1 roll 4 add 3 roll astore 4 1 roll
      }
    if

    % the image dictionary can be built; do so
    % stack: <w> <h> <bps> <mtx> <dsrc|dsrc_array> <multi> <n> <alpha>
    8 1 roll //img_params_ary //img_build_dict exec exch
  }
.bind def

%
%   <?dict?>
%   img_unbuild_image_dict
%   <width>  <height>  <bits/component>  <matrix>  <dsrc0> ...
%   <multi>  <ncomp>
%
% If the top entry of the stack is a dictionary that has the keys required
% by a colorimage dictionary, unpack that dictionary onto the stack.
% Otherwise just leave things as they are. Note that the <has_alpha>
% parameter is not pushd onto the stack.
%
/img_unbuild_image_dict
  {
    //img_check_ary //img_check_keys exec
      {
        //img_unbuild_ary //img_unbuild_dict exec
        1 index type /booleantype eq
          {
            1 index
              { 3 1 roll aload length 2 add -2 roll }
            if
          }
        if
      }
    if
  }
.bind def


%
%   <width>  <height>  <polarity>  <matrix>  <dsrc>
%   img_unbuild_imagemask_dict
%   <dict>
%
% Build the dictionary corresponding to an imagemask stack. This routine
% will verify that the appropriate number of operands are on the stack,
% and that polarity is a boolean. This is all that is necessary to build
% the dictionary.
%
/img_build_imagemask_dict
  {
    % check for proper number of operands
    count 5 lt
      { /imagemask load /stackunderflow signalerror }
    if

    % verify that polarity is a boolean
    2 index type /booleantype ne
      { /imagemask load /typecheck signalerror }
    if

    % the imagemask dictionary can be built; do so
    //img_mask_params_ary //img_build_dict exec
  }
.bind def

%
%   <?dict?>
%   img_unbuild_imagemask_dict
%   <width>  <height>  <polarity>  <matrix>  <dsrc>
%
% If the top entry of the stack is a dictionary that has the keys rquired
% by an imagemask dictionary, unpack that dictionary onto the stack.
% Otherwise just leave things as they are.
%
/img_unbuild_imagemask_dict
  {
    //img_mask_check_ary //img_check_keys exec
      {
        //img_mask_unbuild_ary //img_unbuild_dict exec
        3 -1 roll
        dup type dup /arraytype eq exch /packedarraytype eq or
        1 index rcheck and
          { 0 get 1 eq }
        if
        3 1 roll
      }
    if
  }
.bind def


%
%   <width>  <height>  <bits/component>  <matrix>  <dsrc_0> ...
%   <multi>  <ncomp>  <has_alpha>
%   .colorimage 
%   -
%
% Convert the image/colorimage operator from their traditional form to
% the dictionary form. The <has_alpha> operand is used ot support the
% Next alphaimage extension.
%
% Error handling for these operators is a bit complex, due to the stack
% handling required of operators that potentially invoke procedures.
% This problem is discussed in the comment above. The facts relevant to
% this particular implementation are:
%
%   1. The .image1 (or .alphaimage) operator is executed in a stopped
%      context, so that we can undo the gsave context in the event of
%      an error.
%
%   2. In the event of an error, the stack is examined to see if the
%      dictionary passed to .image1 (.alphaimage) is still present.
%      If so, this dictionary is "unpacked" onto the stack to re-
%      create the original stack. The <has_alpha> parameter is not
%      pushed onto the stack, as it is not required for any of the
%      pseudo-operators than invoke this procedure.
%
%   3. The use of pseudo-operators in this case may yield incorrect
%      results for late-detected errors, as the stack depth will be
%      restored (even though the stack is not). This is, however, no
%      worse than the prior (level >= 2) code, so it should cause no
%      new problems.
%
/.colorimage
  {
    % build the image dictionary
    //img_build_image_dict exec

    % execute .image1 in a stopped context
      {
        gsave
        //img_csary 2 index /IMG_NComps get get setcolorspace
          { .alphaimage }
          { .image1 }
        ifelse
      }
    stopped
    grestore
      {
        //img_unbuild_image_dict exec
        /.colorimage cvx $error /errorname get
        signalerror
      }
    if
  }
.bind def


% 
%   <width>  <height>  <bits/component>  <matrix>  <dsrc_0> ...
%   <multi>  <ncomp>
%   colorimage 
%   -
%
% Build the colorimage pseudo-operator only if setcolorscreen is visible.
%
systemdict /setcolorscreen .knownget
  {
    type /operatortype eq
      {
        /colorimage 
          {
             //false
               //.colorimage
             stopped
               { /colorimage load  $error /errorname get signalerror }
             if
          }
        .bind systemdict begin odef end
      }
    if
  }
if


%
%   width  height  bits_per_component  matrix  data_src   image   -
%   
%   <dict>   image   -
%
% Some special handling is required for ImageType 2 (Display PostScript
% pixmap images) so as to set the appropriate color space as the current
% color space.
%
/image
  {
    dup type /dicttype eq languagelevel 2 ge and
      { 
        dup /ImageType get dup 2 eq
          {
            % verify the ImageType 2 is supported
            //.imagetypes exch known
              {
                %
                % Set either DevicePixel or DeviceRGB as the current
                % color space. DevicePixel is used if the image data is
                % to be copied directly, with only a geometric
                % transformation (PixelCopy true). The use of DeviceRGB
                % in the alternate case is not, in general, correct, and
                % reflects a current implementation limitation. Ideally,
                % an intermediate color space should be used only if
                % the source and destination color models vary; otherwise
                % the native color space corresponding to the color model
                % should be used.
                %
                % The mechanism to determine depth for the DevicePixel
                % color space is somewhat of a hack.
                %
                gsave
                dup /PixelCopy .knownget dup
                  { pop }
                if
                  {
                      [
                        /DevicePixel
                        currentpagedevice dup /ColorValues .knownget
                          { exch pop }
                          {
                            /GrayValues .knownget not
                              { 2 }     % try a guess
                            if
                          }
                        if
                        ln 2 ln div round cvi
                      ]
                  }
                  { /DeviceRGB }
                ifelse
                setcolorspace
                //.imagetypes 2 get
                stopped
                grestore
                  { /image load $error /errorname get signalerror }
                if
              }
              { /image load /undefined signalerror }
            ifelse
          }
          { //.imagetypes exch get exec }
        ifelse
      }
      { 
        //false 1 //false
          //.colorimage
        stopped
          { /image load $error /errorname get signalerror }
        if
      }
    ifelse
  }
.bind systemdict begin odef end


%
%   width  height  polarity  matrix  datasrc   imagemask   -
%
% See the comment preceding the definition of .colorimage for information
% as to the handling of error conditions.
%
/imagemask
  {
    dup type /dicttype eq languagelevel 2 ge and
      { dup /ImageType get //.imagemasktypes exch get exec }
      {
        //img_build_imagemask_dict exec
          { .imagemask1 }
        stopped
          {
            //img_unbuild_imagemask_dict exec
            /imagemask load $error /errorname get signalerror
          }
        if
      }
    ifelse
  }
.bind systemdict begin odef end

end        % img_utils_dict
.setglobal  % restore VM mode

--- NEW FILE: gs_indxd.ps ---
%    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_indxd.ps,v 1.1 2002/08/22 07:12:28 henrys Exp $
% Indexed color space method dictionary


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

.currentglobal true .setglobal
.cspace_util begin

%
%   <num>  <array>   restrict_index   <int>  <array>
%
% Restrict the operand to setcolor for an Indexed color space to be an
% integer in the applicable range.
%
/restrict_index
  {
    exch round cvi
    dup 0 lt
      { pop 0 }
      {
        1 index 2 get 2 copy gt
          { exch }
        if
        pop
      } 
    ifelse
    exch
  }
bind def

%
%   <num>  <array>   get_indexed_base_color   <c1> ... <cn>  <array>
%
% Get the base color corresponding to an indexed color value.
%
/get_indexed_base_color
  {
    % just in case, set the index into the appropriate range
    //restrict_index exec

    % lookup in the string or use the lookup proc
    mark 1 index 3 get dup type /stringtype eq
      {
        2 index 1 get //.cs_get_ncomps exec dup 6 -1 roll mul exch getinterval
          { 255 div }
        forall
      }
      { 4 -1 roll exch exec }
    ifelse
    counttomark 2 add -2 roll pop
  }
bind def


colorspacedict
/Indexed
  mark
    /cs_potential_indexed_base false
    /cs_potential_pattern_base true
    /cs_potential_alternate false
    /cs_potential_icc_alternate false
    /cs_get_ncomps //ncomps_1
    /cs_get_range { 0 exch 2 get 2 array astore } bind
    /cs_get_default_color { pop 0 } bind

    /cs_get_currentgray
      { //get_indexed_base_color exec 1 get //.cs_get_currentgray exec }
    bind
    /cs_get_currentrgb
      { //get_indexed_base_color exec 1 get //.cs_get_currentrgb exec }
    bind
    /cs_get_currentcmyk
      { //get_indexed_base_color exec 1 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 //.cs_validate exec //.cs_potential_indexed_base exec not
          //setcspace_rangecheck
        if
        dup 2 get dup type /integertype ne
          //setcspace_typecheck
          {
            dup 0 lt exch 4095 gt or
              //setcspace_rangecheck
            if
          }
        ifelse
        dup 3 get dup type /stringtype eq
          {
            length
            1 index dup 2 get 1 add exch 1 get //.cs_get_ncomps exec mul
            lt
              //setcspace_rangecheck
            if
          }
          {
            //check_array exec xcheck not
              //setcspace_typecheck
            if
          }
        ifelse
      }
    bind

    % substitute the base space if appropriate
    /cs_substitute
      {
        dup 1 get //.cs_substitute exec 2 copy eq
          { pop pop dup }
          {
            % retain only the new base 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 1 4 -1 roll put

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

    /cs_prepare {}

    %
    % Install the current color space. Note that the alternative color
    % space will already have been set as 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 1 get forcesetcolorspace

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

    /cs_prepare_color //validate_1

    %
    % Adobe implementations always execute a lookup procedure when setcolor
    % is invoked. Ghostscript samples the lookup procedure when
    % setcolorspace is invoked, and then does not access it again. In the
    % unlikely event that an application depends on the Adobe-specific
    % behavior, it is replicated in this method.
    %
    /cs_complete_setcolor
      {
        3 get dup type /stringtype eq
          { pop }
          {
            currentcolor exch exec
            currentcolorspace 1 get //clear_setcolor_operands exec
          }
        ifelse
      }
    bind

  .dicttomark
put

end     % .cspace_util
.setglobal

--- NEW FILE: gs_patrn.ps ---
%    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_patrn.ps,v 1.1 2002/08/22 07:12:28 henrys Exp $
% Pattern color space method dictionary.


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

.currentglobal true .setglobal
.cspace_util begin

%
%   <name1 | array1>   get_pattern_base_cspace   <null | name2 | array2>
%
% If the Pattern color space has a base color space, push that base
% color space onto the stack. Otherwise, push a null object.
%
/get_pattern_base_cspace
  {
    dup type /nametype eq
      { pop //null }
      {
        dup length 1 gt
          { 1 get }
          { pop //null }
        ifelse
      }
    ifelse
  }
bind def


%
%   <dict>   has_base_color   <bool>
%
% Determine if a Pattern "color" includes a base color. This is the case
% if the pattern dictionary has PatternType 1 and PaintType 2.
%
/has_base_color
  {
    dup //null eq
      { pop //false }
      {
        dup /PatternType get 1 eq
          { /PaintType get 2 eq }
          { pop //false }
        ifelse
      }
    ifelse
  }
bind def

%
%   <c1> ... <cn>  <pattern_dict>  <pattern_cspace>
%   get_pattern_base_color
%   <c1> ... <cn>  <base_cspace>  true
% or
%   <?c1?> ... <?cn?>  <dict>  <pattern>
%   get_pattern_base_color
%   false
%
% If a pattern dictionary has a base color, set up that base color and
% color space, and push true. Otherwise, just push false. It is assumed
% that if the pattern includes a base color, the Pattern color space
% has a base color space.
%
/get_pattern_base_color
  {
    exch //has_base_color exec
      { 1 get //true }
      { pop //false }
    ifelse
  }
bind def


colorspacedict
/Pattern
  mark
    /cs_potential_indexed_base false
    /cs_potential_pattern_base false
    /cs_potential_alternate false
    /cs_potential_icc_alternate false

    %
    % We employ the same convention for describing the number of
    % components in a Pattern color space as is used by the graphic
    % library. For pattern spaces with no underlying color space,
    % the result is -1. If a Pattern space has an underlying color
    % space with n components, the result is -(n + 1).
    %
    /cs_get_ncomps
      {
        //get_pattern_base_cspace exec dup //null eq
          { pop 0 }
          //.cs_get_ncomps
        ifelse
        1 add neg
      }
    bind

    % there is no "range" for a Pattern color space
    /cs_get_range { {} cvlit } bind

    /cs_get_default_color { pop //null } bind

    /cs_get_currentgray
      {
        //get_pattern_base_color exec
          //.cs_get_currentgray
          { 0 }
        ifelse
      }
    bind

    /cs_get_currentrgb
      {
        //get_pattern_base_color exec
          //.cs_get_currentrgb
          { 0 0 0 }
        ifelse
      }
    bind

    /cs_get_currentcmyk
      {
        //get_pattern_base_color exec
          //.cs_get_currentcmyk
          { 0 0 0 1.0 }
        ifelse
      }
    bind

    /cs_validate
      {
        dup //get_pattern_base_cspace exec dup //null eq
          { pop }
          {
            //.cs_validate exec //.cs_potential_pattern_base exec not
              //setcspace_rangecheck
            if
          }
        ifelse
      }
    bind

    % substitute the base space if appropriate
    /cs_substitute
      {
        dup //get_pattern_base_cspace exec dup //null eq
          { pop dup }
          {
            //.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 1 4 -1 roll put

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

    /cs_prepare {}

    %
    % 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.
    %
    % In principle, the only errors that are possible for .setpatternspace
    % (given that setcolorspace itself is legal) are limitcheck and/or
    % VMerror. The Ghostscript implementation allows a few others, so 
    % we go through the full code to restore the current color space in
    % the event of an error.
    %
    /cs_install
      {
        dup //get_pattern_base_cspace exec dup //null eq
          {
            pop
            dup type /nametype eq
              { pop { /Pattern } cvlit }
            if
            .setpatternspace
          }
          {
            % save the current color space
            currentcolorspace

            % set the base color space as the current color space
            exch forcesetcolorspace

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


    %
    % Pattern dictionaries generated by makepattern will include an
    % Implementation entry whose value is an internal data structure.
    % Such structures are given executable type names that match their
    % internal structure names. The names used for pattern
    % implementations are gs_pattern1_instance_t and
    % gs_pattern2_instance_t. It is unfortunate to have to expose such
    % internal names at this level, but not easily avoided.
    %
    /cs_prepare_color
      {
        % verify that the topmost operand is a pattern dictionary
        1 index dup type /dicttype ne
          {
            //null ne
              //setcspace_typecheck
            if
            pop
          }
          {
            dup /Implementation .knownget
              {
                type dup dup
                /gs_pattern1_instance_t ne exch /gs_pattern2_instance_t ne and
                exch xcheck not
                or
                  //setcspace_typecheck
                if
              }
              //setcspace_typecheck
            ifelse

            % check if base color space operands are present
            dup /PatternType get 1 eq
              {
                /PaintType get 2 eq
                  {
                    % verify that a base color space exists
                    //get_pattern_base_cspace exec dup //null eq
                      //setcspace_rangecheck
                    if
                    exch 1 index //.cs_get_ncomps exec
                    exch 1 index 3 add 1 roll
                    //check_num_stack exec
                    //.cs_get_ncomps exec 1 add -1 roll
                  }
                  { pop }
                ifelse
              }
              { pop pop }
            ifelse
          }
        ifelse
      }
    bind

    /cs_complete_setcolor //pop_1

  .dicttomark
put

end     % .cspace_util
.setglobal

--- NEW FILE: gs_sepr.ps ---
%    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.1 2002/08/22 07:12:28 henrys 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
        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

        % see if this qualifies as a function
          { .buildfunction }
        .internalstopped
          {
            % no luck, try a sampled function
            dup /Order 3 put
            dup /BitsPerSample 16 put
              { .buildsampledfunction }
            .internalstopped
          }
          { //false }
        ifelse
          { /Function get }     % still no luck; use the original procedure
        if

        % 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

Index: gs_dpnxt.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/gs_dpnxt.ps,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- gs_dpnxt.ps	21 Feb 2002 21:49:28 -0000	1.4
+++ gs_dpnxt.ps	22 Aug 2002 07:12:28 -0000	1.5
@@ -102,6 +102,28 @@
   } //systemdict /exec get 3 packedarray cvx loop
 } bind odef
 
+%
+%   <w>  <h>  <bpc>  <mtx>  <dsrc0> ... <multi>  <ncomp>   alphaimage   -
+%
+img_utils_dict begin
+/.alphaimage  where
+  {
+    pop
+    currentglobal true setglobal
+    /alphaimage
+      {
+        //true
+          //.colorimage
+        stopped
+          { /alphaimage load $error /errorname get signalerror }
+        if
+      }
+    .bind systemdict begin odef end
+    setglobal
+  }
+if
+end
+
 % Implement sizeimage using lower-level operators.
 
 /sizeimage {		% <x> <y> <width> <height> <matrix> sizeimage

Index: gs_icc.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/gs_icc.ps,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- gs_icc.ps	29 Apr 2002 16:59:01 -0000	1.5
+++ gs_icc.ps	22 Aug 2002 07:12:28 -0000	1.6
@@ -1,4 +1,4 @@
-%    Copyright (C) 2001 Aladdin Enterprises.  All rights reserved.
+%    Copyright (C) 2001, 2002 Aladdin Enterprises.  All rights reserved.
 % 
 % This software is provided AS-IS with no warranty, either express or
 % implied.
@@ -14,101 +14,234 @@
 % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
 
 % $Id$
-% PostScript portion of ICCBased color space support
+% ICCBased color space method dictionaries.
+% This assumes gs_ciecs2.ps has already been processed.
 
-//userdict /.icc_comp_map_dict
-  << 1 /DeviceGray  3 /DeviceRGB  4 /DeviceCMYK >>
-put
+%
+% Note that the substitution procedure in this routine will dynamically
+% check for support of ICCBased color space. If such support is not
+% provided, the alternative color space will be used.
+%
+% The validation routine in dictionary (cs_validate) performs a more
+% extensive validation than is done for other color spaces, because
+% .seticcspace does less checking than most other color space setting
+% operators.
+%
 
-colorspacedict /ICCBased
+
+.currentglobal true .setglobal
+.cspace_util begin
+
+%
+% A dictionary for mapping the number of components of an ICCBased color
+% space to the appropriate alternative color space. This is used only
+% if an alternative color space is not specifically provided.
+%
+/icc_comp_map_dict
+  mark 1 /DeviceGray 3 /DeviceRGB 4 /DeviceCMYK .dicttomark
+def
+
+%
+%   <array1>   get_icc_alternative_space   <name | array2>
+%
+% Get the alternative color space for an ICCBased color space.
+%
+/get_icc_alternative_space
   {
-    % Verify that the source object is an array, that it is at least of length
-    % two, and that the second entry is a readable dictionary. If we got this
-    % far, we know the top-level object exists and is readable, but it might
-    % be a dictionary.
-    dup type dup /arraytype ne exch /packedarraytype ne and
-      { /setcolorspace /typecheck signalerror }
-    if
-    dup length 2 lt
-      { /setcolorspace /rangecheck signalerror }
-    if
-    dup 1 get type /dicttype ne
-      { /setcolorspace /typecheck signalerror }
-    if
-    dup 1 get rcheck not
-      { /setcolorspace /invalidaccess signalerror }
-    if
+    1 get dup /Alternate .knownget
+      { exch pop }
+      { /N get //icc_comp_map_dict exch get }
+    ifelse
+  }
+bind def
 
-    % Verify that the dictionary defines the key N (number of components) and
-    %  entry is provided and has a legitimate value
-    dup 1 get /N .knownget
+
+colorspacedict
+/ICCBased
+  mark
+    /cs_potential_indexed_base true
+    /cs_potential_pattern_base true
+    /cs_potential_alternate true
+    /cs_potential_icc_alternate false
+    /cs_get_ncomps { 1 get /N get } bind
+
+    /cs_get_range
       {
-        //.icc_comp_map_dict exch known not
-          { 
-            % generate the appropriate error
-            /setcolorspace 
-            1 index 1 get /N type type /integertype ne
-              { /typecheck }
-              { /rangecheck }
-            ifelse
-            signalerror
+        1 get dup /Range .knownget
+          { exch pop }
+          { /N get 2 mul //dflt_range_4 exch 0 exch getinterval }
+        ifelse
+      }
+    bind
+
+    /cs_get_default_color { 1 get /N get { 0 } repeat } bind
+
+    %
+    % For generating a gray, RGB, or CMYK equivalent color, we will
+    % assume that the alternative color space provides reasonable
+    % mapping.
+    /cs_get_currentgray
+      { //get_icc_alternative_space exec //.cs_get_currentgray exec }
+    bind
+    /cs_get_currentrgb
+      { //get_icc_alternative_space exec //.cs_get_currentrgb exec }
+    bind
+    /cs_get_currentcmyk
+      { //get_icc_alternative_space exec //.cs_get_currentcmyk exec }
+    bind
+
+    % a lot of validation is done by the cs_validate method
+    /cs_validate
+      {
+        //check_cie_cspace exec
+        dup 1 get
+        dup /N get
+        dup type /integertype ne
+          //setcspace_typecheck
+        if
+        //icc_comp_map_dict exch known not
+          //setcspace_rangecheck
+        if
+        dup /DataSource get
+        dup type dup /stringtype ne exch /filetype ne and
+          //setcspace_typecheck
+        if
+        rcheck not
+          //setcspace_invalidaccess
+        if
+        dup /Range .knownget
+          {
+            //check_array exec
+              {
+                type dup /integertype ne exch /realtype ne and
+                  //setcspace_typecheck
+                if
+              }
+            forall
+          }
+        if
+        /Alternate .knownget
+          {
+            //.cs_validate exec
+            //.cs_potential_icc_alternate exec not
+              //setcspace_rangecheck
+            if
           }
         if
       }
-      { /setcolorspace /undefined signalerror }
-    ifelse
+    bind
 
-    % Verify DataSrouce and, if it is a string, convert it to a file
-    dup 1 get /DataSource .knownget
+    % substitute the Alternate space, if appropriate
+    /cs_substitute
       {
-        dup rcheck not
-          { pop /setcolorspace /invalidaccess signalerror }
-        if
-        type dup /stringtype eq
+        dup //get_icc_alternative_space exec
+        //.cs_substitute exec 2 copy eq
+          { pop pop dup }
           {
-            pop
-            2 array copy
-            dup 1
-             2 copy get dup length dict copy
-             dup /DataSource
-              2 copy get /ReusableStreamDecode filter
-             put
+            % retain just the new Alternate space
+            exch pop
+
+            % build all new structures in local VM
+            .currentglobal 3 1 roll //false .setglobal
+
+            % copy the original ICCBased color space array
+            1 index dup length array copy
+
+            % copy the ICCBased dictionary
+            dup 1 2 copy get dup length dict copy
+
+            % insert the new alterante color space
+            dup /Alternate 7 -1 roll put
+
+            % insert the new dictionary into the arra
             put
+
+            % restore the VM mode
+            3 -1 roll .setglobal
           }
+        ifelse
+      }
+    bind
+
+    %
+    % The current implementation of ICCBased color spaces requires the
+    % DataSource to be a file.
+    %
+    /cs_prepare
+      {
+        % make DataSource a file
+        dup 1 get /DataSource get type /stringtype eq
           {
-            /filetype ne
-              { /setcolorspace /typecheck signalerror }
+            % build all new structures in local VM
+            .currentglobal exch //false .setglobal
+
+            % check if we need to copy the color space and dictionary
+            2 copy eq
+              {
+                dup length array copy
+                dup 1 2 copy get dup length dict copy put
+              }
             if
+
+            % fetch DataSource, setting up stack for multiple puts
+            dup 1 2 copy get dup /DataSource 2 copy get
+
+            % convert the string into a file
+            /ReusableStreamDecode filter
+
+            % put the file into the dictioary, the dictionary into the array
+            put put
+
+            % restore the VM mode
+            exch .setglobal
           }
-        ifelse
+        if
       }
-      { /setcolorspace /undefined signalerror }
-    ifelse
+    bind
 
-    % set the alternate color space to be the current color space
-    dup 1 get /Alternate .knownget not
-      { dup 1 get /N get //.icc_comp_map_dict exch get }
-    if
-    .dosetcolorspace
+    %
+    % 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. This can cause
+    % difficulty if an ICCBased color space is being used as a substitute
+    % color space for a device-specific color space, and uses that same
+    % device-specific color space as an alternative space. For this
+    % reason, a special _setcolorspace_nosub operator is provided.
+    %
+    /cs_install
+      {
+        % set the alternative color space to be the current color space
+        dup //get_icc_alternative_space exec //_setcolorspace_nosub exec
 
-    % if CIE spaces are not use, just take the alternate space
-    NOCIE
-      { pop //null }
-      { % check for native support
+        % check for native support
         /.seticcspace where
-          { pop dup 1 get
-	    % Acrobat Reader silently ignores errors with ICC profiles
-	    % and uses the alternate color space -- do the same.
-	    mark exch { .seticcspace } stopped
-	      { cleartomark pop //null }
-	      { pop }
-	    ifelse
-	  }
-          { pop //null }
+          { pop //false }
+          { //true }
+        ifelse
+        NOCIE or
+          //pop_1       % do nothing
+          {
+            % Acrobat Reader silently ignores errors with ICC profiles
+            % and uses the alternate color space -- do the same.
+            mark exch 1 get
+              { .seticcspace }
+            .internalstopped
+            cleartomark
+          }
         ifelse
       }
-    ifelse
-  }
-bind put
+    bind
 
-//userdict /.icc_comp_map_dict undef
+    % for now, the alternative spaces for an ICCBased color space do 
+    % not require special preparation
+    /cs_prepare_color { dup 1 get /N get //check_num_stack exec pop } bind
+    /cs_complete_setcolor //pop_1
+
+  .dicttomark
+put
+
+end     % .cspace_util
+.setglobal

Index: gs_init.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/gs_init.ps,v
retrieving revision 1.59
retrieving revision 1.60
diff -u -d -r1.59 -r1.60
--- gs_init.ps	7 Aug 2002 06:58:53 -0000	1.59
+++ gs_init.ps	22 Aug 2002 07:12:28 -0000	1.60
@@ -377,7 +377,9 @@
 	      { currentrgbcolor { setrgbcolor } }
 	     ifelse
 	   }
-	  ifelse 1 setgray .fillpage exec
+	  ifelse
+          currentoverprint false setoverprint 1 setgray .fillpage setoverprint
+          exec
 	} odef
 % To satisfy the Genoa FTS, executive must be a procedure, not an operator.
 /executive
@@ -473,8 +475,12 @@
 	0 .endpage .doneshowpage {
 	  .currentnumcopies true .outputpage
 	  (>>showpage, press <return> to continue<<\n) .confirm
-	  erasepage
-	} if initgraphics .beginpage
+	  initgraphics
+          currentoverprint false setoverprint 1 setcolor
+          .fillpage
+          setoverprint 0 setcolor
+	}
+	{ initgraphics } ifelse .beginpage
 } odef
 % Code output by Adobe Illustrator relies on the fact that
 % `stack' is a procedure, not an operator!!!
@@ -1209,11 +1215,22 @@
 	% If so, don't bother with color screening or gamma correction.
 	% Also don't do gamma correction on very high-resolution devices.
 	% (This should depend on dot gain, not resolution, but we don't
-	% currently have a way to determine this.)
+	% currently have a way to determine this.) Ignore missing components
+        % (*Values = 1).
    currentdevice mark
      /RedValues 0 /GreenValues 0 /BlueValues 0 /GrayValues 0
    .dicttomark .getdeviceparams
-   counttomark 2 idiv 1 sub { exch pop .min } repeat
+   counttomark 2 idiv 1 sub
+    { exch pop dup 1 le
+       { pop }
+       { exch dup 1 le
+          { pop }
+          { .min }
+        ifelse
+       }
+      ifelse
+    }
+   repeat
    exch pop exch pop 32 lt 4 index 800 lt and 5 1 roll
 	% Stack: doscreen dpi freq angle proc
 	% Ghostscript currently doesn't use correct, per-plane halftones
@@ -1328,6 +1345,23 @@
   .useloresscreen { .setloresscreen } { .sethiresscreen } ifelse
   .setdefaultbgucr
 } bind def
+
+% Load basic color support
+%% Replace 1 (gs_cspace.ps)
+(gs_cspace.ps) runlibfile
+
+(END BASIC COLOR) VMDEBUG
+
+%% Replace 1 (gs_devcs.ps)
+(gs_devcs.ps) runlibfile
+
+(END LEVEL 1 COLOR) VMDEBUG
+
+% Load image support
+%% Replace 1 (gs_img.ps)
+(gs_img.ps) runlibfile
+
+(END IMAGE) VMDEBUG
 
 % Load the initialization files for optional features.
 %% Replace 4 INITFILES

Index: gs_lev2.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/gs_lev2.ps,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- gs_lev2.ps	31 Jul 2002 04:43:44 -0000	1.20
+++ gs_lev2.ps	22 Aug 2002 07:12:28 -0000	1.21
@@ -613,181 +613,30 @@
 
 % ------ Color spaces ------ %
 
-% This set of dictionaries and the following procedure (.getcsrange)
-% will determine an array of the allowed ranges for a given color space
-% specifier.  This color space is the alternate color space specified in
-% the descriptor array which is used to specify a Separation or a DeviceN
-% color space.
-
-% This dictionary used if the color space specified by a name.
-/namedcsranges 10 dict dup begin
-  /DeviceGray { [ 0 1 ] } bind def
-  /DeviceRGB { [ 0 1 0 1 0 1 ] } bind def
-  /DeviceCMYK { [ 0 1 0 1 0 1 0 1 ] } bind def
-  /CIEBasedA /DeviceGray load def
-  /CIEBasedABC /DeviceRGB load def
-  /CIEBasedDEF /DeviceRGB load def
-  /CIEBasedDEFG /DeviceCMYK load def
-  /ICCBased { /typecheck signalerror } bind def		% Must have description for ICCBased
-end def
-
-% This dictionary used if the color space specified by an array.
-/arraycsranges 10 dict dup begin
-  /DeviceGray { [ 0 1 ] } bind def
-  /DeviceRGB { [ 0 1 0 1 0 1 ] } bind def
-  /DeviceCMYK { [ 0 1 0 1 0 1 0 1 ] } bind def
-  /CIEBasedA { dup 1 get /RangeA .knownget not { [ 0 1 ] } if } bind def
-  /CIEBasedABC { dup 1 get /RangeABC .knownget not { [ 0 1 0 1 0 1 ] } if } bind def
-  /CIEBasedDEF { dup 1 get /RangeDEF .knownget not { [ 0 1 0 1 0 1 ] } if } bind def
-  /CIEBasedDEFG { dup 1 get /RangeDEFG .knownget not { [ 0 1 0 1 0 1 0 1 ] } if } bind def
-  /ICCBased {
-    dup 1 get dup /Range .knownget {
-      exch pop
-    }{
-      /N get [ exch {0 1} repeat ] readonly
-    } ifelse
-  } bind def
-end def
-
-% See comments before /namedcsranges
-/.getcsrange {				% <alt color space spec> .getcsrange <range array>
-  dup type /arraytype eq
-  {
-    dup 0 get arraycsranges exch get exec 
-  }
-  {
-    dup namedcsranges exch get exec
-  } ifelse
-  exch pop
-} bind def
-
-% Attempt to convert a tint transformation procedure to a function that
-% we can execute without the actually executing the interpreter in the
-% graphics library.
-% The value <m> is the number of function inputs
-% The current color space will be the alternate color space for the function.
-% Thus the current color space defines the number of function output values.
-%
-% There are three situations.  The PDF interpreter already produces encapsulated
-% functions that we can use as is.  Otherwise we try to convert the given
-% procedure into a type 4 function.  This works for most practical situations.
-% If all else fails then build a color cube (type 0 sampled) function..
+% Move setcolorsapce, setcolor, and colorspacedict to level2dict
+level2dict /setcolorspace .cspace_util 1 index get put
+level2dict /setcolor .cspace_util 1 index get put
+level2dict /colorspacedict .cspace_util 1 index get put
 
-/.converttinttransform {	% [.. .. .. proc ] <m>
-				%   .converttinttransform [.. .. .. proc']
-  % PDF already has the tint transforms functions as encapsulated functions.
-  % If so then we do not need to do anything except clean up and exit.
-  1 index 3 get .isencapfunction 	% Check for encapsulated function
-  {				% Yes - clean up and exit
-    pop					% Clear <m> off stack
-  }
-  {				% No - try to build a type 4 function
-    .currentglobal			% Save current global memory state
-    2 index gcheck .setglobal		% Set gobal mode to match array's mode
-    6 dict				% Build a dictionary for our type 4
-    					% function.  We allow extra space since
-					% we may use dict for building cube
-    dup /FunctionType 4 put		% Set FunctionType
-    dup /Function 5 index 3 get put	% Set function expression
-    dup /Domain
-    [ 5 index {0 1} repeat ] put	% set Domain values
-    dup /Range
-    7 index 2 get .getcsrange put	% Set Range values
-					% Stack: orig m global func_dict
-    { .buildfunction } .internalstopped	% Try to build a type 4 function
-    dup
-    {				% type 4 function failed - build a color cube
-      pop				% Remove dupicate stopped status
-      dup /Order 3 put			% Set Order
-      dup /BitsPerSample 16 put		% Set sample size
-					% Stack: orig m global func_dict
-      { .buildsampledfunction } .internalstopped 
-    } if
-    {				% Color cube build failed - clean up and exit
-      pop .setglobal pop 		% Remove unused parameters
-    }
-    {				% Function build succeeded - install function
-      3 -1 roll pop			% Stack: orig global func
-      2 index 4 array copy dup 3 4 -1 roll put
-      exch .setglobal exch pop
-    } ifelse
-  } ifelse
-} bind def
+% Add the level 2 color spaces
+% DevicePixel is actually a LanguageLevel 3 feature; it is here for
+% historical reasons.
+%% Replace 1 (gs_devpxl.ps) 
+(gs_devpxl.ps) runlibfile
 
-% Define the setcolor procedure:
-%   <param1> ... <paramN> setcolor -
-% This definition is overridden in PS level 3.
-/setcolor {
-  .setcolor
-} odef
+%% Replace 1 (gs_ciecs2.ps)
+(gs_ciecs2.ps) runlibfile
 
-% Define the setcolorspace procedures:
-%	<colorspace> proc <colorspace'|null>
-% We have to define the dictionary first, so it can be bound into the
-% implementation procedure, but we can't populate it until the procedure
-% has been defined, so that the procedure can get bound into recursive calls.
-/colorspacedict 20 dict def
+%% Replace 1 (gs_indxd.ps)
+(gs_indxd.ps) runlibfile
 
-/.devcs [
-  /DeviceGray /DeviceRGB /DeviceCMYK /DevicePixel
-] readonly def
-/currentcolorspace {		% - currentcolorspace <array>
-  .currentcolorspace dup type /integertype eq {
-    //.devcs exch 1 getinterval
-  } if
-} odef
-currentdict /.devcs .undef
+%% Replace 1 (gs_sepr.ps)
+(gs_sepr.ps) runlibfile
 
-% Actual setcolorspace procedure - used internally
-/.dosetcolorspace {		% <name|array> .dosetcolorspace -
-  dup dup dup type /nametype ne { 0 get } if
-  //colorspacedict exch get exec
-  dup null eq { pop } { .setcolorspace } ifelse pop
-} bind def
+%% Replace 1 (gs_patrn.ps)
+(gs_patrn.ps) runlibfile
 
-% external setcolorspace optimized to avoid duplicate actions
-/setcolorspace {
-  currentcolorspace 1 index ne { .dosetcolorspace } { pop } ifelse
-} odef
 
-colorspacedict
-  dup /DeviceGray { pop 0 setgray null } bind put
-  dup /DeviceRGB { pop 0 0 0 setrgbcolor null } bind put
-  /setcmykcolor where
-   { pop dup /DeviceCMYK { pop 0 0 0 1 setcmykcolor null } bind put
-   } if
-  /.setcieaspace where
-   { pop dup /CIEBasedA { NOCIE { pop 0 setgray null } { dup 1 get .setcieaspace } ifelse } bind put
-   } if
-  /.setcieabcspace where
-   { pop dup /CIEBasedABC { NOCIE { pop 0 0 0 setrgbcolor null } { dup 1 get .setcieabcspace } ifelse } bind put
-   } if
-  /.setciedefspace where
-   { pop dup /CIEBasedDEF { NOCIE { pop 0 0 0 setrgbcolor null } { dup 1 get .setciedefspace } ifelse } bind put
-   } if
-  /.setciedefgspace where
-   { pop dup /CIEBasedDEFG { NOCIE { pop 0 0 0 1 setcmykcolor null } { dup 1 get .setciedefgspace } ifelse } bind put
-   } if
-  /.setseparationspace where
-   { pop dup /Separation { dup 2 get .dosetcolorspace dup 1 .converttinttransform .setseparationspace } bind put
-   } if
-  /.setindexedspace where
-   { pop dup /Indexed { dup 1 get .dosetcolorspace dup .setindexedspace } bind put
-   } if
-  /.nullpatternspace [/Pattern] readonly def
-  /.setpatternspace where
-   { pop dup /Pattern
-      { dup type /nametype eq { pop //.nullpatternspace } if
-	dup length 1 gt { dup 1 get .dosetcolorspace } if
-        dup .setpatternspace
-      } bind put
-   } if
-	% If DeviceN space is included, gs_ll3.ps registers it.
-  /.setdevicepixelspace where
-   { pop dup /DevicePixel { dup .setdevicepixelspace } bind put
-   } if
-  currentdict /.nullpatternspace .undef
-pop
 
 % ------ CIE color rendering ------ %
 
@@ -939,40 +788,9 @@
 
 /setpattern {			% [<comp1> ...] <pattern> setpattern -
   currentcolorspace 0 get /Pattern ne {
-    [ /Pattern currentcolorspace ] .dosetcolorspace
+    [ /Pattern currentcolorspace ] setcolorspace
   } if setcolor
 } odef
 
-% Extend image and imagemask to accept dictionaries.
-% We must create .imagetypes and .imagemasktypes outside level2dict,
-% and leave some extra space because we're still in Level 1 mode.
-systemdict begin
-/.imagetypes 5 dict
-  dup 1 /.image1 load put
-def
-/.imagemasktypes 5 dict
-  dup 1 /.imagemask1 load put
-def
-end
-
-/.image /image load def
-/image {
-  dup type /dicttype eq {
-    dup /ImageType get //.imagetypes exch get exec
-  } {
-    //.image
-  } ifelse
-} odef
-currentdict /.image undef
-
-/.imagemask /imagemask load def
-/imagemask {
-  dup type /dicttype eq {
-    dup /ImageType get //.imagemasktypes exch get exec
-  } {
-    //.imagemask
-  } ifelse
-} odef
-currentdict /.imagemask undef
 
 end				% level2dict

Index: gs_ll3.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/gs_ll3.ps,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- gs_ll3.ps	31 Jul 2002 04:43:45 -0000	1.15
+++ gs_ll3.ps	22 Aug 2002 07:12:28 -0000	1.16
@@ -161,29 +161,6 @@
   16 /.sethalftone16 load def
 end
 
-% Redefine the setcolor procedure:
-%   <param1> ... <paramN> setcolor -
-%
-% The setcolor operator is being redefined due to an ugly violation
-% of the PLRM by Adobe Photoshop 5+.  Duotone code generated by Adobe
-% Photoshop 5+ expects that setcolor:
-% (1) calls tint transform function (ttf). PLRM doesn't require this
-% (2) permits ttf to change op stack as side effect. PLRM forbids this
-% (3) assumes op stack for ttf is the same when the call to setcolor was made.
-% Thus we may need to call the ttf.  The Adobe Photoshop Duotone code
-% also has the nasty side effect of creating a ttf which pops more
-% operands than required, thus destroying the stack, if its expectations
-% are not met.
-/setcolor {
-  .setcolor			% Do normal setcolor operation
-  .checkdevicenaltused {	% Check if DeviceN and using alt color space
-    currentcolor		% Get current color (just set)
-    currentcolorspace 3 get	% get tint transform function
-    exec			% Execute tint transform function
-    .discardaltcolor		% delete the resulting color
-  } if
-} odef
-
 % Redefine the halftone-setting operators to honor HalftoneMode.
 /setcolorscreen {
   /HalftoneMode getuserparam 0 eq {
@@ -278,7 +255,7 @@
 	% The .buildshading operators use the current color space
 	% for ColorSpace.
   dup /ShadingType get //.shadingtypes exch get
-  1 index /ColorSpace get .dosetcolorspace exec
+  1 index /ColorSpace get setcolorspace exec
 } bind def
 systemdict /.reuseparamdict undef
 
@@ -291,10 +268,10 @@
   counttomark 4 add -3 roll mark 4 1 roll
 	% Stack: -mark- ..color.. cspace -mark- template matrix shadingdict
   { .buildshading } stopped {
-    cleartomark .dosetcolorspace setcolor pop stop
+    cleartomark setcolorspace setcolor pop stop
   } if
   .buildshadingpattern
-  3 -1 roll pop counttomark 1 add 2 roll .dosetcolorspace setcolor pop
+  3 -1 roll pop counttomark 1 add 2 roll setcolorspace setcolor pop
 } bind def
 
 .patterntypes
@@ -312,94 +289,6 @@
 
 % ------ UseCIEColor ------ %
 
-% The library maintains and detects the UseCIEColor device parameter,
-% but it doesn't have access to the resource dictionaries.  We also
-% want color space substitution to work in systems without a PostScript
-% interpreter.  Therefore, we eagerly inform the library of changes in
-% the (effective) ColorSpace category that might affect the operation of
-% UseCIEColor.  We must notice the following events:
-%	1) defineresource and undefineresource of the Default ColorSpaces.
-%	2) restore.
-%	3) Changes in current VM, which cause a different set of resources
-%	to become visible.
-% #1 is rare.  #2 is handled in C code.  If checking the ColorSpace
-% category on #3 turns out to be expensive, we can cache more information
-% about whether these operations actually affect UseCIEColor.
-
-% This operator implements color space substitution in the library:
-%	<index> <bool> .setsubstitutecolorspace -
-% <bool> = true means substitute the current color space for the one given
-% by <index>; <bool> = false means stop substituting.  Substitution is not
-% affected by grestore/setgstate, but it is affected by restore.
-
-% ColorSpace defineresource and undefineresource for the Default keys
-% call .definedefaultcs and .undefinedefaultcs.  See gs_res.ps.
-
-/.useciecolorkeydict mark
-  /UseCIEColor null
-.dicttomark readonly def
-/.definedefaultcs {	% <index> <value> .definedefaultcs -
-  currentcolorspace
-	% Temporarily disable color substitution, in case the substitute
-	% color space is or mentions a color space that is currently
-	%  being substituted.
-  currentdevice //.useciecolorkeydict .getdeviceparams exch pop exch pop
-  mark 5 -2 roll
-	% Stack: cspace UseCIEColor mark index value
-   { .dosetcolorspace true .setsubstitutecolorspace }
-  stopped counttomark 1 add 1 roll cleartomark
-	% Stack: cspace UseCIEColor stopped?
-  3 1 roll
-  currentdevice null true mark /UseCIEColor 6 -1 roll .putdeviceparams pop pop
-  .dosetcolorspace { stop } if
-} bind def
-currentdict /.useciecolorkeydict undef
-
-/.undefinedefaultcs {	% <index> .undefinedefaultcs -
-  false .setsubstitutecolorspace
-} bind def
-
-/.setdefaultcs {	% <index> <value|null> .setdefaultcs -
-  dup null eq { pop .undefinedefaultcs } { .definedefaultcs } ifelse
-} bind def
-
-/.getdefaultcs {	% <key> .getdefaultcs <value|null>
-  .GetInstance { 0 get } { null } ifelse
-} bind def
-
-/.setglobal {		% <bool> .setglobal -
-  dup .currentglobal ne {
-	% If there are no local definitions of the Default keys,
-	% changing VM can't affect the definition of any resource.
-    /.localcsdefaults .uservar {
-	% We only want to change substitutions for color spaces
-	% whose definitions are actually changing.
-      /ColorSpace /Category findresource begin
-      /DefaultGray .getdefaultcs
-      /DefaultRGB .getdefaultcs
-      /DefaultCMYK .getdefaultcs
-      end
-      3 index .setglobal
-      /ColorSpace /Category findresource begin
-      /DefaultGray .getdefaultcs
-      /DefaultRGB .getdefaultcs
-      /DefaultCMYK .getdefaultcs
-      end
-			% Stack: bool oldgray oldrgb oldcmyk
-			%   newgray newrgb newcmyk
-      dup 4 index ne { 2 exch .setdefaultcs } { pop } ifelse
-      dup 4 index ne { 1 exch .setdefaultcs } { pop } ifelse
-      dup 4 index ne { 0 exch .setdefaultcs } { pop } ifelse
-      pop pop pop pop
-    } {
-      .setglobal
-    } ifelse
-  } {
-    .setglobal
-  } ifelse
-} .bind odef		% bind in .setglobal
-
-
 % Define default device color spaces to allow substitution
 % on UseCIEColor == true :
 
@@ -454,18 +343,17 @@
 
 % ------ DeviceN color space ------ %
 
-% This isn't quite right, because the ColorSpaceFamily resource will exist
-% even with languagelevel < 3, but it's close enough.
+%% Replace 1 (gs_ciecs3.ps)
+(gs_ciecs3.ps) runlibfile
 
-/.setdevicenspace where {
-  pop colorspacedict /DeviceN {
-    dup 2 get .dosetcolorspace dup
-    dup 1 get length		% # of components
-    .converttinttransform .setdevicenspace
-  } bind put
-} if
+%% Replace 1 (gs_devn.ps)
+(gs_devn.ps) runlibfile
 
 % ------ Miscellaneous ------ %
+
+% Make the .setoverprintmode and .currentoverprintmode operators visible (3015)
+systemdict /setoverprintmode /.setoverprintmode load put
+systemdict /currentoverprintmode /.currentoverprintmode load put
 
 % Define additional user and system parameters.
 /HalftoneMode 0 .definepsuserparam

Index: gs_pdfwr.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/gs_pdfwr.ps,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- gs_pdfwr.ps	17 Aug 2002 09:16:34 -0000	1.17
+++ gs_pdfwr.ps	22 Aug 2002 07:12:28 -0000	1.18
@@ -1,4 +1,4 @@
-%    Copyright (C) 1996, 2000 Aladdin Enterprises.  All rights reserved.
+ %    Copyright (C) 1996, 2000 Aladdin Enterprises.  All rights reserved.
 % 
 % This software is provided AS-IS with no warranty, either express or
 % implied.

Index: gs_res.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/gs_res.ps,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -d -r1.24 -r1.25
--- gs_res.ps	12 Jul 2002 06:46:33 -0000	1.24
+++ gs_res.ps	22 Aug 2002 07:12:28 -0000	1.25
@@ -866,7 +866,6 @@
     /ColorRendering exch /ProcSet defineresource pop
     systemdict /ColorRendering undef
     /Default currentcolorrendering /ColorRendering defineresource pop
-    /DefaultColorRendering currentcolorrendering /ColorRendering defineresource pop
   } if
 	% ColorSpace resources
   systemdict /CIEsRGB .knownget {

Index: gs_setpd.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/gs_setpd.ps,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- gs_setpd.ps	10 Jul 2002 23:05:59 -0000	1.12
+++ gs_setpd.ps	22 Aug 2002 07:12:28 -0000	1.13
@@ -199,6 +199,7 @@
   /EndPage {{.callendpage}} bind
   /Policies .defaultpolicies
   /ImagingBBox null		% default value
+  /UseCIEColor /.getuseciecolor load
 .dicttomark readonly def
 
 % Define currentpagedevice so it creates the dictionary on demand if needed,
@@ -241,7 +242,11 @@
 	     { 2 index wcheck not
 		{	% This is the first entry being updated.
 			% Copy the dictionary to make it writable.
-		  3 -1 roll dup length dict .copydict
+		  3 -1 roll
+		  currentglobal 1 index dup gcheck currentglobal and setglobal
+		  length dict
+		  exch setglobal
+		  .copydict
 		  3 1 roll
 		}
 	       if
@@ -703,6 +708,32 @@
    .setdevice pop
    .setpagedevice
 
+		% Implement UseCIEColor directly if this is a LL3 system.
+		% The color substitution feature is now implemented in
+		% the interpreter, and this is used as an optimization.
+		%
+		% NB: This shoud be the only use of the .setuseciecolor
+		%     operator anywhere.
+		%
+		% If UseCIEColor is transitioned to false, set some
+		% color space other than /DeviceGray, to insure that
+		% initgraphics will actually perform a setcolorspace
+		% operation (there is an optimization in setcolorspace
+		% that does nothing if the operand and current color
+		% spaces are the same, and UseCIEColor is false).
+
+    /.setuseciecolor where
+      {
+        pop 1 index /UseCIEColor .knownget
+          {
+            dup .setuseciecolor not
+              { /DeviceRGB setcolorspace }
+            if
+          }
+        if
+      }
+    if
+
 		% Merge the request into the current page device,
 		% unless we're changing the OutputDevice.
 		% Stack: mark ... <merged> <failed>
@@ -759,7 +790,10 @@
 /.postinstall {		% mark ... <failed> <merged> .postinstall -
    matrix currentmatrix .setdefaultmatrix
 		% Erase and initialize the page.
-   erasepage initgraphics
+   initgraphics
+   currentoverprint false setoverprint 1 setcolor
+   .fillpage
+   0 setcolor setoverprint
    .beginpage
 
 		% Clean up, calling PolicyReport if needed.

Index: pdf_draw.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/pdf_draw.ps,v
retrieving revision 1.49
retrieving revision 1.50
diff -u -d -r1.49 -r1.50
--- pdf_draw.ps	3 Jul 2002 22:10:31 -0000	1.49
+++ pdf_draw.ps	22 Aug 2002 07:12:28 -0000	1.50
@@ -443,12 +443,8 @@
 .dicttomark readonly def
 
 /csrdict mark
-  /DeviceGray {
-    /DefaultGray Page /ColorSpace rget { exch pop resolvecolorspace } if
-  }
-  /DeviceRGB {
-    /DefaultRGB Page /ColorSpace rget { exch pop resolvecolorspace } if
-  }
+  /DeviceGray { }
+  /DeviceRGB { }
   /DeviceCMYK { }
   /CalGray {
     1 oget 6 dict begin

Index: pdf_main.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/pdf_main.ps,v
retrieving revision 1.54
retrieving revision 1.55
diff -u -d -r1.54 -r1.55
--- pdf_main.ps	18 Jul 2002 10:46:06 -0000	1.54
+++ pdf_main.ps	22 Aug 2002 07:12:28 -0000	1.55
@@ -112,6 +112,29 @@
    /Page null def
    /DSCPageCount 0 def
    /PDFSave null def
+	% Color space substitution in PDF is handled somewhat differently
+	% than in PostScript. A given device color space will be substituted
+	% if the corresponding "Default..." entry exists in the Page's
+	% Resource dictionary (which might be inhereted); there is no
+	% UseCIEColor to enable/disable color mapping.
+	%
+	% This behavior is achieved by always setting UseCIEColor to true
+	% in the page device dictionary. If the value of this parameter was
+	% originally false (i.e.: the output device does not perform color
+	% space substitution by default), the instances DefaultGray,
+	% DefaultRGB, and DefaultCMYK of the (local) ColorSpace category
+	% are redefined to be DeviceGray, DeviceRGB, and DeviceCMYK,
+	% respectively. This is not done if UseCIEColor is true by default,
+	% as in that case color substitution is presumably desired even
+	% if the file does not request it.
+   currentpagedevice /UseCIEColor .knownget dup { pop } if not
+    { .currentglobal false .setglobal
+      /DefaultGray { /DeviceGray } cvlit /ColorSpace defineresource pop
+      /DefaultRGB { /DeviceRGB } cvlit /ColorSpace defineresource pop
+      /DefaultCMYK { /DeviceCMYK } cvlit /ColorSpace defineresource pop
+      .setglobal
+    }
+   if
    GS_PDF_ProcSet begin
    pdfdict begin
    pdfopen begin
@@ -594,6 +617,8 @@
    pdfshowpage_setpage 
    save /PDFSave exch store
    /PDFdictstackcount countdictstack store
+   % set up color space substitution (this must be inside the page save)
+   pdfshowpage_setcspacesub
    (before exec) VMDEBUG
    pdfshowpage_finish
    % Some PDF files don't have matching q/Q (gsave/grestore) so we need
@@ -634,8 +659,10 @@
 } bind def
 
 /pdfshowpage_setpage {	% <pagedict> pdfshowpage_setpage <pagedict>
-  4 dict begin		% for setpagedevice
+  5 dict begin		% for setpagedevice
 	% Stack: pagedict
+  % UseCIEColor is always true for PDF; see the comment in runpdf above
+  /UseCIEColor true def
   currentpagedevice /Orientation 2 index /Rotate pget not { 0 } if 90 idiv
 	% Rotate specifies *clockwise* rotation!
     neg 3 and def
@@ -821,6 +848,30 @@
     pop false exit
   } loop
 } bind def
+
+% ------ ColorSpace substitution support ------ %
+
+%
+%  <pagedict>   pdfshowpage_setcspacesub   <pagedict>
+%
+% Set up color space substitution for a page. Invocations of this procedure
+% must be bracketed by the save/restore operation for the page, to avoid
+% unintended affects on other pages.
+%
+/pdfshowpage_setcspacesub
+  {
+      { /DefaultGray /DefaultRGB /DefaultCMYK }
+      {
+        dup 2 index /ColorSpace //rget exec
+          { resolvecolorspace /ColorSpace defineresource pop }
+          { pop }
+        ifelse
+      }
+    forall
+  }
+bind def
+
+
 
 end			% pdfdict
 .setglobal




More information about the gs-cvs mailing list