summaryrefslogtreecommitdiff
blob: 522c71ffdc12906230057cd58f92732e1c4366e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/* Copyright (C) 2001-2020 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


/* ImageType 3 image implementation */
#include "math_.h"		/* for ceil, floor */
#include "memory_.h"
#include "gx.h"
#include "gserrors.h"
#include "gsbitops.h"
#include "gscspace.h"
#include "gsstruct.h"
#include "gxdevice.h"
#include "gxdevmem.h"
#include "gxclipm.h"
#include "gximage3.h"
#include "gxgstate.h"
#include "gxdevsop.h"
#include <limits.h> /* For INT_MAX etc */

/* Forward references */
static dev_proc_begin_typed_image(gx_begin_image3);
static image_enum_proc_plane_data(gx_image3_plane_data);
static image_enum_proc_end_image(gx_image3_end_image);
static image_enum_proc_flush(gx_image3_flush);
static image_enum_proc_planes_wanted(gx_image3_planes_wanted);

/* GC descriptor */
private_st_gs_image3();

/* Define the image type for ImageType 3 images. */
const gx_image_type_t gs_image_type_3 = {
    &st_gs_image3, gx_begin_image3, gx_data_image_source_size,
    gx_image_no_sput, gx_image_no_sget, gx_image_default_release, 3
};
static const gx_image_enum_procs_t image3_enum_procs = {
    gx_image3_plane_data, gx_image3_end_image,
    gx_image3_flush, gx_image3_planes_wanted
};

/* Initialize an ImageType 3 image. */
void
gs_image3_t_init(gs_image3_t * pim, gs_color_space * color_space,
                 gs_image3_interleave_type_t interleave_type)
{
    gs_pixel_image_t_init((gs_pixel_image_t *) pim, color_space);
    pim->type = &gs_image_type_3;
    pim->InterleaveType = interleave_type;
    gs_data_image_t_init(&pim->MaskDict, -1);
}

/*
 * We implement ImageType 3 images by interposing a mask clipper in
 * front of an ordinary ImageType 1 image.  Note that we build up the
 * mask row-by-row as we are processing the image.
 *
 * We export a generalized form of the begin_image procedure for use by
 * the PDF and PostScript writers.
 */
typedef struct gx_image3_enum_s {
    gx_image_enum_common;
    gx_device *mdev;		/* gx_device_memory in default impl. */
    gx_device *pcdev;		/* gx_device_mask_clip in default impl. */
    gx_image_enum_common_t *mask_info;
    gx_image_enum_common_t *pixel_info;
    gs_image3_interleave_type_t InterleaveType;
    int num_components;		/* (not counting mask) */
    int bpc;			/* BitsPerComponent */
    int mask_width, mask_height, mask_full_height;
    int pixel_width, pixel_height, pixel_full_height;
    byte *mask_data;		/* (if chunky) */
    byte *pixel_data;		/* (if chunky) */
    /* The following are the only members that change dynamically. */
    int mask_y;
    int pixel_y;
    int mask_skip;		/* # of mask rows to skip, see below */
} gx_image3_enum_t;

extern_st(st_gx_image_enum_common);
gs_private_st_suffix_add6(st_image3_enum, gx_image3_enum_t, "gx_image3_enum_t",
  image3_enum_enum_ptrs, image3_enum_reloc_ptrs, st_gx_image_enum_common,
  mdev, pcdev, pixel_info, mask_info, pixel_data, mask_data);

/* Define the default implementation of ImageType 3 processing. */
static IMAGE3_MAKE_MID_PROC(make_mid_default); /* check prototype */
static int
make_mid_default(gx_device **pmidev, gx_device *dev, int width, int height,
                 gs_memory_t *mem)
{
    gx_device_memory *midev =
        gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
                        "make_mid_default");
    int code;

    if (midev == 0)
        return_error(gs_error_VMerror);
    gs_make_mem_mono_device(midev, mem, NULL);
    midev->bitmap_memory = mem;
    midev->width = width;
    midev->height = height;
    midev->raster = gx_device_raster((gx_device *)midev, 1);
    check_device_separable((gx_device *)midev);
    gx_device_fill_in_procs((gx_device *)midev);
    code = dev_proc(midev, open_device)((gx_device *)midev);
    if (code < 0) {
        gs_free_object(mem, midev, "make_mid_default");
        return code;
    }
    midev->is_open = true;
    dev_proc(midev, fill_rectangle)
        ((gx_device *)midev, 0, 0, width, height, (gx_color_index)0);
    *pmidev = (gx_device *)midev;
    return 0;
}
static IMAGE3_MAKE_MCDE_PROC(make_mcde_default);  /* check prototype */
static int
make_mcde_default(gx_device *dev, const gs_gstate *pgs,
                  const gs_matrix *pmat, const gs_image_common_t *pic,
                  const gs_int_rect *prect, const gx_drawing_color *pdcolor,
                  const gx_clip_path *pcpath, gs_memory_t *mem,
                  gx_image_enum_common_t **pinfo,
                  gx_device **pmcdev, gx_device *midev,
                  gx_image_enum_common_t *pminfo,
                  const gs_int_point *origin)
{
    gx_device_memory *const mdev = (gx_device_memory *)midev;
    gx_device_mask_clip *mcdev =
        gs_alloc_struct(mem, gx_device_mask_clip, &st_device_mask_clip,
                        "make_mcde_default");
    gx_strip_bitmap bits;	/* only gx_bitmap */
    int code;

    if (mcdev == 0)
        return_error(gs_error_VMerror);
    bits.data = mdev->base;
    bits.raster = mdev->raster;
    bits.size.x = mdev->width;
    bits.size.y = mdev->height;
    bits.id = gx_no_bitmap_id;
    bits.num_planes = 1;
    code = gx_mask_clip_initialize(mcdev, &gs_mask_clip_device,
                                   (const gx_bitmap *)&bits, dev,
                                   origin->x, origin->y, mem);
    if (code < 0) {
        gs_free_object(mem, mcdev, "make_mcde_default");
        return code;
    }
    mcdev->tiles = bits;
    code = dev_proc(mcdev, begin_typed_image)
        ((gx_device *)mcdev, pgs, pmat, pic, prect, pdcolor, pcpath, mem,
         pinfo);
    if (code < 0) {
        gs_free_object(mem, mcdev, "make_mcde_default");
        return code;
    }
    *pmcdev = (gx_device *)mcdev;
    return 0;
}
static int
gx_begin_image3(gx_device * dev,
                const gs_gstate * pgs, const gs_matrix * pmat,
                const gs_image_common_t * pic, const gs_int_rect * prect,
                const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
                gs_memory_t * mem, gx_image_enum_common_t ** pinfo)
{
    return gx_begin_image3_generic(dev, pgs, pmat, pic, prect, pdcolor,
                                   pcpath, mem, make_mid_default,
                                   make_mcde_default, pinfo);
}

/*
 * Begin a generic ImageType 3 image, with client handling the creation of
 * the mask image and mask clip devices.
 */
static bool check_image3_extent(double mask_coeff, double data_coeff);
int
gx_begin_image3_generic(gx_device * dev,
                        const gs_gstate *pgs, const gs_matrix *pmat,
                        const gs_image_common_t *pic, const gs_int_rect *prect,
                        const gx_drawing_color *pdcolor,
                        const gx_clip_path *pcpath, gs_memory_t *mem,
                        image3_make_mid_proc_t make_mid,
                        image3_make_mcde_proc_t make_mcde,
                        gx_image_enum_common_t **pinfo)
{
    const gs_image3_t *pim = (const gs_image3_t *)pic;
    gs_image3_t local_pim;
    gx_image3_enum_t *penum;
    gs_int_rect mask_rect, data_rect;
    gx_device *mdev = 0;
    gx_device *pcdev = 0;
    gs_image_t i_pixel, i_mask;
    gs_matrix mi_pixel, mi_mask, mat;
    gs_rect mrect;
    gs_int_point origin;
    int code;

    /* Validate the parameters. */
    if (pim->Height <= 0 || pim->MaskDict.Height <= 0)
        return_error(gs_error_rangecheck);
    switch (pim->InterleaveType) {
        default:
            return_error(gs_error_rangecheck);
        case interleave_chunky:
            if (pim->MaskDict.Width != pim->Width ||
                pim->MaskDict.Height != pim->Height ||
                pim->MaskDict.BitsPerComponent != pim->BitsPerComponent ||
                pim->format != gs_image_format_chunky
                )
                return_error(gs_error_rangecheck);
            break;
        case interleave_scan_lines:
            if (pim->MaskDict.Height % pim->Height != 0 &&
                pim->Height % pim->MaskDict.Height != 0
                )
                return_error(gs_error_rangecheck);
            /* falls through */
        case interleave_separate_source:
            if (pim->MaskDict.BitsPerComponent != 1)
                return_error(gs_error_rangecheck);
    }
    if ((code = gs_matrix_invert(&pim->ImageMatrix, &mi_pixel)) < 0)
        return code;
    /* For Explicit Masking, we follow Acrobats example, and completely
     * ignore the supplied mask. Instead we generate a new one based on the
     * image mask, adjusted for any difference in width/height. */
    if (pim->InterleaveType == interleave_separate_source ||
        pim->InterleaveType == interleave_scan_lines) {
        memcpy(&local_pim, pim, sizeof(local_pim));
        pim = &local_pim;
        gs_matrix_scale(&mi_pixel,
                        ((double)pim->Width)  / pim->MaskDict.Width,
                        ((double)pim->Height) / pim->MaskDict.Height,
                        &mi_mask);
        if ((code = gs_matrix_invert(&mi_mask, &local_pim.MaskDict.ImageMatrix)) < 0)
            return code;
    } else {
        if ((code = gs_matrix_invert(&pim->MaskDict.ImageMatrix, &mi_mask)) < 0)
            return code;

        if (!check_image3_extent(pim->ImageMatrix.xx,
                                 pim->MaskDict.ImageMatrix.xx) ||
            !check_image3_extent(pim->ImageMatrix.xy,
                                 pim->MaskDict.ImageMatrix.xy) ||
            !check_image3_extent(pim->ImageMatrix.yx,
                                 pim->MaskDict.ImageMatrix.yx) ||
            !check_image3_extent(pim->ImageMatrix.yy,
                                 pim->MaskDict.ImageMatrix.yy)
            )
            return_error(gs_error_rangecheck);
    }
    if (fabs(mi_pixel.tx - mi_mask.tx) >= 0.5 ||
        fabs(mi_pixel.ty - mi_mask.ty) >= 0.5
        )
        return_error(gs_error_rangecheck);
#ifdef DEBUG
    {
        /* Although the PLRM says that the Mask and Image *must* be the same size,  */
        /* Adobe CPSI (and other RIPS) ignore this and process anyway. Note that we */
        /* are not compatible if the Mask Height than the Data (pixel) Height. CPSI */
        /* de-interleaves the mask from the data image and stops at the Mask Height */
        /* Problem detected with Genoa 468-03 (part of file 468-01.ps)              */
        /*****           fixme: When Data Image Height > Mask Height            *****/
        gs_point ep, em;

        if ((code = gs_point_transform(pim->Width, pim->Height, &mi_pixel,
                                       &ep)) < 0 ||
            (code = gs_point_transform(pim->MaskDict.Width,
                                       pim->MaskDict.Height, &mi_mask,
                                       &em)) < 0
            )
            return code;
        if (fabs(ep.x - em.x) >= 0.5 || fabs(ep.y - em.y) >= 0.5)
            code = gs_error_rangecheck;	/* leave the check in for debug breakpoint */
    }
#endif /* DEBUG */
    penum = gs_alloc_struct(mem, gx_image3_enum_t, &st_image3_enum,
                            "gx_begin_image3");
    if (penum == 0)
        return_error(gs_error_VMerror);
    penum->num_components =
        gs_color_space_num_components(pim->ColorSpace);
    gx_image_enum_common_init((gx_image_enum_common_t *) penum,
                              (const gs_data_image_t *)pim,
                              &image3_enum_procs, dev,
                              1 + penum->num_components,
                              pim->format);
    /* Initialize pointers now in case we bail out. */
    penum->mask_data = 0;
    penum->pixel_data = 0;
    if (prect) {
        long lmw = pim->MaskDict.Width, lmh = pim->MaskDict.Height;

        data_rect = *prect;
        mask_rect.p.x = (int)(data_rect.p.x * lmw / pim->Width);
        mask_rect.p.y = (int)(data_rect.p.y * lmh / pim->Height);
        mask_rect.q.x = (int)((data_rect.q.x + pim->Width - 1) * lmw /
                              pim->Width);
        mask_rect.q.y = (int)((data_rect.q.y + pim->Height - 1) * lmh /
                              pim->Height);
    } else {
        mask_rect.p.x = mask_rect.p.y = 0;
        mask_rect.q.x = pim->MaskDict.Width;
        mask_rect.q.y = pim->MaskDict.Height;
        data_rect.p.x = data_rect.p.y = 0;
        data_rect.q.x = pim->Width;
        data_rect.q.y = pim->Height;
    }
    penum->mask_width = mask_rect.q.x - mask_rect.p.x;
    penum->mask_height = mask_rect.q.y - mask_rect.p.y;
    penum->mask_full_height = pim->MaskDict.Height;
    penum->mask_y = 0;
    penum->mask_skip = 0;
    penum->pixel_width = data_rect.q.x - data_rect.p.x;
    penum->pixel_height = data_rect.q.y - data_rect.p.y;
    penum->pixel_full_height = pim->Height;
    penum->pixel_y = 0;
    penum->mask_info = 0;
    penum->pixel_info = 0;
    if (pim->InterleaveType == interleave_chunky) {
        /* Allocate row buffers for the mask and pixel data. */
        penum->pixel_data =
            gs_alloc_bytes(mem,
                           (penum->pixel_width * pim->BitsPerComponent *
                            penum->num_components + 7) >> 3,
                           "gx_begin_image3(pixel_data)");
        penum->mask_data =
            gs_alloc_bytes(mem, (penum->mask_width + 7) >> 3,
                           "gx_begin_image3(mask_data)");
        if (penum->pixel_data == 0 || penum->mask_data == 0) {
            code = gs_note_error(gs_error_VMerror);
            goto out1;
        }
        /* Because the mask data is 1 BPC, if the width is not a multiple of 8
         * then we will not fill the last byte of mask_data completely. This
         * provokes valgrind when running to pdfwrite, because pdfwrite has to
         * write the full byte of mask data to the file. It also means (potentially)
         * that we could run the same input twice and get (slightly) different
         * PDF files produced. So we set the last byte to zero to ensure the bits
         * are fully initialised. See Bug #693814
         */
        penum->mask_data[((penum->mask_width + 7) >> 3) - 1] = 0x00;
    }
    penum->InterleaveType = pim->InterleaveType;
    penum->bpc = pim->BitsPerComponent;
    penum->memory = mem;
    mrect.p.x = mrect.p.y = 0;
    mrect.q.x = pim->MaskDict.Width;
    mrect.q.y = pim->MaskDict.Height;
    if (pmat == 0)
        pmat = &ctm_only(pgs);
    if ((code = gs_matrix_multiply(&mi_mask, pmat, &mat)) < 0 ||
        (code = gs_bbox_transform(&mrect, &mat, &mrect)) < 0
        )
        return code;

    /* Bug 700438: If the rectangle is out of range, bail */
    if (mrect.p.x >= (double)INT_MAX || mrect.q.x <= (double)INT_MIN ||
        mrect.p.y >= (double)INT_MAX || mrect.q.y <= (double)INT_MIN) {
            code = gs_note_error(gs_error_rangecheck);
        goto out1;
    }

    /* This code was changed for bug 686843/687411, but in a way that
     * a) looked wrong, and b) doesn't appear to make a difference. Revert
     * it to the sane version until we have evidence why not. */
    origin.x = (int)floor(mrect.p.x);
    origin.y = (int)floor(mrect.p.y);
    code = make_mid(&mdev, dev, (int)ceil(mrect.q.x) - origin.x,
                    (int)ceil(mrect.q.y) - origin.y, mem);
    if (code < 0)
        goto out1;
    penum->mdev = mdev;
    gs_image_t_init_mask(&i_mask, false);
    i_mask.adjust = false;
    {
        const gx_image_type_t *type1 = i_mask.type;

        *(gs_data_image_t *)&i_mask = pim->MaskDict;
        i_mask.type = type1;
        i_mask.BitsPerComponent = 1;
        i_mask.image_parent_type = gs_image_type3;
    }
    {
        gx_drawing_color dcolor;
        gs_matrix m_mat;

        set_nonclient_dev_color(&dcolor, 1);
        /*
         * Adjust the translation for rendering the mask to include a
         * negative translation by origin.{x,y} in device space.
         */
        m_mat = *pmat;
        m_mat.tx -= origin.x;
        m_mat.ty -= origin.y;
        i_mask.override_in_smask = (dev_proc(dev, dev_spec_op)(dev, gxdso_in_smask, NULL, 0)) > 0;
        /*
         * Note that pgs = NULL here, since we don't want to have to
         * create another gs_gstate with default log_op, etc.
         */
        code = gx_device_begin_typed_image(mdev, NULL, &m_mat,
                                           (const gs_image_common_t *)&i_mask,
                                           &mask_rect, &dcolor, NULL, mem,
                                           &penum->mask_info);
        if (code < 0)
            goto out2;
    }
    gs_image_t_init(&i_pixel, pim->ColorSpace);
    {
        const gx_image_type_t *type1 = i_pixel.type;

        *(gs_pixel_image_t *)&i_pixel = *(const gs_pixel_image_t *)pim;
        i_pixel.type = type1;
        i_pixel.image_parent_type = gs_image_type3;
    }
    code = make_mcde(dev, pgs, pmat, (const gs_image_common_t *)&i_pixel,
                     prect, pdcolor, pcpath, mem, &penum->pixel_info,
                     &pcdev, mdev, penum->mask_info, &origin);
    if (code < 0)
        goto out3;
    penum->pcdev = pcdev;
    /*
     * Set num_planes, plane_widths, and plane_depths from the values in the
     * enumerators for the mask and the image data.
     */
    switch (pim->InterleaveType) {
    case interleave_chunky:
        /* Add the mask data to the depth of the image data. */
        penum->num_planes = 1;
        penum->plane_widths[0] = pim->Width;
        penum->plane_depths[0] =
            penum->pixel_info->plane_depths[0] *
            (penum->num_components + 1) / penum->num_components;
        break;
    case interleave_scan_lines:
        /*
         * There is only 1 plane, with dynamically changing width & depth.
         * Initialize it for the mask data, since that is what will be
         * read first.
         */
        penum->num_planes = 1;
        penum->plane_depths[0] = 1;
        penum->plane_widths[0] = pim->MaskDict.Width;
        break;
    case interleave_separate_source:
        /* Insert the mask data as a separate plane before the image data. */
        penum->num_planes = penum->pixel_info->num_planes + 1;
        penum->plane_widths[0] = pim->MaskDict.Width;
        penum->plane_depths[0] = 1;
        memcpy(&penum->plane_widths[1], &penum->pixel_info->plane_widths[0],
               (penum->num_planes - 1) * sizeof(penum->plane_widths[0]));
        memcpy(&penum->plane_depths[1], &penum->pixel_info->plane_depths[0],
               (penum->num_planes - 1) * sizeof(penum->plane_depths[0]));
        break;
    }
    gx_device_retain(mdev, true); /* will free explicitly */
    gx_device_retain(pcdev, true); /* ditto */
    *pinfo = (gx_image_enum_common_t *) penum;
    return 0;
  out3:
    gx_image_end(penum->mask_info, false);
  out2:
    gs_closedevice(mdev);
    gs_free_object(mem, mdev, "gx_begin_image3(mdev)");
  out1:
    gs_free_object(mem, penum->mask_data, "gx_begin_image3(mask_data)");
    gs_free_object(mem, penum->pixel_data, "gx_begin_image3(pixel_data)");
    gs_free_object(mem, penum, "gx_begin_image3");
    return code;
}
static bool
check_image3_extent(double mask_coeff, double data_coeff)
{
    if (mask_coeff == 0)
        return data_coeff == 0;
    if (data_coeff == 0 || (mask_coeff > 0) != (data_coeff > 0))
        return false;
    return true;
}

/*
 * Return > 0 if we want more mask now, < 0 if we want more data now,
 * 0 if we want both.
 */
static int
planes_next(const gx_image3_enum_t *penum)
{
    /*
     * The invariant we need to maintain is that we always have at least as
     * much mask as pixel data, i.e., mask_y / mask_full_height >=
     * pixel_y / pixel_full_height, or, to avoid floating point,
     * mask_y * pixel_full_height >= pixel_y * mask_full_height.
     * We know this condition is true now;
     * return a value that indicates how to maintain it.
     */
    int mask_h = penum->mask_full_height;
    int pixel_h = penum->pixel_full_height;
    long current = penum->pixel_y * (long)mask_h -
        penum->mask_y * (long)pixel_h;

#ifdef DEBUG
    if (current > 0)
        lprintf4("planes_next invariant fails: %d/%d > %d/%d\n",
                 penum->pixel_y, penum->pixel_full_height,
                 penum->mask_y, penum->mask_full_height);
#endif
    return ((current += mask_h) <= 0 ? -1 :
            current - pixel_h <= 0 ? 0 : 1);
}

/* Process the next piece of an ImageType 3 image. */
static int
gx_image3_plane_data(gx_image_enum_common_t * info,
                     const gx_image_plane_t * planes, int height,
                     int *rows_used)
{
    gx_image3_enum_t *penum = (gx_image3_enum_t *) info;
    int pixel_height = penum->pixel_height;
    int pixel_used = 0;
    int mask_height = penum->mask_height;
    int mask_used = 0;
    int h1 = max(pixel_height - penum->pixel_y, mask_height - penum->mask_y);
    int h = min(height, h1);
    const gx_image_plane_t *pixel_planes;
    gx_image_plane_t pixel_plane, mask_plane;
    int code = 0;

    /* Initialized rows_used in case we get an error. */
    *rows_used = 0;
    switch (penum->InterleaveType) {
        case interleave_chunky:
            if (h <= 0)
                return 0;
            if (h > 1) {
                /* Do the operation one row at a time. */
                int h_orig = h;

                mask_plane = planes[0];
                do {
                    code = gx_image3_plane_data(info, &mask_plane, 1,
                                                rows_used);
                    h -= *rows_used;
                    if (code)
                        break;
                    mask_plane.data += mask_plane.raster;
                } while (h);
                *rows_used = h_orig - h;
                return code;
            } {
                /* Pull apart the source data and the mask data. */
                int bpc = penum->bpc;
                int num_components = penum->num_components;
                int width = penum->pixel_width;

                /* We do this in the simplest (not fastest) way for now. */
                uint bit_x = bpc * (num_components + 1) * planes[0].data_x;

                const byte *sptr = planes[0].data + (bit_x >> 3);
                int sbit = bit_x & 7;

                byte *mptr = penum->mask_data;
                int mbit = 0;
                byte mbbyte = 0;
                byte *pptr = penum->pixel_data;
                int pbit = 0;
                byte pbbyte = 0;
                int x;

                mask_plane.data = mptr;
                mask_plane.data_x = 0;
                mask_plane.raster = 0; /* raster doesn't matter, pacify Valgrind */
                pixel_plane.data = pptr;
                pixel_plane.data_x = 0;
                pixel_plane.raster = 0; /* raster doesn't matter, pacify Valgrind */
                pixel_planes = &pixel_plane;
                for (x = 0; x < width; ++x) {
                    uint value;
                    int i;

                    if (sample_load_next12(&value, &sptr, &sbit, bpc) < 0)
                        return_error(gs_error_rangecheck);
                    if (sample_store_next12(value != 0, &mptr, &mbit, 1, &mbbyte) < 0)
                        return_error(gs_error_rangecheck);
                    for (i = 0; i < num_components; ++i) {
                        if (sample_load_next12(&value, &sptr, &sbit, bpc) < 0)
                            return_error(gs_error_rangecheck);
                        if (sample_store_next12(value, &pptr, &pbit, bpc, &pbbyte) < 0)
                            return_error (gs_error_rangecheck);
                    }
                }
                sample_store_flush(mptr, mbit, mbbyte);
                sample_store_flush(pptr, pbit, pbbyte);
            }
            break;
        case interleave_scan_lines:
            if (planes_next(penum) >= 0) {
                /* This is mask data. */
                mask_plane = planes[0];
                pixel_planes = &pixel_plane;
                pixel_plane.data = 0;
            } else {
                /* This is pixel data. */
                mask_plane.data = 0;
                pixel_planes = planes;
            }
            break;
        case interleave_separate_source:
            /*
             * In order to be able to recover from interruptions, we must
             * limit separate-source processing to 1 scan line at a time.
             */
            if (h > 1)
                h = 1;
            mask_plane = planes[0];
            pixel_planes = planes + 1;
            break;
        default:		/* not possible */
            return_error(gs_error_rangecheck);
    }
    /*
     * Process the mask data first, so it will set up the mask
     * device for clipping the pixel data.
     */
    if (mask_plane.data) {
        /*
         * If, on the last call, we processed some mask rows successfully
         * but processing the pixel rows was interrupted, we set rows_used
         * to indicate the number of pixel rows processed (since there is
         * no way to return two rows_used values).  If this happened, some
         * mask rows may get presented again.  We must skip over them
         * rather than processing them again.
         */
        int skip = penum->mask_skip;

        if (skip >= h) {
            penum->mask_skip = skip - (mask_used = h);
        } else {
            int mask_h = h - skip;

            mask_plane.data += skip * mask_plane.raster;
            penum->mask_skip = 0;
            code = gx_image_plane_data_rows(penum->mask_info, &mask_plane,
                                            mask_h, &mask_used);
            mask_used += skip;
        }
        *rows_used = mask_used;
        penum->mask_y += mask_used;
        if (code < 0)
            return code;
    }
    if (pixel_planes[0].data) {
        /*
         * If necessary, flush any buffered mask data to the mask clipping
         * device.
         */
        gx_image_flush(penum->mask_info);
        code = gx_image_plane_data_rows(penum->pixel_info, pixel_planes, h,
                                        &pixel_used);
        /*
         * There isn't any way to set rows_used if different amounts of
         * the mask and pixel data were used.  Fake it.
         */
        *rows_used = pixel_used;
        /*
         * Don't return code yet: we must account for the fact that
         * some mask data may have been processed.
         */
        penum->pixel_y += pixel_used;
        if (code < 0) {
            /*
             * We must prevent the mask data from being processed again.
             * We rely on the fact that h > 1 is only possible if the
             * mask and pixel data have the same Y scaling.
             */
            if (mask_used > pixel_used) {
                int skip = mask_used - pixel_used;

                penum->mask_skip = skip;
                penum->mask_y -= skip;
                mask_used = pixel_used;
            }
        }
    }
    if_debug5m('b', penum->memory, "[b]image3 h=%d %smask_y=%d %spixel_y=%d\n",
               h, (mask_plane.data ? "+" : ""), penum->mask_y,
               (pixel_planes[0].data ? "+" : ""), penum->pixel_y);
    if (penum->mask_y >= penum->mask_height &&
        penum->pixel_y >= penum->pixel_height)
        return 1;
    if (penum->InterleaveType == interleave_scan_lines) {
        /* Update the width and depth in the enumerator. */
        if (planes_next(penum) >= 0) {  /* want mask data next */
            penum->plane_widths[0] = penum->mask_width;
            penum->plane_depths[0] = 1;
        } else {		/* want pixel data next */
            penum->plane_widths[0] = penum->pixel_width;
            penum->plane_depths[0] = penum->pixel_info->plane_depths[0];
        }
    }
    /*
     * The mask may be complete (gx_image_plane_data_rows returned 1),
     * but there may still be pixel rows to go, so don't return 1 here.
     */
    return (code < 0 ? code : 0);
}

/* Flush buffered data. */
static int
gx_image3_flush(gx_image_enum_common_t * info)
{
    gx_image3_enum_t * const penum = (gx_image3_enum_t *) info;
    int code = gx_image_flush(penum->mask_info);

    if (code >= 0)
        code = gx_image_flush(penum->pixel_info);
    return code;
}

/* Determine which data planes are wanted. */
static bool
gx_image3_planes_wanted(const gx_image_enum_common_t * info, byte *wanted)
{
    const gx_image3_enum_t * const penum = (const gx_image3_enum_t *) info;

    switch (penum->InterleaveType) {
    case interleave_chunky:	/* only 1 plane */
        wanted[0] = 0xff;
        return true;
    case interleave_scan_lines:	/* only 1 plane, but varying width/depth */
        wanted[0] = 0xff;
        return false;
    case interleave_separate_source: {
        /*
         * We always want at least as much of the mask to be filled as the
         * pixel data.  next > 0 iff we've processed more data than mask.
         * Plane 0 is the mask, planes [1 .. num_planes - 1] are pixel data.
         */
        int next = planes_next(penum);

        wanted[0] = (next >= 0 ? 0xff : 0);
        memset(wanted + 1, (next <= 0 ? 0xff : 0), info->num_planes - 1);
        /*
         * In principle, wanted will always be true for both mask and pixel
         * data if the full_heights are equal.  Unfortunately, even in this
         * case, processing may be interrupted after a mask row has been
         * passed to the underlying image processor but before the data row
         * has been passed, in which case pixel data will be 'wanted', but
         * not mask data, for the next call.  Therefore, we must return
         * false.
         */
        return false
            /*(next == 0 &&
              penum->mask_full_height == penum->pixel_full_height)*/;
    }
    default:			/* can't happen */
        memset(wanted, 0, info->num_planes);
        return false;
    }
}

/* Clean up after processing an ImageType 3 image. */
static int
gx_image3_end_image(gx_image_enum_common_t * info, bool draw_last)
{
    gx_image3_enum_t *penum = (gx_image3_enum_t *) info;
    gs_memory_t *mem = penum->memory;
    gx_device *mdev = penum->mdev;
    int mcode = gx_image_end(penum->mask_info, draw_last);
    gx_device *pcdev = penum->pcdev;
    int pcode = gx_image_end(penum->pixel_info, draw_last);
    int code1 = gs_closedevice(pcdev);
    int code2 = gs_closedevice(mdev);

    gs_free_object(mem, penum->mask_data,
                   "gx_image3_end_image(mask_data)");
    gs_free_object(mem, penum->pixel_data,
                   "gx_image3_end_image(pixel_data)");
    gs_free_object(mem, pcdev, "gx_image3_end_image(pcdev)");
    gs_free_object(mem, mdev, "gx_image3_end_image(mdev)");
    gx_image_free_enum(&info);
    return (pcode < 0 ? pcode : mcode < 0 ? mcode : code1 < 0 ? code1 : code2);
}