Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gsimage.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
/* Image setup procedures for Ghostscript library */
18
#include "memory_.h"
19
#include "math_.h"
20
#include "gx.h"
21
#include "gserrors.h"
22
#include "gsstruct.h"
23
#include "gscspace.h"
24
#include "gsmatrix.h"   /* for gsiparam.h */
25
#include "gsimage.h"
26
#include "gxarith.h"    /* for igcd */
27
#include "gxdevice.h"
28
#include "gxiparam.h"
29
#include "gxpath.h"   /* for gx_effective_clip_path */
30
#include "gximask.h"
31
#include "gzstate.h"
32
#include "gsutil.h"
33
#include "gxdevsop.h"
34
#include "gximage.h"
35
36
/*
37
  The main internal invariant for the gs_image machinery is
38
  straightforward.  The state consists primarily of N plane buffers
39
  (planes[]).
40
*/
41
typedef struct image_enum_plane_s {
42
/*
43
  The state of each plane consists of:
44
45
  - A row buffer, aligned and (logically) large enough to hold one scan line
46
    for that plane.  (It may have to be reallocated if the plane width or
47
    depth changes.)  A row buffer is "full" if it holds exactly a full scan
48
    line.
49
*/
50
    gs_string row;
51
/*
52
  - A position within the row buffer, indicating how many initial bytes are
53
    occupied.
54
*/
55
    uint pos;
56
/*
57
  - A (retained) source string, which may be empty (size = 0).
58
*/
59
    gs_const_string source;
60
    /* The gs_string 'orig' is only set if the 'txfer_control' flag was set when
61
     * the 'source' string data was initally passed in. In this case we now control the lifetime
62
     * of the string. So when we empty the source string, free it. We need to know the actual
63
     * address of the string, and that gets modified in the peunum->planes->source and size
64
     * members, so we use 'orig' as both a marker for the control and the original size and location.
65
     */
66
    gs_const_string orig;
67
} image_enum_plane_t;
68
/*
69
  The possible states for each plane do not depend on the state of any other
70
  plane.  Either:
71
72
  - pos = 0, source.size = 0.
73
74
  - If the underlying image processor says the plane is currently wanted,
75
    either:
76
77
    - pos = 0, source.size >= one full row of data for this plane.  This
78
      case allows us to avoid copying the data from the source string to the
79
      row buffer if the client is providing data in blocks of at least one
80
      scan line.
81
82
    - pos = full, source.size may have any value.
83
84
    - pos > 0, pos < full, source.size = 0;
85
86
  - If the underlying image processor says the plane is not currently
87
    wanted:
88
89
    - pos = 0, source.size may have any value.
90
91
  This invariant holds at the beginning and end of each call on
92
  gs_image_next_planes.  Note that for each plane, the "plane wanted" status
93
  and size of a full row may change after each call of plane_data.  As
94
  documented in gxiparam.h, we assume that a call of plane_data can only
95
  change a plane's status from "wanted" to "not wanted", or change the width
96
  or depth of a wanted plane, if data for that plane was actually supplied
97
  (and used).
98
*/
99
100
/* Define the enumeration state for this interface layer. */
101
/*typedef struct gs_image_enum_s gs_image_enum; *//* in gsimage.h */
102
struct gs_image_enum_s {
103
    /* The following are set at initialization time. */
104
    gs_memory_t *memory;
105
    gx_device *dev;   /* if 0, just skip over the data */
106
    gx_image_enum_common_t *info; /* driver bookkeeping structure */
107
    int num_planes;
108
    int height;
109
    bool wanted_varies;
110
    /* The following are updated dynamically. */
111
    int plane_index;    /* index of next plane of data, */
112
                                /* only needed for gs_image_next */
113
    int y;
114
    bool error;
115
    byte wanted[GS_IMAGE_MAX_COMPONENTS]; /* cache gx_image_planes_wanted */
116
    byte client_wanted[GS_IMAGE_MAX_COMPONENTS]; /* see gsimage.h */
117
    image_enum_plane_t planes[GS_IMAGE_MAX_COMPONENTS]; /* see above */
118
    /*
119
     * To reduce setup for transferring complete rows, we maintain a
120
     * partially initialized parameter array for gx_image_plane_data_rows.
121
     * The data member is always set just before calling
122
     * gx_image_plane_data_rows; the data_x and raster members are reset
123
     * when needed.
124
     */
125
    gx_image_plane_t image_planes[GS_IMAGE_MAX_COMPONENTS];
126
};
127
128
gs_private_st_composite(st_gs_image_enum, gs_image_enum, "gs_image_enum",
129
                        gs_image_enum_enum_ptrs, gs_image_enum_reloc_ptrs);
130
18
#define gs_image_enum_num_ptrs 2
131
132
/* GC procedures */
133
static
134
30
ENUM_PTRS_WITH(gs_image_enum_enum_ptrs, gs_image_enum *eptr)
135
18
{
136
    /* Enumerate the data planes. */
137
18
    index -= gs_image_enum_num_ptrs;
138
18
    if (index < eptr->num_planes)
139
6
        ENUM_RETURN_STRING_PTR(gs_image_enum, planes[index].source);
140
12
    index -= eptr->num_planes;
141
12
    if (index < eptr->num_planes)
142
6
        ENUM_RETURN_STRING_PTR(gs_image_enum, planes[index].row);
143
6
    return 0;
144
12
}
145
12
ENUM_PTR(0, gs_image_enum, dev);
146
30
ENUM_PTR(1, gs_image_enum, info);
147
30
ENUM_PTRS_END
148
6
static RELOC_PTRS_WITH(gs_image_enum_reloc_ptrs, gs_image_enum *eptr)
149
6
{
150
6
    int i;
151
152
6
    RELOC_PTR(gs_image_enum, dev);
153
6
    RELOC_PTR(gs_image_enum, info);
154
12
    for (i = 0; i < eptr->num_planes; i++)
155
6
        RELOC_CONST_STRING_PTR(gs_image_enum, planes[i].source);
156
12
    for (i = 0; i < eptr->num_planes; i++)
157
6
        RELOC_STRING_PTR(gs_image_enum, planes[i].row);
158
6
}
159
6
RELOC_PTRS_END
160
161
static int
162
is_image_visible(const gs_image_common_t * pic, gs_gstate * pgs, gx_clip_path *pcpath)
163
964k
{
164
964k
    gs_rect image_rect = {{0, 0}, {0, 0}};
165
964k
    gs_rect device_rect;
166
964k
    gs_int_rect device_int_rect;
167
964k
    gs_matrix mat;
168
964k
    int code;
169
170
964k
    image_rect.q.x = pic->Width;
171
964k
    image_rect.q.y = pic->Height;
172
964k
    if (pic->ImageMatrix.xx == ctm_only(pgs).xx &&
173
87.9k
        pic->ImageMatrix.xy == ctm_only(pgs).xy &&
174
87.9k
        pic->ImageMatrix.yx == ctm_only(pgs).yx &&
175
87.9k
        pic->ImageMatrix.yy == ctm_only(pgs).yy) {
176
        /* Handle common special case separately to accept singular matrix */
177
87.8k
        mat.xx = mat.yy = 1.;
178
87.8k
        mat.yx = mat.xy = 0.;
179
87.8k
        mat.tx = ctm_only(pgs).tx - pic->ImageMatrix.tx;
180
87.8k
        mat.ty = ctm_only(pgs).ty - pic->ImageMatrix.ty;
181
876k
    } else {
182
876k
        code = gs_matrix_invert(&pic->ImageMatrix, &mat);
183
876k
        if (code < 0)
184
0
            return code;
185
876k
        code = gs_matrix_multiply(&mat, &ctm_only(pgs), &mat);
186
876k
        if (code < 0)
187
0
            return code;
188
876k
    }
189
964k
    code = gs_bbox_transform(&image_rect, &mat, &device_rect);
190
964k
    if (code < 0)
191
0
        return code;
192
964k
    device_int_rect.p.x = (int)floor(device_rect.p.x);
193
964k
    device_int_rect.p.y = (int)floor(device_rect.p.y);
194
964k
    device_int_rect.q.x = (int)ceil(device_rect.q.x);
195
964k
    device_int_rect.q.y = (int)ceil(device_rect.q.y);
196
964k
    if (!gx_cpath_rect_visible(pcpath, &device_int_rect))
197
362k
        return 0;
198
602k
    return 1;
199
964k
}
200
201
/* Create an image enumerator given image parameters and a graphics state. */
202
int
203
gs_image_begin_typed(const gs_image_common_t * pic, gs_gstate * pgs,
204
                     bool uses_color, bool image_is_text, gx_image_enum_common_t ** ppie)
205
966k
{
206
966k
    gx_device *dev = gs_currentdevice(pgs);
207
966k
    gx_clip_path *pcpath;
208
966k
    int code = gx_effective_clip_path(pgs, &pcpath);
209
966k
    gx_device *dev2 = dev;
210
966k
    gx_device_color dc_temp, *pdevc = gs_currentdevicecolor_inline(pgs);
211
212
966k
    if (code < 0)
213
0
        return code;
214
    /* Processing an image object operation, but this may be for a text object */
215
966k
    ensure_tag_is_set(pgs, pgs->device, image_is_text ? GS_TEXT_TAG : GS_IMAGE_TAG);  /* NB: may unset_dev_color */
216
217
966k
    if (uses_color) {
218
808k
        code = gx_set_dev_color(pgs);
219
808k
        if (code != 0)
220
9
            return code;
221
808k
        code = gs_gstate_color_load(pgs);
222
808k
        if (code < 0)
223
0
            return code;
224
808k
    }
225
226
966k
    if (pgs->overprint || dev_proc(pgs->device, dev_spec_op)(pgs->device, gxdso_overprint_active, NULL, 0) > 0) {
227
273
        gs_overprint_params_t op_params = { 0 };
228
229
273
        if_debug0m(gs_debug_flag_overprint, pgs->memory,
230
273
            "[overprint] Image Overprint\n");
231
273
        code = gs_do_set_overprint(pgs);
232
273
        if (code < 0)
233
0
            return code;
234
235
273
        op_params.op_state = GS_OP_STATE_FILL;
236
273
        gs_gstate_update_overprint(pgs, &op_params);
237
238
273
        dev = gs_currentdevice(pgs);
239
273
        dev2 = dev;
240
273
    }
241
242
    /* Imagemask with shading color needs a special optimization
243
       with converting the image into a clipping.
244
       Check for such case after gs_gstate_color_load is done,
245
       because it can cause interpreter callout.
246
     */
247
966k
    if (pic->type->begin_typed_image == &gx_begin_image1) {
248
954k
        gs_image_t *image = (gs_image_t *)pic;
249
250
954k
        if(image->ImageMask) {
251
800k
            bool transpose = false;
252
800k
            gs_matrix_double mat;
253
254
800k
            if((code = gx_image_compute_mat(pgs, NULL, &(image->ImageMatrix), &mat)) < 0)
255
25
                return code;
256
800k
            if ((any_abs(mat.xy) > any_abs(mat.xx)) && (any_abs(mat.yx) > any_abs(mat.yy)))
257
129k
                transpose = true;   /* pure landscape */
258
800k
            code = gx_image_fill_masked_start(dev, gs_currentdevicecolor_inline(pgs), transpose,
259
800k
                                              pcpath, pgs->memory, pgs->log_op, &dev2);
260
800k
            if (code < 0)
261
0
                return code;
262
800k
        }
263
954k
        if (dev->interpolate_control < 0) {   /* Force interpolation before begin_typed_image */
264
3.73k
            ((gs_data_image_t *)pic)->Interpolate = true;
265
3.73k
        }
266
950k
        else if (dev->interpolate_control == 0) {
267
640k
            ((gs_data_image_t *)pic)->Interpolate = false; /* Suppress interpolation */
268
640k
        }
269
954k
        if (dev2 != dev) {
270
931
            set_nonclient_dev_color(&dc_temp, 1);
271
931
            pdevc = &dc_temp;
272
931
        }
273
954k
    }
274
966k
    code = gx_device_begin_typed_image(dev2, (const gs_gstate *)pgs,
275
966k
                NULL, pic, NULL, pdevc, pcpath, pgs->memory, ppie);
276
966k
    if (code < 0)
277
1.58k
        return code;
278
964k
    code = is_image_visible(pic, pgs, pcpath);
279
964k
    if (code < 0)
280
0
        return code;
281
964k
    if (!code)
282
362k
        (*ppie)->skipping = true;
283
964k
    return 0;
284
964k
}
285
286
/* Allocate an image enumerator. */
287
static void
288
image_enum_init(gs_image_enum * penum)
289
2.12M
{
290
    /* Clean pointers for GC. */
291
2.12M
    penum->info = 0;
292
2.12M
    penum->dev = 0;
293
2.12M
    penum->plane_index = 0;
294
2.12M
    penum->num_planes = 0;
295
2.12M
}
296
gs_image_enum *
297
gs_image_enum_alloc(gs_memory_t * mem, client_name_t cname)
298
1.18M
{
299
1.18M
    gs_image_enum *penum =
300
1.18M
        gs_alloc_struct(mem, gs_image_enum, &st_gs_image_enum, cname);
301
302
1.18M
    if (penum != 0) {
303
1.18M
        penum->memory = mem;
304
1.18M
        image_enum_init(penum);
305
1.18M
    }
306
1.18M
    return penum;
307
1.18M
}
308
309
/* Start processing an ImageType 1 image. */
310
int
311
gs_image_init(gs_image_enum * penum, const gs_image_t * pim, bool multi,
312
              bool image_is_text, gs_gstate * pgs)
313
375k
{
314
375k
    gs_image_t image;
315
375k
    gx_image_enum_common_t *pie;
316
375k
    int code;
317
318
375k
    image = *pim;
319
375k
    if (image.ImageMask) {
320
300k
        image.ColorSpace = NULL;
321
300k
        if (pgs->in_cachedevice <= 1)
322
85.6k
            image.adjust = false;
323
300k
    } else {
324
74.2k
        if (pgs->in_cachedevice)
325
0
            return_error(gs_error_undefined);
326
74.2k
        if (image.ColorSpace == NULL) {
327
            /*
328
             * Use of a non-current color space is potentially
329
             * incorrect, but it appears this case doesn't arise.
330
             */
331
0
            image.ColorSpace = gs_cspace_new_DeviceGray(pgs->memory);
332
0
            if (image.ColorSpace == NULL)
333
0
                return_error(gs_error_VMerror);
334
0
        }
335
74.2k
    }
336
375k
    code = gs_image_begin_typed((const gs_image_common_t *)&image, pgs,
337
375k
                                image.ImageMask | image.CombineWithColor,
338
375k
                                image_is_text, &pie);
339
375k
    if (code < 0)
340
75
        return code;
341
375k
    return gs_image_enum_init(penum, pie, (const gs_data_image_t *)&image,
342
375k
                              pgs);
343
375k
}
344
345
/*
346
 * Return the number of bytes of data per row for a given plane.
347
 */
348
inline uint
349
gs_image_bytes_per_plane_row(const gs_image_enum * penum, int plane)
350
1.79M
{
351
1.79M
    const gx_image_enum_common_t *pie = penum->info;
352
353
1.79M
    return (pie->plane_widths[plane] * pie->plane_depths[plane] + 7) >> 3;
354
1.79M
}
355
356
/* Cache information when initializing, or after transferring plane data. */
357
static void
358
cache_planes(gs_image_enum *penum)
359
32.5M
{
360
32.5M
    int i;
361
362
32.5M
    if (penum->wanted_varies) {
363
1.37M
        penum->wanted_varies =
364
1.37M
            !gx_image_planes_wanted(penum->info, penum->wanted);
365
3.17M
        for (i = 0; i < penum->num_planes; ++i)
366
1.80M
            if (penum->wanted[i])
367
1.79M
                penum->image_planes[i].raster =
368
1.79M
                    gs_image_bytes_per_plane_row(penum, i);
369
8.44k
            else
370
8.44k
                penum->image_planes[i].data = 0;
371
1.37M
    }
372
32.5M
}
373
/* Advance to the next wanted plane. */
374
static void
375
next_plane(gs_image_enum *penum)
376
17.8M
{
377
17.8M
    int px = penum->plane_index;
378
379
17.8M
    do {
380
17.8M
        if (++px == penum->num_planes)
381
16.9M
            px = 0;
382
17.8M
    } while (!penum->wanted[px]);
383
17.8M
    penum->plane_index = px;
384
17.8M
}
385
/*
386
 * Initialize plane_index and (if appropriate) wanted and
387
 * wanted_varies at the beginning of a group of planes.
388
 */
389
static void
390
begin_planes(gs_image_enum *penum)
391
945k
{
392
945k
    cache_planes(penum);
393
945k
    penum->plane_index = -1;
394
945k
    next_plane(penum);
395
945k
}
396
397
int
398
gs_image_common_init(gs_image_enum * penum, gx_image_enum_common_t * pie,
399
            const gs_data_image_t * pim, gx_device * dev)
400
964k
{
401
    /*
402
     * HACK : For a compatibility with gs_image_cleanup_and_free_enum,
403
     * penum->memory must be initialized in advance
404
     * with the memory heap that owns *penum.
405
     */
406
964k
    int i;
407
408
964k
    if (pim->Width == 0 || pim->Height == 0) {
409
18.8k
        gx_device *cdev = pie->dev;
410
411
18.8k
        gx_image_end(pie, false);
412
18.8k
        if (dev_proc(cdev, dev_spec_op)(cdev,
413
18.8k
                    gxdso_pattern_is_cpath_accum, NULL, 0) > 0)
414
69
            gx_device_retain((gx_device *)cdev, false);
415
18.8k
        return 1;
416
18.8k
    }
417
945k
    image_enum_init(penum);
418
945k
    penum->dev = dev;
419
945k
    penum->info = pie;
420
945k
    penum->num_planes = pie->num_planes;
421
    /*
422
     * Note that for ImageType 3 InterleaveType 2, penum->height (the
423
     * expected number of data rows) differs from pim->Height (the height
424
     * of the source image in scan lines).  This doesn't normally cause
425
     * any problems, because penum->height is not used to determine when
426
     * all the data has been processed: that is up to the plane_data
427
     * procedure for the specific image type.
428
     */
429
945k
    penum->height = pim->Height;
430
1.89M
    for (i = 0; i < pie->num_planes; ++i) {
431
950k
        penum->planes[i].pos = 0;
432
950k
        penum->planes[i].source.size = 0; /* for gs_image_next_planes */
433
950k
        penum->planes[i].source.data = 0; /* for GC */
434
950k
        penum->planes[i].row.data = 0; /* for GC */
435
950k
        penum->planes[i].row.size = 0; /* ditto */
436
950k
        penum->image_planes[i].data_x = 0; /* just init once, never changes */
437
950k
    }
438
    /* Initialize the dynamic part of the state. */
439
945k
    penum->y = 0;
440
945k
    penum->error = false;
441
945k
    penum->wanted_varies = true;
442
945k
    begin_planes(penum);
443
945k
    return 0;
444
964k
}
445
446
/* Initialize an enumerator for a general image.
447
   penum->memory must be initialized in advance.
448
*/
449
int
450
gs_image_enum_init(gs_image_enum * penum, gx_image_enum_common_t * pie,
451
                   const gs_data_image_t * pim, gs_gstate *pgs)
452
956k
{
453
956k
    pgs->device->sgr.stroke_stored = false;
454
956k
    return gs_image_common_init(penum, pie, pim,
455
956k
                                (pgs->in_charpath ? NULL :
456
956k
                                 gs_currentdevice_inline(pgs)));
457
956k
}
458
459
/* Return the set of planes wanted. */
460
const byte *
461
gs_image_planes_wanted(gs_image_enum *penum)
462
13.6M
{
463
13.6M
    int i;
464
465
    /*
466
     * A plane is wanted at this interface if it is wanted by the
467
     * underlying machinery and has no buffered or retained data.
468
     */
469
27.2M
    for (i = 0; i < penum->num_planes; ++i)
470
13.6M
        penum->client_wanted[i] =
471
13.6M
            (penum->wanted[i] &&
472
13.6M
             penum->planes[i].pos + penum->planes[i].source.size <
473
13.6M
               penum->image_planes[i].raster);
474
13.6M
    return penum->client_wanted;
475
13.6M
}
476
477
/*
478
 * Return the enumerator memory used for allocating the row buffers.
479
 * Because some PostScript files use save/restore within an image data
480
 * reading procedure, this must be a stable allocator.
481
 */
482
static gs_memory_t *
483
gs_image_row_memory(const gs_image_enum *penum)
484
23.7M
{
485
23.7M
    return gs_memory_stable(penum->memory);
486
23.7M
}
487
488
/* Free the row buffers when cleaning up. */
489
static void
490
free_row_buffers(gs_image_enum *penum, int num_planes, client_name_t cname)
491
966k
{
492
966k
    int i;
493
494
1.91M
    for (i = num_planes - 1; i >= 0; --i) {
495
950k
        if_debug3m('b', penum->memory, "[b]free plane %d row ("PRI_INTPTR",%u)\n",
496
950k
                   i, (intptr_t)penum->planes[i].row.data,
497
950k
                   penum->planes[i].row.size);
498
950k
        gs_free_string(gs_image_row_memory(penum), penum->planes[i].row.data,
499
950k
                       penum->planes[i].row.size, cname);
500
950k
        penum->planes[i].row.data = 0;
501
950k
        penum->planes[i].row.size = 0;
502
950k
    }
503
966k
}
504
505
/* Process the next piece of an image. */
506
int
507
gs_image_next(gs_image_enum * penum, const byte * dbytes, uint dsize,
508
              uint * pused)
509
16.9M
{
510
16.9M
    int px = penum->plane_index;
511
16.9M
    int num_planes = penum->num_planes;
512
16.9M
    int i, code;
513
16.9M
    uint used[GS_IMAGE_MAX_COMPONENTS];
514
16.9M
    gs_const_string plane_data[GS_IMAGE_MAX_COMPONENTS];
515
516
16.9M
    if (penum->planes[px].source.size != 0)
517
8
        return_error(gs_error_rangecheck);
518
33.8M
    for (i = 0; i < num_planes; i++)
519
16.9M
        plane_data[i].size = 0;
520
16.9M
    plane_data[px].data = dbytes;
521
16.9M
    plane_data[px].size = dsize;
522
16.9M
    penum->error = false;
523
16.9M
    code = gs_image_next_planes(penum, plane_data, used, false);
524
16.9M
    *pused = used[px];
525
16.9M
    if (code >= 0)
526
16.9M
        next_plane(penum);
527
16.9M
    return code;
528
16.9M
}
529
530
int
531
gs_image_next_planes(gs_image_enum * penum,
532
                     gs_const_string *plane_data /*[num_planes]*/,
533
                     uint *used /*[num_planes]*/, bool txfer_control)
534
31.0M
{
535
31.0M
    const int num_planes = penum->num_planes;
536
31.0M
    int i;
537
31.0M
    int code = 0;
538
539
#ifdef DEBUG
540
    if (gs_debug_c('b')) {
541
        int pi;
542
543
        for (pi = 0; pi < num_planes; ++pi)
544
            dmprintf6(penum->memory, "[b]plane %d source="PRI_INTPTR",%u pos=%u data="PRI_INTPTR",%u\n",
545
                     pi, (intptr_t)penum->planes[pi].source.data,
546
                     penum->planes[pi].source.size, penum->planes[pi].pos,
547
                     (intptr_t)plane_data[pi].data, plane_data[pi].size);
548
    }
549
#endif
550
62.2M
    for (i = 0; i < num_planes; ++i) {
551
31.1M
        used[i] = 0;
552
31.1M
        if (penum->wanted[i] && plane_data[i].size != 0) {
553
31.1M
            penum->planes[i].source.size = plane_data[i].size;
554
31.1M
            penum->planes[i].source.data = plane_data[i].data;
555
            /* The gs_string 'orig' in penum->planes is set here if the 'txfer_control' flag is set.
556
             * In this case we now control the lifetime of the string. We need to know the actual
557
             * address of the string, and that gets modified in the peunum->planes->source and size
558
             * members, so we use 'orig' as both a marker for the control and the originalsize and location.
559
             */
560
31.1M
            if (txfer_control) {
561
6.95M
                penum->planes[i].orig.data = plane_data[i].data;
562
6.95M
                penum->planes[i].orig.size = plane_data[i].size;
563
24.2M
            } else {
564
24.2M
                penum->planes[i].orig.data = NULL;
565
24.2M
                penum->planes[i].orig.size = 0;
566
24.2M
            }
567
31.1M
        }
568
31.1M
    }
569
61.7M
    for (;;) {
570
        /* If wanted can vary, only transfer 1 row at a time. */
571
61.7M
        int h = (penum->wanted_varies ? 1 : max_int);
572
573
        /* Move partial rows from source[] to row[]. */
574
123M
        for (i = 0; i < num_planes; ++i) {
575
62.2M
            int pos, size;
576
62.2M
            uint raster;
577
578
62.2M
            if (!penum->wanted[i])
579
8.51k
                continue;  /* skip unwanted planes */
580
62.2M
            pos = penum->planes[i].pos;
581
62.2M
            size = penum->planes[i].source.size;
582
62.2M
            raster = penum->image_planes[i].raster;
583
62.2M
            if (size > 0) {
584
42.3M
                if (pos < raster && (pos != 0 || size < raster)) {
585
                    /* Buffer a partial row. */
586
17.1M
                    int copy = min(size, raster - pos);
587
17.1M
                    uint old_size = penum->planes[i].row.size;
588
17.1M
                    gs_memory_t *mem = gs_image_row_memory(penum);
589
590
                    /* Make sure the row buffer is fully allocated. */
591
17.1M
                    if (raster > old_size) {
592
269k
                        byte *old_data = penum->planes[i].row.data;
593
269k
                        byte *row =
594
269k
                            (old_data == 0 ?
595
269k
                             gs_alloc_string(mem, raster,
596
269k
                                             "gs_image_next(row)") :
597
269k
                             gs_resize_string(mem, old_data, old_size, raster,
598
269k
                                              "gs_image_next(row)"));
599
600
269k
                        if_debug5m('b', mem, "[b]plane %d row ("PRI_INTPTR",%u) => ("PRI_INTPTR",%u)\n",
601
269k
                                   i, (intptr_t)old_data, old_size,
602
269k
                                   (intptr_t)row, raster);
603
269k
                        if (row == 0) {
604
0
                            code = gs_note_error(gs_error_VMerror);
605
0
                            free_row_buffers(penum, i, "gs_image_next(row)");
606
0
                            break;
607
0
                        }
608
269k
                        penum->planes[i].row.data = row;
609
269k
                        penum->planes[i].row.size = raster;
610
269k
                    }
611
17.1M
                    memcpy(penum->planes[i].row.data + pos,
612
17.1M
                           penum->planes[i].source.data, copy);
613
17.1M
                    penum->planes[i].source.data += copy;
614
17.1M
                    penum->planes[i].source.size = size -= copy;
615
                    /* The gs_string 'orig' is only set if the 'txfer_control' flag was set when
616
                     * the 'source' string data was initally passed in. In this case we now control the lifetime
617
                     * of the string. So when we empty the source string, free it. We need to know the actual
618
                     * address of the string, and that gets modified in the peunum->planes->source and size
619
                     * members, so we use 'orig' as both a marker for the control and the originalsize and location.
620
                     */
621
17.1M
                    if (penum->planes[i].source.size == 0 && penum->planes[i].orig.size != 0) {
622
4.37M
                        gs_free_string(mem, (byte *)penum->planes[i].orig.data, penum->planes[i].orig.size, "gs_image_next_planes");
623
4.37M
                        penum->planes[i].orig.size = 0;
624
4.37M
                        penum->planes[i].orig.data = NULL;
625
4.37M
                    }
626
17.1M
                    penum->planes[i].pos = pos += copy;
627
17.1M
                    used[i] += copy;
628
17.1M
                }
629
42.3M
            }
630
62.2M
            if (h == 0)
631
10.2k
                continue;  /* can't transfer any data this cycle */
632
62.2M
            if (pos == raster) {
633
                /*
634
                 * This plane will be transferred from the row buffer,
635
                 * so we can only transfer one row.
636
                 */
637
6.89M
                h = min(h, 1);
638
6.89M
                penum->image_planes[i].data = penum->planes[i].row.data;
639
55.3M
            } else if (pos == 0 && size >= raster) {
640
                /* We can transfer 1 or more planes from the source. */
641
25.1M
                if (raster) {
642
25.1M
                    h = min(h, size / raster);
643
25.1M
                    penum->image_planes[i].data = penum->planes[i].source.data;
644
25.1M
                }
645
0
                else
646
0
                    h = 0;
647
25.1M
            } else
648
30.1M
                h = 0;   /* not enough data in this plane */
649
62.2M
        }
650
61.7M
        if (h == 0 || code != 0)
651
30.1M
            break;
652
        /* Pass rows to the device. */
653
31.5M
        if (penum->dev == 0) {
654
            /*
655
             * ****** NOTE: THE FOLLOWING IS NOT CORRECT FOR ImageType 3
656
             * ****** InterleaveType 2, SINCE MASK HEIGHT AND IMAGE HEIGHT
657
             * ****** MAY DIFFER (BY AN INTEGER FACTOR).  ALSO, plane_depths[0]
658
             * ****** AND plane_widths[0] ARE NOT UPDATED.
659
         */
660
0
            if (penum->y + h < penum->height)
661
0
                code = 0;
662
0
            else
663
0
                h = penum->height - penum->y, code = 1;
664
31.5M
        } else {
665
31.5M
            code = gx_image_plane_data_rows(penum->info, penum->image_planes,
666
31.5M
                                            h, &h);
667
31.5M
            if_debug2m('b', penum->memory, "[b]used %d, code=%d\n", h, code);
668
31.5M
            penum->error = code < 0;
669
31.5M
        }
670
31.5M
        penum->y += h;
671
        /* Update positions and sizes. */
672
31.5M
        if (h == 0)
673
3
            break;
674
63.5M
        for (i = 0; i < num_planes; ++i) {
675
31.9M
            int count;
676
677
31.9M
            if (!penum->wanted[i])
678
8.39k
                continue;
679
31.9M
            count = penum->image_planes[i].raster * h;
680
31.9M
            if (penum->planes[i].pos) {
681
                /* We transferred the row from the row buffer. */
682
6.89M
                penum->planes[i].pos = 0;
683
25.0M
            } else {
684
                /* We transferred the row(s) from the source. */
685
25.0M
                penum->planes[i].source.data += count;
686
25.0M
                penum->planes[i].source.size -= count;
687
                /* The gs_string 'orig' is only set if the 'txfer_control' flag was set when
688
                 * the 'source' string data was initally passed in. In this case we now control the lifetime
689
                 * of the string. So when we empty the source string, free it. We need to know the actual
690
                 * address of the string, and that gets modified in the peunum->planes->source and size
691
                 * members, so we use 'orig' as both a marker for the control and the originalsize and location.
692
                 */
693
25.0M
                if (penum->planes[i].source.size == 0 && penum->planes[i].orig.size != 0) {
694
2.33M
                    gs_free_string(gs_image_row_memory(penum), (byte *)penum->planes[i].orig.data, penum->planes[i].orig.size, "gs_image_next_planes");
695
2.33M
                    penum->planes[i].orig.size = 0;
696
2.33M
                    penum->planes[i].orig.data = NULL;
697
2.33M
                }
698
25.0M
                used[i] += count;
699
25.0M
            }
700
31.9M
        }
701
31.5M
        cache_planes(penum);
702
31.5M
        if (code != 0)
703
908k
            break;
704
31.5M
    }
705
    /* Return the retained data pointers. */
706
62.2M
    for (i = 0; i < num_planes; ++i)
707
31.1M
        plane_data[i] = penum->planes[i].source;
708
31.0M
    return code;
709
31.0M
}
710
711
/* Clean up after processing an image. */
712
/* Public for ghostpcl. */
713
int
714
gs_image_cleanup(gs_image_enum * penum, gs_gstate *pgs)
715
966k
{
716
966k
    int code = 0, code1;
717
718
966k
    free_row_buffers(penum, penum->num_planes, "gs_image_cleanup(row)");
719
966k
    if (penum->info != 0) {
720
945k
        if (dev_proc(penum->info->dev, dev_spec_op)(penum->info->dev,
721
945k
                    gxdso_pattern_is_cpath_accum, NULL, 0) > 0) {
722
            /* Performing a conversion of imagemask into a clipping path. */
723
862
            gx_device *cdev = penum->info->dev;
724
725
862
            code = gx_image_end(penum->info, !penum->error); /* Releases penum->info . */
726
862
            code1 = gx_image_fill_masked_end(cdev, penum->dev, gs_currentdevicecolor_inline(pgs));
727
862
            if (code == 0)
728
862
                code = code1;
729
862
        } else
730
944k
            code = gx_image_end(penum->info, !penum->error);
731
945k
    }
732
    /* Don't free the local enumerator -- the client does that. */
733
734
966k
    return code;
735
966k
}
736
737
/* Clean up after processing an image and free the enumerator. */
738
int
739
gs_image_cleanup_and_free_enum(gs_image_enum * penum, gs_gstate *pgs)
740
951k
{
741
951k
    int code;
742
743
951k
    if (penum == NULL)
744
0
            return 0;
745
951k
    code = gs_image_cleanup(penum, pgs);
746
747
    gs_free_object(penum->memory, penum, "gs_image_cleanup_and_free_enum");
748
951k
    return code;
749
951k
}