2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * Last changed in libpng 1.4.0 [January 3, 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 function */
34 (png_ptr, /* png_ptr */
35 &(png_ptr->row_info), /* row_info: */
36 /* png_uint_32 width; width of row */
37 /* png_uint_32 rowbytes; number of bytes in row */
38 /* png_byte color_type; color type of pixels */
39 /* png_byte bit_depth; bit depth of samples */
40 /* png_byte channels; number of channels (1-4) */
41 /* png_byte pixel_depth; bits per pixel (depth*channels) */
42 png_ptr->row_buf + 1); /* start of pixel data for row */
44 #ifdef PNG_WRITE_FILLER_SUPPORTED
45 if (png_ptr->transformations & PNG_FILLER)
46 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
49 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
50 if (png_ptr->transformations & PNG_PACKSWAP)
51 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
53 #ifdef PNG_WRITE_PACK_SUPPORTED
54 if (png_ptr->transformations & PNG_PACK)
55 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
56 (png_uint_32)png_ptr->bit_depth);
58 #ifdef PNG_WRITE_SWAP_SUPPORTED
59 if (png_ptr->transformations & PNG_SWAP_BYTES)
60 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
62 #ifdef PNG_WRITE_SHIFT_SUPPORTED
63 if (png_ptr->transformations & PNG_SHIFT)
64 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
67 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
68 if (png_ptr->transformations & PNG_SWAP_ALPHA)
69 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
71 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
72 if (png_ptr->transformations & PNG_INVERT_ALPHA)
73 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
75 #ifdef PNG_WRITE_BGR_SUPPORTED
76 if (png_ptr->transformations & PNG_BGR)
77 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
79 #ifdef PNG_WRITE_INVERT_SUPPORTED
80 if (png_ptr->transformations & PNG_INVERT_MONO)
81 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
85 #ifdef PNG_WRITE_PACK_SUPPORTED
86 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
87 * row_info bit depth should be 8 (one pixel per byte). The channels
88 * should be 1 (this only happens on grayscale and paletted images).
91 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
93 png_debug(1, "in png_do_pack");
95 if (row_info->bit_depth == 8 &&
96 row_info->channels == 1)
98 switch ((int)bit_depth)
105 png_uint_32 row_width = row_info->width;
112 for (i = 0; i < row_width; i++)
136 png_uint_32 row_width = row_info->width;
142 for (i = 0; i < row_width; i++)
146 value = (png_byte)(*sp & 0x03);
147 v |= (value << shift);
168 png_uint_32 row_width = row_info->width;
174 for (i = 0; i < row_width; i++)
178 value = (png_byte)(*sp & 0x0f);
179 v |= (value << shift);
198 row_info->bit_depth = (png_byte)bit_depth;
199 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
200 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
206 #ifdef PNG_WRITE_SHIFT_SUPPORTED
207 /* Shift pixel values to take advantage of whole range. Pass the
208 * true number of bits in bit_depth. The row should be packed
209 * according to row_info->bit_depth. Thus, if you had a row of
210 * bit depth 4, but the pixels only had values from 0 to 7, you
211 * would pass 3 as bit_depth, and this routine would translate the
215 png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
217 png_debug(1, "in png_do_shift");
220 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
222 int shift_start[4], shift_dec[4];
225 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
227 shift_start[channels] = row_info->bit_depth - bit_depth->red;
228 shift_dec[channels] = bit_depth->red;
230 shift_start[channels] = row_info->bit_depth - bit_depth->green;
231 shift_dec[channels] = bit_depth->green;
233 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
234 shift_dec[channels] = bit_depth->blue;
239 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
240 shift_dec[channels] = bit_depth->gray;
243 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
245 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
246 shift_dec[channels] = bit_depth->alpha;
250 /* With low row depths, could only be grayscale, so one channel */
251 if (row_info->bit_depth < 8)
256 png_uint_32 row_bytes = row_info->rowbytes;
258 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
260 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
265 for (i = 0; i < row_bytes; i++, bp++)
272 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
275 *bp |= (png_byte)((v << j) & 0xff);
277 *bp |= (png_byte)((v >> (-j)) & mask);
281 else if (row_info->bit_depth == 8)
285 png_uint_32 istop = channels * row_info->width;
287 for (i = 0; i < istop; i++, bp++)
292 int c = (int)(i%channels);
296 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
299 *bp |= (png_byte)((v << j) & 0xff);
301 *bp |= (png_byte)((v >> (-j)) & 0xff);
309 png_uint_32 istop = channels * row_info->width;
311 for (bp = row, i = 0; i < istop; i++)
313 int c = (int)(i%channels);
314 png_uint_16 value, v;
317 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
319 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
322 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
324 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
326 *bp++ = (png_byte)(value >> 8);
327 *bp++ = (png_byte)(value & 0xff);
334 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
336 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
338 png_debug(1, "in png_do_write_swap_alpha");
341 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
343 /* This converts from ARGB to RGBA */
344 if (row_info->bit_depth == 8)
348 png_uint_32 row_width = row_info->width;
349 for (i = 0, sp = dp = row; i < row_width; i++)
351 png_byte save = *(sp++);
358 /* This converts from AARRGGBB to RRGGBBAA */
363 png_uint_32 row_width = row_info->width;
365 for (i = 0, sp = dp = row; i < row_width; i++)
381 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
383 /* This converts from AG to GA */
384 if (row_info->bit_depth == 8)
388 png_uint_32 row_width = row_info->width;
390 for (i = 0, sp = dp = row; i < row_width; i++)
392 png_byte save = *(sp++);
397 /* This converts from AAGG to GGAA */
402 png_uint_32 row_width = row_info->width;
404 for (i = 0, sp = dp = row; i < row_width; i++)
420 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
422 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
424 png_debug(1, "in png_do_write_invert_alpha");
427 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
429 /* This inverts the alpha channel in RGBA */
430 if (row_info->bit_depth == 8)
434 png_uint_32 row_width = row_info->width;
435 for (i = 0, sp = dp = row; i < row_width; i++)
443 *(dp++) = (png_byte)(255 - *(sp++));
446 /* This inverts the alpha channel in RRGGBBAA */
451 png_uint_32 row_width = row_info->width;
453 for (i = 0, sp = dp = row; i < row_width; i++)
464 *(dp++) = (png_byte)(255 - *(sp++));
465 *(dp++) = (png_byte)(255 - *(sp++));
469 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
471 /* This inverts the alpha channel in GA */
472 if (row_info->bit_depth == 8)
476 png_uint_32 row_width = row_info->width;
478 for (i = 0, sp = dp = row; i < row_width; i++)
481 *(dp++) = (png_byte)(255 - *(sp++));
484 /* This inverts the alpha channel in GGAA */
489 png_uint_32 row_width = row_info->width;
491 for (i = 0, sp = dp = row; i < row_width; i++)
498 *(dp++) = (png_byte)(255 - *(sp++));
499 *(dp++) = (png_byte)(255 - *(sp++));
507 #ifdef PNG_MNG_FEATURES_SUPPORTED
508 /* Undoes intrapixel differencing */
510 png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
512 png_debug(1, "in png_do_write_intrapixel");
515 (row_info->color_type & PNG_COLOR_MASK_COLOR))
518 png_uint_32 row_width = row_info->width;
519 if (row_info->bit_depth == 8)
524 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
526 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
531 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
533 *(rp) = (png_byte)((*rp - *(rp+1))&0xff);
534 *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
537 else if (row_info->bit_depth == 16)
542 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
544 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
549 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
551 png_uint_32 s0 = (*(rp ) << 8) | *(rp+1);
552 png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3);
553 png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5);
554 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
555 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
556 *(rp ) = (png_byte)((red >> 8) & 0xff);
557 *(rp+1) = (png_byte)(red & 0xff);
558 *(rp+4) = (png_byte)((blue >> 8) & 0xff);
559 *(rp+5) = (png_byte)(blue & 0xff);
564 #endif /* PNG_MNG_FEATURES_SUPPORTED */
565 #endif /* PNG_WRITE_SUPPORTED */