/src/ghostpdl/base/gximage3.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2025 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 | | /* ImageType 3 image implementation */ |
18 | | #include "math_.h" /* for ceil, floor */ |
19 | | #include "memory_.h" |
20 | | #include "gx.h" |
21 | | #include "gserrors.h" |
22 | | #include "gsbitops.h" |
23 | | #include "gscspace.h" |
24 | | #include "gsstruct.h" |
25 | | #include "gxdevice.h" |
26 | | #include "gxdevmem.h" |
27 | | #include "gxclipm.h" |
28 | | #include "gximage3.h" |
29 | | #include "gxgstate.h" |
30 | | #include "gxdevsop.h" |
31 | | #include <limits.h> /* For INT_MAX etc */ |
32 | | |
33 | | /* Forward references */ |
34 | | static dev_proc_begin_typed_image(gx_begin_image3); |
35 | | static image_enum_proc_plane_data(gx_image3_plane_data); |
36 | | static image_enum_proc_end_image(gx_image3_end_image); |
37 | | static image_enum_proc_flush(gx_image3_flush); |
38 | | static image_enum_proc_planes_wanted(gx_image3_planes_wanted); |
39 | | |
40 | | /* GC descriptor */ |
41 | | private_st_gs_image3(); |
42 | | |
43 | | /* Define the image type for ImageType 3 images. */ |
44 | | const gx_image_type_t gs_image_type_3 = { |
45 | | &st_gs_image3, gx_begin_image3, |
46 | | gx_image_no_sput, gx_image_no_sget, gx_image_default_release, 3 |
47 | | }; |
48 | | static const gx_image_enum_procs_t image3_enum_procs = { |
49 | | gx_image3_plane_data, gx_image3_end_image, |
50 | | gx_image3_flush, gx_image3_planes_wanted |
51 | | }; |
52 | | |
53 | | /* Initialize an ImageType 3 image. */ |
54 | | void |
55 | | gs_image3_t_init(gs_image3_t * pim, gs_color_space * color_space, |
56 | | gs_image3_interleave_type_t interleave_type) |
57 | 459 | { |
58 | 459 | gs_pixel_image_t_init((gs_pixel_image_t *) pim, color_space); |
59 | 459 | pim->type = &gs_image_type_3; |
60 | 459 | pim->InterleaveType = interleave_type; |
61 | 459 | gs_data_image_t_init(&pim->MaskDict, -1); |
62 | 459 | } |
63 | | |
64 | | /* |
65 | | * We implement ImageType 3 images by interposing a mask clipper in |
66 | | * front of an ordinary ImageType 1 image. Note that we build up the |
67 | | * mask row-by-row as we are processing the image. |
68 | | * |
69 | | * We export a generalized form of the begin_image procedure for use by |
70 | | * the PDF and PostScript writers. |
71 | | */ |
72 | | typedef struct gx_image3_enum_s { |
73 | | gx_image_enum_common; |
74 | | gx_device *mdev; /* gx_device_memory in default impl. */ |
75 | | gx_device *pcdev; /* gx_device_mask_clip in default impl. */ |
76 | | gx_image_enum_common_t *mask_info; |
77 | | gx_image_enum_common_t *pixel_info; |
78 | | gs_image3_interleave_type_t InterleaveType; |
79 | | int num_components; /* (not counting mask) */ |
80 | | int bpc; /* BitsPerComponent */ |
81 | | int mask_width, mask_height, mask_full_height; |
82 | | int pixel_width, pixel_height, pixel_full_height; |
83 | | byte *mask_data; /* (if chunky) */ |
84 | | byte *pixel_data; /* (if chunky) */ |
85 | | /* The following are the only members that change dynamically. */ |
86 | | int mask_y; |
87 | | int pixel_y; |
88 | | int mask_skip; /* # of mask rows to skip, see below */ |
89 | | } gx_image3_enum_t; |
90 | | |
91 | | extern_st(st_gx_image_enum_common); |
92 | | gs_private_st_suffix_add6(st_image3_enum, gx_image3_enum_t, "gx_image3_enum_t", |
93 | | image3_enum_enum_ptrs, image3_enum_reloc_ptrs, st_gx_image_enum_common, |
94 | | mdev, pcdev, pixel_info, mask_info, pixel_data, mask_data); |
95 | | |
96 | | /* Define the default implementation of ImageType 3 processing. */ |
97 | | static IMAGE3_MAKE_MID_PROC(make_mid_default); /* check prototype */ |
98 | | static int |
99 | | make_mid_default(gx_device **pmidev, gx_device *dev, int width, int height, |
100 | | gs_memory_t *mem) |
101 | 291 | { |
102 | 291 | gx_device_memory *midev = |
103 | 291 | gs_alloc_struct_immovable(mem, gx_device_memory, &st_device_memory, |
104 | 291 | "make_mid_default"); |
105 | 291 | int code; |
106 | | |
107 | 291 | if (midev == 0) |
108 | 0 | return_error(gs_error_VMerror); |
109 | 291 | gs_make_mem_mono_device(midev, mem, NULL); |
110 | 291 | midev->bitmap_memory = mem; |
111 | 291 | midev->width = width; |
112 | 291 | midev->height = height; |
113 | 291 | midev->raster = gx_device_raster((gx_device *)midev, 1); |
114 | 291 | check_device_separable((gx_device *)midev); |
115 | 291 | gx_device_fill_in_procs((gx_device *)midev); |
116 | 291 | code = dev_proc(midev, open_device)((gx_device *)midev); |
117 | 291 | if (code < 0) { |
118 | 0 | gs_free_object(mem, midev, "make_mid_default"); |
119 | 0 | return code; |
120 | 0 | } |
121 | 291 | midev->is_open = true; |
122 | 291 | dev_proc(midev, fill_rectangle) |
123 | 291 | ((gx_device *)midev, 0, 0, width, height, (gx_color_index)0); |
124 | 291 | *pmidev = (gx_device *)midev; |
125 | 291 | return 0; |
126 | 291 | } |
127 | | static IMAGE3_MAKE_MCDE_PROC(make_mcde_default); /* check prototype */ |
128 | | static int |
129 | | make_mcde_default(gx_device *dev, const gs_gstate *pgs, |
130 | | const gs_matrix *pmat, const gs_image_common_t *pic, |
131 | | const gs_int_rect *prect, const gx_drawing_color *pdcolor, |
132 | | const gx_clip_path *pcpath, gs_memory_t *mem, |
133 | | gx_image_enum_common_t **pinfo, |
134 | | gx_device **pmcdev, gx_device *midev, |
135 | | gx_image_enum_common_t *pminfo, |
136 | | const gs_int_point *origin) |
137 | 291 | { |
138 | 291 | gx_device_memory *const mdev = (gx_device_memory *)midev; |
139 | 291 | gx_device_mask_clip *mcdev = NULL; |
140 | 291 | gx_strip_bitmap bits; /* only gx_bitmap */ |
141 | 291 | int code; |
142 | | |
143 | | /* The gx_strip_bitmap structure defines (via gs_tile_bitmap_common) |
144 | | * rep_width and rep_height as being of type 'ushort', device width and |
145 | | * height are of type 'int'. Make sure we don't overflow because that |
146 | | * will lead to memory corruption. |
147 | | */ |
148 | 291 | if (mdev->width > ARCH_MAX_USHORT || mdev->height > ARCH_MAX_USHORT) |
149 | 0 | return_error(gs_error_rangecheck); |
150 | | |
151 | 291 | mcdev = gs_alloc_struct(mem, gx_device_mask_clip, &st_device_mask_clip, |
152 | 291 | "make_mcde_default"); |
153 | | |
154 | 291 | if (mcdev == 0) |
155 | 0 | return_error(gs_error_VMerror); |
156 | 291 | bits.data = mdev->base; |
157 | 291 | bits.raster = mdev->raster; |
158 | | |
159 | 291 | bits.size.x = bits.rep_width = mdev->width; |
160 | 291 | bits.size.y = bits.rep_height = mdev->height; |
161 | 291 | bits.id = gx_no_bitmap_id; |
162 | 291 | bits.num_planes = 1; |
163 | 291 | bits.rep_shift = bits.shift = 0; |
164 | 291 | code = gx_mask_clip_initialize(mcdev, &gs_mask_clip_device, |
165 | 291 | (const gx_bitmap *)&bits, dev, |
166 | 291 | origin->x, origin->y, mem); |
167 | 291 | if (code < 0) { |
168 | 0 | gs_free_object(mem, mcdev, "make_mcde_default"); |
169 | 0 | return code; |
170 | 0 | } |
171 | 291 | mcdev->tiles = bits; |
172 | 291 | code = dev_proc(mcdev, begin_typed_image) |
173 | 291 | ((gx_device *)mcdev, pgs, pmat, pic, prect, pdcolor, pcpath, mem, |
174 | 291 | pinfo); |
175 | 291 | if (code < 0) { |
176 | 0 | gs_free_object(mem, mcdev, "make_mcde_default"); |
177 | 0 | return code; |
178 | 0 | } |
179 | 291 | *pmcdev = (gx_device *)mcdev; |
180 | 291 | return 0; |
181 | 291 | } |
182 | | static int |
183 | | gx_begin_image3(gx_device * dev, |
184 | | const gs_gstate * pgs, const gs_matrix * pmat, |
185 | | const gs_image_common_t * pic, const gs_int_rect * prect, |
186 | | const gx_drawing_color * pdcolor, const gx_clip_path * pcpath, |
187 | | gs_memory_t * mem, gx_image_enum_common_t ** pinfo) |
188 | 296 | { |
189 | 296 | return gx_begin_image3_generic(dev, pgs, pmat, pic, prect, pdcolor, |
190 | 296 | pcpath, mem, make_mid_default, |
191 | 296 | make_mcde_default, pinfo); |
192 | 296 | } |
193 | | |
194 | | /* |
195 | | * Begin a generic ImageType 3 image, with client handling the creation of |
196 | | * the mask image and mask clip devices. |
197 | | */ |
198 | | static bool check_image3_extent(double mask_coeff, double data_coeff); |
199 | | int |
200 | | gx_begin_image3_generic(gx_device * dev, |
201 | | const gs_gstate *pgs, const gs_matrix *pmat, |
202 | | const gs_image_common_t *pic, const gs_int_rect *prect, |
203 | | const gx_drawing_color *pdcolor, |
204 | | const gx_clip_path *pcpath, gs_memory_t *mem, |
205 | | image3_make_mid_proc_t make_mid, |
206 | | image3_make_mcde_proc_t make_mcde, |
207 | | gx_image_enum_common_t **pinfo) |
208 | 407 | { |
209 | 407 | const gs_image3_t *pim = (const gs_image3_t *)pic; |
210 | 407 | gs_image3_t local_pim; |
211 | 407 | gx_image3_enum_t *penum; |
212 | 407 | gs_int_rect mask_rect, data_rect; |
213 | 407 | gx_device *mdev = 0; |
214 | 407 | gx_device *pcdev = 0; |
215 | 407 | gs_image_t i_pixel, i_mask; |
216 | 407 | gs_matrix mi_pixel, mi_mask, mat; |
217 | 407 | gs_rect mrect; |
218 | 407 | gs_int_point origin; |
219 | 407 | int code; |
220 | | |
221 | | /* Validate the parameters. */ |
222 | 407 | if (pim->Width <= 0 || pim->MaskDict.Width <= 0 || |
223 | 407 | pim->Height <= 0 || pim->MaskDict.Height <= 0) |
224 | 2 | return_error(gs_error_rangecheck); |
225 | 405 | switch (pim->InterleaveType) { |
226 | 0 | default: |
227 | 0 | return_error(gs_error_rangecheck); |
228 | 0 | case interleave_chunky: |
229 | 0 | if (pim->MaskDict.Width != pim->Width || |
230 | 0 | pim->MaskDict.Height != pim->Height || |
231 | 0 | pim->MaskDict.BitsPerComponent != pim->BitsPerComponent || |
232 | 0 | pim->format != gs_image_format_chunky |
233 | 0 | ) |
234 | 0 | return_error(gs_error_rangecheck); |
235 | 0 | break; |
236 | 0 | case interleave_scan_lines: |
237 | 0 | if (pim->MaskDict.Height % pim->Height != 0 && |
238 | 0 | pim->Height % pim->MaskDict.Height != 0 |
239 | 0 | ) |
240 | 0 | return_error(gs_error_rangecheck); |
241 | | /* falls through */ |
242 | 405 | case interleave_separate_source: |
243 | 405 | if (pim->MaskDict.BitsPerComponent != 1) |
244 | 0 | return_error(gs_error_rangecheck); |
245 | 405 | } |
246 | 405 | if ((code = gs_matrix_invert(&pim->ImageMatrix, &mi_pixel)) < 0) |
247 | 0 | return code; |
248 | | /* For Explicit Masking, we follow Acrobats example, and completely |
249 | | * ignore the supplied mask. Instead we generate a new one based on the |
250 | | * image mask, adjusted for any difference in width/height. */ |
251 | 405 | if (pim->InterleaveType == interleave_separate_source || |
252 | 405 | pim->InterleaveType == interleave_scan_lines) { |
253 | 405 | memcpy(&local_pim, pim, sizeof(local_pim)); |
254 | 405 | pim = &local_pim; |
255 | 405 | gs_matrix_scale(&mi_pixel, |
256 | 405 | ((double)pim->Width) / pim->MaskDict.Width, |
257 | 405 | ((double)pim->Height) / pim->MaskDict.Height, |
258 | 405 | &mi_mask); |
259 | 405 | if ((code = gs_matrix_invert(&mi_mask, &local_pim.MaskDict.ImageMatrix)) < 0) |
260 | 0 | return code; |
261 | 405 | } else { |
262 | 0 | if ((code = gs_matrix_invert(&pim->MaskDict.ImageMatrix, &mi_mask)) < 0) |
263 | 0 | return code; |
264 | | |
265 | 0 | if (!check_image3_extent(pim->ImageMatrix.xx, |
266 | 0 | pim->MaskDict.ImageMatrix.xx) || |
267 | 0 | !check_image3_extent(pim->ImageMatrix.xy, |
268 | 0 | pim->MaskDict.ImageMatrix.xy) || |
269 | 0 | !check_image3_extent(pim->ImageMatrix.yx, |
270 | 0 | pim->MaskDict.ImageMatrix.yx) || |
271 | 0 | !check_image3_extent(pim->ImageMatrix.yy, |
272 | 0 | pim->MaskDict.ImageMatrix.yy) |
273 | 0 | ) |
274 | 0 | return_error(gs_error_rangecheck); |
275 | 0 | } |
276 | 405 | if (fabs(mi_pixel.tx - mi_mask.tx) >= 0.5 || |
277 | 405 | fabs(mi_pixel.ty - mi_mask.ty) >= 0.5 |
278 | 405 | ) |
279 | 0 | return_error(gs_error_rangecheck); |
280 | | #ifdef DEBUG |
281 | | { |
282 | | /* Although the PLRM says that the Mask and Image *must* be the same size, */ |
283 | | /* Adobe CPSI (and other RIPS) ignore this and process anyway. Note that we */ |
284 | | /* are not compatible if the Mask Height than the Data (pixel) Height. CPSI */ |
285 | | /* de-interleaves the mask from the data image and stops at the Mask Height */ |
286 | | /* Problem detected with Genoa 468-03 (part of file 468-01.ps) */ |
287 | | /***** fixme: When Data Image Height > Mask Height *****/ |
288 | | gs_point ep, em; |
289 | | |
290 | | if ((code = gs_point_transform(pim->Width, pim->Height, &mi_pixel, |
291 | | &ep)) < 0 || |
292 | | (code = gs_point_transform(pim->MaskDict.Width, |
293 | | pim->MaskDict.Height, &mi_mask, |
294 | | &em)) < 0 |
295 | | ) |
296 | | return code; |
297 | | if (fabs(ep.x - em.x) >= 0.5 || fabs(ep.y - em.y) >= 0.5) |
298 | | code = gs_error_rangecheck; /* leave the check in for debug breakpoint */ |
299 | | } |
300 | | #endif /* DEBUG */ |
301 | 405 | penum = gs_alloc_struct(mem, gx_image3_enum_t, &st_image3_enum, |
302 | 405 | "gx_begin_image3"); |
303 | 405 | if (penum == 0) |
304 | 0 | return_error(gs_error_VMerror); |
305 | 405 | penum->num_components = |
306 | 405 | gs_color_space_num_components(pim->ColorSpace); |
307 | 405 | gx_image_enum_common_init((gx_image_enum_common_t *) penum, |
308 | 405 | (const gs_data_image_t *)pim, |
309 | 405 | &image3_enum_procs, dev, |
310 | 405 | 1 + penum->num_components, |
311 | 405 | pim->format); |
312 | | /* Initialize pointers now in case we bail out. */ |
313 | 405 | penum->mask_data = 0; |
314 | 405 | penum->pixel_data = 0; |
315 | 405 | if (prect) { |
316 | 0 | long lmw = pim->MaskDict.Width, lmh = pim->MaskDict.Height; |
317 | |
|
318 | 0 | data_rect = *prect; |
319 | 0 | mask_rect.p.x = (int)(data_rect.p.x * lmw / pim->Width); |
320 | 0 | mask_rect.p.y = (int)(data_rect.p.y * lmh / pim->Height); |
321 | 0 | mask_rect.q.x = (int)((data_rect.q.x + pim->Width - 1) * lmw / |
322 | 0 | pim->Width); |
323 | 0 | mask_rect.q.y = (int)((data_rect.q.y + pim->Height - 1) * lmh / |
324 | 0 | pim->Height); |
325 | 405 | } else { |
326 | 405 | mask_rect.p.x = mask_rect.p.y = 0; |
327 | 405 | mask_rect.q.x = pim->MaskDict.Width; |
328 | 405 | mask_rect.q.y = pim->MaskDict.Height; |
329 | 405 | data_rect.p.x = data_rect.p.y = 0; |
330 | 405 | data_rect.q.x = pim->Width; |
331 | 405 | data_rect.q.y = pim->Height; |
332 | 405 | } |
333 | 405 | penum->mask_width = mask_rect.q.x - mask_rect.p.x; |
334 | 405 | penum->mask_height = mask_rect.q.y - mask_rect.p.y; |
335 | 405 | penum->mask_full_height = pim->MaskDict.Height; |
336 | 405 | penum->mask_y = 0; |
337 | 405 | penum->mask_skip = 0; |
338 | 405 | penum->pixel_width = data_rect.q.x - data_rect.p.x; |
339 | 405 | penum->pixel_height = data_rect.q.y - data_rect.p.y; |
340 | 405 | penum->pixel_full_height = pim->Height; |
341 | 405 | penum->pixel_y = 0; |
342 | 405 | penum->mask_info = 0; |
343 | 405 | penum->pixel_info = 0; |
344 | 405 | if (pim->InterleaveType == interleave_chunky) { |
345 | | /* Allocate row buffers for the mask and pixel data. */ |
346 | 0 | penum->pixel_data = |
347 | 0 | gs_alloc_bytes(mem, |
348 | 0 | (penum->pixel_width * pim->BitsPerComponent * |
349 | 0 | penum->num_components + 7) >> 3, |
350 | 0 | "gx_begin_image3(pixel_data)"); |
351 | 0 | penum->mask_data = |
352 | 0 | gs_alloc_bytes(mem, (penum->mask_width + 7) >> 3, |
353 | 0 | "gx_begin_image3(mask_data)"); |
354 | 0 | if (penum->pixel_data == 0 || penum->mask_data == 0) { |
355 | 0 | code = gs_note_error(gs_error_VMerror); |
356 | 0 | goto out1; |
357 | 0 | } |
358 | | /* Because the mask data is 1 BPC, if the width is not a multiple of 8 |
359 | | * then we will not fill the last byte of mask_data completely. This |
360 | | * provokes valgrind when running to pdfwrite, because pdfwrite has to |
361 | | * write the full byte of mask data to the file. It also means (potentially) |
362 | | * that we could run the same input twice and get (slightly) different |
363 | | * PDF files produced. So we set the last byte to zero to ensure the bits |
364 | | * are fully initialised. See Bug #693814 |
365 | | */ |
366 | 0 | penum->mask_data[((penum->mask_width + 7) >> 3) - 1] = 0x00; |
367 | 0 | } |
368 | 405 | penum->InterleaveType = pim->InterleaveType; |
369 | 405 | penum->bpc = pim->BitsPerComponent; |
370 | 405 | penum->memory = mem; |
371 | 405 | mrect.p.x = mrect.p.y = 0; |
372 | 405 | mrect.q.x = pim->MaskDict.Width; |
373 | 405 | mrect.q.y = pim->MaskDict.Height; |
374 | 405 | if (pmat == 0) |
375 | 319 | pmat = &ctm_only(pgs); |
376 | 405 | if ((code = gs_matrix_multiply(&mi_mask, pmat, &mat)) < 0 || |
377 | 405 | (code = gs_bbox_transform(&mrect, &mat, &mrect)) < 0 |
378 | 405 | ) |
379 | 0 | return code; |
380 | | |
381 | | /* Bug 700438: If the rectangle is out of range, bail */ |
382 | 405 | if (mrect.p.x >= (double)INT_MAX || mrect.q.x <= (double)INT_MIN || |
383 | 405 | mrect.p.y >= (double)INT_MAX || mrect.q.y <= (double)INT_MIN || |
384 | 405 | mrect.p.x <= (double)INT_MIN || mrect.q.x >= (double)INT_MAX || |
385 | 405 | mrect.p.y <= (double)INT_MIN || mrect.q.y >= (double)INT_MAX |
386 | 405 | ) { |
387 | 3 | code = gs_note_error(gs_error_rangecheck); |
388 | 3 | goto out1; |
389 | 3 | } |
390 | | |
391 | | /* This code was changed for bug 686843/687411, but in a way that |
392 | | * a) looked wrong, and b) doesn't appear to make a difference. Revert |
393 | | * it to the sane version until we have evidence why not. */ |
394 | 402 | origin.x = (int)floor(mrect.p.x); |
395 | 402 | origin.y = (int)floor(mrect.p.y); |
396 | 402 | code = make_mid(&mdev, dev, (int)ceil(mrect.q.x) - origin.x, |
397 | 402 | (int)ceil(mrect.q.y) - origin.y, mem); |
398 | 402 | if (code < 0) |
399 | 0 | goto out1; |
400 | 402 | penum->mdev = mdev; |
401 | 402 | gs_image_t_init_mask(&i_mask, false); |
402 | 402 | i_mask.adjust = false; |
403 | 402 | { |
404 | 402 | const gx_image_type_t *type1 = i_mask.type; |
405 | | |
406 | 402 | *(gs_data_image_t *)&i_mask = pim->MaskDict; |
407 | 402 | i_mask.type = type1; |
408 | 402 | i_mask.BitsPerComponent = 1; |
409 | 402 | i_mask.image_parent_type = gs_image_type3; |
410 | 402 | } |
411 | 402 | { |
412 | 402 | gx_drawing_color dcolor; |
413 | 402 | gs_matrix m_mat; |
414 | | |
415 | 402 | set_nonclient_dev_color(&dcolor, 1); |
416 | | /* |
417 | | * Adjust the translation for rendering the mask to include a |
418 | | * negative translation by origin.{x,y} in device space. |
419 | | */ |
420 | 402 | m_mat = *pmat; |
421 | 402 | m_mat.tx -= origin.x; |
422 | 402 | m_mat.ty -= origin.y; |
423 | 402 | i_mask.override_in_smask = (dev_proc(dev, dev_spec_op)(dev, gxdso_in_smask, NULL, 0)) > 0; |
424 | | /* |
425 | | * Note that pgs = NULL here, since we don't want to have to |
426 | | * create another gs_gstate with default log_op, etc. |
427 | | */ |
428 | 402 | code = gx_device_begin_typed_image(mdev, NULL, &m_mat, |
429 | 402 | (const gs_image_common_t *)&i_mask, |
430 | 402 | &mask_rect, &dcolor, NULL, mem, |
431 | 402 | &penum->mask_info); |
432 | 402 | if (code < 0) |
433 | 0 | goto out2; |
434 | 402 | } |
435 | 402 | gs_image_t_init(&i_pixel, pim->ColorSpace); |
436 | 402 | { |
437 | 402 | const gx_image_type_t *type1 = i_pixel.type; |
438 | | |
439 | 402 | *(gs_pixel_image_t *)&i_pixel = *(const gs_pixel_image_t *)pim; |
440 | 402 | i_pixel.type = type1; |
441 | 402 | i_pixel.image_parent_type = gs_image_type3; |
442 | 402 | } |
443 | 402 | code = make_mcde(dev, pgs, pmat, (const gs_image_common_t *)&i_pixel, |
444 | 402 | prect, pdcolor, pcpath, mem, &penum->pixel_info, |
445 | 402 | &pcdev, mdev, penum->mask_info, &origin); |
446 | 402 | if (code < 0) |
447 | 0 | goto out3; |
448 | 402 | penum->pcdev = pcdev; |
449 | | /* |
450 | | * Set num_planes, plane_widths, and plane_depths from the values in the |
451 | | * enumerators for the mask and the image data. |
452 | | */ |
453 | 402 | switch (pim->InterleaveType) { |
454 | 0 | case interleave_chunky: |
455 | | /* Add the mask data to the depth of the image data. */ |
456 | 0 | penum->num_planes = 1; |
457 | 0 | penum->plane_widths[0] = pim->Width; |
458 | 0 | penum->plane_depths[0] = |
459 | 0 | penum->pixel_info->plane_depths[0] * |
460 | 0 | (penum->num_components + 1) / penum->num_components; |
461 | 0 | break; |
462 | 0 | case interleave_scan_lines: |
463 | | /* |
464 | | * There is only 1 plane, with dynamically changing width & depth. |
465 | | * Initialize it for the mask data, since that is what will be |
466 | | * read first. |
467 | | */ |
468 | 0 | penum->num_planes = 1; |
469 | 0 | penum->plane_depths[0] = 1; |
470 | 0 | penum->plane_widths[0] = pim->MaskDict.Width; |
471 | 0 | break; |
472 | 402 | case interleave_separate_source: |
473 | | /* Insert the mask data as a separate plane before the image data. */ |
474 | 402 | penum->num_planes = penum->pixel_info->num_planes + 1; |
475 | 402 | penum->plane_widths[0] = pim->MaskDict.Width; |
476 | 402 | penum->plane_depths[0] = 1; |
477 | 402 | memcpy(&penum->plane_widths[1], &penum->pixel_info->plane_widths[0], |
478 | 402 | (penum->num_planes - 1) * sizeof(penum->plane_widths[0])); |
479 | 402 | memcpy(&penum->plane_depths[1], &penum->pixel_info->plane_depths[0], |
480 | 402 | (penum->num_planes - 1) * sizeof(penum->plane_depths[0])); |
481 | 402 | break; |
482 | 402 | } |
483 | 402 | gx_device_retain(mdev, true); /* will free explicitly */ |
484 | 402 | gx_device_retain(pcdev, true); /* ditto */ |
485 | 402 | *pinfo = (gx_image_enum_common_t *) penum; |
486 | 402 | return 0; |
487 | 0 | out3: |
488 | 0 | gx_image_end(penum->mask_info, false); |
489 | 0 | out2: |
490 | 0 | gs_closedevice(mdev); |
491 | 0 | gs_free_object(mem, mdev, "gx_begin_image3(mdev)"); |
492 | 3 | out1: |
493 | 3 | gs_free_object(mem, penum->mask_data, "gx_begin_image3(mask_data)"); |
494 | 3 | gs_free_object(mem, penum->pixel_data, "gx_begin_image3(pixel_data)"); |
495 | 3 | gs_free_object(mem, penum, "gx_begin_image3"); |
496 | 3 | return code; |
497 | 0 | } |
498 | | static bool |
499 | | check_image3_extent(double mask_coeff, double data_coeff) |
500 | 0 | { |
501 | 0 | if (mask_coeff == 0) |
502 | 0 | return data_coeff == 0; |
503 | 0 | if (data_coeff == 0 || (mask_coeff > 0) != (data_coeff > 0)) |
504 | 0 | return false; |
505 | 0 | return true; |
506 | 0 | } |
507 | | |
508 | | /* |
509 | | * Return > 0 if we want more mask now, < 0 if we want more data now, |
510 | | * 0 if we want both. |
511 | | */ |
512 | | static int |
513 | | planes_next(const gx_image3_enum_t *penum) |
514 | 82.9k | { |
515 | | /* |
516 | | * The invariant we need to maintain is that we always have at least as |
517 | | * much mask as pixel data, i.e., mask_y / mask_full_height >= |
518 | | * pixel_y / pixel_full_height, or, to avoid floating point, |
519 | | * mask_y * pixel_full_height >= pixel_y * mask_full_height. |
520 | | * We know this condition is true now; |
521 | | * return a value that indicates how to maintain it. |
522 | | */ |
523 | 82.9k | int mask_h = penum->mask_full_height; |
524 | 82.9k | int pixel_h = penum->pixel_full_height; |
525 | 82.9k | long current = penum->pixel_y * (long)mask_h - |
526 | 82.9k | penum->mask_y * (long)pixel_h; |
527 | | |
528 | | #ifdef DEBUG |
529 | | if (current > 0) |
530 | | lprintf4("planes_next invariant fails: %d/%d > %d/%d\n", |
531 | | penum->pixel_y, penum->pixel_full_height, |
532 | | penum->mask_y, penum->mask_full_height); |
533 | | #endif |
534 | 82.9k | return ((current += mask_h) <= 0 ? -1 : |
535 | 82.9k | current - pixel_h <= 0 ? 0 : 1); |
536 | 82.9k | } |
537 | | |
538 | | /* Process the next piece of an ImageType 3 image. */ |
539 | | static int |
540 | | gx_image3_plane_data(gx_image_enum_common_t * info, |
541 | | const gx_image_plane_t * planes, int height, |
542 | | int *rows_used) |
543 | 82.5k | { |
544 | 82.5k | gx_image3_enum_t *penum = (gx_image3_enum_t *) info; |
545 | 82.5k | int pixel_height = penum->pixel_height; |
546 | 82.5k | int pixel_used = 0; |
547 | 82.5k | int mask_height = penum->mask_height; |
548 | 82.5k | int mask_used = 0; |
549 | 82.5k | int h1 = max(pixel_height - penum->pixel_y, mask_height - penum->mask_y); |
550 | 82.5k | int h = min(height, h1); |
551 | 82.5k | const gx_image_plane_t *pixel_planes; |
552 | 82.5k | gx_image_plane_t pixel_plane, mask_plane; |
553 | 82.5k | int code = 0; |
554 | | |
555 | | /* Initialized rows_used in case we get an error. */ |
556 | 82.5k | *rows_used = 0; |
557 | 82.5k | switch (penum->InterleaveType) { |
558 | 0 | case interleave_chunky: |
559 | 0 | if (h <= 0) |
560 | 0 | return 0; |
561 | 0 | if (h > 1) { |
562 | | /* Do the operation one row at a time. */ |
563 | 0 | int h_orig = h; |
564 | |
|
565 | 0 | mask_plane = planes[0]; |
566 | 0 | do { |
567 | 0 | code = gx_image3_plane_data(info, &mask_plane, 1, |
568 | 0 | rows_used); |
569 | 0 | h -= *rows_used; |
570 | 0 | if (code) |
571 | 0 | break; |
572 | 0 | mask_plane.data += mask_plane.raster; |
573 | 0 | } while (h); |
574 | 0 | *rows_used = h_orig - h; |
575 | 0 | return code; |
576 | 0 | } { |
577 | | /* Pull apart the source data and the mask data. */ |
578 | 0 | int bpc = penum->bpc; |
579 | 0 | int num_components = penum->num_components; |
580 | 0 | int width = penum->pixel_width; |
581 | | |
582 | | /* We do this in the simplest (not fastest) way for now. */ |
583 | 0 | uint bit_x = bpc * (num_components + 1) * planes[0].data_x; |
584 | |
|
585 | 0 | const byte *sptr = planes[0].data + (bit_x >> 3); |
586 | 0 | int sbit = bit_x & 7; |
587 | |
|
588 | 0 | byte *mptr = penum->mask_data; |
589 | 0 | int mbit = 0; |
590 | 0 | byte mbbyte = 0; |
591 | 0 | byte *pptr = penum->pixel_data; |
592 | 0 | int pbit = 0; |
593 | 0 | byte pbbyte = 0; |
594 | 0 | int x; |
595 | |
|
596 | 0 | mask_plane.data = mptr; |
597 | 0 | mask_plane.data_x = 0; |
598 | 0 | mask_plane.raster = 0; /* raster doesn't matter, pacify Valgrind */ |
599 | 0 | pixel_plane.data = pptr; |
600 | 0 | pixel_plane.data_x = 0; |
601 | 0 | pixel_plane.raster = 0; /* raster doesn't matter, pacify Valgrind */ |
602 | 0 | pixel_planes = &pixel_plane; |
603 | 0 | for (x = 0; x < width; ++x) { |
604 | 0 | uint value; |
605 | 0 | int i; |
606 | |
|
607 | 0 | if (sample_load_next12(&value, &sptr, &sbit, bpc) < 0) |
608 | 0 | return_error(gs_error_rangecheck); |
609 | 0 | if (sample_store_next12(value != 0, &mptr, &mbit, 1, &mbbyte) < 0) |
610 | 0 | return_error(gs_error_rangecheck); |
611 | 0 | for (i = 0; i < num_components; ++i) { |
612 | 0 | if (sample_load_next12(&value, &sptr, &sbit, bpc) < 0) |
613 | 0 | return_error(gs_error_rangecheck); |
614 | 0 | if (sample_store_next12(value, &pptr, &pbit, bpc, &pbbyte) < 0) |
615 | 0 | return_error (gs_error_rangecheck); |
616 | 0 | } |
617 | 0 | } |
618 | 0 | sample_store_flush(mptr, mbit, mbbyte); |
619 | 0 | sample_store_flush(pptr, pbit, pbbyte); |
620 | 0 | } |
621 | 0 | break; |
622 | 0 | case interleave_scan_lines: |
623 | 0 | if (planes_next(penum) >= 0) { |
624 | | /* This is mask data. */ |
625 | 0 | mask_plane = planes[0]; |
626 | 0 | pixel_planes = &pixel_plane; |
627 | 0 | pixel_plane.data = 0; |
628 | 0 | } else { |
629 | | /* This is pixel data. */ |
630 | 0 | mask_plane.data = 0; |
631 | 0 | pixel_planes = planes; |
632 | 0 | } |
633 | 0 | break; |
634 | 82.5k | case interleave_separate_source: |
635 | | /* |
636 | | * In order to be able to recover from interruptions, we must |
637 | | * limit separate-source processing to 1 scan line at a time. |
638 | | */ |
639 | 82.5k | if (h > 1) |
640 | 0 | h = 1; |
641 | 82.5k | mask_plane = planes[0]; |
642 | 82.5k | pixel_planes = planes + 1; |
643 | 82.5k | break; |
644 | 0 | default: /* not possible */ |
645 | 0 | return_error(gs_error_rangecheck); |
646 | 82.5k | } |
647 | | /* |
648 | | * Process the mask data first, so it will set up the mask |
649 | | * device for clipping the pixel data. |
650 | | */ |
651 | 82.5k | if (mask_plane.data) { |
652 | | /* |
653 | | * If, on the last call, we processed some mask rows successfully |
654 | | * but processing the pixel rows was interrupted, we set rows_used |
655 | | * to indicate the number of pixel rows processed (since there is |
656 | | * no way to return two rows_used values). If this happened, some |
657 | | * mask rows may get presented again. We must skip over them |
658 | | * rather than processing them again. |
659 | | */ |
660 | 82.5k | int skip = penum->mask_skip; |
661 | | |
662 | 82.5k | if (skip >= h) { |
663 | 0 | penum->mask_skip = skip - (mask_used = h); |
664 | 82.5k | } else { |
665 | 82.5k | int mask_h = h - skip; |
666 | | |
667 | 82.5k | mask_plane.data += skip * mask_plane.raster; |
668 | 82.5k | penum->mask_skip = 0; |
669 | 82.5k | code = gx_image_plane_data_rows(penum->mask_info, &mask_plane, |
670 | 82.5k | mask_h, &mask_used); |
671 | 82.5k | mask_used += skip; |
672 | 82.5k | } |
673 | 82.5k | *rows_used = mask_used; |
674 | 82.5k | penum->mask_y += mask_used; |
675 | 82.5k | if (code < 0) |
676 | 0 | return code; |
677 | 82.5k | } |
678 | 82.5k | if (pixel_planes[0].data) { |
679 | | /* |
680 | | * If necessary, flush any buffered mask data to the mask clipping |
681 | | * device. |
682 | | */ |
683 | 74.9k | gx_image_flush(penum->mask_info); |
684 | 74.9k | code = gx_image_plane_data_rows(penum->pixel_info, pixel_planes, h, |
685 | 74.9k | &pixel_used); |
686 | | /* |
687 | | * There isn't any way to set rows_used if different amounts of |
688 | | * the mask and pixel data were used. Fake it. |
689 | | */ |
690 | 74.9k | *rows_used = pixel_used; |
691 | | /* |
692 | | * Don't return code yet: we must account for the fact that |
693 | | * some mask data may have been processed. |
694 | | */ |
695 | 74.9k | penum->pixel_y += pixel_used; |
696 | 74.9k | if (code < 0) { |
697 | | /* |
698 | | * We must prevent the mask data from being processed again. |
699 | | * We rely on the fact that h > 1 is only possible if the |
700 | | * mask and pixel data have the same Y scaling. |
701 | | */ |
702 | 0 | if (mask_used > pixel_used) { |
703 | 0 | int skip = mask_used - pixel_used; |
704 | |
|
705 | 0 | penum->mask_skip = skip; |
706 | 0 | penum->mask_y -= skip; |
707 | 0 | mask_used = pixel_used; |
708 | 0 | } |
709 | 0 | } |
710 | 74.9k | } |
711 | 82.5k | if_debug5m('b', penum->memory, "[b]image3 h=%d %smask_y=%d %spixel_y=%d\n", |
712 | 82.5k | h, (mask_plane.data ? "+" : ""), penum->mask_y, |
713 | 82.5k | (pixel_planes[0].data ? "+" : ""), penum->pixel_y); |
714 | 82.5k | if (penum->mask_y >= penum->mask_height && |
715 | 82.5k | penum->pixel_y >= penum->pixel_height) |
716 | 137 | return 1; |
717 | 82.3k | if (penum->InterleaveType == interleave_scan_lines) { |
718 | | /* Update the width and depth in the enumerator. */ |
719 | 0 | if (planes_next(penum) >= 0) { /* want mask data next */ |
720 | 0 | penum->plane_widths[0] = penum->mask_width; |
721 | 0 | penum->plane_depths[0] = 1; |
722 | 0 | } else { /* want pixel data next */ |
723 | 0 | penum->plane_widths[0] = penum->pixel_width; |
724 | 0 | penum->plane_depths[0] = penum->pixel_info->plane_depths[0]; |
725 | 0 | } |
726 | 0 | } |
727 | | /* |
728 | | * The mask may be complete (gx_image_plane_data_rows returned 1), |
729 | | * but there may still be pixel rows to go, so don't return 1 here. |
730 | | */ |
731 | 82.3k | return (code < 0 ? code : 0); |
732 | 82.5k | } |
733 | | |
734 | | /* Flush buffered data. */ |
735 | | static int |
736 | | gx_image3_flush(gx_image_enum_common_t * info) |
737 | 0 | { |
738 | 0 | gx_image3_enum_t * const penum = (gx_image3_enum_t *) info; |
739 | 0 | int code = gx_image_flush(penum->mask_info); |
740 | |
|
741 | 0 | if (code >= 0) |
742 | 0 | code = gx_image_flush(penum->pixel_info); |
743 | 0 | return code; |
744 | 0 | } |
745 | | |
746 | | /* Determine which data planes are wanted. */ |
747 | | static bool |
748 | | gx_image3_planes_wanted(const gx_image_enum_common_t * info, byte *wanted) |
749 | 82.9k | { |
750 | 82.9k | const gx_image3_enum_t * const penum = (const gx_image3_enum_t *) info; |
751 | | |
752 | 82.9k | switch (penum->InterleaveType) { |
753 | 0 | case interleave_chunky: /* only 1 plane */ |
754 | 0 | wanted[0] = 0xff; |
755 | 0 | return true; |
756 | 0 | case interleave_scan_lines: /* only 1 plane, but varying width/depth */ |
757 | 0 | wanted[0] = 0xff; |
758 | 0 | return false; |
759 | 82.9k | case interleave_separate_source: { |
760 | | /* |
761 | | * We always want at least as much of the mask to be filled as the |
762 | | * pixel data. next > 0 iff we've processed more data than mask. |
763 | | * Plane 0 is the mask, planes [1 .. num_planes - 1] are pixel data. |
764 | | */ |
765 | 82.9k | int next = planes_next(penum); |
766 | | |
767 | 82.9k | wanted[0] = (next >= 0 ? 0xff : 0); |
768 | 82.9k | memset(wanted + 1, (next <= 0 ? 0xff : 0), info->num_planes - 1); |
769 | | /* |
770 | | * In principle, wanted will always be true for both mask and pixel |
771 | | * data if the full_heights are equal. Unfortunately, even in this |
772 | | * case, processing may be interrupted after a mask row has been |
773 | | * passed to the underlying image processor but before the data row |
774 | | * has been passed, in which case pixel data will be 'wanted', but |
775 | | * not mask data, for the next call. Therefore, we must return |
776 | | * false. |
777 | | */ |
778 | 82.9k | return false |
779 | | /*(next == 0 && |
780 | 0 | penum->mask_full_height == penum->pixel_full_height)*/; |
781 | 0 | } |
782 | 0 | default: /* can't happen */ |
783 | 0 | memset(wanted, 0, info->num_planes); |
784 | 0 | return false; |
785 | 82.9k | } |
786 | 82.9k | } |
787 | | |
788 | | /* Clean up after processing an ImageType 3 image. */ |
789 | | static int |
790 | | gx_image3_end_image(gx_image_enum_common_t * info, bool draw_last) |
791 | 402 | { |
792 | 402 | gx_image3_enum_t *penum = (gx_image3_enum_t *) info; |
793 | 402 | gs_memory_t *mem = penum->memory; |
794 | 402 | gx_device *mdev = penum->mdev; |
795 | 402 | int mcode = gx_image_end(penum->mask_info, draw_last); |
796 | 402 | gx_device *pcdev = penum->pcdev; |
797 | 402 | int pcode = gx_image_end(penum->pixel_info, draw_last); |
798 | 402 | int code1 = gs_closedevice(pcdev); |
799 | 402 | int code2 = gs_closedevice(mdev); |
800 | | |
801 | 402 | gs_free_object(mem, penum->mask_data, |
802 | 402 | "gx_image3_end_image(mask_data)"); |
803 | 402 | gs_free_object(mem, penum->pixel_data, |
804 | 402 | "gx_image3_end_image(pixel_data)"); |
805 | 402 | gs_free_object(mem, pcdev, "gx_image3_end_image(pcdev)"); |
806 | 402 | gs_free_object(mem, mdev, "gx_image3_end_image(mdev)"); |
807 | 402 | gx_image_free_enum(&info); |
808 | 402 | return (pcode < 0 ? pcode : mcode < 0 ? mcode : code1 < 0 ? code1 : code2); |
809 | 402 | } |