blob: f5ae9006a369d16fbc4c99af22b3d75d158a5881 [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-Pehrson75294572000-05-06 14:09:57 -05004 * libpng 1.0.7beta11 - May 6, 2000
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06008 * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Guy Schalnat0d580581995-07-20 02:43:20 -050010
11#define PNG_INTERNAL
12#include "png.h"
13
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050014/* Transform the data according to the user's wishes. The order of
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060015 * transformations is significant.
16 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050017void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060018png_do_write_transformations(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050019{
Andreas Dilger47a0c421997-05-16 02:46:07 -050020 png_debug(1, "in png_do_write_transformations\n");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060021
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060022 if (png_ptr == NULL)
23 return;
24
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060025#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
26 if (png_ptr->transformations & PNG_USER_TRANSFORM)
27 if(png_ptr->write_user_transform_fn != NULL)
28 (*(png_ptr->write_user_transform_fn)) /* user write transform function */
29 (png_ptr, /* png_ptr */
30 &(png_ptr->row_info), /* row_info: */
31 /* png_uint_32 width; width of row */
32 /* png_uint_32 rowbytes; number of bytes in row */
33 /* png_byte color_type; color type of pixels */
34 /* png_byte bit_depth; bit depth of samples */
35 /* png_byte channels; number of channels (1-4) */
36 /* png_byte pixel_depth; bits per pixel (depth*channels) */
37 png_ptr->row_buf + 1); /* start of pixel data for row */
38#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050039#if defined(PNG_WRITE_FILLER_SUPPORTED)
Guy Schalnat6d764711995-12-19 03:22:19 -060040 if (png_ptr->transformations & PNG_FILLER)
Andreas Dilger47a0c421997-05-16 02:46:07 -050041 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
Guy Schalnate5a37791996-06-05 15:50:50 -050042 png_ptr->flags);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050043#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060044#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
45 if (png_ptr->transformations & PNG_PACKSWAP)
46 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
47#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050048#if defined(PNG_WRITE_PACK_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050049 if (png_ptr->transformations & PNG_PACK)
50 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
Andreas Dilger47a0c421997-05-16 02:46:07 -050051 (png_uint_32)png_ptr->bit_depth);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050052#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060053#if defined(PNG_WRITE_SWAP_SUPPORTED)
54 if (png_ptr->transformations & PNG_SWAP_BYTES)
55 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
56#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050057#if defined(PNG_WRITE_SHIFT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050058 if (png_ptr->transformations & PNG_SHIFT)
59 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
60 &(png_ptr->shift));
Guy Schalnat51f0eb41995-09-26 05:22:39 -050061#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060062#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
63 if (png_ptr->transformations & PNG_INVERT_ALPHA)
64 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
65#endif
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060066#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
67 if (png_ptr->transformations & PNG_SWAP_ALPHA)
68 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
69#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050070#if defined(PNG_WRITE_BGR_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050071 if (png_ptr->transformations & PNG_BGR)
72 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050073#endif
74#if defined(PNG_WRITE_INVERT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050075 if (png_ptr->transformations & PNG_INVERT_MONO)
76 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050077#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050078}
79
Guy Schalnat51f0eb41995-09-26 05:22:39 -050080#if defined(PNG_WRITE_PACK_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060081/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
82 * row_info bit depth should be 8 (one pixel per byte). The channels
83 * should be 1 (this only happens on grayscale and paletted images).
84 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050085void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050086png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050087{
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_debug(1, "in png_do_pack\n");
89 if (row_info->bit_depth == 8 &&
90#if defined(PNG_USELESS_TESTS_SUPPORTED)
91 row != NULL && row_info != NULL &&
92#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050093 row_info->channels == 1)
94 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050095 switch ((int)bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050096 {
97 case 1:
98 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050099 png_bytep sp, dp;
100 int mask, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500101 png_uint_32 i;
102 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500103
104 sp = row;
105 dp = row;
106 mask = 0x80;
107 v = 0;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600108
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500109 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500110 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500111 if (*sp != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500112 v |= mask;
113 sp++;
114 if (mask > 1)
115 mask >>= 1;
116 else
117 {
118 mask = 0x80;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600119 *dp = (png_byte)v;
120 dp++;
Guy Schalnat0d580581995-07-20 02:43:20 -0500121 v = 0;
122 }
123 }
124 if (mask != 0x80)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600125 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500126 break;
127 }
128 case 2:
129 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500130 png_bytep sp, dp;
131 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500132 png_uint_32 i;
133 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500134
135 sp = row;
136 dp = row;
137 shift = 6;
138 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500139 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500140 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500141 png_byte value;
142
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600143 value = (png_byte)(*sp & 0x03);
Guy Schalnat0d580581995-07-20 02:43:20 -0500144 v |= (value << shift);
145 if (shift == 0)
146 {
147 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600148 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500149 dp++;
150 v = 0;
151 }
152 else
153 shift -= 2;
154 sp++;
155 }
156 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600157 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500158 break;
159 }
160 case 4:
161 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500162 png_bytep sp, dp;
163 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500164 png_uint_32 i;
165 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500166
167 sp = row;
168 dp = row;
169 shift = 4;
170 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500171 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500172 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500173 png_byte value;
174
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600175 value = (png_byte)(*sp & 0x0f);
Guy Schalnat0d580581995-07-20 02:43:20 -0500176 v |= (value << shift);
177
178 if (shift == 0)
179 {
180 shift = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600181 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500182 dp++;
183 v = 0;
184 }
185 else
186 shift -= 4;
187
188 sp++;
189 }
190 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600191 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500192 break;
193 }
194 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500195 row_info->bit_depth = (png_byte)bit_depth;
196 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Guy Schalnat0d580581995-07-20 02:43:20 -0500197 row_info->rowbytes =
198 ((row_info->width * row_info->pixel_depth + 7) >> 3);
199 }
200}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500201#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500202
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500203#if defined(PNG_WRITE_SHIFT_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600204/* Shift pixel values to take advantage of whole range. Pass the
205 * true number of bits in bit_depth. The row should be packed
206 * according to row_info->bit_depth. Thus, if you had a row of
207 * bit depth 4, but the pixels only had values from 0 to 7, you
208 * would pass 3 as bit_depth, and this routine would translate the
209 * data to 0 to 15.
210 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500211void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600212png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500213{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500214 png_debug(1, "in png_do_shift\n");
215#if defined(PNG_USELESS_TESTS_SUPPORTED)
216 if (row != NULL && row_info != NULL &&
217#else
218 if (
219#endif
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-Pehrsoncbe52d81998-02-28 07:00:24 -0600250 /* 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
Andreas Dilger47a0c421997-05-16 02:46:07 -0500334#if defined(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{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338 png_debug(1, "in png_do_write_swap_alpha\n");
339#if defined(PNG_USELESS_TESTS_SUPPORTED)
340 if (row != NULL && row_info != NULL)
341#endif
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 /* This converts from ARGB to RGBA */
346 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500347 {
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 /* This converts from AARRGGBB to RRGGBBAA */
361 else
362 {
363 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 /* This converts from AG to GA */
386 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500387 {
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 /* This converts from AAGG to GGAA */
400 else
401 {
402 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-Pehrsonc4a2ae61998-01-16 22:06:18 -0600422#if defined(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{
426 png_debug(1, "in png_do_write_invert_alpha\n");
427#if defined(PNG_USELESS_TESTS_SUPPORTED)
428 if (row != NULL && row_info != NULL)
429#endif
430 {
431 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
432 {
433 /* This inverts the alpha channel in RGBA */
434 if (row_info->bit_depth == 8)
435 {
436 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500437 png_uint_32 i;
438 png_uint_32 row_width = row_info->width;
439 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600440 {
441 *(dp++) = *(sp++);
442 *(dp++) = *(sp++);
443 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500444 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600445 }
446 }
447 /* This inverts the alpha channel in RRGGBBAA */
448 else
449 {
450 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500451 png_uint_32 i;
452 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600453
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500454 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600455 {
456 *(dp++) = *(sp++);
457 *(dp++) = *(sp++);
458 *(dp++) = *(sp++);
459 *(dp++) = *(sp++);
460 *(dp++) = *(sp++);
461 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500462 *(dp++) = (png_byte)(255 - *(sp++));
463 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600464 }
465 }
466 }
467 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
468 {
469 /* This inverts the alpha channel in GA */
470 if (row_info->bit_depth == 8)
471 {
472 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500473 png_uint_32 i;
474 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600475
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500476 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600477 {
478 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500479 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600480 }
481 }
482 /* This inverts the alpha channel in GGAA */
483 else
484 {
485 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500486 png_uint_32 i;
487 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600488
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500489 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600490 {
491 *(dp++) = *(sp++);
492 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500493 *(dp++) = (png_byte)(255 - *(sp++));
494 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600495 }
496 }
497 }
498 }
499}
500#endif