2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * libpng 1.0.9 - January 31, 2001
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2001 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
14 /* Transform the data according to the user's wishes. The order of
15 * transformations is significant.
18 png_do_write_transformations(png_structp png_ptr)
20 png_debug(1, "in png_do_write_transformations\n");
25 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
26 if (png_ptr->transformations & PNG_USER_TRANSFORM)
27 if(png_ptr->write_user_transform_fn != NULL)
28 (*(png_ptr->write_user_transform_fn)) /* user write transform function */
29 (png_ptr, /* png_ptr */
30 &(png_ptr->row_info), /* row_info: */
31 /* png_uint_32 width; width of row */
32 /* png_uint_32 rowbytes; number of bytes in row */
33 /* png_byte color_type; color type of pixels */
34 /* png_byte bit_depth; bit depth of samples */
35 /* png_byte channels; number of channels (1-4) */
36 /* png_byte pixel_depth; bits per pixel (depth*channels) */
37 png_ptr->row_buf + 1); /* start of pixel data for row */
39 #if defined(PNG_WRITE_FILLER_SUPPORTED)
40 if (png_ptr->transformations & PNG_FILLER)
41 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
44 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
45 if (png_ptr->transformations & PNG_PACKSWAP)
46 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
48 #if defined(PNG_WRITE_PACK_SUPPORTED)
49 if (png_ptr->transformations & PNG_PACK)
50 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
51 (png_uint_32)png_ptr->bit_depth);
53 #if defined(PNG_WRITE_SWAP_SUPPORTED)
54 if (png_ptr->transformations & PNG_SWAP_BYTES)
55 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
57 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
58 if (png_ptr->transformations & PNG_SHIFT)
59 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
62 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
63 if (png_ptr->transformations & PNG_INVERT_ALPHA)
64 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
66 #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
67 if (png_ptr->transformations & PNG_SWAP_ALPHA)
68 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
70 #if defined(PNG_WRITE_BGR_SUPPORTED)
71 if (png_ptr->transformations & PNG_BGR)
72 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
74 #if defined(PNG_WRITE_INVERT_SUPPORTED)
75 if (png_ptr->transformations & PNG_INVERT_MONO)
76 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
80 #if defined(PNG_WRITE_PACK_SUPPORTED)
81 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
82 * row_info bit depth should be 8 (one pixel per byte). The channels
83 * should be 1 (this only happens on grayscale and paletted images).
86 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
88 png_debug(1, "in png_do_pack\n");
89 if (row_info->bit_depth == 8 &&
90 #if defined(PNG_USELESS_TESTS_SUPPORTED)
91 row != NULL && row_info != NULL &&
93 row_info->channels == 1)
95 switch ((int)bit_depth)
102 png_uint_32 row_width = row_info->width;
109 for (i = 0; i < row_width; i++)
133 png_uint_32 row_width = row_info->width;
139 for (i = 0; i < row_width; i++)
143 value = (png_byte)(*sp & 0x03);
144 v |= (value << shift);
165 png_uint_32 row_width = row_info->width;
171 for (i = 0; i < row_width; i++)
175 value = (png_byte)(*sp & 0x0f);
176 v |= (value << shift);
195 row_info->bit_depth = (png_byte)bit_depth;
196 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
198 ((row_info->width * row_info->pixel_depth + 7) >> 3);
203 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
204 /* Shift pixel values to take advantage of whole range. Pass the
205 * true number of bits in bit_depth. The row should be packed
206 * according to row_info->bit_depth. Thus, if you had a row of
207 * bit depth 4, but the pixels only had values from 0 to 7, you
208 * would pass 3 as bit_depth, and this routine would translate the
212 png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
214 png_debug(1, "in png_do_shift\n");
215 #if defined(PNG_USELESS_TESTS_SUPPORTED)
216 if (row != NULL && row_info != NULL &&
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 #if defined(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\n");
339 #if defined(PNG_USELESS_TESTS_SUPPORTED)
340 if (row != NULL && row_info != NULL)
343 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
345 /* This converts from ARGB to RGBA */
346 if (row_info->bit_depth == 8)
350 png_uint_32 row_width = row_info->width;
351 for (i = 0, sp = dp = row; i < row_width; i++)
353 png_byte save = *(sp++);
360 /* This converts from AARRGGBB to RRGGBBAA */
365 png_uint_32 row_width = row_info->width;
367 for (i = 0, sp = dp = row; i < row_width; i++)
383 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
385 /* This converts from AG to GA */
386 if (row_info->bit_depth == 8)
390 png_uint_32 row_width = row_info->width;
392 for (i = 0, sp = dp = row; i < row_width; i++)
394 png_byte save = *(sp++);
399 /* This converts from AAGG to GGAA */
404 png_uint_32 row_width = row_info->width;
406 for (i = 0, sp = dp = row; i < row_width; i++)
422 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
424 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
426 png_debug(1, "in png_do_write_invert_alpha\n");
427 #if defined(PNG_USELESS_TESTS_SUPPORTED)
428 if (row != NULL && row_info != NULL)
431 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
433 /* This inverts the alpha channel in RGBA */
434 if (row_info->bit_depth == 8)
438 png_uint_32 row_width = row_info->width;
439 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++)
462 *(dp++) = (png_byte)(255 - *(sp++));
463 *(dp++) = (png_byte)(255 - *(sp++));
467 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
469 /* This inverts the alpha channel in GA */
470 if (row_info->bit_depth == 8)
474 png_uint_32 row_width = row_info->width;
476 for (i = 0, sp = dp = row; i < row_width; i++)
479 *(dp++) = (png_byte)(255 - *(sp++));
482 /* This inverts the alpha channel in GGAA */
487 png_uint_32 row_width = row_info->width;
489 for (i = 0, sp = dp = row; i < row_width; i++)
493 *(dp++) = (png_byte)(255 - *(sp++));
494 *(dp++) = (png_byte)(255 - *(sp++));
502 #if defined(PNG_MNG_FEATURES_SUPPORTED)
503 /* undoes intrapixel differencing */
505 png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
507 png_debug(1, "in png_do_write_intrapixel\n");
509 #if defined(PNG_USELESS_TESTS_SUPPORTED)
510 row != NULL && row_info != NULL &&
512 (row_info->color_type & PNG_COLOR_MASK_COLOR))
515 png_uint_32 row_width = row_info->width;
516 if (row_info->bit_depth == 8)
521 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
523 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
528 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
530 *(rp) = (png_byte)((*rp - *(rp+1))&0xff);
531 *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
534 else if (row_info->bit_depth == 16)
539 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
541 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
546 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
548 png_uint_32 s0=*(rp )<<8 | *(rp+1);
549 png_uint_32 s1=*(rp+2)<<8 | *(rp+3);
550 png_uint_32 s2=*(rp+4)<<8 | *(rp+5);
551 png_uint_32 red=(s0-s1)&0xffff;
552 png_uint_32 blue=(s2-s1)&0xffff;
553 *(rp ) = (png_byte)((red>>8)&0xff);
554 *(rp+1) = (png_byte)(red&0xff);
555 *(rp+4) = (png_byte)((blue>>8)&0xff);
556 *(rp+5) = (png_byte)(blue&0xff);
561 #endif /* PNG_MNG_FEATURES_SUPPORTED */