2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * Last changed in libpng 1.2.43 [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
15 #define PNG_NO_PEDANTIC_WARNINGS
17 #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 #ifdef PNG_USELESS_TESTS_SUPPORTED
98 row != NULL && row_info != NULL &&
100 row_info->channels == 1)
102 switch ((int)bit_depth)
109 png_uint_32 row_width = row_info->width;
116 for (i = 0; i < row_width; i++)
140 png_uint_32 row_width = row_info->width;
146 for (i = 0; i < row_width; i++)
150 value = (png_byte)(*sp & 0x03);
151 v |= (value << shift);
172 png_uint_32 row_width = row_info->width;
178 for (i = 0; i < row_width; i++)
182 value = (png_byte)(*sp & 0x0f);
183 v |= (value << shift);
202 row_info->bit_depth = (png_byte)bit_depth;
203 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
204 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
210 #ifdef PNG_WRITE_SHIFT_SUPPORTED
211 /* Shift pixel values to take advantage of whole range. Pass the
212 * true number of bits in bit_depth. The row should be packed
213 * according to row_info->bit_depth. Thus, if you had a row of
214 * bit depth 4, but the pixels only had values from 0 to 7, you
215 * would pass 3 as bit_depth, and this routine would translate the
219 png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
221 png_debug(1, "in png_do_shift");
223 #ifdef PNG_USELESS_TESTS_SUPPORTED
224 if (row != NULL && row_info != NULL &&
228 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
230 int shift_start[4], shift_dec[4];
233 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
235 shift_start[channels] = row_info->bit_depth - bit_depth->red;
236 shift_dec[channels] = bit_depth->red;
238 shift_start[channels] = row_info->bit_depth - bit_depth->green;
239 shift_dec[channels] = bit_depth->green;
241 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
242 shift_dec[channels] = bit_depth->blue;
247 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
248 shift_dec[channels] = bit_depth->gray;
251 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
253 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
254 shift_dec[channels] = bit_depth->alpha;
258 /* With low row depths, could only be grayscale, so one channel */
259 if (row_info->bit_depth < 8)
264 png_uint_32 row_bytes = row_info->rowbytes;
266 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
268 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
273 for (i = 0; i < row_bytes; i++, bp++)
280 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
283 *bp |= (png_byte)((v << j) & 0xff);
285 *bp |= (png_byte)((v >> (-j)) & mask);
289 else if (row_info->bit_depth == 8)
293 png_uint_32 istop = channels * row_info->width;
295 for (i = 0; i < istop; i++, bp++)
300 int c = (int)(i%channels);
304 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
307 *bp |= (png_byte)((v << j) & 0xff);
309 *bp |= (png_byte)((v >> (-j)) & 0xff);
317 png_uint_32 istop = channels * row_info->width;
319 for (bp = row, i = 0; i < istop; i++)
321 int c = (int)(i%channels);
322 png_uint_16 value, v;
325 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
327 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
330 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
332 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
334 *bp++ = (png_byte)(value >> 8);
335 *bp++ = (png_byte)(value & 0xff);
342 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
344 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
346 png_debug(1, "in png_do_write_swap_alpha");
348 #ifdef PNG_USELESS_TESTS_SUPPORTED
349 if (row != NULL && row_info != NULL)
352 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
354 /* This converts from ARGB to RGBA */
355 if (row_info->bit_depth == 8)
359 png_uint_32 row_width = row_info->width;
360 for (i = 0, sp = dp = row; i < row_width; i++)
362 png_byte save = *(sp++);
369 /* This converts from AARRGGBB to RRGGBBAA */
374 png_uint_32 row_width = row_info->width;
376 for (i = 0, sp = dp = row; i < row_width; i++)
392 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
394 /* This converts from AG to GA */
395 if (row_info->bit_depth == 8)
399 png_uint_32 row_width = row_info->width;
401 for (i = 0, sp = dp = row; i < row_width; i++)
403 png_byte save = *(sp++);
408 /* This converts from AAGG to GGAA */
413 png_uint_32 row_width = row_info->width;
415 for (i = 0, sp = dp = row; i < row_width; i++)
431 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
433 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
435 png_debug(1, "in png_do_write_invert_alpha");
437 #ifdef PNG_USELESS_TESTS_SUPPORTED
438 if (row != NULL && row_info != NULL)
441 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
443 /* This inverts the alpha channel in RGBA */
444 if (row_info->bit_depth == 8)
448 png_uint_32 row_width = row_info->width;
449 for (i = 0, sp = dp = row; i < row_width; i++)
457 *(dp++) = (png_byte)(255 - *(sp++));
460 /* This inverts the alpha channel in RRGGBBAA */
465 png_uint_32 row_width = row_info->width;
467 for (i = 0, sp = dp = row; i < row_width; i++)
478 *(dp++) = (png_byte)(255 - *(sp++));
479 *(dp++) = (png_byte)(255 - *(sp++));
483 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
485 /* This inverts the alpha channel in GA */
486 if (row_info->bit_depth == 8)
490 png_uint_32 row_width = row_info->width;
492 for (i = 0, sp = dp = row; i < row_width; i++)
495 *(dp++) = (png_byte)(255 - *(sp++));
498 /* This inverts the alpha channel in GGAA */
503 png_uint_32 row_width = row_info->width;
505 for (i = 0, sp = dp = row; i < row_width; i++)
512 *(dp++) = (png_byte)(255 - *(sp++));
513 *(dp++) = (png_byte)(255 - *(sp++));
521 #ifdef PNG_MNG_FEATURES_SUPPORTED
522 /* Undoes intrapixel differencing */
524 png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
526 png_debug(1, "in png_do_write_intrapixel");
529 #ifdef PNG_USELESS_TESTS_SUPPORTED
530 row != NULL && row_info != NULL &&
532 (row_info->color_type & PNG_COLOR_MASK_COLOR))
535 png_uint_32 row_width = row_info->width;
536 if (row_info->bit_depth == 8)
541 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
543 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
548 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
550 *(rp) = (png_byte)((*rp - *(rp+1))&0xff);
551 *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
554 else if (row_info->bit_depth == 16)
559 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
561 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
566 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
568 png_uint_32 s0 = (*(rp ) << 8) | *(rp+1);
569 png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3);
570 png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5);
571 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
572 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
573 *(rp ) = (png_byte)((red >> 8) & 0xff);
574 *(rp+1) = (png_byte)(red & 0xff);
575 *(rp+4) = (png_byte)((blue >> 8) & 0xff);
576 *(rp+5) = (png_byte)(blue & 0xff);
581 #endif /* PNG_MNG_FEATURES_SUPPORTED */
582 #endif /* PNG_WRITE_SUPPORTED */