blob: 98703f8c818a6c72bcd25cc0c4872dc709ccd42e [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-Pehrson871b1d02013-03-02 14:58:22 -06004 * Last changed in libpng 1.6.0 [February 14, 2013]
5 * Copyright (c) 1998-2013 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
17
John Bowler4a12f4a2011-04-17 18:34:22 -050018#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050019/* Transform the data according to the user's wishes. The order of
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060020 * transformations is significant.
21 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050022void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -060023png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -050024{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050025 png_debug(1, "in png_do_write_transformations");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060026
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060027 if (png_ptr == NULL)
28 return;
29
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050030#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060031 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050032 if (png_ptr->write_user_transform_fn != NULL)
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -050033 (*(png_ptr->write_user_transform_fn)) /* User write transform
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060034 function */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050035 (png_ptr, /* png_ptr */
36 row_info, /* row_info: */
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -050037 /* png_uint_32 width; width of row */
38 /* png_size_t rowbytes; number of bytes in row */
39 /* png_byte color_type; color type of pixels */
40 /* png_byte bit_depth; bit depth of samples */
41 /* png_byte channels; number of channels (1-4) */
42 /* png_byte pixel_depth; bits per pixel (depth*channels) */
43 png_ptr->row_buf + 1); /* start of pixel data for row */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060044#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050045
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050046#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -060047 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050048 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsoncb1aee22011-04-16 19:27:34 -050049 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
Guy Schalnat51f0eb41995-09-26 05:22:39 -050050#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050051
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050052#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060053 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050054 png_do_packswap(row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060055#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050056
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050057#ifdef PNG_WRITE_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050058 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050059 png_do_pack(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060060 (png_uint_32)png_ptr->bit_depth);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050061#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050062
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050063#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060064 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050065 png_do_swap(row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060066#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050067
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050068#ifdef PNG_WRITE_SHIFT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050069 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050070 png_do_shift(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060071 &(png_ptr->shift));
Guy Schalnat51f0eb41995-09-26 05:22:39 -050072#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050073
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050074#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060075 if (png_ptr->transformations & PNG_SWAP_ALPHA)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050076 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060077#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050078
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050079#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060080 if (png_ptr->transformations & PNG_INVERT_ALPHA)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050081 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060082#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050083
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050084#ifdef PNG_WRITE_BGR_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050085 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050086 png_do_bgr(row_info, png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050087#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050088
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050089#ifdef PNG_WRITE_INVERT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050090 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050091 png_do_invert(row_info, png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050092#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050093}
94
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050095#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060096/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
97 * row_info bit depth should be 8 (one pixel per byte). The channels
98 * should be 1 (this only happens on grayscale and paletted images).
99 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500100void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500102{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500103 png_debug(1, "in png_do_pack");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500104
Andreas Dilger47a0c421997-05-16 02:46:07 -0500105 if (row_info->bit_depth == 8 &&
Guy Schalnat0d580581995-07-20 02:43:20 -0500106 row_info->channels == 1)
107 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500108 switch ((int)bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500109 {
110 case 1:
111 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112 png_bytep sp, dp;
113 int mask, 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 mask = 0x80;
120 v = 0;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600121
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 if (*sp != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500125 v |= mask;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500126
Guy Schalnat0d580581995-07-20 02:43:20 -0500127 sp++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500128
Guy Schalnat0d580581995-07-20 02:43:20 -0500129 if (mask > 1)
130 mask >>= 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500131
Guy Schalnat0d580581995-07-20 02:43:20 -0500132 else
133 {
134 mask = 0x80;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600135 *dp = (png_byte)v;
136 dp++;
Guy Schalnat0d580581995-07-20 02:43:20 -0500137 v = 0;
138 }
139 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500140
Guy Schalnat0d580581995-07-20 02:43:20 -0500141 if (mask != 0x80)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600142 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500143
Guy Schalnat0d580581995-07-20 02:43:20 -0500144 break;
145 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500146
Guy Schalnat0d580581995-07-20 02:43:20 -0500147 case 2:
148 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500149 png_bytep sp, dp;
150 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500151 png_uint_32 i;
152 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500153
154 sp = row;
155 dp = row;
156 shift = 6;
157 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500158
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500159 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500160 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500161 png_byte value;
162
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600163 value = (png_byte)(*sp & 0x03);
Guy Schalnat0d580581995-07-20 02:43:20 -0500164 v |= (value << shift);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500165
Guy Schalnat0d580581995-07-20 02:43:20 -0500166 if (shift == 0)
167 {
168 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600169 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500170 dp++;
171 v = 0;
172 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500173
Guy Schalnat0d580581995-07-20 02:43:20 -0500174 else
175 shift -= 2;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500176
Guy Schalnat0d580581995-07-20 02:43:20 -0500177 sp++;
178 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500179
Guy Schalnat0d580581995-07-20 02:43:20 -0500180 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600181 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500182
Guy Schalnat0d580581995-07-20 02:43:20 -0500183 break;
184 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500185
Guy Schalnat0d580581995-07-20 02:43:20 -0500186 case 4:
187 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500188 png_bytep sp, dp;
189 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500190 png_uint_32 i;
191 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500192
193 sp = row;
194 dp = row;
195 shift = 4;
196 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500197
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500198 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500199 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 png_byte value;
201
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600202 value = (png_byte)(*sp & 0x0f);
Guy Schalnat0d580581995-07-20 02:43:20 -0500203 v |= (value << shift);
204
205 if (shift == 0)
206 {
207 shift = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600208 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500209 dp++;
210 v = 0;
211 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500212
Guy Schalnat0d580581995-07-20 02:43:20 -0500213 else
214 shift -= 4;
215
216 sp++;
217 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500218
Guy Schalnat0d580581995-07-20 02:43:20 -0500219 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600220 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500221
Guy Schalnat0d580581995-07-20 02:43:20 -0500222 break;
223 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600224
225 default:
226 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500227 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500228
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 row_info->bit_depth = (png_byte)bit_depth;
230 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500231 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600232 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500233 }
234}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500235#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500236
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500237#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600238/* Shift pixel values to take advantage of whole range. Pass the
239 * true number of bits in bit_depth. The row should be packed
240 * according to row_info->bit_depth. Thus, if you had a row of
241 * bit depth 4, but the pixels only had values from 0 to 7, you
242 * would pass 3 as bit_depth, and this routine would translate the
243 * data to 0 to 15.
244 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500245void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500246png_do_shift(png_row_infop row_info, png_bytep row,
247 png_const_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500248{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500249 png_debug(1, "in png_do_shift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500250
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500251 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500252 {
253 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500254 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500255
Guy Schalnat0d580581995-07-20 02:43:20 -0500256 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
257 {
258 shift_start[channels] = row_info->bit_depth - bit_depth->red;
259 shift_dec[channels] = bit_depth->red;
260 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500261
Guy Schalnat0d580581995-07-20 02:43:20 -0500262 shift_start[channels] = row_info->bit_depth - bit_depth->green;
263 shift_dec[channels] = bit_depth->green;
264 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500265
Guy Schalnat0d580581995-07-20 02:43:20 -0500266 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
267 shift_dec[channels] = bit_depth->blue;
268 channels++;
269 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500270
Guy Schalnat0d580581995-07-20 02:43:20 -0500271 else
272 {
273 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
274 shift_dec[channels] = bit_depth->gray;
275 channels++;
276 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500277
Guy Schalnat0d580581995-07-20 02:43:20 -0500278 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
279 {
280 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
281 shift_dec[channels] = bit_depth->alpha;
282 channels++;
283 }
284
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500285 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500286 if (row_info->bit_depth < 8)
287 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500288 png_bytep bp = row;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500289 png_size_t i;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600290 unsigned int mask;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500291 png_size_t row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500292
293 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
294 mask = 0x55;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500295
Guy Schalnat0d580581995-07-20 02:43:20 -0500296 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
297 mask = 0x11;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500298
Guy Schalnat0d580581995-07-20 02:43:20 -0500299 else
300 mask = 0xff;
301
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500302 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500303 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304 int j;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600305 unsigned int v, out;
Guy Schalnat0d580581995-07-20 02:43:20 -0500306
307 v = *bp;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600308 out = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500309
Guy Schalnat0d580581995-07-20 02:43:20 -0500310 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
311 {
312 if (j > 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600313 out |= v << j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500314
Guy Schalnat0d580581995-07-20 02:43:20 -0500315 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600316 out |= (v >> (-j)) & mask;
Guy Schalnat0d580581995-07-20 02:43:20 -0500317 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600318
319 *bp = (png_byte)(out & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500320 }
321 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500322
Guy Schalnat0d580581995-07-20 02:43:20 -0500323 else if (row_info->bit_depth == 8)
324 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500325 png_bytep bp = row;
326 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500327 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500328
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500329 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500330 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500331
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600332 const unsigned int c = i%channels;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500333 int j;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600334 unsigned int v, out;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500335
336 v = *bp;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600337 out = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500338
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500339 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500340 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500341 if (j > 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600342 out |= v << j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500343
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500344 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600345 out |= v >> (-j);
Guy Schalnat0d580581995-07-20 02:43:20 -0500346 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600347
348 *bp = (png_byte)(out & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500349 }
350 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500351
Guy Schalnat0d580581995-07-20 02:43:20 -0500352 else
353 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600354 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500355 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500356 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500357
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500358 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500359 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600360 const unsigned int c = i%channels;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500361 int j;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600362 unsigned int value, v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500363
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600364 v = png_get_uint_16(bp);
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500365 value = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500366
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500367 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500368 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500369 if (j > 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600370 value |= v << j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500371
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500372 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600373 value |= v >> (-j);
Guy Schalnat0d580581995-07-20 02:43:20 -0500374 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600375 *bp++ = (png_byte)((value >> 8) & 0xff);
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500376 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500377 }
378 }
379 }
380}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500381#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500382
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500383#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500384void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500386{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500387 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500388
Guy Schalnat0d580581995-07-20 02:43:20 -0500389 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500390 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500391 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500393 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600394 /* This converts from ARGB to RGBA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500395 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500396 png_uint_32 i;
397 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500398
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500399 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500400 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500401 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402 *(dp++) = *(sp++);
403 *(dp++) = *(sp++);
404 *(dp++) = *(sp++);
405 *(dp++) = save;
406 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500407 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500408
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500409#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500410 else
411 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600412 /* This converts from AARRGGBB to RRGGBBAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500413 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;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500416
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500417 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500418 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500419 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500420 save[0] = *(sp++);
421 save[1] = *(sp++);
422 *(dp++) = *(sp++);
423 *(dp++) = *(sp++);
424 *(dp++) = *(sp++);
425 *(dp++) = *(sp++);
426 *(dp++) = *(sp++);
427 *(dp++) = *(sp++);
428 *(dp++) = save[0];
429 *(dp++) = save[1];
430 }
431 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500432#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500433 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500434
Andreas Dilger47a0c421997-05-16 02:46:07 -0500435 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500436 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500437 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500438 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600439 /* This converts from AG to GA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500440 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500442 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500443
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500444 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500445 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500446 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500447 *(dp++) = *(sp++);
448 *(dp++) = save;
449 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500450 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500451
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500452#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 else
454 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600455 /* This converts from AAGG to GGAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500457 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500458 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500459
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500460 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500461 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500462 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500463 save[0] = *(sp++);
464 save[1] = *(sp++);
465 *(dp++) = *(sp++);
466 *(dp++) = *(sp++);
467 *(dp++) = save[0];
468 *(dp++) = save[1];
469 }
470 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500471#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500472 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500473 }
474}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500475#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500476
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500477#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500478void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600479png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
480{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500481 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500482
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600483 {
484 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
485 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600486 if (row_info->bit_depth == 8)
487 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600488 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600489 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500490 png_uint_32 i;
491 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500492
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500493 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600494 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500495 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600496 *(dp++) = *(sp++);
497 *(dp++) = *(sp++);
498 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600499 */
500 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500501 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600502 }
503 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500504
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500505#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600506 else
507 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600508 /* This inverts the alpha channel in RRGGBBAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600509 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500510 png_uint_32 i;
511 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600512
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500513 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600514 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500515 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600516 *(dp++) = *(sp++);
517 *(dp++) = *(sp++);
518 *(dp++) = *(sp++);
519 *(dp++) = *(sp++);
520 *(dp++) = *(sp++);
521 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600522 */
523 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500524 *(dp++) = (png_byte)(255 - *(sp++));
525 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600526 }
527 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500528#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600529 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500530
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600531 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
532 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600533 if (row_info->bit_depth == 8)
534 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600535 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600536 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500537 png_uint_32 i;
538 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600539
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500540 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600541 {
542 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500543 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600544 }
545 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500546
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500547#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600548 else
549 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600550 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600551 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500552 png_uint_32 i;
553 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600554
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500555 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600556 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500557 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600558 *(dp++) = *(sp++);
559 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600560 */
561 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500562 *(dp++) = (png_byte)(255 - *(sp++));
563 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600564 }
565 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500566#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600567 }
568 }
569}
570#endif
John Bowler4a12f4a2011-04-17 18:34:22 -0500571#endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600572
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500573#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500574/* Undoes intrapixel differencing */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600575void /* PRIVATE */
576png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
577{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500578 png_debug(1, "in png_do_write_intrapixel");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500579
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500580 if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600581 {
582 int bytes_per_pixel;
583 png_uint_32 row_width = row_info->width;
584 if (row_info->bit_depth == 8)
585 {
586 png_bytep rp;
587 png_uint_32 i;
588
589 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
590 bytes_per_pixel = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500591
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600592 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
593 bytes_per_pixel = 4;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500594
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600595 else
596 return;
597
598 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
599 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600600 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
601 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600602 }
603 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500604
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500605#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600606 else if (row_info->bit_depth == 16)
607 {
608 png_bytep rp;
609 png_uint_32 i;
610
611 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
612 bytes_per_pixel = 6;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500613
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600614 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
615 bytes_per_pixel = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500616
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600617 else
618 return;
619
620 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
621 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600622 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
623 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
624 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500625 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
626 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600627 *(rp ) = (png_byte)((red >> 8) & 0xff);
628 *(rp + 1) = (png_byte)(red & 0xff);
629 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
630 *(rp + 5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600631 }
632 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500633#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600634 }
635}
636#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500637#endif /* PNG_WRITE_SUPPORTED */