blob: 49a13c1e98d993818f85a77a509965a2adf8ee3b [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 *
Cosmin Trutaa8738932018-07-28 18:47:21 -04004 * Copyright (c) 2018 Cosmin Truta
Cosmin Truta46aedd82018-07-15 23:58:00 -04005 * Copyright (c) 1998-2002,2004,2006-2016,2018 Glenn Randers-Pehrson
Cosmin Trutaa8738932018-07-28 18:47:21 -04006 * Copyright (c) 1996-1997 Andreas Dilger
7 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050014#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050015
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060016#ifdef PNG_WRITE_SUPPORTED
John Bowler4a12f4a2011-04-17 18:34:22 -050017#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050018
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050019#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060020/* 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 */
John Bowler8f1150e2013-12-19 15:33:49 -060024static void
Andreas Dilger47a0c421997-05-16 02:46:07 -050025png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050026{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050027 png_debug(1, "in png_do_pack");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050028
Andreas Dilger47a0c421997-05-16 02:46:07 -050029 if (row_info->bit_depth == 8 &&
Guy Schalnat0d580581995-07-20 02:43:20 -050030 row_info->channels == 1)
31 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050032 switch ((int)bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050033 {
34 case 1:
35 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050036 png_bytep sp, dp;
37 int mask, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -050038 png_uint_32 i;
39 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -050040
41 sp = row;
42 dp = row;
43 mask = 0x80;
44 v = 0;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060045
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -050046 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -050047 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050048 if (*sp != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -050049 v |= mask;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050050
Guy Schalnat0d580581995-07-20 02:43:20 -050051 sp++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050052
Guy Schalnat0d580581995-07-20 02:43:20 -050053 if (mask > 1)
54 mask >>= 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050055
Guy Schalnat0d580581995-07-20 02:43:20 -050056 else
57 {
58 mask = 0x80;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -050059 *dp = (png_byte)v;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060060 dp++;
Guy Schalnat0d580581995-07-20 02:43:20 -050061 v = 0;
62 }
63 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050064
Guy Schalnat0d580581995-07-20 02:43:20 -050065 if (mask != 0x80)
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -050066 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050067
Guy Schalnat0d580581995-07-20 02:43:20 -050068 break;
69 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -050070
Guy Schalnat0d580581995-07-20 02:43:20 -050071 case 2:
72 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050073 png_bytep sp, dp;
John Bowlerb780eba2015-06-03 14:46:34 -050074 unsigned int shift;
75 int v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -050076 png_uint_32 i;
77 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -050078
79 sp = row;
80 dp = row;
81 shift = 6;
82 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050083
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -050084 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -050085 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050086 png_byte value;
87
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060088 value = (png_byte)(*sp & 0x03);
Guy Schalnat0d580581995-07-20 02:43:20 -050089 v |= (value << shift);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050090
Guy Schalnat0d580581995-07-20 02:43:20 -050091 if (shift == 0)
92 {
93 shift = 6;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -050094 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -050095 dp++;
96 v = 0;
97 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050098
Guy Schalnat0d580581995-07-20 02:43:20 -050099 else
100 shift -= 2;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500101
Guy Schalnat0d580581995-07-20 02:43:20 -0500102 sp++;
103 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500104
Guy Schalnat0d580581995-07-20 02:43:20 -0500105 if (shift != 6)
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500106 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500107
Guy Schalnat0d580581995-07-20 02:43:20 -0500108 break;
109 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500110
Guy Schalnat0d580581995-07-20 02:43:20 -0500111 case 4:
112 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500113 png_bytep sp, dp;
John Bowlerb780eba2015-06-03 14:46:34 -0500114 unsigned int shift;
115 int v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500116 png_uint_32 i;
117 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500118
119 sp = row;
120 dp = row;
121 shift = 4;
122 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500123
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500124 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500125 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500126 png_byte value;
127
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600128 value = (png_byte)(*sp & 0x0f);
Guy Schalnat0d580581995-07-20 02:43:20 -0500129 v |= (value << shift);
130
131 if (shift == 0)
132 {
133 shift = 4;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500134 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500135 dp++;
136 v = 0;
137 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500138
Guy Schalnat0d580581995-07-20 02:43:20 -0500139 else
140 shift -= 4;
141
142 sp++;
143 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500144
Guy Schalnat0d580581995-07-20 02:43:20 -0500145 if (shift != 4)
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500146 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500147
Guy Schalnat0d580581995-07-20 02:43:20 -0500148 break;
149 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600150
151 default:
152 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500153 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500154
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500155 row_info->bit_depth = (png_byte)bit_depth;
156 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500157 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600158 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500159 }
160}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500161#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500162
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500163#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600164/* 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 */
John Bowler8f1150e2013-12-19 15:33:49 -0600171static void
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500172png_do_shift(png_row_infop row_info, png_bytep row,
173 png_const_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500174{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500175 png_debug(1, "in png_do_shift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500176
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500177 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500178 {
179 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -0500180 unsigned int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500181
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500182 if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500183 {
184 shift_start[channels] = row_info->bit_depth - bit_depth->red;
185 shift_dec[channels] = bit_depth->red;
186 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500187
Guy Schalnat0d580581995-07-20 02:43:20 -0500188 shift_start[channels] = row_info->bit_depth - bit_depth->green;
189 shift_dec[channels] = bit_depth->green;
190 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500191
Guy Schalnat0d580581995-07-20 02:43:20 -0500192 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
193 shift_dec[channels] = bit_depth->blue;
194 channels++;
195 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500196
Guy Schalnat0d580581995-07-20 02:43:20 -0500197 else
198 {
199 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
200 shift_dec[channels] = bit_depth->gray;
201 channels++;
202 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500203
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500204 if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500205 {
206 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
207 shift_dec[channels] = bit_depth->alpha;
208 channels++;
209 }
210
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500211 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500212 if (row_info->bit_depth < 8)
213 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500214 png_bytep bp = row;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400215 size_t i;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600216 unsigned int mask;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400217 size_t row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500218
219 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
220 mask = 0x55;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500221
Guy Schalnat0d580581995-07-20 02:43:20 -0500222 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
223 mask = 0x11;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500224
Guy Schalnat0d580581995-07-20 02:43:20 -0500225 else
226 mask = 0xff;
227
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500228 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500229 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500230 int j;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600231 unsigned int v, out;
Guy Schalnat0d580581995-07-20 02:43:20 -0500232
233 v = *bp;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600234 out = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500235
Guy Schalnat0d580581995-07-20 02:43:20 -0500236 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
237 {
238 if (j > 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600239 out |= v << j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500240
Guy Schalnat0d580581995-07-20 02:43:20 -0500241 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600242 out |= (v >> (-j)) & mask;
Guy Schalnat0d580581995-07-20 02:43:20 -0500243 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600244
245 *bp = (png_byte)(out & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500246 }
247 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500248
Guy Schalnat0d580581995-07-20 02:43:20 -0500249 else 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;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500253 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500254
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500255 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500256 {
Cosmin Trutaceb32772018-08-18 22:47:16 -0400257 unsigned int c = i%channels;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500258 int j;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600259 unsigned int v, out;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500260
261 v = *bp;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600262 out = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500263
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500264 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500265 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500266 if (j > 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600267 out |= v << j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500268
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500269 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600270 out |= v >> (-j);
Guy Schalnat0d580581995-07-20 02:43:20 -0500271 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600272
273 *bp = (png_byte)(out & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500274 }
275 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500276
Guy Schalnat0d580581995-07-20 02:43:20 -0500277 else
278 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600279 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500280 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500281 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500282
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500283 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500284 {
Cosmin Trutaceb32772018-08-18 22:47:16 -0400285 unsigned int c = i%channels;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500286 int j;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600287 unsigned int value, v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500288
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600289 v = png_get_uint_16(bp);
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500290 value = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500291
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500292 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500293 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500294 if (j > 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600295 value |= v << j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500296
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500297 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600298 value |= v >> (-j);
Guy Schalnat0d580581995-07-20 02:43:20 -0500299 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600300 *bp++ = (png_byte)((value >> 8) & 0xff);
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500301 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500302 }
303 }
304 }
305}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500306#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500307
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500308#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
John Bowler8f1150e2013-12-19 15:33:49 -0600309static void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500310png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500311{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500312 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500313
Guy Schalnat0d580581995-07-20 02:43:20 -0500314 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500316 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500317 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500318 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600319 /* This converts from ARGB to RGBA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500320 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500321 png_uint_32 i;
322 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500323
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500324 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500325 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500326 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500327 *(dp++) = *(sp++);
328 *(dp++) = *(sp++);
329 *(dp++) = *(sp++);
330 *(dp++) = save;
331 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500332 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500333
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500334#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500335 else
336 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600337 /* This converts from AARRGGBB to RRGGBBAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500339 png_uint_32 i;
340 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500341
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500342 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500343 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500344 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500345 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 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600357#endif /* WRITE_16BIT */
Guy Schalnat0d580581995-07-20 02:43:20 -0500358 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500359
Andreas Dilger47a0c421997-05-16 02:46:07 -0500360 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500361 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500362 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500363 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600364 /* This converts from AG to GA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500365 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500366 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500367 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500368
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500369 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500370 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500371 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500372 *(dp++) = *(sp++);
373 *(dp++) = save;
374 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500375 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500376
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500377#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500378 else
379 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600380 /* This converts from AAGG to GGAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500381 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500383 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500384
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500385 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500387 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388 save[0] = *(sp++);
389 save[1] = *(sp++);
390 *(dp++) = *(sp++);
391 *(dp++) = *(sp++);
392 *(dp++) = save[0];
393 *(dp++) = save[1];
394 }
395 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600396#endif /* WRITE_16BIT */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500397 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500398 }
399}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500400#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500401
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500402#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
John Bowler8f1150e2013-12-19 15:33:49 -0600403static void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600404png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
405{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500406 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500407
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600408 {
409 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
410 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600411 if (row_info->bit_depth == 8)
412 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600413 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600414 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500415 png_uint_32 i;
416 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500417
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500418 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600419 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500420 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600421 *(dp++) = *(sp++);
422 *(dp++) = *(sp++);
423 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600424 */
425 sp+=3; dp = sp;
Glenn Randers-Pehrsonc861dc82015-04-01 12:06:01 -0500426 *dp = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600427 }
428 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500429
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500430#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600431 else
432 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600433 /* This inverts the alpha channel in RRGGBBAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600434 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;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600437
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500438 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600439 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500440 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600441 *(dp++) = *(sp++);
442 *(dp++) = *(sp++);
443 *(dp++) = *(sp++);
444 *(dp++) = *(sp++);
445 *(dp++) = *(sp++);
446 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600447 */
448 sp+=6; dp = sp;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500449 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc861dc82015-04-01 12:06:01 -0500450 *dp = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600451 }
452 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600453#endif /* WRITE_16BIT */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600454 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500455
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600456 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
457 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600458 if (row_info->bit_depth == 8)
459 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600460 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600461 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500462 png_uint_32 i;
463 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600464
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500465 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600466 {
467 *(dp++) = *(sp++);
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500468 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600469 }
470 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500471
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500472#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600473 else
474 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600475 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600476 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500477 png_uint_32 i;
478 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600479
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500480 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600481 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500482 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600483 *(dp++) = *(sp++);
484 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600485 */
486 sp+=2; dp = sp;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500487 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc861dc82015-04-01 12:06:01 -0500488 *dp = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600489 }
490 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600491#endif /* WRITE_16BIT */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600492 }
493 }
494}
495#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600496
John Bowlerc10930a2013-12-19 15:24:06 -0600497/* Transform the data according to the user's wishes. The order of
498 * transformations is significant.
499 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600500void /* PRIVATE */
John Bowlerc10930a2013-12-19 15:24:06 -0600501png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600502{
John Bowlerc10930a2013-12-19 15:24:06 -0600503 png_debug(1, "in png_do_write_transformations");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500504
John Bowlerc10930a2013-12-19 15:24:06 -0600505 if (png_ptr == NULL)
506 return;
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600507
John Bowlerc10930a2013-12-19 15:24:06 -0600508#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500509 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600510 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 */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400516 /* size_t rowbytes; number of bytes in row */
John Bowlerc10930a2013-12-19 15:24:06 -0600517 /* 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
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500523
John Bowlerc10930a2013-12-19 15:24:06 -0600524#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500525 if ((png_ptr->transformations & PNG_FILLER) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600526 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500527 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
John Bowlerc10930a2013-12-19 15:24:06 -0600528#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500529
John Bowlerc10930a2013-12-19 15:24:06 -0600530#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500531 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600532 png_do_packswap(row_info, png_ptr->row_buf + 1);
533#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600534
John Bowlerc10930a2013-12-19 15:24:06 -0600535#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500536 if ((png_ptr->transformations & PNG_PACK) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600537 png_do_pack(row_info, png_ptr->row_buf + 1,
538 (png_uint_32)png_ptr->bit_depth);
539#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500540
John Bowlerc10930a2013-12-19 15:24:06 -0600541#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson70cb8f92014-11-06 20:58:33 -0600542# ifdef PNG_16BIT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500543 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600544 png_do_swap(row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson70cb8f92014-11-06 20:58:33 -0600545# endif
John Bowlerc10930a2013-12-19 15:24:06 -0600546#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600547
John Bowlerc10930a2013-12-19 15:24:06 -0600548#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500549 if ((png_ptr->transformations & PNG_SHIFT) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600550 png_do_shift(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500551 &(png_ptr->shift));
John Bowlerc10930a2013-12-19 15:24:06 -0600552#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500553
John Bowlerc10930a2013-12-19 15:24:06 -0600554#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500555 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600556 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
557#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500558
John Bowlerc10930a2013-12-19 15:24:06 -0600559#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500560 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600561 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
562#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600563
John Bowlerc10930a2013-12-19 15:24:06 -0600564#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500565 if ((png_ptr->transformations & PNG_BGR) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600566 png_do_bgr(row_info, png_ptr->row_buf + 1);
567#endif
568
569#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500570 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600571 png_do_invert(row_info, png_ptr->row_buf + 1);
572#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600573}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600574#endif /* WRITE_TRANSFORMS */
575#endif /* WRITE */