/* pngwtran.c - transforms the data in a row for PNG writers
*
- * libpng 1.0.1
- * For conditions of distribution and use, see copyright notice in png.h
- * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
- * Copyright (c) 1996, 1997 Andreas Dilger
- * Copyright (c) 1998, Glenn Randers-Pehrson
- * March 15, 1998
+ * Last changed in libpng 1.4.1 [February 25, 2010]
+ * Copyright (c) 1998-2010 Glenn Randers-Pehrson
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
+ *
+ * This code is released under the libpng license.
+ * For conditions of distribution and use, see the disclaimer
+ * and license in png.h
*/
-#define PNG_INTERNAL
-#include "../png/png.h"
+#define PNG_NO_PEDANTIC_WARNINGS
+#include "png.h"
+#ifdef PNG_WRITE_SUPPORTED
+#include "pngpriv.h"
-/* Transform the data according to the users wishes. The order of
+/* Transform the data according to the user's wishes. The order of
* transformations is significant.
*/
-void
+void /* PRIVATE */
png_do_write_transformations(png_structp png_ptr)
{
- png_debug(1, "in png_do_write_transformations\n");
+ png_debug(1, "in png_do_write_transformations");
+
+ if (png_ptr == NULL)
+ return;
-#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
+#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
if (png_ptr->transformations & PNG_USER_TRANSFORM)
- if(png_ptr->write_user_transform_fn != NULL)
- (*(png_ptr->write_user_transform_fn)) /* user write transform function */
+ if (png_ptr->write_user_transform_fn != NULL)
+ (*(png_ptr->write_user_transform_fn)) /* User write transform
+ function */
(png_ptr, /* png_ptr */
&(png_ptr->row_info), /* row_info: */
/* png_uint_32 width; width of row */
/* png_byte pixel_depth; bits per pixel (depth*channels) */
png_ptr->row_buf + 1); /* start of pixel data for row */
#endif
-#if defined(PNG_WRITE_FILLER_SUPPORTED)
+#ifdef PNG_WRITE_FILLER_SUPPORTED
if (png_ptr->transformations & PNG_FILLER)
png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
png_ptr->flags);
#endif
-#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
+#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
if (png_ptr->transformations & PNG_PACKSWAP)
png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif
-#if defined(PNG_WRITE_PACK_SUPPORTED)
+#ifdef PNG_WRITE_PACK_SUPPORTED
if (png_ptr->transformations & PNG_PACK)
png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
(png_uint_32)png_ptr->bit_depth);
#endif
-#if defined(PNG_WRITE_SHIFT_SUPPORTED)
+#ifdef PNG_WRITE_SWAP_SUPPORTED
+ if (png_ptr->transformations & PNG_SWAP_BYTES)
+ png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
+#endif
+#ifdef PNG_WRITE_SHIFT_SUPPORTED
if (png_ptr->transformations & PNG_SHIFT)
png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
&(png_ptr->shift));
#endif
-#if defined(PNG_WRITE_SWAP_SUPPORTED)
- if (png_ptr->transformations & PNG_SWAP_BYTES)
- png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
+#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
+ if (png_ptr->transformations & PNG_SWAP_ALPHA)
+ png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif
-#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
+#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
if (png_ptr->transformations & PNG_INVERT_ALPHA)
png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif
-#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
- if (png_ptr->transformations & PNG_SWAP_ALPHA)
- png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-#if defined(PNG_WRITE_BGR_SUPPORTED)
+#ifdef PNG_WRITE_BGR_SUPPORTED
if (png_ptr->transformations & PNG_BGR)
png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif
-#if defined(PNG_WRITE_INVERT_SUPPORTED)
+#ifdef PNG_WRITE_INVERT_SUPPORTED
if (png_ptr->transformations & PNG_INVERT_MONO)
png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif
}
-#if defined(PNG_WRITE_PACK_SUPPORTED)
+#ifdef PNG_WRITE_PACK_SUPPORTED
/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
* row_info bit depth should be 8 (one pixel per byte). The channels
* should be 1 (this only happens on grayscale and paletted images).
*/
-void
+void /* PRIVATE */
png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
{
- png_debug(1, "in png_do_pack\n");
+ png_debug(1, "in png_do_pack");
+
if (row_info->bit_depth == 8 &&
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
- row != NULL && row_info != NULL &&
-#endif
row_info->channels == 1)
{
switch ((int)bit_depth)
png_bytep sp, dp;
int mask, v;
png_uint_32 i;
+ png_uint_32 row_width = row_info->width;
sp = row;
dp = row;
mask = 0x80;
v = 0;
- for (i = 0; i < row_info->width; i++)
+
+ for (i = 0; i < row_width; i++)
{
if (*sp != 0)
v |= mask;
png_bytep sp, dp;
int shift, v;
png_uint_32 i;
+ png_uint_32 row_width = row_info->width;
sp = row;
dp = row;
shift = 6;
v = 0;
- for (i = 0; i < row_info->width; i++)
+ for (i = 0; i < row_width; i++)
{
png_byte value;
- value = (png_byte)(*sp & 0x3);
+ value = (png_byte)(*sp & 0x03);
v |= (value << shift);
if (shift == 0)
{
png_bytep sp, dp;
int shift, v;
png_uint_32 i;
+ png_uint_32 row_width = row_info->width;
sp = row;
dp = row;
shift = 4;
v = 0;
- for (i = 0; i < row_info->width; i++)
+ for (i = 0; i < row_width; i++)
{
png_byte value;
- value = (png_byte)(*sp & 0xf);
+ value = (png_byte)(*sp & 0x0f);
v |= (value << shift);
if (shift == 0)
}
row_info->bit_depth = (png_byte)bit_depth;
row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
- row_info->rowbytes =
- ((row_info->width * row_info->pixel_depth + 7) >> 3);
+ row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
+ row_info->width);
}
}
#endif
-#if defined(PNG_WRITE_SHIFT_SUPPORTED)
+#ifdef PNG_WRITE_SHIFT_SUPPORTED
/* Shift pixel values to take advantage of whole range. Pass the
* true number of bits in bit_depth. The row should be packed
* according to row_info->bit_depth. Thus, if you had a row of
* would pass 3 as bit_depth, and this routine would translate the
* data to 0 to 15.
*/
-void
+void /* PRIVATE */
png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
{
- png_debug(1, "in png_do_shift\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
- if (row != NULL && row_info != NULL &&
-#else
+ png_debug(1, "in png_do_shift");
+
if (
-#endif
row_info->color_type != PNG_COLOR_TYPE_PALETTE)
{
int shift_start[4], shift_dec[4];
- png_uint_32 channels;
+ int channels = 0;
- channels = 0;
if (row_info->color_type & PNG_COLOR_MASK_COLOR)
{
shift_start[channels] = row_info->bit_depth - bit_depth->red;
channels++;
}
- /* with low row depths, could only be grayscale, so one channel */
+ /* With low row depths, could only be grayscale, so one channel */
if (row_info->bit_depth < 8)
{
- png_bytep bp;
+ png_bytep bp = row;
png_uint_32 i;
png_byte mask;
+ png_uint_32 row_bytes = row_info->rowbytes;
if (bit_depth->gray == 1 && row_info->bit_depth == 2)
mask = 0x55;
else
mask = 0xff;
- for (bp = row, i = 0; i < row_info->rowbytes; i++, bp++)
+ for (i = 0; i < row_bytes; i++, bp++)
{
png_uint_16 v;
int j;
}
else if (row_info->bit_depth == 8)
{
- png_bytep bp;
+ png_bytep bp = row;
png_uint_32 i;
+ png_uint_32 istop = channels * row_info->width;
- for (bp = row, i = 0; i < row_info->width; i++)
+ for (i = 0; i < istop; i++, bp++)
{
- png_uint_32 c;
- for (c = 0; c < channels; c++, bp++)
- {
- png_uint_16 v;
- int j;
+ png_uint_16 v;
+ int j;
+ int c = (int)(i%channels);
- v = *bp;
- *bp = 0;
- for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
- {
- if (j > 0)
- *bp |= (png_byte)((v << j) & 0xff);
- else
- *bp |= (png_byte)((v >> (-j)) & 0xff);
- }
+ v = *bp;
+ *bp = 0;
+ for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
+ {
+ if (j > 0)
+ *bp |= (png_byte)((v << j) & 0xff);
+ else
+ *bp |= (png_byte)((v >> (-j)) & 0xff);
}
}
}
{
png_bytep bp;
png_uint_32 i;
+ png_uint_32 istop = channels * row_info->width;
- for (bp = row, i = 0; i < row_info->width * row_info->channels; i++)
+ for (bp = row, i = 0; i < istop; i++)
{
- png_uint_32 c;
+ int c = (int)(i%channels);
+ png_uint_16 value, v;
+ int j;
- for (c = 0; c < channels; c++, bp += 2)
+ v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
+ value = 0;
+ for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
{
- png_uint_16 value, v;
- int j;
-
- v = ((png_uint_16)(*bp) << 8) + *(bp + 1);
- value = 0;
- for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
- {
- if (j > 0)
- value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
- else
- value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
- }
- *bp = (png_byte)(value >> 8);
- *(bp + 1) = (png_byte)(value & 0xff);
+ if (j > 0)
+ value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
+ else
+ value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
}
+ *bp++ = (png_byte)(value >> 8);
+ *bp++ = (png_byte)(value & 0xff);
}
}
}
}
#endif
-#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
-void
+#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
+void /* PRIVATE */
png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
{
- png_debug(1, "in png_do_write_swap_alpha\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
- if (row != NULL && row_info != NULL)
-#endif
+ png_debug(1, "in png_do_write_swap_alpha");
+
{
if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
{
if (row_info->bit_depth == 8)
{
png_bytep sp, dp;
- png_byte save;
png_uint_32 i;
-
- for (i = 0, sp = dp = row; i < row_info->width; i++)
+ png_uint_32 row_width = row_info->width;
+ for (i = 0, sp = dp = row; i < row_width; i++)
{
- save = *(sp++);
+ png_byte save = *(sp++);
*(dp++) = *(sp++);
*(dp++) = *(sp++);
*(dp++) = *(sp++);
else
{
png_bytep sp, dp;
- png_byte save[2];
png_uint_32 i;
+ png_uint_32 row_width = row_info->width;
- for (i = 0, sp = dp = row; i < row_info->width; i++)
+ for (i = 0, sp = dp = row; i < row_width; i++)
{
+ png_byte save[2];
save[0] = *(sp++);
save[1] = *(sp++);
*(dp++) = *(sp++);
if (row_info->bit_depth == 8)
{
png_bytep sp, dp;
- png_byte save;
png_uint_32 i;
+ png_uint_32 row_width = row_info->width;
- for (i = 0, sp = dp = row; i < row_info->width; i++)
+ for (i = 0, sp = dp = row; i < row_width; i++)
{
- save = *(sp++);
+ png_byte save = *(sp++);
*(dp++) = *(sp++);
*(dp++) = save;
}
else
{
png_bytep sp, dp;
- png_byte save[2];
png_uint_32 i;
+ png_uint_32 row_width = row_info->width;
- for (i = 0, sp = dp = row; i < row_info->width; i++)
+ for (i = 0, sp = dp = row; i < row_width; i++)
{
+ png_byte save[2];
save[0] = *(sp++);
save[1] = *(sp++);
*(dp++) = *(sp++);
}
#endif
-#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
-void
+#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
+void /* PRIVATE */
png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
{
- png_debug(1, "in png_do_write_invert_alpha\n");
-#if defined(PNG_USELESS_TESTS_SUPPORTED)
- if (row != NULL && row_info != NULL)
-#endif
+ png_debug(1, "in png_do_write_invert_alpha");
+
{
if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
{
{
png_bytep sp, dp;
png_uint_32 i;
-
- for (i = 0, sp = dp = row; i < row_info->width; i++)
+ png_uint_32 row_width = row_info->width;
+ for (i = 0, sp = dp = row; i < row_width; i++)
{
+ /* Does nothing
*(dp++) = *(sp++);
*(dp++) = *(sp++);
*(dp++) = *(sp++);
- *(dp++) = 255 - *(sp++);
+ */
+ sp+=3; dp = sp;
+ *(dp++) = (png_byte)(255 - *(sp++));
}
}
/* This inverts the alpha channel in RRGGBBAA */
{
png_bytep sp, dp;
png_uint_32 i;
+ png_uint_32 row_width = row_info->width;
- for (i = 0, sp = dp = row; i < row_info->width; i++)
+ for (i = 0, sp = dp = row; i < row_width; i++)
{
+ /* Does nothing
*(dp++) = *(sp++);
*(dp++) = *(sp++);
*(dp++) = *(sp++);
*(dp++) = *(sp++);
*(dp++) = *(sp++);
*(dp++) = *(sp++);
- *(dp++) = 255 - *(sp++);
- *(dp++) = 255 - *(sp++);
+ */
+ sp+=6; dp = sp;
+ *(dp++) = (png_byte)(255 - *(sp++));
+ *(dp++) = (png_byte)(255 - *(sp++));
}
}
}
{
png_bytep sp, dp;
png_uint_32 i;
+ png_uint_32 row_width = row_info->width;
- for (i = 0, sp = dp = row; i < row_info->width; i++)
+ for (i = 0, sp = dp = row; i < row_width; i++)
{
*(dp++) = *(sp++);
- *(dp++) = 255 - *(sp++);
+ *(dp++) = (png_byte)(255 - *(sp++));
}
}
/* This inverts the alpha channel in GGAA */
{
png_bytep sp, dp;
png_uint_32 i;
+ png_uint_32 row_width = row_info->width;
- for (i = 0, sp = dp = row; i < row_info->width; i++)
+ for (i = 0, sp = dp = row; i < row_width; i++)
{
+ /* Does nothing
*(dp++) = *(sp++);
*(dp++) = *(sp++);
- *(dp++) = 255 - *(sp++);
- *(dp++) = 255 - *(sp++);
+ */
+ sp+=2; dp = sp;
+ *(dp++) = (png_byte)(255 - *(sp++));
+ *(dp++) = (png_byte)(255 - *(sp++));
}
}
}
}
}
#endif
+
+#ifdef PNG_MNG_FEATURES_SUPPORTED
+/* Undoes intrapixel differencing */
+void /* PRIVATE */
+png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
+{
+ png_debug(1, "in png_do_write_intrapixel");
+
+ if (
+ (row_info->color_type & PNG_COLOR_MASK_COLOR))
+ {
+ int bytes_per_pixel;
+ png_uint_32 row_width = row_info->width;
+ if (row_info->bit_depth == 8)
+ {
+ png_bytep rp;
+ png_uint_32 i;
+
+ if (row_info->color_type == PNG_COLOR_TYPE_RGB)
+ bytes_per_pixel = 3;
+ else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
+ bytes_per_pixel = 4;
+ else
+ return;
+
+ for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
+ {
+ *(rp) = (png_byte)((*rp - *(rp+1))&0xff);
+ *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
+ }
+ }
+ else if (row_info->bit_depth == 16)
+ {
+ png_bytep rp;
+ png_uint_32 i;
+
+ if (row_info->color_type == PNG_COLOR_TYPE_RGB)
+ bytes_per_pixel = 6;
+ else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
+ bytes_per_pixel = 8;
+ else
+ return;
+
+ for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
+ {
+ png_uint_32 s0 = (*(rp ) << 8) | *(rp+1);
+ png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3);
+ png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5);
+ png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
+ png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
+ *(rp ) = (png_byte)((red >> 8) & 0xff);
+ *(rp+1) = (png_byte)(red & 0xff);
+ *(rp+4) = (png_byte)((blue >> 8) & 0xff);
+ *(rp+5) = (png_byte)(blue & 0xff);
+ }
+ }
+ }
+}
+#endif /* PNG_MNG_FEATURES_SUPPORTED */
+#endif /* PNG_WRITE_SUPPORTED */