File: gdevmem.c

package info (click to toggle)
gs 3.33-7
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,436 kB
  • ctags: 15,511
  • sloc: ansic: 92,150; asm: 684; sh: 486; makefile: 91
file content (247 lines) | stat: -rw-r--r-- 7,725 bytes parent folder | download
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
/* Copyright (C) 1989, 1992, 1994 Aladdin Enterprises.  All rights reserved.
  
  This file is part of GNU Ghostscript.
  
  GNU Ghostscript is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  anyone for the consequences of using it or for whether it serves any
  particular purpose or works at all, unless he says so in writing.  Refer
  to the GNU Ghostscript General Public License for full details.
  
*/

/* gdevmem.c */
/* Generic "memory" (stored bitmap) device */
#include "memory_.h"
#include "gx.h"
#include "gserrors.h"
#include "gsstruct.h"
#include "gxdevice.h"
#include "gxdevmem.h"			/* semi-public definitions */
#include "gdevmem.h"			/* private definitions */

/* Structure descriptor */
public_st_device_memory();

/* GC procedures */
#define mptr ((gx_device_memory *)vptr)
private ENUM_PTRS_BEGIN(device_memory_enum_ptrs) {
	return (*st_device_forward.enum_ptrs)(vptr, sizeof(gx_device_forward), index-2, pep);
	}
	case 0:
	  *pep = (mptr->foreign_bits ? NULL : (void *)mptr->base);
	  break;
	ENUM_STRING_PTR(1, gx_device_memory, palette);
ENUM_PTRS_END
private RELOC_PTRS_BEGIN(device_memory_reloc_ptrs) {
	if ( !mptr->foreign_bits )
	{	byte *base_old = mptr->base;
		long reloc;
		int y;
		RELOC_PTR(gx_device_memory, base);
		reloc = base_old - mptr->base;
		for ( y = 0; y < mptr->height; y++ )
		  mptr->line_ptrs[y] -= reloc;
	}
	RELOC_STRING_PTR(gx_device_memory, palette);
	(*st_device_forward.reloc_ptrs)(vptr, sizeof(gx_device_forward), gcst);
} RELOC_PTRS_END
#undef mptr

/* ------ Generic code ------ */

/* Return the appropriate memory device for a given */
/* number of bits per pixel (0 if none suitable). */
const gx_device_memory *
gdev_mem_device_for_bits(int bits_per_pixel)
{	switch ( bits_per_pixel )
	   {
	case 1: return &mem_mono_device;
	case 2: return &mem_mapped2_color_device;
	case 4: return &mem_mapped4_color_device;
	case 8: return &mem_mapped8_color_device;
	case 16: return &mem_true16_color_device;
	case 24: return &mem_true24_color_device;
	case 32: return &mem_true32_color_device;
	default: return 0;
	   }
}

/* Make a memory device. */
void
gs_make_mem_device(gx_device_memory *dev, const gx_device_memory *mdproto,
  gs_memory_t *mem, int page_device, gx_device *target)
{	*dev = *mdproto;
	dev->memory = mem;
	switch ( page_device )
	  {
	  case -1:
	    dev->std_procs.get_page_device = gx_default_get_page_device;
	    break;
	  case 1:
	    dev->std_procs.get_page_device = gx_page_device_get_page_device;
	    break;
	  }
	mdev->target = target;
	if ( target != 0 )
	  {	/* Forward the color mapping operations to the target. */
		gx_device_forward_color_procs((gx_device_forward *)dev);
	  }
}
/* Make a monobit memory device.  This is never a page device. */
void
gs_make_mem_mono_device(gx_device_memory *dev, gs_memory_t *mem,
  gx_device *target)
{	gs_make_mem_device(dev, &mem_mono_device, mem, -1, target);
}

/* Compute the size of the bitmap storage, */
/* including the space for the scan line pointer table. */
/* Note that scan lines are padded to a multiple of align_bitmap_mod bytes, */
/* and additional padding may be needed if the pointer table */
/* must be aligned to an even larger modulus. */
private ulong
mem_bitmap_bits_size(const gx_device_memory *dev)
{	return round_up((ulong)dev->height * gdev_mem_raster(dev),
			max(align_bitmap_mod, arch_align_ptr_mod));
}
ulong
gdev_mem_bitmap_size(const gx_device_memory *dev)
{        return mem_bitmap_bits_size(dev) +
		(ulong)dev->height * sizeof(byte *);
}

/* Open a memory device, allocating the data area if appropriate, */
/* and create the scan line table. */
private void mem_set_line_ptrs(P3(gx_device_memory *, byte **, byte *));
int
mem_open(gx_device *dev)
{	if ( mdev->bitmap_memory != 0 )
	{	/* Allocate the data now. */
		ulong size = gdev_mem_bitmap_size(mdev);
		if ( (uint)size != size )
			return_error(gs_error_limitcheck);
		mdev->base = gs_alloc_bytes(mdev->bitmap_memory, (uint)size,
					    "mem_open");
		if ( mdev->base == 0 )
			return_error(gs_error_VMerror);
		mdev->foreign_bits = false;
	}
/*
 * Macro for adding an offset to a pointer when setting up the
 * scan line table.  This isn't just pointer arithmetic, because of
 * the segmenting considerations discussed in gdevmem.h.
 */
#define huge_ptr_add(base, offset)\
   ((void *)((byte huge *)(base) + (offset)))
	mem_set_line_ptrs(mdev,
			  huge_ptr_add(mdev->base,
				       mem_bitmap_bits_size(mdev)),
			  mdev->base);
	return 0;
}
/* Set up the scan line pointers of a memory device. */
/* Sets line_ptrs, base, raster; uses width, height, color_info.depth. */
private void
mem_set_line_ptrs(gx_device_memory *devm, byte **line_ptrs, byte *base)
{	byte **pptr = devm->line_ptrs = line_ptrs;
	byte **pend = pptr + devm->height;
	byte *scan_line = devm->base = base;
	uint raster = devm->raster = gdev_mem_raster(devm);

	while ( pptr < pend )
	   {	*pptr++ = scan_line;
		scan_line = huge_ptr_add(scan_line, raster);
	   }
}

/* Return the initial transformation matrix */
void
mem_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
{	pmat->xx = mdev->initial_matrix.xx;
	pmat->xy = mdev->initial_matrix.xy;
	pmat->yx = mdev->initial_matrix.yx;
	pmat->yy = mdev->initial_matrix.yy;
	pmat->tx = mdev->initial_matrix.tx;
	pmat->ty = mdev->initial_matrix.ty;
}

/* Test whether a device is a memory device */
bool
gs_device_is_memory(const gx_device *dev)
{	/* We can't just compare the procs, or even an individual proc, */
	/* because we might be tracing.  Compare the device name, */
	/* and hope for the best. */
	const char *name = dev->dname;
	int i;
	for ( i = 0; i < 6; i++ )
	  if ( name[i] != "image("[i] ) return false;
	return true;
}

/* Close a memory device, freeing the data area if appropriate. */
int
mem_close(gx_device *dev)
{	if ( mdev->bitmap_memory != 0 )
	  gs_free_object(mdev->bitmap_memory, mdev->base, "mem_close");
	return 0;
}

/* Copy a scan line to a client. */
#undef chunk
#define chunk byte
int
mem_get_bits(gx_device *dev, int y, byte *str, byte **actual_data)
{	byte *src;
	if ( y < 0 || y >= dev->height )
		return_error(gs_error_rangecheck);
	src = scan_line_base(mdev, y);
	if ( actual_data == 0 )
		memcpy(str, src, gx_device_raster(dev, 0));
	else
		*actual_data = src;
	return 0;
}

/* Map a r-g-b color to a color index for a mapped color memory device */
/* (2, 4, or 8 bits per pixel.) */
/* This requires searching the palette. */
gx_color_index
mem_mapped_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  gx_color_value b)
{	byte br = gx_color_value_to_byte(r);
	byte bg = gx_color_value_to_byte(g);
	byte bb = gx_color_value_to_byte(b);
	register const byte *pptr = mdev->palette.data;
	int cnt = mdev->palette.size;
	const byte *which = 0;		/* initialized only to pacify gcc */
	int best = 256*3;

	while ( (cnt -= 3) >= 0 )
	   {	register int diff = *pptr - br;
		if ( diff < 0 ) diff = -diff;
		if ( diff < best )	/* quick rejection */
		   {	int dg = pptr[1] - bg;
			if ( dg < 0 ) dg = -dg;
			if ( (diff += dg) < best )	/* quick rejection */
			   {	int db = pptr[2] - bb;
				if ( db < 0 ) db = -db;
				if ( (diff += db) < best )
					which = pptr, best = diff;
			   }
		   }
		pptr += 3;
	   }
	return (gx_color_index)((which - mdev->palette.data) / 3);
}

/* Map a color index to a r-g-b color for a mapped color memory device. */
int
mem_mapped_map_color_rgb(gx_device *dev, gx_color_index color,
  gx_color_value prgb[3])
{	const byte *pptr = mdev->palette.data + (int)color * 3;
	prgb[0] = gx_color_value_from_byte(pptr[0]);
	prgb[1] = gx_color_value_from_byte(pptr[1]);
	prgb[2] = gx_color_value_from_byte(pptr[2]);
	return 0;
}