blob: 066e1e2c937e9b3cc0ec3c935b65802f94ef536a [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-Pehrsonf0eda4e2010-10-15 15:01:57 -05004 * Last changed in libpng 1.5.0 [(PENDING RELEASE)]
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-Pehrsonccadcae2010-10-23 17:29:13 -050032 (*(png_ptr->write_user_transform_fn)) /* User write transform
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060033 function */
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -050034 (png_ptr, /* png_ptr */
35 &(png_ptr->row_info), /* row_info: */
36 /* png_uint_32 width; width of row */
37 /* png_size_t 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 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500145
Guy Schalnat0d580581995-07-20 02:43:20 -0500146 case 2:
147 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500148 png_bytep sp, dp;
149 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500150 png_uint_32 i;
151 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500152
153 sp = row;
154 dp = row;
155 shift = 6;
156 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500157
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500158 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500159 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500160 png_byte value;
161
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600162 value = (png_byte)(*sp & 0x03);
Guy Schalnat0d580581995-07-20 02:43:20 -0500163 v |= (value << shift);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500164
Guy Schalnat0d580581995-07-20 02:43:20 -0500165 if (shift == 0)
166 {
167 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600168 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500169 dp++;
170 v = 0;
171 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500172
Guy Schalnat0d580581995-07-20 02:43:20 -0500173 else
174 shift -= 2;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500175
Guy Schalnat0d580581995-07-20 02:43:20 -0500176 sp++;
177 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500178
Guy Schalnat0d580581995-07-20 02:43:20 -0500179 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600180 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500181
Guy Schalnat0d580581995-07-20 02:43:20 -0500182 break;
183 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500184
Guy Schalnat0d580581995-07-20 02:43:20 -0500185 case 4:
186 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 png_bytep sp, dp;
188 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500189 png_uint_32 i;
190 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500191
192 sp = row;
193 dp = row;
194 shift = 4;
195 v = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500196
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500197 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500198 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500199 png_byte value;
200
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600201 value = (png_byte)(*sp & 0x0f);
Guy Schalnat0d580581995-07-20 02:43:20 -0500202 v |= (value << shift);
203
204 if (shift == 0)
205 {
206 shift = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600207 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500208 dp++;
209 v = 0;
210 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500211
Guy Schalnat0d580581995-07-20 02:43:20 -0500212 else
213 shift -= 4;
214
215 sp++;
216 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500217
Guy Schalnat0d580581995-07-20 02:43:20 -0500218 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600219 *dp = (png_byte)v;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500220
Guy Schalnat0d580581995-07-20 02:43:20 -0500221 break;
222 }
223 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500224
Andreas Dilger47a0c421997-05-16 02:46:07 -0500225 row_info->bit_depth = (png_byte)bit_depth;
226 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500227 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600228 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500229 }
230}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500231#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500232
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500233#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600234/* Shift pixel values to take advantage of whole range. Pass the
235 * true number of bits in bit_depth. The row should be packed
236 * according to row_info->bit_depth. Thus, if you had a row of
237 * bit depth 4, but the pixels only had values from 0 to 7, you
238 * would pass 3 as bit_depth, and this routine would translate the
239 * data to 0 to 15.
240 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500241void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500242png_do_shift(png_row_infop row_info, png_bytep row,
243 png_const_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500244{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500245 png_debug(1, "in png_do_shift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500246
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500247 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500248 {
249 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500250 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500251
Guy Schalnat0d580581995-07-20 02:43:20 -0500252 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
253 {
254 shift_start[channels] = row_info->bit_depth - bit_depth->red;
255 shift_dec[channels] = bit_depth->red;
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->green;
259 shift_dec[channels] = bit_depth->green;
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->blue;
263 shift_dec[channels] = bit_depth->blue;
264 channels++;
265 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500266
Guy Schalnat0d580581995-07-20 02:43:20 -0500267 else
268 {
269 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
270 shift_dec[channels] = bit_depth->gray;
271 channels++;
272 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500273
Guy Schalnat0d580581995-07-20 02:43:20 -0500274 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
275 {
276 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
277 shift_dec[channels] = bit_depth->alpha;
278 channels++;
279 }
280
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500281 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500282 if (row_info->bit_depth < 8)
283 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500284 png_bytep bp = row;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500285 png_size_t i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500286 png_byte mask;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500287 png_size_t row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500288
289 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
290 mask = 0x55;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500291
Guy Schalnat0d580581995-07-20 02:43:20 -0500292 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
293 mask = 0x11;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500294
Guy Schalnat0d580581995-07-20 02:43:20 -0500295 else
296 mask = 0xff;
297
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500298 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500299 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300 png_uint_16 v;
301 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500302
303 v = *bp;
304 *bp = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500305
Guy Schalnat0d580581995-07-20 02:43:20 -0500306 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
307 {
308 if (j > 0)
309 *bp |= (png_byte)((v << j) & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500310
Guy Schalnat0d580581995-07-20 02:43:20 -0500311 else
312 *bp |= (png_byte)((v >> (-j)) & mask);
313 }
314 }
315 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500316
Guy Schalnat0d580581995-07-20 02:43:20 -0500317 else if (row_info->bit_depth == 8)
318 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500319 png_bytep bp = row;
320 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500321 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500322
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500323 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500324 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500325
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500326 png_uint_16 v;
327 int j;
328 int c = (int)(i%channels);
329
330 v = *bp;
331 *bp = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500332
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500333 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500334 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500335 if (j > 0)
336 *bp |= (png_byte)((v << j) & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500337
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500338 else
339 *bp |= (png_byte)((v >> (-j)) & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500340 }
341 }
342 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500343
Guy Schalnat0d580581995-07-20 02:43:20 -0500344 else
345 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600346 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500347 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500348 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500349
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500350 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500351 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500352 int c = (int)(i%channels);
353 png_uint_16 value, v;
354 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500355
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500356 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500357 value = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500358
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500359 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500360 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500361 if (j > 0)
362 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500363
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500364 else
365 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500366 }
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500367 *bp++ = (png_byte)(value >> 8);
368 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500369 }
370 }
371 }
372}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500373#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500374
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500375#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500376void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500378{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500379 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500380
Guy Schalnat0d580581995-07-20 02:43:20 -0500381 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500383 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500384 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500385 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600386 /* This converts from ARGB to RGBA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500388 png_uint_32 i;
389 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500390
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500391 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500393 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500394 *(dp++) = *(sp++);
395 *(dp++) = *(sp++);
396 *(dp++) = *(sp++);
397 *(dp++) = save;
398 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500399 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500400
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500401#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402 else
403 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600404 /* This converts from AARRGGBB to RRGGBBAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500406 png_uint_32 i;
407 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500408
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500409 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500410 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500411 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500412 save[0] = *(sp++);
413 save[1] = *(sp++);
414 *(dp++) = *(sp++);
415 *(dp++) = *(sp++);
416 *(dp++) = *(sp++);
417 *(dp++) = *(sp++);
418 *(dp++) = *(sp++);
419 *(dp++) = *(sp++);
420 *(dp++) = save[0];
421 *(dp++) = save[1];
422 }
423 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500424#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500425 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500426
Andreas Dilger47a0c421997-05-16 02:46:07 -0500427 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500428 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500429 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500430 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600431 /* This converts from AG to GA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500432 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500433 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500434 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500435
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500436 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500437 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500438 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500439 *(dp++) = *(sp++);
440 *(dp++) = save;
441 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500442 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500443
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500444#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500445 else
446 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600447 /* This converts from AAGG to GGAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500448 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500449 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500450 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500451
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500452 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500454 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500455 save[0] = *(sp++);
456 save[1] = *(sp++);
457 *(dp++) = *(sp++);
458 *(dp++) = *(sp++);
459 *(dp++) = save[0];
460 *(dp++) = save[1];
461 }
462 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500463#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500464 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500465 }
466}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500467#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500468
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500469#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500470void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600471png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
472{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500473 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500474
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600475 {
476 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
477 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600478 if (row_info->bit_depth == 8)
479 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600480 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600481 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500482 png_uint_32 i;
483 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500484
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500485 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600486 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500487 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600488 *(dp++) = *(sp++);
489 *(dp++) = *(sp++);
490 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600491 */
492 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500493 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600494 }
495 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500496
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500497#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600498 else
499 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600500 /* This inverts the alpha channel in RRGGBBAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600501 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500502 png_uint_32 i;
503 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600504
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500505 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600506 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500507 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600508 *(dp++) = *(sp++);
509 *(dp++) = *(sp++);
510 *(dp++) = *(sp++);
511 *(dp++) = *(sp++);
512 *(dp++) = *(sp++);
513 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600514 */
515 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500516 *(dp++) = (png_byte)(255 - *(sp++));
517 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600518 }
519 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500520#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600521 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500522
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600523 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
524 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600525 if (row_info->bit_depth == 8)
526 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600527 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600528 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500529 png_uint_32 i;
530 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600531
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500532 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600533 {
534 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500535 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600536 }
537 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500538
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500539#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600540 else
541 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600542 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600543 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500544 png_uint_32 i;
545 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600546
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500547 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600548 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500549 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600550 *(dp++) = *(sp++);
551 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600552 */
553 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500554 *(dp++) = (png_byte)(255 - *(sp++));
555 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600556 }
557 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500558#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600559 }
560 }
561}
562#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600563
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500564#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500565/* Undoes intrapixel differencing */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600566void /* PRIVATE */
567png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
568{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500569 png_debug(1, "in png_do_write_intrapixel");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500570
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500571 if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600572 {
573 int bytes_per_pixel;
574 png_uint_32 row_width = row_info->width;
575 if (row_info->bit_depth == 8)
576 {
577 png_bytep rp;
578 png_uint_32 i;
579
580 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
581 bytes_per_pixel = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500582
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600583 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
584 bytes_per_pixel = 4;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500585
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600586 else
587 return;
588
589 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
590 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600591 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
592 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600593 }
594 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500595
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500596#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600597 else if (row_info->bit_depth == 16)
598 {
599 png_bytep rp;
600 png_uint_32 i;
601
602 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
603 bytes_per_pixel = 6;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500604
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600605 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
606 bytes_per_pixel = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500607
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600608 else
609 return;
610
611 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
612 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600613 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
614 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
615 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500616 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
617 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600618 *(rp ) = (png_byte)((red >> 8) & 0xff);
619 *(rp + 1) = (png_byte)(red & 0xff);
620 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
621 *(rp + 5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600622 }
623 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500624#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600625 }
626}
627#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500628#endif /* PNG_WRITE_SUPPORTED */