Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
/* Copyright (C) 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
2
 
3
  This software is provided AS-IS with no warranty, either express or
4
  implied.
5
 
6
  This software is distributed under license and may not be copied,
7
  modified or distributed except as expressly authorized under the terms
8
  of the license contained in the file LICENSE in this distribution.
9
 
10
  For more information about licensing, please refer to
11
  http://www.ghostscript.com/licensing/. For information on
12
  commercial licensing, go to http://www.artifex.com/licensing/ or
13
  contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14
  San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15
*/
16
 
17
/* $Id: gximage3.c,v 1.15 2005/03/29 14:26:26 igor Exp $ */
18
/* ImageType 3 image implementation */
19
#include "math_.h"		/* for ceil, floor */
20
#include "memory_.h"
21
#include "gx.h"
22
#include "gserrors.h"
23
#include "gsbitops.h"
24
#include "gscspace.h"
25
#include "gsstruct.h"
26
#include "gxdevice.h"
27
#include "gxdevmem.h"
28
#include "gxclipm.h"
29
#include "gximage3.h"
30
#include "gxistate.h"
31
 
32
/* Forward references */
33
private dev_proc_begin_typed_image(gx_begin_image3);
34
private image_enum_proc_plane_data(gx_image3_plane_data);
35
private image_enum_proc_end_image(gx_image3_end_image);
36
private image_enum_proc_flush(gx_image3_flush);
37
private image_enum_proc_planes_wanted(gx_image3_planes_wanted);
38
 
39
/* GC descriptor */
40
private_st_gs_image3();
41
 
42
/* Define the image type for ImageType 3 images. */
43
const gx_image_type_t gs_image_type_3 = {
44
    &st_gs_image3, gx_begin_image3, gx_data_image_source_size,
45
    gx_image_no_sput, gx_image_no_sget, gx_image_default_release, 3
46
};
47
private const gx_image_enum_procs_t image3_enum_procs = {
48
    gx_image3_plane_data, gx_image3_end_image,
49
    gx_image3_flush, gx_image3_planes_wanted
50
};
51
 
52
/* Initialize an ImageType 3 image. */
53
void
54
gs_image3_t_init(gs_image3_t * pim, const gs_color_space * color_space,
55
		 gs_image3_interleave_type_t interleave_type)
56
{
57
    gs_pixel_image_t_init((gs_pixel_image_t *) pim, color_space);
58
    pim->type = &gs_image_type_3;
59
    pim->InterleaveType = interleave_type;
60
    gs_data_image_t_init(&pim->MaskDict, -1);
61
}
62
 
63
/*
64
 * We implement ImageType 3 images by interposing a mask clipper in
65
 * front of an ordinary ImageType 1 image.  Note that we build up the
66
 * mask row-by-row as we are processing the image.
67
 *
68
 * We export a generalized form of the begin_image procedure for use by
69
 * the PDF and PostScript writers.
70
 */
71
typedef struct gx_image3_enum_s {
72
    gx_image_enum_common;
73
    gx_device *mdev;		/* gx_device_memory in default impl. */
74
    gx_device *pcdev;		/* gx_device_mask_clip in default impl. */
75
    gx_image_enum_common_t *mask_info;
76
    gx_image_enum_common_t *pixel_info;
77
    gs_image3_interleave_type_t InterleaveType;
78
    int num_components;		/* (not counting mask) */
79
    int bpc;			/* BitsPerComponent */
80
    gs_memory_t *memory;
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
private IMAGE3_MAKE_MID_PROC(make_mid_default); /* check prototype */
98
private int
99
make_mid_default(gx_device **pmidev, gx_device *dev, int width, int height,
100
		 gs_memory_t *mem)
101
{
102
    gx_device_memory *midev =
103
	gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
104
			"make_mid_default");
105
    int code;
106
 
107
    if (midev == 0)
108
	return_error(gs_error_VMerror);
109
    gs_make_mem_mono_device(midev, mem, NULL);
110
    midev->bitmap_memory = mem;
111
    midev->width = width;
112
    midev->height = height;
113
    check_device_separable((gx_device *)midev);
114
    gx_device_fill_in_procs((gx_device *)midev);
115
    code = dev_proc(midev, open_device)((gx_device *)midev);
116
    if (code < 0) {
117
	gs_free_object(mem, midev, "make_mid_default");
118
	return code;
119
    }
120
    midev->is_open = true;
121
    dev_proc(midev, fill_rectangle)
122
	((gx_device *)midev, 0, 0, width, height, (gx_color_index)0);
123
    *pmidev = (gx_device *)midev;
124
    return 0;
125
}
126
private IMAGE3_MAKE_MCDE_PROC(make_mcde_default);  /* check prototype */
127
private int
128
make_mcde_default(gx_device *dev, const gs_imager_state *pis,
129
		  const gs_matrix *pmat, const gs_image_common_t *pic,
130
		  const gs_int_rect *prect, const gx_drawing_color *pdcolor,
131
		  const gx_clip_path *pcpath, gs_memory_t *mem,
132
		  gx_image_enum_common_t **pinfo,
133
		  gx_device **pmcdev, gx_device *midev,
134
		  gx_image_enum_common_t *pminfo,
135
		  const gs_int_point *origin)
136
{
137
    gx_device_memory *const mdev = (gx_device_memory *)midev;
138
    gx_device_mask_clip *mcdev =
139
	gs_alloc_struct(mem, gx_device_mask_clip, &st_device_mask_clip,
140
			"make_mcde_default");
141
    gx_strip_bitmap bits;	/* only gx_bitmap */
142
    int code;
143
 
144
    if (mcdev == 0)
145
	return_error(gs_error_VMerror);
146
    bits.data = mdev->base;
147
    bits.raster = mdev->raster;
148
    bits.size.x = mdev->width;
149
    bits.size.y = mdev->height;
150
    bits.id = gx_no_bitmap_id;
151
    code = gx_mask_clip_initialize(mcdev, &gs_mask_clip_device,
152
				   (const gx_bitmap *)&bits, dev,
153
				   origin->x, origin->y, mem);
154
    if (code < 0) {
155
	gs_free_object(mem, mcdev, "make_mcde_default");
156
	return code;
157
    }
158
    mcdev->tiles = bits;
159
    code = dev_proc(mcdev, begin_typed_image)
160
	((gx_device *)mcdev, pis, pmat, pic, prect, pdcolor, pcpath, mem,
161
	 pinfo);
162
    if (code < 0) {
163
	gs_free_object(mem, mcdev, "make_mcde_default");
164
	return code;
165
    }
166
    *pmcdev = (gx_device *)mcdev;
167
    return 0;
168
}
169
private int
170
gx_begin_image3(gx_device * dev,
171
		const gs_imager_state * pis, const gs_matrix * pmat,
172
		const gs_image_common_t * pic, const gs_int_rect * prect,
173
		const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
174
		gs_memory_t * mem, gx_image_enum_common_t ** pinfo)
175
{
176
    return gx_begin_image3_generic(dev, pis, pmat, pic, prect, pdcolor,
177
				   pcpath, mem, make_mid_default,
178
				   make_mcde_default, pinfo);
179
}
180
 
181
/*
182
 * Begin a generic ImageType 3 image, with client handling the creation of
183
 * the mask image and mask clip devices.
184
 */
185
private bool check_image3_extent(floatp mask_coeff, floatp data_coeff);
186
int
187
gx_begin_image3_generic(gx_device * dev,
188
			const gs_imager_state *pis, const gs_matrix *pmat,
189
			const gs_image_common_t *pic, const gs_int_rect *prect,
190
			const gx_drawing_color *pdcolor,
191
			const gx_clip_path *pcpath, gs_memory_t *mem,
192
			image3_make_mid_proc_t make_mid,
193
			image3_make_mcde_proc_t make_mcde,
194
			gx_image_enum_common_t **pinfo)
195
{
196
    const gs_image3_t *pim = (const gs_image3_t *)pic;
197
    gx_image3_enum_t *penum;
198
    gs_int_rect mask_rect, data_rect;
199
    gx_device *mdev = 0;
200
    gx_device *pcdev = 0;
201
    gs_image_t i_pixel, i_mask;
202
    gs_matrix mi_pixel, mi_mask, mat;
203
    gs_rect mrect;
204
    gs_int_point origin;
205
    int code;
206
 
207
    /* Validate the parameters. */
208
    if (pim->Height <= 0 || pim->MaskDict.Height <= 0)
209
	return_error(gs_error_rangecheck);
210
    switch (pim->InterleaveType) {
211
	default:
212
	    return_error(gs_error_rangecheck);
213
	case interleave_chunky:
214
	    if (pim->MaskDict.Width != pim->Width ||
215
		pim->MaskDict.Height != pim->Height ||
216
		pim->MaskDict.BitsPerComponent != pim->BitsPerComponent ||
217
		pim->format != gs_image_format_chunky
218
		)
219
		return_error(gs_error_rangecheck);
220
	    break;
221
	case interleave_scan_lines:
222
	    if (pim->MaskDict.Height % pim->Height != 0 &&
223
		pim->Height % pim->MaskDict.Height != 0
224
		)
225
		return_error(gs_error_rangecheck);
226
	    /* falls through */
227
	case interleave_separate_source:
228
	    if (pim->MaskDict.BitsPerComponent != 1)
229
		return_error(gs_error_rangecheck);
230
    }
231
    if (!check_image3_extent(pim->ImageMatrix.xx,
232
			     pim->MaskDict.ImageMatrix.xx) ||
233
	!check_image3_extent(pim->ImageMatrix.xy,
234
			     pim->MaskDict.ImageMatrix.xy) ||
235
	!check_image3_extent(pim->ImageMatrix.yx,
236
			     pim->MaskDict.ImageMatrix.yx) ||
237
	!check_image3_extent(pim->ImageMatrix.yy,
238
			     pim->MaskDict.ImageMatrix.yy)
239
	)
240
	return_error(gs_error_rangecheck);
241
    if ((code = gs_matrix_invert(&pim->ImageMatrix, &mi_pixel)) < 0 ||
242
	(code = gs_matrix_invert(&pim->MaskDict.ImageMatrix, &mi_mask)) < 0
243
	)
244
	return code;
245
    if (fabs(mi_pixel.tx - mi_mask.tx) >= 0.5 ||
246
	fabs(mi_pixel.ty - mi_mask.ty) >= 0.5
247
	)
248
	return_error(gs_error_rangecheck);
249
    {
250
	gs_point ep, em;
251
 
252
	if ((code = gs_point_transform(pim->Width, pim->Height, &mi_pixel,
253
				       &ep)) < 0 ||
254
	    (code = gs_point_transform(pim->MaskDict.Width,
255
				       pim->MaskDict.Height, &mi_mask,
256
				       &em)) < 0
257
	    )
258
	    return code;
259
	if (fabs(ep.x - em.x) >= 0.5 || fabs(ep.y - em.y) >= 0.5)
260
	    return_error(gs_error_rangecheck);
261
    }
262
    penum = gs_alloc_struct(mem, gx_image3_enum_t, &st_image3_enum,
263
			    "gx_begin_image3");
264
    if (penum == 0)
265
	return_error(gs_error_VMerror);
266
    penum->num_components =
267
	gs_color_space_num_components(pim->ColorSpace);
268
    gx_image_enum_common_init((gx_image_enum_common_t *) penum,
269
			      (const gs_data_image_t *)pim,
270
			      &image3_enum_procs, dev,
271
			      1 + penum->num_components,
272
			      pim->format);
273
    /* Initialize pointers now in case we bail out. */
274
    penum->mask_data = 0;
275
    penum->pixel_data = 0;
276
    if (prect) {
277
	long lmw = pim->MaskDict.Width, lmh = pim->MaskDict.Height;
278
 
279
	data_rect = *prect;
280
	mask_rect.p.x = (int)(data_rect.p.x * lmw / pim->Width);
281
	mask_rect.p.y = (int)(data_rect.p.y * lmh / pim->Height);
282
	mask_rect.q.x = (int)((data_rect.q.x + pim->Width - 1) * lmw /
283
			      pim->Width);
284
	mask_rect.q.y = (int)((data_rect.q.y + pim->Height - 1) * lmh /
285
			      pim->Height);
286
    } else {
287
	mask_rect.p.x = mask_rect.p.y = 0;
288
	mask_rect.q.x = pim->MaskDict.Width;
289
	mask_rect.q.y = pim->MaskDict.Height;
290
	data_rect.p.x = data_rect.p.y = 0;
291
	data_rect.q.x = pim->Width;
292
	data_rect.q.y = pim->Height;
293
    }
294
    penum->mask_width = mask_rect.q.x - mask_rect.p.x;
295
    penum->mask_height = mask_rect.q.y - mask_rect.p.y;
296
    penum->mask_full_height = pim->MaskDict.Height;
297
    penum->mask_y = 0;
298
    penum->mask_skip = 0;
299
    penum->pixel_width = data_rect.q.x - data_rect.p.x;
300
    penum->pixel_height = data_rect.q.y - data_rect.p.y;
301
    penum->pixel_full_height = pim->Height;
302
    penum->pixel_y = 0;
303
    penum->mask_info = 0;
304
    penum->pixel_info = 0;
305
    if (pim->InterleaveType == interleave_chunky) {
306
	/* Allocate row buffers for the mask and pixel data. */
307
	penum->pixel_data =
308
	    gs_alloc_bytes(mem,
309
			   (penum->pixel_width * pim->BitsPerComponent *
310
			    penum->num_components + 7) >> 3,
311
			   "gx_begin_image3(pixel_data)");
312
	penum->mask_data =
313
	    gs_alloc_bytes(mem, (penum->mask_width + 7) >> 3,
314
			   "gx_begin_image3(mask_data)");
315
	if (penum->pixel_data == 0 || penum->mask_data == 0) {
316
	    code = gs_note_error(gs_error_VMerror);
317
	    goto out1;
318
	}
319
    }
320
    penum->InterleaveType = pim->InterleaveType;
321
    penum->bpc = pim->BitsPerComponent;
322
    penum->memory = mem;
323
    mrect.p.x = mrect.p.y = 0;
324
    mrect.q.x = pim->MaskDict.Width;
325
    mrect.q.y = pim->MaskDict.Height;
326
    if (pmat == 0)
327
	pmat = &ctm_only(pis);
328
    if ((code = gs_matrix_multiply(&mi_mask, pmat, &mat)) < 0 ||
329
	(code = gs_bbox_transform(&mrect, &mat, &mrect)) < 0
330
	)
331
	return code;
332
 
333
    origin.x = (mrect.p.x < 0) ? (int)ceil(mrect.p.x) : (int)floor(mrect.p.x);
334
    origin.y = (mrect.p.y < 0) ? (int)ceil(mrect.p.y) : (int)floor(mrect.p.y);
335
    code = make_mid(&mdev, dev, (int)ceil(mrect.q.x) - origin.x,
336
		    (int)ceil(mrect.q.y) - origin.y, mem);
337
    if (code < 0)
338
	goto out1;
339
    penum->mdev = mdev;
340
    gs_image_t_init_mask(&i_mask, false);
341
    i_mask.adjust = false;
342
    {
343
	const gx_image_type_t *type1 = i_mask.type;
344
 
345
	*(gs_data_image_t *)&i_mask = pim->MaskDict;
346
	i_mask.type = type1;
347
	i_mask.BitsPerComponent = 1;
348
    }
349
    {
350
	gx_drawing_color dcolor;
351
	gs_matrix m_mat;
352
 
353
	set_nonclient_dev_color(&dcolor, 1);
354
	/*
355
	 * Adjust the translation for rendering the mask to include a
356
	 * negative translation by origin.{x,y} in device space.
357
	 */
358
	m_mat = *pmat;
359
	m_mat.tx -= origin.x;
360
	m_mat.ty -= origin.y;
361
	/*
362
	 * Note that pis = NULL here, since we don't want to have to
363
	 * create another imager state with default log_op, etc.
364
	 */
365
	code = gx_device_begin_typed_image(mdev, NULL, &m_mat,
366
					   (const gs_image_common_t *)&i_mask,
367
					   &mask_rect, &dcolor, NULL, mem,
368
					   &penum->mask_info);
369
	if (code < 0)
370
	    goto out2;
371
    }
372
    gs_image_t_init(&i_pixel, pim->ColorSpace);
373
    {
374
	const gx_image_type_t *type1 = i_pixel.type;
375
        const bool mask = i_pixel.ImageMask;
376
 
377
        /* On gcc 2.95.4 for Alpha all structures are padded to 8 byte
378
         * boundary but sizeof(bool) == 4. First member of the subclass
379
         * is restored because it is overwritten by padding data.
380
         */
381
	*(gs_pixel_image_t *)&i_pixel = *(const gs_pixel_image_t *)pim;
382
	i_pixel.ImageMask = mask;
383
	i_pixel.type = type1;
384
    }
385
    code = make_mcde(dev, pis, pmat, (const gs_image_common_t *)&i_pixel,
386
		     prect, pdcolor, pcpath, mem, &penum->pixel_info,
387
		     &pcdev, mdev, penum->mask_info, &origin);
388
    if (code < 0)
389
	goto out3;
390
    penum->pcdev = pcdev;
391
    /*
392
     * Set num_planes, plane_widths, and plane_depths from the values in the
393
     * enumerators for the mask and the image data.
394
     */
395
    switch (pim->InterleaveType) {
396
    case interleave_chunky:
397
	/* Add the mask data to the depth of the image data. */
398
	penum->num_planes = 1;
399
	penum->plane_widths[0] = pim->Width;
400
	penum->plane_depths[0] =
401
	    penum->pixel_info->plane_depths[0] *
402
	    (penum->num_components + 1) / penum->num_components;
403
	break;
404
    case interleave_scan_lines:
405
	/*
406
	 * There is only 1 plane, with dynamically changing width & depth.
407
	 * Initialize it for the mask data, since that is what will be
408
	 * read first.
409
	 */
410
	penum->num_planes = 1;
411
	penum->plane_depths[0] = 1;
412
	penum->plane_widths[0] = pim->MaskDict.Width;
413
	break;
414
    case interleave_separate_source:
415
	/* Insert the mask data as a separate plane before the image data. */
416
	penum->num_planes = penum->pixel_info->num_planes + 1;
417
	penum->plane_widths[0] = pim->MaskDict.Width;
418
	penum->plane_depths[0] = 1;
419
	memcpy(&penum->plane_widths[1], &penum->pixel_info->plane_widths[0],
420
	       (penum->num_planes - 1) * sizeof(penum->plane_widths[0]));
421
	memcpy(&penum->plane_depths[1], &penum->pixel_info->plane_depths[0],
422
	       (penum->num_planes - 1) * sizeof(penum->plane_depths[0]));
423
	break;
424
    }
425
    gx_device_retain(mdev, true); /* will free explicitly */
426
    gx_device_retain(pcdev, true); /* ditto */
427
    *pinfo = (gx_image_enum_common_t *) penum;
428
    return 0;
429
  out3:
430
    gx_image_end(penum->mask_info, false);
431
  out2:
432
    gs_closedevice(mdev);
433
    gs_free_object(mem, mdev, "gx_begin_image3(mdev)");
434
  out1:
435
    gs_free_object(mem, penum->mask_data, "gx_begin_image3(mask_data)");
436
    gs_free_object(mem, penum->pixel_data, "gx_begin_image3(pixel_data)");
437
    gs_free_object(mem, penum, "gx_begin_image3");
438
    return code;
439
}
440
private bool
441
check_image3_extent(floatp mask_coeff, floatp data_coeff)
442
{
443
    if (mask_coeff == 0)
444
	return data_coeff == 0;
445
    if (data_coeff == 0 || (mask_coeff > 0) != (data_coeff > 0))
446
	return false;
447
    return true;
448
}
449
 
450
/*
451
 * Return > 0 if we want more mask now, < 0 if we want more data now,
452
 * 0 if we want both.
453
 */
454
private int
455
planes_next(const gx_image3_enum_t *penum)
456
{
457
    /*
458
     * The invariant we need to maintain is that we always have at least as
459
     * much mask as pixel data, i.e., mask_y / mask_full_height >=
460
     * pixel_y / pixel_full_height, or, to avoid floating point,
461
     * mask_y * pixel_full_height >= pixel_y * mask_full_height.
462
     * We know this condition is true now;
463
     * return a value that indicates how to maintain it.
464
     */
465
    int mask_h = penum->mask_full_height;
466
    int pixel_h = penum->pixel_full_height;
467
    long current = penum->pixel_y * (long)mask_h -
468
	penum->mask_y * (long)pixel_h;
469
 
470
#ifdef DEBUG
471
    if (current > 0)
472
	lprintf4("planes_next invariant fails: %d/%d > %d/%d\n",
473
		 penum->pixel_y, penum->pixel_full_height,
474
		 penum->mask_y, penum->mask_full_height);
475
#endif
476
    return ((current += mask_h) <= 0 ? -1 :
477
	    current - pixel_h <= 0 ? 0 : 1);
478
}
479
 
480
/* Process the next piece of an ImageType 3 image. */
481
private int
482
gx_image3_plane_data(gx_image_enum_common_t * info,
483
		     const gx_image_plane_t * planes, int height,
484
		     int *rows_used)
485
{
486
    gx_image3_enum_t *penum = (gx_image3_enum_t *) info;
487
    int pixel_height = penum->pixel_height;
488
    int pixel_used = 0;
489
    int mask_height = penum->mask_height;
490
    int mask_used = 0;
491
    int h1 = max(pixel_height - penum->pixel_y, mask_height - penum->mask_y);
492
    int h = min(height, h1);
493
    const gx_image_plane_t *pixel_planes;
494
    gx_image_plane_t pixel_plane, mask_plane;
495
    int code = 0;
496
 
497
    /* Initialized rows_used in case we get an error. */
498
    *rows_used = 0;
499
    switch (penum->InterleaveType) {
500
	case interleave_chunky:
501
	    if (h <= 0)
502
		return 0;
503
	    if (h > 1) {
504
		/* Do the operation one row at a time. */
505
		int h_orig = h;
506
 
507
		mask_plane = planes[0];
508
		do {
509
		    code = gx_image3_plane_data(info, &mask_plane, 1,
510
						rows_used);
511
		    h -= *rows_used;
512
		    if (code)
513
			break;
514
		    mask_plane.data += mask_plane.raster;
515
		} while (h);
516
		*rows_used = h_orig - h;
517
		return code;
518
	    } {
519
		/* Pull apart the source data and the mask data. */
520
		int bpc = penum->bpc;
521
		int num_components = penum->num_components;
522
		int width = penum->pixel_width;
523
 
524
		/* We do this in the simplest (not fastest) way for now. */
525
		uint bit_x = bpc * (num_components + 1) * planes[0].data_x;
526
 
527
		sample_load_declare_setup(sptr, sbit,
528
					  planes[0].data + (bit_x >> 3),
529
					  bit_x & 7, bpc);
530
		sample_store_declare_setup(mptr, mbit, mbbyte,
531
					   penum->mask_data, 0, 1);
532
		sample_store_declare_setup(pptr, pbit, pbbyte,
533
					   penum->pixel_data, 0, bpc);
534
		int x;
535
 
536
		mask_plane.data = mptr;
537
		mask_plane.data_x = 0;
538
		/* raster doesn't matter */
539
		pixel_plane.data = pptr;
540
		pixel_plane.data_x = 0;
541
		/* raster doesn't matter */
542
		pixel_planes = &pixel_plane;
543
		for (x = 0; x < width; ++x) {
544
		    uint value;
545
		    int i;
546
 
547
		    sample_load_next12(value, sptr, sbit, bpc);
548
		    sample_store_next12(value != 0, mptr, mbit, 1, mbbyte);
549
		    for (i = 0; i < num_components; ++i) {
550
			sample_load_next12(value, sptr, sbit, bpc);
551
			sample_store_next12(value, pptr, pbit, bpc, pbbyte);
552
		    }
553
		}
554
		sample_store_flush(mptr, mbit, 1, mbbyte);
555
		sample_store_flush(pptr, pbit, bpc, pbbyte);
556
	    }
557
	    break;
558
	case interleave_scan_lines:
559
	    if (planes_next(penum) >= 0) {
560
		/* This is mask data. */
561
		mask_plane = planes[0];
562
		pixel_planes = &pixel_plane;
563
		pixel_plane.data = 0;
564
	    } else {
565
		/* This is pixel data. */
566
		mask_plane.data = 0;
567
		pixel_planes = planes;
568
	    }
569
	    break;
570
	case interleave_separate_source:
571
	    /*
572
	     * In order to be able to recover from interruptions, we must
573
	     * limit separate-source processing to 1 scan line at a time.
574
	     */
575
	    if (h > 1)
576
		h = 1;
577
	    mask_plane = planes[0];
578
	    pixel_planes = planes + 1;
579
	    break;
580
	default:		/* not possible */
581
	    return_error(gs_error_rangecheck);
582
    }
583
    /*
584
     * Process the mask data first, so it will set up the mask
585
     * device for clipping the pixel data.
586
     */
587
    if (mask_plane.data) {
588
	/*
589
	 * If, on the last call, we processed some mask rows successfully
590
	 * but processing the pixel rows was interrupted, we set rows_used
591
	 * to indicate the number of pixel rows processed (since there is
592
	 * no way to return two rows_used values).  If this happened, some
593
	 * mask rows may get presented again.  We must skip over them
594
	 * rather than processing them again.
595
	 */
596
	int skip = penum->mask_skip;
597
 
598
	if (skip >= h) {
599
	    penum->mask_skip = skip - (mask_used = h);
600
	} else {
601
	    int mask_h = h - skip;
602
 
603
	    mask_plane.data += skip * mask_plane.raster;
604
	    penum->mask_skip = 0;
605
	    code = gx_image_plane_data_rows(penum->mask_info, &mask_plane,
606
					    mask_h, &mask_used);
607
	    mask_used += skip;
608
	}
609
	*rows_used = mask_used;
610
	penum->mask_y += mask_used;
611
	if (code < 0)
612
	    return code;
613
    }
614
    if (pixel_planes[0].data) {
615
	/*
616
	 * If necessary, flush any buffered mask data to the mask clipping
617
	 * device.
618
	 */
619
	gx_image_flush(penum->mask_info);
620
	code = gx_image_plane_data_rows(penum->pixel_info, pixel_planes, h,
621
					&pixel_used);
622
	/*
623
	 * There isn't any way to set rows_used if different amounts of
624
	 * the mask and pixel data were used.  Fake it.
625
	 */
626
	*rows_used = pixel_used;
627
	/*
628
	 * Don't return code yet: we must account for the fact that
629
	 * some mask data may have been processed.
630
	 */
631
	penum->pixel_y += pixel_used;
632
	if (code < 0) {
633
	    /*
634
	     * We must prevent the mask data from being processed again.
635
	     * We rely on the fact that h > 1 is only possible if the
636
	     * mask and pixel data have the same Y scaling.
637
	     */
638
	    if (mask_used > pixel_used) {
639
		int skip = mask_used - pixel_used;
640
 
641
		penum->mask_skip = skip;
642
		penum->mask_y -= skip;
643
		mask_used = pixel_used;
644
	    }
645
	}
646
    }
647
    if_debug5('b', "[b]image3 h=%d %smask_y=%d %spixel_y=%d\n",
648
	      h, (mask_plane.data ? "+" : ""), penum->mask_y,
649
	      (pixel_planes[0].data ? "+" : ""), penum->pixel_y);
650
    if (penum->mask_y >= penum->mask_height &&
651
	penum->pixel_y >= penum->pixel_height)
652
	return 1;
653
    if (penum->InterleaveType == interleave_scan_lines) {
654
	/* Update the width and depth in the enumerator. */
655
	if (planes_next(penum) >= 0) {  /* want mask data next */
656
	    penum->plane_widths[0] = penum->mask_width;
657
	    penum->plane_depths[0] = 1;
658
	} else {		/* want pixel data next */
659
	    penum->plane_widths[0] = penum->pixel_width;
660
	    penum->plane_depths[0] = penum->pixel_info->plane_depths[0];
661
	}
662
    }
663
    /*
664
     * The mask may be complete (gx_image_plane_data_rows returned 1),
665
     * but there may still be pixel rows to go, so don't return 1 here.
666
     */
667
    return (code < 0 ? code : 0);
668
}
669
 
670
/* Flush buffered data. */
671
private int
672
gx_image3_flush(gx_image_enum_common_t * info)
673
{
674
    gx_image3_enum_t * const penum = (gx_image3_enum_t *) info;
675
    int code = gx_image_flush(penum->mask_info);
676
 
677
    if (code >= 0)
678
	code = gx_image_flush(penum->pixel_info);
679
    return code;
680
}
681
 
682
/* Determine which data planes are wanted. */
683
private bool
684
gx_image3_planes_wanted(const gx_image_enum_common_t * info, byte *wanted)
685
{
686
    const gx_image3_enum_t * const penum = (const gx_image3_enum_t *) info;
687
 
688
    switch (penum->InterleaveType) {
689
    case interleave_chunky:	/* only 1 plane */
690
	wanted[0] = 0xff;
691
	return true;
692
    case interleave_scan_lines:	/* only 1 plane, but varying width/depth */
693
	wanted[0] = 0xff;
694
	return false;
695
    case interleave_separate_source: {
696
	/*
697
	 * We always want at least as much of the mask to be filled as the
698
	 * pixel data.  next > 0 iff we've processed more data than mask.
699
	 * Plane 0 is the mask, planes [1 .. num_planes - 1] are pixel data.
700
	 */
701
	int next = planes_next(penum);
702
 
703
	wanted[0] = (next >= 0 ? 0xff : 0);
704
	memset(wanted + 1, (next <= 0 ? 0xff : 0), info->num_planes - 1);
705
	/*
706
	 * In principle, wanted will always be true for both mask and pixel
707
	 * data if the full_heights are equal.  Unfortunately, even in this
708
	 * case, processing may be interrupted after a mask row has been
709
	 * passed to the underlying image processor but before the data row
710
	 * has been passed, in which case pixel data will be 'wanted', but
711
	 * not mask data, for the next call.  Therefore, we must return
712
	 * false.
713
	 */
714
	return false
715
	    /*(next == 0 &&
716
	      penum->mask_full_height == penum->pixel_full_height)*/;
717
    }
718
    default:			/* can't happen */
719
	memset(wanted, 0, info->num_planes);
720
	return false;
721
    }
722
}
723
 
724
/* Clean up after processing an ImageType 3 image. */
725
private int
726
gx_image3_end_image(gx_image_enum_common_t * info, bool draw_last)
727
{
728
    gx_image3_enum_t *penum = (gx_image3_enum_t *) info;
729
    gs_memory_t *mem = penum->memory;
730
    gx_device *mdev = penum->mdev;
731
    int mcode = gx_image_end(penum->mask_info, draw_last);
732
    gx_device *pcdev = penum->pcdev;
733
    int pcode = gx_image_end(penum->pixel_info, draw_last);
734
    int code1 = gs_closedevice(pcdev);
735
    int code2 = gs_closedevice(mdev);
736
 
737
    gs_free_object(mem, penum->mask_data,
738
		   "gx_image3_end_image(mask_data)");
739
    gs_free_object(mem, penum->pixel_data,
740
		   "gx_image3_end_image(pixel_data)");
741
    gs_free_object(mem, pcdev, "gx_image3_end_image(pcdev)");
742
    gs_free_object(mem, mdev, "gx_image3_end_image(mdev)");
743
    gs_free_object(mem, penum, "gx_image3_end_image");
744
    return (pcode < 0 ? pcode : mcode < 0 ? mcode : code1 < 0 ? code1 : code2);
745
}