2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * Last changed in libpng 1.5.13 [September 27, 2012]
5 * Copyright (c) 1998-2012 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
16 #ifdef PNG_WRITE_SUPPORTED
18 #ifdef PNG_WRITE_TRANSFORMS_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, png_row_infop row_info)
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 row_info, /* row_info: */
37 /* png_uint_32 width; width of row */
38 /* png_size_t 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 */
46 #ifdef PNG_WRITE_FILLER_SUPPORTED
47 if (png_ptr->transformations & PNG_FILLER)
49 if (png_ptr->color_type & (PNG_COLOR_MASK_ALPHA|PNG_COLOR_MASK_PALETTE))
51 /* GA, RGBA or palette; in any of these cases libpng will not do the
52 * the correct thing (whatever that might be).
54 png_warning(png_ptr, "incorrect png_set_filler call ignored");
55 png_ptr->transformations &= ~PNG_FILLER;
59 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
60 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
64 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
65 if (png_ptr->transformations & PNG_PACKSWAP)
66 png_do_packswap(row_info, png_ptr->row_buf + 1);
69 #ifdef PNG_WRITE_PACK_SUPPORTED
70 if (png_ptr->transformations & PNG_PACK)
71 png_do_pack(row_info, png_ptr->row_buf + 1,
72 (png_uint_32)png_ptr->bit_depth);
75 #ifdef PNG_WRITE_SWAP_SUPPORTED
76 if (png_ptr->transformations & PNG_SWAP_BYTES)
77 png_do_swap(row_info, png_ptr->row_buf + 1);
80 #ifdef PNG_WRITE_SHIFT_SUPPORTED
81 if (png_ptr->transformations & PNG_SHIFT)
82 png_do_shift(row_info, png_ptr->row_buf + 1,
86 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
87 if (png_ptr->transformations & PNG_SWAP_ALPHA)
88 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
91 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
92 if (png_ptr->transformations & PNG_INVERT_ALPHA)
93 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
96 #ifdef PNG_WRITE_BGR_SUPPORTED
97 if (png_ptr->transformations & PNG_BGR)
98 png_do_bgr(row_info, png_ptr->row_buf + 1);
101 #ifdef PNG_WRITE_INVERT_SUPPORTED
102 if (png_ptr->transformations & PNG_INVERT_MONO)
103 png_do_invert(row_info, png_ptr->row_buf + 1);
107 #ifdef PNG_WRITE_PACK_SUPPORTED
108 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
109 * row_info bit depth should be 8 (one pixel per byte). The channels
110 * should be 1 (this only happens on grayscale and paletted images).
113 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
115 png_debug(1, "in png_do_pack");
117 if (row_info->bit_depth == 8 &&
118 row_info->channels == 1)
120 switch ((int)bit_depth)
127 png_uint_32 row_width = row_info->width;
134 for (i = 0; i < row_width; i++)
164 png_uint_32 row_width = row_info->width;
171 for (i = 0; i < row_width; i++)
175 value = (png_byte)(*sp & 0x03);
176 v |= (value << shift);
203 png_uint_32 row_width = row_info->width;
210 for (i = 0; i < row_width; i++)
214 value = (png_byte)(*sp & 0x0f);
215 v |= (value << shift);
241 row_info->bit_depth = (png_byte)bit_depth;
242 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
243 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
249 #ifdef PNG_WRITE_SHIFT_SUPPORTED
250 /* Shift pixel values to take advantage of whole range. Pass the
251 * true number of bits in bit_depth. The row should be packed
252 * according to row_info->bit_depth. Thus, if you had a row of
253 * bit depth 4, but the pixels only had values from 0 to 7, you
254 * would pass 3 as bit_depth, and this routine would translate the
258 png_do_shift(png_row_infop row_info, png_bytep row,
259 png_const_color_8p bit_depth)
261 png_debug(1, "in png_do_shift");
263 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
265 int shift_start[4], shift_dec[4];
268 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
270 shift_start[channels] = row_info->bit_depth - bit_depth->red;
271 shift_dec[channels] = bit_depth->red;
274 shift_start[channels] = row_info->bit_depth - bit_depth->green;
275 shift_dec[channels] = bit_depth->green;
278 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
279 shift_dec[channels] = bit_depth->blue;
285 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
286 shift_dec[channels] = bit_depth->gray;
290 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
292 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
293 shift_dec[channels] = bit_depth->alpha;
297 /* With low row depths, could only be grayscale, so one channel */
298 if (row_info->bit_depth < 8)
303 png_size_t row_bytes = row_info->rowbytes;
305 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
308 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
314 for (i = 0; i < row_bytes; i++, bp++)
322 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
325 *bp |= (png_byte)((v << j) & 0xff);
328 *bp |= (png_byte)((v >> (-j)) & mask);
333 else if (row_info->bit_depth == 8)
337 png_uint_32 istop = channels * row_info->width;
339 for (i = 0; i < istop; i++, bp++)
344 int c = (int)(i%channels);
349 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
352 *bp |= (png_byte)((v << j) & 0xff);
355 *bp |= (png_byte)((v >> (-j)) & 0xff);
364 png_uint_32 istop = channels * row_info->width;
366 for (bp = row, i = 0; i < istop; i++)
368 int c = (int)(i%channels);
369 png_uint_16 value, v;
372 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
375 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
378 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
381 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
383 *bp++ = (png_byte)(value >> 8);
384 *bp++ = (png_byte)(value & 0xff);
391 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
393 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
395 png_debug(1, "in png_do_write_swap_alpha");
398 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
400 if (row_info->bit_depth == 8)
402 /* This converts from ARGB to RGBA */
405 png_uint_32 row_width = row_info->width;
407 for (i = 0, sp = dp = row; i < row_width; i++)
409 png_byte save = *(sp++);
417 #ifdef PNG_WRITE_16BIT_SUPPORTED
420 /* This converts from AARRGGBB to RRGGBBAA */
423 png_uint_32 row_width = row_info->width;
425 for (i = 0, sp = dp = row; i < row_width; i++)
440 #endif /* PNG_WRITE_16BIT_SUPPORTED */
443 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
445 if (row_info->bit_depth == 8)
447 /* This converts from AG to GA */
450 png_uint_32 row_width = row_info->width;
452 for (i = 0, sp = dp = row; i < row_width; i++)
454 png_byte save = *(sp++);
460 #ifdef PNG_WRITE_16BIT_SUPPORTED
463 /* This converts from AAGG to GGAA */
466 png_uint_32 row_width = row_info->width;
468 for (i = 0, sp = dp = row; i < row_width; i++)
479 #endif /* PNG_WRITE_16BIT_SUPPORTED */
485 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
487 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
489 png_debug(1, "in png_do_write_invert_alpha");
492 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
494 if (row_info->bit_depth == 8)
496 /* This inverts the alpha channel in RGBA */
499 png_uint_32 row_width = row_info->width;
501 for (i = 0, sp = dp = row; i < row_width; i++)
509 *(dp++) = (png_byte)(255 - *(sp++));
513 #ifdef PNG_WRITE_16BIT_SUPPORTED
516 /* This inverts the alpha channel in RRGGBBAA */
519 png_uint_32 row_width = row_info->width;
521 for (i = 0, sp = dp = row; i < row_width; i++)
532 *(dp++) = (png_byte)(255 - *(sp++));
533 *(dp++) = (png_byte)(255 - *(sp++));
536 #endif /* PNG_WRITE_16BIT_SUPPORTED */
539 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
541 if (row_info->bit_depth == 8)
543 /* This inverts the alpha channel in GA */
546 png_uint_32 row_width = row_info->width;
548 for (i = 0, sp = dp = row; i < row_width; i++)
551 *(dp++) = (png_byte)(255 - *(sp++));
555 #ifdef PNG_WRITE_16BIT_SUPPORTED
558 /* This inverts the alpha channel in GGAA */
561 png_uint_32 row_width = row_info->width;
563 for (i = 0, sp = dp = row; i < row_width; i++)
570 *(dp++) = (png_byte)(255 - *(sp++));
571 *(dp++) = (png_byte)(255 - *(sp++));
574 #endif /* PNG_WRITE_16BIT_SUPPORTED */
579 #endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */
581 #ifdef PNG_MNG_FEATURES_SUPPORTED
582 /* Undoes intrapixel differencing */
584 png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
586 png_debug(1, "in png_do_write_intrapixel");
588 if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
591 png_uint_32 row_width = row_info->width;
592 if (row_info->bit_depth == 8)
597 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
600 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
606 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
608 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
609 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
613 #ifdef PNG_WRITE_16BIT_SUPPORTED
614 else if (row_info->bit_depth == 16)
619 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
622 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
628 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
630 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
631 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
632 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
633 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
634 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
635 *(rp ) = (png_byte)((red >> 8) & 0xff);
636 *(rp + 1) = (png_byte)(red & 0xff);
637 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
638 *(rp + 5) = (png_byte)(blue & 0xff);
641 #endif /* PNG_WRITE_16BIT_SUPPORTED */
644 #endif /* PNG_MNG_FEATURES_SUPPORTED */
645 #endif /* PNG_WRITE_SUPPORTED */