1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
/* pngwtran.c - transforms the data in a row for PNG writers
|
1998-12-08 19:44:02 +00:00
|
|
|
*
|
2014-11-26 04:42:00 +00:00
|
|
|
* Last changed in libpng 1.6.15 [November 20, 2014]
|
2014-02-25 13:40:53 +00:00
|
|
|
* Copyright (c) 1998-2014 Glenn Randers-Pehrson
|
2000-08-19 02:06:43 +00:00
|
|
|
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
|
|
|
|
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
|
2009-11-11 01:39:31 +00:00
|
|
|
*
|
|
|
|
* This code is released under the libpng license.
|
|
|
|
* For conditions of distribution and use, see the disclaimer
|
|
|
|
* and license in png.h
|
1998-12-08 19:44:02 +00:00
|
|
|
*/
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2010-01-05 21:46:26 +00:00
|
|
|
#include "pngpriv.h"
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2012-03-04 03:18:12 +00:00
|
|
|
#ifdef PNG_WRITE_SUPPORTED
|
|
|
|
#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2010-01-05 21:46:26 +00:00
|
|
|
#ifdef PNG_WRITE_PACK_SUPPORTED
|
1998-12-08 19:44:02 +00:00
|
|
|
/* 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).
|
|
|
|
*/
|
2014-02-25 13:40:53 +00:00
|
|
|
static void
|
1998-03-28 02:44:41 +00:00
|
|
|
png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
|
|
|
|
{
|
2008-12-22 23:40:01 +00:00
|
|
|
png_debug(1, "in png_do_pack");
|
2010-01-05 21:46:26 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
if (row_info->bit_depth == 8 &&
|
1998-03-28 02:44:41 +00:00
|
|
|
row_info->channels == 1)
|
|
|
|
{
|
|
|
|
switch ((int)bit_depth)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
png_bytep sp, dp;
|
|
|
|
int mask, v;
|
|
|
|
png_uint_32 i;
|
1998-12-08 19:44:02 +00:00
|
|
|
png_uint_32 row_width = row_info->width;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
sp = row;
|
|
|
|
dp = row;
|
|
|
|
mask = 0x80;
|
|
|
|
v = 0;
|
2000-08-19 02:06:43 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (i = 0; i < row_width; i++)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
|
|
|
if (*sp != 0)
|
|
|
|
v |= mask;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
sp++;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
if (mask > 1)
|
|
|
|
mask >>= 1;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
mask = 0x80;
|
|
|
|
*dp = (png_byte)v;
|
|
|
|
dp++;
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
if (mask != 0x80)
|
|
|
|
*dp = (png_byte)v;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
png_bytep sp, dp;
|
|
|
|
int shift, v;
|
|
|
|
png_uint_32 i;
|
1998-12-08 19:44:02 +00:00
|
|
|
png_uint_32 row_width = row_info->width;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
sp = row;
|
|
|
|
dp = row;
|
|
|
|
shift = 6;
|
|
|
|
v = 0;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (i = 0; i < row_width; i++)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
1998-12-08 19:44:02 +00:00
|
|
|
png_byte value;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2000-08-19 02:06:43 +00:00
|
|
|
value = (png_byte)(*sp & 0x03);
|
1998-03-28 02:44:41 +00:00
|
|
|
v |= (value << shift);
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
if (shift == 0)
|
|
|
|
{
|
|
|
|
shift = 6;
|
|
|
|
*dp = (png_byte)v;
|
|
|
|
dp++;
|
|
|
|
v = 0;
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
else
|
|
|
|
shift -= 2;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
sp++;
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
if (shift != 6)
|
|
|
|
*dp = (png_byte)v;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
png_bytep sp, dp;
|
|
|
|
int shift, v;
|
|
|
|
png_uint_32 i;
|
1998-12-08 19:44:02 +00:00
|
|
|
png_uint_32 row_width = row_info->width;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
sp = row;
|
|
|
|
dp = row;
|
|
|
|
shift = 4;
|
|
|
|
v = 0;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (i = 0; i < row_width; i++)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
|
|
|
png_byte value;
|
|
|
|
|
2000-08-19 02:06:43 +00:00
|
|
|
value = (png_byte)(*sp & 0x0f);
|
1998-03-28 02:44:41 +00:00
|
|
|
v |= (value << shift);
|
|
|
|
|
|
|
|
if (shift == 0)
|
|
|
|
{
|
|
|
|
shift = 4;
|
|
|
|
*dp = (png_byte)v;
|
|
|
|
dp++;
|
|
|
|
v = 0;
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
else
|
|
|
|
shift -= 4;
|
|
|
|
|
|
|
|
sp++;
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
if (shift != 4)
|
|
|
|
*dp = (png_byte)v;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-04-12 19:45:24 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
row_info->bit_depth = (png_byte)bit_depth;
|
|
|
|
row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
|
2004-10-07 19:21:29 +00:00
|
|
|
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
|
2012-03-04 03:18:12 +00:00
|
|
|
row_info->width);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-01-05 21:46:26 +00:00
|
|
|
#ifdef PNG_WRITE_SHIFT_SUPPORTED
|
1998-12-08 19:44:02 +00:00
|
|
|
/* 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
|
|
|
|
* bit depth 4, but the pixels only had values from 0 to 7, you
|
|
|
|
* would pass 3 as bit_depth, and this routine would translate the
|
|
|
|
* data to 0 to 15.
|
|
|
|
*/
|
2014-02-25 13:40:53 +00:00
|
|
|
static void
|
2012-03-04 03:18:12 +00:00
|
|
|
png_do_shift(png_row_infop row_info, png_bytep row,
|
|
|
|
png_const_color_8p bit_depth)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2008-12-22 23:40:01 +00:00
|
|
|
png_debug(1, "in png_do_shift");
|
2010-01-05 21:46:26 +00:00
|
|
|
|
2012-03-04 03:18:12 +00:00
|
|
|
if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
|
|
|
int shift_start[4], shift_dec[4];
|
1998-12-08 19:44:02 +00:00
|
|
|
int channels = 0;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2014-11-26 04:42:00 +00:00
|
|
|
if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
|
|
|
shift_start[channels] = row_info->bit_depth - bit_depth->red;
|
|
|
|
shift_dec[channels] = bit_depth->red;
|
|
|
|
channels++;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
shift_start[channels] = row_info->bit_depth - bit_depth->green;
|
|
|
|
shift_dec[channels] = bit_depth->green;
|
|
|
|
channels++;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
shift_start[channels] = row_info->bit_depth - bit_depth->blue;
|
|
|
|
shift_dec[channels] = bit_depth->blue;
|
|
|
|
channels++;
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
shift_start[channels] = row_info->bit_depth - bit_depth->gray;
|
|
|
|
shift_dec[channels] = bit_depth->gray;
|
|
|
|
channels++;
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
2014-11-26 04:42:00 +00:00
|
|
|
if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
|
|
|
shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
|
|
|
|
shift_dec[channels] = bit_depth->alpha;
|
|
|
|
channels++;
|
|
|
|
}
|
|
|
|
|
2009-11-11 01:39:31 +00:00
|
|
|
/* With low row depths, could only be grayscale, so one channel */
|
1998-03-28 02:44:41 +00:00
|
|
|
if (row_info->bit_depth < 8)
|
|
|
|
{
|
1998-12-08 19:44:02 +00:00
|
|
|
png_bytep bp = row;
|
2012-03-04 03:18:12 +00:00
|
|
|
png_size_t i;
|
2013-09-26 18:39:44 +00:00
|
|
|
unsigned int mask;
|
2012-03-04 03:18:12 +00:00
|
|
|
png_size_t row_bytes = row_info->rowbytes;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
if (bit_depth->gray == 1 && row_info->bit_depth == 2)
|
|
|
|
mask = 0x55;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
|
|
|
|
mask = 0x11;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
else
|
|
|
|
mask = 0xff;
|
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (i = 0; i < row_bytes; i++, bp++)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
|
|
|
int j;
|
2013-09-26 18:39:44 +00:00
|
|
|
unsigned int v, out;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
|
|
|
v = *bp;
|
2013-09-26 18:39:44 +00:00
|
|
|
out = 0;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
|
|
|
|
{
|
|
|
|
if (j > 0)
|
2013-09-26 18:39:44 +00:00
|
|
|
out |= v << j;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
else
|
2013-09-26 18:39:44 +00:00
|
|
|
out |= (v >> (-j)) & mask;
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2013-09-26 18:39:44 +00:00
|
|
|
|
|
|
|
*bp = (png_byte)(out & 0xff);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
else if (row_info->bit_depth == 8)
|
|
|
|
{
|
1998-12-08 19:44:02 +00:00
|
|
|
png_bytep bp = row;
|
1998-03-28 02:44:41 +00:00
|
|
|
png_uint_32 i;
|
1998-12-08 19:44:02 +00:00
|
|
|
png_uint_32 istop = channels * row_info->width;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (i = 0; i < istop; i++, bp++)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
|
|
|
|
2013-09-26 18:39:44 +00:00
|
|
|
const unsigned int c = i%channels;
|
1998-12-08 19:44:02 +00:00
|
|
|
int j;
|
2013-09-26 18:39:44 +00:00
|
|
|
unsigned int v, out;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
v = *bp;
|
2013-09-26 18:39:44 +00:00
|
|
|
out = 0;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
|
|
|
|
{
|
|
|
|
if (j > 0)
|
2013-09-26 18:39:44 +00:00
|
|
|
out |= v << j;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
else
|
2013-09-26 18:39:44 +00:00
|
|
|
out |= v >> (-j);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2013-09-26 18:39:44 +00:00
|
|
|
|
|
|
|
*bp = (png_byte)(out & 0xff);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
png_bytep bp;
|
|
|
|
png_uint_32 i;
|
1998-12-08 19:44:02 +00:00
|
|
|
png_uint_32 istop = channels * row_info->width;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (bp = row, i = 0; i < istop; i++)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
2013-09-26 18:39:44 +00:00
|
|
|
const unsigned int c = i%channels;
|
1998-12-08 19:44:02 +00:00
|
|
|
int j;
|
2013-09-26 18:39:44 +00:00
|
|
|
unsigned int value, v;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
2013-09-26 18:39:44 +00:00
|
|
|
v = png_get_uint_16(bp);
|
1998-12-08 19:44:02 +00:00
|
|
|
value = 0;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
1998-12-08 19:44:02 +00:00
|
|
|
if (j > 0)
|
2013-09-26 18:39:44 +00:00
|
|
|
value |= v << j;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
else
|
2013-09-26 18:39:44 +00:00
|
|
|
value |= v >> (-j);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2013-09-26 18:39:44 +00:00
|
|
|
*bp++ = (png_byte)((value >> 8) & 0xff);
|
1998-12-08 19:44:02 +00:00
|
|
|
*bp++ = (png_byte)(value & 0xff);
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-01-05 21:46:26 +00:00
|
|
|
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
|
2014-02-25 13:40:53 +00:00
|
|
|
static void
|
1998-03-28 02:44:41 +00:00
|
|
|
png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
|
|
|
|
{
|
2008-12-22 23:40:01 +00:00
|
|
|
png_debug(1, "in png_do_write_swap_alpha");
|
2010-01-05 21:46:26 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
|
|
|
if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
|
|
|
|
{
|
|
|
|
if (row_info->bit_depth == 8)
|
|
|
|
{
|
2012-03-04 03:18:12 +00:00
|
|
|
/* This converts from ARGB to RGBA */
|
1998-03-28 02:44:41 +00:00
|
|
|
png_bytep sp, dp;
|
|
|
|
png_uint_32 i;
|
1998-12-08 19:44:02 +00:00
|
|
|
png_uint_32 row_width = row_info->width;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (i = 0, sp = dp = row; i < row_width; i++)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
1998-12-08 19:44:02 +00:00
|
|
|
png_byte save = *(sp++);
|
1998-03-28 02:44:41 +00:00
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = save;
|
|
|
|
}
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
|
|
|
#ifdef PNG_WRITE_16BIT_SUPPORTED
|
1998-03-28 02:44:41 +00:00
|
|
|
else
|
|
|
|
{
|
2012-03-04 03:18:12 +00:00
|
|
|
/* This converts from AARRGGBB to RRGGBBAA */
|
1998-03-28 02:44:41 +00:00
|
|
|
png_bytep sp, dp;
|
|
|
|
png_uint_32 i;
|
1998-12-08 19:44:02 +00:00
|
|
|
png_uint_32 row_width = row_info->width;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (i = 0, sp = dp = row; i < row_width; i++)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
1998-12-08 19:44:02 +00:00
|
|
|
png_byte save[2];
|
1998-03-28 02:44:41 +00:00
|
|
|
save[0] = *(sp++);
|
|
|
|
save[1] = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = save[0];
|
|
|
|
*(dp++) = save[1];
|
|
|
|
}
|
|
|
|
}
|
2014-11-26 04:42:00 +00:00
|
|
|
#endif /* WRITE_16BIT */
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-03-28 02:44:41 +00:00
|
|
|
else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
|
|
|
{
|
|
|
|
if (row_info->bit_depth == 8)
|
|
|
|
{
|
2012-03-04 03:18:12 +00:00
|
|
|
/* This converts from AG to GA */
|
1998-03-28 02:44:41 +00:00
|
|
|
png_bytep sp, dp;
|
|
|
|
png_uint_32 i;
|
1998-12-08 19:44:02 +00:00
|
|
|
png_uint_32 row_width = row_info->width;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (i = 0, sp = dp = row; i < row_width; i++)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
1998-12-08 19:44:02 +00:00
|
|
|
png_byte save = *(sp++);
|
1998-03-28 02:44:41 +00:00
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = save;
|
|
|
|
}
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
|
|
|
#ifdef PNG_WRITE_16BIT_SUPPORTED
|
1998-03-28 02:44:41 +00:00
|
|
|
else
|
|
|
|
{
|
2012-03-04 03:18:12 +00:00
|
|
|
/* This converts from AAGG to GGAA */
|
1998-03-28 02:44:41 +00:00
|
|
|
png_bytep sp, dp;
|
|
|
|
png_uint_32 i;
|
1998-12-08 19:44:02 +00:00
|
|
|
png_uint_32 row_width = row_info->width;
|
1998-03-28 02:44:41 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (i = 0, sp = dp = row; i < row_width; i++)
|
1998-03-28 02:44:41 +00:00
|
|
|
{
|
1998-12-08 19:44:02 +00:00
|
|
|
png_byte save[2];
|
1998-03-28 02:44:41 +00:00
|
|
|
save[0] = *(sp++);
|
|
|
|
save[1] = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = save[0];
|
|
|
|
*(dp++) = save[1];
|
|
|
|
}
|
|
|
|
}
|
2014-11-26 04:42:00 +00:00
|
|
|
#endif /* WRITE_16BIT */
|
1998-03-28 02:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-01-05 21:46:26 +00:00
|
|
|
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
|
2014-02-25 13:40:53 +00:00
|
|
|
static void
|
1998-12-08 19:44:02 +00:00
|
|
|
png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
|
|
|
|
{
|
2008-12-22 23:40:01 +00:00
|
|
|
png_debug(1, "in png_do_write_invert_alpha");
|
2010-01-05 21:46:26 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
{
|
|
|
|
if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
|
|
|
|
{
|
|
|
|
if (row_info->bit_depth == 8)
|
|
|
|
{
|
2012-03-04 03:18:12 +00:00
|
|
|
/* This inverts the alpha channel in RGBA */
|
1998-12-08 19:44:02 +00:00
|
|
|
png_bytep sp, dp;
|
|
|
|
png_uint_32 i;
|
|
|
|
png_uint_32 row_width = row_info->width;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
for (i = 0, sp = dp = row; i < row_width; i++)
|
|
|
|
{
|
2009-11-11 01:39:31 +00:00
|
|
|
/* Does nothing
|
1998-12-08 19:44:02 +00:00
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
2006-09-29 22:46:58 +00:00
|
|
|
*/
|
|
|
|
sp+=3; dp = sp;
|
2000-08-19 02:06:43 +00:00
|
|
|
*(dp++) = (png_byte)(255 - *(sp++));
|
1998-12-08 19:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
|
|
|
#ifdef PNG_WRITE_16BIT_SUPPORTED
|
1998-12-08 19:44:02 +00:00
|
|
|
else
|
|
|
|
{
|
2012-03-04 03:18:12 +00:00
|
|
|
/* This inverts the alpha channel in RRGGBBAA */
|
1998-12-08 19:44:02 +00:00
|
|
|
png_bytep sp, dp;
|
|
|
|
png_uint_32 i;
|
|
|
|
png_uint_32 row_width = row_info->width;
|
|
|
|
|
|
|
|
for (i = 0, sp = dp = row; i < row_width; i++)
|
|
|
|
{
|
2009-11-11 01:39:31 +00:00
|
|
|
/* Does nothing
|
1998-12-08 19:44:02 +00:00
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
2006-09-29 22:46:58 +00:00
|
|
|
*/
|
|
|
|
sp+=6; dp = sp;
|
2000-08-19 02:06:43 +00:00
|
|
|
*(dp++) = (png_byte)(255 - *(sp++));
|
|
|
|
*(dp++) = (png_byte)(255 - *(sp++));
|
1998-12-08 19:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-26 04:42:00 +00:00
|
|
|
#endif /* WRITE_16BIT */
|
1998-12-08 19:44:02 +00:00
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
1998-12-08 19:44:02 +00:00
|
|
|
else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
|
|
|
{
|
|
|
|
if (row_info->bit_depth == 8)
|
|
|
|
{
|
2012-03-04 03:18:12 +00:00
|
|
|
/* This inverts the alpha channel in GA */
|
1998-12-08 19:44:02 +00:00
|
|
|
png_bytep sp, dp;
|
|
|
|
png_uint_32 i;
|
|
|
|
png_uint_32 row_width = row_info->width;
|
|
|
|
|
|
|
|
for (i = 0, sp = dp = row; i < row_width; i++)
|
|
|
|
{
|
|
|
|
*(dp++) = *(sp++);
|
2000-08-19 02:06:43 +00:00
|
|
|
*(dp++) = (png_byte)(255 - *(sp++));
|
1998-12-08 19:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-04 03:18:12 +00:00
|
|
|
|
|
|
|
#ifdef PNG_WRITE_16BIT_SUPPORTED
|
1998-12-08 19:44:02 +00:00
|
|
|
else
|
|
|
|
{
|
2012-03-04 03:18:12 +00:00
|
|
|
/* This inverts the alpha channel in GGAA */
|
1998-12-08 19:44:02 +00:00
|
|
|
png_bytep sp, dp;
|
|
|
|
png_uint_32 i;
|
|
|
|
png_uint_32 row_width = row_info->width;
|
|
|
|
|
|
|
|
for (i = 0, sp = dp = row; i < row_width; i++)
|
|
|
|
{
|
2009-11-11 01:39:31 +00:00
|
|
|
/* Does nothing
|
1998-12-08 19:44:02 +00:00
|
|
|
*(dp++) = *(sp++);
|
|
|
|
*(dp++) = *(sp++);
|
2006-09-29 22:46:58 +00:00
|
|
|
*/
|
|
|
|
sp+=2; dp = sp;
|
2000-08-19 02:06:43 +00:00
|
|
|
*(dp++) = (png_byte)(255 - *(sp++));
|
|
|
|
*(dp++) = (png_byte)(255 - *(sp++));
|
1998-12-08 19:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-26 04:42:00 +00:00
|
|
|
#endif /* WRITE_16BIT */
|
1998-12-08 19:44:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2001-02-06 02:32:50 +00:00
|
|
|
|
2014-02-25 13:40:53 +00:00
|
|
|
/* Transform the data according to the user's wishes. The order of
|
|
|
|
* transformations is significant.
|
|
|
|
*/
|
2001-02-06 02:32:50 +00:00
|
|
|
void /* PRIVATE */
|
2014-02-25 13:40:53 +00:00
|
|
|
png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
|
2001-02-06 02:32:50 +00:00
|
|
|
{
|
2014-02-25 13:40:53 +00:00
|
|
|
png_debug(1, "in png_do_write_transformations");
|
2010-01-05 21:46:26 +00:00
|
|
|
|
2014-02-25 13:40:53 +00:00
|
|
|
if (png_ptr == NULL)
|
|
|
|
return;
|
2012-03-04 03:18:12 +00:00
|
|
|
|
2014-02-25 13:40:53 +00:00
|
|
|
#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
|
2014-11-26 04:42:00 +00:00
|
|
|
if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
|
2014-02-25 13:40:53 +00:00
|
|
|
if (png_ptr->write_user_transform_fn != NULL)
|
|
|
|
(*(png_ptr->write_user_transform_fn)) /* User write transform
|
|
|
|
function */
|
|
|
|
(png_ptr, /* png_ptr */
|
|
|
|
row_info, /* row_info: */
|
|
|
|
/* png_uint_32 width; width of row */
|
|
|
|
/* png_size_t rowbytes; number of bytes in row */
|
|
|
|
/* png_byte color_type; color type of pixels */
|
|
|
|
/* png_byte bit_depth; bit depth of samples */
|
|
|
|
/* png_byte channels; number of channels (1-4) */
|
|
|
|
/* png_byte pixel_depth; bits per pixel (depth*channels) */
|
|
|
|
png_ptr->row_buf + 1); /* start of pixel data for row */
|
|
|
|
#endif
|
2012-03-04 03:18:12 +00:00
|
|
|
|
2014-02-25 13:40:53 +00:00
|
|
|
#ifdef PNG_WRITE_FILLER_SUPPORTED
|
2014-11-26 04:42:00 +00:00
|
|
|
if ((png_ptr->transformations & PNG_FILLER) != 0)
|
2014-02-25 13:40:53 +00:00
|
|
|
png_do_strip_channel(row_info, png_ptr->row_buf + 1,
|
|
|
|
!(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
|
|
|
|
#endif
|
2001-02-06 02:32:50 +00:00
|
|
|
|
2014-02-25 13:40:53 +00:00
|
|
|
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
|
2014-11-26 04:42:00 +00:00
|
|
|
if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
|
2014-02-25 13:40:53 +00:00
|
|
|
png_do_packswap(row_info, png_ptr->row_buf + 1);
|
|
|
|
#endif
|
2012-03-04 03:18:12 +00:00
|
|
|
|
2014-02-25 13:40:53 +00:00
|
|
|
#ifdef PNG_WRITE_PACK_SUPPORTED
|
2014-11-26 04:42:00 +00:00
|
|
|
if ((png_ptr->transformations & PNG_PACK) != 0)
|
2014-02-25 13:40:53 +00:00
|
|
|
png_do_pack(row_info, png_ptr->row_buf + 1,
|
|
|
|
(png_uint_32)png_ptr->bit_depth);
|
|
|
|
#endif
|
2014-02-14 16:58:14 +00:00
|
|
|
|
2014-02-25 13:40:53 +00:00
|
|
|
#ifdef PNG_WRITE_SWAP_SUPPORTED
|
2014-11-26 04:42:00 +00:00
|
|
|
# ifdef PNG_16BIT_SUPPORTED
|
|
|
|
if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
|
2014-02-25 13:40:53 +00:00
|
|
|
png_do_swap(row_info, png_ptr->row_buf + 1);
|
2014-11-26 04:42:00 +00:00
|
|
|
# endif
|
2014-02-25 13:40:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PNG_WRITE_SHIFT_SUPPORTED
|
2014-11-26 04:42:00 +00:00
|
|
|
if ((png_ptr->transformations & PNG_SHIFT) != 0)
|
2014-02-25 13:40:53 +00:00
|
|
|
png_do_shift(row_info, png_ptr->row_buf + 1,
|
|
|
|
&(png_ptr->shift));
|
|
|
|
#endif
|
2001-02-06 02:32:50 +00:00
|
|
|
|
2014-02-25 13:40:53 +00:00
|
|
|
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
|
2014-11-26 04:42:00 +00:00
|
|
|
if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
|
2014-02-25 13:40:53 +00:00
|
|
|
png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
|
|
|
|
#endif
|
2012-03-04 03:18:12 +00:00
|
|
|
|
2014-02-25 13:40:53 +00:00
|
|
|
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
|
2014-11-26 04:42:00 +00:00
|
|
|
if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
|
2014-02-25 13:40:53 +00:00
|
|
|
png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
|
|
|
|
#endif
|
2012-03-04 03:18:12 +00:00
|
|
|
|
2014-02-25 13:40:53 +00:00
|
|
|
#ifdef PNG_WRITE_BGR_SUPPORTED
|
2014-11-26 04:42:00 +00:00
|
|
|
if ((png_ptr->transformations & PNG_BGR) != 0)
|
2014-02-25 13:40:53 +00:00
|
|
|
png_do_bgr(row_info, png_ptr->row_buf + 1);
|
|
|
|
#endif
|
2001-02-06 02:32:50 +00:00
|
|
|
|
2014-02-25 13:40:53 +00:00
|
|
|
#ifdef PNG_WRITE_INVERT_SUPPORTED
|
2014-11-26 04:42:00 +00:00
|
|
|
if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
|
2014-02-25 13:40:53 +00:00
|
|
|
png_do_invert(row_info, png_ptr->row_buf + 1);
|
|
|
|
#endif
|
2001-02-06 02:32:50 +00:00
|
|
|
}
|
2014-11-26 04:42:00 +00:00
|
|
|
#endif /* WRITE_TRANSFORMS */
|
|
|
|
#endif /* WRITE */
|