blob: 5dc949157d410ba0edbeb07b9a06d82f203562c4 [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngwtran.c - transforms the data in a row for PNG writers
3 *
Matt Sarett96be9082016-05-03 13:29:54 -04004 * Last changed in libpng 1.6.18 [July 23, 2015]
5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
The Android Open Source Project893912b2009-03-03 19:30:05 -08006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04008 *
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
The Android Open Source Project893912b2009-03-03 19:30:05 -080012 */
13
Chris Craikb50c2172013-07-29 15:28:30 -070014#include "pngpriv.h"
15
The Android Open Source Project893912b2009-03-03 19:30:05 -080016#ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -070017#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -080018
Patrick Scott5f6bd842010-06-28 16:55:16 -040019#ifdef PNG_WRITE_PACK_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -080020/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
21 * row_info bit depth should be 8 (one pixel per byte). The channels
22 * should be 1 (this only happens on grayscale and paletted images).
23 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +053024static void
The Android Open Source Project893912b2009-03-03 19:30:05 -080025png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
26{
The Android Open Source Project4215dd12009-03-09 11:52:12 -070027 png_debug(1, "in png_do_pack");
Patrick Scott5f6bd842010-06-28 16:55:16 -040028
The Android Open Source Project893912b2009-03-03 19:30:05 -080029 if (row_info->bit_depth == 8 &&
The Android Open Source Project893912b2009-03-03 19:30:05 -080030 row_info->channels == 1)
31 {
32 switch ((int)bit_depth)
33 {
34 case 1:
35 {
36 png_bytep sp, dp;
37 int mask, v;
38 png_uint_32 i;
39 png_uint_32 row_width = row_info->width;
40
41 sp = row;
42 dp = row;
43 mask = 0x80;
44 v = 0;
45
46 for (i = 0; i < row_width; i++)
47 {
48 if (*sp != 0)
49 v |= mask;
Chris Craikb50c2172013-07-29 15:28:30 -070050
The Android Open Source Project893912b2009-03-03 19:30:05 -080051 sp++;
Chris Craikb50c2172013-07-29 15:28:30 -070052
The Android Open Source Project893912b2009-03-03 19:30:05 -080053 if (mask > 1)
54 mask >>= 1;
Chris Craikb50c2172013-07-29 15:28:30 -070055
The Android Open Source Project893912b2009-03-03 19:30:05 -080056 else
57 {
58 mask = 0x80;
59 *dp = (png_byte)v;
60 dp++;
61 v = 0;
62 }
63 }
Chris Craikb50c2172013-07-29 15:28:30 -070064
The Android Open Source Project893912b2009-03-03 19:30:05 -080065 if (mask != 0x80)
66 *dp = (png_byte)v;
Chris Craikb50c2172013-07-29 15:28:30 -070067
The Android Open Source Project893912b2009-03-03 19:30:05 -080068 break;
69 }
Chris Craikb50c2172013-07-29 15:28:30 -070070
The Android Open Source Project893912b2009-03-03 19:30:05 -080071 case 2:
72 {
73 png_bytep sp, dp;
Matt Sarett96be9082016-05-03 13:29:54 -040074 unsigned int shift;
75 int v;
The Android Open Source Project893912b2009-03-03 19:30:05 -080076 png_uint_32 i;
77 png_uint_32 row_width = row_info->width;
78
79 sp = row;
80 dp = row;
81 shift = 6;
82 v = 0;
Chris Craikb50c2172013-07-29 15:28:30 -070083
The Android Open Source Project893912b2009-03-03 19:30:05 -080084 for (i = 0; i < row_width; i++)
85 {
86 png_byte value;
87
88 value = (png_byte)(*sp & 0x03);
89 v |= (value << shift);
Chris Craikb50c2172013-07-29 15:28:30 -070090
The Android Open Source Project893912b2009-03-03 19:30:05 -080091 if (shift == 0)
92 {
93 shift = 6;
94 *dp = (png_byte)v;
95 dp++;
96 v = 0;
97 }
Chris Craikb50c2172013-07-29 15:28:30 -070098
The Android Open Source Project893912b2009-03-03 19:30:05 -080099 else
100 shift -= 2;
Chris Craikb50c2172013-07-29 15:28:30 -0700101
The Android Open Source Project893912b2009-03-03 19:30:05 -0800102 sp++;
103 }
Chris Craikb50c2172013-07-29 15:28:30 -0700104
The Android Open Source Project893912b2009-03-03 19:30:05 -0800105 if (shift != 6)
106 *dp = (png_byte)v;
Chris Craikb50c2172013-07-29 15:28:30 -0700107
The Android Open Source Project893912b2009-03-03 19:30:05 -0800108 break;
109 }
Chris Craikb50c2172013-07-29 15:28:30 -0700110
The Android Open Source Project893912b2009-03-03 19:30:05 -0800111 case 4:
112 {
113 png_bytep sp, dp;
Matt Sarett96be9082016-05-03 13:29:54 -0400114 unsigned int shift;
115 int v;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800116 png_uint_32 i;
117 png_uint_32 row_width = row_info->width;
118
119 sp = row;
120 dp = row;
121 shift = 4;
122 v = 0;
Chris Craikb50c2172013-07-29 15:28:30 -0700123
The Android Open Source Project893912b2009-03-03 19:30:05 -0800124 for (i = 0; i < row_width; i++)
125 {
126 png_byte value;
127
128 value = (png_byte)(*sp & 0x0f);
129 v |= (value << shift);
130
131 if (shift == 0)
132 {
133 shift = 4;
134 *dp = (png_byte)v;
135 dp++;
136 v = 0;
137 }
Chris Craikb50c2172013-07-29 15:28:30 -0700138
The Android Open Source Project893912b2009-03-03 19:30:05 -0800139 else
140 shift -= 4;
141
142 sp++;
143 }
Chris Craikb50c2172013-07-29 15:28:30 -0700144
The Android Open Source Project893912b2009-03-03 19:30:05 -0800145 if (shift != 4)
146 *dp = (png_byte)v;
Chris Craikb50c2172013-07-29 15:28:30 -0700147
The Android Open Source Project893912b2009-03-03 19:30:05 -0800148 break;
149 }
Chris Craikb50c2172013-07-29 15:28:30 -0700150
151 default:
152 break;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800153 }
Chris Craikb50c2172013-07-29 15:28:30 -0700154
The Android Open Source Project893912b2009-03-03 19:30:05 -0800155 row_info->bit_depth = (png_byte)bit_depth;
156 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
157 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
Chris Craikb50c2172013-07-29 15:28:30 -0700158 row_info->width);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800159 }
160}
161#endif
162
Patrick Scott5f6bd842010-06-28 16:55:16 -0400163#ifdef PNG_WRITE_SHIFT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800164/* Shift pixel values to take advantage of whole range. Pass the
165 * true number of bits in bit_depth. The row should be packed
166 * according to row_info->bit_depth. Thus, if you had a row of
167 * bit depth 4, but the pixels only had values from 0 to 7, you
168 * would pass 3 as bit_depth, and this routine would translate the
169 * data to 0 to 15.
170 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530171static void
Chris Craikb50c2172013-07-29 15:28:30 -0700172png_do_shift(png_row_infop row_info, png_bytep row,
173 png_const_color_8p bit_depth)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800174{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700175 png_debug(1, "in png_do_shift");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400176
Chris Craikb50c2172013-07-29 15:28:30 -0700177 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800178 {
179 int shift_start[4], shift_dec[4];
180 int channels = 0;
181
Matt Sarett96be9082016-05-03 13:29:54 -0400182 if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800183 {
184 shift_start[channels] = row_info->bit_depth - bit_depth->red;
185 shift_dec[channels] = bit_depth->red;
186 channels++;
Chris Craikb50c2172013-07-29 15:28:30 -0700187
The Android Open Source Project893912b2009-03-03 19:30:05 -0800188 shift_start[channels] = row_info->bit_depth - bit_depth->green;
189 shift_dec[channels] = bit_depth->green;
190 channels++;
Chris Craikb50c2172013-07-29 15:28:30 -0700191
The Android Open Source Project893912b2009-03-03 19:30:05 -0800192 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
193 shift_dec[channels] = bit_depth->blue;
194 channels++;
195 }
Chris Craikb50c2172013-07-29 15:28:30 -0700196
The Android Open Source Project893912b2009-03-03 19:30:05 -0800197 else
198 {
199 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
200 shift_dec[channels] = bit_depth->gray;
201 channels++;
202 }
Chris Craikb50c2172013-07-29 15:28:30 -0700203
Matt Sarett96be9082016-05-03 13:29:54 -0400204 if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800205 {
206 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
207 shift_dec[channels] = bit_depth->alpha;
208 channels++;
209 }
210
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400211 /* With low row depths, could only be grayscale, so one channel */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800212 if (row_info->bit_depth < 8)
213 {
214 png_bytep bp = row;
Chris Craikb50c2172013-07-29 15:28:30 -0700215 png_size_t i;
216 unsigned int mask;
217 png_size_t row_bytes = row_info->rowbytes;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800218
219 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
220 mask = 0x55;
Chris Craikb50c2172013-07-29 15:28:30 -0700221
The Android Open Source Project893912b2009-03-03 19:30:05 -0800222 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
223 mask = 0x11;
Chris Craikb50c2172013-07-29 15:28:30 -0700224
The Android Open Source Project893912b2009-03-03 19:30:05 -0800225 else
226 mask = 0xff;
227
228 for (i = 0; i < row_bytes; i++, bp++)
229 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800230 int j;
Chris Craikb50c2172013-07-29 15:28:30 -0700231 unsigned int v, out;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800232
233 v = *bp;
Chris Craikb50c2172013-07-29 15:28:30 -0700234 out = 0;
235
The Android Open Source Project893912b2009-03-03 19:30:05 -0800236 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
237 {
238 if (j > 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700239 out |= v << j;
240
The Android Open Source Project893912b2009-03-03 19:30:05 -0800241 else
Chris Craikb50c2172013-07-29 15:28:30 -0700242 out |= (v >> (-j)) & mask;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800243 }
Chris Craikb50c2172013-07-29 15:28:30 -0700244
245 *bp = (png_byte)(out & 0xff);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800246 }
247 }
Chris Craikb50c2172013-07-29 15:28:30 -0700248
The Android Open Source Project893912b2009-03-03 19:30:05 -0800249 else if (row_info->bit_depth == 8)
250 {
251 png_bytep bp = row;
252 png_uint_32 i;
253 png_uint_32 istop = channels * row_info->width;
254
255 for (i = 0; i < istop; i++, bp++)
256 {
257
Chris Craikb50c2172013-07-29 15:28:30 -0700258 const unsigned int c = i%channels;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800259 int j;
Chris Craikb50c2172013-07-29 15:28:30 -0700260 unsigned int v, out;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800261
262 v = *bp;
Chris Craikb50c2172013-07-29 15:28:30 -0700263 out = 0;
264
The Android Open Source Project893912b2009-03-03 19:30:05 -0800265 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
266 {
267 if (j > 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700268 out |= v << j;
269
The Android Open Source Project893912b2009-03-03 19:30:05 -0800270 else
Chris Craikb50c2172013-07-29 15:28:30 -0700271 out |= v >> (-j);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800272 }
Chris Craikb50c2172013-07-29 15:28:30 -0700273
274 *bp = (png_byte)(out & 0xff);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800275 }
276 }
Chris Craikb50c2172013-07-29 15:28:30 -0700277
The Android Open Source Project893912b2009-03-03 19:30:05 -0800278 else
279 {
280 png_bytep bp;
281 png_uint_32 i;
282 png_uint_32 istop = channels * row_info->width;
283
284 for (bp = row, i = 0; i < istop; i++)
285 {
Chris Craikb50c2172013-07-29 15:28:30 -0700286 const unsigned int c = i%channels;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800287 int j;
Chris Craikb50c2172013-07-29 15:28:30 -0700288 unsigned int value, v;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800289
Chris Craikb50c2172013-07-29 15:28:30 -0700290 v = png_get_uint_16(bp);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800291 value = 0;
Chris Craikb50c2172013-07-29 15:28:30 -0700292
The Android Open Source Project893912b2009-03-03 19:30:05 -0800293 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
294 {
295 if (j > 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700296 value |= v << j;
297
The Android Open Source Project893912b2009-03-03 19:30:05 -0800298 else
Chris Craikb50c2172013-07-29 15:28:30 -0700299 value |= v >> (-j);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800300 }
Chris Craikb50c2172013-07-29 15:28:30 -0700301 *bp++ = (png_byte)((value >> 8) & 0xff);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800302 *bp++ = (png_byte)(value & 0xff);
303 }
304 }
305 }
306}
307#endif
308
Patrick Scott5f6bd842010-06-28 16:55:16 -0400309#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530310static void
The Android Open Source Project893912b2009-03-03 19:30:05 -0800311png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
312{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700313 png_debug(1, "in png_do_write_swap_alpha");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400314
The Android Open Source Project893912b2009-03-03 19:30:05 -0800315 {
316 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
317 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800318 if (row_info->bit_depth == 8)
319 {
Chris Craikb50c2172013-07-29 15:28:30 -0700320 /* This converts from ARGB to RGBA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800321 png_bytep sp, dp;
322 png_uint_32 i;
323 png_uint_32 row_width = row_info->width;
Chris Craikb50c2172013-07-29 15:28:30 -0700324
The Android Open Source Project893912b2009-03-03 19:30:05 -0800325 for (i = 0, sp = dp = row; i < row_width; i++)
326 {
327 png_byte save = *(sp++);
328 *(dp++) = *(sp++);
329 *(dp++) = *(sp++);
330 *(dp++) = *(sp++);
331 *(dp++) = save;
332 }
333 }
Chris Craikb50c2172013-07-29 15:28:30 -0700334
335#ifdef PNG_WRITE_16BIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800336 else
337 {
Chris Craikb50c2172013-07-29 15:28:30 -0700338 /* This converts from AARRGGBB to RRGGBBAA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800339 png_bytep sp, dp;
340 png_uint_32 i;
341 png_uint_32 row_width = row_info->width;
342
343 for (i = 0, sp = dp = row; i < row_width; i++)
344 {
345 png_byte save[2];
346 save[0] = *(sp++);
347 save[1] = *(sp++);
348 *(dp++) = *(sp++);
349 *(dp++) = *(sp++);
350 *(dp++) = *(sp++);
351 *(dp++) = *(sp++);
352 *(dp++) = *(sp++);
353 *(dp++) = *(sp++);
354 *(dp++) = save[0];
355 *(dp++) = save[1];
356 }
357 }
Matt Sarett96be9082016-05-03 13:29:54 -0400358#endif /* WRITE_16BIT */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800359 }
Chris Craikb50c2172013-07-29 15:28:30 -0700360
The Android Open Source Project893912b2009-03-03 19:30:05 -0800361 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
362 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800363 if (row_info->bit_depth == 8)
364 {
Chris Craikb50c2172013-07-29 15:28:30 -0700365 /* This converts from AG to GA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800366 png_bytep sp, dp;
367 png_uint_32 i;
368 png_uint_32 row_width = row_info->width;
369
370 for (i = 0, sp = dp = row; i < row_width; i++)
371 {
372 png_byte save = *(sp++);
373 *(dp++) = *(sp++);
374 *(dp++) = save;
375 }
376 }
Chris Craikb50c2172013-07-29 15:28:30 -0700377
378#ifdef PNG_WRITE_16BIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800379 else
380 {
Chris Craikb50c2172013-07-29 15:28:30 -0700381 /* This converts from AAGG to GGAA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800382 png_bytep sp, dp;
383 png_uint_32 i;
384 png_uint_32 row_width = row_info->width;
385
386 for (i = 0, sp = dp = row; i < row_width; i++)
387 {
388 png_byte save[2];
389 save[0] = *(sp++);
390 save[1] = *(sp++);
391 *(dp++) = *(sp++);
392 *(dp++) = *(sp++);
393 *(dp++) = save[0];
394 *(dp++) = save[1];
395 }
396 }
Matt Sarett96be9082016-05-03 13:29:54 -0400397#endif /* WRITE_16BIT */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800398 }
399 }
400}
401#endif
402
Patrick Scott5f6bd842010-06-28 16:55:16 -0400403#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530404static void
The Android Open Source Project893912b2009-03-03 19:30:05 -0800405png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
406{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700407 png_debug(1, "in png_do_write_invert_alpha");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400408
The Android Open Source Project893912b2009-03-03 19:30:05 -0800409 {
410 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
411 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800412 if (row_info->bit_depth == 8)
413 {
Chris Craikb50c2172013-07-29 15:28:30 -0700414 /* This inverts the alpha channel in RGBA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800415 png_bytep sp, dp;
416 png_uint_32 i;
417 png_uint_32 row_width = row_info->width;
Chris Craikb50c2172013-07-29 15:28:30 -0700418
The Android Open Source Project893912b2009-03-03 19:30:05 -0800419 for (i = 0, sp = dp = row; i < row_width; i++)
420 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400421 /* Does nothing
The Android Open Source Project893912b2009-03-03 19:30:05 -0800422 *(dp++) = *(sp++);
423 *(dp++) = *(sp++);
424 *(dp++) = *(sp++);
425 */
426 sp+=3; dp = sp;
Matt Sarett96be9082016-05-03 13:29:54 -0400427 *dp = (png_byte)(255 - *(sp++));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800428 }
429 }
Chris Craikb50c2172013-07-29 15:28:30 -0700430
431#ifdef PNG_WRITE_16BIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800432 else
433 {
Chris Craikb50c2172013-07-29 15:28:30 -0700434 /* This inverts the alpha channel in RRGGBBAA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800435 png_bytep sp, dp;
436 png_uint_32 i;
437 png_uint_32 row_width = row_info->width;
438
439 for (i = 0, sp = dp = row; i < row_width; i++)
440 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400441 /* Does nothing
The Android Open Source Project893912b2009-03-03 19:30:05 -0800442 *(dp++) = *(sp++);
443 *(dp++) = *(sp++);
444 *(dp++) = *(sp++);
445 *(dp++) = *(sp++);
446 *(dp++) = *(sp++);
447 *(dp++) = *(sp++);
448 */
449 sp+=6; dp = sp;
450 *(dp++) = (png_byte)(255 - *(sp++));
Matt Sarett96be9082016-05-03 13:29:54 -0400451 *dp = (png_byte)(255 - *(sp++));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800452 }
453 }
Matt Sarett96be9082016-05-03 13:29:54 -0400454#endif /* WRITE_16BIT */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800455 }
Chris Craikb50c2172013-07-29 15:28:30 -0700456
The Android Open Source Project893912b2009-03-03 19:30:05 -0800457 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
458 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800459 if (row_info->bit_depth == 8)
460 {
Chris Craikb50c2172013-07-29 15:28:30 -0700461 /* This inverts the alpha channel in GA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800462 png_bytep sp, dp;
463 png_uint_32 i;
464 png_uint_32 row_width = row_info->width;
465
466 for (i = 0, sp = dp = row; i < row_width; i++)
467 {
468 *(dp++) = *(sp++);
469 *(dp++) = (png_byte)(255 - *(sp++));
470 }
471 }
Chris Craikb50c2172013-07-29 15:28:30 -0700472
473#ifdef PNG_WRITE_16BIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800474 else
475 {
Chris Craikb50c2172013-07-29 15:28:30 -0700476 /* This inverts the alpha channel in GGAA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800477 png_bytep sp, dp;
478 png_uint_32 i;
479 png_uint_32 row_width = row_info->width;
480
481 for (i = 0, sp = dp = row; i < row_width; i++)
482 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400483 /* Does nothing
The Android Open Source Project893912b2009-03-03 19:30:05 -0800484 *(dp++) = *(sp++);
485 *(dp++) = *(sp++);
486 */
487 sp+=2; dp = sp;
488 *(dp++) = (png_byte)(255 - *(sp++));
Matt Sarett96be9082016-05-03 13:29:54 -0400489 *dp = (png_byte)(255 - *(sp++));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800490 }
491 }
Matt Sarett96be9082016-05-03 13:29:54 -0400492#endif /* WRITE_16BIT */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800493 }
494 }
495}
496#endif
497
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530498/* Transform the data according to the user's wishes. The order of
499 * transformations is significant.
500 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800501void /* PRIVATE */
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530502png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800503{
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530504 png_debug(1, "in png_do_write_transformations");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400505
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530506 if (png_ptr == NULL)
507 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800508
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530509#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400510 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530511 if (png_ptr->write_user_transform_fn != NULL)
512 (*(png_ptr->write_user_transform_fn)) /* User write transform
513 function */
514 (png_ptr, /* png_ptr */
515 row_info, /* row_info: */
516 /* png_uint_32 width; width of row */
517 /* png_size_t rowbytes; number of bytes in row */
518 /* png_byte color_type; color type of pixels */
519 /* png_byte bit_depth; bit depth of samples */
520 /* png_byte channels; number of channels (1-4) */
521 /* png_byte pixel_depth; bits per pixel (depth*channels) */
522 png_ptr->row_buf + 1); /* start of pixel data for row */
523#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700524
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530525#ifdef PNG_WRITE_FILLER_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400526 if ((png_ptr->transformations & PNG_FILLER) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530527 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
528 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
529#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700530
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530531#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400532 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530533 png_do_packswap(row_info, png_ptr->row_buf + 1);
534#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800535
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530536#ifdef PNG_WRITE_PACK_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400537 if ((png_ptr->transformations & PNG_PACK) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530538 png_do_pack(row_info, png_ptr->row_buf + 1,
539 (png_uint_32)png_ptr->bit_depth);
540#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700541
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530542#ifdef PNG_WRITE_SWAP_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400543# ifdef PNG_16BIT_SUPPORTED
544 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530545 png_do_swap(row_info, png_ptr->row_buf + 1);
Matt Sarett96be9082016-05-03 13:29:54 -0400546# endif
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530547#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800548
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530549#ifdef PNG_WRITE_SHIFT_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400550 if ((png_ptr->transformations & PNG_SHIFT) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530551 png_do_shift(row_info, png_ptr->row_buf + 1,
552 &(png_ptr->shift));
553#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700554
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530555#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400556 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530557 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
558#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700559
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530560#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400561 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530562 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
563#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800564
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530565#ifdef PNG_WRITE_BGR_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400566 if ((png_ptr->transformations & PNG_BGR) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530567 png_do_bgr(row_info, png_ptr->row_buf + 1);
568#endif
569
570#ifdef PNG_WRITE_INVERT_SUPPORTED
Matt Sarett96be9082016-05-03 13:29:54 -0400571 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530572 png_do_invert(row_info, png_ptr->row_buf + 1);
573#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800574}
Matt Sarett96be9082016-05-03 13:29:54 -0400575#endif /* WRITE_TRANSFORMS */
576#endif /* WRITE */