blob: 4236e1023947bfa4cc3c197949ae955dc2a0581c [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-Pehrsonf24daf22010-05-06 09:44:04 -05004 * Last changed in libpng 1.5.0 [May 6, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 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
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050018/* Transform the data according to the user's wishes. The order of
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060019 * transformations is significant.
20 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050021void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060022png_do_write_transformations(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050023{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050024 png_debug(1, "in png_do_write_transformations");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060025
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060026 if (png_ptr == NULL)
27 return;
28
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050029#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060030 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050031 if (png_ptr->write_user_transform_fn != NULL)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060032 (*(png_ptr->write_user_transform_fn)) /* User write transform
33 function */
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060034 (png_ptr, /* png_ptr */
35 &(png_ptr->row_info), /* row_info: */
36 /* png_uint_32 width; width of row */
37 /* png_uint_32 rowbytes; number of bytes in row */
38 /* png_byte color_type; color type of pixels */
39 /* png_byte bit_depth; bit depth of samples */
40 /* png_byte channels; number of channels (1-4) */
41 /* png_byte pixel_depth; bits per pixel (depth*channels) */
42 png_ptr->row_buf + 1); /* start of pixel data for row */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060043#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050044
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050045#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -060046 if (png_ptr->transformations & PNG_FILLER)
Andreas Dilger47a0c421997-05-16 02:46:07 -050047 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060048 png_ptr->flags);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050049#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050050
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050051#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060052 if (png_ptr->transformations & PNG_PACKSWAP)
53 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
54#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050055
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050056#ifdef PNG_WRITE_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050057 if (png_ptr->transformations & PNG_PACK)
58 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060059 (png_uint_32)png_ptr->bit_depth);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050060#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050061
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050062#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060063 if (png_ptr->transformations & PNG_SWAP_BYTES)
64 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
65#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050066
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050067#ifdef PNG_WRITE_SHIFT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050068 if (png_ptr->transformations & PNG_SHIFT)
69 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060070 &(png_ptr->shift));
Guy Schalnat51f0eb41995-09-26 05:22:39 -050071#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050072
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050073#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060074 if (png_ptr->transformations & PNG_SWAP_ALPHA)
75 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
76#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050077
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050078#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060079 if (png_ptr->transformations & PNG_INVERT_ALPHA)
80 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
81#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050082
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050083#ifdef PNG_WRITE_BGR_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050084 if (png_ptr->transformations & PNG_BGR)
85 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050086#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050087
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050088#ifdef PNG_WRITE_INVERT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050089 if (png_ptr->transformations & PNG_INVERT_MONO)
90 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050091#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050092}
93
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050094#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060095/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
96 * row_info bit depth should be 8 (one pixel per byte). The channels
97 * should be 1 (this only happens on grayscale and paletted images).
98 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050099void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500101{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500102 png_debug(1, "in png_do_pack");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500103
Andreas Dilger47a0c421997-05-16 02:46:07 -0500104 if (row_info->bit_depth == 8 &&
Guy Schalnat0d580581995-07-20 02:43:20 -0500105 row_info->channels == 1)
106 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500107 switch ((int)bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500108 {
109 case 1:
110 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500111 png_bytep sp, dp;
112 int mask, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500113 png_uint_32 i;
114 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500115
116 sp = row;
117 dp = row;
118 mask = 0x80;
119 v = 0;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600120
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500121 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500122 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500123 if (*sp != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500124 v |= mask;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500125
Guy Schalnat0d580581995-07-20 02:43:20 -0500126 sp++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500127
Guy Schalnat0d580581995-07-20 02:43:20 -0500128 if (mask > 1)
129 mask >>= 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500130
Guy Schalnat0d580581995-07-20 02:43:20 -0500131 else
132 {
133 mask = 0x80;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600134 *dp = (png_byte)v;
135 dp++;
Guy Schalnat0d580581995-07-20 02:43:20 -0500136 v = 0;
137 }
138 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500139
Guy Schalnat0d580581995-07-20 02:43:20 -0500140 if (mask != 0x80)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600141 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500142
Guy Schalnat0d580581995-07-20 02:43:20 -0500143 break;
144 }
145 case 2:
146 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500147 png_bytep sp, dp;
148 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500149 png_uint_32 i;
150 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500151
152 sp = row;
153 dp = row;
154 shift = 6;
155 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500156
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500157 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500158 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 png_byte value;
160
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600161 value = (png_byte)(*sp & 0x03);
Guy Schalnat0d580581995-07-20 02:43:20 -0500162 v |= (value << shift);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500163
Guy Schalnat0d580581995-07-20 02:43:20 -0500164 if (shift == 0)
165 {
166 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600167 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500168 dp++;
169 v = 0;
170 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500171
Guy Schalnat0d580581995-07-20 02:43:20 -0500172 else
173 shift -= 2;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500174
Guy Schalnat0d580581995-07-20 02:43:20 -0500175 sp++;
176 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500177
Guy Schalnat0d580581995-07-20 02:43:20 -0500178 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600179 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500180
Guy Schalnat0d580581995-07-20 02:43:20 -0500181 break;
182 }
183 case 4:
184 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500185 png_bytep sp, dp;
186 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500187 png_uint_32 i;
188 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500189
190 sp = row;
191 dp = row;
192 shift = 4;
193 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500194
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500195 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500196 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197 png_byte value;
198
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600199 value = (png_byte)(*sp & 0x0f);
Guy Schalnat0d580581995-07-20 02:43:20 -0500200 v |= (value << shift);
201
202 if (shift == 0)
203 {
204 shift = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600205 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500206 dp++;
207 v = 0;
208 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500209
Guy Schalnat0d580581995-07-20 02:43:20 -0500210 else
211 shift -= 4;
212
213 sp++;
214 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500215
Guy Schalnat0d580581995-07-20 02:43:20 -0500216 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600217 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500218
Guy Schalnat0d580581995-07-20 02:43:20 -0500219 break;
220 }
221 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500222 row_info->bit_depth = (png_byte)bit_depth;
223 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500224 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600225 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500226 }
227}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500228#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500229
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500230#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600231/* Shift pixel values to take advantage of whole range. Pass the
232 * true number of bits in bit_depth. The row should be packed
233 * according to row_info->bit_depth. Thus, if you had a row of
234 * bit depth 4, but the pixels only had values from 0 to 7, you
235 * would pass 3 as bit_depth, and this routine would translate the
236 * data to 0 to 15.
237 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500238void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600239png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500240{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500241 png_debug(1, "in png_do_shift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500242
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500243 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500244 {
245 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500246 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500247
Guy Schalnat0d580581995-07-20 02:43:20 -0500248 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
249 {
250 shift_start[channels] = row_info->bit_depth - bit_depth->red;
251 shift_dec[channels] = bit_depth->red;
252 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500253
Guy Schalnat0d580581995-07-20 02:43:20 -0500254 shift_start[channels] = row_info->bit_depth - bit_depth->green;
255 shift_dec[channels] = bit_depth->green;
256 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500257
Guy Schalnat0d580581995-07-20 02:43:20 -0500258 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
259 shift_dec[channels] = bit_depth->blue;
260 channels++;
261 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500262
Guy Schalnat0d580581995-07-20 02:43:20 -0500263 else
264 {
265 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
266 shift_dec[channels] = bit_depth->gray;
267 channels++;
268 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500269
Guy Schalnat0d580581995-07-20 02:43:20 -0500270 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
271 {
272 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
273 shift_dec[channels] = bit_depth->alpha;
274 channels++;
275 }
276
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500277 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500278 if (row_info->bit_depth < 8)
279 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500280 png_bytep bp = row;
281 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500282 png_byte mask;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500283 png_uint_32 row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500284
285 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
286 mask = 0x55;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500287
Guy Schalnat0d580581995-07-20 02:43:20 -0500288 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
289 mask = 0x11;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500290
Guy Schalnat0d580581995-07-20 02:43:20 -0500291 else
292 mask = 0xff;
293
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500294 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500295 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500296 png_uint_16 v;
297 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500298
299 v = *bp;
300 *bp = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500301
Guy Schalnat0d580581995-07-20 02:43:20 -0500302 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
303 {
304 if (j > 0)
305 *bp |= (png_byte)((v << j) & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500306
Guy Schalnat0d580581995-07-20 02:43:20 -0500307 else
308 *bp |= (png_byte)((v >> (-j)) & mask);
309 }
310 }
311 }
312 else if (row_info->bit_depth == 8)
313 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500314 png_bytep bp = row;
315 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500316 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500317
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500318 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500319 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500320
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500321 png_uint_16 v;
322 int j;
323 int c = (int)(i%channels);
324
325 v = *bp;
326 *bp = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500327
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500328 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500329 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500330 if (j > 0)
331 *bp |= (png_byte)((v << j) & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500332
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500333 else
334 *bp |= (png_byte)((v >> (-j)) & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500335 }
336 }
337 }
338 else
339 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600340 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500341 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500342 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500343
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500344 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500345 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500346 int c = (int)(i%channels);
347 png_uint_16 value, v;
348 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500349
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500350 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500351 value = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500352
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500353 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500354 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500355 if (j > 0)
356 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500357
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500358 else
359 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500360 }
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500361 *bp++ = (png_byte)(value >> 8);
362 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500363 }
364 }
365 }
366}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500367#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500368
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500369#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500370void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500371png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500372{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500373 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500374
Guy Schalnat0d580581995-07-20 02:43:20 -0500375 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500376 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500377 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500378 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500379 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600380 /* This converts from ARGB to RGBA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500381 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500382 png_uint_32 i;
383 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500384
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500385 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500387 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388 *(dp++) = *(sp++);
389 *(dp++) = *(sp++);
390 *(dp++) = *(sp++);
391 *(dp++) = save;
392 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500393 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500394
Andreas Dilger47a0c421997-05-16 02:46:07 -0500395 else
396 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600397 /* This converts from AARRGGBB to RRGGBBAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500398 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500399 png_uint_32 i;
400 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500401
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500402 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500404 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405 save[0] = *(sp++);
406 save[1] = *(sp++);
407 *(dp++) = *(sp++);
408 *(dp++) = *(sp++);
409 *(dp++) = *(sp++);
410 *(dp++) = *(sp++);
411 *(dp++) = *(sp++);
412 *(dp++) = *(sp++);
413 *(dp++) = save[0];
414 *(dp++) = save[1];
415 }
416 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500417 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500418
Andreas Dilger47a0c421997-05-16 02:46:07 -0500419 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500420 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500421 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500422 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600423 /* This converts from AG to GA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500424 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500425 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500426 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500427
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500428 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500429 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500430 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500431 *(dp++) = *(sp++);
432 *(dp++) = save;
433 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500434 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500435
Andreas Dilger47a0c421997-05-16 02:46:07 -0500436 else
437 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600438 /* This converts from AAGG to GGAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500439 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500440 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500441 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500442
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500443 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500444 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500445 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446 save[0] = *(sp++);
447 save[1] = *(sp++);
448 *(dp++) = *(sp++);
449 *(dp++) = *(sp++);
450 *(dp++) = save[0];
451 *(dp++) = save[1];
452 }
453 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500454 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500455 }
456}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500457#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500458
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500459#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500460void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600461png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
462{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500463 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500464
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600465 {
466 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
467 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600468 if (row_info->bit_depth == 8)
469 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600470 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600471 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500472 png_uint_32 i;
473 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500474
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500475 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600476 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500477 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600478 *(dp++) = *(sp++);
479 *(dp++) = *(sp++);
480 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600481 */
482 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500483 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600484 }
485 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500486
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600487 else
488 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600489 /* This inverts the alpha channel in RRGGBBAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600490 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500491 png_uint_32 i;
492 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600493
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500494 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600495 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500496 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600497 *(dp++) = *(sp++);
498 *(dp++) = *(sp++);
499 *(dp++) = *(sp++);
500 *(dp++) = *(sp++);
501 *(dp++) = *(sp++);
502 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600503 */
504 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500505 *(dp++) = (png_byte)(255 - *(sp++));
506 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600507 }
508 }
509 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500510
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600511 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
512 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600513 if (row_info->bit_depth == 8)
514 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600515 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600516 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500517 png_uint_32 i;
518 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600519
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500520 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600521 {
522 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500523 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600524 }
525 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500526
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600527 else
528 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600529 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600530 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500531 png_uint_32 i;
532 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600533
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500534 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600535 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500536 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600537 *(dp++) = *(sp++);
538 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600539 */
540 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500541 *(dp++) = (png_byte)(255 - *(sp++));
542 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600543 }
544 }
545 }
546 }
547}
548#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600549
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500550#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500551/* Undoes intrapixel differencing */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600552void /* PRIVATE */
553png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
554{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500555 png_debug(1, "in png_do_write_intrapixel");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500556
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500557 if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600558 {
559 int bytes_per_pixel;
560 png_uint_32 row_width = row_info->width;
561 if (row_info->bit_depth == 8)
562 {
563 png_bytep rp;
564 png_uint_32 i;
565
566 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
567 bytes_per_pixel = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500568
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600569 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
570 bytes_per_pixel = 4;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500571
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600572 else
573 return;
574
575 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
576 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600577 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
578 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600579 }
580 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500581
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600582 else if (row_info->bit_depth == 16)
583 {
584 png_bytep rp;
585 png_uint_32 i;
586
587 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
588 bytes_per_pixel = 6;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500589
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600590 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
591 bytes_per_pixel = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500592
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600593 else
594 return;
595
596 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
597 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600598 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
599 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
600 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500601 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
602 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600603 *(rp ) = (png_byte)((red >> 8) & 0xff);
604 *(rp + 1) = (png_byte)(red & 0xff);
605 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
606 *(rp + 5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600607 }
608 }
609 }
610}
611#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500612#endif /* PNG_WRITE_SUPPORTED */