blob: 49a13c1e98d993818f85a77a509965a2adf8ee3b [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 *
xNombred07bb0d2020-03-10 20:17:12 +01004 * Copyright (c) 2018 Cosmin Truta
5 * Copyright (c) 1998-2002,2004,2006-2016,2018 Glenn Randers-Pehrson
6 * Copyright (c) 1996-1997 Andreas Dilger
7 * 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 Sarett9b1fe632015-11-25 10:21:17 -050074 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 Sarett9b1fe632015-11-25 10:21:17 -0500114 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];
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400180 unsigned int channels = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800181
Matt Sarett9b1fe632015-11-25 10:21:17 -0500182 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 Sarett9b1fe632015-11-25 10:21:17 -0500204 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;
xNombred07bb0d2020-03-10 20:17:12 +0100215 size_t i;
Chris Craikb50c2172013-07-29 15:28:30 -0700216 unsigned int mask;
xNombred07bb0d2020-03-10 20:17:12 +0100217 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 {
xNombred07bb0d2020-03-10 20:17:12 +0100257 unsigned int c = i%channels;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800258 int j;
Chris Craikb50c2172013-07-29 15:28:30 -0700259 unsigned int v, out;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800260
261 v = *bp;
Chris Craikb50c2172013-07-29 15:28:30 -0700262 out = 0;
263
The Android Open Source Project893912b2009-03-03 19:30:05 -0800264 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
265 {
266 if (j > 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700267 out |= v << j;
268
The Android Open Source Project893912b2009-03-03 19:30:05 -0800269 else
Chris Craikb50c2172013-07-29 15:28:30 -0700270 out |= v >> (-j);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800271 }
Chris Craikb50c2172013-07-29 15:28:30 -0700272
273 *bp = (png_byte)(out & 0xff);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800274 }
275 }
Chris Craikb50c2172013-07-29 15:28:30 -0700276
The Android Open Source Project893912b2009-03-03 19:30:05 -0800277 else
278 {
279 png_bytep bp;
280 png_uint_32 i;
281 png_uint_32 istop = channels * row_info->width;
282
283 for (bp = row, i = 0; i < istop; i++)
284 {
xNombred07bb0d2020-03-10 20:17:12 +0100285 unsigned int c = i%channels;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800286 int j;
Chris Craikb50c2172013-07-29 15:28:30 -0700287 unsigned int value, v;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800288
Chris Craikb50c2172013-07-29 15:28:30 -0700289 v = png_get_uint_16(bp);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800290 value = 0;
Chris Craikb50c2172013-07-29 15:28:30 -0700291
The Android Open Source Project893912b2009-03-03 19:30:05 -0800292 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
293 {
294 if (j > 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700295 value |= v << j;
296
The Android Open Source Project893912b2009-03-03 19:30:05 -0800297 else
Chris Craikb50c2172013-07-29 15:28:30 -0700298 value |= v >> (-j);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800299 }
Chris Craikb50c2172013-07-29 15:28:30 -0700300 *bp++ = (png_byte)((value >> 8) & 0xff);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800301 *bp++ = (png_byte)(value & 0xff);
302 }
303 }
304 }
305}
306#endif
307
Patrick Scott5f6bd842010-06-28 16:55:16 -0400308#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530309static void
The Android Open Source Project893912b2009-03-03 19:30:05 -0800310png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
311{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700312 png_debug(1, "in png_do_write_swap_alpha");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400313
The Android Open Source Project893912b2009-03-03 19:30:05 -0800314 {
315 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
316 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800317 if (row_info->bit_depth == 8)
318 {
Chris Craikb50c2172013-07-29 15:28:30 -0700319 /* This converts from ARGB to RGBA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800320 png_bytep sp, dp;
321 png_uint_32 i;
322 png_uint_32 row_width = row_info->width;
Chris Craikb50c2172013-07-29 15:28:30 -0700323
The Android Open Source Project893912b2009-03-03 19:30:05 -0800324 for (i = 0, sp = dp = row; i < row_width; i++)
325 {
326 png_byte save = *(sp++);
327 *(dp++) = *(sp++);
328 *(dp++) = *(sp++);
329 *(dp++) = *(sp++);
330 *(dp++) = save;
331 }
332 }
Chris Craikb50c2172013-07-29 15:28:30 -0700333
334#ifdef PNG_WRITE_16BIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800335 else
336 {
Chris Craikb50c2172013-07-29 15:28:30 -0700337 /* This converts from AARRGGBB to RRGGBBAA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800338 png_bytep sp, dp;
339 png_uint_32 i;
340 png_uint_32 row_width = row_info->width;
341
342 for (i = 0, sp = dp = row; i < row_width; i++)
343 {
344 png_byte save[2];
345 save[0] = *(sp++);
346 save[1] = *(sp++);
347 *(dp++) = *(sp++);
348 *(dp++) = *(sp++);
349 *(dp++) = *(sp++);
350 *(dp++) = *(sp++);
351 *(dp++) = *(sp++);
352 *(dp++) = *(sp++);
353 *(dp++) = save[0];
354 *(dp++) = save[1];
355 }
356 }
Matt Sarett9b1fe632015-11-25 10:21:17 -0500357#endif /* WRITE_16BIT */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800358 }
Chris Craikb50c2172013-07-29 15:28:30 -0700359
The Android Open Source Project893912b2009-03-03 19:30:05 -0800360 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
361 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800362 if (row_info->bit_depth == 8)
363 {
Chris Craikb50c2172013-07-29 15:28:30 -0700364 /* This converts from AG to GA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800365 png_bytep sp, dp;
366 png_uint_32 i;
367 png_uint_32 row_width = row_info->width;
368
369 for (i = 0, sp = dp = row; i < row_width; i++)
370 {
371 png_byte save = *(sp++);
372 *(dp++) = *(sp++);
373 *(dp++) = save;
374 }
375 }
Chris Craikb50c2172013-07-29 15:28:30 -0700376
377#ifdef PNG_WRITE_16BIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800378 else
379 {
Chris Craikb50c2172013-07-29 15:28:30 -0700380 /* This converts from AAGG to GGAA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800381 png_bytep sp, dp;
382 png_uint_32 i;
383 png_uint_32 row_width = row_info->width;
384
385 for (i = 0, sp = dp = row; i < row_width; i++)
386 {
387 png_byte save[2];
388 save[0] = *(sp++);
389 save[1] = *(sp++);
390 *(dp++) = *(sp++);
391 *(dp++) = *(sp++);
392 *(dp++) = save[0];
393 *(dp++) = save[1];
394 }
395 }
Matt Sarett9b1fe632015-11-25 10:21:17 -0500396#endif /* WRITE_16BIT */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800397 }
398 }
399}
400#endif
401
Patrick Scott5f6bd842010-06-28 16:55:16 -0400402#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530403static void
The Android Open Source Project893912b2009-03-03 19:30:05 -0800404png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
405{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700406 png_debug(1, "in png_do_write_invert_alpha");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400407
The Android Open Source Project893912b2009-03-03 19:30:05 -0800408 {
409 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
410 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800411 if (row_info->bit_depth == 8)
412 {
Chris Craikb50c2172013-07-29 15:28:30 -0700413 /* This inverts the alpha channel in RGBA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800414 png_bytep sp, dp;
415 png_uint_32 i;
416 png_uint_32 row_width = row_info->width;
Chris Craikb50c2172013-07-29 15:28:30 -0700417
The Android Open Source Project893912b2009-03-03 19:30:05 -0800418 for (i = 0, sp = dp = row; i < row_width; i++)
419 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400420 /* Does nothing
The Android Open Source Project893912b2009-03-03 19:30:05 -0800421 *(dp++) = *(sp++);
422 *(dp++) = *(sp++);
423 *(dp++) = *(sp++);
424 */
425 sp+=3; dp = sp;
Matt Sarett9b1fe632015-11-25 10:21:17 -0500426 *dp = (png_byte)(255 - *(sp++));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800427 }
428 }
Chris Craikb50c2172013-07-29 15:28:30 -0700429
430#ifdef PNG_WRITE_16BIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800431 else
432 {
Chris Craikb50c2172013-07-29 15:28:30 -0700433 /* This inverts the alpha channel in RRGGBBAA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800434 png_bytep sp, dp;
435 png_uint_32 i;
436 png_uint_32 row_width = row_info->width;
437
438 for (i = 0, sp = dp = row; i < row_width; i++)
439 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400440 /* Does nothing
The Android Open Source Project893912b2009-03-03 19:30:05 -0800441 *(dp++) = *(sp++);
442 *(dp++) = *(sp++);
443 *(dp++) = *(sp++);
444 *(dp++) = *(sp++);
445 *(dp++) = *(sp++);
446 *(dp++) = *(sp++);
447 */
448 sp+=6; dp = sp;
449 *(dp++) = (png_byte)(255 - *(sp++));
Matt Sarett9b1fe632015-11-25 10:21:17 -0500450 *dp = (png_byte)(255 - *(sp++));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800451 }
452 }
Matt Sarett9b1fe632015-11-25 10:21:17 -0500453#endif /* WRITE_16BIT */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800454 }
Chris Craikb50c2172013-07-29 15:28:30 -0700455
The Android Open Source Project893912b2009-03-03 19:30:05 -0800456 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
457 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800458 if (row_info->bit_depth == 8)
459 {
Chris Craikb50c2172013-07-29 15:28:30 -0700460 /* This inverts the alpha channel in GA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800461 png_bytep sp, dp;
462 png_uint_32 i;
463 png_uint_32 row_width = row_info->width;
464
465 for (i = 0, sp = dp = row; i < row_width; i++)
466 {
467 *(dp++) = *(sp++);
468 *(dp++) = (png_byte)(255 - *(sp++));
469 }
470 }
Chris Craikb50c2172013-07-29 15:28:30 -0700471
472#ifdef PNG_WRITE_16BIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800473 else
474 {
Chris Craikb50c2172013-07-29 15:28:30 -0700475 /* This inverts the alpha channel in GGAA */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800476 png_bytep sp, dp;
477 png_uint_32 i;
478 png_uint_32 row_width = row_info->width;
479
480 for (i = 0, sp = dp = row; i < row_width; i++)
481 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400482 /* Does nothing
The Android Open Source Project893912b2009-03-03 19:30:05 -0800483 *(dp++) = *(sp++);
484 *(dp++) = *(sp++);
485 */
486 sp+=2; dp = sp;
487 *(dp++) = (png_byte)(255 - *(sp++));
Matt Sarett9b1fe632015-11-25 10:21:17 -0500488 *dp = (png_byte)(255 - *(sp++));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800489 }
490 }
Matt Sarett9b1fe632015-11-25 10:21:17 -0500491#endif /* WRITE_16BIT */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800492 }
493 }
494}
495#endif
496
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530497/* Transform the data according to the user's wishes. The order of
498 * transformations is significant.
499 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800500void /* PRIVATE */
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530501png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800502{
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530503 png_debug(1, "in png_do_write_transformations");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400504
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530505 if (png_ptr == NULL)
506 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800507
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530508#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -0500509 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530510 if (png_ptr->write_user_transform_fn != NULL)
511 (*(png_ptr->write_user_transform_fn)) /* User write transform
512 function */
513 (png_ptr, /* png_ptr */
514 row_info, /* row_info: */
515 /* png_uint_32 width; width of row */
xNombred07bb0d2020-03-10 20:17:12 +0100516 /* size_t rowbytes; number of bytes in row */
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530517 /* png_byte color_type; color type of pixels */
518 /* png_byte bit_depth; bit depth of samples */
519 /* png_byte channels; number of channels (1-4) */
520 /* png_byte pixel_depth; bits per pixel (depth*channels) */
521 png_ptr->row_buf + 1); /* start of pixel data for row */
522#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700523
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530524#ifdef PNG_WRITE_FILLER_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -0500525 if ((png_ptr->transformations & PNG_FILLER) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530526 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
Alex Naidis7a055fd2016-10-01 12:23:07 +0200527 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530528#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700529
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530530#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -0500531 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530532 png_do_packswap(row_info, png_ptr->row_buf + 1);
533#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800534
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530535#ifdef PNG_WRITE_PACK_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -0500536 if ((png_ptr->transformations & PNG_PACK) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530537 png_do_pack(row_info, png_ptr->row_buf + 1,
538 (png_uint_32)png_ptr->bit_depth);
539#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700540
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530541#ifdef PNG_WRITE_SWAP_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -0500542# ifdef PNG_16BIT_SUPPORTED
543 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530544 png_do_swap(row_info, png_ptr->row_buf + 1);
Matt Sarett9b1fe632015-11-25 10:21:17 -0500545# endif
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530546#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800547
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530548#ifdef PNG_WRITE_SHIFT_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -0500549 if ((png_ptr->transformations & PNG_SHIFT) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530550 png_do_shift(row_info, png_ptr->row_buf + 1,
Alex Naidis7a055fd2016-10-01 12:23:07 +0200551 &(png_ptr->shift));
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530552#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700553
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530554#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -0500555 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530556 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
557#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700558
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530559#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -0500560 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530561 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
562#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800563
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530564#ifdef PNG_WRITE_BGR_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -0500565 if ((png_ptr->transformations & PNG_BGR) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530566 png_do_bgr(row_info, png_ptr->row_buf + 1);
567#endif
568
569#ifdef PNG_WRITE_INVERT_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -0500570 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530571 png_do_invert(row_info, png_ptr->row_buf + 1);
572#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800573}
Matt Sarett9b1fe632015-11-25 10:21:17 -0500574#endif /* WRITE_TRANSFORMS */
575#endif /* WRITE */