1 /* Copyright (C) 1995-2003, artofcode LLC. 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: gdevpng.c,v 1.12 2005/07/15 05:23:43 giles Exp $ */
18 /* PNG (Portable Network Graphics) Format. Pronounced "ping". */
19 /* lpd 1999-09-24: changes PNG_NO_STDIO to PNG_NO_CONSOLE_IO for libpng
20 versions 1.0.3 and later. */
21 /* lpd 1999-07-01: replaced remaining uses of gs_malloc and gs_free with
22 gs_alloc_bytes and gs_free_object. */
23 /* lpd 1999-03-08: changed png.h to png_.h to allow compiling with only
24 headers in /usr/include, no source code. */
25 /* lpd 1997-07-20: changed from using gs_malloc/png_xxx_int to png_create_xxx
26 * for allocating structures, and from gs_free to png_write_destroy for
27 * freeing them. */
28 /* lpd 1997-5-7: added PNG_LIBPNG_VER conditional for operand types of
29 * dummy png_push_fill_buffer. */
30 /* lpd 1997-4-13: Added PNG_NO_STDIO to remove library access to stderr. */
31 /* lpd 1997-3-14: Added resolution (pHYs) to output. */
32 /* lpd 1996-6-24: Added #ifdef for compatibility with old libpng versions. */
33 /* lpd 1996-6-11: Edited to remove unnecessary color mapping code. */
34 /* lpd (L. Peter Deutsch) 1996-4-7: Modified for libpng 0.88. */
35 /* Original version by Russell Lang 1995-07-04 */
36
37 #include "gdevprn.h"
38 #include "gdevmem.h"
39 #include "gdevpccm.h"
40 #include "gscdefs.h"
41
42 #define PNG_INTERNAL
43 /*
44 * libpng versions 1.0.3 and later allow disabling access to the stdxxx
45 * files while retaining support for FILE * I/O.
46 */
47 #define PNG_NO_CONSOLE_IO
48 /*
49 * Earlier libpng versions require disabling FILE * I/O altogether.
50 * This produces a compiler warning about no prototype for png_init_io.
51 * The right thing will happen at link time, since the library itself
52 * is compiled with stdio support. Unfortunately, we can't do this
53 * conditionally depending on PNG_LIBPNG_VER, because this is defined
54 * in png.h.
55 */
56 /*#define PNG_NO_STDIO*/
57 #include "png_.h"
58
59 /* ------ The device descriptors ------ */
60
61 /*
62 * Default X and Y resolution.
63 */
64 #define X_DPI 72
65 #define Y_DPI 72
66
67 private dev_proc_print_page(png_print_page);
68 private dev_proc_open_device(pngalpha_open);
69 private dev_proc_encode_color(pngalpha_encode_color);
70 private dev_proc_decode_color(pngalpha_decode_color);
71 private dev_proc_copy_alpha(pngalpha_copy_alpha);
72 private dev_proc_fill_rectangle(pngalpha_fill_rectangle);
73 private dev_proc_get_params(pngalpha_get_params);
74 private dev_proc_put_params(pngalpha_put_params);
75 private dev_proc_create_buf_device(pngalpha_create_buf_device);
76
77 /* Monochrome. */
78
79 const gx_device_printer gs_pngmono_device =
80 prn_device(prn_std_procs, "pngmono",
81 DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
82 X_DPI, Y_DPI,
83 0, 0, 0, 0, /* margins */
84 1, png_print_page);
85
86 /* 4-bit planar (EGA/VGA-style) color. */
87
88 private const gx_device_procs png16_procs =
89 prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
90 pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
91 const gx_device_printer gs_png16_device = {
92 prn_device_body(gx_device_printer, png16_procs, "png16",
93 DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
94 X_DPI, Y_DPI,
95 0, 0, 0, 0, /* margins */
96 3, 4, 1, 1, 2, 2, png_print_page)
97 };
98
99 /* 8-bit (SuperVGA-style) color. */
100 /* (Uses a fixed palette of 3,3,2 bits.) */
101
102 private const gx_device_procs png256_procs =
103 prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
104 pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
105 const gx_device_printer gs_png256_device = {
106 prn_device_body(gx_device_printer, png256_procs, "png256",
107 DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
108 X_DPI, Y_DPI,
109 0, 0, 0, 0, /* margins */
110 3, 8, 5, 5, 6, 6, png_print_page)
111 };
112
113 /* 8-bit gray */
114
115 private const gx_device_procs pnggray_procs =
116 prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
117 gx_default_gray_map_rgb_color, gx_default_gray_map_color_rgb);
118 const gx_device_printer gs_pnggray_device =
119 {prn_device_body(gx_device_printer, pnggray_procs, "pnggray",
120 DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
121 X_DPI, Y_DPI,
122 0, 0, 0, 0, /* margins */
123 1, 8, 255, 0, 256, 0, png_print_page)
124 };
125
126 /* 24-bit color. */
127
128 private const gx_device_procs png16m_procs =
129 prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
130 gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
131 const gx_device_printer gs_png16m_device =
132 prn_device(png16m_procs, "png16m",
133 DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
134 X_DPI, Y_DPI,
135 0, 0, 0, 0, /* margins */
136 24, png_print_page);
137
138 /* 48 bit color. */
139
140 private const gx_device_procs png48_procs =
141 prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
142 gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
143 const gx_device_printer gs_png48_device =
144 prn_device(png48_procs, "png48",
145 DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
146 X_DPI, Y_DPI,
147 0, 0, 0, 0, /* margins */
148 48, png_print_page);
149
150
151 /* 32-bit RGBA */
152 /* pngalpha device is 32-bit RGBA, with the alpha channel
153 * indicating pixel coverage, not true transparency.
154 * Anti-aliasing is enabled by default.
155 * An erasepage will erase to transparent, not white.
156 * It is intended to be used for creating web graphics with
157 * a transparent background.
158 */
159 typedef struct gx_device_pngalpha_s gx_device_pngalpha;
160 struct gx_device_pngalpha_s {
161 gx_device_common;
162 gx_prn_device_common;
163 dev_t_proc_fill_rectangle((*orig_fill_rectangle), gx_device);
164 int background;
165 };
166 private const gx_device_procs pngalpha_procs =
167 {
168 pngalpha_open,
169 NULL, /* get_initial_matrix */
170 NULL, /* sync_output */
171 gdev_prn_output_page,
172 gdev_prn_close,
173 pngalpha_encode_color, /* map_rgb_color */
174 pngalpha_decode_color, /* map_color_rgb */
175 pngalpha_fill_rectangle,
176 NULL, /* tile_rectangle */
177 NULL, /* copy_mono */
178 NULL, /* copy_color */
179 NULL, /* draw_line */
180 NULL, /* get_bits */
181 pngalpha_get_params,
182 pngalpha_put_params,
183 NULL, /* map_cmyk_color */
184 NULL, /* get_xfont_procs */
185 NULL, /* get_xfont_device */
186 NULL, /* map_rgb_alpha_color */
187 gx_page_device_get_page_device,
188 NULL, /* get_alpha_bits */
189 pngalpha_copy_alpha,
190 NULL, /* get_band */
191 NULL, /* copy_rop */
192 NULL, /* fill_path */
193 NULL, /* stroke_path */
194 NULL, /* fill_mask */
195 NULL, /* fill_trapezoid */
196 NULL, /* fill_parallelogram */
197 NULL, /* fill_triangle */
198 NULL, /* draw_thin_line */
199 NULL, /* begin_image */
200 NULL, /* image_data */
201 NULL, /* end_image */
202 NULL, /* strip_tile_rectangle */
203 NULL, /* strip_copy_rop, */
204 NULL, /* get_clipping_box */
205 NULL, /* begin_typed_image */
206 NULL, /* get_bits_rectangle */
207 NULL, /* map_color_rgb_alpha */
208 NULL, /* create_compositor */
209 NULL, /* get_hardware_params */
210 NULL, /* text_begin */
211 NULL, /* finish_copydevice */
212 NULL, /* begin_transparency_group */
213 NULL, /* end_transparency_group */
214 NULL, /* begin_transparency_mask */
215 NULL, /* end_transparency_mask */
216 NULL, /* discard_transparency_layer */
217 gx_default_DevRGB_get_color_mapping_procs,
218 gx_default_DevRGB_get_color_comp_index,
219 pngalpha_encode_color,
220 pngalpha_decode_color
221 };
222
223 const gx_device_pngalpha gs_pngalpha_device = {
224 std_device_part1_(gx_device_pngalpha, &pngalpha_procs, "pngalpha",
225 &st_device_printer, open_init_closed),
226 /* color_info */
227 {3 /* max components */,
228 3 /* number components */,
229 GX_CINFO_POLARITY_ADDITIVE /* polarity */,
230 32 /* depth */,
231 -1 /* gray index */,
232 255 /* max gray */,
233 255 /* max color */,
234 256 /* dither grays */,
235 256 /* dither colors */,
236 { 4, 4 } /* antialias info text, graphics */,
237 GX_CINFO_SEP_LIN_NONE /* separable_and_linear */,
238 { 0 } /* component shift */,
239 { 0 } /* component bits */,
240 { 0 } /* component mask */,
241 "DeviceRGB" /* process color name */,
242 GX_CINFO_OPMODE_UNKNOWN /* opmode */,
243 0 /* process_cmps */
244 },
245 std_device_part2_(
246 (int)((float)(DEFAULT_WIDTH_10THS) * (X_DPI) / 10 + 0.5),
247 (int)((float)(DEFAULT_HEIGHT_10THS) * (Y_DPI) / 10 + 0.5),
248 X_DPI, Y_DPI),
249 offset_margin_values(0, 0, 0, 0, 0, 0),
250 std_device_part3_(),
251 prn_device_body_rest_(png_print_page),
252 NULL,
253 0xffffff /* white background */
254 };
255
256
257 /* ------ Private definitions ------ */
258
259 /* Write out a page in PNG format. */
260 /* This routine is used for all formats. */
261 private int
png_print_page(gx_device_printer * pdev,FILE * file)262 png_print_page(gx_device_printer * pdev, FILE * file)
263 {
264 gs_memory_t *mem = pdev->memory;
265 int raster = gdev_prn_raster(pdev);
266
267 /* PNG structures */
268 byte *row = gs_alloc_bytes(mem, raster, "png raster buffer");
269 png_struct *png_ptr =
270 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
271 png_info *info_ptr =
272 png_create_info_struct(png_ptr);
273 int height = pdev->height;
274 int depth = pdev->color_info.depth;
275 int y;
276 int code; /* return code */
277 char software_key[80];
278 char software_text[256];
279 png_text text_png;
280
281 if (row == 0 || png_ptr == 0 || info_ptr == 0) {
282 code = gs_note_error(gs_error_VMerror);
283 goto done;
284 }
285 /* set error handling */
286 if (setjmp(png_ptr->jmpbuf)) {
287 /* If we get here, we had a problem reading the file */
288 code = gs_note_error(gs_error_VMerror);
289 goto done;
290 }
291 code = 0; /* for normal path */
292 /* set up the output control */
293 png_init_io(png_ptr, file);
294
295 /* set the file information here */
296 info_ptr->width = pdev->width;
297 info_ptr->height = pdev->height;
298 /* resolution is in pixels per meter vs. dpi */
299 info_ptr->x_pixels_per_unit =
300 (png_uint_32) (pdev->HWResolution[0] * (100.0 / 2.54));
301 info_ptr->y_pixels_per_unit =
302 (png_uint_32) (pdev->HWResolution[1] * (100.0 / 2.54));
303 info_ptr->phys_unit_type = PNG_RESOLUTION_METER;
304 info_ptr->valid |= PNG_INFO_pHYs;
305 switch (depth) {
306 case 32:
307 info_ptr->bit_depth = 8;
308 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
309 png_set_invert_alpha(png_ptr);
310 { gx_device_pngalpha *ppdev = (gx_device_pngalpha *)pdev;
311 png_color_16 background;
312 background.index = 0;
313 background.red = (ppdev->background >> 16) & 0xff;
314 background.green = (ppdev->background >> 8) & 0xff;
315 background.blue = (ppdev->background) & 0xff;
316 background.gray = 0;
317 png_set_bKGD(png_ptr, info_ptr, &background);
318 }
319 break;
320 case 48:
321 info_ptr->bit_depth = 16;
322 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
323 #if defined(ARCH_IS_BIG_ENDIAN) && (!ARCH_IS_BIG_ENDIAN)
324 png_set_swap(png_ptr);
325 #endif
326 break;
327 case 24:
328 info_ptr->bit_depth = 8;
329 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
330 break;
331 case 8:
332 info_ptr->bit_depth = 8;
333 if (gx_device_has_color(pdev))
334 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
335 else
336 info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
337 break;
338 case 4:
339 info_ptr->bit_depth = 4;
340 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
341 break;
342 case 1:
343 info_ptr->bit_depth = 1;
344 info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
345 /* invert monocrome pixels */
346 png_set_invert_mono(png_ptr);
347 break;
348 }
349
350 /* set the palette if there is one */
351 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
352 int i;
353 int num_colors = 1 << depth;
354 gx_color_value rgb[3];
355
356 info_ptr->palette =
357 (void *)gs_alloc_bytes(mem, 256 * sizeof(png_color),
358 "png palette");
359 if (info_ptr->palette == 0) {
360 code = gs_note_error(gs_error_VMerror);
361 goto done;
362 }
363 info_ptr->num_palette = num_colors;
364 info_ptr->valid |= PNG_INFO_PLTE;
365 for (i = 0; i < num_colors; i++) {
366 (*dev_proc(pdev, map_color_rgb)) ((gx_device *) pdev,
367 (gx_color_index) i, rgb);
368 info_ptr->palette[i].red = gx_color_value_to_byte(rgb[0]);
369 info_ptr->palette[i].green = gx_color_value_to_byte(rgb[1]);
370 info_ptr->palette[i].blue = gx_color_value_to_byte(rgb[2]);
371 }
372 }
373 /* add comment */
374 strncpy(software_key, "Software", sizeof(software_key));
375 sprintf(software_text, "%s %d.%02d", gs_product,
376 (int)(gs_revision / 100), (int)(gs_revision % 100));
377 text_png.compression = -1; /* uncompressed */
378 text_png.key = software_key;
379 text_png.text = software_text;
380 text_png.text_length = strlen(software_text);
381 info_ptr->text = &text_png;
382 info_ptr->num_text = 1;
383
384 /* write the file information */
385 png_write_info(png_ptr, info_ptr);
386
387 /* don't write the comments twice */
388 info_ptr->num_text = 0;
389 info_ptr->text = NULL;
390
391 /* Write the contents of the image. */
392 for (y = 0; y < height; y++) {
393 gdev_prn_copy_scan_lines(pdev, y, row, raster);
394 png_write_rows(png_ptr, &row, 1);
395 }
396
397 /* write the rest of the file */
398 png_write_end(png_ptr, info_ptr);
399
400 /* if you alloced the palette, free it here */
401 gs_free_object(mem, info_ptr->palette, "png palette");
402
403 done:
404 /* free the structures */
405 png_destroy_write_struct(&png_ptr, &info_ptr);
406 gs_free_object(mem, row, "png raster buffer");
407
408 return code;
409 }
410
411 /*
412 * Patch around a static reference to a never-used procedure.
413 * This could be avoided if we were willing to edit pngconf.h to
414 * #undef PNG_PROGRESSIVE_READ_SUPPORTED
415 */
416 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
417 # if PNG_LIBPNG_VER >= 95
418 # define PPFB_LENGTH_T png_size_t
419 # else
420 # define PPFB_LENGTH_T png_uint_32
421 # endif
422 void
png_push_fill_buffer(png_structp png_ptr,png_bytep buffer,PPFB_LENGTH_T length)423 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer,
424 PPFB_LENGTH_T length)
425 {
426 }
427 #endif
428
429 private int
pngalpha_open(gx_device * pdev)430 pngalpha_open(gx_device * pdev)
431 {
432 gx_device_pngalpha *ppdev = (gx_device_pngalpha *)pdev;
433 int code;
434 /* We replace create_buf_device so we can replace copy_alpha
435 * for memory device, but not clist.
436 */
437 ppdev->printer_procs.buf_procs.create_buf_device =
438 pngalpha_create_buf_device;
439 code = gdev_prn_open(pdev);
440 /* We intercept fill_rectangle to translate "fill page with white"
441 * into "fill page with transparent". We then call the original
442 * implementation of fill_page.
443 */
444 if ((ppdev->procs.fill_rectangle != pngalpha_fill_rectangle) &&
445 (ppdev->procs.fill_rectangle != NULL)) {
446 ppdev->orig_fill_rectangle = ppdev->procs.fill_rectangle;
447 ppdev->procs.fill_rectangle = pngalpha_fill_rectangle;
448 }
449 return code;
450 }
451
452 private int
pngalpha_create_buf_device(gx_device ** pbdev,gx_device * target,const gx_render_plane_t * render_plane,gs_memory_t * mem,bool for_band)453 pngalpha_create_buf_device(gx_device **pbdev, gx_device *target,
454 const gx_render_plane_t *render_plane, gs_memory_t *mem,
455 bool for_band)
456 {
457 gx_device_printer *ptarget = (gx_device_printer *)target;
458 int code = gx_default_create_buf_device(pbdev, target,
459 render_plane, mem, for_band);
460 /* Now set copy_alpha to one that handles RGBA */
461 set_dev_proc(*pbdev, copy_alpha, ptarget->orig_procs.copy_alpha);
462 return code;
463 }
464
465 private int
pngalpha_put_params(gx_device * pdev,gs_param_list * plist)466 pngalpha_put_params(gx_device * pdev, gs_param_list * plist)
467 {
468 gx_device_pngalpha *ppdev = (gx_device_pngalpha *)pdev;
469 int background;
470 int code;
471
472 /* BackgroundColor in format 16#RRGGBB is used for bKGD chunk */
473 switch(code = param_read_int(plist, "BackgroundColor", &background)) {
474 case 0:
475 ppdev->background = background & 0xffffff;
476 break;
477 case 1: /* not found */
478 code = 0;
479 break;
480 default:
481 param_signal_error(plist, "BackgroundColor", code);
482 break;
483 }
484
485 if (code == 0) {
486 code = gdev_prn_put_params(pdev, plist);
487 if ((ppdev->procs.fill_rectangle != pngalpha_fill_rectangle) &&
488 (ppdev->procs.fill_rectangle != NULL)) {
489 /* Get current implementation of fill_rectangle and restore ours.
490 * Implementation is either clist or memory and can change
491 * during put_params.
492 */
493 ppdev->orig_fill_rectangle = ppdev->procs.fill_rectangle;
494 ppdev->procs.fill_rectangle = pngalpha_fill_rectangle;
495 }
496 }
497 return code;
498 }
499
500 /* Get device parameters */
501 private int
pngalpha_get_params(gx_device * pdev,gs_param_list * plist)502 pngalpha_get_params(gx_device * pdev, gs_param_list * plist)
503 {
504 gx_device_pngalpha *ppdev = (gx_device_pngalpha *)pdev;
505 int code = gdev_prn_get_params(pdev, plist);
506 if (code >= 0)
507 code = param_write_int(plist, "BackgroundColor",
508 &(ppdev->background));
509 return code;
510 }
511
512
513 /* RGB mapping for 32-bit RGBA color devices */
514
515 private gx_color_index
pngalpha_encode_color(gx_device * dev,const gx_color_value cv[])516 pngalpha_encode_color(gx_device * dev, const gx_color_value cv[])
517 {
518 /* bits 0-7 are alpha, stored inverted to avoid white/opaque
519 * being 0xffffffff which is also gx_no_color_index.
520 * So 0xff is transparent and 0x00 is opaque.
521 * We always return opaque colors (bits 0-7 = 0).
522 * Return value is 0xRRGGBB00.
523 */
524 return
525 ((uint) gx_color_value_to_byte(cv[2]) << 8) +
526 ((ulong) gx_color_value_to_byte(cv[1]) << 16) +
527 ((ulong) gx_color_value_to_byte(cv[0]) << 24);
528 }
529
530 /* Map a color index to a r-g-b color. */
531 private int
pngalpha_decode_color(gx_device * dev,gx_color_index color,gx_color_value prgb[3])532 pngalpha_decode_color(gx_device * dev, gx_color_index color,
533 gx_color_value prgb[3])
534 {
535 prgb[0] = gx_color_value_from_byte((color >> 24) & 0xff);
536 prgb[1] = gx_color_value_from_byte((color >> 16) & 0xff);
537 prgb[2] = gx_color_value_from_byte((color >> 8) & 0xff);
538 return 0;
539 }
540
541 private int
pngalpha_fill_rectangle(gx_device * dev,int x,int y,int w,int h,gx_color_index color)542 pngalpha_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
543 gx_color_index color)
544 {
545 gx_device_pngalpha *pdev = (gx_device_pngalpha *)dev;
546 if ((color == 0xffffff00) && (x==0) && (y==0)
547 && (w==dev->width) && (h==dev->height)) {
548 /* If filling whole page with white, make it transparent */
549 return pdev->orig_fill_rectangle(dev, x, y, w, h, 0xfefefeff);
550 }
551 return pdev->orig_fill_rectangle(dev, x, y, w, h, color);
552 }
553
554 /* Implementation for 32-bit RGBA in a memory buffer */
555 /* Derived from gx_default_copy_alpha, but now maintains alpha channel. */
556 private int
pngalpha_copy_alpha(gx_device * dev,const byte * data,int data_x,int raster,gx_bitmap_id id,int x,int y,int width,int height,gx_color_index color,int depth)557 pngalpha_copy_alpha(gx_device * dev, const byte * data, int data_x,
558 int raster, gx_bitmap_id id, int x, int y, int width, int height,
559 gx_color_index color, int depth)
560 { /* This might be called with depth = 1.... */
561 if (depth == 1)
562 return (*dev_proc(dev, copy_mono)) (dev, data, data_x, raster, id,
563 x, y, width, height,
564 gx_no_color_index, color);
565 /*
566 * Simulate alpha by weighted averaging of RGB values.
567 * This is very slow, but functionally correct.
568 */
569 {
570 const byte *row;
571 gs_memory_t *mem = dev->memory;
572 int bpp = dev->color_info.depth;
573 int ncomps = dev->color_info.num_components;
574 uint in_size = gx_device_raster(dev, false);
575 byte *lin;
576 uint out_size;
577 byte *lout;
578 int code = 0;
579 gx_color_value color_cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
580 int ry;
581
582 fit_copy(dev, data, data_x, raster, id, x, y, width, height);
583 row = data;
584 out_size = bitmap_raster(width * bpp);
585 lin = gs_alloc_bytes(mem, in_size, "copy_alpha(lin)");
586 lout = gs_alloc_bytes(mem, out_size, "copy_alpha(lout)");
587 if (lin == 0 || lout == 0) {
588 code = gs_note_error(gs_error_VMerror);
589 goto out;
590 }
591 (*dev_proc(dev, decode_color)) (dev, color, color_cv);
592 for (ry = y; ry < y + height; row += raster, ++ry) {
593 byte *line;
594 int sx, rx;
595
596 DECLARE_LINE_ACCUM_COPY(lout, bpp, x);
597
598 code = (*dev_proc(dev, get_bits)) (dev, ry, lin, &line);
599 if (code < 0)
600 break;
601 for (sx = data_x, rx = x; sx < data_x + width; ++sx, ++rx) {
602 gx_color_index previous = gx_no_color_index;
603 gx_color_index composite;
604 int alpha2, alpha;
605
606 if (depth == 2) /* map 0 - 3 to 0 - 15 */
607 alpha = ((row[sx >> 2] >> ((3 - (sx & 3)) << 1)) & 3) * 5;
608 else
609 alpha2 = row[sx >> 1],
610 alpha = (sx & 1 ? alpha2 & 0xf : alpha2 >> 4);
611 if (alpha == 15) { /* Just write the new color. */
612 composite = color;
613 } else {
614 if (previous == gx_no_color_index) { /* Extract the old color. */
615 const byte *src = line + (rx * (bpp >> 3));
616 previous = 0;
617 previous += (gx_color_index) * src++ << 24;
618 previous += (gx_color_index) * src++ << 16;
619 previous += (gx_color_index) * src++ << 8;
620 previous += *src++;
621 }
622 if (alpha == 0) { /* Just write the old color. */
623 composite = previous;
624 } else { /* Blend values. */
625 gx_color_value cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
626 int i;
627 int old_coverage;
628 int new_coverage;
629
630 (*dev_proc(dev, decode_color)) (dev, previous, cv);
631 /* decode color doesn't give us coverage */
632 cv[3] = previous & 0xff;
633 old_coverage = 255 - cv[3];
634 new_coverage =
635 (255 * alpha + old_coverage * (15 - alpha)) / 15;
636 for (i=0; i<ncomps; i++)
637 cv[i] = min(((255 * alpha * color_cv[i]) +
638 (old_coverage * (15 - alpha ) * cv[i]))
639 / (new_coverage * 15), gx_max_color_value);
640 composite =
641 (*dev_proc(dev, encode_color)) (dev, cv);
642 /* encode color doesn't include coverage */
643 composite |= (255 - new_coverage) & 0xff;
644
645 /* composite can never be gx_no_color_index
646 * because pixel is never completely transparent
647 * (low byte != 0xff).
648 */
649 }
650 }
651 LINE_ACCUM(composite, bpp);
652 }
653 LINE_ACCUM_COPY(dev, lout, bpp, x, rx, raster, ry);
654 }
655 out:gs_free_object(mem, lout, "copy_alpha(lout)");
656 gs_free_object(mem, lin, "copy_alpha(lin)");
657 return code;
658 }
659 }
660
661