blob: fc1ba7cca93d878df0af1c3b618c4394e348a1cc [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-Pehrsond0dce401998-05-09 10:02:29 -05004 * 1.0.1c
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-Pehrson2687fcc1998-01-07 20:54:20 -06008 * Copyright (c) 1998, Glenn Randers-Pehrson
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05009 * May 9, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060010 */
Guy Schalnat0d580581995-07-20 02:43:20 -050011
12#define PNG_INTERNAL
13#include "png.h"
14
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060015/* Transform the data according to the users wishes. The order of
16 * transformations is significant.
17 */
Guy Schalnat0d580581995-07-20 02:43:20 -050018void
Guy Schalnat6d764711995-12-19 03:22:19 -060019png_do_write_transformations(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050020{
Andreas Dilger47a0c421997-05-16 02:46:07 -050021 png_debug(1, "in png_do_write_transformations\n");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060022
23#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
24 if (png_ptr->transformations & PNG_USER_TRANSFORM)
25 if(png_ptr->write_user_transform_fn != NULL)
26 (*(png_ptr->write_user_transform_fn)) /* user write transform function */
27 (png_ptr, /* png_ptr */
28 &(png_ptr->row_info), /* row_info: */
29 /* png_uint_32 width; width of row */
30 /* png_uint_32 rowbytes; number of bytes in row */
31 /* png_byte color_type; color type of pixels */
32 /* png_byte bit_depth; bit depth of samples */
33 /* png_byte channels; number of channels (1-4) */
34 /* png_byte pixel_depth; bits per pixel (depth*channels) */
35 png_ptr->row_buf + 1); /* start of pixel data for row */
36#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050037#if defined(PNG_WRITE_FILLER_SUPPORTED)
Guy Schalnat6d764711995-12-19 03:22:19 -060038 if (png_ptr->transformations & PNG_FILLER)
Andreas Dilger47a0c421997-05-16 02:46:07 -050039 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
Guy Schalnate5a37791996-06-05 15:50:50 -050040 png_ptr->flags);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050041#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060042#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
43 if (png_ptr->transformations & PNG_PACKSWAP)
44 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
45#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050046#if defined(PNG_WRITE_PACK_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050047 if (png_ptr->transformations & PNG_PACK)
48 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
Andreas Dilger47a0c421997-05-16 02:46:07 -050049 (png_uint_32)png_ptr->bit_depth);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050050#endif
51#if defined(PNG_WRITE_SHIFT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050052 if (png_ptr->transformations & PNG_SHIFT)
53 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
54 &(png_ptr->shift));
Guy Schalnat51f0eb41995-09-26 05:22:39 -050055#endif
56#if defined(PNG_WRITE_SWAP_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050057 if (png_ptr->transformations & PNG_SWAP_BYTES)
58 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050059#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060060#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
61 if (png_ptr->transformations & PNG_INVERT_ALPHA)
62 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
63#endif
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060064#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
65 if (png_ptr->transformations & PNG_SWAP_ALPHA)
66 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
67#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050068#if defined(PNG_WRITE_BGR_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050069 if (png_ptr->transformations & PNG_BGR)
70 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050071#endif
72#if defined(PNG_WRITE_INVERT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050073 if (png_ptr->transformations & PNG_INVERT_MONO)
74 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050075#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050076}
77
Guy Schalnat51f0eb41995-09-26 05:22:39 -050078#if defined(PNG_WRITE_PACK_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060079/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
80 * row_info bit depth should be 8 (one pixel per byte). The channels
81 * should be 1 (this only happens on grayscale and paletted images).
82 */
Guy Schalnat0d580581995-07-20 02:43:20 -050083void
Andreas Dilger47a0c421997-05-16 02:46:07 -050084png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050085{
Andreas Dilger47a0c421997-05-16 02:46:07 -050086 png_debug(1, "in png_do_pack\n");
87 if (row_info->bit_depth == 8 &&
88#if defined(PNG_USELESS_TESTS_SUPPORTED)
89 row != NULL && row_info != NULL &&
90#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050091 row_info->channels == 1)
92 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050093 switch ((int)bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050094 {
95 case 1:
96 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050097 png_bytep sp, dp;
98 int mask, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -050099 png_uint_32 i;
100 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500101
102 sp = row;
103 dp = row;
104 mask = 0x80;
105 v = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500106
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500107 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500108 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109 if (*sp != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500110 v |= mask;
111 sp++;
112 if (mask > 1)
113 mask >>= 1;
114 else
115 {
116 mask = 0x80;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600117 *dp = (png_byte)v;
118 dp++;
Guy Schalnat0d580581995-07-20 02:43:20 -0500119 v = 0;
120 }
121 }
122 if (mask != 0x80)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600123 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500124 break;
125 }
126 case 2:
127 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500128 png_bytep sp, dp;
129 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500130 png_uint_32 i;
131 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500132
133 sp = row;
134 dp = row;
135 shift = 6;
136 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500137 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500138 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500139 png_byte value;
140
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600141 value = (png_byte)(*sp & 0x3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500142 v |= (value << shift);
143 if (shift == 0)
144 {
145 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600146 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500147 dp++;
148 v = 0;
149 }
150 else
151 shift -= 2;
152 sp++;
153 }
154 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600155 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500156 break;
157 }
158 case 4:
159 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500160 png_bytep sp, dp;
161 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500162 png_uint_32 i;
163 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500164
165 sp = row;
166 dp = row;
167 shift = 4;
168 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500169 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500170 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500171 png_byte value;
172
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600173 value = (png_byte)(*sp & 0xf);
Guy Schalnat0d580581995-07-20 02:43:20 -0500174 v |= (value << shift);
175
176 if (shift == 0)
177 {
178 shift = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600179 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500180 dp++;
181 v = 0;
182 }
183 else
184 shift -= 4;
185
186 sp++;
187 }
188 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600189 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500190 break;
191 }
192 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500193 row_info->bit_depth = (png_byte)bit_depth;
194 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Guy Schalnat0d580581995-07-20 02:43:20 -0500195 row_info->rowbytes =
196 ((row_info->width * row_info->pixel_depth + 7) >> 3);
197 }
198}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500199#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500200
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500201#if defined(PNG_WRITE_SHIFT_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600202/* Shift pixel values to take advantage of whole range. Pass the
203 * true number of bits in bit_depth. The row should be packed
204 * according to row_info->bit_depth. Thus, if you had a row of
205 * bit depth 4, but the pixels only had values from 0 to 7, you
206 * would pass 3 as bit_depth, and this routine would translate the
207 * data to 0 to 15.
208 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500209void
Guy Schalnat6d764711995-12-19 03:22:19 -0600210png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500211{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500212 png_debug(1, "in png_do_shift\n");
213#if defined(PNG_USELESS_TESTS_SUPPORTED)
214 if (row != NULL && row_info != NULL &&
215#else
216 if (
217#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500218 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
219 {
220 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500221 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500222
Guy Schalnat0d580581995-07-20 02:43:20 -0500223 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
224 {
225 shift_start[channels] = row_info->bit_depth - bit_depth->red;
226 shift_dec[channels] = bit_depth->red;
227 channels++;
228 shift_start[channels] = row_info->bit_depth - bit_depth->green;
229 shift_dec[channels] = bit_depth->green;
230 channels++;
231 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
232 shift_dec[channels] = bit_depth->blue;
233 channels++;
234 }
235 else
236 {
237 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
238 shift_dec[channels] = bit_depth->gray;
239 channels++;
240 }
241 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
242 {
243 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
244 shift_dec[channels] = bit_depth->alpha;
245 channels++;
246 }
247
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600248 /* with low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500249 if (row_info->bit_depth < 8)
250 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500251 png_bytep bp = row;
252 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500253 png_byte mask;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500254 png_uint_32 row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500255
256 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
257 mask = 0x55;
258 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
259 mask = 0x11;
260 else
261 mask = 0xff;
262
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500263 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500264 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500265 png_uint_16 v;
266 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500267
268 v = *bp;
269 *bp = 0;
270 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
271 {
272 if (j > 0)
273 *bp |= (png_byte)((v << j) & 0xff);
274 else
275 *bp |= (png_byte)((v >> (-j)) & mask);
276 }
277 }
278 }
279 else if (row_info->bit_depth == 8)
280 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500281 png_bytep bp = row;
282 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500283 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500284
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500285 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500286 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500287
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500288 png_uint_16 v;
289 int j;
290 int c = (int)(i%channels);
291
292 v = *bp;
293 *bp = 0;
294 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500295 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500296 if (j > 0)
297 *bp |= (png_byte)((v << j) & 0xff);
298 else
299 *bp |= (png_byte)((v >> (-j)) & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500300 }
301 }
302 }
303 else
304 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600305 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500306 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500307 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500308
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500309 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500310 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500311 int c = (int)(i%channels);
312 png_uint_16 value, v;
313 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500314
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500315 v = ((png_uint_16)(*bp) << 8) + *(bp + 1);
316 value = 0;
317 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500318 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500319 if (j > 0)
320 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
321 else
322 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500323 }
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500324 *bp++ = (png_byte)(value >> 8);
325 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500326 }
327 }
328 }
329}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500330#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500331
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500333void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500334png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500335{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500336 png_debug(1, "in png_do_write_swap_alpha\n");
337#if defined(PNG_USELESS_TESTS_SUPPORTED)
338 if (row != NULL && row_info != NULL)
339#endif
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 /* This converts from ARGB to RGBA */
344 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500345 {
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 /* This converts from AARRGGBB to RRGGBBAA */
359 else
360 {
361 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 /* This converts from AG to GA */
384 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500385 {
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 /* This converts from AAGG to GGAA */
398 else
399 {
400 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-Pehrsonc4a2ae61998-01-16 22:06:18 -0600420#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
421void
422png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
423{
424 png_debug(1, "in png_do_write_invert_alpha\n");
425#if defined(PNG_USELESS_TESTS_SUPPORTED)
426 if (row != NULL && row_info != NULL)
427#endif
428 {
429 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
430 {
431 /* This inverts the alpha channel in RGBA */
432 if (row_info->bit_depth == 8)
433 {
434 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 {
439 *(dp++) = *(sp++);
440 *(dp++) = *(sp++);
441 *(dp++) = *(sp++);
442 *(dp++) = 255 - *(sp++);
443 }
444 }
445 /* This inverts the alpha channel in RRGGBBAA */
446 else
447 {
448 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500449 png_uint_32 i;
450 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600451
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500452 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600453 {
454 *(dp++) = *(sp++);
455 *(dp++) = *(sp++);
456 *(dp++) = *(sp++);
457 *(dp++) = *(sp++);
458 *(dp++) = *(sp++);
459 *(dp++) = *(sp++);
460 *(dp++) = 255 - *(sp++);
461 *(dp++) = 255 - *(sp++);
462 }
463 }
464 }
465 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
466 {
467 /* This inverts the alpha channel in GA */
468 if (row_info->bit_depth == 8)
469 {
470 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500471 png_uint_32 i;
472 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600473
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500474 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600475 {
476 *(dp++) = *(sp++);
477 *(dp++) = 255 - *(sp++);
478 }
479 }
480 /* This inverts the alpha channel in GGAA */
481 else
482 {
483 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500484 png_uint_32 i;
485 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600486
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500487 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600488 {
489 *(dp++) = *(sp++);
490 *(dp++) = *(sp++);
491 *(dp++) = 255 - *(sp++);
492 *(dp++) = 255 - *(sp++);
493 }
494 }
495 }
496 }
497}
498#endif