]> git.ipfire.org Git - thirdparty/cups.git/blob - pstoraster/gdevabuf.c
Import cups.org releases
[thirdparty/cups.git] / pstoraster / gdevabuf.c
1 /* Copyright (C) 1994, 1995, 1996 Aladdin Enterprises. All rights reserved.
2
3 This file is part of GNU Ghostscript.
4
5 GNU Ghostscript is distributed in the hope that it will be useful, but
6 WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to
7 anyone for the consequences of using it or for whether it serves any
8 particular purpose or works at all, unless he says so in writing. Refer to
9 the GNU General Public License for full details.
10
11 Everyone is granted permission to copy, modify and redistribute GNU
12 Ghostscript, but only under the conditions described in the GNU General
13 Public License. A copy of this license is supposed to have been given to
14 you along with GNU Ghostscript so you can know your rights and
15 responsibilities. It should be in a file named COPYING. Among other
16 things, the copyright notice and this notice must be preserved on all
17 copies.
18
19 Aladdin Enterprises is not affiliated with the Free Software Foundation or
20 the GNU Project. GNU Ghostscript, as distributed by Aladdin Enterprises,
21 does not depend on any other GNU software.
22 */
23
24 /* gdevabuf.c */
25 /* Alpha-buffering memory devices */
26 #include "memory_.h"
27 #include "gx.h"
28 #include "gserrors.h"
29 #include "gxdevice.h"
30 #include "gxdevmem.h" /* semi-public definitions */
31 #include "gdevmem.h" /* private definitions */
32
33 /* ================ Alpha devices ================ */
34
35 /*
36 * These devices store 2 or 4 bits of alpha. They are a hybrid of a
37 * monobit device (for color mapping) and a 2- or 4-bit device (for painting).
38 * Currently, we only use them for character rasterizing, but they might be
39 * useful for other things someday.
40 */
41
42 /* We can't initialize the device descriptor statically very well, */
43 /* so we patch up the image2 or image4 descriptor. */
44 private dev_proc_map_rgb_color(mem_alpha_map_rgb_color);
45 private dev_proc_map_color_rgb(mem_alpha_map_color_rgb);
46 private dev_proc_map_rgb_alpha_color(mem_alpha_map_rgb_alpha_color);
47 private dev_proc_get_alpha_bits(mem_alpha_get_alpha_bits);
48 private dev_proc_copy_alpha(mem_alpha_copy_alpha);
49
50 void
51 gs_make_mem_alpha_device(gx_device_memory *adev, gs_memory_t *mem,
52 gx_device *target, int alpha_bits)
53 { gs_make_mem_device(adev, gdev_mem_device_for_bits(alpha_bits),
54 mem, 0, target);
55 /* This is a black-and-white device ... */
56 adev->color_info = gdev_mem_device_for_bits(1)->color_info;
57 /* ... but it has multiple bits per pixel ... */
58 adev->color_info.depth = alpha_bits;
59 /* ... and different color mapping. */
60 set_dev_proc(adev, map_rgb_color, mem_alpha_map_rgb_color);
61 set_dev_proc(adev, map_color_rgb, mem_alpha_map_color_rgb);
62 set_dev_proc(adev, map_rgb_alpha_color, mem_alpha_map_rgb_alpha_color);
63 set_dev_proc(adev, get_alpha_bits, mem_alpha_get_alpha_bits);
64 set_dev_proc(adev, copy_alpha, mem_alpha_copy_alpha);
65 }
66
67 /* Reimplement color mapping. */
68 private gx_color_index
69 mem_alpha_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
70 gx_color_value b)
71 { gx_color_index color = gx_forward_map_rgb_color(dev, r, g, b);
72 return (color == 0 || color == gx_no_color_index ? color :
73 (gx_color_index)((1 << mdev->log2_alpha_bits) - 1));
74 }
75 private int
76 mem_alpha_map_color_rgb(gx_device *dev, gx_color_index color,
77 gx_color_value prgb[3])
78 { return
79 gx_forward_map_color_rgb(dev,
80 (color == 0 ? color : (gx_color_index)1),
81 prgb);
82 }
83 private gx_color_index
84 mem_alpha_map_rgb_alpha_color(gx_device *dev, gx_color_value r,
85 gx_color_value g, gx_color_value b, gx_color_value alpha)
86 { gx_color_index color = gx_forward_map_rgb_color(dev, r, g, b);
87 return (color == 0 || color == gx_no_color_index ? color :
88 (gx_color_index)(alpha >> (gx_color_value_bits -
89 mdev->log2_alpha_bits)));
90 }
91 private int
92 mem_alpha_get_alpha_bits(gx_device *dev, graphics_object_type type)
93 { return 1 << mdev->log2_alpha_bits;
94 }
95 /* Implement alpha copying. */
96 private int
97 mem_alpha_copy_alpha(gx_device *dev, const byte *data, int data_x,
98 int raster, gx_bitmap_id id, int x, int y, int width, int height,
99 gx_color_index color, int depth)
100 { /* Just use copy_color. */
101 return (color == 0 ?
102 (*dev_proc(dev, fill_rectangle))(dev, x, y, width, height,
103 color) :
104 (*dev_proc(dev, copy_color))(dev, data, data_x, raster, id,
105 x, y, width, height));
106 }
107
108 /* ================ Alpha-buffer device ================ */
109
110 /*
111 * This device converts graphics sampled at a higher resolution to
112 * alpha values at a lower resolution. It does this by accumulating
113 * the bits of a band and then converting the band to alphas.
114 * In order to make this work, the client of the device must promise
115 * only to visit each band at most once, except possibly for a single
116 * scan line overlapping the adjacent band, and must promise only to write
117 * a single color into the output. In particular, this works
118 * within a single call on gx_fill_path (if the fill loop is constrained
119 * to process bands of limited height on each pass) or a single masked image
120 * scanned in Y order, but not across such calls and not for other
121 * kinds of painting operations.
122 *
123 * We implement this device as a subclass of a monobit memory device.
124 * (We put its state in the definition of gx_device_memory just because
125 * actual subclassing introduces a lot of needless boilerplate.)
126 * We only allocate enough bits for one band. The height of the band
127 * must be a multiple of the Y scale factor; the minimum height
128 * of the band is twice the Y scale factor.
129 *
130 * The bits in storage are actually a sliding window on the true
131 * oversampled image. To avoid having to copy the bits around when we
132 * move the window, we adjust the mapping between the client's Y values
133 * and our own, as follows:
134 * Client Stored
135 * ------ ------
136 * y0..y0+m-1 n-m..n-1
137 * y0+m..y0+n-1 0..n-m-1
138 * where n and m are multiples of the Y scale factor and 0 <= m <= n <=
139 * the height of the band. (In the device structure, m is called
140 * mapped_start and n is called mapped_height.) This allows us to slide
141 * the window incrementally in either direction without copying any bits.
142 */
143
144 /* Procedures */
145 private dev_proc_close_device(mem_abuf_close);
146 private dev_proc_copy_mono(mem_abuf_copy_mono);
147 private dev_proc_fill_rectangle(mem_abuf_fill_rectangle);
148
149 /* The device descriptor. */
150 private const gx_device_memory far_data mem_alpha_buffer_device =
151 mem_device("image(alpha buffer)", 0, 1,
152 gx_forward_map_rgb_color, gx_forward_map_color_rgb,
153 mem_abuf_copy_mono, gx_default_copy_color, mem_abuf_fill_rectangle,
154 gx_no_strip_copy_rop);
155
156 /* Make an alpha-buffer memory device. */
157 /* We use abuf instead of alpha_buffer because */
158 /* gcc under VMS only retains 23 characters of procedure names. */
159 void
160 gs_make_mem_abuf_device(gx_device_memory *adev, gs_memory_t *mem,
161 gx_device *target, const gs_log2_scale_point *pscale,
162 int alpha_bits, int mapped_x)
163 { gs_make_mem_device(adev, &mem_alpha_buffer_device, mem, 0, target);
164 adev->max_fill_band = 1 << pscale->y;
165 adev->log2_scale = *pscale;
166 adev->log2_alpha_bits = alpha_bits >> 1; /* works for 1,2,4 */
167 adev->mapped_x = mapped_x;
168 set_dev_proc(adev, close_device, mem_abuf_close);
169 }
170
171 /* Test whether a device is an alpha-buffering device. */
172 bool
173 gs_device_is_abuf(const gx_device *dev)
174 { /* We can't just compare the procs, or even an individual proc, */
175 /* because we might be tracing. Instead, check the identity of */
176 /* the device name. */
177 return dev->dname == mem_alpha_buffer_device.dname;
178 }
179
180 /* Internal routine to flush a block of the buffer. */
181 /* A block is a group of scan lines whose initial Y is a multiple */
182 /* of the Y scale and whose height is equal to the Y scale. */
183 private int
184 abuf_flush_block(gx_device_memory *adev, int y)
185 { gx_device *target = adev->target;
186 int block_height = 1 << adev->log2_scale.y;
187 int alpha_bits = 1 << adev->log2_alpha_bits;
188 int ddepth =
189 (adev->width >> adev->log2_scale.x) << adev->log2_alpha_bits;
190 uint draster = bitmap_raster(ddepth);
191 int buffer_y = y - adev->mapped_y + adev->mapped_start;
192 byte *bits;
193
194 if ( buffer_y >= adev->height )
195 buffer_y -= adev->height;
196 bits = scan_line_base(adev, buffer_y);
197 { /*
198 * Many bits are typically zero. Save time by computing
199 * an accurate X bounding box before compressing.
200 * Unfortunately, in order to deal with alpha nibble swapping
201 * (see gsbitops.c), we can't expand the box only to pixel
202 * boundaries:
203 int alpha_mask = -1 << adev->log2_alpha_bits;
204 * Instead, we must expand it to byte boundaries,
205 */
206 int alpha_mask = ~7;
207 gs_int_rect bbox;
208 int width;
209
210 bits_bounding_box(bits, block_height, adev->raster, &bbox);
211 bbox.p.x &= alpha_mask;
212 bbox.q.x = (bbox.q.x + ~alpha_mask) & alpha_mask;
213 width = bbox.q.x - bbox.p.x;
214 bits_compress_scaled(bits, bbox.p.x, width, block_height,
215 adev->raster, bits, draster, &adev->log2_scale,
216 adev->log2_alpha_bits);
217 return (*dev_proc(target, copy_alpha))(target,
218 bits, 0, draster, gx_no_bitmap_id,
219 (adev->mapped_x + bbox.p.x) >>
220 adev->log2_scale.x,
221 y >> adev->log2_scale.y,
222 width >> adev->log2_scale.x, 1,
223 adev->save_color, alpha_bits);
224 }
225 }
226 /* Flush the entire buffer. */
227 private int
228 abuf_flush(gx_device_memory *adev)
229 { int y, code = 0;
230 int block_height = 1 << adev->log2_scale.y;
231 for ( y = 0; y < adev->mapped_height; y += block_height )
232 if ( (code = abuf_flush_block(adev, adev->mapped_y + y)) < 0 )
233 return code;
234 adev->mapped_height = adev->mapped_start = 0;
235 return 0;
236 }
237
238 /* Close the device, flushing the buffer. */
239 private int
240 mem_abuf_close(gx_device *dev)
241 { int code = abuf_flush(mdev);
242 if ( code < 0 )
243 return code;
244 return mem_close(dev);
245 }
246
247 /*
248 * Framework for mapping a requested imaging operation to the buffer.
249 * For now, we assume top-to-bottom transfers and use a very simple algorithm.
250 */
251 typedef struct y_transfer_s {
252 int y_next;
253 int height_left;
254 int transfer_y;
255 int transfer_height;
256 } y_transfer;
257 private void near
258 y_transfer_init(y_transfer *pyt, gx_device *dev, int ty, int th)
259 { int bh = 1 << mdev->log2_scale.y;
260 if ( ty < mdev->mapped_y || ty > mdev->mapped_y + mdev->mapped_height )
261 { abuf_flush(mdev);
262 mdev->mapped_y = ty & -bh;
263 mdev->mapped_height = bh;
264 memset(scan_line_base(mdev, 0), 0, bh * mdev->raster);
265 }
266 pyt->y_next = ty;
267 pyt->height_left = th;
268 pyt->transfer_height = 0;
269 }
270 /* while ( yt.height_left > 0 ) { y_transfer_next(&yt, mdev); ... } */
271 private void near
272 y_transfer_next(y_transfer *pyt, gx_device *dev)
273 { int my = mdev->mapped_y, mh = mdev->mapped_height;
274 int ms = mdev->mapped_start;
275 int ty = pyt->y_next += pyt->transfer_height;
276 int th = pyt->height_left;
277 int bh = 1 << mdev->log2_scale.y;
278 /* From here on, we know that my <= ty <= my + mh. */
279 int tby, tbh;
280 if ( ty == my + mh )
281 { /* Add a new block at my1. */
282 if ( mh == mdev->height )
283 { abuf_flush_block(mdev, my);
284 mdev->mapped_y = my += bh;
285 if ( (mdev->mapped_start = ms += bh) == mh )
286 mdev->mapped_start = ms = 0;
287 }
288 else
289 { /* Because we currently never extend backwards, */
290 /* we know we can't wrap around in this case. */
291 mdev->mapped_height = mh += bh;
292 }
293 memset(scan_line_base(mdev, (ms == 0 ? mh : ms) - bh),
294 0, bh * mdev->raster);
295 }
296 /* Now we know that my <= ty < my + mh. */
297 tby = ty - my + ms;
298 if ( tby < mdev->height )
299 { tbh = mdev->height - ms;
300 if ( tbh > mh ) tbh = mh;
301 tbh -= tby - ms;
302 }
303 else /* wrap around */
304 { tby -= mdev->height;
305 tbh = ms + mh - dev->height - tby;
306 }
307 if_debug7('v', "[v]my=%d, mh=%d, ms=%d, ty=%d, th=%d, tby=%d, tbh=%d\n",
308 my, mh, ms, ty, th, tby, tbh);
309 if ( tbh > th ) tbh = th;
310 pyt->height_left = th - tbh;
311 pyt->transfer_y = tby;
312 pyt->transfer_height = tbh;
313 }
314
315 /* Copy a monobit image. */
316 private int
317 mem_abuf_copy_mono(gx_device *dev,
318 const byte *base, int sourcex, int sraster, gx_bitmap_id id,
319 int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
320 { y_transfer yt;
321 if ( zero != gx_no_color_index || one == gx_no_color_index )
322 return_error(gs_error_undefinedresult);
323 x -= mdev->mapped_x;
324 fit_copy_xwh(dev, base, sourcex, sraster, id, x, y, w, h); /* don't limit y */
325 mdev->save_color = one;
326 y_transfer_init(&yt, dev, y, h);
327 while ( yt.height_left > 0 )
328 { y_transfer_next(&yt, dev);
329 (*dev_proc(&mem_mono_device, copy_mono))(dev,
330 base + (yt.y_next - y) * sraster,
331 sourcex, sraster, gx_no_bitmap_id,
332 x, yt.transfer_y, w, yt.transfer_height,
333 gx_no_color_index, (gx_color_index)1);
334 }
335 return 0;
336 }
337
338 /* Fill a rectangle. */
339 private int
340 mem_abuf_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
341 gx_color_index color)
342 { y_transfer yt;
343 x -= mdev->mapped_x;
344 fit_fill_xyw(dev, x, y, w, h); /* don't limit h */
345 /* or check w <= 0, h <= 0 */
346 mdev->save_color = color;
347 y_transfer_init(&yt, dev, y, h);
348 while ( yt.height_left > 0 )
349 { y_transfer_next(&yt, dev);
350 (*dev_proc(&mem_mono_device, fill_rectangle))(dev,
351 x, yt.transfer_y, w, yt.transfer_height,
352 (gx_color_index)1);
353 }
354 return 0;
355 }