Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gximage.c
Line
Count
Source
1
/* Copyright (C) 2001-2026 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Generic image support */
18
#include "memory_.h"
19
#include "gx.h"
20
#include "gscspace.h"
21
#include "gserrors.h"
22
#include "gsmatrix.h"
23
#include "gsutil.h"
24
#include "gxcolor2.h"   /* for lookup map */
25
#include "gxiparam.h"
26
#include "stream.h"
27
#include "gxdevice.h"
28
29
/* ---------------- Generic image support ---------------- */
30
31
/* Structure descriptors */
32
public_st_gs_data_image();
33
public_st_gs_pixel_image();
34
35
/* Initialize the common parts of image structures. */
36
void
37
gs_image_common_t_init(gs_image_common_t * pic)
38
1.78M
{
39
1.78M
    gs_make_identity(&pic->ImageMatrix);
40
1.78M
    pic->imagematrices_are_untrustworthy = false;
41
1.78M
}
42
void
43
gs_data_image_t_init(gs_data_image_t * pim, int num_components)
44
1.78M
{
45
1.78M
    int i;
46
47
1.78M
    gs_image_common_t_init((gs_image_common_t *) pim);
48
1.78M
    pim->Width = pim->Height = 0;
49
1.78M
    pim->BitsPerComponent = 1;
50
1.78M
    if (num_components >= 0) {
51
3.82M
        for (i = 0; i < num_components * 2; i += 2)
52
2.03M
            pim->Decode[i] = 0, pim->Decode[i + 1] = 1;
53
1.78M
    } else {
54
906
        for (i = 0; i < num_components * -2; i += 2)
55
453
            pim->Decode[i] = 1, pim->Decode[i + 1] = 0;
56
453
    }
57
1.78M
    pim->Interpolate = false;
58
1.78M
    pim->imagematrices_are_untrustworthy = false;
59
1.78M
}
60
void
61
gs_pixel_image_t_init(gs_pixel_image_t * pim,
62
                      gs_color_space * color_space)
63
1.77M
{
64
1.77M
    int num_components;
65
66
1.77M
    if (color_space == 0 ||
67
787k
        (num_components =
68
787k
         gs_color_space_num_components(color_space)) < 0
69
1.77M
        )
70
989k
        num_components = 0;
71
1.77M
    gs_data_image_t_init((gs_data_image_t *) pim, num_components);
72
1.77M
    pim->format = gs_image_format_chunky;
73
1.77M
    pim->ColorSpace = color_space;
74
1.77M
    pim->CombineWithColor = false;
75
1.77M
    pim->override_in_smask = 0;
76
1.77M
}
77
78
/* Initialize the common part of an image-processing enumerator. */
79
int
80
gx_image_enum_common_init(gx_image_enum_common_t * piec,
81
                          const gs_data_image_t * pic,
82
                          const gx_image_enum_procs_t * piep,
83
                          gx_device * dev, int num_components,
84
                          gs_image_format_t format)
85
2.13M
{
86
2.13M
    int bpc = pic->BitsPerComponent;
87
2.13M
    int i;
88
2.13M
    uint check;
89
90
2.13M
    piec->image_type = pic->type;
91
2.13M
    piec->procs = piep;
92
2.13M
    piec->dev = dev;
93
2.13M
    piec->id = gs_next_ids(dev->memory, 1);
94
2.13M
    piec->skipping = false;
95
2.13M
    piec->pgs = NULL;
96
97
2.13M
    switch (format) {
98
2.10M
        case gs_image_format_chunky:
99
2.10M
            piec->num_planes = 1;
100
2.10M
            piec->plane_depths[0] = bpc * num_components;
101
2.10M
            break;
102
23.4k
        case gs_image_format_component_planar:
103
23.4k
            piec->num_planes = num_components;
104
117k
            for (i = 0; i < num_components; ++i)
105
94.0k
                piec->plane_depths[i] = bpc;
106
23.4k
            break;
107
0
        case gs_image_format_bit_planar:
108
0
            piec->num_planes = bpc * num_components;
109
0
            for (i = 0; i < piec->num_planes; ++i)
110
0
                piec->plane_depths[i] = 1;
111
0
            break;
112
0
        default:
113
0
            return_error(gs_error_rangecheck);
114
2.13M
    }
115
4.33M
    for (i = 0; i < piec->num_planes; ++i) {
116
2.20M
        piec->plane_widths[i] = pic->Width;
117
2.20M
        if (check_uint32_multiply(pic->Width, piec->plane_depths[i], &check) != 0)
118
7
            return_error(gs_error_limitcheck);
119
2.20M
    }
120
2.13M
    return 0;
121
2.13M
}
122
123
/* Process the next piece of an image with no source data. */
124
/* This procedure should never be called. */
125
int
126
gx_no_plane_data(gx_image_enum_common_t * info,
127
                 const gx_image_plane_t * planes, int height,
128
                 int *height_used)
129
0
{
130
0
    return_error(gs_error_Fatal);
131
0
}
132
133
/* Clean up after processing an image with no source data. */
134
/* This procedure may be called, but should do nothing. */
135
int
136
gx_ignore_end_image(gx_image_enum_common_t * info, bool draw_last)
137
0
{
138
0
    return 0;
139
0
}
140
141
/* ---------------- Client procedures ---------------- */
142
143
int
144
gx_image_data(gx_image_enum_common_t * info, const byte ** plane_data,
145
              int data_x, uint raster, int height)
146
0
{
147
0
    int num_planes = info->num_planes;
148
0
    gx_image_plane_t planes[GS_IMAGE_MAX_COMPONENTS];
149
0
    int i;
150
151
#ifdef DEBUG
152
    if (num_planes > GS_IMAGE_MAX_COMPONENTS) {
153
        lprintf2("num_planes=%d > GS_IMAGE_MAX_COMPONENTS=%d!\n",
154
                 num_planes, GS_IMAGE_MAX_COMPONENTS);
155
        return_error(gs_error_Fatal);
156
    }
157
#endif
158
0
    for (i = 0; i < num_planes; ++i) {
159
0
        planes[i].data = plane_data[i];
160
0
        planes[i].data_x = data_x;
161
0
        planes[i].raster = raster;
162
0
    }
163
0
    return gx_image_plane_data(info, planes, height);
164
0
}
165
166
int
167
gx_image_plane_data(gx_image_enum_common_t * info,
168
                    const gx_image_plane_t * planes, int height)
169
1.67M
{
170
1.67M
    int ignore_rows_used;
171
172
1.67M
    return gx_image_plane_data_rows(info, planes, height, &ignore_rows_used);
173
1.67M
}
174
175
int
176
gx_image_plane_data_rows(gx_image_enum_common_t * info,
177
                         const gx_image_plane_t * planes, int height,
178
                         int *rows_used)
179
34.0M
{
180
34.0M
    return info->procs->plane_data(info, planes, height, rows_used);
181
34.0M
}
182
183
int
184
gx_image_flush(gx_image_enum_common_t * info)
185
1.91M
{
186
1.91M
    int (*flush)(gx_image_enum_common_t *) = info->procs->flush;
187
188
1.91M
    return (flush ? flush(info) : 0);
189
1.91M
}
190
191
bool
192
gx_image_planes_wanted(const gx_image_enum_common_t *info, byte *wanted)
193
1.37M
{
194
1.37M
    bool (*planes_wanted)(const gx_image_enum_common_t *, byte *) =
195
1.37M
        info->procs->planes_wanted;
196
197
1.37M
    if (planes_wanted)
198
431k
        return planes_wanted(info, wanted);
199
941k
    else {
200
941k
        memset(wanted, 0xff, info->num_planes);
201
941k
        return true;
202
941k
    }
203
1.37M
}
204
205
int
206
gx_image_end(gx_image_enum_common_t * info, bool draw_last)
207
1.38M
{
208
1.38M
    return info->procs->end_image(info, draw_last);
209
1.38M
}
210
211
/* ---------------- Serialization ---------------- */
212
213
/*
214
 * Define dummy sput/sget/release procedures for image types that don't
215
 * implement these functions.
216
 */
217
218
int
219
gx_image_no_sput(const gs_image_common_t *pic, stream *s,
220
                 const gs_color_space **ppcs)
221
0
{
222
0
    return_error(gs_error_rangecheck);
223
0
}
224
225
int
226
gx_image_no_sget(gs_image_common_t *pic, stream *s,
227
                 gs_color_space *pcs)
228
0
{
229
0
    return_error(gs_error_rangecheck);
230
0
}
231
232
void
233
gx_image_default_release(gs_image_common_t *pic, gs_memory_t *mem)
234
0
{
235
0
    gs_free_object(mem, pic, "gx_image_default_release");
236
0
}
237
238
#ifdef DEBUG
239
static void
240
debug_b_print_matrix(const gs_pixel_image_t *pim)
241
{
242
    if_debug6('b', "      ImageMatrix=[%g %g %g %g %g %g]\n",
243
              pim->ImageMatrix.xx, pim->ImageMatrix.xy,
244
              pim->ImageMatrix.yx, pim->ImageMatrix.yy,
245
              pim->ImageMatrix.tx, pim->ImageMatrix.ty);
246
}
247
static void
248
debug_b_print_decode(const gs_pixel_image_t *pim, int num_decode)
249
{
250
    if (gs_debug_c('b')) {
251
        const char *str = "      Decode=[";
252
        int i;
253
254
        for (i = 0; i < num_decode; str = " ", ++i)
255
            dprintf2("%s%g", str, pim->Decode[i]);
256
        dputs("]\n");
257
    }
258
}
259
#else
260
1.10k
#  define debug_b_print_matrix(pim) DO_NOTHING
261
785
#  define debug_b_print_decode(pim, num_decode) DO_NOTHING
262
#endif
263
264
/* Test whether an image has a default ImageMatrix. */
265
bool
266
gx_image_matrix_is_default(const gs_data_image_t *pid)
267
312k
{
268
312k
    return (is_xxyy(&pid->ImageMatrix) &&
269
285k
            pid->ImageMatrix.xx == pid->Width &&
270
245k
            pid->ImageMatrix.yy == -pid->Height &&
271
203k
            is_fzero(pid->ImageMatrix.tx) &&
272
203k
            pid->ImageMatrix.ty == pid->Height);
273
312k
}
274
275
/* Put a variable-length uint on a stream. */
276
void
277
sput_variable_uint(stream *s, uint w)
278
936k
{
279
961k
    for (; w > 0x7f; w >>= 7)
280
24.5k
        sputc(s, (byte)(w | 0x80));
281
936k
    sputc(s, (byte)w);
282
936k
}
283
284
/*
285
 * Write generic pixel image parameters.  The format is the following,
286
 * encoded as a variable-length uint in the usual way:
287
 *  xxxFEDCCBBBBA
288
 *      A = 0 if standard ImageMatrix, 1 if explicit ImageMatrix
289
 *      BBBB = BitsPerComponent - 1
290
 *      CC = format
291
 *      D = 0 if standard (0..1) Decode, 1 if explicit Decode
292
 *      E = Interpolate
293
 *      F = CombineWithColor
294
 *      xxx = extra information from caller
295
 */
296
350k
#define PI_ImageMatrix 0x001
297
350k
#define PI_BPC_SHIFT 1
298
308k
#define PI_BPC_MASK 0xf
299
350k
#define PI_FORMAT_SHIFT 5
300
308k
#define PI_FORMAT_MASK 0x3
301
350k
#define PI_Decode 0x080
302
308k
#define PI_Interpolate 0x100
303
308k
#define PI_CombineWithColor 0x200
304
350k
#define PI_BITS 10
305
/*
306
 *  Width, encoded as a variable-length uint
307
 *  Height, encoded ditto
308
 *  ImageMatrix (if A = 1), per gs_matrix_store/fetch
309
 *  Decode (if D = 1): blocks of up to 4 components
310
 *      aabbccdd, where each xx is decoded as:
311
 *    00 = default, 01 = swapped default,
312
 *    10 = (0,V), 11 = (U,V)
313
 *      non-defaulted components (up to 8 floats)
314
 */
315
int
316
gx_pixel_image_sput(const gs_pixel_image_t *pim, stream *s,
317
                    const gs_color_space **ppcs, int extra)
318
41.5k
{
319
41.5k
    const gs_color_space *pcs = pim->ColorSpace;
320
41.5k
    int bpc = pim->BitsPerComponent;
321
41.5k
    int num_components = gs_color_space_num_components(pcs);
322
41.5k
    int num_decode;
323
41.5k
    uint control = extra << PI_BITS;
324
41.5k
    float decode_default_1 = 1;
325
41.5k
    int i;
326
41.5k
    uint ignore;
327
328
    /* Construct the control word. */
329
330
41.5k
    if (!gx_image_matrix_is_default((const gs_data_image_t *)pim))
331
421
        control |= PI_ImageMatrix;
332
41.5k
    switch (pim->format) {
333
41.4k
    case gs_image_format_chunky:
334
41.5k
    case gs_image_format_component_planar:
335
41.5k
        switch (bpc) {
336
41.5k
        case 1: case 2: case 4: case 8: case 12: case 16: break;
337
1
        default: return_error(gs_error_rangecheck);
338
41.5k
        }
339
41.5k
        break;
340
41.5k
    case gs_image_format_bit_planar:
341
0
        if (bpc < 1 || bpc > 8)
342
0
            return_error(gs_error_rangecheck);
343
41.5k
    }
344
41.5k
    control |= (bpc - 1) << PI_BPC_SHIFT;
345
41.5k
    control |= pim->format << PI_FORMAT_SHIFT;
346
41.5k
    num_decode = num_components * 2;
347
41.5k
    if (gs_color_space_get_index(pcs) == gs_color_space_index_Indexed)
348
5.29k
        decode_default_1 = (float)pcs->params.indexed.hival;
349
207k
    for (i = 0; i < num_decode; ++i)
350
166k
        if (pim->Decode[i] != DECODE_DEFAULT(i, decode_default_1)) {
351
399
            control |= PI_Decode;
352
399
            break;
353
399
        }
354
41.5k
    if (pim->Interpolate)
355
0
        control |= PI_Interpolate;
356
41.5k
    if (pim->CombineWithColor)
357
0
        control |= PI_CombineWithColor;
358
359
    /* Write the encoding on the stream. */
360
361
41.5k
    if_debug3('b', "[b]put control=0x%x, Width=%d, Height=%d\n",
362
41.5k
              control, pim->Width, pim->Height);
363
41.5k
    sput_variable_uint(s, control);
364
41.5k
    sput_variable_uint(s, (uint)pim->Width);
365
41.5k
    sput_variable_uint(s, (uint)pim->Height);
366
41.5k
    if (control & PI_ImageMatrix) {
367
420
        debug_b_print_matrix(pim);
368
420
        sput_matrix(s, &pim->ImageMatrix);
369
420
    }
370
41.5k
    if (control & PI_Decode) {
371
399
        int i;
372
399
        uint dflags = 1;
373
399
        float decode[8];
374
399
        int di = 0;
375
376
399
        debug_b_print_decode(pim, num_decode);
377
802
        for (i = 0; i < num_decode; i += 2) {
378
403
            float u = pim->Decode[i], v = pim->Decode[i + 1];
379
403
            float dv = DECODE_DEFAULT(i + 1, decode_default_1);
380
381
403
            if (dflags >= 0x100) {
382
0
                sputc(s, (byte)(dflags & 0xff));
383
0
                sputs(s, (const byte *)decode, di * sizeof(float), &ignore);
384
0
                dflags = 1;
385
0
                di = 0;
386
0
            }
387
403
            dflags <<= 2;
388
403
            if (u == 0 && v == dv)
389
403
                DO_NOTHING;
390
401
            else if (u == dv && v == 0)
391
83
                dflags += 1;
392
318
            else {
393
318
                if (u != 0) {
394
2
                    dflags++;
395
2
                    decode[di++] = u;
396
2
                }
397
318
                dflags += 2;
398
318
                decode[di++] = v;
399
318
            }
400
403
        }
401
399
        sputc(s, (byte)((dflags << (8 - num_decode)) & 0xff));
402
399
        sputs(s, (const byte *)decode, di * sizeof(float), &ignore);
403
399
    }
404
41.5k
    *ppcs = pcs;
405
41.5k
    return 0;
406
41.5k
}
407
408
/* Set an image's ImageMatrix to the default. */
409
void
410
gx_image_matrix_set_default(gs_data_image_t *pid)
411
409k
{
412
409k
    pid->ImageMatrix.xx = (float)pid->Width;
413
409k
    pid->ImageMatrix.xy = 0;
414
409k
    pid->ImageMatrix.yx = 0;
415
409k
    pid->ImageMatrix.yy = (float)-pid->Height;
416
409k
    pid->ImageMatrix.tx = 0;
417
409k
    pid->ImageMatrix.ty = (float)pid->Height;
418
409k
}
419
420
/* Get a variable-length uint from a stream. */
421
int
422
sget_variable_uint(stream *s, uint *pw)
423
1.23M
{
424
1.23M
    uint w = 0;
425
1.23M
    int shift = 0;
426
1.23M
    int ch;
427
428
1.46M
    for (; (ch = sgetc(s)) >= 0x80; shift += 7)
429
233k
        w += (ch & 0x7f) << shift;
430
1.23M
    if (ch < 0)
431
0
        return_error(gs_error_ioerror);
432
1.23M
    *pw = w + (ch << shift);
433
1.23M
    return 0;
434
1.23M
}
435
436
/*
437
 * Read generic pixel image parameters.
438
 */
439
int
440
gx_pixel_image_sget(gs_pixel_image_t *pim, stream *s,
441
                    gs_color_space *pcs)
442
308k
{
443
308k
    uint control;
444
308k
    float decode_default_1 = 1;
445
308k
    int num_components, num_decode;
446
308k
    int i;
447
308k
    int code;
448
308k
    uint ignore;
449
450
308k
    if ((code = sget_variable_uint(s, &control)) < 0 ||
451
308k
        (code = sget_variable_uint(s, (uint *)&pim->Width)) < 0 ||
452
308k
        (code = sget_variable_uint(s, (uint *)&pim->Height)) < 0
453
308k
        )
454
0
        return code;
455
308k
    if_debug3('b', "[b]get control=0x%x, Width=%d, Height=%d\n",
456
308k
              control, pim->Width, pim->Height);
457
308k
    if (control & PI_ImageMatrix) {
458
682
        if ((code = sget_matrix(s, &pim->ImageMatrix)) < 0)
459
0
            return code;
460
682
        debug_b_print_matrix(pim);
461
682
    } else
462
307k
        gx_image_matrix_set_default((gs_data_image_t *)pim);
463
308k
    pim->BitsPerComponent = ((control >> PI_BPC_SHIFT) & PI_BPC_MASK) + 1;
464
308k
    pim->format = (control >> PI_FORMAT_SHIFT) & PI_FORMAT_MASK;
465
308k
    pim->ColorSpace = pcs;
466
308k
    num_components = gs_color_space_num_components(pcs);
467
308k
    num_decode = num_components * 2;
468
308k
    if (gs_color_space_get_index(pcs) == gs_color_space_index_Indexed)
469
13.6k
        decode_default_1 = (float)pcs->params.indexed.hival;
470
308k
    if (control & PI_Decode) {
471
386
        uint dflags = 0x10000;
472
386
        float *dp = pim->Decode;
473
474
772
        for (i = 0; i < num_decode; i += 2, dp += 2, dflags <<= 2) {
475
386
            if (dflags >= 0x10000) {
476
386
                dflags = sgetc(s) + 0x100;
477
386
                if (dflags < 0x100)
478
0
                    return_error(gs_error_ioerror);
479
386
            }
480
386
            switch (dflags & 0xc0) {
481
0
            case 0x00:
482
0
                dp[0] = 0, dp[1] = DECODE_DEFAULT(i + 1, decode_default_1);
483
0
                break;
484
106
            case 0x40:
485
106
                dp[0] = DECODE_DEFAULT(i + 1, decode_default_1), dp[1] = 0;
486
106
                break;
487
280
            case 0x80:
488
280
                dp[0] = 0;
489
280
                if (sgets(s, (byte *)(dp + 1), sizeof(float), &ignore) < 0)
490
0
                    return_error(gs_error_ioerror);
491
280
                break;
492
280
            case 0xc0:
493
0
                if (sgets(s, (byte *)dp, sizeof(float) * 2, &ignore) < 0)
494
0
                    return_error(gs_error_ioerror);
495
0
                break;
496
386
            }
497
386
        }
498
386
        debug_b_print_decode(pim, num_decode);
499
308k
    } else {
500
1.40M
        for (i = 0; i < num_decode; ++i)
501
1.09M
            pim->Decode[i] = DECODE_DEFAULT(i, decode_default_1);
502
308k
    }
503
308k
    pim->Interpolate = (control & PI_Interpolate) != 0;
504
308k
    pim->CombineWithColor = (control & PI_CombineWithColor) != 0;
505
308k
    return control >> PI_BITS;
506
308k
}
507
508
/*
509
 * Release a pixel image object.  Currently this just frees the object.
510
 */
511
void
512
gx_pixel_image_release(gs_pixel_image_t *pic, gs_memory_t *mem)
513
0
{
514
0
    gx_image_default_release((gs_image_common_t *)pic, mem);
515
0
}