summaryrefslogtreecommitdiff
path: root/gs/base/gdevpdfb.c
blob: 2220d4c697d175fd5a4dbe5e2d9612deb291f61f (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
/* Copyright (C) 2001-2006 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 that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id$ */
/* Low-level bitmap image handling for PDF-writing driver */
#include "string_.h"
#include "gx.h"
#include "gserrors.h"
#include "gdevpdfx.h"
#include "gdevpdfg.h"
#include "gdevpdfo.h"                /* for data stream */
#include "gxcspace.h"
#include "gxdcolor.h"
#include "gxpcolor.h"
#include "gxhldevc.h"
#include "gxchar.h"
#include "gdevpdtf.h"                /* Required to include gdevpdti.h */
#include "gdevpdti.h"                /* For pdf_charproc_x_offset */
#include "gsptype1.h"

/* We need this color space type for constructing temporary color spaces. */
extern const gs_color_space_type gs_color_space_type_Indexed;

/* ---------------- Utilities ---------------- */

/* Fill in the image parameters for a bitmap image. */
static void
pdf_make_bitmap_image(gs_image_t * pim, int x, int y, int w, int h)
{
    pim->Width = w;
    pim->Height = h;
    pdf_make_bitmap_matrix(&pim->ImageMatrix, x, y, w, h, h);
}

/* ---------------- Driver procedures ---------------- */

/* Copy a mask bitmap.  for_pattern = -1 means put the image in-line, */
/* 1 means put the image in a resource. */
static int
pdf_copy_mask_data(gx_device_pdf * pdev, const byte * base, int sourcex,
                   int raster, gx_bitmap_id id, int x, int y, int w, int h,
                   gs_image_t *pim, pdf_image_writer *piw,
                   int for_pattern)
{
    ulong nbytes;
    int code;
    const byte *row_base;
    int row_step;
    bool in_line;

    gs_image_t_init_mask(pim, true);
    pdf_make_bitmap_image(pim, x, y, w, h);
    nbytes = ((ulong)w * h + 7) / 8;

    if (for_pattern) {
        /*
         * Patterns must be emitted in order of increasing user Y, i.e.,
         * the opposite of PDF's standard image order.
         */
        row_base = base + (h - 1) * raster;
        row_step = -raster;
        in_line = for_pattern < 0;
    } else {
        row_base = base;
        row_step = raster;
        in_line = nbytes < pdev->MaxInlineImageSize;
        pdf_put_image_matrix(pdev, &pim->ImageMatrix, 1.0);
        /*
         * Check whether we've already made an XObject resource for this
         * image.
         */
        if (id != gx_no_bitmap_id) {
            piw->pres = pdf_find_resource_by_gs_id(pdev, resourceXObject, id);
            if (piw->pres)
                return 0;
        }
    }
    /*
     * We have to be able to control whether to put Pattern images in line,
     * to avoid trying to create an XObject resource while we're in the
     * middle of writing a Pattern resource.
     */
    if (for_pattern < 0)
        stream_puts(pdev->strm, "q ");
    pdf_image_writer_init(piw);
    pdev->ParamCompatibilityLevel = pdev->CompatibilityLevel;
    if ((code = pdf_begin_write_image(pdev, piw, id, w, h, NULL, in_line)) < 0 ||
        (code = psdf_setup_lossless_filters((gx_device_psdf *) pdev,
                                            &piw->binary[0],
                                            (gs_pixel_image_t *)pim, in_line)) < 0 ||
        (code = pdf_begin_image_data(pdev, piw, (const gs_pixel_image_t *)pim,
                                     NULL, 0)) < 0
        )
        return code;
    pdf_copy_mask_bits(piw->binary[0].strm, row_base, sourcex, row_step, w, h, 0);
    pdf_end_image_binary(pdev, piw, piw->height);
    return pdf_end_write_image(pdev, piw);
}

static void
set_image_color(gx_device_pdf *pdev, gx_color_index c)
{
    pdf_set_pure_color(pdev, c, &pdev->saved_fill_color,
                        &pdev->fill_used_process_color,
                        &psdf_set_fill_color_commands);
    if (!pdev->HaveStrokeColor)
        pdf_set_pure_color(pdev, c, &pdev->saved_stroke_color,
                            &pdev->stroke_used_process_color,
                            &psdf_set_stroke_color_commands);
}

static int
pdf_copy_mono(gx_device_pdf *pdev,
              const byte *base, int sourcex, int raster, gx_bitmap_id id,
              int x, int y, int w, int h, gx_color_index zero,
              gx_color_index one, const gx_clip_path *pcpath)
{
    int code;
    gs_color_space *pcs = NULL;
    cos_value_t cs_value;
    cos_value_t *pcsvalue;
    byte palette[arch_sizeof_color_index * 2];
    gs_image_t image;
    pdf_image_writer writer;
    pdf_stream_position_t ipos;
    pdf_resource_t *pres = 0;
    byte invert = 0;
    bool in_line = false;
    gs_show_enum *show_enum = (gs_show_enum *)pdev->pte;
    int x_offset, y_offset;
    double width;

    if(id != gx_no_bitmap_id && sourcex == 0 && show_enum &&
        pdev->type3_accum_status == 1)
    {
        /* We should only get here if we had started accumulating a type 3 CharProc
         * but ended up rendering an existing cached glyph. This can happen if
         * we glyphshow a type 3 glyph, and the glyph does not exist so we use
         * the /.notdef *and* we've already cached the /.notdef. In this case
         * we want to abort the charproc accumulation.
         * Ideally we would note that in the text enumerator, but that's a
         * pdfwrite text enumerator, and we can't access that here. So in a
         * rather kludgy manner we pas the status to and fro via the device.
         * QL CET 136-01.ps exhibits this.
         */
        pdf_resource_t *pres = pdev->accumulating_substream_resource;

        code = pdf_exit_substream(pdev);
        if (code < 0)
            return code;
        code = pdf_cancel_resource(pdev, pres, resourceCharProc);
        if (code < 0)
            return code;
        pdf_forget_resource(pdev, pres, resourceCharProc);
        pdev->type3_accum_status = 2;
    }
    /* Update clipping. */
    if (pdf_must_put_clip_path(pdev, pcpath)) {
        code = pdf_open_page(pdev, PDF_IN_STREAM);
        if (code < 0)
            return code;
        code = pdf_put_clip_path(pdev, pcpath);
        if (code < 0)
            return code;
    }
    /* We have 3 cases: mask, inverse mask, and solid. */
    if (zero == gx_no_color_index) {
        if (one == gx_no_color_index)
            return 0;
        /* If a mask has an id, assume it's a character. */
        if (id != gx_no_bitmap_id && sourcex == 0 && show_enum) {
            pdf_char_proc_t *pcp;

            if (show_enum->use_wxy_float)
                pdev->char_width.x = show_enum->wxy_float.x;
            else
                pdev->char_width.x = fixed2float(show_enum->wxy.x);
            pres = pdf_find_resource_by_gs_id(pdev, resourceCharProc, id);
            if (pres == 0) {        /* Define the character in an embedded font. */
                gs_image_t_init_mask(&image, false);
                invert = 0xff;
                x_offset = x - (int)show_enum->pis->current_point.x;
                y_offset = y - (int)show_enum->pis->current_point.y;
                x -= x_offset;
                y -= y_offset;
                y -= h;
                pdf_make_bitmap_image(&image, x, y, w, h);
                /*
                 * The Y axis of the text matrix is inverted,
                 * so we need to negate the Y offset appropriately.
                 */
                code = pdf_begin_char_proc(pdev, w, h, 0, y_offset, x_offset, id,
                                           &pcp, &ipos);
                if (code < 0)
                    return code;
                y_offset = -y_offset;
                width = psdf_round(pdev->char_width.x, 100, 10); /* See
                        pdf_write_Widths about rounding. We need to provide
                        a compatible data for Tj. */
                pprintg1(pdev->strm, "%g ", width);
                pprintd4(pdev->strm, "0 %d %d %d %d d1\n",  x_offset, -h + y_offset, w + x_offset, y_offset);
                pprintd4(pdev->strm, "%d 0 0 %d %d %d cm\n", w, h, x_offset,
                         -h + y_offset);
                pdf_image_writer_init(&writer);
                code = pdf_begin_write_image(pdev, &writer, gs_no_id, w, h, NULL, true);
                if (code < 0)
                    return code;
                pres = (pdf_resource_t *) pcp;
                goto wr;
            } else if (pdev->pte) {
                /* We're under pdf_text_process. It set a high level color. */
            } else
                set_image_color(pdev, one);
            pcp = (pdf_char_proc_t *) pres;
            x -= pdf_charproc_x_offset(pcp);
            y -= pdf_charproc_y_offset(pcp);
            y -= h;
            pdf_make_bitmap_image(&image, x, y, w, h);
            goto rx;
        }
        set_image_color(pdev, one);
        gs_image_t_init_mask(&image, false);
        invert = 0xff;
    } else if (one == gx_no_color_index) {
        gs_image_t_init_mask(&image, false);
        set_image_color(pdev, zero);
    } else if (zero == pdev->black && one == pdev->white) {
        pcs = gs_cspace_new_DeviceGray(pdev->memory);
        gs_image_t_init(&image, pcs);
    } else if (zero == pdev->white && one == pdev->black) {
        pcs = gs_cspace_new_DeviceGray(pdev->memory);
        gs_image_t_init(&image, pcs);
        invert = 0xff;
    } else {
        /*
         * We think this code is never executed when interpreting PostScript
         * or PDF: the library never uses monobit non-mask images
         * internally, and high-level images don't go through this code.
         * However, we still want the code to work.
         */
        gs_color_space *pcs_base;
        gx_color_index c[2];
        int i, j;
        int ncomp = pdev->color_info.num_components;
        byte *p;

        code = pdf_cspace_init_Device(pdev->memory, &pcs_base, ncomp);
        if (code < 0)
            return code;
        c[0] = psdf_adjust_color_index((gx_device_vector *)pdev, zero);
        c[1] = psdf_adjust_color_index((gx_device_vector *)pdev, one);
        pcs = gs_cspace_alloc(pdev->memory, &gs_color_space_type_Indexed);
        if (pcs == NULL) {
            rc_decrement_cs(pcs_base, "pdf_copy_mono");
            return_error(gs_error_VMerror);
        }
        pcs->base_space = pcs_base;
        pcs->params.indexed.hival = 1;
        pcs->params.indexed.n_comps = ncomp;
        p = palette;
        for (i = 0; i < 2; ++i)
            for (j = ncomp - 1; j >= 0; --j)
                *p++ = (byte)(c[i] >> (j * 8));
        pcs->params.indexed.lookup.table.data = palette;
        pcs->params.indexed.lookup.table.size = p - palette;
        pcs->params.indexed.use_proc = false;
        gs_image_t_init(&image, pcs);
        image.BitsPerComponent = 1;
    }
    pdf_make_bitmap_image(&image, x, y, w, h);
    {
        ulong nbytes = (ulong) ((w + 7) >> 3) * h;

        code = pdf_open_page(pdev, PDF_IN_STREAM);
        if (code < 0)
            return code;
        in_line = nbytes < pdev->MaxInlineImageSize;
        if (in_line)
            pdf_put_image_matrix(pdev, &image.ImageMatrix, 1.0);
        pdf_image_writer_init(&writer);
        code = pdf_begin_write_image(pdev, &writer, gs_no_id, w, h, NULL, in_line);
        if (code < 0)
            return code;
    }
  wr:
    if (image.ImageMask)
        pcsvalue = NULL;
    else {
        /*
         * We don't have to worry about color space scaling: the color
         * space is always a Device space.
         */
        code = pdf_color_space_named(pdev, &cs_value, NULL, pcs,
                               &writer.pin->color_spaces, in_line, NULL, 0);
        if (code < 0)
            return code;
        pcsvalue = &cs_value;
    }
    /*
     * There are 3 different cases at this point:
     *      - Writing an in-line image (pres == 0, writer.pres == 0);
     *      - Writing an XObject image (pres == 0, writer.pres != 0);
     *      - Writing the image for a CharProc (pres != 0).
     * We handle them with in-line code followed by a switch,
     * rather than making the shared code into a procedure,
     * simply because there would be an awful lot of parameters
     * that would need to be passed.
     */
    if (pres) {
        /*
         * Always use CCITTFax 2-D for character bitmaps.  It takes less
         * space to invert the data with Decode than to set BlackIs1.
         */
        float d0 = image.Decode[0];

        image.Decode[0] = image.Decode[1];
        image.Decode[1] = d0;
        psdf_CFE_binary(&writer.binary[0], image.Width, image.Height, true);
        invert ^= 0xff;
    } else {
        /* Use the Distiller compression parameters. */
        pdev->ParamCompatibilityLevel = pdev->CompatibilityLevel;
        psdf_setup_image_filters((gx_device_psdf *) pdev, &writer.binary[0],
                                 (gs_pixel_image_t *)&image, NULL, NULL, true, in_line);
    }
    pdf_begin_image_data(pdev, &writer, (const gs_pixel_image_t *)&image,
                         pcsvalue, 0);
    code = pdf_copy_mask_bits(writer.binary[0].strm, base, sourcex, raster,
                              w, h, invert);
    if (code < 0)
        return code;
    pdf_end_image_binary(pdev, &writer, writer.height);
    if (!pres) {
        switch ((code = pdf_end_write_image(pdev, &writer))) {
            default:                /* error */
                return code;
            case 1:
                return 0;
            case 0:
                return pdf_do_image(pdev, writer.pres, &image.ImageMatrix,
                                    true);
        }
    }
    writer.end_string = "";        /* no Q */
    switch ((code = pdf_end_write_image(pdev, &writer))) {
    default:                /* error */
        return code;
    case 0:                        /* not possible */
        return_error(gs_error_Fatal);
    case 1:
        break;
    }
    code = pdf_end_char_proc(pdev, &ipos);
    if (code < 0)
        return code;
  rx:{
        gs_matrix imat;

        imat = image.ImageMatrix;
        imat.xx /= w;
        imat.xy /= h;
        imat.yx /= w;
        imat.yy /= h;
        return pdf_do_char_image(pdev, (const pdf_char_proc_t *)pres, &imat);
    }
}
int
gdev_pdf_copy_mono(gx_device * dev,
                   const byte * base, int sourcex, int raster, gx_bitmap_id id,
                   int x, int y, int w, int h, gx_color_index zero,
                   gx_color_index one)
{
    gx_device_pdf *pdev = (gx_device_pdf *) dev;

    if (w <= 0 || h <= 0)
        return 0;
    return pdf_copy_mono(pdev, base, sourcex, raster, id, x, y, w, h,
                         zero, one, NULL);
}

/* Copy a color bitmap.  for_pattern = -1 means put the image in-line, */
/* 1 means put the image in a resource, 2 means image is a rasterized shading. */
int
pdf_copy_color_data(gx_device_pdf * pdev, const byte * base, int sourcex,
                    int raster, gx_bitmap_id id, int x, int y, int w, int h,
                    gs_image_t *pim, pdf_image_writer *piw,
                    int for_pattern)
{
    int depth = pdev->color_info.depth;
    int bytes_per_pixel = depth >> 3;
    gs_color_space *pcs;
    cos_value_t cs_value;
    ulong nbytes;
    int code = pdf_cspace_init_Device(pdev->memory, &pcs, bytes_per_pixel);
    const byte *row_base;
    int row_step;
    bool in_line;

    if (code < 0)
        return code;                /* can't happen */
    gs_image_t_init(pim, pcs);
    pdf_make_bitmap_image(pim, x, y, w, h);
    pim->BitsPerComponent = 8;
    nbytes = (ulong)w * bytes_per_pixel * h;

    if (for_pattern == 1) {
        /*
         * Patterns must be emitted in order of increasing user Y, i.e.,
         * the opposite of PDF's standard image order.
         */
        row_base = base + (h - 1) * raster;
        row_step = -raster;
        in_line = for_pattern < 0;
    } else {
        row_base = base;
        row_step = raster;
        in_line = nbytes < pdev->MaxInlineImageSize;
        pdf_put_image_matrix(pdev, &pim->ImageMatrix, 1.0);
        /*
         * Check whether we've already made an XObject resource for this
         * image.
         */
        if (id != gx_no_bitmap_id) {
            piw->pres = pdf_find_resource_by_gs_id(pdev, resourceXObject, id);
            if (piw->pres)
                return 0;
        }
    }
    /*
     * We have to be able to control whether to put Pattern images in line,
     * to avoid trying to create an XObject resource while we're in the
     * middle of writing a Pattern resource.
     */
    if (for_pattern < 0)
        stream_puts(pdev->strm, "q ");
    /*
     * We don't have to worry about color space scaling: the color
     * space is always a Device space.
     */
    pdf_image_writer_init(piw);
    pdev->ParamCompatibilityLevel = pdev->CompatibilityLevel;
    if ((code = pdf_begin_write_image(pdev, piw, id, w, h, NULL, in_line)) < 0 ||
        (code = pdf_color_space_named(pdev, &cs_value, NULL, pcs,
                                &piw->pin->color_spaces, in_line, NULL, 0)) < 0 ||
        (for_pattern < 2 || nbytes < 512000 ?
            (code = psdf_setup_lossless_filters((gx_device_psdf *) pdev,
                        &piw->binary[0], (gs_pixel_image_t *)pim, false)) :
            (code = psdf_setup_image_filters((gx_device_psdf *) pdev,
                        &piw->binary[0], (gs_pixel_image_t *)pim, NULL, NULL, false, false))
        ) < 0 ||
        (code = pdf_begin_image_data(pdev, piw, (const gs_pixel_image_t *)pim,
                                     &cs_value, 0)) < 0
        )
        return code;
    pdf_copy_color_bits(piw->binary[0].strm, row_base, sourcex, row_step, w, h,
                        bytes_per_pixel);
    pdf_end_image_binary(pdev, piw, piw->height);
    return pdf_end_write_image(pdev, piw);
}

int
gdev_pdf_copy_color(gx_device * dev, const byte * base, int sourcex,
                    int raster, gx_bitmap_id id, int x, int y, int w, int h)
{
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
    gs_image_t image;
    pdf_image_writer writer;
    int code;

    if (w <= 0 || h <= 0)
        return 0;
    code = pdf_open_page(pdev, PDF_IN_STREAM);
    if (code < 0)
        return code;
    /* Make sure we aren't being clipped. */
    code = pdf_put_clip_path(pdev, NULL);
    if (code < 0)
        return code;
    code = pdf_copy_color_data(pdev, base, sourcex, raster, id, x, y, w, h,
                               &image, &writer, 0);
    switch (code) {
        default:
            return code;        /* error */
        case 1:
            return 0;
        case 0:
            return pdf_do_image(pdev, writer.pres, NULL, true);
    }
}

/* Fill a mask. */
int
gdev_pdf_fill_mask(gx_device * dev,
                 const byte * data, int data_x, int raster, gx_bitmap_id id,
                   int x, int y, int width, int height,
                   const gx_drawing_color * pdcolor, int depth,
                   gs_logical_operation_t lop, const gx_clip_path * pcpath)
{
    gx_device_pdf *pdev = (gx_device_pdf *) dev;

    if (width <= 0 || height <= 0)
        return 0;
    if (depth > 1 || (!gx_dc_is_pure(pdcolor) != 0 && !(gx_dc_is_pattern1_color(pdcolor))))
        return gx_default_fill_mask(dev, data, data_x, raster, id,
                                    x, y, width, height, pdcolor, depth, lop,
                                    pcpath);
    return pdf_copy_mono(pdev, data, data_x, raster, id, x, y, width, height,
                         gx_no_color_index, gx_dc_pure_color(pdcolor),
                         pcpath);
}

/* Tile with a bitmap.  This is important for pattern fills. */
int
gdev_pdf_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
                              int x, int y, int w, int h,
                              gx_color_index color0, gx_color_index color1,
                              int px, int py)
{
    gx_device_pdf *const pdev = (gx_device_pdf *) dev;
    int tw = tiles->rep_width, th = tiles->rep_height;
    double xscale = pdev->HWResolution[0] / 72.0,
        yscale = pdev->HWResolution[1] / 72.0;
    bool mask;
    int depth;
    int (*copy_data)(gx_device_pdf *, const byte *, int, int,
                     gx_bitmap_id, int, int, int, int,
                     gs_image_t *, pdf_image_writer *, int);
    pdf_resource_t *pres;
    cos_value_t cs_value;
    int code;

    if (tiles->id == gx_no_bitmap_id || tiles->shift != 0 ||
        (w < tw && h < th) ||
        color0 != gx_no_color_index
        )
        goto use_default;
    if (color1 != gx_no_color_index) {
        /* This is a mask pattern. */
        mask = true;
        depth = 1;
        copy_data = pdf_copy_mask_data;
        code = pdf_cs_Pattern_uncolored(pdev, &cs_value);
    } else {
        /* This is a colored pattern. */
        mask = false;
        depth = pdev->color_info.depth;
        copy_data = pdf_copy_color_data;
        code = pdf_cs_Pattern_colored(pdev, &cs_value);
    }
    if (code < 0)
        goto use_default;
    pres = pdf_find_resource_by_gs_id(pdev, resourcePattern, tiles->id);
    if (!pres) {
        /* Create the Pattern resource. */
        int code;
        long image_id, length_id, start, end;
        stream *s;
        gs_image_t image;
        pdf_image_writer writer;
        long image_bytes = ((long)tw * depth + 7) / 8 * th;
        bool in_line = image_bytes < pdev->MaxInlineImageSize;
        ulong tile_id =
            (tw == tiles->size.x && th == tiles->size.y ? tiles->id :
             gx_no_bitmap_id);

        if (in_line)
            image_id = 0;
        else if (image_bytes > 65500) {
            /*
             * Acrobat Reader can't handle image Patterns with more than
             * 64K of data.  :-(
             */
            goto use_default;
        } else {
            /* Write the image as an XObject resource now. */
            code = copy_data(pdev, tiles->data, 0, tiles->raster,
                             tile_id, 0, 0, tw, th, &image, &writer, 1);
            if (code < 0)
                goto use_default;
            image_id = pdf_resource_id(writer.pres);
        }
        code = pdf_begin_resource(pdev, resourcePattern, tiles->id, &pres);
        if (code < 0)
            goto use_default;
        s = pdev->strm;
        pprintd1(s, "/PatternType 1/PaintType %d/TilingType 1/Resources<<\n",
                 (mask ? 2 : 1));
        if (image_id)
            pprintld2(s, "/XObject<</R%ld %ld 0 R>>", image_id, image_id);
        pprints1(s, "/ProcSet[/PDF/Image%s]>>\n", (mask ? "B" : "C"));
        /*
         * Because of bugs in Acrobat Reader's Print function, we can't use
         * the natural BBox and Step here: they have to be 1.
         */
        pprintg2(s, "/Matrix[%g 0 0 %g 0 0]", tw / xscale, th / yscale);
        stream_puts(s, "/BBox[0 0 1 1]/XStep 1/YStep 1/Length ");
        if (image_id) {
            char buf[MAX_REF_CHARS + 6 + 1]; /* +6 for /R# Do\n */

            sprintf(buf, "/R%ld Do\n", image_id);
            pprintd1(s, "%d>>stream\n", strlen(buf));
            if (pdev->PDFA)
                pprints1(s, "%s\nendstream\n", buf);
            else
                pprints1(s, "%sendstream\n", buf);
            pdf_end_resource(pdev, resourcePattern);
        } else {
            length_id = pdf_obj_ref(pdev);
            pprintld1(s, "%ld 0 R>>stream\n", length_id);
            start = pdf_stell(pdev);
            code = copy_data(pdev, tiles->data, 0, tiles->raster,
                             tile_id, 0, 0, tw, th, &image, &writer, -1);
            switch (code) {
            default:
                return code;        /* error */
            case 1:
                break;
            case 0:                        /* not possible */
                return_error(gs_error_Fatal);
            }
            end = pdf_stell(pdev);
            stream_puts(s, "\nendstream\n");
            pdf_end_resource(pdev, resourcePattern);
            pdf_open_separate(pdev, length_id, resourceNone);
            pprintld1(pdev->strm, "%ld\n", end - start);
            pdf_end_separate(pdev, resourceNone);
        }
        pres->object->written = true; /* don't write at end of page */
    }
    /* Fill the rectangle with the Pattern. */
    {
        int code = pdf_open_page(pdev, PDF_IN_STREAM);
        stream *s;

        if (code < 0)
            goto use_default;
        /* Make sure we aren't being clipped. */
        code = pdf_put_clip_path(pdev, NULL);
        if (code < 0)
            return code;
        s = pdev->strm;
        /*
         * Because of bugs in Acrobat Reader's Print function, we can't
         * leave the CTM alone here: we have to reset it to the default.
         */
        pprintg2(s, "q %g 0 0 %g 0 0 cm\n", xscale, yscale);
        cos_value_write(&cs_value, pdev);
        stream_puts(s, " cs");
        if (mask)
            pprintg3(s, " %g %g %g", (int)(color1 >> 16) / 255.0,
                     (int)((color1 >> 8) & 0xff) / 255.0,
                     (int)(color1 & 0xff) / 255.0);
        pprintld1(s, "/R%ld scn", pdf_resource_id(pres));
        pprintg4(s, " %g %g %g %g re f Q\n",
                 x / xscale, y / yscale, w / xscale, h / xscale);
    }
    return 0;
use_default:
    return gx_default_strip_tile_rectangle(dev, tiles, x, y, w, h,
                                           color0, color1, px, py);
}