2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * Last changed in libpng 1.4.1 [February 25, 2010]
5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
14 #define PNG_NO_PEDANTIC_WARNINGS
16 #ifdef PNG_WRITE_SUPPORTED
19 /* Transform the data according to the user's wishes. The order of
20 * transformations is significant.
23 png_do_write_transformations(png_structp png_ptr)
25 png_debug(1, "in png_do_write_transformations");
30 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
31 if (png_ptr->transformations & PNG_USER_TRANSFORM)
32 if (png_ptr->write_user_transform_fn != NULL)
33 (*(png_ptr->write_user_transform_fn)) /* User write transform
35 (png_ptr, /* png_ptr */
36 &(png_ptr->row_info), /* row_info: */
37 /* png_uint_32 width; width of row */
38 /* png_uint_32 rowbytes; number of bytes in row */
39 /* png_byte color_type; color type of pixels */
40 /* png_byte bit_depth; bit depth of samples */
41 /* png_byte channels; number of channels (1-4) */
42 /* png_byte pixel_depth; bits per pixel (depth*channels) */
43 png_ptr->row_buf + 1); /* start of pixel data for row */
45 #ifdef PNG_WRITE_FILLER_SUPPORTED
46 if (png_ptr->transformations & PNG_FILLER)
47 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
50 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
51 if (png_ptr->transformations & PNG_PACKSWAP)
52 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
54 #ifdef PNG_WRITE_PACK_SUPPORTED
55 if (png_ptr->transformations & PNG_PACK)
56 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
57 (png_uint_32)png_ptr->bit_depth);
59 #ifdef PNG_WRITE_SWAP_SUPPORTED
60 if (png_ptr->transformations & PNG_SWAP_BYTES)
61 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
63 #ifdef PNG_WRITE_SHIFT_SUPPORTED
64 if (png_ptr->transformations & PNG_SHIFT)
65 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
68 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
69 if (png_ptr->transformations & PNG_SWAP_ALPHA)
70 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
72 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
73 if (png_ptr->transformations & PNG_INVERT_ALPHA)
74 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
76 #ifdef PNG_WRITE_BGR_SUPPORTED
77 if (png_ptr->transformations & PNG_BGR)
78 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
80 #ifdef PNG_WRITE_INVERT_SUPPORTED
81 if (png_ptr->transformations & PNG_INVERT_MONO)
82 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
86 #ifdef PNG_WRITE_PACK_SUPPORTED
87 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
88 * row_info bit depth should be 8 (one pixel per byte). The channels
89 * should be 1 (this only happens on grayscale and paletted images).
92 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
94 png_debug(1, "in png_do_pack");
96 if (row_info->bit_depth == 8 &&
97 row_info->channels == 1)
99 switch ((int)bit_depth)
106 png_uint_32 row_width = row_info->width;
113 for (i = 0; i < row_width; i++)
137 png_uint_32 row_width = row_info->width;
143 for (i = 0; i < row_width; i++)
147 value = (png_byte)(*sp & 0x03);
148 v |= (value << shift);
169 png_uint_32 row_width = row_info->width;
175 for (i = 0; i < row_width; i++)
179 value = (png_byte)(*sp & 0x0f);
180 v |= (value << shift);
199 row_info->bit_depth = (png_byte)bit_depth;
200 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
201 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
207 #ifdef PNG_WRITE_SHIFT_SUPPORTED
208 /* Shift pixel values to take advantage of whole range. Pass the
209 * true number of bits in bit_depth. The row should be packed
210 * according to row_info->bit_depth. Thus, if you had a row of
211 * bit depth 4, but the pixels only had values from 0 to 7, you
212 * would pass 3 as bit_depth, and this routine would translate the
216 png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
218 png_debug(1, "in png_do_shift");
221 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
223 int shift_start[4], shift_dec[4];
226 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
228 shift_start[channels] = row_info->bit_depth - bit_depth->red;
229 shift_dec[channels] = bit_depth->red;
231 shift_start[channels] = row_info->bit_depth - bit_depth->green;
232 shift_dec[channels] = bit_depth->green;
234 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
235 shift_dec[channels] = bit_depth->blue;
240 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
241 shift_dec[channels] = bit_depth->gray;
244 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
246 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
247 shift_dec[channels] = bit_depth->alpha;
251 /* With low row depths, could only be grayscale, so one channel */
252 if (row_info->bit_depth < 8)
257 png_uint_32 row_bytes = row_info->rowbytes;
259 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
261 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
266 for (i = 0; i < row_bytes; i++, bp++)
273 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
276 *bp |= (png_byte)((v << j) & 0xff);
278 *bp |= (png_byte)((v >> (-j)) & mask);
282 else if (row_info->bit_depth == 8)
286 png_uint_32 istop = channels * row_info->width;
288 for (i = 0; i < istop; i++, bp++)
293 int c = (int)(i%channels);
297 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
300 *bp |= (png_byte)((v << j) & 0xff);
302 *bp |= (png_byte)((v >> (-j)) & 0xff);
310 png_uint_32 istop = channels * row_info->width;
312 for (bp = row, i = 0; i < istop; i++)
314 int c = (int)(i%channels);
315 png_uint_16 value, v;
318 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
320 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
323 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
325 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
327 *bp++ = (png_byte)(value >> 8);
328 *bp++ = (png_byte)(value & 0xff);
335 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
337 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
339 png_debug(1, "in png_do_write_swap_alpha");
342 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
344 /* This converts from ARGB to RGBA */
345 if (row_info->bit_depth == 8)
349 png_uint_32 row_width = row_info->width;
350 for (i = 0, sp = dp = row; i < row_width; i++)
352 png_byte save = *(sp++);
359 /* This converts from AARRGGBB to RRGGBBAA */
364 png_uint_32 row_width = row_info->width;
366 for (i = 0, sp = dp = row; i < row_width; i++)
382 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
384 /* This converts from AG to GA */
385 if (row_info->bit_depth == 8)
389 png_uint_32 row_width = row_info->width;
391 for (i = 0, sp = dp = row; i < row_width; i++)
393 png_byte save = *(sp++);
398 /* This converts from AAGG to GGAA */
403 png_uint_32 row_width = row_info->width;
405 for (i = 0, sp = dp = row; i < row_width; i++)
421 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
423 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
425 png_debug(1, "in png_do_write_invert_alpha");
428 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
430 /* This inverts the alpha channel in RGBA */
431 if (row_info->bit_depth == 8)
435 png_uint_32 row_width = row_info->width;
436 for (i = 0, sp = dp = row; i < row_width; i++)
444 *(dp++) = (png_byte)(255 - *(sp++));
447 /* This inverts the alpha channel in RRGGBBAA */
452 png_uint_32 row_width = row_info->width;
454 for (i = 0, sp = dp = row; i < row_width; i++)
465 *(dp++) = (png_byte)(255 - *(sp++));
466 *(dp++) = (png_byte)(255 - *(sp++));
470 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
472 /* This inverts the alpha channel in GA */
473 if (row_info->bit_depth == 8)
477 png_uint_32 row_width = row_info->width;
479 for (i = 0, sp = dp = row; i < row_width; i++)
482 *(dp++) = (png_byte)(255 - *(sp++));
485 /* This inverts the alpha channel in GGAA */
490 png_uint_32 row_width = row_info->width;
492 for (i = 0, sp = dp = row; i < row_width; i++)
499 *(dp++) = (png_byte)(255 - *(sp++));
500 *(dp++) = (png_byte)(255 - *(sp++));
508 #ifdef PNG_MNG_FEATURES_SUPPORTED
509 /* Undoes intrapixel differencing */
511 png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
513 png_debug(1, "in png_do_write_intrapixel");
516 (row_info->color_type & PNG_COLOR_MASK_COLOR))
519 png_uint_32 row_width = row_info->width;
520 if (row_info->bit_depth == 8)
525 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
527 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
532 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
534 *(rp) = (png_byte)((*rp - *(rp+1))&0xff);
535 *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
538 else if (row_info->bit_depth == 16)
543 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
545 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
550 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
552 png_uint_32 s0 = (*(rp ) << 8) | *(rp+1);
553 png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3);
554 png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5);
555 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
556 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
557 *(rp ) = (png_byte)((red >> 8) & 0xff);
558 *(rp+1) = (png_byte)(red & 0xff);
559 *(rp+4) = (png_byte)((blue >> 8) & 0xff);
560 *(rp+5) = (png_byte)(blue & 0xff);
565 #endif /* PNG_MNG_FEATURES_SUPPORTED */
566 #endif /* PNG_WRITE_SUPPORTED */