Coverage Report

Created: 2026-07-24 07:44

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