blob: 215bd68a9d872d0c3ed9451149e3a94027e56176 [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-Pehrson3e753eb2014-02-05 22:28:57 -06004 * Last changed in libpng 1.6.9 [February 6, 2014]
Glenn Randers-Pehrson95a19732013-12-31 21:10:13 -06005 * Copyright (c) 1998-2014 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;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060059 *dp = (png_byte)v;
60 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)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060066 *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;
74 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -050075 png_uint_32 i;
76 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -050077
78 sp = row;
79 dp = row;
80 shift = 6;
81 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050082
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -050083 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -050084 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050085 png_byte value;
86
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060087 value = (png_byte)(*sp & 0x03);
Guy Schalnat0d580581995-07-20 02:43:20 -050088 v |= (value << shift);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050089
Guy Schalnat0d580581995-07-20 02:43:20 -050090 if (shift == 0)
91 {
92 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060093 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -050094 dp++;
95 v = 0;
96 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050097
Guy Schalnat0d580581995-07-20 02:43:20 -050098 else
99 shift -= 2;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500100
Guy Schalnat0d580581995-07-20 02:43:20 -0500101 sp++;
102 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500103
Guy Schalnat0d580581995-07-20 02:43:20 -0500104 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600105 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500106
Guy Schalnat0d580581995-07-20 02:43:20 -0500107 break;
108 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500109
Guy Schalnat0d580581995-07-20 02:43:20 -0500110 case 4:
111 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112 png_bytep sp, dp;
113 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500114 png_uint_32 i;
115 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500116
117 sp = row;
118 dp = row;
119 shift = 4;
120 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500121
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500122 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500123 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500124 png_byte value;
125
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600126 value = (png_byte)(*sp & 0x0f);
Guy Schalnat0d580581995-07-20 02:43:20 -0500127 v |= (value << shift);
128
129 if (shift == 0)
130 {
131 shift = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600132 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500133 dp++;
134 v = 0;
135 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500136
Guy Schalnat0d580581995-07-20 02:43:20 -0500137 else
138 shift -= 4;
139
140 sp++;
141 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500142
Guy Schalnat0d580581995-07-20 02:43:20 -0500143 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600144 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500145
Guy Schalnat0d580581995-07-20 02:43:20 -0500146 break;
147 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600148
149 default:
150 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500151 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500152
Andreas Dilger47a0c421997-05-16 02:46:07 -0500153 row_info->bit_depth = (png_byte)bit_depth;
154 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500155 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600156 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500157 }
158}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500159#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500160
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500161#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600162/* Shift pixel values to take advantage of whole range. Pass the
163 * true number of bits in bit_depth. The row should be packed
164 * according to row_info->bit_depth. Thus, if you had a row of
165 * bit depth 4, but the pixels only had values from 0 to 7, you
166 * would pass 3 as bit_depth, and this routine would translate the
167 * data to 0 to 15.
168 */
John Bowler8f1150e2013-12-19 15:33:49 -0600169static void
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500170png_do_shift(png_row_infop row_info, png_bytep row,
171 png_const_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500172{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500173 png_debug(1, "in png_do_shift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500174
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500175 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500176 {
177 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500178 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500179
Guy Schalnat0d580581995-07-20 02:43:20 -0500180 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
181 {
182 shift_start[channels] = row_info->bit_depth - bit_depth->red;
183 shift_dec[channels] = bit_depth->red;
184 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500185
Guy Schalnat0d580581995-07-20 02:43:20 -0500186 shift_start[channels] = row_info->bit_depth - bit_depth->green;
187 shift_dec[channels] = bit_depth->green;
188 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500189
Guy Schalnat0d580581995-07-20 02:43:20 -0500190 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
191 shift_dec[channels] = bit_depth->blue;
192 channels++;
193 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500194
Guy Schalnat0d580581995-07-20 02:43:20 -0500195 else
196 {
197 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
198 shift_dec[channels] = bit_depth->gray;
199 channels++;
200 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500201
Guy Schalnat0d580581995-07-20 02:43:20 -0500202 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
203 {
204 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
205 shift_dec[channels] = bit_depth->alpha;
206 channels++;
207 }
208
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500209 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500210 if (row_info->bit_depth < 8)
211 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500212 png_bytep bp = row;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500213 png_size_t i;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600214 unsigned int mask;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500215 png_size_t row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500216
217 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
218 mask = 0x55;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500219
Guy Schalnat0d580581995-07-20 02:43:20 -0500220 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
221 mask = 0x11;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500222
Guy Schalnat0d580581995-07-20 02:43:20 -0500223 else
224 mask = 0xff;
225
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500226 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500227 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500228 int j;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600229 unsigned int v, out;
Guy Schalnat0d580581995-07-20 02:43:20 -0500230
231 v = *bp;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600232 out = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500233
Guy Schalnat0d580581995-07-20 02:43:20 -0500234 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
235 {
236 if (j > 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600237 out |= v << j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500238
Guy Schalnat0d580581995-07-20 02:43:20 -0500239 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600240 out |= (v >> (-j)) & mask;
Guy Schalnat0d580581995-07-20 02:43:20 -0500241 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600242
243 *bp = (png_byte)(out & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500244 }
245 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500246
Guy Schalnat0d580581995-07-20 02:43:20 -0500247 else if (row_info->bit_depth == 8)
248 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500249 png_bytep bp = row;
250 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500251 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500252
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500253 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500254 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500255
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600256 const unsigned int c = i%channels;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500257 int j;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600258 unsigned int v, out;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500259
260 v = *bp;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600261 out = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500262
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500263 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500264 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500265 if (j > 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600266 out |= v << j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500267
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500268 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600269 out |= v >> (-j);
Guy Schalnat0d580581995-07-20 02:43:20 -0500270 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600271
272 *bp = (png_byte)(out & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500273 }
274 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500275
Guy Schalnat0d580581995-07-20 02:43:20 -0500276 else
277 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600278 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500279 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500280 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500281
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500282 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500283 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600284 const unsigned int c = i%channels;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500285 int j;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600286 unsigned int value, v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500287
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600288 v = png_get_uint_16(bp);
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500289 value = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500290
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500291 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500292 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500293 if (j > 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600294 value |= v << j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500295
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500296 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600297 value |= v >> (-j);
Guy Schalnat0d580581995-07-20 02:43:20 -0500298 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600299 *bp++ = (png_byte)((value >> 8) & 0xff);
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500300 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500301 }
302 }
303 }
304}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500305#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500306
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500307#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
John Bowler8f1150e2013-12-19 15:33:49 -0600308static void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500309png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500310{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500311 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500312
Guy Schalnat0d580581995-07-20 02:43:20 -0500313 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500314 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500315 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500316 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500317 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600318 /* This converts from ARGB to RGBA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500319 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500320 png_uint_32 i;
321 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500322
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500323 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500324 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500325 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500326 *(dp++) = *(sp++);
327 *(dp++) = *(sp++);
328 *(dp++) = *(sp++);
329 *(dp++) = save;
330 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500331 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500332
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500333#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500334 else
335 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600336 /* This converts from AARRGGBB to RRGGBBAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500338 png_uint_32 i;
339 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500340
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500341 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500342 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500343 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 save[0] = *(sp++);
345 save[1] = *(sp++);
346 *(dp++) = *(sp++);
347 *(dp++) = *(sp++);
348 *(dp++) = *(sp++);
349 *(dp++) = *(sp++);
350 *(dp++) = *(sp++);
351 *(dp++) = *(sp++);
352 *(dp++) = save[0];
353 *(dp++) = save[1];
354 }
355 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500356#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500357 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500358
Andreas Dilger47a0c421997-05-16 02:46:07 -0500359 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500360 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500361 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500362 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600363 /* This converts from AG to GA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500364 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500365 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500366 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500367
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500368 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500370 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500371 *(dp++) = *(sp++);
372 *(dp++) = save;
373 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500374 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500375
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500376#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377 else
378 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600379 /* This converts from AAGG to GGAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500380 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500381 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500382 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500384 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500386 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 save[0] = *(sp++);
388 save[1] = *(sp++);
389 *(dp++) = *(sp++);
390 *(dp++) = *(sp++);
391 *(dp++) = save[0];
392 *(dp++) = save[1];
393 }
394 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500395#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500396 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500397 }
398}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500399#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500400
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500401#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
John Bowler8f1150e2013-12-19 15:33:49 -0600402static void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600403png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
404{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500405 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500406
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600407 {
408 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
409 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600410 if (row_info->bit_depth == 8)
411 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600412 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600413 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500414 png_uint_32 i;
415 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500416
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500417 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600418 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500419 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600420 *(dp++) = *(sp++);
421 *(dp++) = *(sp++);
422 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600423 */
424 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500425 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600426 }
427 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500428
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500429#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600430 else
431 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600432 /* This inverts the alpha channel in RRGGBBAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600433 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500434 png_uint_32 i;
435 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600436
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500437 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600438 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500439 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600440 *(dp++) = *(sp++);
441 *(dp++) = *(sp++);
442 *(dp++) = *(sp++);
443 *(dp++) = *(sp++);
444 *(dp++) = *(sp++);
445 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600446 */
447 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500448 *(dp++) = (png_byte)(255 - *(sp++));
449 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600450 }
451 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500452#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600453 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500454
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600455 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
456 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600457 if (row_info->bit_depth == 8)
458 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600459 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600460 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500461 png_uint_32 i;
462 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600463
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500464 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600465 {
466 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500467 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600468 }
469 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500470
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500471#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600472 else
473 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600474 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600475 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500476 png_uint_32 i;
477 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600478
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500479 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600480 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500481 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600482 *(dp++) = *(sp++);
483 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600484 */
485 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500486 *(dp++) = (png_byte)(255 - *(sp++));
487 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600488 }
489 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500490#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600491 }
492 }
493}
494#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600495
John Bowlerc10930a2013-12-19 15:24:06 -0600496/* Transform the data according to the user's wishes. The order of
497 * transformations is significant.
498 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600499void /* PRIVATE */
John Bowlerc10930a2013-12-19 15:24:06 -0600500png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600501{
John Bowlerc10930a2013-12-19 15:24:06 -0600502 png_debug(1, "in png_do_write_transformations");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500503
John Bowlerc10930a2013-12-19 15:24:06 -0600504 if (png_ptr == NULL)
505 return;
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600506
John Bowlerc10930a2013-12-19 15:24:06 -0600507#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
508 if (png_ptr->transformations & PNG_USER_TRANSFORM)
509 if (png_ptr->write_user_transform_fn != NULL)
510 (*(png_ptr->write_user_transform_fn)) /* User write transform
511 function */
512 (png_ptr, /* png_ptr */
513 row_info, /* row_info: */
514 /* png_uint_32 width; width of row */
515 /* png_size_t rowbytes; number of bytes in row */
516 /* png_byte color_type; color type of pixels */
517 /* png_byte bit_depth; bit depth of samples */
518 /* png_byte channels; number of channels (1-4) */
519 /* png_byte pixel_depth; bits per pixel (depth*channels) */
520 png_ptr->row_buf + 1); /* start of pixel data for row */
521#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500522
John Bowlerc10930a2013-12-19 15:24:06 -0600523#ifdef PNG_WRITE_FILLER_SUPPORTED
524 if (png_ptr->transformations & PNG_FILLER)
525 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
526 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
527#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500528
John Bowlerc10930a2013-12-19 15:24:06 -0600529#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
530 if (png_ptr->transformations & PNG_PACKSWAP)
531 png_do_packswap(row_info, png_ptr->row_buf + 1);
532#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600533
John Bowlerc10930a2013-12-19 15:24:06 -0600534#ifdef PNG_WRITE_PACK_SUPPORTED
535 if (png_ptr->transformations & PNG_PACK)
536 png_do_pack(row_info, png_ptr->row_buf + 1,
537 (png_uint_32)png_ptr->bit_depth);
538#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500539
John Bowlerc10930a2013-12-19 15:24:06 -0600540#ifdef PNG_WRITE_SWAP_SUPPORTED
541 if (png_ptr->transformations & PNG_SWAP_BYTES)
542 png_do_swap(row_info, png_ptr->row_buf + 1);
543#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600544
John Bowlerc10930a2013-12-19 15:24:06 -0600545#ifdef PNG_WRITE_SHIFT_SUPPORTED
546 if (png_ptr->transformations & PNG_SHIFT)
547 png_do_shift(row_info, png_ptr->row_buf + 1,
548 &(png_ptr->shift));
549#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500550
John Bowlerc10930a2013-12-19 15:24:06 -0600551#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
552 if (png_ptr->transformations & PNG_SWAP_ALPHA)
553 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
554#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500555
John Bowlerc10930a2013-12-19 15:24:06 -0600556#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
557 if (png_ptr->transformations & PNG_INVERT_ALPHA)
558 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
559#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600560
John Bowlerc10930a2013-12-19 15:24:06 -0600561#ifdef PNG_WRITE_BGR_SUPPORTED
562 if (png_ptr->transformations & PNG_BGR)
563 png_do_bgr(row_info, png_ptr->row_buf + 1);
564#endif
565
566#ifdef PNG_WRITE_INVERT_SUPPORTED
567 if (png_ptr->transformations & PNG_INVERT_MONO)
568 png_do_invert(row_info, png_ptr->row_buf + 1);
569#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600570}
John Bowlerc10930a2013-12-19 15:24:06 -0600571#endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500572#endif /* PNG_WRITE_SUPPORTED */