Coverage Report

Created: 2022-10-31 07:00

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