summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlgs/lib/gs_devcs.ps
blob: 08ad3591978abf029bfbbfa1b89c2925a8ac57d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
%    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 7216 2006-11-22 01:26:37Z ray $
% 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

systemdict /..page_default_spaces 3 dict put

% <color_space_name> ..includecolorspace -
/..includecolorspace
{ % Only includes ones explicitly defined by the document.
  systemdict /..page_default_spaces get 1 index known {
    pop
  } {
    mark exch
    { dup /ColorSpace resourcestatus {
        pop 0 eq {
          systemdict /..page_default_spaces get 1 index //true put
          gsave
          { dup /ColorSpace findresource //_setcolorspace_nosub exec .includecolorspace
          } stopped pop
          grestore
        } if
      } if
    } stopped pop
    cleartomark
  } ifelse
} bind def

% <color_space> <color_space_name> cs_substitute_generic <color_space1> <color_space2>
/cs_substitute_generic
{ .getuseciecolor
    { NOSUBSTDEVICECOLORS 
        { //..includecolorspace exec dup }
        { /ColorSpace findresource }
      ifelse
    }
    { pop dup }
  ifelse
}
bind def

% <color_space> <color_space_name> cs_substitute_DeviceRGB_for_PDFX_or_PDFA <color_space1> <color_space2>
/cs_substitute_DeviceRGB_for_PDFX_or_PDFA
{ systemdict /PDFX .knownget not { false } if
  systemdict /PDFA .knownget not { false } if
  or {
    dup /ColorSpace resourcestatus {
      pop pop
    } {
      (Error: Need a /DefaultRGB /ColorSpace resource for generating a PDF/X or PDF/A document.) =
      /cs_substitute_DeviceRGB_for_PDFX_or_PDFA cvx /undefined signalerror
    } ifelse
    /ColorSpace findresource
  } {
    //cs_substitute_generic exec
  } 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.0 } bind
    /cs_get_currentgray //pop_1
    /cs_get_currentrgb { pop dup dup } bind
    /cs_get_currentcmyk { pop 1.0 exch sub 0.0 0.0 0.0 4 -1 roll } bind
    /cs_validate {}

    /cs_substitute
      { /DefaultGray //cs_substitute_generic exec
      }
    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.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.0 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 cvr 4 1 roll
        3
          { 3 index sub //bound_0_1 exec 3 1 roll }
        repeat

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

    /cs_validate {}

    /cs_substitute
      { /DefaultRGB //cs_substitute_DeviceRGB_for_PDFX_or_PDFA exec
      }
    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.0 0.0 1.0 } bind

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

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

    /cs_get_currentcmyk //pop_1

    /cs_validate {}

    /cs_substitute
      { /DefaultCMYK //cs_substitute_generic exec
      }
    bind

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

currentdict /..includecolorspace .undef
currentdict /cs_substitute_generic .undef

end     % .cspace_util
.setglobal