/src/ghostpdl/base/gsicc_nocm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2023 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* gsicc handling for unmanaged color. */ |
18 | | |
19 | | #include "std.h" |
20 | | #include "string_.h" |
21 | | #include "stdpre.h" |
22 | | #include "gstypes.h" |
23 | | #include "gsmemory.h" |
24 | | #include "gsstruct.h" |
25 | | #include "scommon.h" |
26 | | #include "strmio.h" |
27 | | #include "gx.h" |
28 | | #include "gxgstate.h" |
29 | | #include "gxcspace.h" |
30 | | #include "gsicc_cms.h" |
31 | | #include "gsicc_cache.h" |
32 | | |
33 | | /* A link structure for our non-cm color transform */ |
34 | | typedef struct nocm_link_s { |
35 | | /* Since RGB to CMYK requires BG and UCR, we need to have the |
36 | | gs_gstate available */ |
37 | | gs_gstate *pgs; |
38 | | byte num_in; |
39 | | byte num_out; |
40 | | gs_memory_t *memory; |
41 | | } nocm_link_t; |
42 | | |
43 | | static void gsicc_nocm_transform_general(const gx_device *dev, gsicc_link_t *icclink, |
44 | | void *inputcolor, void *outputcolor, |
45 | | int num_bytes_in, int num_bytes_out); |
46 | | |
47 | | /* Functions that should be optimized later to do planar/chunky with |
48 | | color conversions. Just putting in something that should work |
49 | | right now */ |
50 | | |
51 | | /* At most, we have 4 input and 4 output ptrs. Since this is used only in |
52 | | DeviceGray, DeviceRGB and DeviceCMYK cases */ |
53 | | static void |
54 | | gsicc_nocm_planar_to_planar(const gx_device *dev, gsicc_link_t *icclink, |
55 | | gsicc_bufferdesc_t *input_buff_desc, |
56 | | gsicc_bufferdesc_t *output_buff_desc, |
57 | | void *inputbuffer, void *outputbuffer) |
58 | 0 | { |
59 | 0 | int k, j; |
60 | 0 | byte *inputpos[4]; |
61 | 0 | byte *outputpos[4]; |
62 | 0 | byte *in_buffer_ptr = (byte *) inputbuffer; |
63 | 0 | byte *out_buffer_ptr = (byte *) outputbuffer; |
64 | 0 | byte in_color[4], out_color[4]; |
65 | |
|
66 | 0 | for (k = 0; k < input_buff_desc->num_chan; k++) { |
67 | 0 | inputpos[k] = in_buffer_ptr + k * input_buff_desc->plane_stride; |
68 | 0 | } |
69 | 0 | for (k = 0; k < output_buff_desc->num_chan; k++) { |
70 | 0 | outputpos[k] = out_buffer_ptr + k * output_buff_desc->plane_stride; |
71 | 0 | } |
72 | | /* Note to self. We currently only do this in the transparency buffer |
73 | | case which has byte representation so just stepping through |
74 | | plane_stride is ok at this time. */ |
75 | 0 | for (k = 0; k < input_buff_desc->plane_stride ; k++) { |
76 | 0 | for (j = 0; j < input_buff_desc->num_chan; j++) { |
77 | 0 | in_color[j] = *(inputpos[j]); |
78 | 0 | inputpos[j] += input_buff_desc->bytes_per_chan; |
79 | 0 | } |
80 | 0 | gsicc_nocm_transform_general(dev, icclink, (void*) &(in_color[0]), |
81 | 0 | (void*) &(out_color[0]), 1, 1); |
82 | 0 | for (j = 0; j < output_buff_desc->num_chan; j++) { |
83 | 0 | *(outputpos[j]) = out_color[j]; |
84 | 0 | outputpos[j] += output_buff_desc->bytes_per_chan; |
85 | 0 | } |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | /* This is not really used yet */ |
90 | | static void |
91 | | gsicc_nocm_planar_to_chunky(const gx_device *dev, gsicc_link_t *icclink, |
92 | | gsicc_bufferdesc_t *input_buff_desc, |
93 | | gsicc_bufferdesc_t *output_buff_desc, |
94 | | void *inputbuffer, void *outputbuffer) |
95 | 0 | { |
96 | | |
97 | |
|
98 | 0 | } |
99 | | |
100 | | /* This is used with the fast thresholding code when doing -dUseFastColor |
101 | | and going out to a planar device */ |
102 | | static void |
103 | | gsicc_nocm_chunky_to_planar(const gx_device *dev, gsicc_link_t *icclink, |
104 | | gsicc_bufferdesc_t *input_buff_desc, |
105 | | gsicc_bufferdesc_t *output_buff_desc, |
106 | | void *inputbuffer, void *outputbuffer) |
107 | 0 | { |
108 | 0 | int k, j, m; |
109 | 0 | byte *inputpos = (byte *) inputbuffer; |
110 | 0 | byte *outputpos = (byte *) outputbuffer; |
111 | 0 | byte *output_loc; |
112 | 0 | byte *inputcolor; |
113 | 0 | byte outputcolor[8]; /* 8 since we have max 4 colorants and 2 bytes/colorant */ |
114 | 0 | unsigned short *pos_in_short, *pos_out_short; |
115 | 0 | int num_bytes_in = input_buff_desc->bytes_per_chan; |
116 | 0 | int num_bytes_out = output_buff_desc->bytes_per_chan; |
117 | 0 | int pixel_in_step = num_bytes_in * input_buff_desc->num_chan; |
118 | 0 | int plane_stride = output_buff_desc->plane_stride; |
119 | | |
120 | | /* Do row by row. */ |
121 | 0 | for (k = 0; k < input_buff_desc->num_rows ; k++) { |
122 | 0 | inputcolor = inputpos; |
123 | 0 | output_loc = outputpos; |
124 | | |
125 | | /* split the 2 byte 1 byte case here to avoid decision in inner loop */ |
126 | 0 | if (output_buff_desc->bytes_per_chan == 1) { |
127 | 0 | for (j = 0; j < input_buff_desc->pixels_per_row; j++) { |
128 | 0 | gsicc_nocm_transform_general(dev, icclink, (void*) inputcolor, |
129 | 0 | (void*) &(outputcolor[0]), num_bytes_in, |
130 | 0 | num_bytes_out); |
131 | | /* Stuff the output in the proper planar location */ |
132 | 0 | for (m = 0; m < output_buff_desc->num_chan; m++) { |
133 | 0 | *(output_loc + m * plane_stride + j) = outputcolor[m]; |
134 | 0 | } |
135 | 0 | inputcolor += pixel_in_step; |
136 | 0 | } |
137 | 0 | inputpos += input_buff_desc->row_stride; |
138 | 0 | outputpos += output_buff_desc->row_stride; |
139 | 0 | } else { |
140 | 0 | for (j = 0; j < input_buff_desc->pixels_per_row; j++) { |
141 | 0 | gsicc_nocm_transform_general(dev, icclink, (void*) inputcolor, |
142 | 0 | (void*) &(outputcolor[0]), num_bytes_in, |
143 | 0 | num_bytes_out); |
144 | | /* Stuff the output in the proper planar location */ |
145 | 0 | pos_in_short = (unsigned short*) &(outputcolor[0]); |
146 | 0 | pos_out_short = (unsigned short*) (output_loc); |
147 | 0 | for (m = 0; m < output_buff_desc->num_chan; m++) { |
148 | 0 | *(pos_out_short + m * plane_stride + j) = pos_in_short[m]; |
149 | 0 | } |
150 | 0 | inputcolor += pixel_in_step; |
151 | 0 | } |
152 | 0 | inputpos += input_buff_desc->row_stride; |
153 | 0 | outputpos += output_buff_desc->row_stride; |
154 | 0 | } |
155 | 0 | } |
156 | 0 | } |
157 | | |
158 | | static void |
159 | | gsicc_nocm_chunky_to_chunky(const gx_device *dev, gsicc_link_t *icclink, |
160 | | gsicc_bufferdesc_t *input_buff_desc, |
161 | | gsicc_bufferdesc_t *output_buff_desc, |
162 | | void *inputbuffer, void *outputbuffer) |
163 | 0 | { |
164 | 0 | int k, j; |
165 | 0 | byte *inputpos = (byte *) inputbuffer; |
166 | 0 | byte *outputpos = (byte *) outputbuffer; |
167 | 0 | byte *inputcolor, *outputcolor; |
168 | 0 | int num_bytes_in = input_buff_desc->bytes_per_chan; |
169 | 0 | int num_bytes_out = output_buff_desc->bytes_per_chan; |
170 | 0 | int pixel_in_step = num_bytes_in * input_buff_desc->num_chan; |
171 | 0 | int pixel_out_step = num_bytes_out * output_buff_desc->num_chan; |
172 | | |
173 | | /* Do row by row. */ |
174 | 0 | for (k = 0; k < input_buff_desc->num_rows ; k++) { |
175 | 0 | inputcolor = inputpos; |
176 | 0 | outputcolor = outputpos; |
177 | 0 | for (j = 0; j < input_buff_desc->pixels_per_row; j++) { |
178 | 0 | gsicc_nocm_transform_general(dev, icclink, (void*) inputcolor, |
179 | 0 | (void*) outputcolor, num_bytes_in, |
180 | 0 | num_bytes_out); |
181 | 0 | inputcolor += pixel_in_step; |
182 | 0 | outputcolor += pixel_out_step; |
183 | 0 | } |
184 | 0 | inputpos += input_buff_desc->row_stride; |
185 | 0 | outputpos += output_buff_desc->row_stride; |
186 | 0 | } |
187 | 0 | } |
188 | | |
189 | | /* Transform an entire buffer using the generic (non color managed) |
190 | | transformations */ |
191 | | static int |
192 | | gsicc_nocm_transform_color_buffer(gx_device *dev, gsicc_link_t *icclink, |
193 | | gsicc_bufferdesc_t *input_buff_desc, |
194 | | gsicc_bufferdesc_t *output_buff_desc, |
195 | | void *inputbuffer, void *outputbuffer) |
196 | 0 | { |
197 | | /* Since we have to do the mappings to and from frac colors we will for |
198 | | now just call the gsicc_nocm_transform_color as we step through the |
199 | | buffers. This process can be significantly sped up */ |
200 | |
|
201 | 0 | if (input_buff_desc->is_planar) { |
202 | 0 | if (output_buff_desc->is_planar) { |
203 | 0 | gsicc_nocm_planar_to_planar(dev, icclink, input_buff_desc, |
204 | 0 | output_buff_desc, inputbuffer, |
205 | 0 | outputbuffer); |
206 | 0 | } else { |
207 | 0 | gsicc_nocm_planar_to_chunky(dev, icclink, input_buff_desc, |
208 | 0 | output_buff_desc, inputbuffer, |
209 | 0 | outputbuffer); |
210 | 0 | } |
211 | 0 | } else { |
212 | 0 | if (output_buff_desc->is_planar) { |
213 | 0 | gsicc_nocm_chunky_to_planar(dev, icclink, input_buff_desc, |
214 | 0 | output_buff_desc, inputbuffer, |
215 | 0 | outputbuffer); |
216 | 0 | } else { |
217 | 0 | gsicc_nocm_chunky_to_chunky(dev, icclink, input_buff_desc, |
218 | 0 | output_buff_desc, inputbuffer, |
219 | 0 | outputbuffer); |
220 | 0 | } |
221 | 0 | } |
222 | 0 | return 0; |
223 | 0 | } |
224 | | |
225 | | /* Shared function between the single and buffer conversions */ |
226 | | static void |
227 | | gsicc_nocm_transform_general(const gx_device *dev, gsicc_link_t *icclink, |
228 | | void *inputcolor, void *outputcolor, |
229 | | int num_bytes_in, int num_bytes_out) |
230 | 0 | { |
231 | | /* Input data is either single byte or 2 byte color values. The |
232 | | color mapping procs work on frac values so we have to sandwich |
233 | | the transformation between to and from frac conversions. We are only |
234 | | doing at most 4 source colors here */ |
235 | 0 | nocm_link_t *link = (nocm_link_t*) icclink->link_handle; |
236 | 0 | byte num_in = link->num_in; |
237 | 0 | byte num_out = link->num_out; |
238 | 0 | frac frac_in[4]; |
239 | 0 | frac frac_out[GX_DEVICE_COLOR_MAX_COMPONENTS]; |
240 | 0 | int k; |
241 | 0 | const gx_device *map_dev; |
242 | 0 | const gx_cm_color_map_procs *procs; |
243 | | |
244 | |
|
245 | 0 | if (num_bytes_in == 2) { |
246 | 0 | unsigned short *data = (unsigned short *) inputcolor; |
247 | 0 | for (k = 0; k < num_in; k++) { |
248 | 0 | frac_in[k] = ushort2frac(data[k]); |
249 | 0 | } |
250 | 0 | } else { |
251 | 0 | byte *data = (byte *) inputcolor; |
252 | 0 | for (k = 0; k < num_in; k++) { |
253 | 0 | frac_in[k] = byte2frac(data[k]); |
254 | 0 | } |
255 | 0 | } |
256 | | /* Use the device procedures to do the mapping */ |
257 | 0 | switch (num_in) { |
258 | 0 | case 1: |
259 | 0 | procs = dev_proc(dev, get_color_mapping_procs)(dev, &map_dev); |
260 | 0 | procs->map_gray(map_dev, frac_in[0], frac_out); |
261 | 0 | break; |
262 | 0 | case 3: |
263 | 0 | procs = dev_proc(dev, get_color_mapping_procs)(dev, &map_dev); |
264 | 0 | procs->map_rgb(map_dev, link->pgs, frac_in[0], frac_in[1], |
265 | 0 | frac_in[2], frac_out); |
266 | 0 | break; |
267 | 0 | case 4: |
268 | 0 | procs = dev_proc(dev, get_color_mapping_procs)(dev, &map_dev); |
269 | 0 | procs->map_cmyk(map_dev, frac_in[0], frac_in[1], |
270 | 0 | frac_in[2], frac_in[3], frac_out); |
271 | 0 | break; |
272 | 0 | default: |
273 | 0 | memset(&(frac_out[0]), 0, sizeof(frac_out)); |
274 | 0 | break; |
275 | 0 | } |
276 | 0 | if (num_bytes_out == 2) { |
277 | 0 | unsigned short *data = (unsigned short *) outputcolor; |
278 | 0 | for (k = 0; k < num_out; k++) { |
279 | 0 | data[k] = frac2ushort(frac_out[k]); |
280 | 0 | } |
281 | 0 | } else { |
282 | 0 | byte *data = (byte *) outputcolor; |
283 | 0 | for (k = 0; k < num_out; k++) { |
284 | 0 | data[k] = frac2byte(frac_out[k]); |
285 | 0 | } |
286 | 0 | } |
287 | 0 | return; |
288 | 0 | } |
289 | | |
290 | | /* Transform a single color using the generic (non color managed) |
291 | | transformations */ |
292 | | static int |
293 | | gsicc_nocm_transform_color(gx_device *dev, gsicc_link_t *icclink, void *inputcolor, |
294 | | void *outputcolor, int num_bytes) |
295 | 0 | { |
296 | |
|
297 | 0 | gsicc_nocm_transform_general(dev, icclink, inputcolor, outputcolor, |
298 | 0 | num_bytes, num_bytes); |
299 | 0 | return 0; |
300 | 0 | } |
301 | | |
302 | | static void |
303 | | gsicc_nocm_freelink(gsicc_link_t *icclink) |
304 | 0 | { |
305 | 0 | nocm_link_t *nocm_link = (nocm_link_t*) icclink->link_handle; |
306 | |
|
307 | 0 | if (nocm_link) { |
308 | 0 | if (nocm_link->pgs != NULL) { |
309 | 0 | if (nocm_link->pgs->black_generation != NULL) { |
310 | 0 | gs_free_object(nocm_link->memory, nocm_link->pgs->black_generation, |
311 | 0 | "gsicc_nocm_freelink"); |
312 | 0 | } |
313 | 0 | if (nocm_link->pgs->undercolor_removal != NULL) { |
314 | 0 | gs_free_object(nocm_link->memory, nocm_link->pgs->undercolor_removal, |
315 | 0 | "gsicc_nocm_freelink"); |
316 | 0 | } |
317 | 0 | gs_free_object(nocm_link->memory, nocm_link->pgs, "gsicc_nocm_freelink"); |
318 | 0 | } |
319 | 0 | gs_free_object(nocm_link->memory, nocm_link, "gsicc_nocm_freelink"); |
320 | 0 | icclink->link_handle = NULL; |
321 | 0 | } |
322 | 0 | } |
323 | | |
324 | | /* Since this is the only occurence of this object we are not going to |
325 | | fool aroung with reference counting and closure functions. When |
326 | | the link is destroyed, we will simply free the bytes */ |
327 | | static gx_transfer_map* |
328 | | gsicc_nocm_copy_curve(gx_transfer_map *in_map, gs_memory_t *mem) |
329 | 0 | { |
330 | 0 | gx_transfer_map *out_map; |
331 | |
|
332 | 0 | if (in_map == NULL) { |
333 | 0 | return NULL; |
334 | 0 | } else { |
335 | 0 | out_map = (gx_transfer_map*) gs_alloc_bytes(mem, sizeof(gx_transfer_map), |
336 | 0 | "gsicc_nocm_copy_curve"); |
337 | 0 | if (out_map) { |
338 | 0 | memset(out_map, 0, sizeof(gx_transfer_map)); |
339 | 0 | out_map->proc = in_map->proc; |
340 | 0 | memcpy(&(out_map->values[0]), &(in_map->values[0]), |
341 | 0 | sizeof(frac) * transfer_map_size); |
342 | 0 | out_map->id = gs_no_id; |
343 | 0 | } |
344 | 0 | return out_map; |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | | /* Get the link, which is the mapping procedure in this non color managed |
349 | | transformation case. */ |
350 | | gsicc_link_t* |
351 | | gsicc_nocm_get_link(const gs_gstate *pgs, gx_device *dev, |
352 | | int num_input) |
353 | 0 | { |
354 | 0 | gsicc_link_t *result; |
355 | 0 | gsicc_hashlink_t hash; |
356 | 0 | nocm_link_t *nocm_link; |
357 | 0 | gs_memory_t *mem = pgs->icc_link_cache->memory->non_gc_memory; |
358 | 0 | bool pageneutralcolor = false; |
359 | 0 | cmm_dev_profile_t *dev_profile; |
360 | 0 | int code; |
361 | 0 | gsicc_colorbuffer_t data_cs = gsRGB; |
362 | |
|
363 | 0 | if (dev == NULL) |
364 | 0 | return NULL; |
365 | | |
366 | | /* Need to check if we need to monitor for color */ |
367 | 0 | code = dev_proc(dev, get_profile)(dev, &dev_profile); |
368 | 0 | if (code < 0) |
369 | 0 | return NULL; |
370 | 0 | if (dev_profile != NULL) { |
371 | 0 | pageneutralcolor = dev_profile->pageneutralcolor; |
372 | 0 | } |
373 | | |
374 | | /* We will add this to the link cache so that we can avoid the issue |
375 | | of black_generation and undercolor removal being GC values. |
376 | | Since the link is not GC we would need to copy the contents over |
377 | | each time a link was requested. This could be costly if we had |
378 | | a lot of link requests. */ |
379 | 0 | hash.rend_hash = gsCMM_NONE; |
380 | 0 | hash.des_hash = dev->color_info.num_components; |
381 | 0 | hash.src_hash = num_input; |
382 | 0 | hash.link_hashcode = num_input + hash.des_hash * 256 + hash.rend_hash * 4096; |
383 | | |
384 | | /* Check the cache for a hit. */ |
385 | 0 | result = gsicc_findcachelink(hash, pgs->icc_link_cache, false, false); |
386 | 0 | if (result != NULL) { |
387 | 0 | return result; |
388 | 0 | } |
389 | | /* If not, then lets create a new one. This may actually return a link if |
390 | | another thread has already created it while we were trying to do so */ |
391 | 0 | if (gsicc_alloc_link_entry(pgs->icc_link_cache, &result, hash, false, false)) |
392 | 0 | return result; |
393 | | |
394 | 0 | if (result == NULL) |
395 | 0 | return NULL; |
396 | | |
397 | | /* Now compute the link contents */ |
398 | | /* We (this thread) owns the lock on the new link just created. */ |
399 | | |
400 | 0 | result->procs.map_buffer = gsicc_nocm_transform_color_buffer; |
401 | 0 | result->procs.map_color = gsicc_nocm_transform_color; |
402 | 0 | result->procs.free_link = gsicc_nocm_freelink; |
403 | 0 | result->hashcode = hash; |
404 | 0 | nocm_link = (nocm_link_t *) gs_alloc_bytes(mem, sizeof(nocm_link_t), |
405 | 0 | "gsicc_nocm_get_link"); |
406 | 0 | if (nocm_link == NULL) |
407 | 0 | return NULL; |
408 | 0 | result->link_handle = (void*) nocm_link; |
409 | 0 | nocm_link->memory = mem; |
410 | | /* Create a dummy gs_gstate and populate the ucr/bg values. This |
411 | | is the only part that we need */ |
412 | 0 | if ((pgs->black_generation == NULL && pgs->undercolor_removal == NULL)) { |
413 | 0 | nocm_link->pgs = NULL; |
414 | 0 | } else { |
415 | 0 | nocm_link->pgs = (gs_gstate*) |
416 | 0 | gs_alloc_bytes(mem, sizeof(gs_gstate), |
417 | 0 | "gsicc_nocm_get_link"); |
418 | 0 | if (nocm_link->pgs == NULL) |
419 | 0 | return NULL; |
420 | 0 | memset(nocm_link->pgs, 0, sizeof(gs_gstate)); |
421 | | /* Note if allocation of either of the maps fails, just use NULL */ |
422 | 0 | nocm_link->pgs->black_generation = (gx_transfer_map*) |
423 | 0 | gsicc_nocm_copy_curve(pgs->black_generation, mem); |
424 | 0 | nocm_link->pgs->undercolor_removal = (gx_transfer_map*) |
425 | 0 | gsicc_nocm_copy_curve(pgs->undercolor_removal, mem); |
426 | 0 | } |
427 | 0 | nocm_link->num_out = min(dev->color_info.num_components, |
428 | 0 | GS_CLIENT_COLOR_MAX_COMPONENTS); |
429 | 0 | nocm_link->num_in = num_input; |
430 | |
|
431 | 0 | result->num_input = nocm_link->num_in; |
432 | 0 | result->num_output = nocm_link->num_out; |
433 | 0 | result->link_handle = nocm_link; |
434 | 0 | result->hashcode.link_hashcode = hash.link_hashcode; |
435 | 0 | result->hashcode.des_hash = hash.des_hash; |
436 | 0 | result->hashcode.src_hash = hash.src_hash; |
437 | 0 | result->hashcode.rend_hash = hash.rend_hash; |
438 | 0 | result->includes_softproof = false; |
439 | 0 | result->includes_devlink = false; |
440 | 0 | if (hash.src_hash == hash.des_hash) { |
441 | 0 | result->is_identity = true; |
442 | 0 | } else { |
443 | 0 | result->is_identity = false; |
444 | 0 | } |
445 | 0 | if (nocm_link->num_in == 4) |
446 | 0 | data_cs = gsCMYK; |
447 | 0 | else if (nocm_link->num_in == 1) |
448 | 0 | data_cs = gsGRAY; |
449 | 0 | result->data_cs = data_cs; |
450 | | |
451 | | /* Set up for monitoring if not gray */ |
452 | 0 | if (pageneutralcolor && nocm_link->num_in != 1) { |
453 | 0 | gsicc_mcm_set_link(result); |
454 | 0 | } |
455 | 0 | result->valid = true; |
456 | | /* Now release any tasks/threads waiting for these contents by unlocking */ |
457 | 0 | gx_monitor_leave(result->lock); /* done with updating, let everyone run */ |
458 | |
|
459 | 0 | return result; |
460 | 0 | } |