Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/base/gxiscale.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
/* Interpolated image procedures */
18
#include "gx.h"
19
#include "math_.h"
20
#include "memory_.h"
21
#include "stdint_.h"
22
#include "gpcheck.h"
23
#include "gserrors.h"
24
#include "gxfixed.h"
25
#include "gxfrac.h"
26
#include "gxarith.h"
27
#include "gxmatrix.h"
28
#include "gsccolor.h"
29
#include "gspaint.h"
30
#include "gxdevice.h"
31
#include "gxcmap.h"
32
#include "gxdcolor.h"
33
#include "gxgstate.h"
34
#include "gxdevmem.h"
35
#include "gxcpath.h"
36
#include "gximage.h"
37
#include "stream.h"             /* for s_alloc_state */
38
#include "siinterp.h"           /* for spatial interpolation */
39
#include "siscale.h"            /* for Mitchell filtering */
40
#include "sidscale.h"           /* for special case downscale filter */
41
#include "gscindex.h"           /* included for proper handling of index color spaces
42
                                and keeping data in source color space */
43
#include "gsbitops.h"
44
#include "gxcolor2.h"           /* define of float_color_to_byte_color */
45
#include "gscspace.h"           /* Needed for checking is space is CIE */
46
#include "gsicc_cache.h"
47
#include "gsicc_manage.h"
48
#include "gsicc.h"
49
#include "gxdevsop.h"
50
#include <limits.h>             /* For INT_MAX */
51
52
static void
53
decode_sample_frac_to_float(gx_image_enum *penum, frac sample_value, gs_client_color *cc, int i);
54
55
/*
56
 * Define whether we are using Mitchell filtering or spatial
57
 * interpolation to implement Interpolate.  (The latter doesn't work yet.)
58
 */
59
#define USE_MITCHELL_FILTER
60
61
/* ------ Strategy procedure ------ */
62
63
/* Check the prototype. */
64
iclass_proc(gs_image_class_0_interpolate);
65
66
/* If we're interpolating, use special logic.
67
   This function just gets interpolation stucture
68
   initialized and allocates buffer space if needed */
69
static irender_proc(image_render_interpolate);
70
static irender_proc(image_render_interpolate_icc);
71
static irender_proc(image_render_interpolate_masked);
72
static irender_proc(image_render_interpolate_masked_hl);
73
static irender_proc(image_render_interpolate_landscape);
74
static irender_proc(image_render_interpolate_landscape_icc);
75
static irender_proc(image_render_interpolate_landscape_masked);
76
static irender_proc(image_render_interpolate_landscape_masked_hl);
77
78
#if 0 /* Unused now, but potentially useful in the future */
79
static bool
80
is_high_level_device(gx_device *dev)
81
{
82
    char data[] = "HighLevelDevice";
83
    dev_param_req_t request;
84
    gs_c_param_list list;
85
    int highlevel = 0;
86
    int code;
87
88
    gs_c_param_list_write(&list, dev->memory);
89
    /* Stuff the data into a structure for passing to the spec_op */
90
    request.Param = data;
91
    request.list = &list;
92
    code = dev_proc(dev, dev_spec_op)(dev, gxdso_get_dev_param, &request, sizeof(dev_param_req_t));
93
    if (code < 0 && code != gs_error_undefined) {
94
        gs_c_param_list_release(&list);
95
        return 0;
96
    }
97
    gs_c_param_list_read(&list);
98
    code = param_read_bool((gs_param_list *)&list,
99
            "HighLevelDevice",
100
            &highlevel);
101
    gs_c_param_list_release(&list);
102
    if (code < 0)
103
        return 0;
104
105
    return highlevel;
106
}
107
#endif
108
109
static bool
110
device_allows_imagemask_interpolation(gx_device *dev)
111
38
{
112
38
    char data[] = "NoInterpolateImagemasks";
113
38
    dev_param_req_t request;
114
38
    gs_c_param_list list;
115
38
    bool nointerpolate = false;
116
38
    int code;
117
118
38
    gs_c_param_list_write(&list, dev->memory);
119
    /* Stuff the data into a structure for passing to the spec_op */
120
38
    request.Param = data;
121
38
    request.list = &list;
122
38
    code = dev_proc(dev, dev_spec_op)(dev, gxdso_get_dev_param, &request, sizeof(dev_param_req_t));
123
38
    if (code < 0 && code != gs_error_undefined) {
124
0
        gs_c_param_list_release(&list);
125
0
        return 0;
126
0
    }
127
38
    gs_c_param_list_read(&list);
128
38
    code = param_read_bool((gs_param_list *)&list,
129
38
            "NoInterpolateImagemasks",
130
38
            &nointerpolate);
131
38
    gs_c_param_list_release(&list);
132
38
    if (code < 0)
133
0
        return 0;
134
135
38
    return !nointerpolate;
136
38
}
137
138
#define DC_IS_NULL(pdc)\
139
  (gx_dc_is_pure(pdc) && (pdc)->colors.pure == gx_no_color_index)
140
141
/* Returns < 0 for error, 0 for pure color, 1 for high level */
142
static int mask_suitable_for_interpolation(gx_image_enum *penum)
143
38
{
144
38
    gx_device_color * const pdc1 = penum->icolor1;
145
38
    int code;
146
38
    int high_level_color = 1;
147
148
38
    if (gx_device_must_halftone(penum->dev))
149
        /* We don't interpolate when going to 1bpp outputs */
150
0
        return -1;
151
38
    if (dev_proc(penum->dev, dev_spec_op)(penum->dev, gxdso_copy_alpha_disabled, NULL, 0) == 1)
152
        /* The target device has copy_alpha() disabled. */
153
0
        return -1;
154
38
    if (gx_dc_is_pure(pdc1) && (pdc1)->colors.pure != gx_no_color_index &&
155
38
        dev_proc(penum->dev, copy_alpha) != NULL &&
156
38
        dev_proc(penum->dev, copy_alpha) != gx_no_copy_alpha) {
157
        /* We have a 'pure' color, and a valid copy_alpha. We can work with that. */
158
38
        high_level_color = 0;
159
38
    } else if (dev_proc(penum->dev, copy_alpha_hl_color) == gx_default_no_copy_alpha_hl_color) {
160
        /* No copy_alpha_hl_color. We're out of luck. */
161
0
        return -1;
162
0
    } else if ((code = gx_color_load(pdc1, penum->pgs, penum->dev)) < 0) {
163
        /* Otherwise we'll need to load the color value. If this gives an
164
         * error, we can't cope. */
165
0
        return -1;
166
0
    } else if (!gx_dc_is_devn(pdc1)) {
167
        /* If it's not a devn color, then we're really out of luck. */
168
0
        return -1;
169
0
    }
170
171
    /* Never turn this on for devices that disallow it (primarily
172
     * high level devices) */
173
38
    if (!device_allows_imagemask_interpolation(penum->dev))
174
38
        return -1;
175
176
0
    return high_level_color;
177
38
}
178
179
/*
180
    Helper function to take an int64_t, and "fixed2int_pixround_perfect"
181
    it while testing for overflow.
182
*/
183
static int
184
safe64fixed2int(int64_t v, int *overflow)
185
28
{
186
28
    if (v > (int)((1U<<(sizeof(int)*8-1))-128) ||
187
28
        v < 0)
188
0
        *overflow = 1;
189
28
    return fixed2int_pixround_perfect(v);
190
28
}
191
192
int
193
gs_image_class_0_interpolate(gx_image_enum * penum, irender_proc_t *render_fn)
194
2.28M
{
195
2.28M
    gs_memory_t *mem = penum->memory;
196
2.28M
    stream_image_scale_params_t iss;
197
2.28M
    stream_image_scale_state *pss;
198
2.28M
    const stream_template *templat;
199
2.28M
    byte *line;
200
2.28M
    const gs_color_space *pcs = penum->pcs;
201
2.28M
    uint in_size;
202
2.28M
    bool use_icc = false;
203
2.28M
    int num_des_comps;
204
2.28M
    cmm_dev_profile_t *dev_profile;
205
2.28M
    int code;
206
2.28M
    gx_color_polarity_t pol = GX_CINFO_POLARITY_UNKNOWN;
207
2.28M
    int mask_col_high_level = 0;
208
2.28M
    int interpolate_control = penum->dev->interpolate_control;
209
2.28M
    int abs_interp_limit = max(1, any_abs(interpolate_control));
210
2.28M
    int limited_WidthOut, limited_HeightOut;
211
2.28M
    int overflow = 0;
212
213
2.28M
    if (interpolate_control < 0)
214
97
        penum->interpolate = interp_on;   /* not the same as "interp_force" -- threshold still used */
215
2.28M
    if (interpolate_control == interp_off || penum->interpolate == interp_off) {
216
2.28M
        penum->interpolate = interp_off;
217
2.28M
        return 0;
218
2.28M
    }
219
97
    if (penum->masked && (mask_col_high_level = mask_suitable_for_interpolation(penum)) < 0) {
220
38
        penum->interpolate = interp_off;
221
38
        return 0;
222
38
    }
223
59
    if (penum->use_mask_color ||
224
59
        (penum->posture != image_portrait &&
225
59
         penum->posture != image_landscape) ||
226
59
        penum->alpha) {
227
        /* We can't handle these cases yet.  Punt. */
228
50
        penum->interpolate = interp_off;
229
50
        return 0;
230
50
    }
231
9
    if (penum->Width == 0 || penum->Height == 0) {
232
0
        penum->interpolate = interp_off; /* No need to interpolate and      */
233
0
        return 0;                  /* causes division by 0 if we try. */
234
0
    }
235
9
    if (penum->Width == 1 && penum->Height == 1) {
236
0
        penum->interpolate = interp_off; /* No need to interpolate */
237
0
        return 0;
238
0
    }
239
9
    if (any_abs(penum->dst_width) < 0 || any_abs(penum->dst_height) < 0)
240
0
    {
241
        /* A calculation has overflowed. Bale */
242
0
        return 0;
243
0
    }
244
9
    if (penum->x_extent.x == INT_MIN || penum->x_extent.x == INT_MAX ||
245
9
        penum->x_extent.y == INT_MIN || penum->x_extent.y == INT_MAX ||
246
9
        penum->y_extent.x == INT_MIN || penum->y_extent.x == INT_MAX ||
247
9
        penum->y_extent.y == INT_MIN || penum->y_extent.y == INT_MAX)
248
7
    {
249
        /* A calculation has overflowed. Bail */
250
7
        return 0;
251
7
    }
252
253
2
    if (penum->masked) {
254
0
        abs_interp_limit = 1;   /* ignore this for masked images for now */
255
0
        use_icc = false;
256
0
        num_des_comps = 1;
257
0
        if (pcs)
258
0
            pol = cs_polarity(pcs);
259
0
        else
260
0
            pol = GX_CINFO_POLARITY_ADDITIVE;
261
2
    } else {
262
2
        if (pcs == NULL)
263
0
            return 0;   /* can't handle this */
264
2
        if (pcs->cmm_icc_profile_data != NULL) {
265
2
            use_icc = true;
266
2
        }
267
2
        if (pcs->type->index == gs_color_space_index_Indexed) {
268
0
            if (pcs->base_space->cmm_icc_profile_data != NULL) {
269
0
                use_icc = true;
270
0
            }
271
0
        }
272
2
        if (!(penum->bps <= 8 || penum->bps == 16)) {
273
0
            use_icc = false;
274
0
        }
275
        /* Do not allow mismatch in devices component output with the
276
           profile output size.  For example sep device with CMYK profile should
277
           not go through the fast method */
278
2
        code = dev_proc(penum->dev, get_profile)(penum->dev, &dev_profile);
279
2
        if (code) {
280
0
            penum->interpolate = interp_off;
281
0
            return 0;
282
0
        }
283
2
        num_des_comps = gsicc_get_device_profile_comps(dev_profile);
284
2
        if (num_des_comps != penum->dev->color_info.num_components ||
285
2
            dev_profile->usefastcolor == true) {
286
0
            use_icc = false;
287
0
        }
288
        /* If the device has some unique color mapping procs due to its color space,
289
           then we will need to use those and go through pixel by pixel instead
290
           of blasting through buffers.  This is true for example with many of
291
           the color spaces for CUPs */
292
2
        if(!gx_device_uses_std_cmap_procs(penum->dev, penum->pgs)) {
293
0
            use_icc = false;
294
0
        }
295
2
        pol = cs_polarity(pcs);
296
2
    }
297
/*
298
 * USE_CONSERVATIVE_INTERPOLATION_RULES is normally NOT defined since
299
 * the MITCHELL digital filter seems OK as long as we are going out to
300
 * a device that can produce > 15 shades.
301
 */
302
#if defined(USE_MITCHELL_FILTER) && defined(USE_CONSERVATIVE_INTERPOLATION_RULES)
303
    /*
304
     * We interpolate using a digital filter, rather than Adobe's
305
     * spatial interpolation algorithm: this produces very bad-looking
306
     * results if the input resolution is close to the output resolution,
307
     * especially if the input has low color resolution, so we resort to
308
     * some hack tests on the input color resolution and scale to suppress
309
     * interpolation if we think the result would look especially bad.
310
     * If we used Adobe's spatial interpolation approach, we wouldn't need
311
     * to do this, but the spatial interpolation filter doesn't work yet.
312
     */
313
    if (penum->bps < 4 || penum->bps * penum->spp < 8 ||
314
        (fabs(penum->matrix.xx) <= 5 && fabs(penum->matrix.yy <= 5))
315
        ) {
316
        penum->interpolate = interp_off;
317
        return 0;
318
    }
319
#endif
320
2
    iss.abs_interp_limit = abs_interp_limit;
321
2
    if (penum->masked) {
322
0
        iss.BitsPerComponentOut = 8;
323
0
        iss.MaxValueOut = 0xff;
324
2
    } else if (use_icc) {
325
2
        iss.BitsPerComponentOut = 16;
326
2
        iss.MaxValueOut = 0xffff;
327
2
    } else {
328
0
        iss.BitsPerComponentOut = sizeof(frac) * 8;
329
0
        iss.MaxValueOut = frac_1;
330
0
    }
331
2
    iss.PatchWidthIn = penum->drect.w;
332
2
    iss.PatchHeightIn = penum->drect.h;
333
2
    iss.LeftMarginIn = penum->drect.x - penum->rect.x;
334
2
    iss.TopMarginIn = penum->drect.y - penum->rect.y;
335
2
    if (penum->posture == image_portrait) {
336
2
        fixed dw = any_abs(penum->dst_width);
337
2
        fixed dh = any_abs(penum->dst_height);
338
2
        iss.WidthOut = safe64fixed2int((int64_t)(penum->rect.x + penum->rect.w) *
339
2
                                       dw / penum->Width, &overflow)
340
2
                     - safe64fixed2int((int64_t)penum->rect.x *
341
2
                                       dw / penum->Width, &overflow);
342
2
        iss.HeightOut = safe64fixed2int((int64_t)(penum->rect.y + penum->rect.h) *
343
2
                                        dh / penum->Height, &overflow)
344
2
                      - safe64fixed2int((int64_t)penum->rect.y *
345
2
                                        dh / penum->Height, &overflow);
346
2
        iss.EntireWidthOut = safe64fixed2int(dw, &overflow);
347
2
        iss.EntireHeightOut = safe64fixed2int(dh, &overflow);
348
2
        iss.TopMarginOut = safe64fixed2int((int64_t)(penum->rrect.y - penum->rect.y) *
349
2
                                           dh / penum->Height, &overflow);
350
2
        iss.PatchHeightOut = safe64fixed2int((int64_t)penum->rrect.h *
351
2
                                             dh / penum->Height, &overflow)
352
2
                           - iss.TopMarginOut;
353
2
        iss.PatchWidthOut = safe64fixed2int((int64_t)(penum->rrect.x + penum->rrect.w) *
354
2
                                            dw / penum->Width, &overflow)
355
2
                          - safe64fixed2int((int64_t)penum->rrect.x *
356
2
                                            dw / penum->Width, &overflow);
357
2
        iss.LeftMarginOut = safe64fixed2int((int64_t)(penum->rrect.x - penum->rect.x) *
358
2
                                            dw / penum->Width, &overflow);
359
2
        iss.TopMarginOut2 = safe64fixed2int((int64_t)penum->rrect.y *
360
2
                                            dh / penum->Height, &overflow);
361
2
        iss.PatchHeightOut2 = safe64fixed2int((int64_t)(penum->rrect.y + penum->rrect.h) *
362
2
                                              dh / penum->Height, &overflow)
363
2
                           - iss.TopMarginOut2;
364
2
        iss.pad_y = iss.TopMarginOut2
365
2
                  - safe64fixed2int((int64_t)penum->rect.y *
366
2
                                    dh / penum->Height, &overflow);
367
2
    } else {
368
0
        fixed dw = any_abs(penum->dst_width);
369
0
        fixed dh = any_abs(penum->dst_height);
370
0
        iss.WidthOut = safe64fixed2int((int64_t)(penum->rect.x + penum->rect.w) *
371
0
                                       dh / penum->Width, &overflow)
372
0
                     - safe64fixed2int((int64_t)penum->rect.x *
373
0
                                       dh / penum->Width, &overflow);
374
0
        iss.HeightOut = safe64fixed2int((int64_t)(penum->rect.y + penum->rect.h) *
375
0
                                        dw / penum->Height, &overflow)
376
0
                      - safe64fixed2int((int64_t)penum->rect.y *
377
0
                                        dw / penum->Height, &overflow);
378
0
        iss.EntireWidthOut = safe64fixed2int(dh, &overflow);
379
0
        iss.EntireHeightOut = safe64fixed2int(dw, &overflow);
380
0
        iss.TopMarginOut = safe64fixed2int((int64_t)(penum->rrect.y - penum->rect.y) *
381
0
                                           dw / penum->Height, &overflow);
382
0
        iss.PatchHeightOut = safe64fixed2int((int64_t)penum->rrect.h *
383
0
                                             dw / penum->Height, &overflow)
384
0
                           - iss.TopMarginOut;
385
0
        iss.PatchWidthOut = safe64fixed2int((int64_t)(penum->rrect.x + penum->rrect.w) *
386
0
                                            dh / penum->Width, &overflow)
387
0
                          - safe64fixed2int((int64_t)penum->rrect.x *
388
0
                                            dh / penum->Width, &overflow);
389
0
        iss.LeftMarginOut = safe64fixed2int((int64_t)(penum->rrect.x - penum->rect.x) *
390
0
                                            dh / penum->Width, &overflow);
391
0
        iss.TopMarginOut2 = safe64fixed2int((int64_t)penum->rect.y *
392
0
                                            dw / penum->Height, &overflow);
393
0
        iss.PatchHeightOut2 = safe64fixed2int((int64_t)(penum->rrect.y + penum->rrect.h) *
394
0
                                              dw / penum->Height, &overflow)
395
0
                           - iss.TopMarginOut2;
396
0
        iss.pad_y = 0;
397
0
    }
398
2
    iss.PatchWidthOut = any_abs(iss.PatchWidthOut);
399
2
    if (iss.LeftMarginOut + iss.PatchWidthOut >= iss.WidthOut) {
400
2
        iss.LeftMarginOut = iss.WidthOut - iss.PatchWidthOut;
401
2
        if (iss.LeftMarginOut < 0) {
402
0
            iss.WidthOut += iss.LeftMarginOut;
403
0
            iss.LeftMarginOut = 0;
404
0
        }
405
2
    }
406
2
    iss.src_y_offset = penum->rect.y;
407
2
    iss.EntireWidthIn = penum->Width;
408
2
    iss.EntireHeightIn = penum->Height;
409
2
    iss.WidthIn = penum->rect.w;
410
2
    iss.HeightIn = penum->rect.h;
411
2
    iss.WidthOut = any_abs(iss.WidthOut);
412
2
    iss.HeightOut = any_abs(iss.HeightOut);
413
2
    limited_WidthOut = (iss.WidthOut + abs_interp_limit - 1)/abs_interp_limit;
414
2
    limited_HeightOut = (iss.HeightOut + abs_interp_limit - 1)/abs_interp_limit;
415
2
    if ((penum->posture == image_portrait ? penum->dst_width : penum->dst_height) < 0)
416
0
        iss.LeftMarginOut = iss.WidthOut - iss.LeftMarginOut - iss.PatchWidthOut;
417
    /* For interpolator cores that don't set Active, have us always active */
418
2
    iss.Active = 1;
419
2
    if (iss.EntireWidthOut == 0 || iss.EntireHeightOut == 0 || overflow)
420
2
    {
421
2
        penum->interpolate = interp_off;
422
2
        return 0;
423
2
    }
424
0
    if (penum->masked) {
425
0
        iss.spp_decode = 1;
426
0
    } else {
427
        /* If we are in an indexed space then we need to use the number of components
428
           in the base space.  Otherwise we use the number of components in the source space */
429
0
        if (pcs->type->index == gs_color_space_index_Indexed) {
430
           /* Use the number of colors in the base space */
431
0
            iss.spp_decode = cs_num_components(pcs->base_space);
432
0
        } else {
433
            /* Use the number of colors that exist in the source space
434
            as this is where we are doing our interpolation */
435
0
            iss.spp_decode = cs_num_components(pcs);
436
0
        }
437
0
    }
438
    /* Set up the filter template. May be changed for "Special" downscaling */
439
0
#ifdef USE_MITCHELL_FILTER
440
0
    templat = &s_IScale_template;
441
#else
442
    templat = &s_IIEncode_template;
443
#endif
444
445
0
    if ((iss.WidthOut < iss.WidthIn) &&
446
0
        (iss.HeightOut < iss.HeightIn) &&       /* downsampling */
447
0
        (pol != GX_CINFO_POLARITY_UNKNOWN) &&
448
0
        (dev_proc(penum->dev, dev_spec_op)(penum->dev, gxdso_interpolate_antidropout, NULL, 0) > 0)) {
449
        /* Special case handling for when we are downsampling to a dithered
450
        * device.  The point of this non-linear downsampling is to preserve
451
        * dark pixels from the source image to avoid dropout. The color
452
        * polarity is used for this. */
453
0
        templat = &s_ISpecialDownScale_template;
454
0
    } else {
455
        /* No interpolation unless we exceed the device selected minimum */
456
0
        int threshold = dev_proc(penum->dev, dev_spec_op)(penum->dev, gxdso_interpolate_threshold, NULL, 0);
457
458
0
        if ((iss.WidthOut == iss.WidthIn && iss.HeightOut == iss.HeightIn) ||
459
0
            ((penum->interpolate != interp_force) &&
460
0
                (threshold > 0) &&
461
0
                (iss.WidthOut < iss.WidthIn * threshold) &&
462
0
                (iss.HeightOut < iss.HeightIn * threshold))) {
463
0
            penum->interpolate = interp_off;
464
0
            return 0;       /* don't interpolate if not scaled up enough */
465
0
        }
466
0
    }
467
    /* The SpecialDownScale filter needs polarity, either ADDITIVE or SUBTRACTIVE */
468
    /* UNKNOWN case (such as for palette colors) has been handled above */
469
0
    iss.ColorPolarityAdditive = (pol == GX_CINFO_POLARITY_ADDITIVE);
470
471
0
    if (iss.HeightOut > iss.EntireHeightIn && use_icc) {
472
0
        iss.early_cm = true;
473
0
        iss.spp_interp = num_des_comps;
474
0
    } else {
475
0
        iss.early_cm = false;
476
0
        iss.spp_interp = iss.spp_decode;
477
0
    }
478
0
    if (penum->bps <= 8 ) {
479
       /* If the input is ICC or other device independent format, go ahead
480
          and do the interpolation in that space.
481
          If we have more than 8 bits per channel then we will need to
482
          handle that in a slightly different manner so
483
          that the interpolation algorithm handles it properly.
484
          The interpolation will still be in the source
485
          color space.  Note that if image data was less the 8 bps
486
          It is handed here to us in 8 bit form already decoded. */
487
0
        iss.BitsPerComponentIn = 8;
488
0
        iss.MaxValueIn = 0xff;
489
0
        if (penum->masked) {
490
0
            in_size = iss.WidthIn * iss.spp_decode;
491
0
        } else {
492
            /* We either use the data as is, or allocate space if it is
493
               reversed in X or if the colorspace requires it */
494
            /* Do we need a buffer for reversing each scan line? */
495
0
            bool reverse = (penum->posture == image_portrait ?
496
0
                            penum->matrix.xx : penum->matrix.xy) < 0;
497
0
            in_size = (reverse ? iss.WidthIn * iss.spp_decode : 0);
498
            /* If it is not reversed, and we have 8 bit/color channel data then
499
               we may not need to allocate extra as we will use the source directly.
500
               However, if we have a nonstandard encoding and are in a device
501
               color space we will need to allocate in that case also. We will
502
               maintain 8 bits but do the decode and then interpolate.
503
               This is OK for the linear decode
504
            */
505
0
            if (!penum->device_color ||  pcs->type->index > gs_color_space_index_DeviceCMYK) {
506
0
                in_size = iss.WidthIn * iss.spp_decode;
507
0
            }
508
0
        }
509
0
    } else {
510
        /* If it has more than 8 bits per color channel then we will go to frac
511
           for the interpolation to mantain precision or 16 bit for icc  */
512
0
        if (use_icc) {
513
0
            iss.BitsPerComponentIn = 16;
514
0
            iss.MaxValueIn = 0xffff;
515
0
        } else {
516
0
            iss.BitsPerComponentIn = sizeof(frac) * 8;
517
0
            iss.MaxValueIn = frac_1;
518
0
        }
519
0
        in_size = round_up(iss.WidthIn * iss.spp_decode * (int)sizeof(frac),
520
0
                           align_bitmap_mod);
521
        /* Size to allocate space to store the input as frac type */
522
0
    }
523
    /* Allocate a buffer for one source/destination line.     */
524
    /* NB: The out_size is for full device res, regardless of abs_interp_limit  */
525
    /*     since we will expand into that area in the x-loop      */
526
0
    {
527
0
        uint out_size = iss.WidthOut * max(iss.spp_interp * ((iss.BitsPerComponentOut) / 8),
528
0
                                   ARCH_SIZEOF_COLOR_INDEX);
529
530
        /* Allocate based upon frac size (as BitsPerComponentOut=16) output scan
531
           line input plus output. The outsize may have an adjustment for
532
           word boundary on it. Need to account for that now */
533
0
        out_size += align_bitmap_mod;
534
0
        line = gs_alloc_bytes(mem, in_size + out_size,
535
0
                              "image scale src+dst line");
536
0
    }
537
0
    pss = (stream_image_scale_state *)
538
0
        s_alloc_state(mem, templat->stype, "image scale state");
539
0
    if (line == 0 || pss == 0 ||
540
0
        (pss->params = iss, pss->templat = templat,
541
0
         (*pss->templat->init) ((stream_state *) pss) < 0)
542
0
        ) {
543
0
        gs_free_object(mem, pss, "image scale state");
544
0
        gs_free_object(mem, line, "image scale src+dst line");
545
        /* Try again without interpolation. */
546
0
        penum->interpolate = interp_off;
547
0
        return 0;
548
0
    }
549
0
    penum->line = line;  /* Set to the input and output buffer */
550
0
    penum->scaler = pss;
551
0
    penum->line_xy = 0;
552
0
    if (penum->posture == image_portrait) {
553
0
        gx_dda_fixed x0;
554
0
        x0 = penum->dda.pixel0.x;
555
        /* We always plot from left to right. If the matrix would have us
556
         * plotting from right to left, then adjust to allow for the fact
557
         * we'll flip the data later. */
558
0
        if (penum->matrix.xx < 0)
559
0
            dda_advance(x0, penum->rect.w);
560
0
        penum->xyi.x = fixed2int_pixround(dda_current(x0)) + pss->params.LeftMarginOut;
561
0
        penum->xyi.y = penum->yi0 + fixed2int_pixround_perfect(
562
0
                                 (fixed)((int64_t)penum->rect.y *
563
0
                                         penum->dst_height / penum->Height));
564
0
    } else /* penum->posture == image_landscape */ {
565
        /* We always plot from top to bottom. If the matrix would have us
566
         * plotting from bottom to top, then adjust to allow for the fact
567
         * we'll flip the data later. */
568
0
        int x0 = penum->rrect.x;
569
0
        if (penum->matrix.xy < 0)
570
0
            x0 += penum->rrect.w;
571
0
        penum->xyi.x = fixed2int_pixround(dda_current(penum->dda.pixel0.x));
572
0
        penum->xyi.y = penum->yi0 + fixed2int_pixround_perfect(
573
0
                                 (fixed)((int64_t)x0 *
574
0
                                         penum->dst_height / penum->Width));
575
0
    }
576
    /* If the InterpolationControl specifies interpolation to less than full device res */
577
    /* Set up a scaling DDA to control scaling back to desired image size.    */
578
0
    if (abs_interp_limit > 1) {
579
0
        dda_init(pss->params.scale_dda.x, 0, iss.WidthOut, limited_WidthOut);
580
0
        dda_init(pss->params.scale_dda.y, 0, iss.HeightOut, limited_HeightOut);
581
0
    }
582
0
    if_debug0m('b', penum->memory, "[b]render=interpolate\n");
583
0
    if (penum->masked) {
584
0
        if (!mask_col_high_level) {
585
0
            *render_fn = (penum->posture == image_portrait ?
586
0
                    &image_render_interpolate_masked :
587
0
                    &image_render_interpolate_landscape_masked);
588
0
            return 0;
589
0
        } else {
590
0
            *render_fn = (penum->posture == image_portrait ?
591
0
                    &image_render_interpolate_masked_hl :
592
0
                    &image_render_interpolate_landscape_masked_hl);
593
0
            return 0;
594
0
        }
595
0
    } else if (use_icc) {
596
        /* Set up the link now */
597
0
        const gs_color_space *pcs;
598
0
        gsicc_rendering_param_t rendering_params;
599
0
        int k;
600
0
        int src_num_comp = cs_num_components(penum->pcs);
601
602
0
        penum->icc_setup.need_decode = false;
603
        /* Check if we need to do any decoding.  If yes, then that will slow us down */
604
0
        for (k = 0; k < src_num_comp; k++) {
605
0
            if ( penum->map[k].decoding != sd_none ) {
606
0
                penum->icc_setup.need_decode = true;
607
0
                break;
608
0
            }
609
0
        }
610
        /* Define the rendering intents */
611
0
        rendering_params.black_point_comp = penum->pgs->blackptcomp;
612
0
        rendering_params.graphics_type_tag = GS_IMAGE_TAG;
613
0
        rendering_params.override_icc = false;
614
0
        rendering_params.preserve_black = gsBKPRESNOTSPECIFIED;
615
0
        rendering_params.rendering_intent = penum->pgs->renderingintent;
616
0
        rendering_params.cmm = gsCMM_DEFAULT;
617
0
        if (gs_color_space_is_PSCIE(penum->pcs) && penum->pcs->icc_equivalent != NULL) {
618
0
            pcs = penum->pcs->icc_equivalent;
619
0
        } else {
620
            /* Look for indexed space */
621
0
            if ( penum->pcs->type->index == gs_color_space_index_Indexed) {
622
0
                pcs = penum->pcs->base_space;
623
0
            } else {
624
0
                pcs = penum->pcs;
625
0
            }
626
0
        }
627
0
        penum->icc_setup.is_lab = pcs->cmm_icc_profile_data->islab;
628
0
        if (penum->icc_setup.is_lab) penum->icc_setup.need_decode = false;
629
0
        penum->icc_setup.must_halftone = gx_device_must_halftone(penum->dev);
630
0
        penum->icc_setup.has_transfer =
631
0
            gx_has_transfer(penum->pgs, num_des_comps);
632
0
        if (penum->icc_link == NULL) {
633
0
            penum->icc_link = gsicc_get_link(penum->pgs, penum->dev, pcs, NULL,
634
0
                &rendering_params, penum->memory);
635
0
        }
636
        /* We need to make sure that we do the proper unpacking proc if we
637
           are doing 16 bit */
638
0
        if (penum->bps == 16) {
639
0
            penum->unpack = sample_unpackicc_16;
640
0
        }
641
0
        *render_fn = (penum->posture == image_portrait ?
642
0
                &image_render_interpolate_icc :
643
0
                &image_render_interpolate_landscape_icc);
644
0
        return 0;
645
0
    } else {
646
647
        /* If we are doing fastcolor we may still have
648
           a decode situation.  To avoid yet another
649
           variable, use the existing one in the
650
           icc setup */
651
0
        if (dev_profile->usefastcolor) {
652
0
            int k;
653
0
            int src_num_comp = cs_num_components(penum->pcs);
654
655
0
            penum->icc_setup.need_decode = false;
656
            /* Check if we need to do any decoding.  If yes, then that will slow us down */
657
0
            for (k = 0; k < src_num_comp; k++) {
658
0
                if ( penum->map[k].decoding != sd_none ) {
659
0
                    penum->icc_setup.need_decode = true;
660
0
                    break;
661
0
                }
662
0
            }
663
0
        }
664
665
0
        *render_fn = (penum->posture == image_portrait ?
666
0
                &image_render_interpolate :
667
0
                &image_render_interpolate_landscape);
668
0
        return 0;
669
0
    }
670
0
}
671
672
/* ------ Rendering for interpolated images ------ */
673
674
/* This does some initial required decoding of index spaces and general
675
   decoding of odd scaled image data needed prior to interpolation or
676
   application of color management. */
677
static void
678
initial_decode(gx_image_enum * penum, const byte * buffer, int data_x, int h,
679
               stream_cursor_read *stream_r, bool is_icc)
680
0
{
681
0
    stream_image_scale_state *pss = penum->scaler;
682
0
    const gs_color_space *pcs = penum->pcs;
683
0
    int spp_decode = pss->params.spp_decode;
684
0
    byte *out = penum->line;
685
0
    bool need_decode;
686
0
    int reversed =
687
0
        (penum->posture == image_portrait ? penum->matrix.xx : penum->matrix.xy) < 0;
688
    /* If cs is neither a device color nor a CIE color then for an image case
689
       it is going to be deviceN, sep, or index. */
690
0
    bool is_devn_sep_index =
691
0
        (!(penum->device_color) && !gs_color_space_is_CIE(pcs));
692
693
    /* Determine if we need to perform any decode procedures */
694
0
    if (is_icc) {
695
        /* In icc case, decode upfront occurs if specified by icc setup or if
696
           cs is neither a device color nor a CIE color (i.e. if it's DeviceN,
697
           Index or Separation) The color space cannot be a pattern for an image */
698
0
        need_decode = (penum->icc_setup.need_decode || is_devn_sep_index);
699
0
    } else {
700
0
        cmm_dev_profile_t *dev_profile;
701
702
0
        dev_proc(penum->dev, get_profile)(penum->dev, &dev_profile);
703
0
        if (dev_profile != NULL && dev_profile->usefastcolor)
704
0
            need_decode = (penum->icc_setup.need_decode || is_devn_sep_index);
705
0
        else
706
0
            need_decode = is_devn_sep_index;
707
0
    }
708
709
0
    if (h != 0) {
710
        /* Convert the unpacked data to concrete values in the source buffer. */
711
0
        int sizeofPixelIn = pss->params.BitsPerComponentIn / 8;
712
0
        uint row_size = pss->params.WidthIn * spp_decode * sizeofPixelIn;
713
        /* raw input data */
714
0
        const int raw_size = (pcs != NULL && pcs->type->index == gs_color_space_index_Indexed ?
715
0
                              1 : spp_decode);
716
0
        const unsigned char *bdata = buffer + data_x * raw_size * sizeofPixelIn;
717
        /* We have the following cases to worry about
718
          1) Device 8 bit color but not indexed (e.g. ICC).
719
             Apply CMM after interpolation if needed.
720
             Also if ICC CIELAB do not do a decode operation
721
          2) Indexed 8 bit color.  Get to the base space. We will then be in
722
             the same state as 1.
723
          3) 16 bit not indexed.  Remap after interpolation.
724
          4) Indexed 16bit color.   Get to base space in 16bit form. We
725
             will then be in same state as 3.
726
         */
727
0
        if (sizeofPixelIn == 1) {
728
0
            if (pcs == NULL || pcs->type->index != gs_color_space_index_Indexed) {
729
                /* An issue here is that we may not be "device color" due to
730
                   how the data is encoded.  Need to check for that case here */
731
                /* Decide here if we need to decode or not. Essentially, as
732
                 * far as I can gather, we use the top case if we DON'T need
733
                 * to decode. This is fairly obviously conditional on
734
                 * need_decode being set to 0. The major exception to this is
735
                 * that if the colorspace is CIE, we interpolate, THEN decode,
736
                 * so the decode is done later in the pipeline, so we needn't
737
                 * decode here (see Bugs 692225 and 692331). */
738
0
                if (!need_decode) {
739
                    /* 8-bit color values, possibly device  indep. or device
740
                       depend., not indexed. Decode range was [0 1] */
741
0
                    if (!reversed) {
742
                        /* Use the input data directly. sets up data in the
743
                           stream buffer structure */
744
0
                        stream_r->ptr = bdata - 1;
745
0
                    } else {
746
                        /* Mirror the data in X. */
747
0
                        const byte *p = bdata + row_size - spp_decode;
748
0
                        byte *q = out;
749
0
                        int i;
750
751
0
                        for (i = 0; i < pss->params.WidthIn;
752
0
                            p -= spp_decode, q += spp_decode, ++i)
753
0
                            memcpy(q, p, spp_decode);
754
0
                        stream_r->ptr = out - 1;
755
0
                    }
756
0
                } else {
757
                    /* We need to do some decoding. Data will remain in 8 bits
758
                       This does not occur if color space was CIE encoded.
759
                       Then we do the decode during concretization which occurs
760
                       after interpolation */
761
0
                    int dc = penum->spp;
762
0
                    const byte *pdata = bdata;
763
0
                    byte *psrc = (byte *) penum->line;
764
0
                    int i, j;
765
0
                    int dpd = dc;
766
0
                    gs_client_color cc;
767
768
                    /* Go backwards through the data */
769
0
                    if (reversed) {
770
0
                        pdata += (pss->params.WidthIn - 1) * dpd;
771
0
                        dpd = - dpd;
772
0
                    }
773
0
                    stream_r->ptr = (byte *) psrc - 1;
774
0
                    for (i = 0; i < pss->params.WidthIn; i++, psrc += spp_decode) {
775
                        /* Do the decode but remain in 8 bits */
776
0
                        for (j = 0; j < dc;  ++j) {
777
0
                            decode_sample(pdata[j], cc, j);
778
0
                            psrc[j] = float_color_to_byte_color(cc.paint.values[j]);
779
0
                        }
780
0
                        pdata += dpd;
781
0
                    }
782
0
                }
783
0
            } else {
784
                /* indexed 8 bit color values, possibly a device indep. or
785
                   device depend. base space. We need to get out of the indexed
786
                   space and into the base color space. Note that we need to
787
                   worry about the decode function for the index values. */
788
0
                int bps = penum->bps;
789
0
                int dc = penum->spp;
790
0
                const byte *pdata = bdata; /* Input buffer */
791
0
                unsigned char *psrc = (unsigned char *) penum->line;  /* Output */
792
0
                int i;
793
0
                int dpd = dc * (bps <= 8 ? 1 : sizeof(frac));
794
0
                float max_range;
795
796
                /* Get max of decode range */
797
0
                max_range = (penum->map[0].decode_factor < 0 ?
798
0
                    penum->map[0].decode_base :
799
0
                penum->map[0].decode_base + 255.0 * penum->map[0].decode_factor);
800
                /* flip the horizontal direction if indicated by the matrix value */
801
0
                if (reversed) {
802
0
                    pdata += (pss->params.WidthIn - 1) * dpd;
803
0
                    dpd = - dpd;
804
0
                }
805
0
                stream_r->ptr = (byte *) psrc - 1;
806
807
0
                for (i = 0; i < pss->params.WidthIn; i++, psrc += spp_decode) {
808
                    /* Let's get directly to a decoded byte type loaded into
809
                       psrc, and do the interpolation in the source space. Then
810
                       we will do the appropriate remap function after
811
                       interpolation. */
812
                    /* First we need to get the properly decoded value. */
813
0
                    float decode_value;
814
0
                    switch ( penum->map[0].decoding )
815
0
                    {
816
0
                        case sd_none:
817
                         /* while our indexin is going to be 0 to 255.0 due to
818
                            what is getting handed to us, the range of our
819
                            original data may not have been as such and we may
820
                            need to rescale, to properly lookup at the correct
821
                            location (or do the proc correctly) during the index
822
                            look-up.  This occurs even if decoding was set to
823
                            sd_none.  */
824
0
                            decode_value = (float) pdata[0] * (float)max_range / 255.0;
825
0
                            break;
826
0
                        case sd_lookup:
827
0
                            decode_value =
828
0
                              (float) penum->map[0].decode_lookup[pdata[0] >> 4];
829
0
                            break;
830
0
                        case sd_compute:
831
0
                            decode_value =
832
0
                              penum->map[0].decode_base +
833
0
                              ((float) pdata[0]) * penum->map[0].decode_factor;
834
0
                            break;
835
0
                        default:
836
0
                            decode_value = 0; /* Quiet gcc warning. */
837
0
                    }
838
0
                    gs_cspace_indexed_lookup_bytes(pcs, decode_value,psrc);
839
0
                    pdata += dpd;    /* Can't have just ++
840
                                        since we could be going backwards */
841
0
                }
842
0
            }
843
0
        } else {
844
            /* More than 8-bits/color values */
845
            /* Even in this case we need to worry about an indexed color space.
846
               We need to get to the base color space for the interpolation and
847
               then if necessary do the remap to the device space */
848
0
            if (pcs == NULL || pcs->type->index != gs_color_space_index_Indexed) {
849
0
                int bps = penum->bps;
850
0
                int dc = penum->spp;
851
0
                const byte *pdata = bdata;
852
0
                frac *psrc = (frac *) penum->line;
853
0
                int i, j;
854
0
                int dpd = dc * (bps <= 8 ? 1 : sizeof(frac));
855
856
0
                if (reversed) {
857
0
                    pdata += (pss->params.WidthIn - 1) * dpd;
858
0
                    dpd = - dpd;
859
0
                }
860
0
                stream_r->ptr = (byte *) psrc - 1;
861
0
                if_debug0m('B', penum->memory, "[B]Remap row:\n[B]");
862
0
                if (is_icc) {
863
0
                    if (reversed) {
864
0
                        byte *to = penum->line;
865
0
                        for (i = 0; i < pss->params.WidthIn; i++) {
866
0
                            memcpy(to, pdata, -dpd);
867
0
                            to -= dpd;
868
0
                            pdata += dpd;
869
0
                        }
870
0
                    } else {
871
0
                        stream_r->ptr = (byte *) pdata - 1;
872
0
                    }
873
0
                } else {
874
0
                    if (sizeof(frac) * dc == dpd) {
875
0
                        stream_r->ptr = (byte *) pdata - 1;
876
0
                    } else {
877
0
                        for (i = 0; i < pss->params.WidthIn; i++,
878
0
                             psrc += spp_decode) {
879
0
                            for (j = 0; j < dc; ++j) {
880
0
                                psrc[j] = ((const frac *)pdata)[j];
881
0
                            }
882
0
                            pdata += dpd;
883
    #ifdef DEBUG
884
                            if (gs_debug_c('B')) {
885
                                int ci;
886
887
                                for (ci = 0; ci < spp_decode; ++ci)
888
                                    dmprintf2(penum->memory, "%c%04x", (ci == 0 ? ' ' : ','), psrc[ci]);
889
                            }
890
    #endif
891
0
                        }
892
0
                    }
893
0
                }
894
0
                if_debug0m('B', penum->memory, "\n");
895
0
            } else {
896
                /* indexed and more than 8bps.  Need to get to the base space */
897
0
                int bps = penum->bps;
898
0
                int dc = penum->spp;
899
0
                const byte *pdata = bdata; /* Input buffer */
900
0
                frac *psrc = (frac *) penum->line;    /* Output buffer */
901
0
                int i;
902
0
                int dpd = dc * (bps <= 8 ? 1 : sizeof(frac));
903
0
                float decode_value;
904
905
                /* flip the horizontal direction if indicated by the matrix value */
906
0
                if (reversed) {
907
0
                    pdata += (pss->params.WidthIn - 1) * dpd;
908
0
                    dpd = - dpd;
909
0
                }
910
0
                stream_r->ptr = (byte *) psrc - 1;
911
0
                for (i = 0; i < pss->params.WidthIn; i++, psrc += spp_decode) {
912
                    /* Lets get the decoded value. Then we need to do the lookup
913
                       of this */
914
0
                    decode_value = penum->map[0].decode_base +
915
0
                        (((const frac *)pdata)[0]) * penum->map[0].decode_factor;
916
                    /* Now we need to do the lookup of this value, and stick it
917
                       in psrc as a frac, which is what the interpolator is
918
                       expecting, since we had more than 8 bits of original
919
                       image data */
920
0
                    gs_cspace_indexed_lookup_frac(pcs, decode_value,psrc);
921
0
                    pdata += dpd;
922
0
                }
923
0
            } /* end of else on indexed */
924
0
        }  /* end of else on more than 8 bps */
925
0
        stream_r->limit = stream_r->ptr + row_size;
926
0
    } else {                    /* h == 0 */
927
0
        stream_r->ptr = 0, stream_r->limit = 0;
928
0
    }
929
0
}
930
931
static int
932
handle_device_color(gx_image_enum *penum, const frac *psrc,
933
                    gx_device_color *devc, gx_device *dev,
934
                    const cmm_dev_profile_t *dev_profile,
935
                    const gs_color_space *pcs)
936
0
{
937
0
    const gs_gstate *pgs = penum->pgs;
938
939
0
    return (*pcs->type->remap_concrete_color)
940
0
            (pcs, psrc, devc, pgs, dev, gs_color_select_source, dev_profile);
941
0
}
942
943
/* LAB colors are normally decoded with a decode array
944
 * of [0 100  -128 127   -128 127 ]. The color management
945
 * however, expects this decode array NOT to have been
946
 * applied.
947
 *
948
 * It would be possible for an LAB image to be given a
949
 * non-standard decode array, in which case, we should
950
 * take account of that. The easiest way is to apply the
951
 * decode array as given, and then 'undo' the standard
952
 * one.
953
 */
954
static int
955
handle_labicc_color8(gx_image_enum *penum, const frac *psrc,
956
                     gx_device_color *devc, gx_device *dev,
957
                     const cmm_dev_profile_t *dev_profile,
958
                     const gs_color_space *pcs)
959
0
{
960
0
    const gs_gstate *pgs = penum->pgs;
961
0
    gs_client_color cc;
962
963
0
    decode_sample_frac_to_float(penum, psrc[0], &cc, 0);
964
0
    decode_sample_frac_to_float(penum, psrc[1], &cc, 1);
965
0
    decode_sample_frac_to_float(penum, psrc[2], &cc, 2);
966
0
    cc.paint.values[0] /= 100.0;
967
0
    cc.paint.values[1] = (cc.paint.values[1] + 128) / 255.0;
968
0
    cc.paint.values[2] = (cc.paint.values[2] + 128) / 255.0;
969
0
    return gx_remap_ICC_imagelab(&cc, pcs, devc, pgs, dev, gs_color_select_source);
970
0
}
971
972
static int
973
handle_labicc_color16(gx_image_enum *penum, const frac *psrc,
974
                      gx_device_color *devc, gx_device *dev,
975
                      const cmm_dev_profile_t *dev_profile,
976
                      const gs_color_space *pcs)
977
0
{
978
0
    const gs_gstate *pgs = penum->pgs;
979
0
    gs_client_color cc;
980
981
0
    decode_sample_frac_to_float(penum, psrc[0], &cc, 0);
982
0
    decode_sample_frac_to_float(penum, psrc[1], &cc, 1);
983
0
    decode_sample_frac_to_float(penum, psrc[2], &cc, 2);
984
0
    cc.paint.values[0] *= 0x7ff8 / 25500.0f;
985
0
    cc.paint.values[1] = (cc.paint.values[1] + 128) * 0x7ff8 / 65025.0;
986
0
    cc.paint.values[2] = (cc.paint.values[2] + 128) * 0x7ff8 / 65025.0;
987
0
    return gx_remap_ICC_imagelab(&cc, pcs, devc, pgs, dev, gs_color_select_source);
988
0
}
989
990
static int
991
handle_lab_color8(gx_image_enum *penum, const frac *psrc,
992
                  gx_device_color *devc, gx_device *dev,
993
                  const cmm_dev_profile_t *dev_profile,
994
                  const gs_color_space *pcs)
995
0
{
996
0
    const gs_gstate *pgs = penum->pgs;
997
0
    gs_client_color cc;
998
999
0
    decode_sample_frac_to_float(penum, psrc[0], &cc, 0);
1000
0
    decode_sample_frac_to_float(penum, psrc[1], &cc, 1);
1001
0
    decode_sample_frac_to_float(penum, psrc[2], &cc, 2);
1002
0
    cc.paint.values[0] /= 100.0;
1003
0
    cc.paint.values[1] = (cc.paint.values[1] + 128) / 255.0;
1004
0
    cc.paint.values[2] = (cc.paint.values[2] + 128) / 255.0;
1005
0
    return (pcs->type->remap_color)
1006
0
                (&cc, pcs, devc, pgs, dev, gs_color_select_source);
1007
0
}
1008
1009
static int
1010
handle_lab_color16(gx_image_enum *penum, const frac *psrc,
1011
                   gx_device_color *devc, gx_device *dev,
1012
                   const cmm_dev_profile_t *dev_profile,
1013
                   const gs_color_space *pcs)
1014
0
{
1015
0
    const gs_gstate *pgs = penum->pgs;
1016
0
    gs_client_color cc;
1017
1018
0
    decode_sample_frac_to_float(penum, psrc[0], &cc, 0);
1019
0
    decode_sample_frac_to_float(penum, psrc[1], &cc, 1);
1020
0
    decode_sample_frac_to_float(penum, psrc[2], &cc, 2);
1021
0
    cc.paint.values[0] *= 0x7ff8 / 25500.0f;
1022
0
    cc.paint.values[1] = (cc.paint.values[1] + 128) * 0x7ff8 / 65025.0;
1023
0
    cc.paint.values[2] = (cc.paint.values[2] + 128) * 0x7ff8 / 65025.0;
1024
0
    return (pcs->type->remap_color)
1025
0
                (&cc, pcs, devc, pgs, dev, gs_color_select_source);
1026
0
}
1027
1028
static int
1029
handle_labicc_color2_idx(gx_image_enum *penum, const frac *psrc,
1030
                         gx_device_color *devc, gx_device *dev,
1031
                         const cmm_dev_profile_t *dev_profile,
1032
                         const gs_color_space *pcs)
1033
0
{
1034
0
    const gs_gstate *pgs = penum->pgs;
1035
0
    gs_client_color cc;
1036
0
    int j;
1037
0
    int num_components = gs_color_space_num_components(pcs);
1038
1039
    /* If we were indexed, dont use the decode procedure for the index
1040
       values just get to float directly */
1041
0
    for (j = 0; j < num_components; ++j)
1042
0
        cc.paint.values[j] = frac2float(psrc[j]);
1043
    /* If the source colors are LAB then use the mapping that does not
1044
       rescale the source colors */
1045
0
    return gx_remap_ICC_imagelab(&cc, pcs, devc, pgs, dev,
1046
0
                                 gs_color_select_source);
1047
0
}
1048
1049
static int
1050
handle_remap_color_idx(gx_image_enum *penum, const frac *psrc,
1051
                       gx_device_color *devc, gx_device *dev,
1052
                       const cmm_dev_profile_t *dev_profile,
1053
                       const gs_color_space *pcs)
1054
0
{
1055
0
    const gs_gstate *pgs = penum->pgs;
1056
0
    gs_client_color cc;
1057
0
    int j;
1058
0
    int num_components = gs_color_space_num_components(pcs);
1059
1060
0
    for (j = 0; j < num_components; ++j)
1061
0
        cc.paint.values[j] = frac2float(psrc[j]);
1062
1063
0
    return (pcs->type->remap_color)
1064
0
                (&cc, pcs, devc, pgs, dev, gs_color_select_source);
1065
0
}
1066
1067
static int
1068
handle_labicc_color2(gx_image_enum *penum, const frac *psrc,
1069
                     gx_device_color *devc, gx_device *dev,
1070
                     const cmm_dev_profile_t *dev_profile,
1071
                     const gs_color_space *pcs)
1072
0
{
1073
0
    const gs_gstate *pgs = penum->pgs;
1074
0
    gs_client_color cc;
1075
0
    int j;
1076
0
    int num_components = gs_color_space_num_components(pcs);
1077
1078
0
    for (j = 0; j < num_components; ++j)
1079
0
        decode_sample_frac_to_float(penum, psrc[j], &cc, j);
1080
    /* If the source colors are LAB then use the mapping that does not
1081
       rescale the source colors */
1082
0
    return gx_remap_ICC_imagelab(&cc, pcs, devc, pgs, dev,
1083
0
                                 gs_color_select_source);
1084
0
}
1085
1086
static int
1087
handle_remap_color(gx_image_enum *penum, const frac *psrc,
1088
                   gx_device_color *devc, gx_device *dev,
1089
                   const cmm_dev_profile_t *dev_profile,
1090
                   const gs_color_space *pcs)
1091
0
{
1092
0
    const gs_gstate *pgs = penum->pgs;
1093
0
    gs_client_color cc;
1094
0
    int j;
1095
0
    int num_components = gs_color_space_num_components(pcs);
1096
1097
0
    for (j = 0; j < num_components; ++j)
1098
0
        decode_sample_frac_to_float(penum, psrc[j], &cc, j);
1099
1100
0
    return (pcs->type->remap_color)
1101
0
                (&cc, pcs, devc, pgs, dev, gs_color_select_source);
1102
0
}
1103
1104
typedef int (color_handler_fn)(gx_image_enum *penum, const frac *psrc,
1105
                               gx_device_color *devc, gx_device *dev,
1106
                               const cmm_dev_profile_t *dev_profile,
1107
                               const gs_color_space *pcs);
1108
1109
static color_handler_fn *
1110
get_color_handler(gx_image_enum *penum, int spp_decode,
1111
                  bool islab, const cmm_dev_profile_t *dev_profile,
1112
                  const gs_color_space **pconc)
1113
0
{
1114
0
    const gs_gstate *pgs = penum->pgs;
1115
0
    const gs_color_space *pcs = penum->pcs;
1116
0
    bool is_index_space;
1117
1118
0
    if (pcs == NULL)
1119
0
        return NULL; /* Must be masked */
1120
1121
0
    is_index_space = (pcs->type->index == gs_color_space_index_Indexed);
1122
    /* If we are in a non device space then work from the pcs not from the
1123
    concrete space also handle index case, where base case was device type */
1124
    /* We'll have done the interpolation in the base space, not the indexed
1125
     * space, so allow for that here. Also avoid problems with separation
1126
     * color spaces with check for presence of ICC profile */
1127
0
    if (is_index_space)
1128
0
        pcs = pcs->base_space;
1129
0
    if (dev_profile->usefastcolor && pcs->cmm_icc_profile_data != NULL &&
1130
0
        gsicc_is_default_profile(pcs->cmm_icc_profile_data) &&
1131
0
        dev_profile->device_profile[GS_DEFAULT_DEVICE_PROFILE]->num_comps == spp_decode) {
1132
0
        const gs_color_space * pconcs = cs_concrete_space(pcs, pgs);
1133
0
        if (pconcs && pconcs == pcs) {
1134
0
            *pconc = pconcs;
1135
0
            return handle_device_color;
1136
0
        }
1137
0
    }
1138
1139
0
    *pconc = pcs;
1140
    /* If we are device dependent we need to get back to float prior to remap.*/
1141
0
    if (islab) {
1142
0
        if (gs_color_space_is_ICC(pcs) &&
1143
0
            pcs->cmm_icc_profile_data != NULL &&
1144
0
            pcs->cmm_icc_profile_data->islab)
1145
0
            return penum->bps <= 8 ? handle_labicc_color8 : handle_labicc_color16;
1146
0
        else
1147
0
            return penum->bps <= 8 ? handle_lab_color8 : handle_lab_color16;
1148
0
    } else if (is_index_space) {
1149
0
        if (gs_color_space_is_ICC(pcs) &&
1150
0
            pcs->cmm_icc_profile_data != NULL &&
1151
0
            pcs->cmm_icc_profile_data->islab)
1152
0
            return handle_labicc_color2_idx;
1153
0
        else
1154
0
            return handle_remap_color_idx;
1155
0
    } else {
1156
0
        if (gs_color_space_is_ICC(pcs) &&
1157
0
            pcs->cmm_icc_profile_data != NULL &&
1158
0
            pcs->cmm_icc_profile_data->islab)
1159
0
            return handle_labicc_color2;
1160
0
        else
1161
0
            return handle_remap_color;
1162
0
    }
1163
0
}
1164
1165
/* returns the expanded width using the dda.x */
1166
static int
1167
interpolate_scaled_expanded_width(int delta_x, stream_image_scale_state *pss)
1168
0
{
1169
0
    gx_dda_fixed_point tmp_dda = pss->params.scale_dda;
1170
0
    int start_x = dda_current(tmp_dda.x);
1171
1172
0
    do {
1173
0
        dda_next(tmp_dda.x);
1174
0
    } while (--delta_x > 0);
1175
1176
0
    return dda_current(tmp_dda.x) - start_x;
1177
0
}
1178
1179
/* returns the expanded height using the dda.y */
1180
static int
1181
interpolate_scaled_expanded_height(int delta_y, stream_image_scale_state *pss)
1182
0
{
1183
0
    gx_dda_fixed_point tmp_dda = pss->params.scale_dda;
1184
0
    int start_y = dda_current(tmp_dda.y);
1185
1186
0
    do {
1187
0
        dda_next(tmp_dda.y);
1188
0
    } while (--delta_y > 0);
1189
1190
0
    return dda_current(tmp_dda.y) - start_y;
1191
0
}
1192
1193
static int
1194
image_render_interpolate(gx_image_enum * penum, const byte * buffer,
1195
                         int data_x, uint iw, int h, gx_device * dev)
1196
0
{
1197
0
    stream_image_scale_state *pss = penum->scaler;
1198
0
    const gs_color_space *pcs = penum->pcs;
1199
0
    gs_logical_operation_t lop = penum->log_op;
1200
0
    int spp_decode = pss->params.spp_decode;
1201
0
    stream_cursor_read stream_r;
1202
0
    stream_cursor_write stream_w;
1203
0
    byte *out = penum->line;
1204
0
    bool islab = false;
1205
0
    int abs_interp_limit = pss->params.abs_interp_limit;
1206
0
    int limited_PatchWidthOut = (pss->params.PatchWidthOut + abs_interp_limit - 1) / abs_interp_limit;
1207
0
    const gs_color_space *pconc;
1208
0
    cmm_dev_profile_t *dev_profile;
1209
0
    int code = dev_proc(penum->dev, get_profile)(penum->dev, &dev_profile);
1210
1211
0
    if (code < 0)
1212
0
        return code;
1213
1214
0
    if (!penum->masked && pcs->cmm_icc_profile_data != NULL) {
1215
0
        islab = pcs->cmm_icc_profile_data->islab;
1216
0
    }
1217
    /* Perform any decode procedure if needed */
1218
0
    initial_decode(penum, buffer, data_x, h, &stream_r, false);
1219
    /*
1220
     * Process input and/or collect output.  By construction, the pixels are
1221
     * 1-for-1 with the device if the abs(interpolate_control) is 1  but the
1222
     * Y coordinate might be inverted.
1223
     */
1224
0
    {
1225
0
        int xo = penum->xyi.x;
1226
0
        int yo = penum->xyi.y;
1227
0
        int width = (pss->params.WidthOut + abs_interp_limit - 1) / abs_interp_limit;
1228
0
        int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
1229
0
        int dy;
1230
0
        int bpp = dev->color_info.depth;
1231
0
        uint raster = bitmap_raster(width * bpp);
1232
0
        color_handler_fn *color_handler = NULL;
1233
1234
0
        if (penum->matrix.yy > 0)
1235
0
            dy = 1;
1236
0
        else
1237
0
            dy = -1, yo--;
1238
0
        for (;;) {
1239
0
            int ry = yo + penum->line_xy * dy;
1240
0
            int x;
1241
0
            const frac *psrc;
1242
0
            gx_device_color devc;
1243
0
            int status;
1244
0
            byte *l_dptr = out;
1245
0
            int l_dbit = 0;
1246
0
            byte l_dbyte = 0;
1247
0
            int l_xprev = (xo);
1248
0
            int scaled_x_prev = 0;
1249
0
            gx_dda_fixed save_x_dda = pss->params.scale_dda.x;
1250
1251
0
            devc.type = gx_dc_type_none; /* Needed for coverity, in call to color_is_pure() if color_handler is NULL. */
1252
0
            stream_w.limit = out + pss->params.WidthOut *
1253
0
                max(spp_decode * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
1254
0
            stream_w.ptr = stream_w.limit - width * spp_decode * sizeofPixelOut;
1255
0
            psrc = (const frac *)(stream_w.ptr + 1);
1256
            /* This is where the rescale takes place; this will consume the
1257
             * data from stream_r, and post processed data into stream_w. The
1258
             * data in stream_w may be bogus if we are outside the active
1259
             * region, and this will be indicated by pss->params.Active being
1260
             * set to false. */
1261
0
            status = (*pss->templat->process)
1262
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
1263
0
            if (status < 0 && status != EOFC)
1264
0
                return_error(gs_error_ioerror);
1265
0
            if (stream_w.ptr == stream_w.limit) {
1266
0
                int xe = xo + limited_PatchWidthOut;
1267
0
                int scaled_w = 0;   /* accumulate scaled up width */
1268
0
                int scaled_h = 0;
1269
0
                int scaled_y = 0;
1270
1271
0
                if (abs_interp_limit > 1) {
1272
0
                    scaled_h = interpolate_scaled_expanded_height(1, pss);
1273
0
                    scaled_y = yo + (dy * dda_current(pss->params.scale_dda.y));
1274
0
                }
1275
1276
                /* Are we active? (i.e. in the render rectangle) */
1277
0
                if (!pss->params.Active)
1278
0
                    goto inactive;
1279
0
                if_debug1m('B', penum->memory, "[B]Interpolated row %d:\n[B]",
1280
0
                           penum->line_xy);
1281
0
                psrc += ((pss->params.LeftMarginOut + abs_interp_limit - 1) / abs_interp_limit) * spp_decode;
1282
1283
0
                if (color_handler == NULL)
1284
0
                    color_handler = get_color_handler(penum, spp_decode, islab, dev_profile, &pconc);
1285
0
                for (x = xo; x < xe;) {
1286
0
                    if (color_handler != NULL) {
1287
#ifdef DEBUG
1288
                        if (gs_debug_c('B')) {
1289
                            int ci;
1290
1291
                            for (ci = 0; ci < spp_decode; ++ci)
1292
                                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
1293
                                          psrc[ci]);
1294
                        }
1295
#endif
1296
0
                        code = color_handler(penum, psrc, &devc, dev, dev_profile, pconc);
1297
0
                        if (code < 0)
1298
0
                            return code;
1299
0
                    }
1300
0
                    if (color_is_pure(&devc)) {
1301
0
                        gx_color_index color = devc.colors.pure;
1302
0
                        int expand = 1;
1303
1304
0
                        if (abs_interp_limit > 1) {
1305
0
                            expand = interpolate_scaled_expanded_width(1, pss);
1306
0
                        }
1307
                        /* Just pack colors into a scan line. */
1308
                        /* Skip runs quickly for the common cases. */
1309
0
                        switch (spp_decode) {
1310
0
                            case 1:
1311
0
                                do {
1312
0
                                    scaled_w += expand;
1313
0
                                    while (expand-- > 0) {
1314
0
                                        if (sizeof(color) > 4) {
1315
0
                                            if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1316
0
                                                return_error(gs_error_rangecheck);
1317
0
                                        }
1318
0
                                        else {
1319
0
                                            if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1320
0
                                                return_error(gs_error_rangecheck);
1321
0
                                        }
1322
0
                                    };
1323
0
                                    x++, psrc += 1;
1324
0
                                    if (abs_interp_limit > 1) {
1325
0
                                        dda_next(pss->params.scale_dda.x);
1326
0
                                        expand = interpolate_scaled_expanded_width(1, pss);
1327
0
                                    } else
1328
0
                                        expand = 1;
1329
0
                                } while (x < xe && psrc[-1] == psrc[0]);
1330
0
                                break;
1331
0
                            case 3:
1332
0
                                do {
1333
0
                                    scaled_w += expand;
1334
0
                                    while (expand-- > 0) {
1335
0
                                        if (sizeof(color) > 4) {
1336
0
                                            if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1337
0
                                                return_error(gs_error_rangecheck);
1338
0
                                        }
1339
0
                                        else {
1340
0
                                            if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1341
0
                                                return_error(gs_error_rangecheck);
1342
0
                                        }
1343
0
                                    };
1344
0
                                    x++, psrc += 3;
1345
0
                                    if (abs_interp_limit > 1) {
1346
0
                                        dda_next(pss->params.scale_dda.x);
1347
0
                                        expand = interpolate_scaled_expanded_width(1, pss);
1348
0
                                    } else
1349
0
                                        expand = 1;
1350
0
                                } while (x < xe &&
1351
0
                                         psrc[-3] == psrc[0] &&
1352
0
                                         psrc[-2] == psrc[1] &&
1353
0
                                         psrc[-1] == psrc[2]);
1354
0
                                break;
1355
0
                            case 4:
1356
0
                                do {
1357
0
                                    scaled_w += expand;
1358
0
                                    while (expand-- > 0) {
1359
0
                                        if (sizeof(color) > 4) {
1360
0
                                            if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1361
0
                                                return_error(gs_error_rangecheck);
1362
0
                                        }
1363
0
                                        else {
1364
0
                                            if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1365
0
                                                return_error(gs_error_rangecheck);
1366
0
                                        }
1367
0
                                    };
1368
0
                                    x++, psrc += 4;
1369
0
                                    if (abs_interp_limit > 1) {
1370
0
                                        dda_next(pss->params.scale_dda.x);
1371
0
                                        expand = interpolate_scaled_expanded_width(1, pss);
1372
0
                                    } else
1373
0
                                        expand = 1;
1374
0
                                } while (x < xe &&
1375
0
                                         psrc[-4] == psrc[0] &&
1376
0
                                         psrc[-3] == psrc[1] &&
1377
0
                                         psrc[-2] == psrc[2] &&
1378
0
                                         psrc[-1] == psrc[3]);
1379
0
                                break;
1380
0
                            default:  /* no run length check for these spp cases */
1381
0
                                scaled_w += expand;
1382
0
                                while (expand-- > 0) {
1383
0
                                    if (sizeof(color) > 4) {
1384
0
                                        if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1385
0
                                            return_error(gs_error_rangecheck);
1386
0
                                    }
1387
0
                                    else {
1388
0
                                        if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1389
0
                                            return_error(gs_error_rangecheck);
1390
0
                                    }
1391
0
                                };
1392
0
                                x++, psrc += spp_decode;
1393
0
                                if (abs_interp_limit > 1)
1394
0
                                    dda_next(pss->params.scale_dda.x);
1395
0
                        }
1396
0
                    } else {
1397
0
                        int rcode, rep = 0;
1398
1399
                        /* do _COPY in case any pure colors were accumulated above */
1400
0
                        if ( x > l_xprev ) {
1401
0
                            sample_store_flush(l_dptr, l_dbit, l_dbyte);
1402
0
                            if (abs_interp_limit <= 1) {
1403
0
                                code = (*dev_proc(dev, copy_color))
1404
0
                                  (dev, out, l_xprev - xo, raster,
1405
0
                                   gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1406
0
                                if (code < 0)
1407
0
                                    return code;
1408
0
                            } else {
1409
                                /* scale up in X and Y */
1410
0
                                int scaled_x = xo + scaled_x_prev;
1411
0
                                int i = scaled_h;
1412
0
                                int iy = scaled_y;
1413
1414
0
                                for (; i > 0; --i) {
1415
0
                                    code = (*dev_proc(dev, copy_color))
1416
0
                                      (dev, out, scaled_x_prev, raster,
1417
0
                                       gx_no_bitmap_id, scaled_x, iy, scaled_w, 1);
1418
0
                                    if (code < 0)
1419
0
                                        return code;
1420
0
                                    iy += dy;
1421
0
                                }
1422
0
                                scaled_x_prev = dda_current(pss->params.scale_dda.x);
1423
0
                            }
1424
0
                        }
1425
                        /* as above, see if we can accumulate any runs */
1426
0
                        switch (spp_decode) {
1427
0
                            case 1:
1428
0
                                do {
1429
0
                                    rep++, psrc += 1;
1430
0
                                } while ((rep + x) < xe &&
1431
0
                                         psrc[-1] == psrc[0]);
1432
0
                                break;
1433
0
                            case 3:
1434
0
                                do {
1435
0
                                    rep++, psrc += 3;
1436
0
                                } while ((rep + x) < xe &&
1437
0
                                         psrc[-3] == psrc[0] &&
1438
0
                                         psrc[-2] == psrc[1] &&
1439
0
                                         psrc[-1] == psrc[2]);
1440
0
                                break;
1441
0
                            case 4:
1442
0
                                do {
1443
0
                                    rep++, psrc += 4;
1444
0
                                } while ((rep + x) < xe &&
1445
0
                                         psrc[-4] == psrc[0] &&
1446
0
                                         psrc[-3] == psrc[1] &&
1447
0
                                         psrc[-2] == psrc[2] &&
1448
0
                                         psrc[-1] == psrc[3]);
1449
0
                                break;
1450
0
                            default:
1451
0
                                rep = 1;
1452
0
                                psrc += spp_decode;
1453
0
                                break;
1454
0
                        }
1455
0
                        if (abs_interp_limit <= 1) {
1456
0
                            scaled_w = rep;
1457
0
                            rcode = gx_fill_rectangle_device_rop(x, ry, rep, 1, &devc, dev, lop);
1458
0
                            if (rcode < 0)
1459
0
                                return rcode;
1460
0
                        } else {
1461
0
                            int scaled_x = xo + scaled_x_prev;
1462
1463
0
                            scaled_w = interpolate_scaled_expanded_width(rep, pss);
1464
0
                            rcode = gx_fill_rectangle_device_rop(scaled_x, scaled_y, scaled_w, scaled_h,
1465
0
                                                                 &devc, dev, lop);
1466
0
                            if (rcode < 0)
1467
0
                                return rcode;
1468
0
                            dda_advance(pss->params.scale_dda.x, rep);
1469
0
                            scaled_x_prev = dda_current(pss->params.scale_dda.x);
1470
0
                        }
1471
0
                        while (scaled_w-- > 0)
1472
0
                            sample_store_skip_next(&l_dptr, &l_dbit, bpp, &l_dbyte);
1473
0
                        scaled_w = 0;
1474
0
                        l_xprev = x + rep;
1475
0
                        x += rep;
1476
0
                    }
1477
0
                }  /* End on x loop */
1478
0
                if ( x > l_xprev ) {
1479
0
                    sample_store_flush(l_dptr, l_dbit, l_dbyte);
1480
0
                    if (abs_interp_limit <= 1) {
1481
0
                        code = (*dev_proc(dev, copy_color))
1482
0
                          (dev, out, l_xprev - xo, raster,
1483
0
                           gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1484
0
                        if (code < 0)
1485
0
                            return code;
1486
0
                    } else {
1487
                        /* scale up in X and Y */
1488
0
                        int scaled_x = xo + scaled_x_prev;
1489
1490
0
                        for (; scaled_h > 0; --scaled_h) {
1491
0
                            code = (*dev_proc(dev, copy_color))
1492
0
                              (dev, out, scaled_x_prev, raster,
1493
0
                               gx_no_bitmap_id, scaled_x, scaled_y, scaled_w, 1);
1494
0
                            if (code < 0)
1495
0
                                return code;
1496
0
                            scaled_y += dy;
1497
0
                        }
1498
0
                    }
1499
0
                }
1500
                /*if_debug1m('w', dev->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
1501
0
inactive:
1502
0
                penum->line_xy++;
1503
0
                if (abs_interp_limit > 1) {
1504
0
                    dda_next(pss->params.scale_dda.y);
1505
0
                    pss->params.scale_dda.x = save_x_dda; /* reset X to start of line */
1506
0
                }
1507
0
                if_debug0m('B', dev->memory, "\n");
1508
0
            }
1509
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
1510
0
                break;
1511
0
        }
1512
0
    }
1513
0
    return (h == 0 ? 0 : 1);
1514
0
}
1515
1516
static int
1517
image_render_interpolate_masked(gx_image_enum * penum, const byte * buffer,
1518
                                int data_x, uint iw, int h, gx_device * dev)
1519
0
{
1520
0
    stream_image_scale_state *pss = penum->scaler;
1521
0
    stream_cursor_read stream_r;
1522
0
    stream_cursor_write stream_w;
1523
0
    byte *out = penum->line;
1524
0
    gx_color_index color = penum->icolor1->colors.pure;
1525
1526
    /* Perform any decode procedure if needed */
1527
0
    initial_decode(penum, buffer, data_x, h, &stream_r, false);
1528
    /*
1529
     * Process input and/or collect output.  By construction, the pixels are
1530
     * 1-for-1 with the device, but the Y coordinate might be inverted.
1531
     */
1532
0
    {
1533
0
        int xo = penum->xyi.x;
1534
0
        int yo = penum->xyi.y;
1535
0
        int width = pss->params.WidthOut;
1536
0
        int dy;
1537
0
        int bpp = dev->color_info.depth;
1538
0
        uint raster = bitmap_raster(width * bpp);
1539
1540
0
        if (penum->matrix.yy > 0)
1541
0
            dy = 1;
1542
0
        else
1543
0
            dy = -1, yo--;
1544
0
        for (;;) {
1545
0
            int ry = yo + penum->line_xy * dy;
1546
0
            const byte *psrc;
1547
0
            int status, code;
1548
1549
0
            stream_w.limit = out + width - 1;
1550
0
            stream_w.ptr = stream_w.limit - width;
1551
0
            psrc = stream_w.ptr + 1;
1552
            /* This is where the rescale takes place; this will consume the
1553
             * data from stream_r, and post processed data into stream_w. The
1554
             * data in stream_w may be bogus if we are outside the active
1555
             * region, and this will be indicated by pss->params.Active being
1556
             * set to false. */
1557
0
            status = (*pss->templat->process)
1558
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
1559
0
            if (status < 0 && status != EOFC)
1560
0
                return_error(gs_error_ioerror);
1561
0
            if (stream_w.ptr == stream_w.limit) {
1562
0
                int xe = xo + pss->params.PatchWidthOut;
1563
1564
                /* Are we active? (i.e. in the render rectangle) */
1565
0
                if (!pss->params.Active)
1566
0
                    goto inactive;
1567
0
                if_debug1m('B', penum->memory, "[B]Interpolated mask row %d:\n[B]",
1568
0
                           penum->line_xy);
1569
0
                psrc += pss->params.LeftMarginOut;
1570
0
                code = (*dev_proc(dev, copy_alpha))
1571
0
                            (dev, psrc, 0, raster,
1572
0
                             gx_no_bitmap_id, xo, ry, xe-xo, 1,
1573
0
                             color, 8);
1574
0
                if ( code < 0 )
1575
0
                    return code;
1576
0
inactive:
1577
0
                penum->line_xy++;
1578
0
                if_debug0m('B', dev->memory, "\n");
1579
0
            }
1580
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
1581
0
                break;
1582
0
        }
1583
0
    }
1584
0
    return (h == 0 ? 0 : 1);
1585
0
}
1586
1587
static int
1588
image_render_interpolate_masked_hl(gx_image_enum * penum, const byte * buffer,
1589
                                   int data_x, uint iw, int h, gx_device * dev)
1590
0
{
1591
0
    stream_image_scale_state *pss = penum->scaler;
1592
0
    stream_cursor_read stream_r;
1593
0
    stream_cursor_write stream_w;
1594
0
    byte *out = penum->line;
1595
1596
    /* Perform any decode procedure if needed */
1597
0
    initial_decode(penum, buffer, data_x, h, &stream_r, false);
1598
    /*
1599
     * Process input and/or collect output.  By construction, the pixels are
1600
     * 1-for-1 with the device, but the Y coordinate might be inverted.
1601
     */
1602
0
    {
1603
0
        int xo = penum->xyi.x;
1604
0
        int yo = penum->xyi.y;
1605
0
        int width = pss->params.WidthOut;
1606
0
        int dy;
1607
0
        int bpp = dev->color_info.depth;
1608
0
        uint raster = bitmap_raster(width * bpp);
1609
1610
0
        if (penum->matrix.yy > 0)
1611
0
            dy = 1;
1612
0
        else
1613
0
            dy = -1, yo--;
1614
0
        for (;;) {
1615
0
            int ry = yo + penum->line_xy * dy;
1616
0
            const byte *psrc;
1617
0
            int status, code;
1618
1619
0
            stream_w.limit = out + width - 1;
1620
0
            stream_w.ptr = stream_w.limit - width;
1621
0
            psrc = stream_w.ptr + 1;
1622
            /* This is where the rescale takes place; this will consume the
1623
             * data from stream_r, and post processed data into stream_w. The
1624
             * data in stream_w may be bogus if we are outside the active
1625
             * region, and this will be indicated by pss->params.Active being
1626
             * set to false. */
1627
0
            status = (*pss->templat->process)
1628
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
1629
0
            if (status < 0 && status != EOFC)
1630
0
                return_error(gs_error_ioerror);
1631
0
            if (stream_w.ptr == stream_w.limit) {
1632
0
                int xe = xo + pss->params.PatchWidthOut;
1633
1634
                /* Are we active? (i.e. in the render rectangle) */
1635
0
                if (!pss->params.Active)
1636
0
                    goto inactive;
1637
0
                if_debug1m('B', penum->memory, "[B]Interpolated mask row %d:\n[B]",
1638
0
                           penum->line_xy);
1639
0
                psrc += pss->params.LeftMarginOut;
1640
0
                code = (*dev_proc(dev, copy_alpha_hl_color))
1641
0
                            (dev, psrc, 0, raster,
1642
0
                             gx_no_bitmap_id, xo, ry, xe-xo, 1,
1643
0
                             penum->icolor1, 8);
1644
0
                if ( code < 0 )
1645
0
                    return code;
1646
0
inactive:
1647
0
                penum->line_xy++;
1648
0
                if_debug0m('B', dev->memory, "\n");
1649
0
            }
1650
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
1651
0
                break;
1652
0
        }
1653
0
    }
1654
0
    return (h == 0 ? 0 : 1);
1655
0
}
1656
1657
static void
1658
get_device_color(gx_image_enum * penum, unsigned short *p_cm_interp,
1659
gx_device_color *devc, gx_color_index *color, gx_device * dev)
1660
0
{
1661
0
    bool must_halftone = penum->icc_setup.must_halftone;
1662
0
    bool has_transfer = penum->icc_setup.has_transfer;
1663
1664
0
    if (must_halftone || has_transfer) {
1665
        /* We need to do the tranfer function and/or the halftoning */
1666
0
        cmap_transfer_halftone(p_cm_interp, devc, penum->pgs, dev,
1667
0
            has_transfer, must_halftone, gs_color_select_source);
1668
0
    } else {
1669
        /* encode as a color index. avoid all the cv to frac to cv conversions */
1670
0
        *color = dev_proc(dev, encode_color)(dev, p_cm_interp);
1671
        /* check if the encoding was successful; we presume failure is rare */
1672
0
        if (*color != gx_no_color_index)
1673
0
            color_set_pure(devc, *color);
1674
0
    }
1675
0
}
1676
1677
typedef int (*irii_core_fn)(gx_image_enum * penum, int xo, int xe, int spp_cm, unsigned short *p_cm_interp, gx_device *dev, int abs_interp_limit, int bpp, int raster, int yo, int dy, gs_logical_operation_t lop);
1678
1679
static inline int
1680
irii_inner_template(gx_image_enum * penum, int xo, int xe, int spp_cm, unsigned short *p_cm_interp, gx_device *dev, int abs_interp_limit, int bpp, int raster, int yo, int dy, gs_logical_operation_t lop)
1681
0
{
1682
0
    int x;
1683
0
    gx_device_color devc;
1684
0
    gx_color_index color;
1685
0
    stream_image_scale_state *pss = penum->scaler;
1686
0
    int scaled_w = 0;   /* accumulate scaled up width */
1687
0
    byte *out = penum->line;
1688
0
    byte *l_dptr = out;
1689
0
    int l_dbit = 0;
1690
0
    byte l_dbyte = 0;
1691
0
    int l_xprev = (xo);
1692
0
    int scaled_x_prev = 0;
1693
0
    int code;
1694
0
    int ry = yo + penum->line_xy * dy;
1695
0
    gx_dda_fixed save_x_dda = pss->params.scale_dda.x;
1696
0
    int scaled_h = 0;
1697
0
    int scaled_y = 0;
1698
1699
0
    if (abs_interp_limit > 1) {
1700
0
        scaled_h = interpolate_scaled_expanded_height(1, pss);
1701
0
        scaled_y = yo + (dy * dda_current(pss->params.scale_dda.y));
1702
0
    }
1703
1704
0
    for (x = xo; x < xe;) {
1705
1706
#ifdef DEBUG
1707
        if (gs_debug_c('B')) {
1708
            int ci;
1709
1710
            for (ci = 0; ci < spp_cm; ++ci)
1711
                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
1712
                          p_cm_interp[ci]);
1713
        }
1714
#endif
1715
        /* Get the device color */
1716
0
        get_device_color(penum, p_cm_interp, &devc, &color, dev);
1717
0
        if (color_is_pure(&devc)) {
1718
0
            gx_color_index color = devc.colors.pure;
1719
0
            int expand = 1;
1720
1721
0
            if (abs_interp_limit > 1) {
1722
0
                expand = interpolate_scaled_expanded_width(1, pss);
1723
0
            }
1724
            /* Just pack colors into a scan line. */
1725
            /* Skip runs quickly for the common cases. */
1726
0
            switch (spp_cm) {
1727
0
                case 1:
1728
0
                    do {
1729
0
                        scaled_w += expand;
1730
0
                        while (expand-- > 0) {
1731
0
                            if (sizeof(color) > 4) {
1732
0
                                if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1733
0
                                    return_error(gs_error_rangecheck);
1734
0
                            } else {
1735
0
                                if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1736
0
                                    return_error(gs_error_rangecheck);
1737
0
                            }
1738
0
                        }
1739
0
                        x++, p_cm_interp += 1;
1740
0
                        if (abs_interp_limit > 1) {
1741
0
                            dda_next(pss->params.scale_dda.x);
1742
0
                            expand = interpolate_scaled_expanded_width(1, pss);
1743
0
                        } else
1744
0
                            expand = 1;
1745
0
                    } while (x < xe && p_cm_interp[-1] == p_cm_interp[0]);
1746
0
                    break;
1747
0
                case 3:
1748
0
                    do {
1749
0
                        scaled_w += expand;
1750
0
                        while (expand-- > 0) {
1751
0
                            if (sizeof(color) > 4) {
1752
0
                                if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1753
0
                                    return_error(gs_error_rangecheck);
1754
0
                            } else {
1755
0
                                if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1756
0
                                    return_error(gs_error_rangecheck);
1757
0
                            }
1758
0
                        }
1759
0
                        x++, p_cm_interp += 3;
1760
0
                        if (abs_interp_limit > 1) {
1761
0
                            dda_next(pss->params.scale_dda.x);
1762
0
                            expand = interpolate_scaled_expanded_width(1, pss);
1763
0
                        } else
1764
0
                            expand = 1;
1765
0
                    } while (x < xe && p_cm_interp[-3] == p_cm_interp[0] &&
1766
0
                                       p_cm_interp[-2] == p_cm_interp[1] &&
1767
0
                                       p_cm_interp[-1] == p_cm_interp[2]);
1768
0
                    break;
1769
0
                case 4:
1770
0
                    do {
1771
0
                        scaled_w += expand;
1772
0
                        while (expand-- > 0) {
1773
0
                            if (sizeof(color) > 4) {
1774
0
                                if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1775
0
                                    return_error(gs_error_rangecheck);
1776
0
                            } else {
1777
0
                                if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1778
0
                                    return_error(gs_error_rangecheck);
1779
0
                            }
1780
0
                        }
1781
0
                        x++, p_cm_interp += 4;
1782
0
                        if (abs_interp_limit > 1) {
1783
0
                            dda_next(pss->params.scale_dda.x);
1784
0
                            expand = interpolate_scaled_expanded_width(1, pss);
1785
0
                        } else
1786
0
                            expand = 1;
1787
0
                    } while (x < xe && p_cm_interp[-4] == p_cm_interp[0] &&
1788
0
                                       p_cm_interp[-3] == p_cm_interp[1] &&
1789
0
                                       p_cm_interp[-2] == p_cm_interp[2] &&
1790
0
                                       p_cm_interp[-1] == p_cm_interp[3]);
1791
0
                    break;
1792
0
                default:
1793
0
                    scaled_w += expand;
1794
0
                    while (expand-- > 0) {
1795
0
                        if (sizeof(color) > 4) {
1796
0
                            if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1797
0
                                return_error(gs_error_rangecheck);
1798
0
                        } else {
1799
0
                            if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1800
0
                                return_error(gs_error_rangecheck);
1801
0
                        }
1802
0
                    };
1803
0
                    x++, p_cm_interp += spp_cm;
1804
0
                    if (abs_interp_limit > 1)
1805
0
                        dda_next(pss->params.scale_dda.x);
1806
0
            }
1807
0
        } else {
1808
0
            int rcode, rep = 0;
1809
1810
            /* do _COPY in case any pure colors were accumulated above*/
1811
0
            if ( x > l_xprev ) {
1812
0
                sample_store_flush(l_dptr, l_dbit, l_dbyte);
1813
0
                if (abs_interp_limit <= 1) {
1814
0
                    code = (*dev_proc(dev, copy_color))
1815
0
                                   (dev, out, l_xprev - xo, raster,
1816
0
                                    gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1817
0
                    if (code < 0)
1818
0
                        return code;
1819
0
                } else {
1820
                    /* scale up in X and Y */
1821
0
                    int scaled_x = xo + scaled_x_prev;
1822
0
                    int i = scaled_h;
1823
0
                    int iy = scaled_y;
1824
1825
0
                    for (; i > 0; --i) {
1826
0
                         code = (*dev_proc(dev, copy_color))
1827
0
                                      (dev, out, scaled_x_prev, raster,
1828
0
                                       gx_no_bitmap_id, scaled_x, iy, scaled_w, 1);
1829
0
                         if (code < 0)
1830
0
                             return code;
1831
0
                         iy += dy;
1832
0
                    }
1833
0
                    scaled_x_prev = dda_current(pss->params.scale_dda.x);
1834
0
                }
1835
0
            }
1836
            /* as above, see if we can accumulate any runs */
1837
0
            switch (spp_cm) {
1838
0
                case 1:
1839
0
                    do {
1840
0
                        rep++, p_cm_interp += 1;
1841
0
                    } while ((rep + x) < xe && p_cm_interp[-1] == p_cm_interp[0]);
1842
0
                    break;
1843
0
                case 3:
1844
0
                    do {
1845
0
                        rep++, p_cm_interp += 3;
1846
0
                    } while ((rep + x) < xe && p_cm_interp[-3] == p_cm_interp[0] &&
1847
0
                                               p_cm_interp[-2] == p_cm_interp[1] &&
1848
0
                                               p_cm_interp[-1] == p_cm_interp[2]);
1849
0
                    break;
1850
0
                case 4:
1851
0
                    do {
1852
0
                        rep++, p_cm_interp += 4;
1853
0
                    } while ((rep + x) < xe && p_cm_interp[-4] == p_cm_interp[0] &&
1854
0
                                               p_cm_interp[-3] == p_cm_interp[1] &&
1855
0
                                               p_cm_interp[-2] == p_cm_interp[2] &&
1856
0
                                               p_cm_interp[-1] == p_cm_interp[3]);
1857
0
                    break;
1858
0
                default:
1859
0
                    rep = 1, p_cm_interp += spp_cm;
1860
0
                    break;
1861
0
            }
1862
0
            if (abs_interp_limit <= 1) {
1863
0
                scaled_w = rep;
1864
0
                rcode = gx_fill_rectangle_device_rop(x, ry, rep, 1, &devc, dev, lop);
1865
0
                if (rcode < 0)
1866
0
                    return rcode;
1867
0
            } else {
1868
0
                int scaled_x = xo + scaled_x_prev;
1869
1870
0
                scaled_w = interpolate_scaled_expanded_width(rep, pss);
1871
0
                rcode = gx_fill_rectangle_device_rop(scaled_x, scaled_y, scaled_w, scaled_h,
1872
0
                                                     &devc, dev, lop);
1873
0
                if (rcode < 0)
1874
0
                    return rcode;
1875
0
                dda_advance(pss->params.scale_dda.x, rep);
1876
0
                scaled_x_prev = dda_current(pss->params.scale_dda.x);
1877
0
            }
1878
0
            while (scaled_w-- > 0)
1879
0
                sample_store_skip_next(&l_dptr, &l_dbit, bpp, &l_dbyte);
1880
0
            scaled_w = 0;
1881
0
            x += rep;
1882
0
            l_xprev = x;
1883
0
        }
1884
0
    }  /* End on x loop */
1885
0
    if ( x > l_xprev ) {
1886
0
        sample_store_flush(l_dptr, l_dbit, l_dbyte);
1887
0
        if (abs_interp_limit <= 1) {
1888
0
            code = (*dev_proc(dev, copy_color))
1889
0
                          (dev, out, l_xprev - xo, raster,
1890
0
                           gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1891
0
            if (code < 0)
1892
0
                return code;
1893
0
        } else {
1894
            /* scale up in X and Y */
1895
0
            int scaled_x = xo + scaled_x_prev;
1896
1897
0
            for (; scaled_h > 0; --scaled_h) {
1898
0
                code = (*dev_proc(dev, copy_color))
1899
0
                              (dev, out, scaled_x_prev, raster,
1900
0
                               gx_no_bitmap_id, scaled_x, scaled_y, scaled_w, 1);
1901
0
                if (code < 0)
1902
0
                    return code;
1903
0
                scaled_y += dy;
1904
0
            }
1905
0
        }
1906
0
    }
1907
0
    if (abs_interp_limit > 1) {
1908
0
        pss->params.scale_dda.x = save_x_dda; /* reset X to start of line */
1909
0
    }
1910
    /*if_debug1m('w', penum->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
1911
0
    return 0;
1912
0
}
1913
1914
static int irii_inner_32bpp_4spp_1abs(gx_image_enum * penum, int xo, int xe, int spp_cm, unsigned short *p_cm_interp, gx_device *dev, int abs_interp_limit, int bpp, int raster, int yo, int dy, gs_logical_operation_t lop)
1915
0
{
1916
0
    int x;
1917
0
    gx_device_color devc;
1918
0
    gx_color_index color;
1919
0
    byte *out = penum->line;
1920
0
    byte *l_dptr = out;
1921
0
    int l_xprev = (xo);
1922
0
    int code;
1923
0
    int ry = yo + penum->line_xy * dy;
1924
1925
0
    for (x = xo; x < xe;) {
1926
1927
#ifdef DEBUG
1928
        if (gs_debug_c('B')) {
1929
            int ci;
1930
1931
            for (ci = 0; ci < 3; ++ci)
1932
                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
1933
                    p_cm_interp[ci]);
1934
        }
1935
#endif
1936
        /* Get the device color */
1937
0
        get_device_color(penum, p_cm_interp, &devc, &color, dev);
1938
0
        if (color_is_pure(&devc)) {
1939
0
            gx_color_index color = devc.colors.pure;
1940
1941
            /* Just pack colors into a scan line. */
1942
            /* Skip runs quickly for the common cases. */
1943
0
            do {
1944
0
                *l_dptr++ = (byte)(color >> 24);
1945
0
                *l_dptr++ = (byte)(color >> 16);
1946
0
                *l_dptr++ = (byte)(color >> 8);
1947
0
                *l_dptr++ = (byte)(color);
1948
0
                x++, p_cm_interp += 4;
1949
0
            } while (x < xe && p_cm_interp[-4] == p_cm_interp[0] &&
1950
0
                               p_cm_interp[-3] == p_cm_interp[1] &&
1951
0
                               p_cm_interp[-2] == p_cm_interp[2] &&
1952
0
                               p_cm_interp[-1] == p_cm_interp[3]);
1953
0
        }
1954
0
        else {
1955
0
            int rep = 0;
1956
1957
            /* do _COPY in case any pure colors were accumulated above*/
1958
0
            if (x > l_xprev) {
1959
0
                code = (*dev_proc(dev, copy_color))
1960
0
                    (dev, out, l_xprev - xo, raster,
1961
0
                        gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1962
0
                if (code < 0)
1963
0
                    return code;
1964
0
            }
1965
            /* as above, see if we can accumulate any runs */
1966
0
            do {
1967
0
                rep++, p_cm_interp += 4;
1968
0
            } while ((rep + x) < xe && p_cm_interp[-4] == p_cm_interp[0] &&
1969
0
                                       p_cm_interp[-3] == p_cm_interp[1] &&
1970
0
                                       p_cm_interp[-2] == p_cm_interp[2] &&
1971
0
                                       p_cm_interp[-1] == p_cm_interp[3]);
1972
0
            code = gx_fill_rectangle_device_rop(x, ry, rep, 1, &devc, dev, lop);
1973
0
            if (code < 0)
1974
0
                return code;
1975
0
            x += rep;
1976
0
            l_xprev = x;
1977
0
            l_dptr += 4 * rep;
1978
0
        }
1979
0
    }  /* End on x loop */
1980
0
    if (x > l_xprev) {
1981
0
        code = (*dev_proc(dev, copy_color))
1982
0
            (dev, out, l_xprev - xo, raster,
1983
0
                gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1984
0
        if (code < 0)
1985
0
            return code;
1986
0
    }
1987
    /*if_debug1m('w', penum->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
1988
0
    return 0;
1989
0
}
1990
1991
static int irii_inner_24bpp_3spp_1abs(gx_image_enum * penum, int xo, int xe, int spp_cm, unsigned short *p_cm_interp, gx_device *dev, int abs_interp_limit, int bpp, int raster, int yo, int dy, gs_logical_operation_t lop)
1992
0
{
1993
0
    int x;
1994
0
    gx_device_color devc;
1995
0
    gx_color_index color;
1996
0
    byte *out = penum->line;
1997
0
    byte *l_dptr = out;
1998
0
    int l_xprev = (xo);
1999
0
    int code;
2000
0
    int ry = yo + penum->line_xy * dy;
2001
2002
0
    for (x = xo; x < xe;) {
2003
#ifdef DEBUG
2004
        if (gs_debug_c('B')) {
2005
            int ci;
2006
2007
            for (ci = 0; ci < 3; ++ci)
2008
                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
2009
                    p_cm_interp[ci]);
2010
        }
2011
#endif
2012
        /* Get the device color */
2013
0
        get_device_color(penum, p_cm_interp, &devc, &color, dev);
2014
0
        if (color_is_pure(&devc)) {
2015
0
            gx_color_index color = devc.colors.pure;
2016
2017
            /* Just pack colors into a scan line. */
2018
            /* Skip runs quickly for the common cases. */
2019
0
            do {
2020
0
                *l_dptr++ = (byte)(color >> 16);
2021
0
                *l_dptr++ = (byte)(color >> 8);
2022
0
                *l_dptr++ = (byte)(color);
2023
0
                x++, p_cm_interp += 3;
2024
0
            } while (x < xe && p_cm_interp[-3] == p_cm_interp[0] &&
2025
0
                               p_cm_interp[-2] == p_cm_interp[1] &&
2026
0
                               p_cm_interp[-1] == p_cm_interp[2]);
2027
0
        }
2028
0
        else {
2029
0
            int rep = 0;
2030
2031
            /* do _COPY in case any pure colors were accumulated above*/
2032
0
            if (x > l_xprev) {
2033
0
                code = (*dev_proc(dev, copy_color))
2034
0
                    (dev, out, l_xprev - xo, raster,
2035
0
                        gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
2036
0
                if (code < 0)
2037
0
                    return code;
2038
0
            }
2039
            /* as above, see if we can accumulate any runs */
2040
0
            do {
2041
0
                rep++, p_cm_interp += 3;
2042
0
            } while ((rep + x) < xe && p_cm_interp[-3] == p_cm_interp[0] &&
2043
0
                p_cm_interp[-2] == p_cm_interp[1] &&
2044
0
                p_cm_interp[-1] == p_cm_interp[2]);
2045
0
            code = gx_fill_rectangle_device_rop(x, ry, rep, 1, &devc, dev, lop);
2046
0
            if (code < 0)
2047
0
                return code;
2048
0
            x += rep;
2049
0
            l_xprev = x;
2050
0
            l_dptr += 3 * rep;
2051
0
        }
2052
0
    }  /* End on x loop */
2053
0
    if (x > l_xprev) {
2054
0
        code = (*dev_proc(dev, copy_color))
2055
0
            (dev, out, l_xprev - xo, raster,
2056
0
                gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
2057
0
        if (code < 0)
2058
0
            return code;
2059
0
    }
2060
    /*if_debug1m('w', penum->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2061
0
    return 0;
2062
0
}
2063
2064
static int irii_inner_8bpp_1spp_1abs(gx_image_enum * penum, int xo, int xe, int spp_cm, unsigned short *p_cm_interp, gx_device *dev, int abs_interp_limit, int bpp, int raster, int yo, int dy, gs_logical_operation_t lop)
2065
0
{
2066
0
    int x;
2067
0
    gx_device_color devc;
2068
0
    gx_color_index color;
2069
0
    byte *out = penum->line;
2070
0
    byte *l_dptr = out;
2071
0
    int l_xprev = (xo);
2072
0
    int code;
2073
0
    int ry = yo + penum->line_xy * dy;
2074
2075
0
    for (x = xo; x < xe;) {
2076
#ifdef DEBUG
2077
        if (gs_debug_c('B')) {
2078
            int ci;
2079
2080
            for (ci = 0; ci < 3; ++ci)
2081
                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
2082
                    p_cm_interp[ci]);
2083
        }
2084
#endif
2085
        /* Get the device color */
2086
0
        get_device_color(penum, p_cm_interp, &devc, &color, dev);
2087
0
        if (color_is_pure(&devc)) {
2088
0
            gx_color_index color = devc.colors.pure;
2089
2090
            /* Just pack colors into a scan line. */
2091
            /* Skip runs quickly for the common cases. */
2092
0
            do {
2093
0
                *l_dptr++ = (byte)(color);
2094
0
                x++, p_cm_interp++;
2095
0
            } while (x < xe && p_cm_interp[-1] == p_cm_interp[0]);
2096
0
        }
2097
0
        else {
2098
0
            int rep = 0;
2099
2100
            /* do _COPY in case any pure colors were accumulated above*/
2101
0
            if (x > l_xprev) {
2102
0
                code = (*dev_proc(dev, copy_color))
2103
0
                    (dev, out, l_xprev - xo, raster,
2104
0
                        gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
2105
0
                if (code < 0)
2106
0
                    return code;
2107
0
            }
2108
            /* as above, see if we can accumulate any runs */
2109
0
            do {
2110
0
                rep++, p_cm_interp++;
2111
0
            } while ((rep + x) < xe && p_cm_interp[-1] == p_cm_interp[0]);
2112
0
            code = gx_fill_rectangle_device_rop(x, ry, rep, 1, &devc, dev, lop);
2113
0
            if (code < 0)
2114
0
                return code;
2115
0
            x += rep;
2116
0
            l_xprev = x;
2117
0
            l_dptr += rep;
2118
0
        }
2119
0
    }  /* End on x loop */
2120
0
    if (x > l_xprev) {
2121
0
        code = (*dev_proc(dev, copy_color))
2122
0
            (dev, out, l_xprev - xo, raster,
2123
0
                gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
2124
0
        if (code < 0)
2125
0
            return code;
2126
0
    }
2127
    /*if_debug1m('w', penum->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2128
0
    return 0;
2129
0
}
2130
2131
static int irii_inner_generic(gx_image_enum * penum, int xo, int xe, int spp_cm, unsigned short *p_cm_interp, gx_device *dev, int abs_interp_limit, int bpp, int raster, int yo, int dy, gs_logical_operation_t lop)
2132
0
{
2133
0
    return irii_inner_template(penum, xo, xe, spp_cm, p_cm_interp, dev, abs_interp_limit, bpp, raster, yo, dy, lop);
2134
0
}
2135
2136
/* Interpolation with ICC based source spaces. This is done seperately to
2137
   enable optimization and avoid the multiple tranformations that occur in
2138
   the above code */
2139
static int
2140
image_render_interpolate_icc(gx_image_enum * penum, const byte * buffer,
2141
                         int data_x, uint iw, int h, gx_device * dev)
2142
0
{
2143
0
    stream_image_scale_state *pss = penum->scaler;
2144
0
    const gs_gstate *pgs = penum->pgs;
2145
0
    gs_logical_operation_t lop = penum->log_op;
2146
0
    byte *out = penum->line;
2147
0
    stream_cursor_read stream_r;
2148
0
    stream_cursor_write stream_w;
2149
0
    int abs_interp_limit = pss->params.abs_interp_limit;
2150
0
    int limited_PatchWidthOut = (pss->params.PatchWidthOut + abs_interp_limit - 1) / abs_interp_limit;
2151
2152
0
    if (penum->icc_link == NULL) {
2153
0
        return gs_rethrow(-1, "ICC Link not created during gs_image_class_0_interpolate");
2154
0
    }
2155
0
    initial_decode(penum, buffer, data_x, h, &stream_r, true);
2156
    /*
2157
     * Process input and/or collect output.  By construction, the pixels are
2158
     * 1-for-1 with the device, but the Y coordinate might be inverted.
2159
     * CM is performed on the entire row.
2160
     */
2161
0
    {
2162
0
        int xo = penum->xyi.x;
2163
0
        int yo = penum->xyi.y;
2164
0
        int width = (pss->params.WidthOut + abs_interp_limit - 1) / abs_interp_limit;
2165
0
        int width_in = pss->params.WidthIn;
2166
0
        int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
2167
0
        int dy;
2168
0
        int bpp = dev->color_info.depth;
2169
0
        uint raster = bitmap_raster(width * bpp);
2170
0
        unsigned short *p_cm_interp;
2171
0
        byte *p_cm_buff = NULL;
2172
0
        byte *psrc;
2173
0
        int spp_decode = pss->params.spp_decode;
2174
0
        int spp_interp = pss->params.spp_interp;
2175
0
        int spp_cm;
2176
0
        gsicc_bufferdesc_t input_buff_desc;
2177
0
        gsicc_bufferdesc_t output_buff_desc;
2178
0
        int code;
2179
0
        cmm_dev_profile_t *dev_profile;
2180
0
        int num_bytes_decode = pss->params.BitsPerComponentIn / 8;
2181
0
        irii_core_fn irii_core;
2182
2183
0
        code = dev_proc(dev, get_profile)(dev, &dev_profile);
2184
0
        if (code)
2185
0
            return code;
2186
0
        spp_cm = gsicc_get_device_profile_comps(dev_profile);
2187
0
        if (penum->matrix.yy > 0)
2188
0
            dy = 1;
2189
0
        else
2190
0
            dy = -1, yo--;
2191
0
        if (spp_cm == 4 && abs_interp_limit == 1 && bpp == 32)
2192
0
            irii_core = &irii_inner_32bpp_4spp_1abs;
2193
0
        else if (spp_cm == 3 && abs_interp_limit == 1 && bpp == 24)
2194
0
            irii_core = &irii_inner_24bpp_3spp_1abs;
2195
0
        else if (spp_cm == 1 && abs_interp_limit == 1 && bpp == 8)
2196
0
            irii_core = &irii_inner_8bpp_1spp_1abs;
2197
0
        else
2198
0
            irii_core = &irii_inner_generic;
2199
2200
        /* If it makes sense (if enlarging), do early CM */
2201
0
        if (pss->params.early_cm && !penum->icc_link->is_identity
2202
0
            && stream_r.ptr != stream_r.limit) {
2203
            /* Get the buffers set up. */
2204
0
            p_cm_buff =
2205
0
                (byte *) gs_alloc_bytes(pgs->memory,
2206
0
                                        (size_t)num_bytes_decode * width_in * spp_cm,
2207
0
                                        "image_render_interpolate_icc");
2208
0
            if (p_cm_buff == NULL)
2209
0
                return_error(gs_error_VMerror);
2210
2211
            /* Set up the buffer descriptors. We keep the bytes the same */
2212
0
            gsicc_init_buffer(&input_buff_desc, spp_decode, num_bytes_decode,
2213
0
                          false, false, false, 0, width_in * spp_decode,
2214
0
                          1, width_in);
2215
0
            gsicc_init_buffer(&output_buff_desc, spp_cm, num_bytes_decode,
2216
0
                          false, false, false, 0, width_in * spp_cm,
2217
0
                          1, width_in);
2218
            /* Do the transformation */
2219
0
            psrc = (byte*) (stream_r.ptr + 1);
2220
0
            code = (penum->icc_link->procs.map_buffer)(dev, penum->icc_link, &input_buff_desc,
2221
0
                                                &output_buff_desc, (void*) psrc,
2222
0
                                                (void*) p_cm_buff);
2223
0
            if (code < 0)
2224
0
                return code;
2225
2226
            /* Re-set the reading stream to use the cm data */
2227
0
            stream_r.ptr = p_cm_buff - 1;
2228
0
            stream_r.limit = stream_r.ptr + num_bytes_decode * width_in * spp_cm;
2229
0
        } else {
2230
            /* CM after interpolation (or none).  Just set up the buffers
2231
               if needed.  16 bit operations if CM takes place.  */
2232
0
            if (!penum->icc_link->is_identity) {
2233
0
                p_cm_buff = (byte *) gs_alloc_bytes(pgs->memory,
2234
0
                    sizeof(unsigned short) * width * spp_cm,
2235
0
                    "image_render_interpolate_icc");
2236
0
                if (!p_cm_buff) {
2237
0
                    return gs_error_VMerror;
2238
0
                }
2239
                /* Set up the buffer descriptors. */
2240
0
                gsicc_init_buffer(&input_buff_desc, spp_decode, 2,
2241
0
                              false, false, false, 0, width * spp_decode,
2242
0
                              1, limited_PatchWidthOut);
2243
0
                gsicc_init_buffer(&output_buff_desc, spp_cm, 2,
2244
0
                              false, false, false, 0, width * spp_cm,
2245
0
                              1, limited_PatchWidthOut);
2246
0
            }
2247
0
        }
2248
0
        for (;;) {
2249
0
            const unsigned short *pinterp;
2250
0
            int status;
2251
2252
0
            stream_w.limit = out + pss->params.WidthOut *
2253
0
                max(spp_decode * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
2254
0
            stream_w.ptr = stream_w.limit - width * spp_interp * sizeofPixelOut;
2255
0
            pinterp = (const unsigned short *)(stream_w.ptr + 1);
2256
            /* This is where the rescale takes place; this will consume the
2257
             * data from stream_r, and post processed data into stream_w. The
2258
             * data in stream_w may be bogus if we are outside the active
2259
             * region, and this will be indiated by pss->params.Active being
2260
             * set to false. */
2261
0
            status = (*pss->templat->process)
2262
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
2263
0
            if (status < 0 && status != EOFC)
2264
0
                return_error(gs_error_ioerror);
2265
0
            if (stream_w.ptr == stream_w.limit) {
2266
0
                int xe = xo + limited_PatchWidthOut;
2267
2268
                /* Are we active? (i.e. in the render rectangle) */
2269
0
                if (!pss->params.Active)
2270
0
                    goto inactive;
2271
0
                if_debug1m('B', penum->memory, "[B]Interpolated row %d:\n[B]",
2272
0
                           penum->line_xy);
2273
                /* Take care of CM on the entire interpolated row, if we
2274
                   did not already do CM */
2275
0
                if (penum->icc_link->is_identity || pss->params.early_cm) {
2276
                    /* Fastest case. No CM needed */
2277
0
                    p_cm_interp = (unsigned short *) pinterp;
2278
0
                    p_cm_interp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_cm;
2279
0
                } else {
2280
                    /* Transform */
2281
0
                    pinterp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_decode;
2282
0
                    p_cm_interp = (unsigned short *) p_cm_buff;
2283
0
                    p_cm_interp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_cm;
2284
0
                    code = (penum->icc_link->procs.map_buffer)(dev, penum->icc_link,
2285
0
                                                        &input_buff_desc,
2286
0
                                                        &output_buff_desc,
2287
0
                                                        (void*) pinterp,
2288
0
                                                        (void*) p_cm_interp);
2289
0
                    if (code < 0)
2290
0
                        return code;
2291
0
                }
2292
0
                code = irii_core(penum, xo, xe, spp_cm, p_cm_interp, dev, abs_interp_limit, bpp, raster, yo, dy, lop);
2293
0
                if (code < 0)
2294
0
                    return code;
2295
0
inactive:
2296
0
                penum->line_xy++;
2297
0
                if (abs_interp_limit > 1) {
2298
0
                    dda_next(pss->params.scale_dda.y);
2299
0
                }
2300
0
                if_debug0m('B', penum->memory, "\n");
2301
0
            }
2302
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
2303
0
                break;
2304
0
        }
2305
        /* Free cm buffer, if it was used */
2306
0
        if (p_cm_buff != NULL) {
2307
0
            gs_free_object(pgs->memory, (byte *)p_cm_buff,
2308
0
                           "image_render_interpolate_icc");
2309
0
        }
2310
0
    }
2311
0
    return (h == 0 ? 0 : 1);
2312
0
}
2313
2314
static int
2315
image_render_interpolate_landscape(gx_image_enum * penum,
2316
                                   const byte * buffer,
2317
                                   int data_x, uint iw, int h,
2318
                                   gx_device * dev)
2319
0
{
2320
0
    stream_image_scale_state *pss = penum->scaler;
2321
0
    const gs_color_space *pcs = penum->pcs;
2322
0
    gs_logical_operation_t lop = penum->log_op;
2323
0
    int spp_decode = pss->params.spp_decode;
2324
0
    stream_cursor_read stream_r;
2325
0
    stream_cursor_write stream_w;
2326
0
    byte *out = penum->line;
2327
0
    bool islab = false;
2328
0
    int abs_interp_limit = pss->params.abs_interp_limit;
2329
0
    const gs_color_space *pconc;
2330
0
    cmm_dev_profile_t *dev_profile;
2331
0
    int code = dev_proc(penum->dev, get_profile)(penum->dev, &dev_profile);
2332
2333
0
    if (code < 0)
2334
0
        return code;
2335
2336
0
    if (pcs->cmm_icc_profile_data != NULL) {
2337
0
        islab = pcs->cmm_icc_profile_data->islab;
2338
0
    }
2339
    /* Perform any decode procedure if needed */
2340
0
    initial_decode(penum, buffer, data_x, h, &stream_r, false);
2341
    /*
2342
     * Process input and/or collect output.  By construction, the pixels are
2343
     * 1-for-1 with the device, but the Y coordinate might be inverted.
2344
     */
2345
0
    {
2346
0
        int xo = penum->xyi.y;
2347
0
        int yo = penum->xyi.x;
2348
0
        int width = (pss->params.WidthOut + abs_interp_limit - 1) / abs_interp_limit;
2349
0
        int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
2350
0
        int dy;
2351
0
        color_handler_fn *color_handler = NULL;
2352
2353
0
        if (penum->matrix.yx > 0)
2354
0
            dy = 1;
2355
0
        else
2356
0
            dy = -1, yo--;
2357
0
        for (;;) {
2358
0
            int ry = yo + penum->line_xy * dy;
2359
0
            int x;
2360
0
            const frac *psrc;
2361
0
            gx_device_color devc;
2362
0
            int status;
2363
0
            int scaled_w = 0;
2364
0
            gx_dda_fixed save_x_dda;
2365
2366
0
            if (abs_interp_limit > 1) {
2367
0
                save_x_dda = pss->params.scale_dda.x;
2368
0
            }
2369
0
            stream_w.limit = out + width *
2370
0
                max(spp_decode * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
2371
0
            stream_w.ptr = stream_w.limit - width * spp_decode * sizeofPixelOut;
2372
0
            psrc = (const frac *)(stream_w.ptr + 1);
2373
            /* This is where the rescale takes place; this will consume the
2374
             * data from stream_r, and post processed data into stream_w. The
2375
             * data in stream_w may be bogus if we are outside the active
2376
             * region, and this will be indicated by pss->params.Active being
2377
             * set to false. */
2378
0
            status = (*pss->templat->process)
2379
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
2380
0
            if (status < 0 && status != EOFC)
2381
0
                return_error(gs_error_ioerror);
2382
0
            if (stream_w.ptr == stream_w.limit) {
2383
0
                int xe = xo + (pss->params.PatchWidthOut + abs_interp_limit - 1) / abs_interp_limit;
2384
0
                int scaled_h = 0;
2385
0
                int scaled_y = 0;
2386
2387
0
                if (abs_interp_limit > 1) {
2388
0
                    scaled_h = interpolate_scaled_expanded_height(1, pss);
2389
0
                    scaled_y = yo + (dy * dda_current(pss->params.scale_dda.y)) -
2390
0
                               ((dy < 0) ? (scaled_h - 1) : 0);
2391
0
                }
2392
2393
                /* Are we active? (i.e. in the render rectangle) */
2394
0
                if (!pss->params.Active)
2395
0
                    goto inactive;
2396
0
                if_debug1m('B', penum->memory, "[B]Interpolated (rotated) row %d:\n[B]",
2397
0
                           penum->line_xy);
2398
0
                psrc += (pss->params.LeftMarginOut / abs_interp_limit) * spp_decode;
2399
0
                if (color_handler == NULL)
2400
0
                    color_handler = get_color_handler(penum, spp_decode, islab, dev_profile, &pconc);
2401
0
                for (x = xo; x < xe;) {
2402
0
                    if (color_handler != NULL) {
2403
#ifdef DEBUG
2404
                        if (gs_debug_c('B')) {
2405
                            int ci;
2406
2407
                            for (ci = 0; ci < spp_decode; ++ci)
2408
                                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
2409
                                          psrc[ci]);
2410
                        }
2411
#endif
2412
0
                        code = color_handler(penum, psrc, &devc, dev, dev_profile, pconc);
2413
0
                        if (code < 0)
2414
0
                            return code;
2415
0
                    }
2416
                    /* We scan for vertical runs of pixels, even if they end up
2417
                     * being split up in most cases within copy_color_unaligned anyway. */
2418
0
                    {
2419
0
                        int rcode;
2420
0
                        int rep = 0;
2421
2422
                        /* as above, see if we can accumulate any runs */
2423
0
                        switch (spp_decode) {
2424
0
                            case 1:
2425
0
                                do {
2426
0
                                    rep++, psrc += 1;
2427
0
                                } while ((rep + x) < xe &&
2428
0
                                         psrc[-1] == psrc[0]);
2429
0
                                break;
2430
0
                            case 3:
2431
0
                                do {
2432
0
                                    rep++, psrc += 3;
2433
0
                                } while ((rep + x) < xe &&
2434
0
                                         psrc[-3] == psrc[0] &&
2435
0
                                         psrc[-2] == psrc[1] &&
2436
0
                                         psrc[-1] == psrc[2]);
2437
0
                                break;
2438
0
                            case 4:
2439
0
                                do {
2440
0
                                    rep++, psrc += 4;
2441
0
                                } while ((rep + x) < xe &&
2442
0
                                         psrc[-4] == psrc[0] &&
2443
0
                                         psrc[-3] == psrc[1] &&
2444
0
                                         psrc[-2] == psrc[2] &&
2445
0
                                         psrc[-1] == psrc[3]);
2446
0
                                break;
2447
0
                            default:
2448
0
                                rep = 1;
2449
0
                                psrc += spp_decode;
2450
0
                                break;
2451
0
                        }
2452
0
                        if (abs_interp_limit <= 1) {
2453
0
                            rcode = gx_fill_rectangle_device_rop(ry, x, 1, rep, &devc, dev, lop);
2454
0
                            if (rcode < 0)
2455
0
                                return rcode;
2456
0
                        } else {
2457
0
                            int scaled_x = xo + dda_current(pss->params.scale_dda.x);
2458
2459
0
                            scaled_w = interpolate_scaled_expanded_width(rep, pss);
2460
0
                            rcode = gx_fill_rectangle_device_rop(scaled_y, scaled_x, scaled_h, scaled_w,
2461
0
                                                                 &devc, dev, lop);
2462
0
                            if (rcode < 0)
2463
0
                                return rcode;
2464
0
                            dda_advance(pss->params.scale_dda.x, rep);
2465
0
                        }
2466
0
                        x += rep;
2467
0
                    }
2468
0
                }
2469
                /*if_debug1m('w', dev->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2470
0
inactive:
2471
0
                penum->line_xy++;
2472
0
                if (abs_interp_limit > 1) {
2473
0
                    dda_next(pss->params.scale_dda.y);
2474
0
                    pss->params.scale_dda.x = save_x_dda; /* reset X to start of line */
2475
0
                }
2476
0
                if_debug0m('B', dev->memory, "\n");
2477
0
            }
2478
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
2479
0
                break;
2480
0
        }
2481
0
    }
2482
0
    return (h == 0 ? 0 : 1);
2483
0
}
2484
2485
static int
2486
image_render_interpolate_landscape_masked(gx_image_enum * penum,
2487
                                          const byte * buffer,
2488
                                          int data_x, uint iw, int h,
2489
                                          gx_device * dev)
2490
0
{
2491
0
    stream_image_scale_state *pss = penum->scaler;
2492
0
    int spp_decode = pss->params.spp_decode;
2493
0
    stream_cursor_read stream_r;
2494
0
    stream_cursor_write stream_w;
2495
0
    byte *out = penum->line;
2496
0
    gx_color_index color = penum->icolor1->colors.pure;
2497
2498
    /* Perform any decode procedure if needed. Probably only reversal
2499
     * of the data in this case. */
2500
0
    initial_decode(penum, buffer, data_x, h, &stream_r, false);
2501
    /*
2502
     * Process input and/or collect output.  By construction, the pixels are
2503
     * 1-for-1 with the device, but the Y coordinate might be inverted.
2504
     */
2505
0
    {
2506
0
        int xo = penum->xyi.y;
2507
0
        int yo = penum->xyi.x;
2508
0
        int width = pss->params.WidthOut;
2509
0
        int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
2510
0
        int dy;
2511
2512
0
        if (penum->matrix.yx > 0)
2513
0
            dy = 1;
2514
0
        else
2515
0
            dy = -1, yo--;
2516
0
        for (;;) {
2517
0
            int ry = yo + penum->line_xy * dy;
2518
0
            int x;
2519
0
            const byte *psrc;
2520
0
            int status, code;
2521
2522
0
            stream_w.limit = out + width *
2523
0
                max(spp_decode * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
2524
0
            stream_w.ptr = stream_w.limit - width * spp_decode * sizeofPixelOut;
2525
0
            psrc = stream_w.ptr + 1;
2526
            /* This is where the rescale takes place; this will consume the
2527
             * data from stream_r, and post processed data into stream_w. The
2528
             * data in stream_w may be bogus if we are outside the active
2529
             * region, and this will be indicated by pss->params.Active being
2530
             * set to false. */
2531
0
            status = (*pss->templat->process)
2532
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
2533
0
            if (status < 0 && status != EOFC)
2534
0
                return_error(gs_error_ioerror);
2535
0
            if (stream_w.ptr == stream_w.limit) {
2536
0
                int xe = xo + pss->params.PatchWidthOut;
2537
2538
                /* Are we active? (i.e. in the render rectangle) */
2539
0
                if (!pss->params.Active)
2540
0
                    goto inactive;
2541
0
                if_debug1m('B', penum->memory, "[B]Interpolated masked (rotated) row %d:\n[B]",
2542
0
                           penum->line_xy);
2543
0
                psrc += pss->params.LeftMarginOut * spp_decode;
2544
0
                for (x = xo; x < xe; x++) {
2545
0
                    code = (*dev_proc(dev, copy_alpha))(dev, psrc, 0, 0,
2546
0
                        gx_no_bitmap_id, ry, x, 1, 1, color, 8);
2547
0
                    if (code < 0)
2548
0
                        return code;
2549
0
                    psrc += spp_decode;
2550
0
                }
2551
                /*if_debug1m('w', dev->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2552
0
inactive:
2553
0
                penum->line_xy++;
2554
0
                if_debug0m('B', dev->memory, "\n");
2555
0
            }
2556
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
2557
0
                break;
2558
0
        }
2559
0
    }
2560
0
    return (h == 0 ? 0 : 1);
2561
0
}
2562
2563
static int
2564
image_render_interpolate_landscape_masked_hl(gx_image_enum * penum,
2565
                                             const byte * buffer,
2566
                                             int data_x, uint iw, int h,
2567
                                             gx_device * dev)
2568
0
{
2569
0
    stream_image_scale_state *pss = penum->scaler;
2570
0
    int spp_decode = pss->params.spp_decode;
2571
0
    stream_cursor_read stream_r;
2572
0
    stream_cursor_write stream_w;
2573
0
    byte *out = penum->line;
2574
2575
    /* Perform any decode procedure if needed. Probably only reversal
2576
     * of the data in this case. */
2577
0
    initial_decode(penum, buffer, data_x, h, &stream_r, false);
2578
    /*
2579
     * Process input and/or collect output.  By construction, the pixels are
2580
     * 1-for-1 with the device, but the Y coordinate might be inverted.
2581
     */
2582
0
    {
2583
0
        int xo = penum->xyi.y;
2584
0
        int yo = penum->xyi.x;
2585
0
        int width = pss->params.WidthOut;
2586
0
        int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
2587
0
        int dy;
2588
2589
0
        if (penum->matrix.yx > 0)
2590
0
            dy = 1;
2591
0
        else
2592
0
            dy = -1, yo--;
2593
0
        for (;;) {
2594
0
            int ry = yo + penum->line_xy * dy;
2595
0
            int x;
2596
0
            const byte *psrc;
2597
0
            int status, code;
2598
2599
0
            stream_w.limit = out + width *
2600
0
                max(spp_decode * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
2601
0
            stream_w.ptr = stream_w.limit - width * spp_decode * sizeofPixelOut;
2602
0
            psrc = stream_w.ptr + 1;
2603
            /* This is where the rescale takes place; this will consume the
2604
             * data from stream_r, and post processed data into stream_w. The
2605
             * data in stream_w may be bogus if we are outside the active
2606
             * region, and this will be indicated by pss->params.Active being
2607
             * set to false. */
2608
0
            status = (*pss->templat->process)
2609
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
2610
0
            if (status < 0 && status != EOFC)
2611
0
                return_error(gs_error_ioerror);
2612
0
            if (stream_w.ptr == stream_w.limit) {
2613
0
                int xe = xo + pss->params.PatchWidthOut;
2614
2615
                /* Are we active? (i.e. in the render rectangle) */
2616
0
                if (!pss->params.Active)
2617
0
                    goto inactive;
2618
0
                if_debug1m('B', penum->memory, "[B]Interpolated masked (rotated) row %d:\n[B]",
2619
0
                           penum->line_xy);
2620
0
                psrc += pss->params.LeftMarginOut * spp_decode;
2621
0
                for (x = xo; x < xe; x++) {
2622
0
                    code = (*dev_proc(dev, copy_alpha_hl_color))(dev, psrc, 0, 0,
2623
0
                        gx_no_bitmap_id, ry, x, 1, 1, penum->icolor1, 8);
2624
0
                    if (code < 0)
2625
0
                        return code;
2626
0
                    psrc += spp_decode;
2627
0
                }
2628
                /*if_debug1m('w', dev->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2629
0
inactive:
2630
0
                penum->line_xy++;
2631
0
                if_debug0m('B', dev->memory, "\n");
2632
0
            }
2633
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
2634
0
                break;
2635
0
        }
2636
0
    }
2637
0
    return (h == 0 ? 0 : 1);
2638
0
}
2639
2640
/* Interpolation with ICC based source spaces. This is done seperately to
2641
   enable optimization and avoid the multiple tranformations that occur in
2642
   the above code */
2643
static int
2644
image_render_interpolate_landscape_icc(gx_image_enum * penum,
2645
                                       const byte * buffer,
2646
                                       int data_x, uint iw, int h,
2647
                                       gx_device * dev)
2648
0
{
2649
0
    stream_image_scale_state *pss = penum->scaler;
2650
0
    const gs_gstate *pgs = penum->pgs;
2651
0
    gs_logical_operation_t lop = penum->log_op;
2652
0
    byte *out = penum->line;
2653
0
    stream_cursor_read stream_r;
2654
0
    stream_cursor_write stream_w;
2655
0
    int abs_interp_limit = pss->params.abs_interp_limit;
2656
0
    int limited_PatchWidthOut = (pss->params.PatchWidthOut + abs_interp_limit - 1) / abs_interp_limit;
2657
2658
0
    if (penum->icc_link == NULL) {
2659
0
        return gs_rethrow(-1, "ICC Link not created during gs_image_class_0_interpolate");
2660
0
    }
2661
0
    initial_decode(penum, buffer, data_x, h, &stream_r, true);
2662
    /*
2663
     * Process input and/or collect output.  By construction, the pixels are
2664
     * 1-for-1 with the device, but the Y coordinate might be inverted.
2665
     * CM is performed on the entire row.
2666
     */
2667
0
    {
2668
0
        int xo = penum->xyi.y;
2669
0
        int yo = penum->xyi.x;
2670
0
        int width = (pss->params.WidthOut + abs_interp_limit - 1) / abs_interp_limit;
2671
0
        int width_in = pss->params.WidthIn;
2672
0
        int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
2673
0
        int dy;
2674
0
        unsigned short *p_cm_interp;
2675
0
        byte *p_cm_buff = NULL;
2676
0
        byte *psrc;
2677
0
        int spp_decode = pss->params.spp_decode;
2678
0
        int spp_interp = pss->params.spp_interp;
2679
0
        int spp_cm;
2680
0
        gsicc_bufferdesc_t input_buff_desc;
2681
0
        gsicc_bufferdesc_t output_buff_desc;
2682
0
        gx_color_index color;
2683
0
        int code;
2684
0
        cmm_dev_profile_t *dev_profile;
2685
0
        int num_bytes_decode = pss->params.BitsPerComponentIn / 8;
2686
2687
0
        code = dev_proc(dev, get_profile)(dev, &dev_profile);
2688
0
        if (code) {
2689
0
            penum->interpolate = interp_off;
2690
0
            return 0;
2691
0
        }
2692
0
        spp_cm = gsicc_get_device_profile_comps(dev_profile);
2693
0
        if (penum->matrix.yx > 0)
2694
0
            dy = 1;
2695
0
        else
2696
0
            dy = -1, yo--;
2697
        /* If it makes sense (if enlarging), do early CM */
2698
0
        if (pss->params.early_cm && !penum->icc_link->is_identity
2699
0
            && stream_r.ptr != stream_r.limit) {
2700
            /* Get the buffers set up. */
2701
0
            p_cm_buff =
2702
0
                (byte *) gs_alloc_bytes(pgs->memory,
2703
0
                                        (size_t)num_bytes_decode * width_in * spp_cm,
2704
0
                                        "image_render_interpolate_icc");
2705
0
            if (p_cm_buff == NULL)
2706
0
                return_error(gs_error_VMerror);
2707
2708
            /* Set up the buffer descriptors. We keep the bytes the same */
2709
0
            gsicc_init_buffer(&input_buff_desc, spp_decode, num_bytes_decode,
2710
0
                          false, false, false, 0, width_in * spp_decode,
2711
0
                          1, width_in);
2712
0
            gsicc_init_buffer(&output_buff_desc, spp_cm, num_bytes_decode,
2713
0
                          false, false, false, 0, width_in * spp_cm,
2714
0
                          1, width_in);
2715
            /* Do the transformation */
2716
0
            psrc = (byte*) (stream_r.ptr + 1);
2717
0
            code = (penum->icc_link->procs.map_buffer)(dev, penum->icc_link, &input_buff_desc,
2718
0
                                                &output_buff_desc, (void*) psrc,
2719
0
                                                (void*) p_cm_buff);
2720
0
            if (code < 0)
2721
0
                return code;
2722
2723
            /* Re-set the reading stream to use the cm data */
2724
0
            stream_r.ptr = p_cm_buff - 1;
2725
0
            stream_r.limit = stream_r.ptr + num_bytes_decode * width_in * spp_cm;
2726
0
        } else {
2727
            /* CM after interpolation (or none).  Just set up the buffers
2728
               if needed.  16 bit operations if CM takes place.  */
2729
0
            if (!penum->icc_link->is_identity) {
2730
0
                p_cm_buff = (byte *) gs_alloc_bytes(pgs->memory,
2731
0
                    sizeof(unsigned short) * width * spp_cm,
2732
0
                    "image_render_interpolate_icc");
2733
0
                if (p_cm_buff == NULL)
2734
0
                    return_error(gs_error_VMerror);
2735
                /* Set up the buffer descriptors. */
2736
0
                gsicc_init_buffer(&input_buff_desc, spp_decode, 2,
2737
0
                              false, false, false, 0, width * spp_decode,
2738
0
                              1, limited_PatchWidthOut);
2739
0
                gsicc_init_buffer(&output_buff_desc, spp_cm, 2,
2740
0
                              false, false, false, 0, width * spp_cm,
2741
0
                              1, limited_PatchWidthOut);
2742
0
            }
2743
0
        }
2744
0
        for (;;) {
2745
0
            int ry = yo + penum->line_xy * dy;
2746
0
            int x;
2747
0
            const unsigned short *pinterp;
2748
0
            gx_device_color devc;
2749
0
            int status;
2750
0
            int scaled_w = 0;
2751
0
            gx_dda_fixed save_x_dda;
2752
2753
0
            if (abs_interp_limit > 1) {
2754
0
                save_x_dda = pss->params.scale_dda.x;
2755
0
            }
2756
0
            stream_w.limit = out + width *
2757
0
                max(spp_interp * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
2758
0
            stream_w.ptr = stream_w.limit - width * spp_interp * sizeofPixelOut;
2759
0
            pinterp = (const unsigned short *)(stream_w.ptr + 1);
2760
            /* This is where the rescale takes place; this will consume the
2761
             * data from stream_r, and post processed data into stream_w. The
2762
             * data in stream_w may be bogus if we are outside the active
2763
             * region, and this will be indiated by pss->params.Active being
2764
             * set to false. */
2765
0
            status = (*pss->templat->process)
2766
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
2767
0
            if (status < 0 && status != EOFC)
2768
0
                return_error(gs_error_ioerror);
2769
0
            if (stream_w.ptr == stream_w.limit) {
2770
0
                int xe = xo + (pss->params.PatchWidthOut + abs_interp_limit - 1) / abs_interp_limit;
2771
0
                int scaled_h = 0;
2772
0
                int scaled_y = 0;
2773
2774
0
                if (abs_interp_limit > 1) {
2775
0
                    scaled_h = interpolate_scaled_expanded_height(1, pss);
2776
0
                    scaled_y = yo + (dy * dda_current(pss->params.scale_dda.y)) -
2777
0
                               ((dy < 0) ? (scaled_h - 1) : 0);
2778
0
                }
2779
2780
                /* Are we active? (i.e. in the render rectangle) */
2781
0
                if (!pss->params.Active)
2782
0
                    goto inactive;
2783
0
                if_debug1m('B', penum->memory, "[B]Interpolated row %d:\n[B]",
2784
0
                           penum->line_xy);
2785
                /* Take care of CM on the entire interpolated row, if we
2786
                   did not already do CM */
2787
0
                if (penum->icc_link->is_identity || pss->params.early_cm) {
2788
                    /* Fastest case. No CM needed */
2789
0
                    p_cm_interp = (unsigned short *) pinterp;
2790
0
                    p_cm_interp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_cm;
2791
0
                } else {
2792
                    /* Transform */
2793
0
                    pinterp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_decode;
2794
0
                    p_cm_interp = (unsigned short *) p_cm_buff;
2795
0
                    p_cm_interp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_cm;
2796
0
                    code = (penum->icc_link->procs.map_buffer)(dev, penum->icc_link,
2797
0
                                                        &input_buff_desc,
2798
0
                                                        &output_buff_desc,
2799
0
                                                        (void*) pinterp,
2800
0
                                                        (void*) p_cm_interp);
2801
0
                    if (code < 0)
2802
0
                        return code;
2803
0
                }
2804
0
                for (x = xo; x < xe;) {
2805
#ifdef DEBUG
2806
                    if (gs_debug_c('B')) {
2807
                        int ci;
2808
2809
                        for (ci = 0; ci < spp_cm; ++ci)
2810
                            dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
2811
                                     p_cm_interp[ci]);
2812
                    }
2813
#endif
2814
                    /* Get the device color */
2815
0
                    get_device_color(penum, p_cm_interp, &devc, &color, dev);
2816
                    /* We scan for vertical runs of pixels, even if they end up
2817
                     * being split up in most cases within copy_color_unaligned anyway. */
2818
0
                    {
2819
0
                        int rcode;
2820
0
                        int rep = 0;
2821
2822
0
                        switch (spp_cm) {
2823
0
                            case 1:
2824
0
                                do {
2825
0
                                    rep++, p_cm_interp += 1;
2826
0
                                } while ((rep + x) < xe && p_cm_interp[-1] == p_cm_interp[0]);
2827
0
                                break;
2828
0
                            case 3:
2829
0
                                do {
2830
0
                                    rep++, p_cm_interp += 3;
2831
0
                                } while ((rep + x) < xe && p_cm_interp[-3] == p_cm_interp[0] &&
2832
0
                                     p_cm_interp[-2] == p_cm_interp[1] &&
2833
0
                                     p_cm_interp[-1] == p_cm_interp[2]);
2834
0
                                break;
2835
0
                            case 4:
2836
0
                                do {
2837
0
                                    rep++, p_cm_interp += 4;
2838
0
                                } while ((rep + x) < xe && p_cm_interp[-4] == p_cm_interp[0] &&
2839
0
                                     p_cm_interp[-3] == p_cm_interp[1] &&
2840
0
                                     p_cm_interp[-2] == p_cm_interp[2] &&
2841
0
                                     p_cm_interp[-1] == p_cm_interp[3]);
2842
0
                                break;
2843
0
                            default:
2844
0
                                rep = 1, p_cm_interp += spp_cm;
2845
0
                                break;
2846
0
                        }
2847
2848
0
                        if (abs_interp_limit <= 1) {
2849
0
                            rcode = gx_fill_rectangle_device_rop(ry, x, 1, rep, &devc, dev, lop);
2850
0
                            if (rcode < 0)
2851
0
                                return rcode;
2852
0
                        } else {
2853
0
                            int scaled_x = xo + dda_current(pss->params.scale_dda.x);
2854
2855
0
                            scaled_w = interpolate_scaled_expanded_width(rep, pss);
2856
0
                            rcode = gx_fill_rectangle_device_rop(scaled_y, scaled_x, scaled_h, scaled_w,
2857
0
                                                                 &devc, dev, lop);
2858
0
                            if (rcode < 0)
2859
0
                                return rcode;
2860
0
                            dda_advance(pss->params.scale_dda.x, rep);
2861
0
                        }
2862
0
                        x += rep;
2863
0
                    }
2864
0
                }  /* End on x loop */
2865
                /*if_debug1m('w', penum->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2866
0
inactive:
2867
0
                penum->line_xy++;
2868
0
                if (abs_interp_limit > 1) {
2869
0
                    dda_next(pss->params.scale_dda.y);
2870
0
                    pss->params.scale_dda.x = save_x_dda; /* reset X to start of line */
2871
0
                }
2872
0
                if_debug0m('B', penum->memory, "\n");
2873
0
            }
2874
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
2875
0
                break;
2876
0
        }
2877
        /* Free cm buffer, if it was used */
2878
0
        if (p_cm_buff != NULL) {
2879
0
            gs_free_object(pgs->memory, (byte *)p_cm_buff,
2880
0
                           "image_render_interpolate_icc");
2881
0
        }
2882
0
    }
2883
0
    return (h == 0 ? 0 : 1);
2884
0
}
2885
2886
/* Decode a 16-bit sample into a floating point color component.
2887
   This is used for cases where the spatial interpolation function output is 16 bit.
2888
   It is only used here, hence the static declaration for now. */
2889
2890
static void
2891
decode_sample_frac_to_float(gx_image_enum *penum, frac sample_value, gs_client_color *cc, int i)
2892
0
{
2893
0
    switch ( penum->map[i].decoding )
2894
0
    {
2895
0
        case sd_none:
2896
0
            cc->paint.values[i] = frac2float(sample_value);
2897
0
            break;
2898
0
        case sd_lookup:
2899
0
            cc->paint.values[i] =
2900
0
                   penum->map[i].decode_lookup[(frac2byte(sample_value)) >> 4];
2901
0
            break;
2902
0
        case sd_compute:
2903
0
            cc->paint.values[i] =
2904
0
                   penum->map[i].decode_base + frac2float(sample_value)*255.0 * penum->map[i].decode_factor;
2905
0
    }
2906
0
}