blob: f4b4898e9974ddb0fad64ef21bf306507d1d3adc [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-Pehrsonbcb3aac2010-09-10 22:05:27 -05004 * Last changed in libpng 1.5.0 [September 11, 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 */
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -050037 /* png_size_t rowbytes; number of bytes in row */
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060038 /* 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 */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500239png_do_shift(png_row_infop row_info, png_bytep row,
240 png_const_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500241{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500242 png_debug(1, "in png_do_shift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500243
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500244 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500245 {
246 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500247 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500248
Guy Schalnat0d580581995-07-20 02:43:20 -0500249 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
250 {
251 shift_start[channels] = row_info->bit_depth - bit_depth->red;
252 shift_dec[channels] = bit_depth->red;
253 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500254
Guy Schalnat0d580581995-07-20 02:43:20 -0500255 shift_start[channels] = row_info->bit_depth - bit_depth->green;
256 shift_dec[channels] = bit_depth->green;
257 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500258
Guy Schalnat0d580581995-07-20 02:43:20 -0500259 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
260 shift_dec[channels] = bit_depth->blue;
261 channels++;
262 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500263
Guy Schalnat0d580581995-07-20 02:43:20 -0500264 else
265 {
266 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
267 shift_dec[channels] = bit_depth->gray;
268 channels++;
269 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500270
Guy Schalnat0d580581995-07-20 02:43:20 -0500271 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
272 {
273 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
274 shift_dec[channels] = bit_depth->alpha;
275 channels++;
276 }
277
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500278 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500279 if (row_info->bit_depth < 8)
280 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500281 png_bytep bp = row;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500282 png_size_t i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500283 png_byte mask;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500284 png_size_t row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500285
286 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
287 mask = 0x55;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500288
Guy Schalnat0d580581995-07-20 02:43:20 -0500289 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
290 mask = 0x11;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500291
Guy Schalnat0d580581995-07-20 02:43:20 -0500292 else
293 mask = 0xff;
294
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500295 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500296 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297 png_uint_16 v;
298 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500299
300 v = *bp;
301 *bp = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500302
Guy Schalnat0d580581995-07-20 02:43:20 -0500303 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
304 {
305 if (j > 0)
306 *bp |= (png_byte)((v << j) & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500307
Guy Schalnat0d580581995-07-20 02:43:20 -0500308 else
309 *bp |= (png_byte)((v >> (-j)) & mask);
310 }
311 }
312 }
313 else if (row_info->bit_depth == 8)
314 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500315 png_bytep bp = row;
316 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500317 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500318
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500319 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500320 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500321
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500322 png_uint_16 v;
323 int j;
324 int c = (int)(i%channels);
325
326 v = *bp;
327 *bp = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500328
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500329 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500330 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500331 if (j > 0)
332 *bp |= (png_byte)((v << j) & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500333
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500334 else
335 *bp |= (png_byte)((v >> (-j)) & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500336 }
337 }
338 }
339 else
340 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600341 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500342 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500343 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500344
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500345 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500346 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500347 int c = (int)(i%channels);
348 png_uint_16 value, v;
349 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500350
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500351 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500352 value = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500353
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500354 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500355 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500356 if (j > 0)
357 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500358
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500359 else
360 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500361 }
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500362 *bp++ = (png_byte)(value >> 8);
363 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500364 }
365 }
366 }
367}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500368#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500369
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500370#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500371void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500372png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500373{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500374 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500375
Guy Schalnat0d580581995-07-20 02:43:20 -0500376 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500378 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500379 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500380 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600381 /* This converts from ARGB to RGBA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500383 png_uint_32 i;
384 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500385
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500386 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500388 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500389 *(dp++) = *(sp++);
390 *(dp++) = *(sp++);
391 *(dp++) = *(sp++);
392 *(dp++) = save;
393 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500394 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500395
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500396#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 else
398 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600399 /* This converts from AARRGGBB to RRGGBBAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500400 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500401 png_uint_32 i;
402 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500404 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500406 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500407 save[0] = *(sp++);
408 save[1] = *(sp++);
409 *(dp++) = *(sp++);
410 *(dp++) = *(sp++);
411 *(dp++) = *(sp++);
412 *(dp++) = *(sp++);
413 *(dp++) = *(sp++);
414 *(dp++) = *(sp++);
415 *(dp++) = save[0];
416 *(dp++) = save[1];
417 }
418 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500419#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500420 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500421
Andreas Dilger47a0c421997-05-16 02:46:07 -0500422 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500423 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500424 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500425 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600426 /* This converts from AG to GA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500427 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500428 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500429 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500430
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500431 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500432 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500433 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500434 *(dp++) = *(sp++);
435 *(dp++) = save;
436 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500437 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500438
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500439#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500440 else
441 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600442 /* This converts from AAGG to GGAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500443 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500444 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500445 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500447 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500448 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500449 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500450 save[0] = *(sp++);
451 save[1] = *(sp++);
452 *(dp++) = *(sp++);
453 *(dp++) = *(sp++);
454 *(dp++) = save[0];
455 *(dp++) = save[1];
456 }
457 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500458#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500459 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500460 }
461}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500462#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500463
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500464#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500465void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600466png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
467{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500468 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500469
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600470 {
471 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
472 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600473 if (row_info->bit_depth == 8)
474 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600475 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600476 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500477 png_uint_32 i;
478 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500479
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500480 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600481 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500482 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600483 *(dp++) = *(sp++);
484 *(dp++) = *(sp++);
485 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600486 */
487 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500488 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600489 }
490 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500491
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500492#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600493 else
494 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600495 /* This inverts the alpha channel in RRGGBBAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600496 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500497 png_uint_32 i;
498 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600499
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500500 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600501 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500502 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600503 *(dp++) = *(sp++);
504 *(dp++) = *(sp++);
505 *(dp++) = *(sp++);
506 *(dp++) = *(sp++);
507 *(dp++) = *(sp++);
508 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600509 */
510 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500511 *(dp++) = (png_byte)(255 - *(sp++));
512 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600513 }
514 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500515#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600516 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500517
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600518 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
519 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600520 if (row_info->bit_depth == 8)
521 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600522 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600523 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500524 png_uint_32 i;
525 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600526
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500527 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600528 {
529 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500530 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600531 }
532 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500533
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500534#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600535 else
536 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600537 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600538 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500539 png_uint_32 i;
540 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600541
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500542 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600543 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500544 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600545 *(dp++) = *(sp++);
546 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600547 */
548 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500549 *(dp++) = (png_byte)(255 - *(sp++));
550 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600551 }
552 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500553#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600554 }
555 }
556}
557#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600558
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500559#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500560/* Undoes intrapixel differencing */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600561void /* PRIVATE */
562png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
563{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500564 png_debug(1, "in png_do_write_intrapixel");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500565
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500566 if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600567 {
568 int bytes_per_pixel;
569 png_uint_32 row_width = row_info->width;
570 if (row_info->bit_depth == 8)
571 {
572 png_bytep rp;
573 png_uint_32 i;
574
575 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
576 bytes_per_pixel = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500577
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600578 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
579 bytes_per_pixel = 4;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500580
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600581 else
582 return;
583
584 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
585 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600586 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
587 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600588 }
589 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500590
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500591#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600592 else if (row_info->bit_depth == 16)
593 {
594 png_bytep rp;
595 png_uint_32 i;
596
597 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
598 bytes_per_pixel = 6;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500599
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600600 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
601 bytes_per_pixel = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500602
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600603 else
604 return;
605
606 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
607 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600608 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
609 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
610 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500611 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
612 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600613 *(rp ) = (png_byte)((red >> 8) & 0xff);
614 *(rp + 1) = (png_byte)(red & 0xff);
615 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
616 *(rp + 5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600617 }
618 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500619#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600620 }
621}
622#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500623#endif /* PNG_WRITE_SUPPORTED */