Coverage Report

Created: 2025-04-22 06:20

/src/libspectre/ghostscript/devices/vector/gdevpdfj.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2020 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.,  1305 Grant Avenue - Suite 200, Novato,
13
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14
*/
15
16
17
/* Image-writing utilities for pdfwrite driver */
18
#include "memory_.h"
19
#include "gx.h"
20
#include "gserrors.h"
21
#include "gdevpdfx.h"
22
#include "gdevpdfg.h"
23
#include "gdevpdfo.h"
24
#include "gxcspace.h"
25
#include "gsiparm4.h"
26
#include "gdevpsds.h"
27
#include "spngpx.h"
28
#include <stdlib.h> /* for atoi */
29
30
#define CHECK(expr)\
31
0
  BEGIN if ((code = (expr)) < 0) return code; END
32
33
/* GC descriptors */
34
public_st_pdf_image_writer();
35
0
static ENUM_PTRS_WITH(pdf_image_writer_enum_ptrs, pdf_image_writer *piw)
36
0
     index -= 4;
37
0
     if (index < psdf_binary_writer_max_ptrs * piw->alt_writer_count) {
38
0
         gs_ptr_type_t ret =
39
0
             ENUM_USING(st_psdf_binary_writer, &piw->binary[index / psdf_binary_writer_max_ptrs],
40
0
                        sizeof(psdf_binary_writer), index % psdf_binary_writer_max_ptrs);
41
42
0
         if (ret == 0)   /* don't stop early */
43
0
             ENUM_RETURN(0);
44
0
         return ret;
45
0
    }
46
0
    return 0;
47
0
case 0: ENUM_RETURN(piw->pres);
48
0
case 1: ENUM_RETURN(piw->data);
49
0
case 2: ENUM_RETURN(piw->named);
50
0
case 3: ENUM_RETURN(piw->pres_mask);
51
0
ENUM_PTRS_END
52
0
static RELOC_PTRS_WITH(pdf_image_writer_reloc_ptrs, pdf_image_writer *piw)
53
0
{
54
0
    int i;
55
56
0
    for (i = 0; i < piw->alt_writer_count; ++i)
57
0
        RELOC_USING(st_psdf_binary_writer, &piw->binary[i],
58
0
                    sizeof(psdf_binary_writer));
59
0
    RELOC_VAR(piw->pres);
60
0
    RELOC_VAR(piw->data);
61
0
    RELOC_VAR(piw->named);
62
0
    RELOC_VAR(piw->pres_mask);
63
0
}
64
0
RELOC_PTRS_END
65
66
/* ---------------- Image stream dictionaries ---------------- */
67
68
const pdf_image_names_t pdf_image_names_full = {
69
    { PDF_COLOR_SPACE_NAMES },
70
    { PDF_FILTER_NAMES },
71
    PDF_IMAGE_PARAM_NAMES
72
};
73
const pdf_image_names_t pdf_image_names_short = {
74
    { PDF_COLOR_SPACE_NAMES_SHORT },
75
    { PDF_FILTER_NAMES_SHORT },
76
    PDF_IMAGE_PARAM_NAMES_SHORT
77
};
78
79
/* Store the values of image parameters other than filters. */
80
/* pdev is used only for updating procsets. */
81
/* pcsvalue is not used for masks. */
82
static int
83
pdf_put_pixel_image_values(cos_dict_t *pcd, gx_device_pdf *pdev,
84
                           const gs_pixel_image_t *pim,
85
                           const gs_color_space *pcs,
86
                           const pdf_image_names_t *pin,
87
                           const cos_value_t *pcsvalue)
88
0
{
89
0
    int num_components;
90
0
    float indexed_decode[2];
91
0
    const float *default_decode = NULL;
92
0
    int code;
93
94
0
    if (pcs) {
95
0
        CHECK(cos_dict_put_c_key(pcd, pin->ColorSpace, pcsvalue));
96
0
        pdf_color_space_procsets(pdev, pcs);
97
0
        num_components = gs_color_space_num_components(pcs);
98
0
        if (gs_color_space_get_index(pcs) == gs_color_space_index_Indexed) {
99
0
            indexed_decode[0] = 0;
100
0
            indexed_decode[1] = (float)((1 << pim->BitsPerComponent) - 1);
101
0
            default_decode = indexed_decode;
102
0
        }
103
0
    } else
104
0
        num_components = 1;
105
0
    CHECK(cos_dict_put_c_key_int(pcd, pin->Width, pim->Width));
106
0
    CHECK(cos_dict_put_c_key_int(pcd, pin->Height, pim->Height));
107
0
    CHECK(cos_dict_put_c_key_int(pcd, pin->BitsPerComponent,
108
0
                                 pim->BitsPerComponent));
109
0
    {
110
0
        int i;
111
112
0
        for (i = 0; i < num_components * 2; ++i) {
113
0
            if (pim->Decode[i] !=
114
0
                (default_decode ? default_decode[i] : i & 1)
115
0
                )
116
0
                break;
117
0
        }
118
0
        if (i < num_components * 2) {
119
0
            cos_array_t *pca =
120
0
                cos_array_alloc(pdev, "pdf_put_pixel_image_values(decode)");
121
122
0
            if (pca == 0)
123
0
                return_error(gs_error_VMerror);
124
0
            if (pcs == NULL) {
125
                /* 269-01.ps sets /Decode[0 100] with a mask image. */
126
0
                for (i = 0; i < num_components * 2; ++i)
127
0
                    CHECK(cos_array_add_real(pca, min(pim->Decode[i], 1)));
128
0
            } else {
129
0
                for (i = 0; i < num_components * 2; ++i)
130
0
                    CHECK(cos_array_add_real(pca, pim->Decode[i]));
131
0
            }
132
0
            CHECK(cos_dict_put_c_key_object(pcd, pin->Decode,
133
0
                                            COS_OBJECT(pca)));
134
0
        }
135
0
    }
136
0
    if (pim->Interpolate) {
137
0
        if (pdev->PDFA != 0)
138
0
            emprintf(pdev->memory,
139
0
                     "PDFA doesn't allow images with Interpolate true.\n");
140
0
        else
141
0
            CHECK(cos_dict_put_c_strings(pcd, pin->Interpolate, "true"));
142
0
    }
143
0
    return 0;
144
0
}
145
int
146
pdf_put_image_values(cos_dict_t *pcd, gx_device_pdf *pdev,
147
                     const gs_pixel_image_t *pic,
148
                     const pdf_image_names_t *pin,
149
                     const cos_value_t *pcsvalue)
150
0
{
151
0
    const gs_color_space *pcs = pic->ColorSpace;
152
0
    int code;
153
154
0
    switch (pic->type->index) {
155
0
    case 1: {
156
0
        const gs_image1_t *pim = (const gs_image1_t *)pic;
157
158
0
        if (pim->ImageMask) {
159
0
            CHECK(cos_dict_put_c_strings(pcd, pin->ImageMask, "true"));
160
0
            pdev->procsets |= ImageB;
161
0
            pcs = NULL;
162
0
        }
163
0
    }
164
0
        break;
165
0
    case 3: {
166
        /*
167
         * Clients must treat this as a special case: they must call
168
         * pdf_put_image_values for the MaskDict separately, and must
169
         * add the Mask entry to the main image stream (dictionary).
170
         */
171
        /*const gs_image3_t *pim = (const gs_image3_t *)pic;*/
172
173
        /* Masked images are only supported starting in PDF 1.3. */
174
0
        if (pdev->CompatibilityLevel < 1.3)
175
0
            return_error(gs_error_rangecheck);
176
0
    }
177
0
        break;
178
0
    case 4: {
179
0
        const gs_image4_t *pim = (const gs_image4_t *)pic;
180
0
        int num_components = gs_color_space_num_components(pcs);
181
0
        cos_array_t *pca;
182
0
        int i;
183
184
        /* Masked images are only supported starting in PDF 1.3. */
185
0
        if (pdev->CompatibilityLevel < 1.3)
186
0
            break; /* Will convert into an imagemask with a pattern color. */
187
0
        pca = cos_array_alloc(pdev, "pdf_put_image_values(mask)");
188
0
        if (pca == 0)
189
0
            return_error(gs_error_VMerror);
190
0
        for (i = 0; i < num_components; ++i) {
191
0
            int lo, hi;
192
193
0
            if (pim->MaskColor_is_range)
194
0
                lo = pim->MaskColor[i * 2], hi = pim->MaskColor[i * 2 + 1];
195
0
            else
196
0
                lo = hi = pim->MaskColor[i];
197
0
            CHECK(cos_array_add_int(pca, lo));
198
0
            CHECK(cos_array_add_int(pca, hi));
199
0
        }
200
0
        CHECK(cos_dict_put_c_key_object(pcd, "/Mask", COS_OBJECT(pca)));
201
0
    }
202
0
        break;
203
0
    default:
204
0
        return_error(gs_error_rangecheck);
205
0
    }
206
0
    return pdf_put_pixel_image_values(pcd, pdev, pic, pcs, pin, pcsvalue);
207
0
}
208
209
/* Store filters for an image. */
210
/* Currently this only saves parameters for CCITTFaxDecode. */
211
int
212
pdf_put_image_filters(cos_dict_t *pcd, gx_device_pdf *pdev,
213
                      const psdf_binary_writer * pbw,
214
                      const pdf_image_names_t *pin)
215
0
{
216
0
    return pdf_put_filters(pcd, pdev, pbw->strm, &pin->filter_names);
217
0
}
218
219
/* ---------------- Image writing ---------------- */
220
221
/*
222
 * Fill in the image parameters for a device space bitmap.
223
 * PDF images are always specified top-to-bottom.
224
 * data_h is the actual number of data rows, which may be less than h.
225
 */
226
void
227
pdf_make_bitmap_matrix(gs_matrix * pmat, int x, int y, int w, int h,
228
                       int h_actual)
229
0
{
230
0
    pmat->xx = (float)w;
231
0
    pmat->xy = 0;
232
0
    pmat->yx = 0;
233
0
    pmat->yy = (float)(-h_actual);
234
0
    pmat->tx = (float)x;
235
0
    pmat->ty = (float)(y + h);
236
0
}
237
238
/*
239
 * Put out the gsave and matrix for an image.  y_scale adjusts the matrix
240
 * for images that end prematurely.
241
 */
242
void
243
pdf_put_image_matrix(gx_device_pdf * pdev, const gs_matrix * pmat,
244
                     double y_scale)
245
0
{
246
0
    gs_matrix imat = {1, 0, 0, 1, 0 ,0};
247
248
0
    gs_matrix_translate(pmat, 0.0, 1.0 - y_scale, &imat);
249
0
    gs_matrix_scale(&imat, 1.0, y_scale, &imat);
250
0
    pdf_put_matrix(pdev, "q ", &imat, "cm\n");
251
0
}
252
253
/* Put out a reference to an image resource. */
254
int
255
pdf_do_image_by_id(gx_device_pdf * pdev, double scale,
256
             const gs_matrix * pimat, bool in_contents, gs_id id)
257
0
{
258
    /* fixme : in_contents is always true (there are no calls with false). */
259
0
    if (in_contents) {
260
0
        int code = pdf_open_contents(pdev, PDF_IN_STREAM);
261
262
0
        if (code < 0)
263
0
            return code;
264
0
    }
265
0
    if (pimat)
266
0
        pdf_put_image_matrix(pdev, pimat, scale);
267
0
    pprintld1(pdev->strm, "/R%ld Do\nQ\n", id);
268
0
    return 0;
269
0
}
270
int
271
pdf_do_image(gx_device_pdf * pdev, const pdf_resource_t * pres,
272
             const gs_matrix * pimat, bool in_contents)
273
0
{
274
    /* fixme : call pdf_do_image_by_id when pimam == NULL. */
275
0
    double scale = 1;
276
277
0
    if (pimat) {
278
        /* Adjust the matrix to account for short images. */
279
0
        const pdf_x_object_t *const pxo = (const pdf_x_object_t *)pres;
280
0
        scale = (double)pxo->data_height / pxo->height;
281
0
    }
282
0
    return pdf_do_image_by_id(pdev, scale, pimat, in_contents, pdf_resource_id(pres));
283
0
}
284
285
/* ------ Begin / finish ------ */
286
287
/* Initialize image writer. */
288
void
289
pdf_image_writer_init(pdf_image_writer * piw)
290
0
{
291
0
    memset(piw, 0, sizeof(*piw));
292
0
    piw->alt_writer_count = 1; /* Default. */
293
0
}
294
295
/*
296
 * Begin writing an image, creating the resource if not in-line, and setting
297
 * up the binary writer.  If pnamed != 0, it is a stream object created by a
298
 * NI pdfmark.
299
 */
300
int
301
pdf_begin_write_image(gx_device_pdf * pdev, pdf_image_writer * piw,
302
                      gx_bitmap_id id, int w, int h, cos_dict_t *named,
303
                      bool in_line)
304
0
{
305
    /* Patch pdev->strm so the right stream gets into the writer. */
306
0
    stream *save_strm = pdev->strm;
307
0
    cos_stream_t *data;
308
0
    bool mask = (piw->data != NULL);
309
0
    int alt_stream_index = (!mask ? 0 : piw->alt_writer_count);
310
0
    int code;
311
312
0
    if (in_line) {
313
0
        piw->pres = 0;
314
0
        piw->pin = &pdf_image_names_short;
315
0
        data = cos_stream_alloc(pdev, "pdf_begin_image_data");
316
0
        if (data == 0)
317
0
            return_error(gs_error_VMerror);
318
0
        piw->end_string = " Q";
319
0
        piw->named = 0;   /* must have named == 0 */
320
0
    } else {
321
0
        pdf_x_object_t *pxo;
322
0
        cos_stream_t *pcos;
323
0
        pdf_resource_t *pres;
324
325
        /*
326
         * Note that if named != 0, there are two objects with the same id
327
         * while the image is being accumulated: named, and pres->object.
328
         */
329
0
        code = pdf_alloc_resource(pdev, resourceXObject, id, &pres,
330
0
                                  (named ? named->id : -1L));
331
0
        if (code < 0)
332
0
            return code;
333
0
        *(mask ? &piw->pres_mask : &piw->pres) = pres;
334
0
        cos_become(pres->object, cos_type_stream);
335
0
        pres->rid = id;
336
0
        piw->pin = &pdf_image_names_full;
337
0
        pxo = (pdf_x_object_t *)pres;
338
0
        pcos = (cos_stream_t *)pxo->object;
339
0
        CHECK(cos_dict_put_c_strings(cos_stream_dict(pcos), "/Subtype",
340
0
                                     "/Image"));
341
0
        pxo->width = w;
342
0
        pxo->height = h;
343
        /* Initialize data_height for the benefit of copy_{mono,color}. */
344
0
        pxo->data_height = h;
345
0
        data = pcos;
346
0
        if (!mask)
347
0
            piw->named = named;
348
0
    }
349
0
    pdev->strm = pdev->streams.strm;
350
0
    pdev->strm = cos_write_stream_alloc(data, pdev, "pdf_begin_write_image");
351
0
    if (pdev->strm == 0)
352
0
        return_error(gs_error_VMerror);
353
0
    if (!mask)
354
0
        piw->data = data;
355
0
    piw->height = h;
356
0
    code = psdf_begin_binary((gx_device_psdf *) pdev, &piw->binary[alt_stream_index]);
357
0
    piw->binary[alt_stream_index].target = NULL; /* We don't need target with cos_write_stream. */
358
0
    pdev->strm = save_strm;
359
0
    return code;
360
0
}
361
362
/*
363
 *  Make alternative stream for image compression choice.
364
 */
365
int
366
pdf_make_alt_stream(gx_device_pdf * pdev, psdf_binary_writer * pbw)
367
0
{
368
0
    stream *save_strm = pdev->strm;
369
0
    cos_stream_t *pcos = cos_stream_alloc(pdev, "pdf_make_alt_stream");
370
0
    int code;
371
372
0
    if (pcos == 0)
373
0
        return_error(gs_error_VMerror);
374
0
    pcos->id = 0;
375
0
    CHECK(cos_dict_put_c_strings(cos_stream_dict(pcos), "/Subtype", "/Image"));
376
0
    pbw->strm = cos_write_stream_alloc(pcos, pdev, "pdf_make_alt_stream");
377
0
    if (pbw->strm == 0)
378
0
        return_error(gs_error_VMerror);
379
0
    pbw->dev = (gx_device_psdf *)pdev;
380
0
    pbw->memory = pdev->pdf_memory;
381
0
    pdev->strm = pbw->strm;
382
0
    code = psdf_begin_binary((gx_device_psdf *) pdev, pbw);
383
0
    pdev->strm = save_strm;
384
0
    pbw->target = NULL; /* We don't need target with cos_write_stream. */
385
0
    return code;
386
0
}
387
388
/* Begin writing the image data, setting up the dictionary and filters. */
389
int
390
pdf_begin_image_data(gx_device_pdf * pdev, pdf_image_writer * piw,
391
                     const gs_pixel_image_t * pim, const cos_value_t *pcsvalue,
392
                     int alt_writer_index)
393
0
{
394
395
0
    cos_stream_t *s;
396
0
    cos_dict_t *pcd;
397
0
    int code;
398
399
0
    s = cos_stream_from_pipeline(piw->binary[alt_writer_index].strm);
400
0
    if (s == 0L)
401
0
        return gs_note_error(gs_error_ioerror);
402
403
0
    pcd = cos_stream_dict(s);
404
0
    code = pdf_put_image_values(pcd, pdev, pim, piw->pin, pcsvalue);
405
0
    if (code >= 0)
406
0
        code = pdf_put_image_filters(pcd, pdev, &piw->binary[alt_writer_index], piw->pin);
407
0
    if (code < 0) {
408
0
        if (!piw->pres)
409
0
            COS_FREE(piw->data, "pdf_begin_image_data");
410
0
        piw->data = 0;
411
0
    }
412
0
    if (pdev->JPEG_PassThrough) {
413
0
        CHECK(cos_dict_put_c_strings(pcd, "/Filter", "/DCTDecode"));
414
0
    }
415
0
    return code;
416
0
}
417
418
/* Complete image data. */
419
int
420
pdf_complete_image_data(gx_device_pdf *pdev, pdf_image_writer *piw, int data_h,
421
                        int width, int bits_per_pixel)
422
0
{
423
0
    if (data_h != piw->height) {
424
0
        if (piw->binary[0].strm->procs.process == s_DCTE_template.process ||
425
0
            piw->binary[0].strm->procs.process == s_PNGPE_template.process ) {
426
            /*  Since DCTE and PNGPE can't safely close with incomplete data,
427
                we add stub data to complete the stream.
428
            */
429
0
            int bytes_per_line = (width * bits_per_pixel + 7) / 8;
430
0
            int lines_left = piw->height - data_h;
431
0
            byte buf[256];
432
0
            const uint lb = sizeof(buf);
433
0
            int i, l;
434
0
            uint ignore;
435
436
0
            memset(buf, 128, lb);
437
0
            for (; lines_left; lines_left--)
438
0
                for (i = 0; i < piw->alt_writer_count; i++) {
439
0
                    for (l = bytes_per_line; l > 0; l -= lb)
440
0
                        if ((sputs(piw->binary[i].strm, buf, min(l, lb),
441
0
                                            &ignore)) < 0)
442
0
                            return_error(gs_error_ioerror);
443
0
                }
444
0
        }
445
0
    }
446
0
    return 0;
447
0
}
448
449
/* Finish writing the binary image data. */
450
int
451
pdf_end_image_binary(gx_device_pdf *pdev, pdf_image_writer *piw, int data_h)
452
0
{
453
0
    int code, code1 = 0;
454
455
0
    if (piw->alt_writer_count > 2)
456
0
        code = pdf_choose_compression(piw, true);
457
0
    else
458
0
        code = psdf_end_binary(&piw->binary[0]);
459
    /* If the image ended prematurely, update the Height. */
460
0
    if (data_h != piw->height) {
461
0
        char data[256];
462
0
        int OutHeight;
463
0
        cos_value_t *value;
464
0
        value = (cos_value_t *)cos_dict_find(cos_stream_dict(piw->data),
465
0
                                      (const byte *)piw->pin->Height, strlen(piw->pin->Height));
466
0
        if (!value || value->contents.chars.size > 255)
467
0
            return(gs_error_rangecheck);
468
0
        strncpy((char *)&data, (const char *)value->contents.chars.data, value->contents.chars.size);
469
0
        data[value->contents.chars.size] = 0x00;
470
0
        OutHeight = atoi(data);
471
0
        if (OutHeight != piw->height) {
472
            /* Looks like we are downsampling, so we can't use the number
473
             * of rows of data actually received, we must divide those by
474
             * the sampling factor.
475
             */
476
0
            float factor = (float)OutHeight / piw->height;
477
0
            OutHeight = (int)(factor * data_h);
478
0
            code1 = cos_dict_put_c_key_int(cos_stream_dict(piw->data),
479
0
                                      piw->pin->Height, OutHeight);
480
0
        } else {
481
482
0
            code1 = cos_dict_put_c_key_int(cos_stream_dict(piw->data),
483
0
                                      piw->pin->Height, data_h);
484
0
        }
485
0
    }
486
0
    return code < 0 ? code : code1;
487
0
}
488
489
/* When writing out an image, we check to see if its a duplicate of an existing image, and if so we simply
490
 * use the existing image. There is one potential problem here; if we have an image with a SMask, the SMask is
491
 * itself animage, and the SMask image and the image which uses it are identical. Because we don't add the SMask
492
 * entry to the image dictionary until we have closed the image, its possible that the image can detect the alredy
493
 * stored SMask image as being identical and attempt to use the SMask image instead. This leads to an image with
494
 * an SMask entry referencing itself.
495
 * We detect this here simply by checking if the detected image resource ID is the same as any current SMask. Worst
496
 * case is we fail to detect a duplicate, which is better than detecting an incorrect duplicate.
497
 * This check function is only used in pdf_end_write_image() below as an argument to pdf_substitute_resource().
498
 */
499
static int
500
smask_image_check(gx_device_pdf * pdev, pdf_resource_t *pres0, pdf_resource_t *pres1)
501
0
{
502
0
    cos_value_t *v = NULL;
503
504
    /* image_mask_id is non-zero if we have a pending SMask image */
505
0
    if (pdev->image_mask_id != 0) {
506
0
        if (pres0->object->id == pdev->image_mask_id || pres1->object->id == pdev->image_mask_id)
507
0
            return 0;
508
0
        if (pdev->image_mask_is_SMask)
509
0
            v = (cos_value_t *)cos_dict_find_c_key((const cos_dict_t *)pres1->object, "/SMask");
510
0
        else
511
0
            v = (cos_value_t *)cos_dict_find_c_key((const cos_dict_t *)pres1->object, "/Mask");
512
0
        if (v == 0)
513
0
            return 0;
514
0
        if (v != 0) {
515
0
            const byte *p = v->contents.chars.data;
516
0
            int ix = 0;
517
518
0
            while (*p != 0x20) {
519
0
                if (p > v->contents.chars.data + v->contents.chars.size)
520
0
                    return 0;
521
0
                ix *= 10;
522
0
                ix += (*p) - 0x30;
523
0
            }
524
0
            if (ix != pdev->image_mask_id)
525
0
                return 0;
526
0
        }
527
0
    }
528
0
    return 1;
529
0
}
530
531
/*
532
 * Finish writing an image.  If in-line, write the BI/dict/ID/data/EI and
533
 * return 1; if a resource, write the resource definition and return 0.
534
 */
535
int
536
pdf_end_write_image(gx_device_pdf * pdev, pdf_image_writer * piw)
537
0
{
538
0
    pdf_resource_t *pres = piw->pres;
539
540
0
    if (pres) {     /* image resource */
541
0
        cos_object_t *const pco = pres->object;
542
0
        cos_stream_t *const pcs = (cos_stream_t *)pco;
543
0
        cos_dict_t *named = piw->named;
544
0
        int code;
545
546
0
        if (named) {
547
0
            if (pdev->ForOPDFRead) {
548
0
                code = cos_dict_put_c_key_bool(named, "/.Global", true);
549
0
                if (code < 0)
550
0
                    return code;
551
0
            }
552
            /*
553
             * This image was named by NI.  Copy any dictionary elements
554
             * from the named dictionary to the image stream, and then
555
             * associate the name with the stream.
556
             */
557
0
            code = cos_dict_move_all(cos_stream_dict(pcs), named);
558
0
            if (code < 0)
559
0
                return code;
560
0
            pres->named = true;
561
            /*
562
             * We need to make the entry in the name dictionary point to
563
             * the stream (pcs) rather than the object created by NI (named).
564
             * Unfortunately, we no longer know what dictionary to use.
565
             * Instead, overwrite the latter with the former's contents,
566
             * and change the only relevant pointer.
567
             */
568
0
            *(cos_object_t *)named = *pco;
569
0
            pres->object = COS_OBJECT(named);
570
0
        } else if (!pres->named) { /* named objects are written at the end */
571
0
            if (pdev->DetectDuplicateImages) {
572
0
                pdf_x_object_t *pxo = (pdf_x_object_t *)piw->pres;
573
0
                int height = pxo->height, width = pxo->width;
574
575
0
                code = pdf_substitute_resource(pdev, &piw->pres, resourceXObject, smask_image_check, false);
576
0
                if (code < 0)
577
0
                    return code;
578
579
                /* These values are related to the image matrix and should *not* be
580
                 * substituted if we found a duplicate image, or the matrix calculation
581
                 * will be incorrect! This only seems to matter for the PCL interpreter.
582
                 */
583
0
                pxo = (pdf_x_object_t *)piw->pres;
584
0
                pxo->height = height;
585
0
                pxo->width = width;
586
0
            } else {
587
0
                pdf_reserve_object_id(pdev, piw->pres, gs_no_id);
588
0
            }
589
            /*  Warning : If the substituted image used alternate streams,
590
                its space in the pdev->streams.strm file won't be released. */
591
0
            piw->pres->where_used |= pdev->used_mask;
592
0
        }
593
0
        code = pdf_add_resource(pdev, pdev->substream_Resources, "/XObject", piw->pres);
594
0
        if (code < 0)
595
0
            return code;
596
0
        return 0;
597
0
    } else {     /* in-line image */
598
0
        stream *s = pdev->strm;
599
0
        uint KeyLength = pdev->KeyLength;
600
601
0
        stream_puts(s, "BI\n");
602
0
        cos_stream_elements_write(piw->data, pdev);
603
0
        stream_puts(s, (pdev->binary_ok ? "ID " : "ID\n"));
604
0
        pdev->KeyLength = 0; /* Disable encryption for the inline image. */
605
0
        cos_stream_contents_write(piw->data, pdev);
606
0
        pdev->KeyLength = KeyLength;
607
0
        pprints1(s, "\nEI%s\n", piw->end_string);
608
0
        COS_FREE(piw->data, "pdf_end_write_image");
609
0
        return 1;
610
0
    }
611
0
}
612
613
/* ------ Copy data ------ */
614
615
/* Copy the data for a mask or monobit bitmap. */
616
int
617
pdf_copy_mask_bits(stream *s, const byte *base, int sourcex, int raster,
618
                   int w, int h, byte invert)
619
0
{
620
0
    int yi;
621
622
0
    for (yi = 0; yi < h; ++yi) {
623
0
        const byte *data = base + yi * raster + (sourcex >> 3);
624
0
        int sbit = sourcex & 7;
625
626
0
        if (sbit == 0) {
627
0
            int nbytes = (w + 7) >> 3;
628
0
            int i;
629
630
0
            for (i = 0; i < nbytes; ++data, ++i)
631
0
                sputc(s, (byte)(*data ^ invert));
632
0
        } else {
633
0
            int wleft = w;
634
0
            int rbit = 8 - sbit;
635
636
0
            for (; wleft + sbit > 8; ++data, wleft -= 8)
637
0
                sputc(s, (byte)(((*data << sbit) + (data[1] >> rbit)) ^ invert));
638
0
            if (wleft > 0)
639
0
                sputc(s, (byte)(((*data << sbit) ^ invert) &
640
0
                      (byte) (0xff00 >> wleft)));
641
0
        }
642
0
    }
643
0
    return 0;
644
0
}
645
646
/* Copy the data for a colored image (device pixels). */
647
int
648
pdf_copy_color_bits(stream *s, const byte *base, int sourcex, int raster,
649
                    int w, int h, int bytes_per_pixel)
650
0
{
651
0
    int yi;
652
653
0
    for (yi = 0; yi < h; ++yi) {
654
0
        uint ignore;
655
656
0
        sputs(s, base + sourcex * bytes_per_pixel + yi * raster,
657
0
              w * bytes_per_pixel, &ignore);
658
0
    }
659
0
    return 0;
660
0
}
661
662
/* Choose image compression - auxiliary procs */
663
static inline bool much_bigger__DL(long l1, long l2)
664
0
{
665
0
    return l1 > 1024*1024 && l2 < l1 / 3;
666
0
}
667
static void
668
pdf_choose_compression_cos(pdf_image_writer *piw, cos_stream_t *s[2], bool force)
669
0
{   /*  Assume s[0] is Flate, s[1] is DCT, s[2] is chooser. */
670
0
    long l0, l1;
671
0
    int k0, k1;
672
673
0
    l0 = cos_stream_length(s[0]);
674
0
    l1 = cos_stream_length(s[1]);
675
676
0
    if ((force && l0 <= l1) || l1 == -1)
677
0
        k0 = 1; /* Use Flate if it is not longer. Or if the DCT failed */
678
0
    else {
679
0
        k0 = s_compr_chooser__get_choice(
680
0
            (stream_compr_chooser_state *)piw->binary[2].strm->state, force);
681
0
        if (k0 && l0 > 0 && l1 > 0)
682
0
            k0--;
683
0
        else if (much_bigger__DL(l0, l1))
684
0
            k0 = 0;
685
0
        else if (much_bigger__DL(l1, l0) || force)
686
0
            k0 = 1;
687
0
        else
688
0
           return;
689
0
    }
690
0
    k1 = 1 - k0;
691
0
    s_close_filters(&piw->binary[k0].strm, piw->binary[k0].target);
692
0
    s[k0]->cos_procs->release((cos_object_t *)s[k0], "pdf_image_choose_filter");
693
0
    s[k0]->written = 1;
694
0
    piw->binary[0].strm = piw->binary[k1].strm;
695
0
    s_close_filters(&piw->binary[2].strm, piw->binary[2].target);
696
0
    piw->binary[1].strm = piw->binary[2].strm = 0; /* for GC */
697
0
    piw->binary[1].target = piw->binary[2].target = 0;
698
0
    s[k1]->id = piw->pres->object->id;
699
0
    piw->pres->object = (cos_object_t *)s[k1];
700
0
    piw->data = s[k1];
701
0
    if (piw->alt_writer_count > 3) {
702
0
        piw->binary[1] = piw->binary[3];
703
0
        piw->binary[3].strm = 0; /* for GC */
704
0
        piw->binary[3].target = 0;
705
0
    }
706
0
    piw->alt_writer_count -= 2;
707
0
}
708
709
/* End binary with choosing image compression. */
710
int
711
pdf_choose_compression(pdf_image_writer * piw, bool end_binary)
712
0
{
713
0
    cos_stream_t *s[2];
714
0
    int status;
715
716
0
    s[0] = cos_stream_from_pipeline(piw->binary[0].strm);
717
0
    s[1] = cos_stream_from_pipeline(piw->binary[1].strm);
718
719
0
    if (s[0] == 0L) {
720
0
        return_error(gs_error_ioerror);
721
0
    }
722
0
    if (s[1] == 0L) {
723
0
        s_close_filters(&piw->binary[0].strm, piw->binary[0].target);
724
0
        return_error(gs_error_ioerror);
725
0
    }
726
0
    if (end_binary) {
727
0
        status = s_close_filters(&piw->binary[0].strm, piw->binary[0].target);
728
0
        if (status < 0)
729
0
            return_error(gs_error_ioerror);
730
0
        status = s_close_filters(&piw->binary[1].strm, piw->binary[1].target);
731
0
        if (status < 0)
732
0
            s[1]->length = -1;
733
0
    }
734
0
    pdf_choose_compression_cos(piw, s, end_binary);
735
0
    return 0;
736
0
}