blob: b6b0f4785a5dd997575883cd33cd33754f799d38 [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-Pehrsonc6fecfb2010-03-09 22:27:26 -06004 * Last changed in libpng 1.5.0 [March 10, 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-Pehrsonbeb572e2006-08-19 13:59:24 -050014#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050015
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060016#ifdef PNG_WRITE_SUPPORTED
17
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050018/* Transform the data according to the user's wishes. The order of
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060019 * transformations is significant.
20 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050021void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060022png_do_write_transformations(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050023{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050024 png_debug(1, "in png_do_write_transformations");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060025
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060026 if (png_ptr == NULL)
27 return;
28
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050029#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060030 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050031 if (png_ptr->write_user_transform_fn != NULL)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060032 (*(png_ptr->write_user_transform_fn)) /* User write transform
33 function */
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060034 (png_ptr, /* png_ptr */
35 &(png_ptr->row_info), /* row_info: */
36 /* png_uint_32 width; width of row */
37 /* png_uint_32 rowbytes; number of bytes in row */
38 /* png_byte color_type; color type of pixels */
39 /* png_byte bit_depth; bit depth of samples */
40 /* png_byte channels; number of channels (1-4) */
41 /* png_byte pixel_depth; bits per pixel (depth*channels) */
42 png_ptr->row_buf + 1); /* start of pixel data for row */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060043#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050044#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -060045 if (png_ptr->transformations & PNG_FILLER)
Andreas Dilger47a0c421997-05-16 02:46:07 -050046 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060047 png_ptr->flags);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050048#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050049#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060050 if (png_ptr->transformations & PNG_PACKSWAP)
51 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
52#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050053#ifdef PNG_WRITE_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050054 if (png_ptr->transformations & PNG_PACK)
55 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060056 (png_uint_32)png_ptr->bit_depth);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050057#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050058#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060059 if (png_ptr->transformations & PNG_SWAP_BYTES)
60 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
61#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050062#ifdef PNG_WRITE_SHIFT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050063 if (png_ptr->transformations & PNG_SHIFT)
64 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060065 &(png_ptr->shift));
Guy Schalnat51f0eb41995-09-26 05:22:39 -050066#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050067#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060068 if (png_ptr->transformations & PNG_SWAP_ALPHA)
69 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
70#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050071#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060072 if (png_ptr->transformations & PNG_INVERT_ALPHA)
73 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
74#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050075#ifdef PNG_WRITE_BGR_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050076 if (png_ptr->transformations & PNG_BGR)
77 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050078#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050079#ifdef PNG_WRITE_INVERT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050080 if (png_ptr->transformations & PNG_INVERT_MONO)
81 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050082#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050083}
84
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050085#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060086/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
87 * row_info bit depth should be 8 (one pixel per byte). The channels
88 * should be 1 (this only happens on grayscale and paletted images).
89 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050090void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050091png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050092{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050093 png_debug(1, "in png_do_pack");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050094
Andreas Dilger47a0c421997-05-16 02:46:07 -050095 if (row_info->bit_depth == 8 &&
Guy Schalnat0d580581995-07-20 02:43:20 -050096 row_info->channels == 1)
97 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050098 switch ((int)bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050099 {
100 case 1:
101 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 png_bytep sp, dp;
103 int mask, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500104 png_uint_32 i;
105 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500106
107 sp = row;
108 dp = row;
109 mask = 0x80;
110 v = 0;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600111
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500112 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500113 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500114 if (*sp != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500115 v |= mask;
116 sp++;
117 if (mask > 1)
118 mask >>= 1;
119 else
120 {
121 mask = 0x80;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600122 *dp = (png_byte)v;
123 dp++;
Guy Schalnat0d580581995-07-20 02:43:20 -0500124 v = 0;
125 }
126 }
127 if (mask != 0x80)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600128 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500129 break;
130 }
131 case 2:
132 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 png_bytep sp, dp;
134 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500135 png_uint_32 i;
136 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500137
138 sp = row;
139 dp = row;
140 shift = 6;
141 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500142 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500143 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500144 png_byte value;
145
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600146 value = (png_byte)(*sp & 0x03);
Guy Schalnat0d580581995-07-20 02:43:20 -0500147 v |= (value << shift);
148 if (shift == 0)
149 {
150 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600151 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500152 dp++;
153 v = 0;
154 }
155 else
156 shift -= 2;
157 sp++;
158 }
159 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600160 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500161 break;
162 }
163 case 4:
164 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500165 png_bytep sp, dp;
166 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500167 png_uint_32 i;
168 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500169
170 sp = row;
171 dp = row;
172 shift = 4;
173 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500174 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500175 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176 png_byte value;
177
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600178 value = (png_byte)(*sp & 0x0f);
Guy Schalnat0d580581995-07-20 02:43:20 -0500179 v |= (value << shift);
180
181 if (shift == 0)
182 {
183 shift = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600184 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500185 dp++;
186 v = 0;
187 }
188 else
189 shift -= 4;
190
191 sp++;
192 }
193 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600194 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500195 break;
196 }
197 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500198 row_info->bit_depth = (png_byte)bit_depth;
199 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500200 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600201 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500202 }
203}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500204#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500205
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500206#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600207/* Shift pixel values to take advantage of whole range. Pass the
208 * true number of bits in bit_depth. The row should be packed
209 * according to row_info->bit_depth. Thus, if you had a row of
210 * bit depth 4, but the pixels only had values from 0 to 7, you
211 * would pass 3 as bit_depth, and this routine would translate the
212 * data to 0 to 15.
213 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500214void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600215png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500216{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500217 png_debug(1, "in png_do_shift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500218
Andreas Dilger47a0c421997-05-16 02:46:07 -0500219 if (
Guy Schalnat0d580581995-07-20 02:43:20 -0500220 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
221 {
222 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500223 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500224
Guy Schalnat0d580581995-07-20 02:43:20 -0500225 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
226 {
227 shift_start[channels] = row_info->bit_depth - bit_depth->red;
228 shift_dec[channels] = bit_depth->red;
229 channels++;
230 shift_start[channels] = row_info->bit_depth - bit_depth->green;
231 shift_dec[channels] = bit_depth->green;
232 channels++;
233 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
234 shift_dec[channels] = bit_depth->blue;
235 channels++;
236 }
237 else
238 {
239 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
240 shift_dec[channels] = bit_depth->gray;
241 channels++;
242 }
243 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
244 {
245 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
246 shift_dec[channels] = bit_depth->alpha;
247 channels++;
248 }
249
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500250 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500251 if (row_info->bit_depth < 8)
252 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500253 png_bytep bp = row;
254 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500255 png_byte mask;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500256 png_uint_32 row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500257
258 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
259 mask = 0x55;
260 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
261 mask = 0x11;
262 else
263 mask = 0xff;
264
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500265 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500266 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500267 png_uint_16 v;
268 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500269
270 v = *bp;
271 *bp = 0;
272 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
273 {
274 if (j > 0)
275 *bp |= (png_byte)((v << j) & 0xff);
276 else
277 *bp |= (png_byte)((v >> (-j)) & mask);
278 }
279 }
280 }
281 else if (row_info->bit_depth == 8)
282 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500283 png_bytep bp = row;
284 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500285 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500286
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500287 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500288 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500289
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500290 png_uint_16 v;
291 int j;
292 int c = (int)(i%channels);
293
294 v = *bp;
295 *bp = 0;
296 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500297 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500298 if (j > 0)
299 *bp |= (png_byte)((v << j) & 0xff);
300 else
301 *bp |= (png_byte)((v >> (-j)) & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500302 }
303 }
304 }
305 else
306 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600307 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500308 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500309 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500310
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500311 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500312 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500313 int c = (int)(i%channels);
314 png_uint_16 value, v;
315 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500316
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500317 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500318 value = 0;
319 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500320 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500321 if (j > 0)
322 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
323 else
324 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500325 }
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500326 *bp++ = (png_byte)(value >> 8);
327 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500328 }
329 }
330 }
331}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500332#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500333
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500334#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500335void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500336png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500337{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500338 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500339
Guy Schalnat0d580581995-07-20 02:43:20 -0500340 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500341 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500342 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500343 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500344 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600345 /* This converts from ARGB to RGBA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500346 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500347 png_uint_32 i;
348 png_uint_32 row_width = row_info->width;
349 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500350 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500351 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500352 *(dp++) = *(sp++);
353 *(dp++) = *(sp++);
354 *(dp++) = *(sp++);
355 *(dp++) = save;
356 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500357 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500358 else
359 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600360 /* This converts from AARRGGBB to RRGGBBAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500361 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500362 png_uint_32 i;
363 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500364
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500365 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500366 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500367 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500368 save[0] = *(sp++);
369 save[1] = *(sp++);
370 *(dp++) = *(sp++);
371 *(dp++) = *(sp++);
372 *(dp++) = *(sp++);
373 *(dp++) = *(sp++);
374 *(dp++) = *(sp++);
375 *(dp++) = *(sp++);
376 *(dp++) = save[0];
377 *(dp++) = save[1];
378 }
379 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500380 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500381 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500382 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500384 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600385 /* This converts from AG to GA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500388 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500389
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500390 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500391 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500392 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500393 *(dp++) = *(sp++);
394 *(dp++) = save;
395 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500396 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 else
398 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600399 /* This converts from AAGG to GGAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500400 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500401 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500402 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500404 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500406 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500407 save[0] = *(sp++);
408 save[1] = *(sp++);
409 *(dp++) = *(sp++);
410 *(dp++) = *(sp++);
411 *(dp++) = save[0];
412 *(dp++) = save[1];
413 }
414 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500415 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500416 }
417}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500418#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500419
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500420#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500421void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600422png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
423{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500424 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500425
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600426 {
427 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
428 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600429 if (row_info->bit_depth == 8)
430 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600431 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600432 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500433 png_uint_32 i;
434 png_uint_32 row_width = row_info->width;
435 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600436 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500437 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600438 *(dp++) = *(sp++);
439 *(dp++) = *(sp++);
440 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600441 */
442 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500443 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600444 }
445 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600446 else
447 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600448 /* This inverts the alpha channel in RRGGBBAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600449 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500450 png_uint_32 i;
451 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600452
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500453 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600454 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500455 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600456 *(dp++) = *(sp++);
457 *(dp++) = *(sp++);
458 *(dp++) = *(sp++);
459 *(dp++) = *(sp++);
460 *(dp++) = *(sp++);
461 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600462 */
463 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500464 *(dp++) = (png_byte)(255 - *(sp++));
465 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600466 }
467 }
468 }
469 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
470 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600471 if (row_info->bit_depth == 8)
472 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600473 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600474 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500475 png_uint_32 i;
476 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600477
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500478 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600479 {
480 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500481 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600482 }
483 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600484 else
485 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600486 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600487 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500488 png_uint_32 i;
489 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600490
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500491 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600492 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500493 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600494 *(dp++) = *(sp++);
495 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600496 */
497 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500498 *(dp++) = (png_byte)(255 - *(sp++));
499 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600500 }
501 }
502 }
503 }
504}
505#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600506
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500507#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500508/* Undoes intrapixel differencing */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600509void /* PRIVATE */
510png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
511{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500512 png_debug(1, "in png_do_write_intrapixel");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500513
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600514 if (
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600515 (row_info->color_type & PNG_COLOR_MASK_COLOR))
516 {
517 int bytes_per_pixel;
518 png_uint_32 row_width = row_info->width;
519 if (row_info->bit_depth == 8)
520 {
521 png_bytep rp;
522 png_uint_32 i;
523
524 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
525 bytes_per_pixel = 3;
526 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
527 bytes_per_pixel = 4;
528 else
529 return;
530
531 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
532 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600533 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
534 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600535 }
536 }
537 else if (row_info->bit_depth == 16)
538 {
539 png_bytep rp;
540 png_uint_32 i;
541
542 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
543 bytes_per_pixel = 6;
544 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
545 bytes_per_pixel = 8;
546 else
547 return;
548
549 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
550 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600551 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
552 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
553 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500554 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
555 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600556 *(rp ) = (png_byte)((red >> 8) & 0xff);
557 *(rp + 1) = (png_byte)(red & 0xff);
558 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
559 *(rp + 5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600560 }
561 }
562 }
563}
564#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500565#endif /* PNG_WRITE_SUPPORTED */