Coverage Report

Created: 2025-11-16 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gximage3.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
/* ImageType 3 image implementation */
18
#include "math_.h"    /* for ceil, floor */
19
#include "memory_.h"
20
#include "gx.h"
21
#include "gserrors.h"
22
#include "gsbitops.h"
23
#include "gscspace.h"
24
#include "gsstruct.h"
25
#include "gxdevice.h"
26
#include "gxdevmem.h"
27
#include "gxclipm.h"
28
#include "gximage3.h"
29
#include "gxgstate.h"
30
#include "gxdevsop.h"
31
#include <limits.h> /* For INT_MAX etc */
32
33
/* Forward references */
34
static dev_proc_begin_typed_image(gx_begin_image3);
35
static image_enum_proc_plane_data(gx_image3_plane_data);
36
static image_enum_proc_end_image(gx_image3_end_image);
37
static image_enum_proc_flush(gx_image3_flush);
38
static image_enum_proc_planes_wanted(gx_image3_planes_wanted);
39
40
/* GC descriptor */
41
private_st_gs_image3();
42
43
/* Define the image type for ImageType 3 images. */
44
const gx_image_type_t gs_image_type_3 = {
45
    &st_gs_image3, gx_begin_image3,
46
    gx_image_no_sput, gx_image_no_sget, gx_image_default_release, 3
47
};
48
static const gx_image_enum_procs_t image3_enum_procs = {
49
    gx_image3_plane_data, gx_image3_end_image,
50
    gx_image3_flush, gx_image3_planes_wanted
51
};
52
53
/* Initialize an ImageType 3 image. */
54
void
55
gs_image3_t_init(gs_image3_t * pim, gs_color_space * color_space,
56
                 gs_image3_interleave_type_t interleave_type)
57
654
{
58
654
    gs_pixel_image_t_init((gs_pixel_image_t *) pim, color_space);
59
654
    pim->type = &gs_image_type_3;
60
654
    pim->InterleaveType = interleave_type;
61
654
    gs_data_image_t_init(&pim->MaskDict, -1);
62
654
}
63
64
extern_st(st_gx_image_enum_common);
65
gs_private_st_suffix_add6(st_image3_enum, gx_image3_enum_t, "gx_image3_enum_t",
66
  image3_enum_enum_ptrs, image3_enum_reloc_ptrs, st_gx_image_enum_common,
67
  mdev, pcdev, pixel_info, mask_info, pixel_data, mask_data);
68
69
/* Define the default implementation of ImageType 3 processing. */
70
static IMAGE3_MAKE_MID_PROC(make_mid_default); /* check prototype */
71
static int
72
make_mid_default(gx_device **pmidev, gx_device *dev, int width, int height,
73
                 gs_memory_t *mem)
74
462
{
75
462
    gx_device_memory *midev =
76
462
        gs_alloc_struct_immovable(mem, gx_device_memory, &st_device_memory,
77
462
                        "make_mid_default");
78
462
    int code;
79
80
462
    if (midev == 0)
81
0
        return_error(gs_error_VMerror);
82
462
    gs_make_mem_mono_device(midev, mem, NULL);
83
462
    midev->bitmap_memory = mem;
84
462
    midev->width = width;
85
462
    midev->height = height;
86
462
    midev->raster = gx_device_raster((gx_device *)midev, 1);
87
462
    check_device_separable((gx_device *)midev);
88
462
    gx_device_fill_in_procs((gx_device *)midev);
89
462
    code = dev_proc(midev, open_device)((gx_device *)midev);
90
462
    if (code < 0) {
91
0
        gs_free_object(mem, midev, "make_mid_default");
92
0
        return code;
93
0
    }
94
462
    midev->is_open = true;
95
462
    dev_proc(midev, fill_rectangle)
96
462
        ((gx_device *)midev, 0, 0, width, height, (gx_color_index)0);
97
462
    *pmidev = (gx_device *)midev;
98
462
    return 0;
99
462
}
100
static IMAGE3_MAKE_MCDE_PROC(make_mcde_default);  /* check prototype */
101
static int
102
make_mcde_default(gx_device *dev, const gs_gstate *pgs,
103
                  const gs_matrix *pmat, const gs_image_common_t *pic,
104
                  const gs_int_rect *prect, const gx_drawing_color *pdcolor,
105
                  const gx_clip_path *pcpath, gs_memory_t *mem,
106
                  gx_image_enum_common_t **pinfo,
107
                  gx_device **pmcdev, gx_device *midev,
108
                  gx_image_enum_common_t *pminfo,
109
                  const gs_int_point *origin)
110
462
{
111
462
    gx_device_memory *const mdev = (gx_device_memory *)midev;
112
462
    gx_device_mask_clip *mcdev = NULL;
113
462
    gx_strip_bitmap bits; /* only gx_bitmap */
114
462
    int code;
115
116
    /* The gx_strip_bitmap structure defines (via gs_tile_bitmap_common)
117
     * rep_width and rep_height as being of type 'ushort', device width and
118
     * height are of type 'int'. Make sure we don't overflow because that
119
     * will lead to memory corruption.
120
     */
121
462
    if (mdev->width > ARCH_MAX_USHORT || mdev->height > ARCH_MAX_USHORT)
122
1
        return_error(gs_error_rangecheck);
123
124
461
    mcdev = gs_alloc_struct(mem, gx_device_mask_clip, &st_device_mask_clip,
125
461
                        "make_mcde_default");
126
127
461
    if (mcdev == 0)
128
0
        return_error(gs_error_VMerror);
129
461
    bits.data = mdev->base;
130
461
    bits.raster = mdev->raster;
131
132
461
    bits.size.x = bits.rep_width = mdev->width;
133
461
    bits.size.y = bits.rep_height = mdev->height;
134
461
    bits.id = gx_no_bitmap_id;
135
461
    bits.num_planes = 1;
136
461
    bits.rep_shift = bits.shift = 0;
137
461
    code = gx_mask_clip_initialize(mcdev, &gs_mask_clip_device,
138
461
                                   (const gx_bitmap *)&bits, dev,
139
461
                                   origin->x, origin->y, mem);
140
461
    if (code < 0) {
141
0
        gs_free_object(mem, mcdev, "make_mcde_default");
142
0
        return code;
143
0
    }
144
461
    mcdev->tiles = bits;
145
461
    code = dev_proc(mcdev, begin_typed_image)
146
461
        ((gx_device *)mcdev, pgs, pmat, pic, prect, pdcolor, pcpath, mem,
147
461
         pinfo);
148
461
    if (code < 0) {
149
0
        gs_free_object(mem, mcdev, "make_mcde_default");
150
0
        return code;
151
0
    }
152
461
    *pmcdev = (gx_device *)mcdev;
153
461
    return 0;
154
461
}
155
static int
156
gx_begin_image3(gx_device * dev,
157
                const gs_gstate * pgs, const gs_matrix * pmat,
158
                const gs_image_common_t * pic, const gs_int_rect * prect,
159
                const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
160
                gs_memory_t * mem, gx_image_enum_common_t ** pinfo)
161
471
{
162
471
    return gx_begin_image3_generic(dev, pgs, pmat, pic, prect, pdcolor,
163
471
                                   pcpath, mem, make_mid_default,
164
471
                                   make_mcde_default, pinfo);
165
471
}
166
167
/*
168
 * Begin a generic ImageType 3 image, with client handling the creation of
169
 * the mask image and mask clip devices.
170
 */
171
static bool check_image3_extent(double mask_coeff, double data_coeff);
172
int
173
gx_begin_image3_generic(gx_device * dev,
174
                        const gs_gstate *pgs, const gs_matrix *pmat,
175
                        const gs_image_common_t *pic, const gs_int_rect *prect,
176
                        const gx_drawing_color *pdcolor,
177
                        const gx_clip_path *pcpath, gs_memory_t *mem,
178
                        image3_make_mid_proc_t make_mid,
179
                        image3_make_mcde_proc_t make_mcde,
180
                        gx_image_enum_common_t **pinfo)
181
587
{
182
587
    const gs_image3_t *pim = (const gs_image3_t *)pic;
183
587
    gs_image3_t local_pim;
184
587
    gx_image3_enum_t *penum;
185
587
    gs_int_rect mask_rect, data_rect;
186
587
    gx_device *mdev = 0;
187
587
    gx_device *pcdev = 0;
188
587
    gs_image_t i_pixel, i_mask;
189
587
    gs_matrix mi_pixel, mi_mask, mat;
190
587
    gs_rect mrect;
191
587
    gs_int_point origin;
192
587
    int code;
193
194
    /* Validate the parameters. */
195
587
    if (pim->Width <= 0 || pim->MaskDict.Width <= 0 ||
196
585
        pim->Height <= 0 || pim->MaskDict.Height <= 0)
197
3
        return_error(gs_error_rangecheck);
198
584
    switch (pim->InterleaveType) {
199
0
        default:
200
0
            return_error(gs_error_rangecheck);
201
0
        case interleave_chunky:
202
0
            if (pim->MaskDict.Width != pim->Width ||
203
0
                pim->MaskDict.Height != pim->Height ||
204
0
                pim->MaskDict.BitsPerComponent != pim->BitsPerComponent ||
205
0
                pim->format != gs_image_format_chunky
206
0
                )
207
0
                return_error(gs_error_rangecheck);
208
0
            break;
209
0
        case interleave_scan_lines:
210
0
            if (pim->MaskDict.Height % pim->Height != 0 &&
211
0
                pim->Height % pim->MaskDict.Height != 0
212
0
                )
213
0
                return_error(gs_error_rangecheck);
214
            /* falls through */
215
584
        case interleave_separate_source:
216
584
            if (pim->MaskDict.BitsPerComponent != 1)
217
0
                return_error(gs_error_rangecheck);
218
584
    }
219
584
    if ((code = gs_matrix_invert(&pim->ImageMatrix, &mi_pixel)) < 0)
220
0
        return code;
221
    /* For Explicit Masking, we follow Acrobats example, and completely
222
     * ignore the supplied mask. Instead we generate a new one based on the
223
     * image mask, adjusted for any difference in width/height. */
224
584
    if (pim->InterleaveType == interleave_separate_source ||
225
584
        pim->InterleaveType == interleave_scan_lines) {
226
584
        memcpy(&local_pim, pim, sizeof(local_pim));
227
584
        pim = &local_pim;
228
584
        gs_matrix_scale(&mi_pixel,
229
584
                        ((double)pim->Width)  / pim->MaskDict.Width,
230
584
                        ((double)pim->Height) / pim->MaskDict.Height,
231
584
                        &mi_mask);
232
584
        if ((code = gs_matrix_invert(&mi_mask, &local_pim.MaskDict.ImageMatrix)) < 0)
233
0
            return code;
234
584
    } else {
235
0
        if ((code = gs_matrix_invert(&pim->MaskDict.ImageMatrix, &mi_mask)) < 0)
236
0
            return code;
237
238
0
        if (!check_image3_extent(pim->ImageMatrix.xx,
239
0
                                 pim->MaskDict.ImageMatrix.xx) ||
240
0
            !check_image3_extent(pim->ImageMatrix.xy,
241
0
                                 pim->MaskDict.ImageMatrix.xy) ||
242
0
            !check_image3_extent(pim->ImageMatrix.yx,
243
0
                                 pim->MaskDict.ImageMatrix.yx) ||
244
0
            !check_image3_extent(pim->ImageMatrix.yy,
245
0
                                 pim->MaskDict.ImageMatrix.yy)
246
0
            )
247
0
            return_error(gs_error_rangecheck);
248
0
    }
249
584
    if (fabs(mi_pixel.tx - mi_mask.tx) >= 0.5 ||
250
584
        fabs(mi_pixel.ty - mi_mask.ty) >= 0.5
251
584
        )
252
0
        return_error(gs_error_rangecheck);
253
#ifdef DEBUG
254
    {
255
        /* Although the PLRM says that the Mask and Image *must* be the same size,  */
256
        /* Adobe CPSI (and other RIPS) ignore this and process anyway. Note that we */
257
        /* are not compatible if the Mask Height than the Data (pixel) Height. CPSI */
258
        /* de-interleaves the mask from the data image and stops at the Mask Height */
259
        /* Problem detected with Genoa 468-03 (part of file 468-01.ps)              */
260
        /*****           fixme: When Data Image Height > Mask Height            *****/
261
        gs_point ep, em;
262
263
        if ((code = gs_point_transform(pim->Width, pim->Height, &mi_pixel,
264
                                       &ep)) < 0 ||
265
            (code = gs_point_transform(pim->MaskDict.Width,
266
                                       pim->MaskDict.Height, &mi_mask,
267
                                       &em)) < 0
268
            )
269
            return code;
270
        if (fabs(ep.x - em.x) >= 0.5 || fabs(ep.y - em.y) >= 0.5)
271
            code = gs_error_rangecheck; /* leave the check in for debug breakpoint */
272
    }
273
#endif /* DEBUG */
274
584
    penum = gs_alloc_struct(mem, gx_image3_enum_t, &st_image3_enum,
275
584
                            "gx_begin_image3");
276
584
    if (penum == 0)
277
0
        return_error(gs_error_VMerror);
278
584
    penum->num_components =
279
584
        gs_color_space_num_components(pim->ColorSpace);
280
584
    gx_image_enum_common_init((gx_image_enum_common_t *) penum,
281
584
                              (const gs_data_image_t *)pim,
282
584
                              &image3_enum_procs, dev,
283
584
                              1 + penum->num_components,
284
584
                              pim->format);
285
    /* Initialize pointers now in case we bail out. */
286
584
    penum->mask_data = 0;
287
584
    penum->pixel_data = 0;
288
584
    if (prect) {
289
0
        long lmw = pim->MaskDict.Width, lmh = pim->MaskDict.Height;
290
291
0
        data_rect = *prect;
292
0
        mask_rect.p.x = (int)(data_rect.p.x * lmw / pim->Width);
293
0
        mask_rect.p.y = (int)(data_rect.p.y * lmh / pim->Height);
294
0
        mask_rect.q.x = (int)((data_rect.q.x + pim->Width - 1) * lmw /
295
0
                              pim->Width);
296
0
        mask_rect.q.y = (int)((data_rect.q.y + pim->Height - 1) * lmh /
297
0
                              pim->Height);
298
584
    } else {
299
584
        mask_rect.p.x = mask_rect.p.y = 0;
300
584
        mask_rect.q.x = pim->MaskDict.Width;
301
584
        mask_rect.q.y = pim->MaskDict.Height;
302
584
        data_rect.p.x = data_rect.p.y = 0;
303
584
        data_rect.q.x = pim->Width;
304
584
        data_rect.q.y = pim->Height;
305
584
    }
306
584
    penum->mask_width = mask_rect.q.x - mask_rect.p.x;
307
584
    penum->mask_height = mask_rect.q.y - mask_rect.p.y;
308
584
    penum->mask_full_height = pim->MaskDict.Height;
309
584
    penum->mask_y = 0;
310
584
    penum->mask_skip = 0;
311
584
    penum->pixel_width = data_rect.q.x - data_rect.p.x;
312
584
    penum->pixel_height = data_rect.q.y - data_rect.p.y;
313
584
    penum->pixel_full_height = pim->Height;
314
584
    penum->pixel_y = 0;
315
584
    penum->mask_info = 0;
316
584
    penum->pixel_info = 0;
317
584
    if (pim->InterleaveType == interleave_chunky) {
318
        /* Allocate row buffers for the mask and pixel data. */
319
0
        penum->pixel_data =
320
0
            gs_alloc_bytes(mem,
321
0
                           ((size_t)penum->pixel_width * pim->BitsPerComponent *
322
0
                            penum->num_components + 7) >> 3,
323
0
                           "gx_begin_image3(pixel_data)");
324
0
        penum->mask_data =
325
0
            gs_alloc_bytes(mem, (penum->mask_width + 7) >> 3,
326
0
                           "gx_begin_image3(mask_data)");
327
0
        if (penum->pixel_data == 0 || penum->mask_data == 0) {
328
0
            code = gs_note_error(gs_error_VMerror);
329
0
            goto out1;
330
0
        }
331
        /* Because the mask data is 1 BPC, if the width is not a multiple of 8
332
         * then we will not fill the last byte of mask_data completely. This
333
         * provokes valgrind when running to pdfwrite, because pdfwrite has to
334
         * write the full byte of mask data to the file. It also means (potentially)
335
         * that we could run the same input twice and get (slightly) different
336
         * PDF files produced. So we set the last byte to zero to ensure the bits
337
         * are fully initialised. See Bug #693814
338
         */
339
0
        penum->mask_data[((penum->mask_width + 7) >> 3) - 1] = 0x00;
340
0
    }
341
584
    penum->InterleaveType = pim->InterleaveType;
342
584
    penum->bpc = pim->BitsPerComponent;
343
584
    penum->memory = mem;
344
584
    mrect.p.x = mrect.p.y = 0;
345
584
    mrect.q.x = pim->MaskDict.Width;
346
584
    mrect.q.y = pim->MaskDict.Height;
347
584
    if (pmat == 0)
348
524
        pmat = &ctm_only(pgs);
349
584
    if ((code = gs_matrix_multiply(&mi_mask, pmat, &mat)) < 0 ||
350
584
        (code = gs_bbox_transform(&mrect, &mat, &mrect)) < 0
351
584
        )
352
0
        return code;
353
354
    /* Bug 700438: If the rectangle is out of range, bail */
355
584
    if (mrect.p.x >= (double)INT_MAX || mrect.q.x <= (double)INT_MIN ||
356
575
        mrect.p.y >= (double)INT_MAX || mrect.q.y <= (double)INT_MIN ||
357
575
        mrect.p.x <= (double)INT_MIN || mrect.q.x >= (double)INT_MAX ||
358
575
        mrect.p.y <= (double)INT_MIN || mrect.q.y >= (double)INT_MAX
359
584
  ) {
360
9
            code = gs_note_error(gs_error_rangecheck);
361
9
        goto out1;
362
9
    }
363
364
    /* This code was changed for bug 686843/687411, but in a way that
365
     * a) looked wrong, and b) doesn't appear to make a difference. Revert
366
     * it to the sane version until we have evidence why not. */
367
575
    origin.x = (int)floor(mrect.p.x);
368
575
    origin.y = (int)floor(mrect.p.y);
369
575
    code = make_mid(&mdev, dev, (int)ceil(mrect.q.x) - origin.x,
370
575
                    (int)ceil(mrect.q.y) - origin.y, mem);
371
575
    if (code < 0)
372
0
        goto out1;
373
575
    penum->mdev = mdev;
374
575
    gs_image_t_init_mask(&i_mask, false);
375
575
    i_mask.adjust = false;
376
575
    {
377
575
        const gx_image_type_t *type1 = i_mask.type;
378
379
575
        *(gs_data_image_t *)&i_mask = pim->MaskDict;
380
575
        i_mask.type = type1;
381
575
        i_mask.BitsPerComponent = 1;
382
575
        i_mask.image_parent_type = gs_image_type3;
383
575
    }
384
575
    {
385
575
        gx_drawing_color dcolor;
386
575
        gs_matrix m_mat;
387
388
575
        set_nonclient_dev_color(&dcolor, 1);
389
        /*
390
         * Adjust the translation for rendering the mask to include a
391
         * negative translation by origin.{x,y} in device space.
392
         */
393
575
        m_mat = *pmat;
394
575
        m_mat.tx -= origin.x;
395
575
        m_mat.ty -= origin.y;
396
575
        i_mask.override_in_smask = (dev_proc(dev, dev_spec_op)(dev, gxdso_in_smask, NULL, 0)) > 0;
397
        /*
398
         * Note that pgs = NULL here, since we don't want to have to
399
         * create another gs_gstate with default log_op, etc.
400
         */
401
575
        code = gx_device_begin_typed_image(mdev, NULL, &m_mat,
402
575
                                           (const gs_image_common_t *)&i_mask,
403
575
                                           &mask_rect, &dcolor, NULL, mem,
404
575
                                           &penum->mask_info);
405
575
        if (code < 0)
406
0
            goto out2;
407
575
    }
408
575
    gs_image_t_init(&i_pixel, pim->ColorSpace);
409
575
    {
410
575
        const gx_image_type_t *type1 = i_pixel.type;
411
412
575
        *(gs_pixel_image_t *)&i_pixel = *(const gs_pixel_image_t *)pim;
413
575
        i_pixel.type = type1;
414
575
        i_pixel.image_parent_type = gs_image_type3;
415
575
    }
416
575
    code = make_mcde(dev, pgs, pmat, (const gs_image_common_t *)&i_pixel,
417
575
                     prect, pdcolor, pcpath, mem, &penum->pixel_info,
418
575
                     &pcdev, mdev, penum->mask_info, &origin);
419
575
    if (code < 0)
420
1
        goto out3;
421
574
    penum->pcdev = pcdev;
422
    /*
423
     * Set num_planes, plane_widths, and plane_depths from the values in the
424
     * enumerators for the mask and the image data.
425
     */
426
574
    switch (pim->InterleaveType) {
427
0
    case interleave_chunky:
428
        /* Add the mask data to the depth of the image data. */
429
0
        penum->num_planes = 1;
430
0
        penum->plane_widths[0] = pim->Width;
431
0
        penum->plane_depths[0] =
432
0
            penum->pixel_info->plane_depths[0] *
433
0
            (penum->num_components + 1) / penum->num_components;
434
0
        break;
435
0
    case interleave_scan_lines:
436
        /*
437
         * There is only 1 plane, with dynamically changing width & depth.
438
         * Initialize it for the mask data, since that is what will be
439
         * read first.
440
         */
441
0
        penum->num_planes = 1;
442
0
        penum->plane_depths[0] = 1;
443
0
        penum->plane_widths[0] = pim->MaskDict.Width;
444
0
        break;
445
574
    case interleave_separate_source:
446
        /* Insert the mask data as a separate plane before the image data. */
447
574
        penum->num_planes = penum->pixel_info->num_planes + 1;
448
574
        penum->plane_widths[0] = pim->MaskDict.Width;
449
574
        penum->plane_depths[0] = 1;
450
574
        memcpy(&penum->plane_widths[1], &penum->pixel_info->plane_widths[0],
451
574
               (penum->num_planes - 1) * sizeof(penum->plane_widths[0]));
452
574
        memcpy(&penum->plane_depths[1], &penum->pixel_info->plane_depths[0],
453
574
               (penum->num_planes - 1) * sizeof(penum->plane_depths[0]));
454
574
        break;
455
574
    }
456
574
    gx_device_retain(mdev, true); /* will free explicitly */
457
574
    gx_device_retain(pcdev, true); /* ditto */
458
574
    *pinfo = (gx_image_enum_common_t *) penum;
459
574
    return 0;
460
1
  out3:
461
1
    gx_image_end(penum->mask_info, false);
462
1
  out2:
463
1
    gs_closedevice(mdev);
464
1
    gs_free_object(mem, mdev, "gx_begin_image3(mdev)");
465
10
  out1:
466
10
    gs_free_object(mem, penum->mask_data, "gx_begin_image3(mask_data)");
467
10
    gs_free_object(mem, penum->pixel_data, "gx_begin_image3(pixel_data)");
468
10
    gs_free_object(mem, penum, "gx_begin_image3");
469
10
    return code;
470
1
}
471
static bool
472
check_image3_extent(double mask_coeff, double data_coeff)
473
0
{
474
0
    if (mask_coeff == 0)
475
0
        return data_coeff == 0;
476
0
    if (data_coeff == 0 || (mask_coeff > 0) != (data_coeff > 0))
477
0
        return false;
478
0
    return true;
479
0
}
480
481
/*
482
 * Return > 0 if we want more mask now, < 0 if we want more data now,
483
 * 0 if we want both.
484
 */
485
static int
486
planes_next(const gx_image3_enum_t *penum)
487
112k
{
488
    /*
489
     * The invariant we need to maintain is that we always have at least as
490
     * much mask as pixel data, i.e., mask_y / mask_full_height >=
491
     * pixel_y / pixel_full_height, or, to avoid floating point,
492
     * mask_y * pixel_full_height >= pixel_y * mask_full_height.
493
     * We know this condition is true now;
494
     * return a value that indicates how to maintain it.
495
     */
496
112k
    int mask_h = penum->mask_full_height;
497
112k
    int pixel_h = penum->pixel_full_height;
498
112k
    long current = penum->pixel_y * (long)mask_h -
499
112k
        penum->mask_y * (long)pixel_h;
500
501
#ifdef DEBUG
502
    if (current > 0)
503
        lprintf4("planes_next invariant fails: %d/%d > %d/%d\n",
504
                 penum->pixel_y, penum->pixel_full_height,
505
                 penum->mask_y, penum->mask_full_height);
506
#endif
507
112k
    return ((current += mask_h) <= 0 ? -1 :
508
112k
            current - pixel_h <= 0 ? 0 : 1);
509
112k
}
510
511
/* Process the next piece of an ImageType 3 image. */
512
static int
513
gx_image3_plane_data(gx_image_enum_common_t * info,
514
                     const gx_image_plane_t * planes, int height,
515
                     int *rows_used)
516
111k
{
517
111k
    gx_image3_enum_t *penum = (gx_image3_enum_t *) info;
518
111k
    int pixel_height = penum->pixel_height;
519
111k
    int pixel_used = 0;
520
111k
    int mask_height = penum->mask_height;
521
111k
    int mask_used = 0;
522
111k
    int h1 = max(pixel_height - penum->pixel_y, mask_height - penum->mask_y);
523
111k
    int h = min(height, h1);
524
111k
    const gx_image_plane_t *pixel_planes;
525
111k
    gx_image_plane_t pixel_plane, mask_plane;
526
111k
    int code = 0;
527
528
    /* Initialized rows_used in case we get an error. */
529
111k
    *rows_used = 0;
530
111k
    switch (penum->InterleaveType) {
531
0
        case interleave_chunky:
532
0
            if (h <= 0)
533
0
                return 0;
534
0
            if (h > 1) {
535
                /* Do the operation one row at a time. */
536
0
                int h_orig = h;
537
538
0
                mask_plane = planes[0];
539
0
                do {
540
0
                    code = gx_image3_plane_data(info, &mask_plane, 1,
541
0
                                                rows_used);
542
0
                    h -= *rows_used;
543
0
                    if (code)
544
0
                        break;
545
0
                    mask_plane.data += mask_plane.raster;
546
0
                } while (h);
547
0
                *rows_used = h_orig - h;
548
0
                return code;
549
0
            } {
550
                /* Pull apart the source data and the mask data. */
551
0
                int bpc = penum->bpc;
552
0
                int num_components = penum->num_components;
553
0
                int width = penum->pixel_width;
554
555
                /* We do this in the simplest (not fastest) way for now. */
556
0
                uint bit_x = bpc * (num_components + 1) * planes[0].data_x;
557
558
0
                const byte *sptr = planes[0].data + (bit_x >> 3);
559
0
                int sbit = bit_x & 7;
560
561
0
                byte *mptr = penum->mask_data;
562
0
                int mbit = 0;
563
0
                byte mbbyte = 0;
564
0
                byte *pptr = penum->pixel_data;
565
0
                int pbit = 0;
566
0
                byte pbbyte = 0;
567
0
                int x;
568
569
0
                mask_plane.data = mptr;
570
0
                mask_plane.data_x = 0;
571
0
                mask_plane.raster = 0; /* raster doesn't matter, pacify Valgrind */
572
0
                pixel_plane.data = pptr;
573
0
                pixel_plane.data_x = 0;
574
0
                pixel_plane.raster = 0; /* raster doesn't matter, pacify Valgrind */
575
0
                pixel_planes = &pixel_plane;
576
0
                for (x = 0; x < width; ++x) {
577
0
                    uint value;
578
0
                    int i;
579
580
0
                    if (sample_load_next12(&value, &sptr, &sbit, bpc) < 0)
581
0
                        return_error(gs_error_rangecheck);
582
0
                    if (sample_store_next12(value != 0, &mptr, &mbit, 1, &mbbyte) < 0)
583
0
                        return_error(gs_error_rangecheck);
584
0
                    for (i = 0; i < num_components; ++i) {
585
0
                        if (sample_load_next12(&value, &sptr, &sbit, bpc) < 0)
586
0
                            return_error(gs_error_rangecheck);
587
0
                        if (sample_store_next12(value, &pptr, &pbit, bpc, &pbbyte) < 0)
588
0
                            return_error (gs_error_rangecheck);
589
0
                    }
590
0
                }
591
0
                sample_store_flush(mptr, mbit, mbbyte);
592
0
                sample_store_flush(pptr, pbit, pbbyte);
593
0
            }
594
0
            break;
595
0
        case interleave_scan_lines:
596
0
            if (planes_next(penum) >= 0) {
597
                /* This is mask data. */
598
0
                mask_plane = planes[0];
599
0
                pixel_planes = &pixel_plane;
600
0
                pixel_plane.data = 0;
601
0
            } else {
602
                /* This is pixel data. */
603
0
                mask_plane.data = 0;
604
0
                pixel_planes = planes;
605
0
            }
606
0
            break;
607
111k
        case interleave_separate_source:
608
            /*
609
             * In order to be able to recover from interruptions, we must
610
             * limit separate-source processing to 1 scan line at a time.
611
             */
612
111k
            if (h > 1)
613
0
                h = 1;
614
111k
            mask_plane = planes[0];
615
111k
            pixel_planes = planes + 1;
616
111k
            break;
617
0
        default:    /* not possible */
618
0
            return_error(gs_error_rangecheck);
619
111k
    }
620
    /*
621
     * Process the mask data first, so it will set up the mask
622
     * device for clipping the pixel data.
623
     */
624
111k
    if (mask_plane.data) {
625
        /*
626
         * If, on the last call, we processed some mask rows successfully
627
         * but processing the pixel rows was interrupted, we set rows_used
628
         * to indicate the number of pixel rows processed (since there is
629
         * no way to return two rows_used values).  If this happened, some
630
         * mask rows may get presented again.  We must skip over them
631
         * rather than processing them again.
632
         */
633
111k
        int skip = penum->mask_skip;
634
635
111k
        if (skip >= h) {
636
0
            penum->mask_skip = skip - (mask_used = h);
637
111k
        } else {
638
111k
            int mask_h = h - skip;
639
640
111k
            mask_plane.data += skip * mask_plane.raster;
641
111k
            penum->mask_skip = 0;
642
111k
            code = gx_image_plane_data_rows(penum->mask_info, &mask_plane,
643
111k
                                            mask_h, &mask_used);
644
111k
            mask_used += skip;
645
111k
        }
646
111k
        *rows_used = mask_used;
647
111k
        penum->mask_y += mask_used;
648
111k
        if (code < 0)
649
0
            return code;
650
111k
    }
651
111k
    if (pixel_planes[0].data) {
652
        /*
653
         * If necessary, flush any buffered mask data to the mask clipping
654
         * device.
655
         */
656
99.3k
        gx_image_flush(penum->mask_info);
657
99.3k
        code = gx_image_plane_data_rows(penum->pixel_info, pixel_planes, h,
658
99.3k
                                        &pixel_used);
659
        /*
660
         * There isn't any way to set rows_used if different amounts of
661
         * the mask and pixel data were used.  Fake it.
662
         */
663
99.3k
        *rows_used = pixel_used;
664
        /*
665
         * Don't return code yet: we must account for the fact that
666
         * some mask data may have been processed.
667
         */
668
99.3k
        penum->pixel_y += pixel_used;
669
99.3k
        if (code < 0) {
670
            /*
671
             * We must prevent the mask data from being processed again.
672
             * We rely on the fact that h > 1 is only possible if the
673
             * mask and pixel data have the same Y scaling.
674
             */
675
0
            if (mask_used > pixel_used) {
676
0
                int skip = mask_used - pixel_used;
677
678
0
                penum->mask_skip = skip;
679
0
                penum->mask_y -= skip;
680
0
                mask_used = pixel_used;
681
0
            }
682
0
        }
683
99.3k
    }
684
111k
    if_debug5m('b', penum->memory, "[b]image3 h=%d %smask_y=%d %spixel_y=%d\n",
685
111k
               h, (mask_plane.data ? "+" : ""), penum->mask_y,
686
111k
               (pixel_planes[0].data ? "+" : ""), penum->pixel_y);
687
111k
    if (penum->mask_y >= penum->mask_height &&
688
176
        penum->pixel_y >= penum->pixel_height)
689
176
        return 1;
690
111k
    if (penum->InterleaveType == interleave_scan_lines) {
691
        /* Update the width and depth in the enumerator. */
692
0
        if (planes_next(penum) >= 0) {  /* want mask data next */
693
0
            penum->plane_widths[0] = penum->mask_width;
694
0
            penum->plane_depths[0] = 1;
695
0
        } else {   /* want pixel data next */
696
0
            penum->plane_widths[0] = penum->pixel_width;
697
0
            penum->plane_depths[0] = penum->pixel_info->plane_depths[0];
698
0
        }
699
0
    }
700
    /*
701
     * The mask may be complete (gx_image_plane_data_rows returned 1),
702
     * but there may still be pixel rows to go, so don't return 1 here.
703
     */
704
111k
    return (code < 0 ? code : 0);
705
111k
}
706
707
/* Flush buffered data. */
708
static int
709
gx_image3_flush(gx_image_enum_common_t * info)
710
0
{
711
0
    gx_image3_enum_t * const penum = (gx_image3_enum_t *) info;
712
0
    int code = gx_image_flush(penum->mask_info);
713
714
0
    if (code >= 0)
715
0
        code = gx_image_flush(penum->pixel_info);
716
0
    return code;
717
0
}
718
719
/* Determine which data planes are wanted. */
720
static bool
721
gx_image3_planes_wanted(const gx_image_enum_common_t * info, byte *wanted)
722
112k
{
723
112k
    const gx_image3_enum_t * const penum = (const gx_image3_enum_t *) info;
724
725
112k
    switch (penum->InterleaveType) {
726
0
    case interleave_chunky: /* only 1 plane */
727
0
        wanted[0] = 0xff;
728
0
        return true;
729
0
    case interleave_scan_lines: /* only 1 plane, but varying width/depth */
730
0
        wanted[0] = 0xff;
731
0
        return false;
732
112k
    case interleave_separate_source: {
733
        /*
734
         * We always want at least as much of the mask to be filled as the
735
         * pixel data.  next > 0 iff we've processed more data than mask.
736
         * Plane 0 is the mask, planes [1 .. num_planes - 1] are pixel data.
737
         */
738
112k
        int next = planes_next(penum);
739
740
112k
        wanted[0] = (next >= 0 ? 0xff : 0);
741
112k
        memset(wanted + 1, (next <= 0 ? 0xff : 0), info->num_planes - 1);
742
        /*
743
         * In principle, wanted will always be true for both mask and pixel
744
         * data if the full_heights are equal.  Unfortunately, even in this
745
         * case, processing may be interrupted after a mask row has been
746
         * passed to the underlying image processor but before the data row
747
         * has been passed, in which case pixel data will be 'wanted', but
748
         * not mask data, for the next call.  Therefore, we must return
749
         * false.
750
         */
751
112k
        return false
752
            /*(next == 0 &&
753
0
              penum->mask_full_height == penum->pixel_full_height)*/;
754
0
    }
755
0
    default:      /* can't happen */
756
0
        memset(wanted, 0, info->num_planes);
757
0
        return false;
758
112k
    }
759
112k
}
760
761
/* Clean up after processing an ImageType 3 image. */
762
static int
763
gx_image3_end_image(gx_image_enum_common_t * info, bool draw_last)
764
574
{
765
574
    gx_image3_enum_t *penum = (gx_image3_enum_t *) info;
766
574
    gs_memory_t *mem = penum->memory;
767
574
    gx_device *mdev = penum->mdev;
768
574
    int mcode = gx_image_end(penum->mask_info, draw_last);
769
574
    gx_device *pcdev = penum->pcdev;
770
574
    int pcode = gx_image_end(penum->pixel_info, draw_last);
771
574
    int code1 = gs_closedevice(pcdev);
772
574
    int code2 = gs_closedevice(mdev);
773
774
574
    gs_free_object(mem, penum->mask_data,
775
574
                   "gx_image3_end_image(mask_data)");
776
574
    gs_free_object(mem, penum->pixel_data,
777
574
                   "gx_image3_end_image(pixel_data)");
778
574
    gs_free_object(mem, pcdev, "gx_image3_end_image(pcdev)");
779
574
    gs_free_object(mem, mdev, "gx_image3_end_image(mdev)");
780
574
    gx_image_free_enum(&info);
781
574
    return (pcode < 0 ? pcode : mcode < 0 ? mcode : code1 < 0 ? code1 : code2);
782
574
}