blob: 159dbd748ada2559c730cdfb7b69c542504bb26a [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngwtran.c - transforms the data in a row for PNG writers
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson65a22372010-03-03 05:38:29 -06004 * Last changed in libpng 1.5.0 [March 3, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
Glenn Randers-Pehrsonf9795312010-02-09 01:16:48 -060014#define PNG_EXPOSE_INTERNAL_STRUCTURES
Glenn Randers-Pehrson03f9b022009-12-04 08:40:41 -060015#define PNG_NO_PEDANTIC_WARNINGS
Guy Schalnat0d580581995-07-20 02:43:20 -050016#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050017#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050018#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050019
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050020/* Transform the data according to the user's wishes. The order of
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060021 * transformations is significant.
22 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050023void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060024png_do_write_transformations(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050025{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050026 png_debug(1, "in png_do_write_transformations");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060027
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060028 if (png_ptr == NULL)
29 return;
30
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050031#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060032 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050033 if (png_ptr->write_user_transform_fn != NULL)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060034 (*(png_ptr->write_user_transform_fn)) /* User write transform
35 function */
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060036 (png_ptr, /* png_ptr */
37 &(png_ptr->row_info), /* row_info: */
38 /* png_uint_32 width; width of row */
39 /* png_uint_32 rowbytes; number of bytes in row */
40 /* png_byte color_type; color type of pixels */
41 /* png_byte bit_depth; bit depth of samples */
42 /* png_byte channels; number of channels (1-4) */
43 /* png_byte pixel_depth; bits per pixel (depth*channels) */
44 png_ptr->row_buf + 1); /* start of pixel data for row */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060045#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050046#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -060047 if (png_ptr->transformations & PNG_FILLER)
Andreas Dilger47a0c421997-05-16 02:46:07 -050048 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060049 png_ptr->flags);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050050#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050051#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060052 if (png_ptr->transformations & PNG_PACKSWAP)
53 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
54#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050055#ifdef PNG_WRITE_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050056 if (png_ptr->transformations & PNG_PACK)
57 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060058 (png_uint_32)png_ptr->bit_depth);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050059#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050060#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060061 if (png_ptr->transformations & PNG_SWAP_BYTES)
62 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
63#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050064#ifdef PNG_WRITE_SHIFT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050065 if (png_ptr->transformations & PNG_SHIFT)
66 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060067 &(png_ptr->shift));
Guy Schalnat51f0eb41995-09-26 05:22:39 -050068#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050069#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060070 if (png_ptr->transformations & PNG_SWAP_ALPHA)
71 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
72#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050073#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060074 if (png_ptr->transformations & PNG_INVERT_ALPHA)
75 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
76#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050077#ifdef PNG_WRITE_BGR_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050078 if (png_ptr->transformations & PNG_BGR)
79 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050080#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050081#ifdef PNG_WRITE_INVERT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050082 if (png_ptr->transformations & PNG_INVERT_MONO)
83 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050084#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050085}
86
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050087#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060088/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
89 * row_info bit depth should be 8 (one pixel per byte). The channels
90 * should be 1 (this only happens on grayscale and paletted images).
91 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050092void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050093png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050094{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050095 png_debug(1, "in png_do_pack");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050096
Andreas Dilger47a0c421997-05-16 02:46:07 -050097 if (row_info->bit_depth == 8 &&
Guy Schalnat0d580581995-07-20 02:43:20 -050098 row_info->channels == 1)
99 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100 switch ((int)bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500101 {
102 case 1:
103 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500104 png_bytep sp, dp;
105 int mask, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500106 png_uint_32 i;
107 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500108
109 sp = row;
110 dp = row;
111 mask = 0x80;
112 v = 0;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600113
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500114 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500115 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116 if (*sp != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500117 v |= mask;
118 sp++;
119 if (mask > 1)
120 mask >>= 1;
121 else
122 {
123 mask = 0x80;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600124 *dp = (png_byte)v;
125 dp++;
Guy Schalnat0d580581995-07-20 02:43:20 -0500126 v = 0;
127 }
128 }
129 if (mask != 0x80)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600130 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500131 break;
132 }
133 case 2:
134 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500135 png_bytep sp, dp;
136 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500137 png_uint_32 i;
138 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500139
140 sp = row;
141 dp = row;
142 shift = 6;
143 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500144 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500145 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500146 png_byte value;
147
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600148 value = (png_byte)(*sp & 0x03);
Guy Schalnat0d580581995-07-20 02:43:20 -0500149 v |= (value << shift);
150 if (shift == 0)
151 {
152 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600153 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500154 dp++;
155 v = 0;
156 }
157 else
158 shift -= 2;
159 sp++;
160 }
161 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600162 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500163 break;
164 }
165 case 4:
166 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 png_bytep sp, dp;
168 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500169 png_uint_32 i;
170 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500171
172 sp = row;
173 dp = row;
174 shift = 4;
175 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500176 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500177 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 png_byte value;
179
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600180 value = (png_byte)(*sp & 0x0f);
Guy Schalnat0d580581995-07-20 02:43:20 -0500181 v |= (value << shift);
182
183 if (shift == 0)
184 {
185 shift = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600186 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500187 dp++;
188 v = 0;
189 }
190 else
191 shift -= 4;
192
193 sp++;
194 }
195 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600196 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500197 break;
198 }
199 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 row_info->bit_depth = (png_byte)bit_depth;
201 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500202 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600203 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500204 }
205}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500206#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500207
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500208#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600209/* Shift pixel values to take advantage of whole range. Pass the
210 * true number of bits in bit_depth. The row should be packed
211 * according to row_info->bit_depth. Thus, if you had a row of
212 * bit depth 4, but the pixels only had values from 0 to 7, you
213 * would pass 3 as bit_depth, and this routine would translate the
214 * data to 0 to 15.
215 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500216void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600217png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500218{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500219 png_debug(1, "in png_do_shift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500220
Andreas Dilger47a0c421997-05-16 02:46:07 -0500221 if (
Guy Schalnat0d580581995-07-20 02:43:20 -0500222 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
223 {
224 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500225 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500226
Guy Schalnat0d580581995-07-20 02:43:20 -0500227 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
228 {
229 shift_start[channels] = row_info->bit_depth - bit_depth->red;
230 shift_dec[channels] = bit_depth->red;
231 channels++;
232 shift_start[channels] = row_info->bit_depth - bit_depth->green;
233 shift_dec[channels] = bit_depth->green;
234 channels++;
235 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
236 shift_dec[channels] = bit_depth->blue;
237 channels++;
238 }
239 else
240 {
241 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
242 shift_dec[channels] = bit_depth->gray;
243 channels++;
244 }
245 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
246 {
247 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
248 shift_dec[channels] = bit_depth->alpha;
249 channels++;
250 }
251
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500252 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500253 if (row_info->bit_depth < 8)
254 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500255 png_bytep bp = row;
256 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500257 png_byte mask;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500258 png_uint_32 row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500259
260 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
261 mask = 0x55;
262 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
263 mask = 0x11;
264 else
265 mask = 0xff;
266
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500267 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500268 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500269 png_uint_16 v;
270 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500271
272 v = *bp;
273 *bp = 0;
274 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
275 {
276 if (j > 0)
277 *bp |= (png_byte)((v << j) & 0xff);
278 else
279 *bp |= (png_byte)((v >> (-j)) & mask);
280 }
281 }
282 }
283 else if (row_info->bit_depth == 8)
284 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500285 png_bytep bp = row;
286 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500287 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500288
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500289 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500290 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500291
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500292 png_uint_16 v;
293 int j;
294 int c = (int)(i%channels);
295
296 v = *bp;
297 *bp = 0;
298 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500299 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500300 if (j > 0)
301 *bp |= (png_byte)((v << j) & 0xff);
302 else
303 *bp |= (png_byte)((v >> (-j)) & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500304 }
305 }
306 }
307 else
308 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600309 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500310 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500311 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500312
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500313 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500314 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500315 int c = (int)(i%channels);
316 png_uint_16 value, v;
317 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500318
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500319 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500320 value = 0;
321 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500322 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500323 if (j > 0)
324 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
325 else
326 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500327 }
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500328 *bp++ = (png_byte)(value >> 8);
329 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500330 }
331 }
332 }
333}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500334#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500335
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500336#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500337void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500339{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500340 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500341
Guy Schalnat0d580581995-07-20 02:43:20 -0500342 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500343 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500344 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500345 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500346 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600347 /* This converts from ARGB to RGBA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500348 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500349 png_uint_32 i;
350 png_uint_32 row_width = row_info->width;
351 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500352 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500353 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500354 *(dp++) = *(sp++);
355 *(dp++) = *(sp++);
356 *(dp++) = *(sp++);
357 *(dp++) = save;
358 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500359 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500360 else
361 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600362 /* This converts from AARRGGBB to RRGGBBAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500364 png_uint_32 i;
365 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500366
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500367 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500368 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500369 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500370 save[0] = *(sp++);
371 save[1] = *(sp++);
372 *(dp++) = *(sp++);
373 *(dp++) = *(sp++);
374 *(dp++) = *(sp++);
375 *(dp++) = *(sp++);
376 *(dp++) = *(sp++);
377 *(dp++) = *(sp++);
378 *(dp++) = save[0];
379 *(dp++) = save[1];
380 }
381 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500382 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500384 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500386 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600387 /* This converts from AG to GA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500389 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500390 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500391
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500392 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500393 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500394 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500395 *(dp++) = *(sp++);
396 *(dp++) = save;
397 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500398 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500399 else
400 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600401 /* This converts from AAGG to GGAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500404 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500406 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500407 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500408 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500409 save[0] = *(sp++);
410 save[1] = *(sp++);
411 *(dp++) = *(sp++);
412 *(dp++) = *(sp++);
413 *(dp++) = save[0];
414 *(dp++) = save[1];
415 }
416 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500417 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500418 }
419}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500420#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500421
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500422#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500423void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600424png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
425{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500426 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500427
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600428 {
429 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
430 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600431 if (row_info->bit_depth == 8)
432 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600433 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600434 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500435 png_uint_32 i;
436 png_uint_32 row_width = row_info->width;
437 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600438 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500439 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600440 *(dp++) = *(sp++);
441 *(dp++) = *(sp++);
442 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600443 */
444 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500445 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600446 }
447 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600448 else
449 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600450 /* This inverts the alpha channel in RRGGBBAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600451 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500452 png_uint_32 i;
453 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600454
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500455 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600456 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500457 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600458 *(dp++) = *(sp++);
459 *(dp++) = *(sp++);
460 *(dp++) = *(sp++);
461 *(dp++) = *(sp++);
462 *(dp++) = *(sp++);
463 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600464 */
465 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500466 *(dp++) = (png_byte)(255 - *(sp++));
467 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600468 }
469 }
470 }
471 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
472 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600473 if (row_info->bit_depth == 8)
474 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600475 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600476 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500477 png_uint_32 i;
478 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600479
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500480 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600481 {
482 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500483 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600484 }
485 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600486 else
487 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600488 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600489 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500490 png_uint_32 i;
491 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600492
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500493 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600494 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500495 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600496 *(dp++) = *(sp++);
497 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600498 */
499 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500500 *(dp++) = (png_byte)(255 - *(sp++));
501 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600502 }
503 }
504 }
505 }
506}
507#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600508
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500509#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500510/* Undoes intrapixel differencing */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600511void /* PRIVATE */
512png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
513{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500514 png_debug(1, "in png_do_write_intrapixel");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500515
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600516 if (
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600517 (row_info->color_type & PNG_COLOR_MASK_COLOR))
518 {
519 int bytes_per_pixel;
520 png_uint_32 row_width = row_info->width;
521 if (row_info->bit_depth == 8)
522 {
523 png_bytep rp;
524 png_uint_32 i;
525
526 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
527 bytes_per_pixel = 3;
528 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
529 bytes_per_pixel = 4;
530 else
531 return;
532
533 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
534 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600535 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
536 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600537 }
538 }
539 else if (row_info->bit_depth == 16)
540 {
541 png_bytep rp;
542 png_uint_32 i;
543
544 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
545 bytes_per_pixel = 6;
546 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
547 bytes_per_pixel = 8;
548 else
549 return;
550
551 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
552 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600553 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
554 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
555 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500556 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
557 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600558 *(rp ) = (png_byte)((red >> 8) & 0xff);
559 *(rp + 1) = (png_byte)(red & 0xff);
560 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
561 *(rp + 5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600562 }
563 }
564 }
565}
566#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500567#endif /* PNG_WRITE_SUPPORTED */