blob: 038a2ef5dcd5a7c5f97c3b0bf3cbe1b0305d9fab [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-Pehrsone6172802015-07-22 22:36:43 -05004 * Last changed in libpng 1.6.18 [July 23, 2015]
Glenn Randers-Pehrson4d8de332015-12-13 22:41:17 -06005 * Copyright (c) 1998-2002,2004,2006-2015 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 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-Pehrsond0dce401998-05-09 10:02:29 -0500180 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;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500215 png_size_t i;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600216 unsigned int mask;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500217 png_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 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500257
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600258 const unsigned int c = i%channels;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500259 int j;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600260 unsigned int v, out;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500261
262 v = *bp;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600263 out = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500264
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500265 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500266 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500267 if (j > 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600268 out |= v << j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500269
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500270 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600271 out |= v >> (-j);
Guy Schalnat0d580581995-07-20 02:43:20 -0500272 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600273
274 *bp = (png_byte)(out & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500275 }
276 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500277
Guy Schalnat0d580581995-07-20 02:43:20 -0500278 else
279 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600280 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500281 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500282 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500283
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500284 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500285 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600286 const unsigned int c = i%channels;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500287 int j;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600288 unsigned int value, v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500289
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600290 v = png_get_uint_16(bp);
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500291 value = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500292
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500293 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500294 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500295 if (j > 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600296 value |= v << j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500297
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500298 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600299 value |= v >> (-j);
Guy Schalnat0d580581995-07-20 02:43:20 -0500300 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600301 *bp++ = (png_byte)((value >> 8) & 0xff);
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500302 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500303 }
304 }
305 }
306}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500307#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500308
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500309#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
John Bowler8f1150e2013-12-19 15:33:49 -0600310static void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500311png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500312{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500313 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500314
Guy Schalnat0d580581995-07-20 02:43:20 -0500315 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500316 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500317 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500319 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600320 /* This converts from ARGB to RGBA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500321 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500322 png_uint_32 i;
323 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500324
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500325 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500326 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500327 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328 *(dp++) = *(sp++);
329 *(dp++) = *(sp++);
330 *(dp++) = *(sp++);
331 *(dp++) = save;
332 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500333 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500334
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500335#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500336 else
337 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600338 /* This converts from AARRGGBB to RRGGBBAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500339 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500340 png_uint_32 i;
341 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500342
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500343 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500345 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500346 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 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600358#endif /* WRITE_16BIT */
Guy Schalnat0d580581995-07-20 02:43:20 -0500359 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500360
Andreas Dilger47a0c421997-05-16 02:46:07 -0500361 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500362 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500364 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600365 /* This converts from AG to GA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500366 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500367 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500368 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500370 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500371 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500372 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500373 *(dp++) = *(sp++);
374 *(dp++) = save;
375 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500376 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500377
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500378#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500379 else
380 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600381 /* This converts from AAGG to GGAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500384 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500386 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500388 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500389 save[0] = *(sp++);
390 save[1] = *(sp++);
391 *(dp++) = *(sp++);
392 *(dp++) = *(sp++);
393 *(dp++) = save[0];
394 *(dp++) = save[1];
395 }
396 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600397#endif /* WRITE_16BIT */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500398 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500399 }
400}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500401#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500402
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500403#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
John Bowler8f1150e2013-12-19 15:33:49 -0600404static void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600405png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
406{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500407 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500408
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600409 {
410 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
411 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600412 if (row_info->bit_depth == 8)
413 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600414 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600415 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500416 png_uint_32 i;
417 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500418
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500419 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600420 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500421 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600422 *(dp++) = *(sp++);
423 *(dp++) = *(sp++);
424 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600425 */
426 sp+=3; dp = sp;
Glenn Randers-Pehrsonc861dc82015-04-01 12:06:01 -0500427 *dp = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600428 }
429 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500430
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500431#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600432 else
433 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600434 /* This inverts the alpha channel in RRGGBBAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600435 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500436 png_uint_32 i;
437 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600438
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500439 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600440 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500441 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600442 *(dp++) = *(sp++);
443 *(dp++) = *(sp++);
444 *(dp++) = *(sp++);
445 *(dp++) = *(sp++);
446 *(dp++) = *(sp++);
447 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600448 */
449 sp+=6; dp = sp;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500450 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc861dc82015-04-01 12:06:01 -0500451 *dp = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600452 }
453 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600454#endif /* WRITE_16BIT */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600455 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500456
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600457 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
458 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600459 if (row_info->bit_depth == 8)
460 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600461 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600462 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500463 png_uint_32 i;
464 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600465
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500466 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600467 {
468 *(dp++) = *(sp++);
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500469 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600470 }
471 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500472
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500473#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600474 else
475 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600476 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600477 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500478 png_uint_32 i;
479 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600480
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500481 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600482 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500483 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600484 *(dp++) = *(sp++);
485 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600486 */
487 sp+=2; dp = sp;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500488 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc861dc82015-04-01 12:06:01 -0500489 *dp = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600490 }
491 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600492#endif /* WRITE_16BIT */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600493 }
494 }
495}
496#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600497
John Bowlerc10930a2013-12-19 15:24:06 -0600498/* Transform the data according to the user's wishes. The order of
499 * transformations is significant.
500 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600501void /* PRIVATE */
John Bowlerc10930a2013-12-19 15:24:06 -0600502png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600503{
John Bowlerc10930a2013-12-19 15:24:06 -0600504 png_debug(1, "in png_do_write_transformations");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500505
John Bowlerc10930a2013-12-19 15:24:06 -0600506 if (png_ptr == NULL)
507 return;
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600508
John Bowlerc10930a2013-12-19 15:24:06 -0600509#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500510 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600511 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
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500524
John Bowlerc10930a2013-12-19 15:24:06 -0600525#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500526 if ((png_ptr->transformations & PNG_FILLER) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600527 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
528 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
529#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500530
John Bowlerc10930a2013-12-19 15:24:06 -0600531#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500532 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600533 png_do_packswap(row_info, png_ptr->row_buf + 1);
534#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600535
John Bowlerc10930a2013-12-19 15:24:06 -0600536#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500537 if ((png_ptr->transformations & PNG_PACK) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600538 png_do_pack(row_info, png_ptr->row_buf + 1,
539 (png_uint_32)png_ptr->bit_depth);
540#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500541
John Bowlerc10930a2013-12-19 15:24:06 -0600542#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson70cb8f92014-11-06 20:58:33 -0600543# ifdef PNG_16BIT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500544 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600545 png_do_swap(row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson70cb8f92014-11-06 20:58:33 -0600546# endif
John Bowlerc10930a2013-12-19 15:24:06 -0600547#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600548
John Bowlerc10930a2013-12-19 15:24:06 -0600549#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500550 if ((png_ptr->transformations & PNG_SHIFT) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600551 png_do_shift(row_info, png_ptr->row_buf + 1,
552 &(png_ptr->shift));
553#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500554
John Bowlerc10930a2013-12-19 15:24:06 -0600555#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500556 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600557 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
558#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500559
John Bowlerc10930a2013-12-19 15:24:06 -0600560#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500561 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600562 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
563#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600564
John Bowlerc10930a2013-12-19 15:24:06 -0600565#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500566 if ((png_ptr->transformations & PNG_BGR) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600567 png_do_bgr(row_info, png_ptr->row_buf + 1);
568#endif
569
570#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500571 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600572 png_do_invert(row_info, png_ptr->row_buf + 1);
573#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600574}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600575#endif /* WRITE_TRANSFORMS */
576#endif /* WRITE */