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