2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * Last changed in libpng 1.6.17 [March 26, 2015]
5 * Copyright (c) 1998-2015 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
17 #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
19 #ifdef PNG_WRITE_PACK_SUPPORTED
20 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
21 * row_info bit depth should be 8 (one pixel per byte). The channels
22 * should be 1 (this only happens on grayscale and paletted images).
25 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
27 png_debug(1, "in png_do_pack");
29 if (row_info->bit_depth == 8 &&
30 row_info->channels == 1)
32 switch ((int)bit_depth)
39 png_uint_32 row_width = row_info->width;
46 for (i = 0; i < row_width; i++)
76 png_uint_32 row_width = row_info->width;
83 for (i = 0; i < row_width; i++)
87 value = (png_byte)(*sp & 0x03);
88 v |= (value << shift);
115 png_uint_32 row_width = row_info->width;
122 for (i = 0; i < row_width; i++)
126 value = (png_byte)(*sp & 0x0f);
127 v |= (value << shift);
153 row_info->bit_depth = (png_byte)bit_depth;
154 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
155 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
161 #ifdef PNG_WRITE_SHIFT_SUPPORTED
162 /* Shift pixel values to take advantage of whole range. Pass the
163 * true number of bits in bit_depth. The row should be packed
164 * according to row_info->bit_depth. Thus, if you had a row of
165 * bit depth 4, but the pixels only had values from 0 to 7, you
166 * would pass 3 as bit_depth, and this routine would translate the
170 png_do_shift(png_row_infop row_info, png_bytep row,
171 png_const_color_8p bit_depth)
173 png_debug(1, "in png_do_shift");
175 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
177 int shift_start[4], shift_dec[4];
180 if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
182 shift_start[channels] = row_info->bit_depth - bit_depth->red;
183 shift_dec[channels] = bit_depth->red;
186 shift_start[channels] = row_info->bit_depth - bit_depth->green;
187 shift_dec[channels] = bit_depth->green;
190 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
191 shift_dec[channels] = bit_depth->blue;
197 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
198 shift_dec[channels] = bit_depth->gray;
202 if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
204 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
205 shift_dec[channels] = bit_depth->alpha;
209 /* With low row depths, could only be grayscale, so one channel */
210 if (row_info->bit_depth < 8)
215 png_size_t row_bytes = row_info->rowbytes;
217 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
220 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
226 for (i = 0; i < row_bytes; i++, bp++)
234 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
240 out |= (v >> (-j)) & mask;
243 *bp = (png_byte)(out & 0xff);
247 else if (row_info->bit_depth == 8)
251 png_uint_32 istop = channels * row_info->width;
253 for (i = 0; i < istop; i++, bp++)
256 const unsigned int c = i%channels;
263 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
272 *bp = (png_byte)(out & 0xff);
280 png_uint_32 istop = channels * row_info->width;
282 for (bp = row, i = 0; i < istop; i++)
284 const unsigned int c = i%channels;
286 unsigned int value, v;
288 v = png_get_uint_16(bp);
291 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
299 *bp++ = (png_byte)((value >> 8) & 0xff);
300 *bp++ = (png_byte)(value & 0xff);
307 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
309 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
311 png_debug(1, "in png_do_write_swap_alpha");
314 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
316 if (row_info->bit_depth == 8)
318 /* This converts from ARGB to RGBA */
321 png_uint_32 row_width = row_info->width;
323 for (i = 0, sp = dp = row; i < row_width; i++)
325 png_byte save = *(sp++);
333 #ifdef PNG_WRITE_16BIT_SUPPORTED
336 /* This converts from AARRGGBB to RRGGBBAA */
339 png_uint_32 row_width = row_info->width;
341 for (i = 0, sp = dp = row; i < row_width; i++)
356 #endif /* WRITE_16BIT */
359 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
361 if (row_info->bit_depth == 8)
363 /* This converts from AG to GA */
366 png_uint_32 row_width = row_info->width;
368 for (i = 0, sp = dp = row; i < row_width; i++)
370 png_byte save = *(sp++);
376 #ifdef PNG_WRITE_16BIT_SUPPORTED
379 /* This converts from AAGG to GGAA */
382 png_uint_32 row_width = row_info->width;
384 for (i = 0, sp = dp = row; i < row_width; i++)
395 #endif /* WRITE_16BIT */
401 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
403 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
405 png_debug(1, "in png_do_write_invert_alpha");
408 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
410 if (row_info->bit_depth == 8)
412 /* This inverts the alpha channel in RGBA */
415 png_uint_32 row_width = row_info->width;
417 for (i = 0, sp = dp = row; i < row_width; i++)
425 *(dp++) = (png_byte)(255 - *(sp++));
429 #ifdef PNG_WRITE_16BIT_SUPPORTED
432 /* This inverts the alpha channel in RRGGBBAA */
435 png_uint_32 row_width = row_info->width;
437 for (i = 0, sp = dp = row; i < row_width; i++)
448 *(dp++) = (png_byte)(255 - *(sp++));
449 *(dp++) = (png_byte)(255 - *(sp++));
452 #endif /* WRITE_16BIT */
455 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
457 if (row_info->bit_depth == 8)
459 /* This inverts the alpha channel in GA */
462 png_uint_32 row_width = row_info->width;
464 for (i = 0, sp = dp = row; i < row_width; i++)
467 *(dp++) = (png_byte)(255 - *(sp++));
471 #ifdef PNG_WRITE_16BIT_SUPPORTED
474 /* This inverts the alpha channel in GGAA */
477 png_uint_32 row_width = row_info->width;
479 for (i = 0, sp = dp = row; i < row_width; i++)
486 *(dp++) = (png_byte)(255 - *(sp++));
487 *(dp++) = (png_byte)(255 - *(sp++));
490 #endif /* WRITE_16BIT */
496 /* Transform the data according to the user's wishes. The order of
497 * transformations is significant.
500 png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
502 png_debug(1, "in png_do_write_transformations");
507 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
508 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
509 if (png_ptr->write_user_transform_fn != NULL)
510 (*(png_ptr->write_user_transform_fn)) /* User write transform
512 (png_ptr, /* png_ptr */
513 row_info, /* row_info: */
514 /* png_uint_32 width; width of row */
515 /* png_size_t rowbytes; number of bytes in row */
516 /* png_byte color_type; color type of pixels */
517 /* png_byte bit_depth; bit depth of samples */
518 /* png_byte channels; number of channels (1-4) */
519 /* png_byte pixel_depth; bits per pixel (depth*channels) */
520 png_ptr->row_buf + 1); /* start of pixel data for row */
523 #ifdef PNG_WRITE_FILLER_SUPPORTED
524 if ((png_ptr->transformations & PNG_FILLER) != 0)
525 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
526 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
529 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
530 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
531 png_do_packswap(row_info, png_ptr->row_buf + 1);
534 #ifdef PNG_WRITE_PACK_SUPPORTED
535 if ((png_ptr->transformations & PNG_PACK) != 0)
536 png_do_pack(row_info, png_ptr->row_buf + 1,
537 (png_uint_32)png_ptr->bit_depth);
540 #ifdef PNG_WRITE_SWAP_SUPPORTED
541 # ifdef PNG_16BIT_SUPPORTED
542 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
543 png_do_swap(row_info, png_ptr->row_buf + 1);
547 #ifdef PNG_WRITE_SHIFT_SUPPORTED
548 if ((png_ptr->transformations & PNG_SHIFT) != 0)
549 png_do_shift(row_info, png_ptr->row_buf + 1,
553 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
554 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
555 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
558 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
559 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
560 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
563 #ifdef PNG_WRITE_BGR_SUPPORTED
564 if ((png_ptr->transformations & PNG_BGR) != 0)
565 png_do_bgr(row_info, png_ptr->row_buf + 1);
568 #ifdef PNG_WRITE_INVERT_SUPPORTED
569 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
570 png_do_invert(row_info, png_ptr->row_buf + 1);
573 #endif /* WRITE_TRANSFORMS */