Coverage Report

Created: 2025-11-16 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gxiscale.c
Line
Count
Source
1
/* Copyright (C) 2001-2025 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* 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
40
{
186
40
    if (v > (int)((1U<<(sizeof(int)*8-1))-128) ||
187
40
        v < 0)
188
0
        *overflow = 1;
189
40
    return fixed2int_pixround_perfect(v);
190
40
}
191
192
int
193
gs_image_class_0_interpolate(gx_image_enum * penum, irender_proc_t *render_fn)
194
2.87M
{
195
2.87M
    gs_memory_t *mem = penum->memory;
196
2.87M
    stream_image_scale_params_t iss;
197
2.87M
    stream_image_scale_state *pss;
198
2.87M
    const stream_template *templat;
199
2.87M
    byte *line;
200
2.87M
    const gs_color_space *pcs = penum->pcs;
201
2.87M
    uint in_size;
202
2.87M
    bool use_icc = false;
203
2.87M
    int num_des_comps;
204
2.87M
    cmm_dev_profile_t *dev_profile;
205
2.87M
    int code;
206
2.87M
    gx_color_polarity_t pol = GX_CINFO_POLARITY_UNKNOWN;
207
2.87M
    int mask_col_high_level = 0;
208
2.87M
    int interpolate_control = penum->dev->interpolate_control;
209
2.87M
    int abs_interp_limit = max(1, any_abs(interpolate_control));
210
2.87M
    int limited_WidthOut, limited_HeightOut;
211
2.87M
    int overflow = 0;
212
213
2.87M
    if (interpolate_control < 0)
214
101
        penum->interpolate = interp_on;   /* not the same as "interp_force" -- threshold still used */
215
2.87M
    if (interpolate_control == interp_off || penum->interpolate == interp_off) {
216
2.87M
        penum->interpolate = interp_off;
217
2.87M
        return 0;
218
2.87M
    }
219
101
    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
63
    if (penum->use_mask_color ||
224
63
        (penum->posture != image_portrait &&
225
55
         penum->posture != image_landscape) ||
226
52
        penum->alpha) {
227
        /* We can't handle these cases yet.  Punt. */
228
52
        penum->interpolate = interp_off;
229
52
        return 0;
230
52
    }
231
11
    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
11
    if (penum->Width == 1 && penum->Height == 1) {
236
0
        penum->interpolate = interp_off; /* No need to interpolate */
237
0
        return 0;
238
0
    }
239
11
    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
11
    if (penum->x_extent.x == INT_MIN || penum->x_extent.x == INT_MAX ||
245
11
        penum->x_extent.y == INT_MIN || penum->x_extent.y == INT_MAX ||
246
10
        penum->y_extent.x == INT_MIN || penum->y_extent.x == INT_MAX ||
247
10
        penum->y_extent.y == INT_MIN || penum->y_extent.y == INT_MAX)
248
8
    {
249
        /* A calculation has overflowed. Bail */
250
8
        return 0;
251
8
    }
252
253
3
    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
3
    } else {
262
3
        if (pcs == NULL)
263
0
            return 0;   /* can't handle this */
264
3
        if (pcs->cmm_icc_profile_data != NULL) {
265
3
            use_icc = true;
266
3
        }
267
3
        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
3
        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
3
        code = dev_proc(penum->dev, get_profile)(penum->dev, &dev_profile);
279
3
        if (code) {
280
0
            penum->interpolate = interp_off;
281
0
            return 0;
282
0
        }
283
3
        num_des_comps = gsicc_get_device_profile_comps(dev_profile);
284
3
        if (num_des_comps != penum->dev->color_info.num_components ||
285
3
            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
3
        if(!gx_device_uses_std_cmap_procs(penum->dev, penum->pgs)) {
293
0
            use_icc = false;
294
0
        }
295
3
        pol = cs_polarity(pcs);
296
3
    }
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
3
    iss.abs_interp_limit = abs_interp_limit;
321
3
    if (penum->masked) {
322
0
        iss.BitsPerComponentOut = 8;
323
0
        iss.MaxValueOut = 0xff;
324
3
    } else if (use_icc) {
325
3
        iss.BitsPerComponentOut = 16;
326
3
        iss.MaxValueOut = 0xffff;
327
3
    } else {
328
0
        iss.BitsPerComponentOut = sizeof(frac) * 8;
329
0
        iss.MaxValueOut = frac_1;
330
0
    }
331
3
    iss.PatchWidthIn = penum->drect.w;
332
3
    iss.PatchHeightIn = penum->drect.h;
333
3
    iss.LeftMarginIn = penum->drect.x - penum->rect.x;
334
3
    iss.TopMarginIn = penum->drect.y - penum->rect.y;
335
3
    if (penum->posture == image_portrait) {
336
1
        fixed dw = any_abs(penum->dst_width);
337
1
        fixed dh = any_abs(penum->dst_height);
338
1
        iss.WidthOut = safe64fixed2int((int64_t)(penum->rect.x + penum->rect.w) *
339
1
                                       dw / penum->Width, &overflow)
340
1
                     - safe64fixed2int((int64_t)penum->rect.x *
341
1
                                       dw / penum->Width, &overflow);
342
1
        iss.HeightOut = safe64fixed2int((int64_t)(penum->rect.y + penum->rect.h) *
343
1
                                        dh / penum->Height, &overflow)
344
1
                      - safe64fixed2int((int64_t)penum->rect.y *
345
1
                                        dh / penum->Height, &overflow);
346
1
        iss.EntireWidthOut = safe64fixed2int(dw, &overflow);
347
1
        iss.EntireHeightOut = safe64fixed2int(dh, &overflow);
348
1
        iss.TopMarginOut = safe64fixed2int((int64_t)(penum->rrect.y - penum->rect.y) *
349
1
                                           dh / penum->Height, &overflow);
350
1
        iss.PatchHeightOut = safe64fixed2int((int64_t)penum->rrect.h *
351
1
                                             dh / penum->Height, &overflow)
352
1
                           - iss.TopMarginOut;
353
1
        iss.PatchWidthOut = safe64fixed2int((int64_t)(penum->rrect.x + penum->rrect.w) *
354
1
                                            dw / penum->Width, &overflow)
355
1
                          - safe64fixed2int((int64_t)penum->rrect.x *
356
1
                                            dw / penum->Width, &overflow);
357
1
        iss.LeftMarginOut = safe64fixed2int((int64_t)(penum->rrect.x - penum->rect.x) *
358
1
                                            dw / penum->Width, &overflow);
359
1
        iss.TopMarginOut2 = safe64fixed2int((int64_t)penum->rrect.y *
360
1
                                            dh / penum->Height, &overflow);
361
1
        iss.PatchHeightOut2 = safe64fixed2int((int64_t)(penum->rrect.y + penum->rrect.h) *
362
1
                                              dh / penum->Height, &overflow)
363
1
                           - iss.TopMarginOut2;
364
1
        iss.pad_y = iss.TopMarginOut2
365
1
                  - safe64fixed2int((int64_t)penum->rect.y *
366
1
                                    dh / penum->Height, &overflow);
367
2
    } else {
368
2
        fixed dw = any_abs(penum->dst_width);
369
2
        fixed dh = any_abs(penum->dst_height);
370
2
        iss.WidthOut = safe64fixed2int((int64_t)(penum->rect.x + penum->rect.w) *
371
2
                                       dh / penum->Width, &overflow)
372
2
                     - safe64fixed2int((int64_t)penum->rect.x *
373
2
                                       dh / penum->Width, &overflow);
374
2
        iss.HeightOut = safe64fixed2int((int64_t)(penum->rect.y + penum->rect.h) *
375
2
                                        dw / penum->Height, &overflow)
376
2
                      - safe64fixed2int((int64_t)penum->rect.y *
377
2
                                        dw / penum->Height, &overflow);
378
2
        iss.EntireWidthOut = safe64fixed2int(dh, &overflow);
379
2
        iss.EntireHeightOut = safe64fixed2int(dw, &overflow);
380
2
        iss.TopMarginOut = safe64fixed2int((int64_t)(penum->rrect.y - penum->rect.y) *
381
2
                                           dw / penum->Height, &overflow);
382
2
        iss.PatchHeightOut = safe64fixed2int((int64_t)penum->rrect.h *
383
2
                                             dw / penum->Height, &overflow)
384
2
                           - iss.TopMarginOut;
385
2
        iss.PatchWidthOut = safe64fixed2int((int64_t)(penum->rrect.x + penum->rrect.w) *
386
2
                                            dh / penum->Width, &overflow)
387
2
                          - safe64fixed2int((int64_t)penum->rrect.x *
388
2
                                            dh / penum->Width, &overflow);
389
2
        iss.LeftMarginOut = safe64fixed2int((int64_t)(penum->rrect.x - penum->rect.x) *
390
2
                                            dh / penum->Width, &overflow);
391
2
        iss.TopMarginOut2 = safe64fixed2int((int64_t)penum->rect.y *
392
2
                                            dw / penum->Height, &overflow);
393
2
        iss.PatchHeightOut2 = safe64fixed2int((int64_t)(penum->rrect.y + penum->rrect.h) *
394
2
                                              dw / penum->Height, &overflow)
395
2
                           - iss.TopMarginOut2;
396
2
        iss.pad_y = 0;
397
2
    }
398
3
    iss.PatchWidthOut = any_abs(iss.PatchWidthOut);
399
3
    if (iss.LeftMarginOut + iss.PatchWidthOut >= iss.WidthOut) {
400
3
        iss.LeftMarginOut = iss.WidthOut - iss.PatchWidthOut;
401
3
        if (iss.LeftMarginOut < 0) {
402
0
            iss.WidthOut += iss.LeftMarginOut;
403
0
            iss.LeftMarginOut = 0;
404
0
        }
405
3
    }
406
3
    iss.src_y_offset = penum->rect.y;
407
3
    iss.EntireWidthIn = penum->Width;
408
3
    iss.EntireHeightIn = penum->Height;
409
3
    iss.WidthIn = penum->rect.w;
410
3
    iss.HeightIn = penum->rect.h;
411
3
    iss.WidthOut = any_abs(iss.WidthOut);
412
3
    iss.HeightOut = any_abs(iss.HeightOut);
413
3
    limited_WidthOut = (iss.WidthOut + abs_interp_limit - 1)/abs_interp_limit;
414
3
    limited_HeightOut = (iss.HeightOut + abs_interp_limit - 1)/abs_interp_limit;
415
3
    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
3
    iss.Active = 1;
419
3
    if (iss.EntireWidthOut == 0 || iss.EntireHeightOut == 0 || overflow)
420
3
    {
421
3
        penum->interpolate = interp_off;
422
3
        return 0;
423
3
    }
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.tag = device_current_tag(dev);
1252
0
            devc.type = gx_dc_type_none; /* Needed for coverity, in call to color_is_pure() if color_handler is NULL. */
1253
0
            stream_w.limit = out + pss->params.WidthOut *
1254
0
                max(spp_decode * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
1255
0
            stream_w.ptr = stream_w.limit - width * spp_decode * sizeofPixelOut;
1256
0
            psrc = (const frac *)(stream_w.ptr + 1);
1257
            /* This is where the rescale takes place; this will consume the
1258
             * data from stream_r, and post processed data into stream_w. The
1259
             * data in stream_w may be bogus if we are outside the active
1260
             * region, and this will be indicated by pss->params.Active being
1261
             * set to false. */
1262
0
            status = (*pss->templat->process)
1263
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
1264
0
            if (status < 0 && status != EOFC)
1265
0
                return_error(gs_error_ioerror);
1266
0
            if (stream_w.ptr == stream_w.limit) {
1267
0
                int xe = xo + limited_PatchWidthOut;
1268
0
                int scaled_w = 0;   /* accumulate scaled up width */
1269
0
                int scaled_h = 0;
1270
0
                int scaled_y = 0;
1271
1272
0
                if (abs_interp_limit > 1) {
1273
0
                    scaled_h = interpolate_scaled_expanded_height(1, pss);
1274
0
                    scaled_y = yo + (dy * dda_current(pss->params.scale_dda.y));
1275
0
                }
1276
1277
                /* Are we active? (i.e. in the render rectangle) */
1278
0
                if (!pss->params.Active)
1279
0
                    goto inactive;
1280
0
                if_debug1m('B', penum->memory, "[B]Interpolated row %d:\n[B]",
1281
0
                           penum->line_xy);
1282
0
                psrc += ((pss->params.LeftMarginOut + abs_interp_limit - 1) / abs_interp_limit) * spp_decode;
1283
1284
0
                if (color_handler == NULL)
1285
0
                    color_handler = get_color_handler(penum, spp_decode, islab, dev_profile, &pconc);
1286
0
                for (x = xo; x < xe;) {
1287
0
                    if (color_handler != NULL) {
1288
#ifdef DEBUG
1289
                        if (gs_debug_c('B')) {
1290
                            int ci;
1291
1292
                            for (ci = 0; ci < spp_decode; ++ci)
1293
                                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
1294
                                          psrc[ci]);
1295
                        }
1296
#endif
1297
0
                        code = color_handler(penum, psrc, &devc, dev, dev_profile, pconc);
1298
0
                        if (code < 0)
1299
0
                            return code;
1300
0
                    }
1301
0
                    if (color_is_pure(&devc)) {
1302
0
                        gx_color_index color = devc.colors.pure;
1303
0
                        int expand = 1;
1304
1305
0
                        if (abs_interp_limit > 1) {
1306
0
                            expand = interpolate_scaled_expanded_width(1, pss);
1307
0
                        }
1308
                        /* Just pack colors into a scan line. */
1309
                        /* Skip runs quickly for the common cases. */
1310
0
                        switch (spp_decode) {
1311
0
                            case 1:
1312
0
                                do {
1313
0
                                    scaled_w += expand;
1314
0
                                    while (expand-- > 0) {
1315
0
                                        if (sizeof(color) > 4) {
1316
0
                                            if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1317
0
                                                return_error(gs_error_rangecheck);
1318
0
                                        }
1319
0
                                        else {
1320
0
                                            if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1321
0
                                                return_error(gs_error_rangecheck);
1322
0
                                        }
1323
0
                                    };
1324
0
                                    x++, psrc += 1;
1325
0
                                    if (abs_interp_limit > 1) {
1326
0
                                        dda_next(pss->params.scale_dda.x);
1327
0
                                        expand = interpolate_scaled_expanded_width(1, pss);
1328
0
                                    } else
1329
0
                                        expand = 1;
1330
0
                                } while (x < xe && psrc[-1] == psrc[0]);
1331
0
                                break;
1332
0
                            case 3:
1333
0
                                do {
1334
0
                                    scaled_w += expand;
1335
0
                                    while (expand-- > 0) {
1336
0
                                        if (sizeof(color) > 4) {
1337
0
                                            if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1338
0
                                                return_error(gs_error_rangecheck);
1339
0
                                        }
1340
0
                                        else {
1341
0
                                            if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1342
0
                                                return_error(gs_error_rangecheck);
1343
0
                                        }
1344
0
                                    };
1345
0
                                    x++, psrc += 3;
1346
0
                                    if (abs_interp_limit > 1) {
1347
0
                                        dda_next(pss->params.scale_dda.x);
1348
0
                                        expand = interpolate_scaled_expanded_width(1, pss);
1349
0
                                    } else
1350
0
                                        expand = 1;
1351
0
                                } while (x < xe &&
1352
0
                                         psrc[-3] == psrc[0] &&
1353
0
                                         psrc[-2] == psrc[1] &&
1354
0
                                         psrc[-1] == psrc[2]);
1355
0
                                break;
1356
0
                            case 4:
1357
0
                                do {
1358
0
                                    scaled_w += expand;
1359
0
                                    while (expand-- > 0) {
1360
0
                                        if (sizeof(color) > 4) {
1361
0
                                            if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1362
0
                                                return_error(gs_error_rangecheck);
1363
0
                                        }
1364
0
                                        else {
1365
0
                                            if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1366
0
                                                return_error(gs_error_rangecheck);
1367
0
                                        }
1368
0
                                    };
1369
0
                                    x++, psrc += 4;
1370
0
                                    if (abs_interp_limit > 1) {
1371
0
                                        dda_next(pss->params.scale_dda.x);
1372
0
                                        expand = interpolate_scaled_expanded_width(1, pss);
1373
0
                                    } else
1374
0
                                        expand = 1;
1375
0
                                } while (x < xe &&
1376
0
                                         psrc[-4] == psrc[0] &&
1377
0
                                         psrc[-3] == psrc[1] &&
1378
0
                                         psrc[-2] == psrc[2] &&
1379
0
                                         psrc[-1] == psrc[3]);
1380
0
                                break;
1381
0
                            default:  /* no run length check for these spp cases */
1382
0
                                scaled_w += expand;
1383
0
                                while (expand-- > 0) {
1384
0
                                    if (sizeof(color) > 4) {
1385
0
                                        if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1386
0
                                            return_error(gs_error_rangecheck);
1387
0
                                    }
1388
0
                                    else {
1389
0
                                        if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1390
0
                                            return_error(gs_error_rangecheck);
1391
0
                                    }
1392
0
                                };
1393
0
                                x++, psrc += spp_decode;
1394
0
                                if (abs_interp_limit > 1)
1395
0
                                    dda_next(pss->params.scale_dda.x);
1396
0
                        }
1397
0
                    } else {
1398
0
                        int rcode, rep = 0;
1399
1400
                        /* do _COPY in case any pure colors were accumulated above */
1401
0
                        if ( x > l_xprev ) {
1402
0
                            sample_store_flush(l_dptr, l_dbit, l_dbyte);
1403
0
                            if (abs_interp_limit <= 1) {
1404
0
                                code = (*dev_proc(dev, copy_color))
1405
0
                                  (dev, out, l_xprev - xo, raster,
1406
0
                                   gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1407
0
                                if (code < 0)
1408
0
                                    return code;
1409
0
                            } else {
1410
                                /* scale up in X and Y */
1411
0
                                int scaled_x = xo + scaled_x_prev;
1412
0
                                int i = scaled_h;
1413
0
                                int iy = scaled_y;
1414
1415
0
                                for (; i > 0; --i) {
1416
0
                                    code = (*dev_proc(dev, copy_color))
1417
0
                                      (dev, out, scaled_x_prev, raster,
1418
0
                                       gx_no_bitmap_id, scaled_x, iy, scaled_w, 1);
1419
0
                                    if (code < 0)
1420
0
                                        return code;
1421
0
                                    iy += dy;
1422
0
                                }
1423
0
                                scaled_x_prev = dda_current(pss->params.scale_dda.x);
1424
0
                            }
1425
0
                        }
1426
                        /* as above, see if we can accumulate any runs */
1427
0
                        switch (spp_decode) {
1428
0
                            case 1:
1429
0
                                do {
1430
0
                                    rep++, psrc += 1;
1431
0
                                } while ((rep + x) < xe &&
1432
0
                                         psrc[-1] == psrc[0]);
1433
0
                                break;
1434
0
                            case 3:
1435
0
                                do {
1436
0
                                    rep++, psrc += 3;
1437
0
                                } while ((rep + x) < xe &&
1438
0
                                         psrc[-3] == psrc[0] &&
1439
0
                                         psrc[-2] == psrc[1] &&
1440
0
                                         psrc[-1] == psrc[2]);
1441
0
                                break;
1442
0
                            case 4:
1443
0
                                do {
1444
0
                                    rep++, psrc += 4;
1445
0
                                } while ((rep + x) < xe &&
1446
0
                                         psrc[-4] == psrc[0] &&
1447
0
                                         psrc[-3] == psrc[1] &&
1448
0
                                         psrc[-2] == psrc[2] &&
1449
0
                                         psrc[-1] == psrc[3]);
1450
0
                                break;
1451
0
                            default:
1452
0
                                rep = 1;
1453
0
                                psrc += spp_decode;
1454
0
                                break;
1455
0
                        }
1456
0
                        if (abs_interp_limit <= 1) {
1457
0
                            scaled_w = rep;
1458
0
                            rcode = gx_fill_rectangle_device_rop(x, ry, rep, 1, &devc, dev, lop);
1459
0
                            if (rcode < 0)
1460
0
                                return rcode;
1461
0
                        } else {
1462
0
                            int scaled_x = xo + scaled_x_prev;
1463
1464
0
                            scaled_w = interpolate_scaled_expanded_width(rep, pss);
1465
0
                            rcode = gx_fill_rectangle_device_rop(scaled_x, scaled_y, scaled_w, scaled_h,
1466
0
                                                                 &devc, dev, lop);
1467
0
                            if (rcode < 0)
1468
0
                                return rcode;
1469
0
                            dda_advance(pss->params.scale_dda.x, rep);
1470
0
                            scaled_x_prev = dda_current(pss->params.scale_dda.x);
1471
0
                        }
1472
0
                        while (scaled_w-- > 0)
1473
0
                            sample_store_skip_next(&l_dptr, &l_dbit, bpp, &l_dbyte);
1474
0
                        scaled_w = 0;
1475
0
                        l_xprev = x + rep;
1476
0
                        x += rep;
1477
0
                    }
1478
0
                }  /* End on x loop */
1479
0
                if ( x > l_xprev ) {
1480
0
                    sample_store_flush(l_dptr, l_dbit, l_dbyte);
1481
0
                    if (abs_interp_limit <= 1) {
1482
0
                        code = (*dev_proc(dev, copy_color))
1483
0
                          (dev, out, l_xprev - xo, raster,
1484
0
                           gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1485
0
                        if (code < 0)
1486
0
                            return code;
1487
0
                    } else {
1488
                        /* scale up in X and Y */
1489
0
                        int scaled_x = xo + scaled_x_prev;
1490
1491
0
                        for (; scaled_h > 0; --scaled_h) {
1492
0
                            code = (*dev_proc(dev, copy_color))
1493
0
                              (dev, out, scaled_x_prev, raster,
1494
0
                               gx_no_bitmap_id, scaled_x, scaled_y, scaled_w, 1);
1495
0
                            if (code < 0)
1496
0
                                return code;
1497
0
                            scaled_y += dy;
1498
0
                        }
1499
0
                    }
1500
0
                }
1501
                /*if_debug1m('w', dev->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
1502
0
inactive:
1503
0
                penum->line_xy++;
1504
0
                if (abs_interp_limit > 1) {
1505
0
                    dda_next(pss->params.scale_dda.y);
1506
0
                    pss->params.scale_dda.x = save_x_dda; /* reset X to start of line */
1507
0
                }
1508
0
                if_debug0m('B', dev->memory, "\n");
1509
0
            }
1510
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
1511
0
                break;
1512
0
        }
1513
0
    }
1514
0
    return (h == 0 ? 0 : 1);
1515
0
}
1516
1517
static int
1518
image_render_interpolate_masked(gx_image_enum * penum, const byte * buffer,
1519
                                int data_x, uint iw, int h, gx_device * dev)
1520
0
{
1521
0
    stream_image_scale_state *pss = penum->scaler;
1522
0
    stream_cursor_read stream_r;
1523
0
    stream_cursor_write stream_w;
1524
0
    byte *out = penum->line;
1525
0
    gx_color_index color = penum->icolor1->colors.pure;
1526
1527
    /* Perform any decode procedure if needed */
1528
0
    initial_decode(penum, buffer, data_x, h, &stream_r, false);
1529
    /*
1530
     * Process input and/or collect output.  By construction, the pixels are
1531
     * 1-for-1 with the device, but the Y coordinate might be inverted.
1532
     */
1533
0
    {
1534
0
        int xo = penum->xyi.x;
1535
0
        int yo = penum->xyi.y;
1536
0
        int width = pss->params.WidthOut;
1537
0
        int dy;
1538
0
        int bpp = dev->color_info.depth;
1539
0
        uint raster = bitmap_raster(width * bpp);
1540
1541
0
        if (penum->matrix.yy > 0)
1542
0
            dy = 1;
1543
0
        else
1544
0
            dy = -1, yo--;
1545
0
        for (;;) {
1546
0
            int ry = yo + penum->line_xy * dy;
1547
0
            const byte *psrc;
1548
0
            int status, code;
1549
1550
0
            stream_w.limit = out + width - 1;
1551
0
            stream_w.ptr = stream_w.limit - width;
1552
0
            psrc = stream_w.ptr + 1;
1553
            /* This is where the rescale takes place; this will consume the
1554
             * data from stream_r, and post processed data into stream_w. The
1555
             * data in stream_w may be bogus if we are outside the active
1556
             * region, and this will be indicated by pss->params.Active being
1557
             * set to false. */
1558
0
            status = (*pss->templat->process)
1559
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
1560
0
            if (status < 0 && status != EOFC)
1561
0
                return_error(gs_error_ioerror);
1562
0
            if (stream_w.ptr == stream_w.limit) {
1563
0
                int xe = xo + pss->params.PatchWidthOut;
1564
1565
                /* Are we active? (i.e. in the render rectangle) */
1566
0
                if (!pss->params.Active)
1567
0
                    goto inactive;
1568
0
                if_debug1m('B', penum->memory, "[B]Interpolated mask row %d:\n[B]",
1569
0
                           penum->line_xy);
1570
0
                psrc += pss->params.LeftMarginOut;
1571
0
                code = (*dev_proc(dev, copy_alpha))
1572
0
                            (dev, psrc, 0, raster,
1573
0
                             gx_no_bitmap_id, xo, ry, xe-xo, 1,
1574
0
                             color, 8);
1575
0
                if ( code < 0 )
1576
0
                    return code;
1577
0
inactive:
1578
0
                penum->line_xy++;
1579
0
                if_debug0m('B', dev->memory, "\n");
1580
0
            }
1581
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
1582
0
                break;
1583
0
        }
1584
0
    }
1585
0
    return (h == 0 ? 0 : 1);
1586
0
}
1587
1588
static int
1589
image_render_interpolate_masked_hl(gx_image_enum * penum, const byte * buffer,
1590
                                   int data_x, uint iw, int h, gx_device * dev)
1591
0
{
1592
0
    stream_image_scale_state *pss = penum->scaler;
1593
0
    stream_cursor_read stream_r;
1594
0
    stream_cursor_write stream_w;
1595
0
    byte *out = penum->line;
1596
1597
    /* Perform any decode procedure if needed */
1598
0
    initial_decode(penum, buffer, data_x, h, &stream_r, false);
1599
    /*
1600
     * Process input and/or collect output.  By construction, the pixels are
1601
     * 1-for-1 with the device, but the Y coordinate might be inverted.
1602
     */
1603
0
    {
1604
0
        int xo = penum->xyi.x;
1605
0
        int yo = penum->xyi.y;
1606
0
        int width = pss->params.WidthOut;
1607
0
        int dy;
1608
0
        int bpp = dev->color_info.depth;
1609
0
        uint raster = bitmap_raster(width * bpp);
1610
1611
0
        if (penum->matrix.yy > 0)
1612
0
            dy = 1;
1613
0
        else
1614
0
            dy = -1, yo--;
1615
0
        for (;;) {
1616
0
            int ry = yo + penum->line_xy * dy;
1617
0
            const byte *psrc;
1618
0
            int status, code;
1619
1620
0
            stream_w.limit = out + width - 1;
1621
0
            stream_w.ptr = stream_w.limit - width;
1622
0
            psrc = stream_w.ptr + 1;
1623
            /* This is where the rescale takes place; this will consume the
1624
             * data from stream_r, and post processed data into stream_w. The
1625
             * data in stream_w may be bogus if we are outside the active
1626
             * region, and this will be indicated by pss->params.Active being
1627
             * set to false. */
1628
0
            status = (*pss->templat->process)
1629
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
1630
0
            if (status < 0 && status != EOFC)
1631
0
                return_error(gs_error_ioerror);
1632
0
            if (stream_w.ptr == stream_w.limit) {
1633
0
                int xe = xo + pss->params.PatchWidthOut;
1634
1635
                /* Are we active? (i.e. in the render rectangle) */
1636
0
                if (!pss->params.Active)
1637
0
                    goto inactive;
1638
0
                if_debug1m('B', penum->memory, "[B]Interpolated mask row %d:\n[B]",
1639
0
                           penum->line_xy);
1640
0
                psrc += pss->params.LeftMarginOut;
1641
0
                code = (*dev_proc(dev, copy_alpha_hl_color))
1642
0
                            (dev, psrc, 0, raster,
1643
0
                             gx_no_bitmap_id, xo, ry, xe-xo, 1,
1644
0
                             penum->icolor1, 8);
1645
0
                if ( code < 0 )
1646
0
                    return code;
1647
0
inactive:
1648
0
                penum->line_xy++;
1649
0
                if_debug0m('B', dev->memory, "\n");
1650
0
            }
1651
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
1652
0
                break;
1653
0
        }
1654
0
    }
1655
0
    return (h == 0 ? 0 : 1);
1656
0
}
1657
1658
static void
1659
get_device_color(gx_image_enum * penum, unsigned short *p_cm_interp,
1660
gx_device_color *devc, gx_color_index *color, gx_device * dev)
1661
0
{
1662
0
    bool must_halftone = penum->icc_setup.must_halftone;
1663
0
    bool has_transfer = penum->icc_setup.has_transfer;
1664
1665
0
    if (must_halftone || has_transfer) {
1666
        /* We need to do the tranfer function and/or the halftoning */
1667
0
        cmap_transfer_halftone(p_cm_interp, devc, penum->pgs, dev,
1668
0
            has_transfer, must_halftone, gs_color_select_source);
1669
0
    } else {
1670
        /* encode as a color index. avoid all the cv to frac to cv conversions */
1671
0
        *color = dev_proc(dev, encode_color)(dev, p_cm_interp);
1672
        /* check if the encoding was successful; we presume failure is rare */
1673
0
        if (*color != gx_no_color_index)
1674
0
            color_set_pure(devc, *color);
1675
0
    }
1676
0
}
1677
1678
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);
1679
1680
static inline int
1681
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)
1682
0
{
1683
0
    int x;
1684
0
    gx_device_color devc;
1685
0
    gx_color_index color;
1686
0
    stream_image_scale_state *pss = penum->scaler;
1687
0
    int scaled_w = 0;   /* accumulate scaled up width */
1688
0
    byte *out = penum->line;
1689
0
    byte *l_dptr = out;
1690
0
    int l_dbit = 0;
1691
0
    byte l_dbyte = 0;
1692
0
    int l_xprev = (xo);
1693
0
    int scaled_x_prev = 0;
1694
0
    int code;
1695
0
    int ry = yo + penum->line_xy * dy;
1696
0
    gx_dda_fixed save_x_dda = pss->params.scale_dda.x;
1697
0
    int scaled_h = 0;
1698
0
    int scaled_y = 0;
1699
1700
0
    devc.tag = device_current_tag(dev);
1701
1702
0
    if (abs_interp_limit > 1) {
1703
0
        scaled_h = interpolate_scaled_expanded_height(1, pss);
1704
0
        scaled_y = yo + (dy * dda_current(pss->params.scale_dda.y));
1705
0
    }
1706
1707
0
    for (x = xo; x < xe;) {
1708
1709
#ifdef DEBUG
1710
        if (gs_debug_c('B')) {
1711
            int ci;
1712
1713
            for (ci = 0; ci < spp_cm; ++ci)
1714
                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
1715
                          p_cm_interp[ci]);
1716
        }
1717
#endif
1718
        /* Get the device color */
1719
0
        get_device_color(penum, p_cm_interp, &devc, &color, dev);
1720
0
        if (color_is_pure(&devc)) {
1721
0
            gx_color_index color = devc.colors.pure;
1722
0
            int expand = 1;
1723
1724
0
            if (abs_interp_limit > 1) {
1725
0
                expand = interpolate_scaled_expanded_width(1, pss);
1726
0
            }
1727
            /* Just pack colors into a scan line. */
1728
            /* Skip runs quickly for the common cases. */
1729
0
            switch (spp_cm) {
1730
0
                case 1:
1731
0
                    do {
1732
0
                        scaled_w += expand;
1733
0
                        while (expand-- > 0) {
1734
0
                            if (sizeof(color) > 4) {
1735
0
                                if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1736
0
                                    return_error(gs_error_rangecheck);
1737
0
                            } else {
1738
0
                                if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1739
0
                                    return_error(gs_error_rangecheck);
1740
0
                            }
1741
0
                        }
1742
0
                        x++, p_cm_interp += 1;
1743
0
                        if (abs_interp_limit > 1) {
1744
0
                            dda_next(pss->params.scale_dda.x);
1745
0
                            expand = interpolate_scaled_expanded_width(1, pss);
1746
0
                        } else
1747
0
                            expand = 1;
1748
0
                    } while (x < xe && p_cm_interp[-1] == p_cm_interp[0]);
1749
0
                    break;
1750
0
                case 3:
1751
0
                    do {
1752
0
                        scaled_w += expand;
1753
0
                        while (expand-- > 0) {
1754
0
                            if (sizeof(color) > 4) {
1755
0
                                if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1756
0
                                    return_error(gs_error_rangecheck);
1757
0
                            } else {
1758
0
                                if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1759
0
                                    return_error(gs_error_rangecheck);
1760
0
                            }
1761
0
                        }
1762
0
                        x++, p_cm_interp += 3;
1763
0
                        if (abs_interp_limit > 1) {
1764
0
                            dda_next(pss->params.scale_dda.x);
1765
0
                            expand = interpolate_scaled_expanded_width(1, pss);
1766
0
                        } else
1767
0
                            expand = 1;
1768
0
                    } while (x < xe && p_cm_interp[-3] == p_cm_interp[0] &&
1769
0
                                       p_cm_interp[-2] == p_cm_interp[1] &&
1770
0
                                       p_cm_interp[-1] == p_cm_interp[2]);
1771
0
                    break;
1772
0
                case 4:
1773
0
                    do {
1774
0
                        scaled_w += expand;
1775
0
                        while (expand-- > 0) {
1776
0
                            if (sizeof(color) > 4) {
1777
0
                                if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1778
0
                                    return_error(gs_error_rangecheck);
1779
0
                            } else {
1780
0
                                if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1781
0
                                    return_error(gs_error_rangecheck);
1782
0
                            }
1783
0
                        }
1784
0
                        x++, p_cm_interp += 4;
1785
0
                        if (abs_interp_limit > 1) {
1786
0
                            dda_next(pss->params.scale_dda.x);
1787
0
                            expand = interpolate_scaled_expanded_width(1, pss);
1788
0
                        } else
1789
0
                            expand = 1;
1790
0
                    } while (x < xe && p_cm_interp[-4] == p_cm_interp[0] &&
1791
0
                                       p_cm_interp[-3] == p_cm_interp[1] &&
1792
0
                                       p_cm_interp[-2] == p_cm_interp[2] &&
1793
0
                                       p_cm_interp[-1] == p_cm_interp[3]);
1794
0
                    break;
1795
0
                default:
1796
0
                    scaled_w += expand;
1797
0
                    while (expand-- > 0) {
1798
0
                        if (sizeof(color) > 4) {
1799
0
                            if (sample_store_next64(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1800
0
                                return_error(gs_error_rangecheck);
1801
0
                        } else {
1802
0
                            if (sample_store_next32(color, &l_dptr, &l_dbit, bpp, &l_dbyte) < 0)
1803
0
                                return_error(gs_error_rangecheck);
1804
0
                        }
1805
0
                    };
1806
0
                    x++, p_cm_interp += spp_cm;
1807
0
                    if (abs_interp_limit > 1)
1808
0
                        dda_next(pss->params.scale_dda.x);
1809
0
            }
1810
0
        } else {
1811
0
            int rcode, rep = 0;
1812
1813
            /* do _COPY in case any pure colors were accumulated above*/
1814
0
            if ( x > l_xprev ) {
1815
0
                sample_store_flush(l_dptr, l_dbit, l_dbyte);
1816
0
                if (abs_interp_limit <= 1) {
1817
0
                    code = (*dev_proc(dev, copy_color))
1818
0
                                   (dev, out, l_xprev - xo, raster,
1819
0
                                    gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1820
0
                    if (code < 0)
1821
0
                        return code;
1822
0
                } else {
1823
                    /* scale up in X and Y */
1824
0
                    int scaled_x = xo + scaled_x_prev;
1825
0
                    int i = scaled_h;
1826
0
                    int iy = scaled_y;
1827
1828
0
                    for (; i > 0; --i) {
1829
0
                         code = (*dev_proc(dev, copy_color))
1830
0
                                      (dev, out, scaled_x_prev, raster,
1831
0
                                       gx_no_bitmap_id, scaled_x, iy, scaled_w, 1);
1832
0
                         if (code < 0)
1833
0
                             return code;
1834
0
                         iy += dy;
1835
0
                    }
1836
0
                    scaled_x_prev = dda_current(pss->params.scale_dda.x);
1837
0
                }
1838
0
            }
1839
            /* as above, see if we can accumulate any runs */
1840
0
            switch (spp_cm) {
1841
0
                case 1:
1842
0
                    do {
1843
0
                        rep++, p_cm_interp += 1;
1844
0
                    } while ((rep + x) < xe && p_cm_interp[-1] == p_cm_interp[0]);
1845
0
                    break;
1846
0
                case 3:
1847
0
                    do {
1848
0
                        rep++, p_cm_interp += 3;
1849
0
                    } while ((rep + x) < xe && p_cm_interp[-3] == p_cm_interp[0] &&
1850
0
                                               p_cm_interp[-2] == p_cm_interp[1] &&
1851
0
                                               p_cm_interp[-1] == p_cm_interp[2]);
1852
0
                    break;
1853
0
                case 4:
1854
0
                    do {
1855
0
                        rep++, p_cm_interp += 4;
1856
0
                    } while ((rep + x) < xe && p_cm_interp[-4] == p_cm_interp[0] &&
1857
0
                                               p_cm_interp[-3] == p_cm_interp[1] &&
1858
0
                                               p_cm_interp[-2] == p_cm_interp[2] &&
1859
0
                                               p_cm_interp[-1] == p_cm_interp[3]);
1860
0
                    break;
1861
0
                default:
1862
0
                    rep = 1, p_cm_interp += spp_cm;
1863
0
                    break;
1864
0
            }
1865
0
            if (abs_interp_limit <= 1) {
1866
0
                scaled_w = rep;
1867
0
                rcode = gx_fill_rectangle_device_rop(x, ry, rep, 1, &devc, dev, lop);
1868
0
                if (rcode < 0)
1869
0
                    return rcode;
1870
0
            } else {
1871
0
                int scaled_x = xo + scaled_x_prev;
1872
1873
0
                scaled_w = interpolate_scaled_expanded_width(rep, pss);
1874
0
                rcode = gx_fill_rectangle_device_rop(scaled_x, scaled_y, scaled_w, scaled_h,
1875
0
                                                     &devc, dev, lop);
1876
0
                if (rcode < 0)
1877
0
                    return rcode;
1878
0
                dda_advance(pss->params.scale_dda.x, rep);
1879
0
                scaled_x_prev = dda_current(pss->params.scale_dda.x);
1880
0
            }
1881
0
            while (scaled_w-- > 0)
1882
0
                sample_store_skip_next(&l_dptr, &l_dbit, bpp, &l_dbyte);
1883
0
            scaled_w = 0;
1884
0
            x += rep;
1885
0
            l_xprev = x;
1886
0
        }
1887
0
    }  /* End on x loop */
1888
0
    if ( x > l_xprev ) {
1889
0
        sample_store_flush(l_dptr, l_dbit, l_dbyte);
1890
0
        if (abs_interp_limit <= 1) {
1891
0
            code = (*dev_proc(dev, copy_color))
1892
0
                          (dev, out, l_xprev - xo, raster,
1893
0
                           gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1894
0
            if (code < 0)
1895
0
                return code;
1896
0
        } else {
1897
            /* scale up in X and Y */
1898
0
            int scaled_x = xo + scaled_x_prev;
1899
1900
0
            for (; scaled_h > 0; --scaled_h) {
1901
0
                code = (*dev_proc(dev, copy_color))
1902
0
                              (dev, out, scaled_x_prev, raster,
1903
0
                               gx_no_bitmap_id, scaled_x, scaled_y, scaled_w, 1);
1904
0
                if (code < 0)
1905
0
                    return code;
1906
0
                scaled_y += dy;
1907
0
            }
1908
0
        }
1909
0
    }
1910
0
    if (abs_interp_limit > 1) {
1911
0
        pss->params.scale_dda.x = save_x_dda; /* reset X to start of line */
1912
0
    }
1913
    /*if_debug1m('w', penum->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
1914
0
    return 0;
1915
0
}
1916
1917
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)
1918
0
{
1919
0
    int x;
1920
0
    gx_device_color devc;
1921
0
    gx_color_index color;
1922
0
    byte *out = penum->line;
1923
0
    byte *l_dptr = out;
1924
0
    int l_xprev = (xo);
1925
0
    int code;
1926
0
    int ry = yo + penum->line_xy * dy;
1927
1928
0
    devc.tag = device_current_tag(dev);
1929
0
    for (x = xo; x < xe;) {
1930
1931
#ifdef DEBUG
1932
        if (gs_debug_c('B')) {
1933
            int ci;
1934
1935
            for (ci = 0; ci < 3; ++ci)
1936
                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
1937
                    p_cm_interp[ci]);
1938
        }
1939
#endif
1940
        /* Get the device color */
1941
0
        get_device_color(penum, p_cm_interp, &devc, &color, dev);
1942
0
        if (color_is_pure(&devc)) {
1943
0
            gx_color_index color = devc.colors.pure;
1944
1945
            /* Just pack colors into a scan line. */
1946
            /* Skip runs quickly for the common cases. */
1947
0
            do {
1948
0
                *l_dptr++ = (byte)(color >> 24);
1949
0
                *l_dptr++ = (byte)(color >> 16);
1950
0
                *l_dptr++ = (byte)(color >> 8);
1951
0
                *l_dptr++ = (byte)(color);
1952
0
                x++, p_cm_interp += 4;
1953
0
            } while (x < xe && p_cm_interp[-4] == p_cm_interp[0] &&
1954
0
                               p_cm_interp[-3] == p_cm_interp[1] &&
1955
0
                               p_cm_interp[-2] == p_cm_interp[2] &&
1956
0
                               p_cm_interp[-1] == p_cm_interp[3]);
1957
0
        }
1958
0
        else {
1959
0
            int rep = 0;
1960
1961
            /* do _COPY in case any pure colors were accumulated above*/
1962
0
            if (x > l_xprev) {
1963
0
                code = (*dev_proc(dev, copy_color))
1964
0
                    (dev, out, l_xprev - xo, raster,
1965
0
                        gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1966
0
                if (code < 0)
1967
0
                    return code;
1968
0
            }
1969
            /* as above, see if we can accumulate any runs */
1970
0
            do {
1971
0
                rep++, p_cm_interp += 4;
1972
0
            } while ((rep + x) < xe && p_cm_interp[-4] == p_cm_interp[0] &&
1973
0
                                       p_cm_interp[-3] == p_cm_interp[1] &&
1974
0
                                       p_cm_interp[-2] == p_cm_interp[2] &&
1975
0
                                       p_cm_interp[-1] == p_cm_interp[3]);
1976
0
            code = gx_fill_rectangle_device_rop(x, ry, rep, 1, &devc, dev, lop);
1977
0
            if (code < 0)
1978
0
                return code;
1979
0
            x += rep;
1980
0
            l_xprev = x;
1981
0
            l_dptr += 4 * rep;
1982
0
        }
1983
0
    }  /* End on x loop */
1984
0
    if (x > l_xprev) {
1985
0
        code = (*dev_proc(dev, copy_color))
1986
0
            (dev, out, l_xprev - xo, raster,
1987
0
                gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
1988
0
        if (code < 0)
1989
0
            return code;
1990
0
    }
1991
    /*if_debug1m('w', penum->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
1992
0
    return 0;
1993
0
}
1994
1995
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)
1996
0
{
1997
0
    int x;
1998
0
    gx_device_color devc;
1999
0
    gx_color_index color;
2000
0
    byte *out = penum->line;
2001
0
    byte *l_dptr = out;
2002
0
    int l_xprev = (xo);
2003
0
    int code;
2004
0
    int ry = yo + penum->line_xy * dy;
2005
2006
0
    devc.tag = device_current_tag(dev);
2007
0
    for (x = xo; x < xe;) {
2008
#ifdef DEBUG
2009
        if (gs_debug_c('B')) {
2010
            int ci;
2011
2012
            for (ci = 0; ci < 3; ++ci)
2013
                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
2014
                    p_cm_interp[ci]);
2015
        }
2016
#endif
2017
        /* Get the device color */
2018
0
        get_device_color(penum, p_cm_interp, &devc, &color, dev);
2019
0
        if (color_is_pure(&devc)) {
2020
0
            gx_color_index color = devc.colors.pure;
2021
2022
            /* Just pack colors into a scan line. */
2023
            /* Skip runs quickly for the common cases. */
2024
0
            do {
2025
0
                *l_dptr++ = (byte)(color >> 16);
2026
0
                *l_dptr++ = (byte)(color >> 8);
2027
0
                *l_dptr++ = (byte)(color);
2028
0
                x++, p_cm_interp += 3;
2029
0
            } while (x < xe && p_cm_interp[-3] == p_cm_interp[0] &&
2030
0
                               p_cm_interp[-2] == p_cm_interp[1] &&
2031
0
                               p_cm_interp[-1] == p_cm_interp[2]);
2032
0
        }
2033
0
        else {
2034
0
            int rep = 0;
2035
2036
            /* do _COPY in case any pure colors were accumulated above*/
2037
0
            if (x > l_xprev) {
2038
0
                code = (*dev_proc(dev, copy_color))
2039
0
                    (dev, out, l_xprev - xo, raster,
2040
0
                        gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
2041
0
                if (code < 0)
2042
0
                    return code;
2043
0
            }
2044
            /* as above, see if we can accumulate any runs */
2045
0
            do {
2046
0
                rep++, p_cm_interp += 3;
2047
0
            } while ((rep + x) < xe && p_cm_interp[-3] == p_cm_interp[0] &&
2048
0
                p_cm_interp[-2] == p_cm_interp[1] &&
2049
0
                p_cm_interp[-1] == p_cm_interp[2]);
2050
0
            code = gx_fill_rectangle_device_rop(x, ry, rep, 1, &devc, dev, lop);
2051
0
            if (code < 0)
2052
0
                return code;
2053
0
            x += rep;
2054
0
            l_xprev = x;
2055
0
            l_dptr += 3 * rep;
2056
0
        }
2057
0
    }  /* End on x loop */
2058
0
    if (x > l_xprev) {
2059
0
        code = (*dev_proc(dev, copy_color))
2060
0
            (dev, out, l_xprev - xo, raster,
2061
0
                gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
2062
0
        if (code < 0)
2063
0
            return code;
2064
0
    }
2065
    /*if_debug1m('w', penum->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2066
0
    return 0;
2067
0
}
2068
2069
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)
2070
0
{
2071
0
    int x;
2072
0
    gx_device_color devc;
2073
0
    gx_color_index color;
2074
0
    byte *out = penum->line;
2075
0
    byte *l_dptr = out;
2076
0
    int l_xprev = (xo);
2077
0
    int code;
2078
0
    int ry = yo + penum->line_xy * dy;
2079
2080
0
    devc.tag = device_current_tag(dev);
2081
0
    for (x = xo; x < xe;) {
2082
#ifdef DEBUG
2083
        if (gs_debug_c('B')) {
2084
            int ci;
2085
2086
            for (ci = 0; ci < 3; ++ci)
2087
                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
2088
                    p_cm_interp[ci]);
2089
        }
2090
#endif
2091
        /* Get the device color */
2092
0
        get_device_color(penum, p_cm_interp, &devc, &color, dev);
2093
0
        if (color_is_pure(&devc)) {
2094
0
            gx_color_index color = devc.colors.pure;
2095
2096
            /* Just pack colors into a scan line. */
2097
            /* Skip runs quickly for the common cases. */
2098
0
            do {
2099
0
                *l_dptr++ = (byte)(color);
2100
0
                x++, p_cm_interp++;
2101
0
            } while (x < xe && p_cm_interp[-1] == p_cm_interp[0]);
2102
0
        }
2103
0
        else {
2104
0
            int rep = 0;
2105
2106
            /* do _COPY in case any pure colors were accumulated above*/
2107
0
            if (x > l_xprev) {
2108
0
                code = (*dev_proc(dev, copy_color))
2109
0
                    (dev, out, l_xprev - xo, raster,
2110
0
                        gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
2111
0
                if (code < 0)
2112
0
                    return code;
2113
0
            }
2114
            /* as above, see if we can accumulate any runs */
2115
0
            do {
2116
0
                rep++, p_cm_interp++;
2117
0
            } while ((rep + x) < xe && p_cm_interp[-1] == p_cm_interp[0]);
2118
0
            code = gx_fill_rectangle_device_rop(x, ry, rep, 1, &devc, dev, lop);
2119
0
            if (code < 0)
2120
0
                return code;
2121
0
            x += rep;
2122
0
            l_xprev = x;
2123
0
            l_dptr += rep;
2124
0
        }
2125
0
    }  /* End on x loop */
2126
0
    if (x > l_xprev) {
2127
0
        code = (*dev_proc(dev, copy_color))
2128
0
            (dev, out, l_xprev - xo, raster,
2129
0
                gx_no_bitmap_id, l_xprev, ry, x - l_xprev, 1);
2130
0
        if (code < 0)
2131
0
            return code;
2132
0
    }
2133
    /*if_debug1m('w', penum->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2134
0
    return 0;
2135
0
}
2136
2137
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)
2138
0
{
2139
0
    return irii_inner_template(penum, xo, xe, spp_cm, p_cm_interp, dev, abs_interp_limit, bpp, raster, yo, dy, lop);
2140
0
}
2141
2142
/* Interpolation with ICC based source spaces. This is done seperately to
2143
   enable optimization and avoid the multiple tranformations that occur in
2144
   the above code */
2145
static int
2146
image_render_interpolate_icc(gx_image_enum * penum, const byte * buffer,
2147
                         int data_x, uint iw, int h, gx_device * dev)
2148
0
{
2149
0
    stream_image_scale_state *pss = penum->scaler;
2150
0
    const gs_gstate *pgs = penum->pgs;
2151
0
    gs_logical_operation_t lop = penum->log_op;
2152
0
    byte *out = penum->line;
2153
0
    stream_cursor_read stream_r;
2154
0
    stream_cursor_write stream_w;
2155
0
    int abs_interp_limit = pss->params.abs_interp_limit;
2156
0
    int limited_PatchWidthOut = (pss->params.PatchWidthOut + abs_interp_limit - 1) / abs_interp_limit;
2157
2158
0
    if (penum->icc_link == NULL) {
2159
0
        return gs_rethrow(-1, "ICC Link not created during gs_image_class_0_interpolate");
2160
0
    }
2161
0
    initial_decode(penum, buffer, data_x, h, &stream_r, true);
2162
    /*
2163
     * Process input and/or collect output.  By construction, the pixels are
2164
     * 1-for-1 with the device, but the Y coordinate might be inverted.
2165
     * CM is performed on the entire row.
2166
     */
2167
0
    {
2168
0
        int xo = penum->xyi.x;
2169
0
        int yo = penum->xyi.y;
2170
0
        int width = (pss->params.WidthOut + abs_interp_limit - 1) / abs_interp_limit;
2171
0
        int width_in = pss->params.WidthIn;
2172
0
        int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
2173
0
        int dy;
2174
0
        int bpp = dev->color_info.depth;
2175
0
        uint raster = bitmap_raster(width * bpp);
2176
0
        unsigned short *p_cm_interp;
2177
0
        byte *p_cm_buff = NULL;
2178
0
        byte *psrc;
2179
0
        int spp_decode = pss->params.spp_decode;
2180
0
        int spp_interp = pss->params.spp_interp;
2181
0
        int spp_cm;
2182
0
        gsicc_bufferdesc_t input_buff_desc;
2183
0
        gsicc_bufferdesc_t output_buff_desc;
2184
0
        int code;
2185
0
        cmm_dev_profile_t *dev_profile;
2186
0
        int num_bytes_decode = pss->params.BitsPerComponentIn / 8;
2187
0
        irii_core_fn irii_core;
2188
2189
0
        code = dev_proc(dev, get_profile)(dev, &dev_profile);
2190
0
        if (code)
2191
0
            return code;
2192
0
        spp_cm = gsicc_get_device_profile_comps(dev_profile);
2193
0
        if (penum->matrix.yy > 0)
2194
0
            dy = 1;
2195
0
        else
2196
0
            dy = -1, yo--;
2197
0
        if (spp_cm == 4 && abs_interp_limit == 1 && bpp == 32)
2198
0
            irii_core = &irii_inner_32bpp_4spp_1abs;
2199
0
        else if (spp_cm == 3 && abs_interp_limit == 1 && bpp == 24)
2200
0
            irii_core = &irii_inner_24bpp_3spp_1abs;
2201
0
        else if (spp_cm == 1 && abs_interp_limit == 1 && bpp == 8)
2202
0
            irii_core = &irii_inner_8bpp_1spp_1abs;
2203
0
        else
2204
0
            irii_core = &irii_inner_generic;
2205
2206
        /* If it makes sense (if enlarging), do early CM */
2207
0
        if (pss->params.early_cm && !penum->icc_link->is_identity
2208
0
            && stream_r.ptr != stream_r.limit) {
2209
            /* Get the buffers set up. */
2210
0
            p_cm_buff =
2211
0
                (byte *) gs_alloc_bytes(pgs->memory,
2212
0
                                        (size_t)num_bytes_decode * width_in * spp_cm,
2213
0
                                        "image_render_interpolate_icc");
2214
0
            if (p_cm_buff == NULL)
2215
0
                return_error(gs_error_VMerror);
2216
2217
            /* Set up the buffer descriptors. We keep the bytes the same */
2218
0
            gsicc_init_buffer(&input_buff_desc, spp_decode, num_bytes_decode,
2219
0
                          false, false, false, 0, width_in * spp_decode,
2220
0
                          1, width_in);
2221
0
            gsicc_init_buffer(&output_buff_desc, spp_cm, num_bytes_decode,
2222
0
                          false, false, false, 0, width_in * spp_cm,
2223
0
                          1, width_in);
2224
            /* Do the transformation */
2225
0
            psrc = (byte*) (stream_r.ptr + 1);
2226
0
            code = (penum->icc_link->procs.map_buffer)(dev, penum->icc_link, &input_buff_desc,
2227
0
                                                &output_buff_desc, (void*) psrc,
2228
0
                                                (void*) p_cm_buff);
2229
0
            if (code < 0)
2230
0
                return code;
2231
2232
            /* Re-set the reading stream to use the cm data */
2233
0
            stream_r.ptr = p_cm_buff - 1;
2234
0
            stream_r.limit = stream_r.ptr + num_bytes_decode * width_in * spp_cm;
2235
0
        } else {
2236
            /* CM after interpolation (or none).  Just set up the buffers
2237
               if needed.  16 bit operations if CM takes place.  */
2238
0
            if (!penum->icc_link->is_identity) {
2239
0
                p_cm_buff = (byte *) gs_alloc_bytes(pgs->memory,
2240
0
                    sizeof(unsigned short) * (size_t)width * spp_cm,
2241
0
                    "image_render_interpolate_icc");
2242
0
                if (!p_cm_buff) {
2243
0
                    return gs_error_VMerror;
2244
0
                }
2245
                /* Set up the buffer descriptors. */
2246
0
                gsicc_init_buffer(&input_buff_desc, spp_decode, 2,
2247
0
                              false, false, false, 0, width * spp_decode,
2248
0
                              1, limited_PatchWidthOut);
2249
0
                gsicc_init_buffer(&output_buff_desc, spp_cm, 2,
2250
0
                              false, false, false, 0, width * spp_cm,
2251
0
                              1, limited_PatchWidthOut);
2252
0
            }
2253
0
        }
2254
0
        for (;;) {
2255
0
            const unsigned short *pinterp;
2256
0
            int status;
2257
2258
0
            stream_w.limit = out + pss->params.WidthOut *
2259
0
                max(spp_decode * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
2260
0
            stream_w.ptr = stream_w.limit - width * spp_interp * sizeofPixelOut;
2261
0
            pinterp = (const unsigned short *)(stream_w.ptr + 1);
2262
            /* This is where the rescale takes place; this will consume the
2263
             * data from stream_r, and post processed data into stream_w. The
2264
             * data in stream_w may be bogus if we are outside the active
2265
             * region, and this will be indiated by pss->params.Active being
2266
             * set to false. */
2267
0
            status = (*pss->templat->process)
2268
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
2269
0
            if (status < 0 && status != EOFC)
2270
0
                return_error(gs_error_ioerror);
2271
0
            if (stream_w.ptr == stream_w.limit) {
2272
0
                int xe = xo + limited_PatchWidthOut;
2273
2274
                /* Are we active? (i.e. in the render rectangle) */
2275
0
                if (!pss->params.Active)
2276
0
                    goto inactive;
2277
0
                if_debug1m('B', penum->memory, "[B]Interpolated row %d:\n[B]",
2278
0
                           penum->line_xy);
2279
                /* Take care of CM on the entire interpolated row, if we
2280
                   did not already do CM */
2281
0
                if (penum->icc_link->is_identity || pss->params.early_cm) {
2282
                    /* Fastest case. No CM needed */
2283
0
                    p_cm_interp = (unsigned short *) pinterp;
2284
0
                    p_cm_interp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_cm;
2285
0
                } else {
2286
                    /* Transform */
2287
0
                    pinterp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_decode;
2288
0
                    p_cm_interp = (unsigned short *) p_cm_buff;
2289
0
                    p_cm_interp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_cm;
2290
0
                    code = (penum->icc_link->procs.map_buffer)(dev, penum->icc_link,
2291
0
                                                        &input_buff_desc,
2292
0
                                                        &output_buff_desc,
2293
0
                                                        (void*) pinterp,
2294
0
                                                        (void*) p_cm_interp);
2295
0
                    if (code < 0)
2296
0
                        return code;
2297
0
                }
2298
0
                code = irii_core(penum, xo, xe, spp_cm, p_cm_interp, dev, abs_interp_limit, bpp, raster, yo, dy, lop);
2299
0
                if (code < 0)
2300
0
                    return code;
2301
0
inactive:
2302
0
                penum->line_xy++;
2303
0
                if (abs_interp_limit > 1) {
2304
0
                    dda_next(pss->params.scale_dda.y);
2305
0
                }
2306
0
                if_debug0m('B', penum->memory, "\n");
2307
0
            }
2308
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
2309
0
                break;
2310
0
        }
2311
        /* Free cm buffer, if it was used */
2312
0
        if (p_cm_buff != NULL) {
2313
0
            gs_free_object(pgs->memory, (byte *)p_cm_buff,
2314
0
                           "image_render_interpolate_icc");
2315
0
        }
2316
0
    }
2317
0
    return (h == 0 ? 0 : 1);
2318
0
}
2319
2320
static int
2321
image_render_interpolate_landscape(gx_image_enum * penum,
2322
                                   const byte * buffer,
2323
                                   int data_x, uint iw, int h,
2324
                                   gx_device * dev)
2325
0
{
2326
0
    stream_image_scale_state *pss = penum->scaler;
2327
0
    const gs_color_space *pcs = penum->pcs;
2328
0
    gs_logical_operation_t lop = penum->log_op;
2329
0
    int spp_decode = pss->params.spp_decode;
2330
0
    stream_cursor_read stream_r;
2331
0
    stream_cursor_write stream_w;
2332
0
    byte *out = penum->line;
2333
0
    bool islab = false;
2334
0
    int abs_interp_limit = pss->params.abs_interp_limit;
2335
0
    const gs_color_space *pconc;
2336
0
    cmm_dev_profile_t *dev_profile;
2337
0
    int code = dev_proc(penum->dev, get_profile)(penum->dev, &dev_profile);
2338
2339
0
    if (code < 0)
2340
0
        return code;
2341
2342
0
    if (pcs->cmm_icc_profile_data != NULL) {
2343
0
        islab = pcs->cmm_icc_profile_data->islab;
2344
0
    }
2345
    /* Perform any decode procedure if needed */
2346
0
    initial_decode(penum, buffer, data_x, h, &stream_r, false);
2347
    /*
2348
     * Process input and/or collect output.  By construction, the pixels are
2349
     * 1-for-1 with the device, but the Y coordinate might be inverted.
2350
     */
2351
0
    {
2352
0
        int xo = penum->xyi.y;
2353
0
        int yo = penum->xyi.x;
2354
0
        int width = (pss->params.WidthOut + abs_interp_limit - 1) / abs_interp_limit;
2355
0
        int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
2356
0
        int dy;
2357
0
        color_handler_fn *color_handler = NULL;
2358
2359
0
        if (penum->matrix.yx > 0)
2360
0
            dy = 1;
2361
0
        else
2362
0
            dy = -1, yo--;
2363
0
        for (;;) {
2364
0
            int ry = yo + penum->line_xy * dy;
2365
0
            int x;
2366
0
            const frac *psrc;
2367
0
            gx_device_color devc;
2368
0
            int status;
2369
0
            int scaled_w = 0;
2370
0
            gx_dda_fixed save_x_dda;
2371
2372
0
            devc.tag = device_current_tag(dev);
2373
0
            if (abs_interp_limit > 1) {
2374
0
                save_x_dda = pss->params.scale_dda.x;
2375
0
            }
2376
0
            stream_w.limit = out + width *
2377
0
                max(spp_decode * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
2378
0
            stream_w.ptr = stream_w.limit - width * spp_decode * sizeofPixelOut;
2379
0
            psrc = (const frac *)(stream_w.ptr + 1);
2380
            /* This is where the rescale takes place; this will consume the
2381
             * data from stream_r, and post processed data into stream_w. The
2382
             * data in stream_w may be bogus if we are outside the active
2383
             * region, and this will be indicated by pss->params.Active being
2384
             * set to false. */
2385
0
            status = (*pss->templat->process)
2386
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
2387
0
            if (status < 0 && status != EOFC)
2388
0
                return_error(gs_error_ioerror);
2389
0
            if (stream_w.ptr == stream_w.limit) {
2390
0
                int xe = xo + (pss->params.PatchWidthOut + abs_interp_limit - 1) / abs_interp_limit;
2391
0
                int scaled_h = 0;
2392
0
                int scaled_y = 0;
2393
2394
0
                if (abs_interp_limit > 1) {
2395
0
                    scaled_h = interpolate_scaled_expanded_height(1, pss);
2396
0
                    scaled_y = yo + (dy * dda_current(pss->params.scale_dda.y)) -
2397
0
                               ((dy < 0) ? (scaled_h - 1) : 0);
2398
0
                }
2399
2400
                /* Are we active? (i.e. in the render rectangle) */
2401
0
                if (!pss->params.Active)
2402
0
                    goto inactive;
2403
0
                if_debug1m('B', penum->memory, "[B]Interpolated (rotated) row %d:\n[B]",
2404
0
                           penum->line_xy);
2405
0
                psrc += (pss->params.LeftMarginOut / abs_interp_limit) * spp_decode;
2406
0
                if (color_handler == NULL)
2407
0
                    color_handler = get_color_handler(penum, spp_decode, islab, dev_profile, &pconc);
2408
0
                for (x = xo; x < xe;) {
2409
0
                    if (color_handler != NULL) {
2410
#ifdef DEBUG
2411
                        if (gs_debug_c('B')) {
2412
                            int ci;
2413
2414
                            for (ci = 0; ci < spp_decode; ++ci)
2415
                                dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
2416
                                          psrc[ci]);
2417
                        }
2418
#endif
2419
0
                        code = color_handler(penum, psrc, &devc, dev, dev_profile, pconc);
2420
0
                        if (code < 0)
2421
0
                            return code;
2422
0
                    }
2423
                    /* We scan for vertical runs of pixels, even if they end up
2424
                     * being split up in most cases within copy_color_unaligned anyway. */
2425
0
                    {
2426
0
                        int rcode;
2427
0
                        int rep = 0;
2428
2429
                        /* as above, see if we can accumulate any runs */
2430
0
                        switch (spp_decode) {
2431
0
                            case 1:
2432
0
                                do {
2433
0
                                    rep++, psrc += 1;
2434
0
                                } while ((rep + x) < xe &&
2435
0
                                         psrc[-1] == psrc[0]);
2436
0
                                break;
2437
0
                            case 3:
2438
0
                                do {
2439
0
                                    rep++, psrc += 3;
2440
0
                                } while ((rep + x) < xe &&
2441
0
                                         psrc[-3] == psrc[0] &&
2442
0
                                         psrc[-2] == psrc[1] &&
2443
0
                                         psrc[-1] == psrc[2]);
2444
0
                                break;
2445
0
                            case 4:
2446
0
                                do {
2447
0
                                    rep++, psrc += 4;
2448
0
                                } while ((rep + x) < xe &&
2449
0
                                         psrc[-4] == psrc[0] &&
2450
0
                                         psrc[-3] == psrc[1] &&
2451
0
                                         psrc[-2] == psrc[2] &&
2452
0
                                         psrc[-1] == psrc[3]);
2453
0
                                break;
2454
0
                            default:
2455
0
                                rep = 1;
2456
0
                                psrc += spp_decode;
2457
0
                                break;
2458
0
                        }
2459
0
                        if (abs_interp_limit <= 1) {
2460
0
                            rcode = gx_fill_rectangle_device_rop(ry, x, 1, rep, &devc, dev, lop);
2461
0
                            if (rcode < 0)
2462
0
                                return rcode;
2463
0
                        } else {
2464
0
                            int scaled_x = xo + dda_current(pss->params.scale_dda.x);
2465
2466
0
                            scaled_w = interpolate_scaled_expanded_width(rep, pss);
2467
0
                            rcode = gx_fill_rectangle_device_rop(scaled_y, scaled_x, scaled_h, scaled_w,
2468
0
                                                                 &devc, dev, lop);
2469
0
                            if (rcode < 0)
2470
0
                                return rcode;
2471
0
                            dda_advance(pss->params.scale_dda.x, rep);
2472
0
                        }
2473
0
                        x += rep;
2474
0
                    }
2475
0
                }
2476
                /*if_debug1m('w', dev->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2477
0
inactive:
2478
0
                penum->line_xy++;
2479
0
                if (abs_interp_limit > 1) {
2480
0
                    dda_next(pss->params.scale_dda.y);
2481
0
                    pss->params.scale_dda.x = save_x_dda; /* reset X to start of line */
2482
0
                }
2483
0
                if_debug0m('B', dev->memory, "\n");
2484
0
            }
2485
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
2486
0
                break;
2487
0
        }
2488
0
    }
2489
0
    return (h == 0 ? 0 : 1);
2490
0
}
2491
2492
static int
2493
image_render_interpolate_landscape_masked(gx_image_enum * penum,
2494
                                          const byte * buffer,
2495
                                          int data_x, uint iw, int h,
2496
                                          gx_device * dev)
2497
0
{
2498
0
    stream_image_scale_state *pss = penum->scaler;
2499
0
    int spp_decode = pss->params.spp_decode;
2500
0
    stream_cursor_read stream_r;
2501
0
    stream_cursor_write stream_w;
2502
0
    byte *out = penum->line;
2503
0
    gx_color_index color = penum->icolor1->colors.pure;
2504
2505
    /* Perform any decode procedure if needed. Probably only reversal
2506
     * of the data in this case. */
2507
0
    initial_decode(penum, buffer, data_x, h, &stream_r, false);
2508
    /*
2509
     * Process input and/or collect output.  By construction, the pixels are
2510
     * 1-for-1 with the device, but the Y coordinate might be inverted.
2511
     */
2512
0
    {
2513
0
        int xo = penum->xyi.y;
2514
0
        int yo = penum->xyi.x;
2515
0
        int width = pss->params.WidthOut;
2516
0
        int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
2517
0
        int dy;
2518
2519
0
        if (penum->matrix.yx > 0)
2520
0
            dy = 1;
2521
0
        else
2522
0
            dy = -1, yo--;
2523
0
        for (;;) {
2524
0
            int ry = yo + penum->line_xy * dy;
2525
0
            int x;
2526
0
            const byte *psrc;
2527
0
            int status, code;
2528
2529
0
            stream_w.limit = out + width *
2530
0
                max(spp_decode * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
2531
0
            stream_w.ptr = stream_w.limit - width * spp_decode * sizeofPixelOut;
2532
0
            psrc = stream_w.ptr + 1;
2533
            /* This is where the rescale takes place; this will consume the
2534
             * data from stream_r, and post processed data into stream_w. The
2535
             * data in stream_w may be bogus if we are outside the active
2536
             * region, and this will be indicated by pss->params.Active being
2537
             * set to false. */
2538
0
            status = (*pss->templat->process)
2539
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
2540
0
            if (status < 0 && status != EOFC)
2541
0
                return_error(gs_error_ioerror);
2542
0
            if (stream_w.ptr == stream_w.limit) {
2543
0
                int xe = xo + pss->params.PatchWidthOut;
2544
2545
                /* Are we active? (i.e. in the render rectangle) */
2546
0
                if (!pss->params.Active)
2547
0
                    goto inactive;
2548
0
                if_debug1m('B', penum->memory, "[B]Interpolated masked (rotated) row %d:\n[B]",
2549
0
                           penum->line_xy);
2550
0
                psrc += pss->params.LeftMarginOut * spp_decode;
2551
0
                for (x = xo; x < xe; x++) {
2552
0
                    code = (*dev_proc(dev, copy_alpha))(dev, psrc, 0, 0,
2553
0
                        gx_no_bitmap_id, ry, x, 1, 1, color, 8);
2554
0
                    if (code < 0)
2555
0
                        return code;
2556
0
                    psrc += spp_decode;
2557
0
                }
2558
                /*if_debug1m('w', dev->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2559
0
inactive:
2560
0
                penum->line_xy++;
2561
0
                if_debug0m('B', dev->memory, "\n");
2562
0
            }
2563
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
2564
0
                break;
2565
0
        }
2566
0
    }
2567
0
    return (h == 0 ? 0 : 1);
2568
0
}
2569
2570
static int
2571
image_render_interpolate_landscape_masked_hl(gx_image_enum * penum,
2572
                                             const byte * buffer,
2573
                                             int data_x, uint iw, int h,
2574
                                             gx_device * dev)
2575
0
{
2576
0
    stream_image_scale_state *pss = penum->scaler;
2577
0
    int spp_decode = pss->params.spp_decode;
2578
0
    stream_cursor_read stream_r;
2579
0
    stream_cursor_write stream_w;
2580
0
    byte *out = penum->line;
2581
2582
    /* Perform any decode procedure if needed. Probably only reversal
2583
     * of the data in this case. */
2584
0
    initial_decode(penum, buffer, data_x, h, &stream_r, false);
2585
    /*
2586
     * Process input and/or collect output.  By construction, the pixels are
2587
     * 1-for-1 with the device, but the Y coordinate might be inverted.
2588
     */
2589
0
    {
2590
0
        int xo = penum->xyi.y;
2591
0
        int yo = penum->xyi.x;
2592
0
        int width = pss->params.WidthOut;
2593
0
        int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
2594
0
        int dy;
2595
2596
0
        if (penum->matrix.yx > 0)
2597
0
            dy = 1;
2598
0
        else
2599
0
            dy = -1, yo--;
2600
0
        for (;;) {
2601
0
            int ry = yo + penum->line_xy * dy;
2602
0
            int x;
2603
0
            const byte *psrc;
2604
0
            int status, code;
2605
2606
0
            stream_w.limit = out + width *
2607
0
                max(spp_decode * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
2608
0
            stream_w.ptr = stream_w.limit - width * spp_decode * sizeofPixelOut;
2609
0
            psrc = stream_w.ptr + 1;
2610
            /* This is where the rescale takes place; this will consume the
2611
             * data from stream_r, and post processed data into stream_w. The
2612
             * data in stream_w may be bogus if we are outside the active
2613
             * region, and this will be indicated by pss->params.Active being
2614
             * set to false. */
2615
0
            status = (*pss->templat->process)
2616
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
2617
0
            if (status < 0 && status != EOFC)
2618
0
                return_error(gs_error_ioerror);
2619
0
            if (stream_w.ptr == stream_w.limit) {
2620
0
                int xe = xo + pss->params.PatchWidthOut;
2621
2622
                /* Are we active? (i.e. in the render rectangle) */
2623
0
                if (!pss->params.Active)
2624
0
                    goto inactive;
2625
0
                if_debug1m('B', penum->memory, "[B]Interpolated masked (rotated) row %d:\n[B]",
2626
0
                           penum->line_xy);
2627
0
                psrc += pss->params.LeftMarginOut * spp_decode;
2628
0
                for (x = xo; x < xe; x++) {
2629
0
                    code = (*dev_proc(dev, copy_alpha_hl_color))(dev, psrc, 0, 0,
2630
0
                        gx_no_bitmap_id, ry, x, 1, 1, penum->icolor1, 8);
2631
0
                    if (code < 0)
2632
0
                        return code;
2633
0
                    psrc += spp_decode;
2634
0
                }
2635
                /*if_debug1m('w', dev->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2636
0
inactive:
2637
0
                penum->line_xy++;
2638
0
                if_debug0m('B', dev->memory, "\n");
2639
0
            }
2640
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
2641
0
                break;
2642
0
        }
2643
0
    }
2644
0
    return (h == 0 ? 0 : 1);
2645
0
}
2646
2647
/* Interpolation with ICC based source spaces. This is done seperately to
2648
   enable optimization and avoid the multiple tranformations that occur in
2649
   the above code */
2650
static int
2651
image_render_interpolate_landscape_icc(gx_image_enum * penum,
2652
                                       const byte * buffer,
2653
                                       int data_x, uint iw, int h,
2654
                                       gx_device * dev)
2655
0
{
2656
0
    stream_image_scale_state *pss = penum->scaler;
2657
0
    const gs_gstate *pgs = penum->pgs;
2658
0
    gs_logical_operation_t lop = penum->log_op;
2659
0
    byte *out = penum->line;
2660
0
    stream_cursor_read stream_r;
2661
0
    stream_cursor_write stream_w;
2662
0
    int abs_interp_limit = pss->params.abs_interp_limit;
2663
0
    int limited_PatchWidthOut = (pss->params.PatchWidthOut + abs_interp_limit - 1) / abs_interp_limit;
2664
2665
0
    if (penum->icc_link == NULL) {
2666
0
        return gs_rethrow(-1, "ICC Link not created during gs_image_class_0_interpolate");
2667
0
    }
2668
0
    initial_decode(penum, buffer, data_x, h, &stream_r, true);
2669
    /*
2670
     * Process input and/or collect output.  By construction, the pixels are
2671
     * 1-for-1 with the device, but the Y coordinate might be inverted.
2672
     * CM is performed on the entire row.
2673
     */
2674
0
    {
2675
0
        int xo = penum->xyi.y;
2676
0
        int yo = penum->xyi.x;
2677
0
        int width = (pss->params.WidthOut + abs_interp_limit - 1) / abs_interp_limit;
2678
0
        int width_in = pss->params.WidthIn;
2679
0
        int sizeofPixelOut = pss->params.BitsPerComponentOut / 8;
2680
0
        int dy;
2681
0
        unsigned short *p_cm_interp;
2682
0
        byte *p_cm_buff = NULL;
2683
0
        byte *psrc;
2684
0
        int spp_decode = pss->params.spp_decode;
2685
0
        int spp_interp = pss->params.spp_interp;
2686
0
        int spp_cm;
2687
0
        gsicc_bufferdesc_t input_buff_desc;
2688
0
        gsicc_bufferdesc_t output_buff_desc;
2689
0
        gx_color_index color;
2690
0
        int code;
2691
0
        cmm_dev_profile_t *dev_profile;
2692
0
        int num_bytes_decode = pss->params.BitsPerComponentIn / 8;
2693
2694
0
        code = dev_proc(dev, get_profile)(dev, &dev_profile);
2695
0
        if (code) {
2696
0
            penum->interpolate = interp_off;
2697
0
            return 0;
2698
0
        }
2699
0
        spp_cm = gsicc_get_device_profile_comps(dev_profile);
2700
0
        if (penum->matrix.yx > 0)
2701
0
            dy = 1;
2702
0
        else
2703
0
            dy = -1, yo--;
2704
        /* If it makes sense (if enlarging), do early CM */
2705
0
        if (pss->params.early_cm && !penum->icc_link->is_identity
2706
0
            && stream_r.ptr != stream_r.limit) {
2707
            /* Get the buffers set up. */
2708
0
            p_cm_buff =
2709
0
                (byte *) gs_alloc_bytes(pgs->memory,
2710
0
                                        (size_t)num_bytes_decode * width_in * spp_cm,
2711
0
                                        "image_render_interpolate_icc");
2712
0
            if (p_cm_buff == NULL)
2713
0
                return_error(gs_error_VMerror);
2714
2715
            /* Set up the buffer descriptors. We keep the bytes the same */
2716
0
            gsicc_init_buffer(&input_buff_desc, spp_decode, num_bytes_decode,
2717
0
                          false, false, false, 0, width_in * spp_decode,
2718
0
                          1, width_in);
2719
0
            gsicc_init_buffer(&output_buff_desc, spp_cm, num_bytes_decode,
2720
0
                          false, false, false, 0, width_in * spp_cm,
2721
0
                          1, width_in);
2722
            /* Do the transformation */
2723
0
            psrc = (byte*) (stream_r.ptr + 1);
2724
0
            code = (penum->icc_link->procs.map_buffer)(dev, penum->icc_link, &input_buff_desc,
2725
0
                                                &output_buff_desc, (void*) psrc,
2726
0
                                                (void*) p_cm_buff);
2727
0
            if (code < 0)
2728
0
                return code;
2729
2730
            /* Re-set the reading stream to use the cm data */
2731
0
            stream_r.ptr = p_cm_buff - 1;
2732
0
            stream_r.limit = stream_r.ptr + num_bytes_decode * width_in * spp_cm;
2733
0
        } else {
2734
            /* CM after interpolation (or none).  Just set up the buffers
2735
               if needed.  16 bit operations if CM takes place.  */
2736
0
            if (!penum->icc_link->is_identity) {
2737
0
                p_cm_buff = (byte *) gs_alloc_bytes(pgs->memory,
2738
0
                    sizeof(unsigned short) * (size_t)width * spp_cm,
2739
0
                    "image_render_interpolate_icc");
2740
0
                if (p_cm_buff == NULL)
2741
0
                    return_error(gs_error_VMerror);
2742
                /* Set up the buffer descriptors. */
2743
0
                gsicc_init_buffer(&input_buff_desc, spp_decode, 2,
2744
0
                              false, false, false, 0, width * spp_decode,
2745
0
                              1, limited_PatchWidthOut);
2746
0
                gsicc_init_buffer(&output_buff_desc, spp_cm, 2,
2747
0
                              false, false, false, 0, width * spp_cm,
2748
0
                              1, limited_PatchWidthOut);
2749
0
            }
2750
0
        }
2751
0
        for (;;) {
2752
0
            int ry = yo + penum->line_xy * dy;
2753
0
            int x;
2754
0
            const unsigned short *pinterp;
2755
0
            gx_device_color devc;
2756
0
            int status;
2757
0
            int scaled_w = 0;
2758
0
            gx_dda_fixed save_x_dda;
2759
2760
0
            devc.tag = device_current_tag(dev);
2761
0
            if (abs_interp_limit > 1) {
2762
0
                save_x_dda = pss->params.scale_dda.x;
2763
0
            }
2764
0
            stream_w.limit = out + width *
2765
0
                max(spp_interp * sizeofPixelOut, ARCH_SIZEOF_COLOR_INDEX) - 1;
2766
0
            stream_w.ptr = stream_w.limit - width * spp_interp * sizeofPixelOut;
2767
0
            pinterp = (const unsigned short *)(stream_w.ptr + 1);
2768
            /* This is where the rescale takes place; this will consume the
2769
             * data from stream_r, and post processed data into stream_w. The
2770
             * data in stream_w may be bogus if we are outside the active
2771
             * region, and this will be indiated by pss->params.Active being
2772
             * set to false. */
2773
0
            status = (*pss->templat->process)
2774
0
                ((stream_state *) pss, &stream_r, &stream_w, h == 0);
2775
0
            if (status < 0 && status != EOFC)
2776
0
                return_error(gs_error_ioerror);
2777
0
            if (stream_w.ptr == stream_w.limit) {
2778
0
                int xe = xo + (pss->params.PatchWidthOut + abs_interp_limit - 1) / abs_interp_limit;
2779
0
                int scaled_h = 0;
2780
0
                int scaled_y = 0;
2781
2782
0
                if (abs_interp_limit > 1) {
2783
0
                    scaled_h = interpolate_scaled_expanded_height(1, pss);
2784
0
                    scaled_y = yo + (dy * dda_current(pss->params.scale_dda.y)) -
2785
0
                               ((dy < 0) ? (scaled_h - 1) : 0);
2786
0
                }
2787
2788
                /* Are we active? (i.e. in the render rectangle) */
2789
0
                if (!pss->params.Active)
2790
0
                    goto inactive;
2791
0
                if_debug1m('B', penum->memory, "[B]Interpolated row %d:\n[B]",
2792
0
                           penum->line_xy);
2793
                /* Take care of CM on the entire interpolated row, if we
2794
                   did not already do CM */
2795
0
                if (penum->icc_link->is_identity || pss->params.early_cm) {
2796
                    /* Fastest case. No CM needed */
2797
0
                    p_cm_interp = (unsigned short *) pinterp;
2798
0
                    p_cm_interp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_cm;
2799
0
                } else {
2800
                    /* Transform */
2801
0
                    pinterp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_decode;
2802
0
                    p_cm_interp = (unsigned short *) p_cm_buff;
2803
0
                    p_cm_interp += (pss->params.LeftMarginOut / abs_interp_limit) * spp_cm;
2804
0
                    code = (penum->icc_link->procs.map_buffer)(dev, penum->icc_link,
2805
0
                                                        &input_buff_desc,
2806
0
                                                        &output_buff_desc,
2807
0
                                                        (void*) pinterp,
2808
0
                                                        (void*) p_cm_interp);
2809
0
                    if (code < 0)
2810
0
                        return code;
2811
0
                }
2812
0
                for (x = xo; x < xe;) {
2813
#ifdef DEBUG
2814
                    if (gs_debug_c('B')) {
2815
                        int ci;
2816
2817
                        for (ci = 0; ci < spp_cm; ++ci)
2818
                            dmprintf2(dev->memory, "%c%04x", (ci == 0 ? ' ' : ','),
2819
                                     p_cm_interp[ci]);
2820
                    }
2821
#endif
2822
                    /* Get the device color */
2823
0
                    get_device_color(penum, p_cm_interp, &devc, &color, dev);
2824
                    /* We scan for vertical runs of pixels, even if they end up
2825
                     * being split up in most cases within copy_color_unaligned anyway. */
2826
0
                    {
2827
0
                        int rcode;
2828
0
                        int rep = 0;
2829
2830
0
                        switch (spp_cm) {
2831
0
                            case 1:
2832
0
                                do {
2833
0
                                    rep++, p_cm_interp += 1;
2834
0
                                } while ((rep + x) < xe && p_cm_interp[-1] == p_cm_interp[0]);
2835
0
                                break;
2836
0
                            case 3:
2837
0
                                do {
2838
0
                                    rep++, p_cm_interp += 3;
2839
0
                                } while ((rep + x) < xe && p_cm_interp[-3] == p_cm_interp[0] &&
2840
0
                                     p_cm_interp[-2] == p_cm_interp[1] &&
2841
0
                                     p_cm_interp[-1] == p_cm_interp[2]);
2842
0
                                break;
2843
0
                            case 4:
2844
0
                                do {
2845
0
                                    rep++, p_cm_interp += 4;
2846
0
                                } while ((rep + x) < xe && p_cm_interp[-4] == p_cm_interp[0] &&
2847
0
                                     p_cm_interp[-3] == p_cm_interp[1] &&
2848
0
                                     p_cm_interp[-2] == p_cm_interp[2] &&
2849
0
                                     p_cm_interp[-1] == p_cm_interp[3]);
2850
0
                                break;
2851
0
                            default:
2852
0
                                rep = 1, p_cm_interp += spp_cm;
2853
0
                                break;
2854
0
                        }
2855
2856
0
                        if (abs_interp_limit <= 1) {
2857
0
                            rcode = gx_fill_rectangle_device_rop(ry, x, 1, rep, &devc, dev, lop);
2858
0
                            if (rcode < 0)
2859
0
                                return rcode;
2860
0
                        } else {
2861
0
                            int scaled_x = xo + dda_current(pss->params.scale_dda.x);
2862
2863
0
                            scaled_w = interpolate_scaled_expanded_width(rep, pss);
2864
0
                            rcode = gx_fill_rectangle_device_rop(scaled_y, scaled_x, scaled_h, scaled_w,
2865
0
                                                                 &devc, dev, lop);
2866
0
                            if (rcode < 0)
2867
0
                                return rcode;
2868
0
                            dda_advance(pss->params.scale_dda.x, rep);
2869
0
                        }
2870
0
                        x += rep;
2871
0
                    }
2872
0
                }  /* End on x loop */
2873
                /*if_debug1m('w', penum->memory, "[w]Y=%d:\n", ry);*/ /* See siscale.c about 'w'. */
2874
0
inactive:
2875
0
                penum->line_xy++;
2876
0
                if (abs_interp_limit > 1) {
2877
0
                    dda_next(pss->params.scale_dda.y);
2878
0
                    pss->params.scale_dda.x = save_x_dda; /* reset X to start of line */
2879
0
                }
2880
0
                if_debug0m('B', penum->memory, "\n");
2881
0
            }
2882
0
            if ((status == 0 && stream_r.ptr == stream_r.limit) || status == EOFC)
2883
0
                break;
2884
0
        }
2885
        /* Free cm buffer, if it was used */
2886
0
        if (p_cm_buff != NULL) {
2887
0
            gs_free_object(pgs->memory, (byte *)p_cm_buff,
2888
0
                           "image_render_interpolate_icc");
2889
0
        }
2890
0
    }
2891
0
    return (h == 0 ? 0 : 1);
2892
0
}
2893
2894
/* Decode a 16-bit sample into a floating point color component.
2895
   This is used for cases where the spatial interpolation function output is 16 bit.
2896
   It is only used here, hence the static declaration for now. */
2897
2898
static void
2899
decode_sample_frac_to_float(gx_image_enum *penum, frac sample_value, gs_client_color *cc, int i)
2900
0
{
2901
0
    switch ( penum->map[i].decoding )
2902
0
    {
2903
0
        case sd_none:
2904
0
            cc->paint.values[i] = frac2float(sample_value);
2905
0
            break;
2906
0
        case sd_lookup:
2907
0
            cc->paint.values[i] =
2908
0
                   penum->map[i].decode_lookup[(frac2byte(sample_value)) >> 4];
2909
0
            break;
2910
0
        case sd_compute:
2911
0
            cc->paint.values[i] =
2912
0
                   penum->map[i].decode_base + frac2float(sample_value)*255.0 * penum->map[i].decode_factor;
2913
0
    }
2914
0
}