summaryrefslogtreecommitdiff
path: root/gs/src/gsimage.c
blob: 0be1d4e9257b58329145485881872472cdd327c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* Copyright (C) 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
 * This software is licensed to a single customer by Artifex Software Inc.
 * under the terms of a specific OEM agreement.
 */

/*$RCSfile$ $Revision$ */
/* Image setup procedures for Ghostscript library */
#include "memory_.h"
#include "gx.h"
#include "gserrors.h"
#include "gsstruct.h"
#include "gscspace.h"
#include "gsmatrix.h"		/* for gsiparam.h */
#include "gsimage.h"
#include "gxarith.h"		/* for igcd */
#include "gxdevice.h"
#include "gxiparam.h"
#include "gxpath.h"		/* for gx_effective_clip_path */
#include "gzstate.h"

/* Define the enumeration state for this interface layer. */
/*typedef struct gs_image_enum_s gs_image_enum; *//* in gsimage.h */
typedef struct image_enum_plane_s {
    /* Change dynamically */
    uint pos;			/* byte position within the scan line */
    gs_const_string source;	/* source data, [0 .. num_planes - 1] */
    gs_string row;		/* row buffers, [0 .. num_planes - 1] */
} image_enum_plane_t;
struct gs_image_enum_s {
    /* The following are set at initialization time. */
    gs_memory_t *memory;
    gx_device *dev;		/* if 0, just skip over the data */
    gx_image_enum_common_t *info;	/* driver bookkeeping structure */
    int num_planes;
    int height;
    bool wanted_varies;
    /* The following are updated dynamically. */
    int plane_index;		/* index of next plane of data, */
				/* only needed for gs_image_next */
    int y;
    bool error;
    byte wanted[gs_image_max_planes];
    image_enum_plane_t planes[gs_image_max_planes];
};

gs_private_st_composite(st_gs_image_enum, gs_image_enum, "gs_image_enum",
			gs_image_enum_enum_ptrs, gs_image_enum_reloc_ptrs);
#define gs_image_enum_num_ptrs 2

/* GC procedures */
private 
ENUM_PTRS_WITH(gs_image_enum_enum_ptrs, gs_image_enum *eptr)
{
    /* Enumerate the data planes. */
    index -= gs_image_enum_num_ptrs;
    if (index < eptr->plane_index)
	ENUM_RETURN_STRING_PTR(gs_image_enum, planes[index].source);
    index -= eptr->plane_index;
    if (index < eptr->num_planes)
	ENUM_RETURN_STRING_PTR(gs_image_enum, planes[index].row);
    return 0;
}
ENUM_PTR(0, gs_image_enum, dev);
ENUM_PTR(1, gs_image_enum, info);
ENUM_PTRS_END
private RELOC_PTRS_WITH(gs_image_enum_reloc_ptrs, gs_image_enum *eptr)
{
    int i;

    RELOC_PTR(gs_image_enum, dev);
    RELOC_PTR(gs_image_enum, info);
    for (i = 0; i < eptr->plane_index; i++)
	RELOC_CONST_STRING_PTR(gs_image_enum, planes[i].source);
    for (i = 0; i < eptr->num_planes; i++)
	RELOC_STRING_PTR(gs_image_enum, planes[i].row);
}
RELOC_PTRS_END

/* Create an image enumerator given image parameters and a graphics state. */
int
gs_image_begin_typed(const gs_image_common_t * pic, gs_state * pgs,
		     bool uses_color, gx_image_enum_common_t ** ppie)
{
    gx_device *dev = gs_currentdevice(pgs);
    gx_clip_path *pcpath;
    int code = gx_effective_clip_path(pgs, &pcpath);

    if (code < 0)
	return code;
    if (uses_color)
	gx_set_dev_color(pgs);
    return gx_device_begin_typed_image(dev, (const gs_imager_state *)pgs,
		NULL, pic, NULL, pgs->dev_color, pcpath, pgs->memory, ppie);
}

/* Allocate an image enumerator. */
private void
image_enum_init(gs_image_enum * penum)
{
    /* Clean pointers for GC. */
    penum->info = 0;
    penum->dev = 0;
    penum->plane_index = 0;
    penum->num_planes = 0;
}
gs_image_enum *
gs_image_enum_alloc(gs_memory_t * mem, client_name_t cname)
{
    gs_image_enum *penum =
	gs_alloc_struct(mem, gs_image_enum, &st_gs_image_enum, cname);

    if (penum != 0) {
	penum->memory = mem;
	image_enum_init(penum);
    }
    return penum;
}

/* Start processing an ImageType 1 image. */
int
gs_image_init(gs_image_enum * penum, const gs_image_t * pim, bool multi,
	      gs_state * pgs)
{
    gs_image_t image;
    gx_image_enum_common_t *pie;
    int code;

    image = *pim;
    if (image.ImageMask) {
	image.ColorSpace = NULL;
	if (pgs->in_cachedevice <= 1)
	    image.adjust = false;
    } else {
	if (pgs->in_cachedevice)
	    return_error(gs_error_undefined);
	if (image.ColorSpace == NULL)
	    image.ColorSpace =
		gs_cspace_DeviceGray((const gs_imager_state *)pgs);
    }
    code = gs_image_begin_typed((const gs_image_common_t *)&image, pgs,
				image.ImageMask | image.CombineWithColor,
				&pie);
    if (code < 0)
	return code;
    return gs_image_enum_init(penum, pie, (const gs_data_image_t *)&image,
			      pgs);
}
/* Start processing a general image. */
private void
begin_planes(gs_image_enum *penum)
{
    /*
     * Initialize plane_index and (if appropriate) wanted and
     * wanted_varies at the beginning of a group of planes.
     */
    int px = 0;

    if (penum->wanted_varies) {
	penum->wanted_varies =
	    !gx_image_planes_wanted(penum->info, penum->wanted);
    }
    while (!penum->wanted[px])
	++px;
    penum->plane_index = px;
}
int
gs_image_enum_init(gs_image_enum * penum, gx_image_enum_common_t * pie,
		   const gs_data_image_t * pim, gs_state *pgs)
{
    return gs_image_common_init(penum, pie, pim, pgs->memory,
				(pgs->in_charpath ? NULL :
				 gs_currentdevice_inline(pgs)));
}
int
gs_image_common_init(gs_image_enum * penum, gx_image_enum_common_t * pie,
	    const gs_data_image_t * pim, gs_memory_t * mem, gx_device * dev)
{
    int i;

    if (pim->Width == 0 || pim->Height == 0) {
	gx_image_end(pie, false);
	return 1;
    }
    image_enum_init(penum);
    penum->memory = mem;
    penum->dev = dev;
    penum->info = pie;
    penum->num_planes = pie->num_planes;
    penum->height = pim->Height;
    for (i = 0; i < pie->num_planes; ++i) {
	penum->planes[i].pos = 0;
	penum->planes[i].source.size = 0;	/* for gs_image_next_planes */
	penum->planes[i].row.data = 0; /* for GC */
	penum->planes[i].row.size = 0; /* ditto */
    }
    /* Initialize the dynamic part of the state. */
    penum->y = 0;
    penum->error = false;
    penum->wanted_varies = true;
    begin_planes(penum);
    return 0;
}

/*
 * Return the number of bytes of data per row for a given plane.
 */
inline uint
gs_image_bytes_per_plane_row(const gs_image_enum * penum, int plane)
{
    const gx_image_enum_common_t *pie = penum->info;

    return (pie->plane_widths[plane] * pie->plane_depths[plane] + 7) >> 3;
}

/* Return the set of planes wanted. */
const byte *
gs_image_planes_wanted(const gs_image_enum *penum)
{
    return penum->wanted;
}

/* Free the row buffers when cleaning up. */
private void
free_row_buffers(gs_image_enum *penum, int num_planes, client_name_t cname)
{
    int i;

    for (i = num_planes - 1; i >= 0; --i) {
	gs_free_string(penum->memory, penum->planes[i].row.data,
		       penum->planes[i].row.size, cname);
	penum->planes[i].row.data = 0;
	penum->planes[i].row.size = 0;
    }
}

/* Process the next piece of an image. */
private int
copy_planes(gx_device * dev, gs_image_enum * penum,
	    const gx_image_plane_t *planes, int h, int *rows_used)
{
    int code;

    if (penum->dev == 0) {
	if (penum->y + h < penum->height)
	    *rows_used = h, code = 0;
	else
	    *rows_used = penum->height - penum->y, code = 1;
    } else {
	code = gx_image_plane_data_rows(penum->info, planes, h, rows_used);
	penum->error = code < 0;
    }
    return code;
}
int
gs_image_next(gs_image_enum * penum, const byte * dbytes, uint dsize,
	      uint * pused)
{
    int px = penum->plane_index;
    int num_planes = penum->num_planes;
    int i, code;
    uint used[gs_image_max_planes];
    gs_const_string plane_data[gs_image_max_planes];

    /*
     * Handle the following differences between gs_image_next and
     * the device image_data procedure:
     *
     *      - image_data requires an array of planes; gs_image_next
     *      expects planes in successive calls.
     *
     * This function is now deprecated -- gs_image_next_planes does
     * the real work of buffering.
     */
    for (i = 0; i < num_planes; i++)
	plane_data[i].size = 0;
    plane_data[px].data = dbytes;
    plane_data[px].size = dsize;
    penum->error = false;
    code = gs_image_next_planes(penum, plane_data, used);
    if (code >= 0) {
	/* Advance px to the next plane that needs data. */
	do {
	    if (++px == num_planes)
		px = 0;
	} while (!penum->wanted[px]);
	penum->plane_index = px;
    }
    *pused = used[px];
    return code;
}

int
gs_image_next_planes(gs_image_enum * penum, gs_const_string *plane_data,
		     uint * used)
{
    gx_image_plane_t planes[gs_image_max_planes];
    gx_device *dev = penum->dev;
    int num_planes = penum->num_planes;
    int i;
    int code = 0;
    bool need_planes = false;

    /*
     * Handle the following differences between gs_image_next_planes and
     * the device image_data procedure:
     *
     *      - image_data requires an array of planes; not all of the
     *      planes may be available on any given call.
     *
     *      - image_data requires that each call pass entire rows;
     *      gs_image_next_planes allows arbitrary amounts of data.
     */
    for (i = 0; i < num_planes; ++i) {
        used[i] = 0;
	if (!penum->wanted[i])
	    planes[i].data = 0;
	else {
	    if (plane_data[i].size != 0) {
		penum->planes[i].source.size = plane_data[i].size;
		penum->planes[i].source.data = plane_data[i].data;
		planes[i].data_x = 0;
		planes[i].raster = gs_image_bytes_per_plane_row(penum, i);
	    }
	    if (penum->planes[i].source.size == 0)
		need_planes = true;	/* can't proceed until we have all planes */
	}
    }
    if (need_planes) 
	return 0;	/* exit for more data, nothing used */
    while (!code) {
	int direct = max_int;
	bool filled = true;
	bool empty = false;

	/* see if we can do some rows without buffering (i.e., direct) */
	for (i = 0; i < num_planes; ++i) {
	    /* need to reset planes[].raster if wanted_varies */
	    if (penum->wanted_varies)
		planes[i].raster = gs_image_bytes_per_plane_row(penum, i);
	    if (penum->planes[i].pos == 0)  /* nothing buffered for this row */
		direct = min(direct,
			     penum->planes[i].source.size / planes[i].raster);
	    else
		direct = 0;
	}
	if (direct) {
	    /*
	     * Pass (a) row(s) directly from the source.  If wanted_varies,
	     * we can only safely pass 1 scan line at a time.
	     */
	    if (penum->wanted_varies)
		direct = 1;
	    for (i = 0; i < num_planes; ++i)
		if (penum->wanted[i])
		    planes[i].data = penum->planes[i].source.data;
	    code = copy_planes(dev, penum, planes, direct, &direct);
	    if (code < 0)
		return code;	/* don't update */
	    for (i = 0; i < num_planes; ++i)
		if (planes[i].data) {
		    uint direct_used = direct * planes[i].raster;

		    used[i] += direct_used;
		    penum->planes[i].source.data += direct_used;
		    penum->planes[i].source.size -= direct_used;
		    if (penum->planes[i].source.size == 0)
		        empty = true;
		}
	    penum->y += direct;
	    if (code < 0)
		break;
	} else {		/* Buffer a partial row. */
	    uint count[gs_image_max_planes];

	    for (i = 0; i < num_planes; ++i) {
		if (penum->planes[i].source.size != 0) {
		    uint raster = planes[i].raster;
		    uint old_size = penum->planes[i].row.size;

		    /* Make sure the row buffers are allocated. */
		    if (raster > old_size) {
			byte *old_data = penum->planes[i].row.data;
			byte *row =
			    (old_data == 0 ?
			     gs_alloc_string(penum->memory, raster,
					     "gs_image_next(row)") :
			     gs_resize_string(penum->memory, old_data,
					      old_size, raster,
					      "gs_image_next(row)"));

			if (row == 0) {
			    code = gs_note_error(gs_error_VMerror);
			    free_row_buffers(penum, i, "gs_image_next(row)");
			    break;
			}
			penum->planes[i].row.data = row;
			penum->planes[i].row.size = raster;
		    }
		    if ((count[i] = min(raster - penum->planes[i].pos,
				        penum->planes[i].source.size)) > 0) {
			memcpy(penum->planes[i].row.data + penum->planes[i].pos,
			   penum->planes[i].source.data, count[i]);
			if ((penum->planes[i].pos += count[i]) < raster)
			    filled = false;
		    }
		}
	    }
	    if (code < 0)
		break;
	    if (filled) {
		for (i = 0; i < num_planes; ++i)
		    if (penum->wanted[i])
			planes[i].data = penum->planes[i].row.data;
		code = copy_planes(dev, penum, planes, 1, &direct);
		if (code < 0) {
		    /* Undo the incrementing of pos. */
		    for (i = 0; i < num_planes; ++i)
			if (penum->wanted[i])
			    penum->planes[i].pos -= count[i];
		    break;
		}
		for (i = 0; i < num_planes; ++i) {
		    penum->planes[i].pos = 0;
		}
		penum->y++;
	    }
	    for (i = 0; i < num_planes; ++i) {
		if (penum->wanted[i]) {
		    used[i] += count[i];
		    penum->planes[i].source.data += count[i];
		    if ((penum->planes[i].source.size -= count[i]) == 0)
			empty = true;
		}
	    }
	}
	if (filled & !code)
	    begin_planes(penum);
	if (empty)
	    break;
    }
    return code;
}

/* Clean up after processing an image. */
void
gs_image_cleanup(gs_image_enum * penum)
{
    free_row_buffers(penum, penum->num_planes, "gs_image_cleanup(row)");
    if (penum->dev != 0)
	gx_image_end(penum->info, !penum->error);
    /* Don't free the local enumerator -- the client does that. */
}