/src/ghostpdl/base/gxi12bit.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2025 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 | | /* 12-bit image procedures */ |
18 | | #include "gx.h" |
19 | | #include "memory_.h" |
20 | | #include "gpcheck.h" |
21 | | #include "gserrors.h" |
22 | | #include "gxfixed.h" |
23 | | #include "gxfrac.h" |
24 | | #include "gxarith.h" |
25 | | #include "gxmatrix.h" |
26 | | #include "gsccolor.h" |
27 | | #include "gspaint.h" |
28 | | #include "gxdevice.h" |
29 | | #include "gxcmap.h" |
30 | | #include "gxdcolor.h" |
31 | | #include "gxgstate.h" |
32 | | #include "gxdevmem.h" |
33 | | #include "gxcpath.h" |
34 | | #include "gximage.h" |
35 | | #include "gsicc.h" |
36 | | #include "gsicc_cache.h" |
37 | | #include "gsicc_cms.h" |
38 | | #include "gxcie.h" |
39 | | #include "gscie.h" |
40 | | #include "gxdevsop.h" |
41 | | |
42 | | /* ---------------- Unpacking procedures ---------------- */ |
43 | | |
44 | | const byte * |
45 | | sample_unpack_12(byte * bptr, int *pdata_x, const byte * data, |
46 | | int data_x, uint dsize, const sample_map *ignore_smap, int spread, |
47 | | int ignore_num_components_per_plane) |
48 | 0 | { |
49 | | /* Assuming an identity map for all components. */ |
50 | 0 | register frac *bufp = (frac *) bptr; |
51 | 0 | uint dskip = (data_x >> 1) * 3; |
52 | 0 | const byte *psrc = data + dskip; |
53 | 0 | #define inc_bufp(bp, n) bp = (frac *)((byte *)(bp) + (n)) |
54 | 0 | uint sample; |
55 | 0 | int left = dsize - dskip; |
56 | |
|
57 | 0 | if ((data_x & 1) && left > 0) |
58 | 0 | switch (left) { |
59 | 0 | default: |
60 | 0 | sample = ((uint) (psrc[1] & 0xf) << 8) + psrc[2]; |
61 | 0 | *bufp = bits2frac(sample, 12); |
62 | 0 | inc_bufp(bufp, spread); |
63 | 0 | psrc += 3; |
64 | 0 | left -= 3; |
65 | 0 | break; |
66 | 0 | case 2: /* xxxxxxxx xxxxdddd */ |
67 | 0 | *bufp = (psrc[1] & 0xf) * (frac_1 / 15); |
68 | 0 | case 1: /* xxxxxxxx */ |
69 | 0 | left = 0; |
70 | 0 | } |
71 | 0 | while (left >= 3) { |
72 | 0 | sample = ((uint) * psrc << 4) + (psrc[1] >> 4); |
73 | 0 | *bufp = bits2frac(sample, 12); |
74 | 0 | inc_bufp(bufp, spread); |
75 | 0 | sample = ((uint) (psrc[1] & 0xf) << 8) + psrc[2]; |
76 | 0 | *bufp = bits2frac(sample, 12); |
77 | 0 | inc_bufp(bufp, spread); |
78 | 0 | psrc += 3; |
79 | 0 | left -= 3; |
80 | 0 | } |
81 | | /* Handle trailing bytes. */ |
82 | 0 | switch (left) { |
83 | 0 | case 2: /* dddddddd ddddxxxx */ |
84 | 0 | sample = ((uint) * psrc << 4) + (psrc[1] >> 4); |
85 | 0 | *bufp = bits2frac(sample, 12); |
86 | 0 | inc_bufp(bufp, spread); |
87 | 0 | *bufp = (psrc[1] & 0xf) * (frac_1 / 15); |
88 | 0 | break; |
89 | 0 | case 1: /* dddddddd */ |
90 | 0 | sample = (uint) * psrc << 4; |
91 | 0 | *bufp = bits2frac(sample, 12); |
92 | 0 | break; |
93 | 0 | case 0: /* Nothing more to do. */ |
94 | 0 | ; |
95 | 0 | } |
96 | 0 | *pdata_x = 0; |
97 | 0 | return bptr; |
98 | 0 | } |
99 | | |
100 | | /* ------ Strategy procedure ------ */ |
101 | | |
102 | | /* Check the prototype. */ |
103 | | iclass_proc(gs_image_class_2_fracs); |
104 | | |
105 | | /* Use special (slow) logic for 12-bit source values. */ |
106 | | static irender_proc(image_render_frac); |
107 | | static irender_proc(image_render_icc16); /* icc 16bit case */ |
108 | | |
109 | | int |
110 | | gs_image_class_2_fracs(gx_image_enum * penum, irender_proc_t *render_fn) |
111 | 1.99M | { |
112 | 1.99M | bool std_cmap_procs; |
113 | | |
114 | 1.99M | if (penum->bps > 8) { |
115 | 0 | if (penum->use_mask_color) { |
116 | | /* Convert color mask values to fracs. */ |
117 | 0 | int i; |
118 | |
|
119 | 0 | for (i = 0; i < penum->spp * 2; ++i) |
120 | 0 | penum->mask_color.values[i] = |
121 | 0 | bits2frac(penum->mask_color.values[i], 12); |
122 | 0 | } |
123 | | /* If the device has some unique color mapping procs due to its color space, |
124 | | then we will need to use those and go through pixel by pixel instead |
125 | | of blasting through buffers. This is true for example with many of |
126 | | the color spaces for CUPs */ |
127 | 0 | std_cmap_procs = gx_device_uses_std_cmap_procs(penum->dev, penum->pgs); |
128 | 0 | if ( (gs_color_space_get_index(penum->pcs) == gs_color_space_index_DeviceN && |
129 | 0 | penum->pcs->cmm_icc_profile_data == NULL) || |
130 | 0 | (gs_color_space_get_index(penum->pcs) == gs_color_space_index_Separation && |
131 | 0 | penum->pcs->cmm_icc_profile_data == NULL) || |
132 | 0 | penum->use_mask_color || |
133 | 0 | penum->bps != 16 || !std_cmap_procs || |
134 | 0 | gs_color_space_get_index(penum->pcs) == gs_color_space_index_DevicePixel || |
135 | 0 | gs_color_space_get_index(penum->pcs) == gs_color_space_index_Indexed) { |
136 | | /* DevicePixel color space used in mask from 3x type. Basically |
137 | | a simple color space that just is scaled to the device bit |
138 | | depth when remapped. No CM needed */ |
139 | 0 | if_debug0m('b', penum->memory, "[b]render=frac\n"); |
140 | 0 | *render_fn = &image_render_frac; |
141 | 0 | return 0; |
142 | 0 | } else { |
143 | | /* Set up the link now */ |
144 | 0 | const gs_color_space *pcs; |
145 | 0 | gsicc_rendering_param_t rendering_params; |
146 | 0 | int k; |
147 | 0 | int src_num_comp = cs_num_components(penum->pcs); |
148 | 0 | int num_des_comps; |
149 | 0 | int code; |
150 | 0 | cmm_dev_profile_t *dev_profile; |
151 | |
|
152 | 0 | code = dev_proc(penum->dev, get_profile)(penum->dev, &dev_profile); |
153 | 0 | if (code < 0) |
154 | 0 | return 0; |
155 | 0 | num_des_comps = gsicc_get_device_profile_comps(dev_profile); |
156 | 0 | penum->icc_setup.need_decode = false; |
157 | | /* Check if we need to do any decoding. If yes, then that will slow us down */ |
158 | 0 | for (k = 0; k < src_num_comp; k++) { |
159 | 0 | if ( penum->map[k].decoding != sd_none ) { |
160 | 0 | penum->icc_setup.need_decode = true; |
161 | 0 | break; |
162 | 0 | } |
163 | 0 | } |
164 | | /* Define the rendering intents */ |
165 | 0 | rendering_params.black_point_comp = penum->pgs->blackptcomp; |
166 | 0 | rendering_params.graphics_type_tag = GS_IMAGE_TAG; |
167 | 0 | rendering_params.override_icc = false; |
168 | 0 | rendering_params.preserve_black = gsBKPRESNOTSPECIFIED; |
169 | 0 | rendering_params.rendering_intent = penum->pgs->renderingintent; |
170 | 0 | rendering_params.cmm = gsCMM_DEFAULT; |
171 | 0 | if (gs_color_space_is_PSCIE(penum->pcs) && penum->pcs->icc_equivalent != NULL) { |
172 | 0 | pcs = penum->pcs->icc_equivalent; |
173 | 0 | } else { |
174 | 0 | pcs = penum->pcs; |
175 | 0 | } |
176 | 0 | if (pcs->cmm_icc_profile_data != NULL) |
177 | 0 | penum->icc_setup.is_lab = pcs->cmm_icc_profile_data->islab; |
178 | 0 | penum->icc_setup.must_halftone = gx_device_must_halftone(penum->dev); |
179 | 0 | penum->icc_setup.has_transfer = |
180 | 0 | gx_has_transfer(penum->pgs, num_des_comps); |
181 | 0 | if (penum->icc_setup.is_lab) penum->icc_setup.need_decode = false; |
182 | 0 | if (penum->icc_link == NULL) { |
183 | 0 | penum->icc_link = gsicc_get_link(penum->pgs, penum->dev, pcs, NULL, |
184 | 0 | &rendering_params, penum->memory); |
185 | 0 | } |
186 | | /* Use the direct unpacking proc */ |
187 | 0 | penum->unpack = sample_unpackicc_16; |
188 | 0 | if_debug0m('b', penum->memory, "[b]render=icc16\n"); |
189 | 0 | *render_fn = &image_render_icc16; |
190 | 0 | return 0; |
191 | 0 | } |
192 | 0 | } |
193 | 1.99M | return 0; |
194 | 1.99M | } |
195 | | |
196 | | /* ---------------- Rendering procedures ---------------- */ |
197 | | |
198 | | /* ------ Rendering for 12-bit samples ------ */ |
199 | | |
200 | | #define FRACS_PER_LONG (ARCH_SIZEOF_LONG / arch_sizeof_frac) |
201 | | typedef union { |
202 | | frac v[GS_IMAGE_MAX_COLOR_COMPONENTS]; |
203 | | #define LONGS_PER_COLOR_FRACS\ |
204 | | ((GS_IMAGE_MAX_COLOR_COMPONENTS + FRACS_PER_LONG - 1) / FRACS_PER_LONG) |
205 | | long all[LONGS_PER_COLOR_FRACS]; /* for fast comparison */ |
206 | | } color_fracs; |
207 | | |
208 | | #define LONGS_PER_4_FRACS ((arch_sizeof_frac * 4 + ARCH_SIZEOF_LONG - 1) / ARCH_SIZEOF_LONG) |
209 | | #if LONGS_PER_4_FRACS == 1 |
210 | | # define COLOR_FRACS_4_EQ(f1, f2)\ |
211 | 0 | ((f1).all[0] == (f2).all[0]) |
212 | | #else |
213 | | #if LONGS_PER_4_FRACS == 2 |
214 | | # define COLOR_FRACS_4_EQ(f1, f2)\ |
215 | | ((f1).all[0] == (f2).all[0] && (f1).all[1] == (f2).all[1]) |
216 | | #endif |
217 | | #endif |
218 | | |
219 | | /* Test whether a color is transparent. */ |
220 | | static bool |
221 | | mask_color12_matches(const frac *v, const gx_image_enum *penum, |
222 | | int num_components) |
223 | 0 | { |
224 | 0 | int i; |
225 | |
|
226 | 0 | for (i = num_components * 2, v += num_components - 1; (i -= 2) >= 0; --v) |
227 | 0 | if (*v < penum->mask_color.values[i] || |
228 | 0 | *v > penum->mask_color.values[i + 1] |
229 | 0 | ) |
230 | 0 | return false; |
231 | 0 | return true; |
232 | 0 | } |
233 | | |
234 | | /* Render an image with more than 8 bits per sample. */ |
235 | | /* The samples have been expanded into fracs. */ |
236 | | static int |
237 | | image_render_frac(gx_image_enum * penum, const byte * buffer, int data_x, |
238 | | uint w, int h, gx_device * dev) |
239 | 0 | { |
240 | 0 | const gs_gstate *pgs = penum->pgs; |
241 | 0 | gs_logical_operation_t lop = penum->log_op; |
242 | 0 | gx_dda_fixed_point pnext; |
243 | 0 | image_posture posture = penum->posture; |
244 | 0 | fixed xl, ytf; |
245 | 0 | fixed pdyx, pdyy; /* edge of parallelogram */ |
246 | 0 | int yt = penum->yci, iht = penum->hci; |
247 | 0 | const gs_color_space *pcs = penum->pcs; |
248 | 0 | cs_proc_remap_color((*remap_color)) = pcs->type->remap_color; |
249 | 0 | gs_client_color cc; |
250 | 0 | bool device_color = penum->device_color; |
251 | 0 | const gx_color_map_procs *cmap_procs = gx_get_cmap_procs(pgs, dev); |
252 | 0 | cmap_proc_rgb((*map_rgb)) = cmap_procs->map_rgb; |
253 | 0 | cmap_proc_cmyk((*map_cmyk)) = cmap_procs->map_cmyk; |
254 | 0 | bool use_mask_color = penum->use_mask_color; |
255 | 0 | gx_device_color devc1, devc2; |
256 | 0 | gx_device_color *pdevc = &devc1; |
257 | 0 | gx_device_color *pdevc_next = &devc2; |
258 | 0 | int spp = penum->spp; |
259 | 0 | const frac *psrc_initial = (const frac *)buffer + data_x * spp; |
260 | 0 | const frac *psrc = psrc_initial; |
261 | 0 | const frac *rsrc = psrc + spp; /* psrc + spp at start of run */ |
262 | 0 | fixed xrun; /* x at start of run */ |
263 | 0 | int irun; /* int xrun */ |
264 | 0 | fixed yrun; /* y ditto */ |
265 | 0 | color_fracs run; /* run value */ |
266 | 0 | color_fracs next; /* next sample value */ |
267 | 0 | const frac *bufend = psrc + w; |
268 | 0 | int code = 0, mcode = 0; |
269 | 0 | int i; |
270 | 0 | bool is_devn = false; |
271 | 0 | bool is_sep = (gs_color_space_get_index(penum->pcs) == |
272 | 0 | gs_color_space_index_Separation); |
273 | |
|
274 | 0 | if (h == 0) |
275 | 0 | return 0; |
276 | 0 | pnext = penum->dda.pixel0; |
277 | 0 | xrun = xl = dda_current(pnext.x); |
278 | 0 | irun = fixed2int_var_rounded(xrun); |
279 | 0 | yrun = ytf = dda_current(pnext.y); |
280 | 0 | pdyx = dda_current(penum->dda.row.x) - penum->cur.x; |
281 | 0 | pdyy = dda_current(penum->dda.row.y) - penum->cur.y; |
282 | 0 | if_debug5m('b', penum->memory, "[b]y=%d data_x=%d w=%d xt=%f yt=%f\n", |
283 | 0 | penum->y, data_x, w, fixed2float(xl), fixed2float(ytf)); |
284 | 0 | memset(&run, 0, sizeof(run)); |
285 | 0 | memset(&next, 0, sizeof(next)); |
286 | | /* Ensure that we don't get any false dev_color_eq hits. */ |
287 | 0 | set_nonclient_dev_color(&devc1, gx_no_color_index); |
288 | 0 | set_nonclient_dev_color(&devc2, gx_no_color_index); |
289 | 0 | cs_full_init_color(&cc, pcs); |
290 | 0 | run.v[0] = ~psrc[0]; /* force remap */ |
291 | |
|
292 | 0 | if (dev_proc(dev, dev_spec_op)(dev, gxdso_supports_devn, NULL, 0)) { |
293 | 0 | for (i = 0; i < GS_CLIENT_COLOR_MAX_COMPONENTS; i++) { |
294 | 0 | pdevc->colors.devn.values[i] = 0; |
295 | 0 | pdevc_next->colors.devn.values[i] = 0; |
296 | 0 | } |
297 | 0 | is_devn = true; |
298 | 0 | } |
299 | 0 | while (psrc < bufend) { |
300 | 0 | next.v[0] = psrc[0]; |
301 | 0 | switch (spp) { |
302 | 0 | case 4: /* may be CMYK */ |
303 | 0 | next.v[1] = psrc[1]; |
304 | 0 | next.v[2] = psrc[2]; |
305 | 0 | next.v[3] = psrc[3]; |
306 | 0 | psrc += 4; |
307 | 0 | if (COLOR_FRACS_4_EQ(next, run)) |
308 | 0 | goto inc; |
309 | 0 | if (use_mask_color && mask_color12_matches(next.v, penum, 4)) { |
310 | 0 | color_set_null(pdevc_next); |
311 | 0 | goto f; |
312 | 0 | } |
313 | 0 | if (device_color) { |
314 | 0 | (*map_cmyk) (next.v[0], next.v[1], |
315 | 0 | next.v[2], next.v[3], |
316 | 0 | pdevc_next, pgs, dev, |
317 | 0 | gs_color_select_source, NULL); |
318 | 0 | goto f; |
319 | 0 | } |
320 | 0 | decode_frac(next.v[0], cc, 0); |
321 | 0 | decode_frac(next.v[1], cc, 1); |
322 | 0 | decode_frac(next.v[2], cc, 2); |
323 | 0 | decode_frac(next.v[3], cc, 3); |
324 | 0 | if_debug4m('B', penum->memory, "[B]cc[0..3]=%g,%g,%g,%g\n", |
325 | 0 | cc.paint.values[0], cc.paint.values[1], |
326 | 0 | cc.paint.values[2], cc.paint.values[3]); |
327 | 0 | break; |
328 | 0 | case 3: /* may be RGB */ |
329 | 0 | next.v[1] = psrc[1]; |
330 | 0 | next.v[2] = psrc[2]; |
331 | 0 | psrc += 3; |
332 | 0 | if (COLOR_FRACS_4_EQ(next, run)) |
333 | 0 | goto inc; |
334 | 0 | if (use_mask_color && mask_color12_matches(next.v, penum, 3)) { |
335 | 0 | color_set_null(pdevc_next); |
336 | 0 | goto f; |
337 | 0 | } |
338 | 0 | if (device_color) { |
339 | 0 | (*map_rgb) (next.v[0], next.v[1], |
340 | 0 | next.v[2], pdevc_next, pgs, dev, |
341 | 0 | gs_color_select_source); |
342 | 0 | goto f; |
343 | 0 | } |
344 | 0 | decode_frac(next.v[0], cc, 0); |
345 | 0 | decode_frac(next.v[1], cc, 1); |
346 | 0 | decode_frac(next.v[2], cc, 2); |
347 | 0 | if_debug3m('B', penum->memory, "[B]cc[0..2]=%g,%g,%g\n", |
348 | 0 | cc.paint.values[0], cc.paint.values[1], |
349 | 0 | cc.paint.values[2]); |
350 | 0 | break; |
351 | 0 | case 1: /* may be Gray, but could be a separation */ |
352 | 0 | if (is_devn && is_sep) { |
353 | 0 | psrc++; |
354 | 0 | if (next.v[0] == run.v[0]) |
355 | 0 | goto inc; |
356 | 0 | if (use_mask_color && mask_color12_matches(next.v, penum, 1)) { |
357 | 0 | color_set_null(pdevc_next); |
358 | 0 | goto f; |
359 | 0 | } |
360 | 0 | decode_frac(next.v[0], cc, 0); |
361 | 0 | if_debug1m('B', penum->memory, "[B]cc[0]=%g\n", |
362 | 0 | cc.paint.values[0]); |
363 | 0 | break; |
364 | 0 | } else { |
365 | 0 | psrc++; |
366 | 0 | if (next.v[0] == run.v[0]) |
367 | 0 | goto inc; |
368 | 0 | if (use_mask_color && mask_color12_matches(next.v, penum, 1)) { |
369 | 0 | color_set_null(pdevc_next); |
370 | 0 | goto f; |
371 | 0 | } |
372 | 0 | if (device_color) { |
373 | 0 | (*map_rgb) (next.v[0], next.v[0], |
374 | 0 | next.v[0], pdevc_next, pgs, dev, |
375 | 0 | gs_color_select_source); |
376 | 0 | goto f; |
377 | 0 | } |
378 | 0 | decode_frac(next.v[0], cc, 0); |
379 | 0 | if_debug1('B', "[B]cc[0]=%g\n", |
380 | 0 | cc.paint.values[0]); |
381 | 0 | break; |
382 | 0 | } |
383 | 0 | default: /* DeviceN */ |
384 | 0 | { |
385 | 0 | int i; |
386 | |
|
387 | 0 | for (i = 1; i < spp; ++i) |
388 | 0 | next.v[i] = psrc[i]; |
389 | 0 | psrc += spp; |
390 | 0 | if (!memcmp(next.v, run.v, spp * sizeof(next.v[0]))) |
391 | 0 | goto inc; |
392 | 0 | if (use_mask_color && |
393 | 0 | mask_color12_matches(next.v, penum, spp) |
394 | 0 | ) { |
395 | 0 | color_set_null(pdevc_next); |
396 | 0 | goto f; |
397 | 0 | } |
398 | 0 | for (i = 0; i < spp; ++i) |
399 | 0 | decode_frac(next.v[i], cc, i); |
400 | | #ifdef DEBUG |
401 | | if (gs_debug_c('B')) { |
402 | | dmprintf2(dev->memory, "[B]cc[0..%d]=%g", spp - 1, |
403 | | cc.paint.values[0]); |
404 | | for (i = 1; i < spp; ++i) |
405 | | dmprintf1(dev->memory, ",%g", cc.paint.values[i]); |
406 | | dmputs(dev->memory, "\n"); |
407 | | } |
408 | | #endif |
409 | 0 | } |
410 | 0 | break; |
411 | 0 | } |
412 | 0 | mcode = remap_color(&cc, pcs, pdevc_next, pgs, dev, |
413 | 0 | gs_color_select_source); |
414 | 0 | if (mcode < 0) |
415 | 0 | goto fill; |
416 | 0 | f: |
417 | 0 | if (sizeof(pdevc_next->colors.binary.color[0]) <= sizeof(ulong)) |
418 | 0 | if_debug7m('B', penum->memory, |
419 | 0 | "[B]0x%x,0x%x,0x%x,0x%x -> 0x%lx,0x%lx," PRI_INTPTR "\n", |
420 | 0 | next.v[0], next.v[1], next.v[2], next.v[3], |
421 | 0 | (ulong)pdevc_next->colors.binary.color[0], |
422 | 0 | (ulong)pdevc_next->colors.binary.color[1], |
423 | 0 | (intptr_t)pdevc_next->type); |
424 | 0 | else |
425 | 0 | if_debug9m('B', penum->memory, |
426 | 0 | "[B]0x%x,0x%x,0x%x,0x%x -> 0x%08lx%08lx,0x%08lx%08lx," PRI_INTPTR "\n", |
427 | 0 | next.v[0], next.v[1], next.v[2], next.v[3], |
428 | 0 | (ulong)(pdevc_next->colors.binary.color[0] >> |
429 | 0 | 8 * (sizeof(pdevc_next->colors.binary.color[0]) - sizeof(ulong))), |
430 | 0 | (ulong)pdevc_next->colors.binary.color[0], |
431 | 0 | (ulong)(pdevc_next->colors.binary.color[1] >> |
432 | 0 | 8 * (sizeof(pdevc_next->colors.binary.color[1]) - sizeof(ulong))), |
433 | 0 | (ulong)pdevc_next->colors.binary.color[1], |
434 | 0 | (intptr_t)pdevc_next->type); |
435 | | /* NB: sizeof gx_color_index is 4 or 8 bytes! */ |
436 | | |
437 | | /* Even though the supplied colors don't match, */ |
438 | | /* the device colors might. */ |
439 | 0 | if (!dev_color_eq(devc1, devc2)) { |
440 | | /* Fill the region between xrun/irun and xl */ |
441 | 0 | gx_device_color *ptemp; |
442 | |
|
443 | 0 | fill: |
444 | 0 | if (posture != image_portrait) { /* Parallelogram */ |
445 | 0 | code = (*dev_proc(dev, fill_parallelogram)) |
446 | 0 | (dev, xrun, yrun, |
447 | 0 | xl - xrun, ytf - yrun, pdyx, pdyy, |
448 | 0 | pdevc, lop); |
449 | 0 | } else { /* Rectangle */ |
450 | 0 | int xi = irun; |
451 | 0 | int wi = (irun = fixed2int_var_rounded(xl)) - xi; |
452 | |
|
453 | 0 | if (wi < 0) |
454 | 0 | xi += wi, wi = -wi; |
455 | 0 | code = gx_fill_rectangle_device_rop(xi, yt, |
456 | 0 | wi, iht, pdevc, dev, lop); |
457 | 0 | } |
458 | 0 | if (code < 0) |
459 | 0 | goto err; |
460 | 0 | rsrc = psrc; |
461 | 0 | if ((code = mcode) < 0) |
462 | 0 | goto err; |
463 | 0 | ptemp = pdevc; |
464 | 0 | pdevc = pdevc_next; |
465 | 0 | pdevc_next = ptemp; |
466 | 0 | xrun = xl; |
467 | 0 | yrun = ytf; |
468 | 0 | } |
469 | 0 | run = next; |
470 | 0 | inc: |
471 | 0 | dda_next_assign(pnext.x, xl); |
472 | 0 | dda_next_assign(pnext.y, ytf); |
473 | 0 | } |
474 | | /* Fill the final run. */ |
475 | 0 | if (posture != image_portrait) { |
476 | 0 | code = (*dev_proc(dev, fill_parallelogram)) |
477 | 0 | (dev, xrun, yrun, xl - xrun, ytf - yrun, pdyx, pdyy, pdevc, lop); |
478 | 0 | } else { |
479 | | /* Same code as above near 'fill:' : */ |
480 | 0 | int xi = irun; |
481 | 0 | int wi = (irun = fixed2int_var_rounded(xl)) - xi; |
482 | |
|
483 | 0 | if (wi < 0) |
484 | 0 | xi += wi, wi = -wi; |
485 | 0 | code = gx_fill_rectangle_device_rop(xi, yt, |
486 | 0 | wi, iht, pdevc, dev, lop); |
487 | 0 | } |
488 | 0 | return (code < 0 ? code : 1); |
489 | | |
490 | | /* Save position if error, in case we resume. */ |
491 | 0 | err: |
492 | 0 | penum->used.x = (rsrc - spp - psrc_initial) / spp; |
493 | 0 | penum->used.y = 0; |
494 | 0 | return code; |
495 | 0 | } |
496 | | |
497 | | static inline float |
498 | | rescale_input_color(gs_range range, float input) |
499 | 0 | { |
500 | 0 | return((input-range.rmin)/(range.rmax-range.rmin)); |
501 | 0 | } |
502 | | |
503 | | /* This one includes an extra adjustment for the CIE PS color space |
504 | | non standard range */ |
505 | | static void |
506 | | decode_row_cie16(const gx_image_enum *penum, const unsigned short *psrc, |
507 | | int spp, unsigned short *pdes, |
508 | | const unsigned short *bufend, gs_range range_array[]) |
509 | 0 | { |
510 | 0 | unsigned short *curr_pos = pdes; |
511 | 0 | int k; |
512 | 0 | float temp; |
513 | |
|
514 | 0 | while ( curr_pos < bufend ) { |
515 | 0 | for ( k = 0; k < spp; k ++ ) { |
516 | 0 | switch ( penum->map[k].decoding ) { |
517 | 0 | case sd_none: |
518 | 0 | *curr_pos = *psrc; |
519 | 0 | break; |
520 | 0 | case sd_lookup: |
521 | 0 | temp = penum->map[k].decode_lookup[(*psrc) >> 4]*65535.0; |
522 | 0 | temp = rescale_input_color(range_array[k], temp); |
523 | 0 | temp = temp*65535; |
524 | 0 | if (temp > 65535.0) temp = 65535.0; |
525 | 0 | if (temp < 0 ) temp = 0; |
526 | 0 | *curr_pos = (unsigned short) temp; |
527 | 0 | break; |
528 | 0 | case sd_compute: |
529 | 0 | temp = penum->map[k].decode_base + |
530 | 0 | (*psrc) * penum->map[k].decode_factor; |
531 | 0 | temp = rescale_input_color(range_array[k], temp); |
532 | 0 | temp = temp*65535; |
533 | 0 | if (temp > 65535) temp = 65535; |
534 | 0 | if (temp < 0 ) temp = 0; |
535 | 0 | *curr_pos = (unsigned short) temp; |
536 | 0 | default: |
537 | 0 | break; |
538 | 0 | } |
539 | 0 | curr_pos++; |
540 | 0 | psrc++; |
541 | 0 | } |
542 | 0 | } |
543 | 0 | } |
544 | | |
545 | | static void |
546 | | decode_row16(const gx_image_enum *penum, const unsigned short *psrc, int spp, |
547 | | unsigned short *pdes,const unsigned short *bufend) |
548 | 0 | { |
549 | 0 | unsigned short *curr_pos = pdes; |
550 | 0 | int k; |
551 | 0 | float temp; |
552 | |
|
553 | 0 | while ( curr_pos < bufend ) { |
554 | 0 | for ( k = 0; k < spp; k ++ ) { |
555 | 0 | switch ( penum->map[k].decoding ) { |
556 | 0 | case sd_none: |
557 | 0 | *curr_pos = *psrc; |
558 | 0 | break; |
559 | 0 | case sd_lookup: |
560 | 0 | temp = penum->map[k].decode_lookup[(*psrc) >> 4]*65535; |
561 | 0 | if (temp > 65535) temp = 65535; |
562 | 0 | if (temp < 0 ) temp = 0; |
563 | 0 | *curr_pos = (unsigned short) temp; |
564 | 0 | break; |
565 | 0 | case sd_compute: |
566 | 0 | temp = penum->map[k].decode_base + |
567 | 0 | (*psrc) * penum->map[k].decode_factor; |
568 | 0 | temp *= 65535; |
569 | 0 | if (temp > 65535) temp = 65535; |
570 | 0 | if (temp < 0 ) temp = 0; |
571 | 0 | *curr_pos = (unsigned short) temp; |
572 | 0 | default: |
573 | 0 | break; |
574 | 0 | } |
575 | 0 | curr_pos++; |
576 | 0 | psrc++; |
577 | 0 | } |
578 | 0 | } |
579 | 0 | } |
580 | | |
581 | | /* Render an image with more than 8 bits per sample where we keep the data |
582 | | in 16 bit form and hand directly to the CMM */ |
583 | | static int |
584 | | image_render_icc16(gx_image_enum * penum, const byte * buffer, int data_x, |
585 | | uint w, int h, gx_device * dev) |
586 | 0 | { |
587 | 0 | const gs_gstate *pgs = penum->pgs; |
588 | 0 | gs_logical_operation_t lop = penum->log_op; |
589 | 0 | gx_dda_fixed_point pnext; |
590 | 0 | image_posture posture = penum->posture; |
591 | 0 | fixed pdyx, pdyy; /* edge of parallelogram */ |
592 | 0 | int vci, vdi; |
593 | 0 | int spp = penum->spp; |
594 | 0 | const unsigned short *psrc = (const unsigned short *)buffer + data_x * spp; |
595 | 0 | fixed xrun; /* x at start of run */ |
596 | 0 | int irun; /* int xrun */ |
597 | 0 | fixed yrun; /* y ditto */ |
598 | 0 | const unsigned short *bufend = psrc + w; |
599 | 0 | unsigned short *run; |
600 | 0 | int code = 0; |
601 | 0 | gsicc_bufferdesc_t input_buff_desc; |
602 | 0 | gsicc_bufferdesc_t output_buff_desc; |
603 | 0 | unsigned short *psrc_cm, *psrc_cm_start, *psrc_decode, *psrc_cm_initial; |
604 | 0 | int k; |
605 | 0 | int spp_cm, num_pixels; |
606 | 0 | bool need_decode = penum->icc_setup.need_decode; |
607 | 0 | bool must_halftone = penum->icc_setup.must_halftone; |
608 | 0 | bool has_transfer = penum->icc_setup.has_transfer; |
609 | 0 | int num_des_comps; |
610 | 0 | cmm_dev_profile_t *dev_profile; |
611 | 0 | gx_cmapper_t data; |
612 | 0 | gx_cmapper_fn *mapper; |
613 | 0 | gx_color_value *conc = &data.conc[0]; |
614 | 0 | int first = 1; |
615 | |
|
616 | 0 | if (h == 0) |
617 | 0 | return 0; |
618 | | |
619 | 0 | if (penum->icc_link == NULL) { |
620 | 0 | return gs_rethrow(-1, "ICC Link not created during image render icc16"); |
621 | 0 | } |
622 | 0 | gx_get_cmapper(&data, pgs, dev, has_transfer, must_halftone, gs_color_select_source); |
623 | 0 | mapper = data.set_color; |
624 | | /* Needed for device N */ |
625 | 0 | code = dev_proc(dev, get_profile)(dev, &dev_profile); |
626 | 0 | num_des_comps = gsicc_get_device_profile_comps(dev_profile); |
627 | | /* If the link is the identity, then we don't need to do any color |
628 | | conversions except for potentially a decode. */ |
629 | 0 | if (penum->icc_link->is_identity && !need_decode) { |
630 | | /* Fastest case. No decode or CM needed */ |
631 | 0 | psrc_cm = (unsigned short *) psrc; |
632 | 0 | spp_cm = spp; |
633 | 0 | bufend = psrc_cm + w; |
634 | 0 | psrc_cm_start = NULL; |
635 | 0 | } else { |
636 | 0 | spp_cm = num_des_comps; |
637 | 0 | psrc_cm = (unsigned short*) gs_alloc_bytes(pgs->memory, |
638 | 0 | sizeof(unsigned short) * w * spp_cm/spp, |
639 | 0 | "image_render_icc16"); |
640 | 0 | if (psrc_cm == NULL) |
641 | 0 | return_error(gs_error_VMerror); |
642 | | |
643 | 0 | psrc_cm_start = psrc_cm; |
644 | 0 | bufend = psrc_cm + w * spp_cm/spp; |
645 | 0 | if (penum->icc_link->is_identity) { |
646 | | /* decode only. no CM. This is slow but does not happen that often */ |
647 | 0 | decode_row16(penum, psrc, spp, psrc_cm, bufend); |
648 | 0 | } else { |
649 | | /* Set up the buffer descriptors. */ |
650 | 0 | num_pixels = w/spp; |
651 | 0 | gsicc_init_buffer(&input_buff_desc, spp, 2, |
652 | 0 | false, false, false, 0, w * 2, |
653 | 0 | 1, num_pixels); |
654 | 0 | gsicc_init_buffer(&output_buff_desc, spp_cm, 2, |
655 | 0 | false, false, false, 0, num_pixels * spp_cm * 2, |
656 | 0 | 1, num_pixels); |
657 | | /* For now, just blast it all through the link. If we had a significant reduction |
658 | | we will want to repack the data first and then do this. That will be |
659 | | an optimization shortly. For now just allocate a new output |
660 | | buffer. We can reuse the old one if the number of channels in the output is |
661 | | less than or equal to the new one. */ |
662 | 0 | if (need_decode) { |
663 | | /* Need decode and CM. This is slow but does not happen that often */ |
664 | 0 | psrc_decode = (unsigned short*) gs_alloc_bytes(pgs->memory, |
665 | 0 | sizeof(unsigned short) * w * spp, |
666 | 0 | "image_render_icc16"); |
667 | 0 | if (psrc_decode == NULL) { |
668 | 0 | gs_free_object(pgs->memory, (byte *)psrc_cm_start, "image_render_icc16"); |
669 | 0 | return_error(gs_error_VMerror); |
670 | 0 | } |
671 | 0 | if (!penum->use_cie_range) { |
672 | 0 | decode_row16(penum, psrc, spp, psrc_decode, |
673 | 0 | (const unsigned short*) (psrc_decode+w)); |
674 | 0 | } else { |
675 | | /* Decode needs to include adjustment for CIE range */ |
676 | 0 | decode_row_cie16(penum, psrc, spp, psrc_decode, |
677 | 0 | (const unsigned short*) (psrc_decode+w), |
678 | 0 | get_cie_range(penum->pcs)); |
679 | 0 | } |
680 | 0 | code = (penum->icc_link->procs.map_buffer)(dev, penum->icc_link, |
681 | 0 | &input_buff_desc, |
682 | 0 | &output_buff_desc, |
683 | 0 | (void*) psrc_decode, |
684 | 0 | (void*) psrc_cm); |
685 | 0 | gs_free_object(pgs->memory, (byte *)psrc_decode, "image_render_color_icc"); |
686 | 0 | if (code < 0) |
687 | 0 | return code; |
688 | 0 | } else { |
689 | | /* CM only. No decode */ |
690 | 0 | code = (penum->icc_link->procs.map_buffer)(dev, penum->icc_link, |
691 | 0 | &input_buff_desc, |
692 | 0 | &output_buff_desc, |
693 | 0 | (void*) psrc, |
694 | 0 | (void*) psrc_cm); |
695 | 0 | if (code < 0) |
696 | 0 | return code; |
697 | 0 | } |
698 | 0 | } |
699 | 0 | } |
700 | 0 | psrc_cm_initial = psrc_cm; |
701 | |
|
702 | 0 | pnext = penum->dda.pixel0; |
703 | 0 | xrun = dda_current(pnext.x); |
704 | 0 | yrun = dda_current(pnext.y); |
705 | 0 | pdyx = dda_current(penum->dda.row.x) - penum->cur.x; |
706 | 0 | pdyy = dda_current(penum->dda.row.y) - penum->cur.y; |
707 | 0 | switch (posture) { |
708 | 0 | case image_portrait: |
709 | 0 | vci = penum->yci, vdi = penum->hci; |
710 | 0 | irun = fixed2int_var_rounded(xrun); |
711 | 0 | break; |
712 | 0 | case image_landscape: |
713 | 0 | default: /* we don't handle skew -- treat as landscape */ |
714 | 0 | vci = penum->xci, vdi = penum->wci; |
715 | 0 | irun = fixed2int_var_rounded(yrun); |
716 | 0 | break; |
717 | 0 | } |
718 | 0 | if_debug5m('b', penum->memory, "[b]y=%d data_x=%d w=%d xt=%f yt=%f\n", |
719 | 0 | penum->y, data_x, w, fixed2float(xrun), fixed2float(yrun)); |
720 | 0 | while (psrc_cm < bufend) { |
721 | | /* Find the length of the next run. It will either end when we hit |
722 | | * the end of the source data, or when the pixel data differs. */ |
723 | 0 | run = psrc_cm + spp_cm; |
724 | 0 | while (1) |
725 | 0 | { |
726 | 0 | dda_next(pnext.x); |
727 | 0 | dda_next(pnext.y); |
728 | 0 | if (run >= bufend) |
729 | 0 | break; |
730 | 0 | if (memcmp(run, psrc_cm, spp_cm * 2)) |
731 | 0 | break; |
732 | 0 | if (posture == image_skewed) { |
733 | 0 | if (first) /* We always need to map the first pixel on a line */ |
734 | 0 | break; |
735 | | /* The color doesn't need remapping, but we can't handle a run */ |
736 | 0 | goto skewed_but_same; |
737 | 0 | } |
738 | 0 | run += spp_cm; |
739 | 0 | } |
740 | 0 | first = 0; |
741 | | /* So we have a run of pixels from psrc_cm to run that are all the same. */ |
742 | | /* This needs to be sped up */ |
743 | 0 | for ( k = 0; k < spp_cm; k++ ) { |
744 | 0 | conc[k] = psrc_cm[k]; |
745 | 0 | } |
746 | 0 | mapper(&data); |
747 | | /* Fill the region between */ |
748 | | /* xrun/irun and pnext.x/pnext.y */ |
749 | 0 | switch(posture) |
750 | 0 | { |
751 | 0 | default: /* skew */ |
752 | 0 | skewed_but_same: |
753 | 0 | { |
754 | 0 | fixed xprev = dda_current(pnext.x); |
755 | 0 | fixed yprev = dda_current(pnext.y); |
756 | 0 | code = (*dev_proc(dev, fill_parallelogram)) |
757 | 0 | (dev, xrun, yrun, xprev - xrun, yprev - yrun, pdyx, pdyy, |
758 | 0 | &data.devc, lop); |
759 | 0 | xrun = xprev; |
760 | 0 | yrun = yprev; |
761 | 0 | break; |
762 | 0 | } |
763 | 0 | case image_portrait: |
764 | 0 | { |
765 | 0 | int xi = irun; |
766 | 0 | int wi = (irun = fixed2int_var_rounded(dda_current(pnext.x))) - xi; |
767 | |
|
768 | 0 | if (wi < 0) |
769 | 0 | xi += wi, wi = -wi; |
770 | 0 | if (wi > 0) |
771 | 0 | code = gx_fill_rectangle_device_rop(xi, vci, wi, vdi, |
772 | 0 | &data.devc, dev, lop); |
773 | 0 | break; |
774 | 0 | } |
775 | 0 | case image_landscape: |
776 | 0 | { |
777 | 0 | int yi = irun; |
778 | 0 | int hi = (irun = fixed2int_var_rounded(dda_current(pnext.y))) - yi; |
779 | |
|
780 | 0 | if (hi < 0) |
781 | 0 | yi += hi, hi = -hi; |
782 | 0 | if (hi > 0) |
783 | 0 | code = gx_fill_rectangle_device_rop(vci, yi, vdi, hi, |
784 | 0 | &data.devc, dev, lop); |
785 | 0 | } |
786 | 0 | } |
787 | 0 | if (code < 0) |
788 | 0 | goto err; |
789 | 0 | psrc_cm = run; |
790 | 0 | } |
791 | | /* Free cm buffer, if it was used */ |
792 | 0 | if (psrc_cm_start != NULL) { |
793 | 0 | gs_free_object(pgs->memory, (byte *)psrc_cm_start, "image_render_icc16"); |
794 | 0 | } |
795 | 0 | return (code < 0 ? code : 1); |
796 | | |
797 | | /* Save position if error, in case we resume. */ |
798 | 0 | err: |
799 | 0 | gs_free_object(pgs->memory, (byte *)psrc_cm_start, "image_render_icc16"); |
800 | 0 | penum->used.x = (psrc_cm - psrc_cm_initial) / spp_cm; |
801 | 0 | penum->used.y = 0; |
802 | 0 | return code; |
803 | 0 | } |