blob: 1765ef89d0e26a67e0c864f16c586b784f4cc3b7 [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 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600223
224 default:
225 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500226 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500227
Andreas Dilger47a0c421997-05-16 02:46:07 -0500228 row_info->bit_depth = (png_byte)bit_depth;
229 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500230 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600231 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500232 }
233}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500234#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500235
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500236#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600237/* Shift pixel values to take advantage of whole range. Pass the
238 * true number of bits in bit_depth. The row should be packed
239 * according to row_info->bit_depth. Thus, if you had a row of
240 * bit depth 4, but the pixels only had values from 0 to 7, you
241 * would pass 3 as bit_depth, and this routine would translate the
242 * data to 0 to 15.
243 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500244void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500245png_do_shift(png_row_infop row_info, png_bytep row,
246 png_const_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500247{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500248 png_debug(1, "in png_do_shift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500249
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500250 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500251 {
252 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500253 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500254
Guy Schalnat0d580581995-07-20 02:43:20 -0500255 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
256 {
257 shift_start[channels] = row_info->bit_depth - bit_depth->red;
258 shift_dec[channels] = bit_depth->red;
259 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500260
Guy Schalnat0d580581995-07-20 02:43:20 -0500261 shift_start[channels] = row_info->bit_depth - bit_depth->green;
262 shift_dec[channels] = bit_depth->green;
263 channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500264
Guy Schalnat0d580581995-07-20 02:43:20 -0500265 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
266 shift_dec[channels] = bit_depth->blue;
267 channels++;
268 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500269
Guy Schalnat0d580581995-07-20 02:43:20 -0500270 else
271 {
272 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
273 shift_dec[channels] = bit_depth->gray;
274 channels++;
275 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500276
Guy Schalnat0d580581995-07-20 02:43:20 -0500277 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
278 {
279 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
280 shift_dec[channels] = bit_depth->alpha;
281 channels++;
282 }
283
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500284 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500285 if (row_info->bit_depth < 8)
286 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500287 png_bytep bp = row;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500288 png_size_t i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500289 png_byte mask;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500290 png_size_t row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500291
292 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
293 mask = 0x55;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500294
Guy Schalnat0d580581995-07-20 02:43:20 -0500295 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
296 mask = 0x11;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500297
Guy Schalnat0d580581995-07-20 02:43:20 -0500298 else
299 mask = 0xff;
300
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500301 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500302 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500303 png_uint_16 v;
304 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500305
306 v = *bp;
307 *bp = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500308
Guy Schalnat0d580581995-07-20 02:43:20 -0500309 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
310 {
311 if (j > 0)
312 *bp |= (png_byte)((v << j) & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500313
Guy Schalnat0d580581995-07-20 02:43:20 -0500314 else
315 *bp |= (png_byte)((v >> (-j)) & mask);
316 }
317 }
318 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500319
Guy Schalnat0d580581995-07-20 02:43:20 -0500320 else if (row_info->bit_depth == 8)
321 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500322 png_bytep bp = row;
323 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500324 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500325
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500326 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500327 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500328
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500329 png_uint_16 v;
330 int j;
331 int c = (int)(i%channels);
332
333 v = *bp;
334 *bp = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500335
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500336 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500337 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500338 if (j > 0)
339 *bp |= (png_byte)((v << j) & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500340
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500341 else
342 *bp |= (png_byte)((v >> (-j)) & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500343 }
344 }
345 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500346
Guy Schalnat0d580581995-07-20 02:43:20 -0500347 else
348 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600349 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500350 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500351 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500352
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500353 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500354 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500355 int c = (int)(i%channels);
356 png_uint_16 value, v;
357 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500358
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500359 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500360 value = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500361
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500362 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500363 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500364 if (j > 0)
365 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500366
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500367 else
368 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500369 }
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500370 *bp++ = (png_byte)(value >> 8);
371 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500372 }
373 }
374 }
375}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500376#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500377
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500378#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500379void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500380png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500381{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500382 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500383
Guy Schalnat0d580581995-07-20 02:43:20 -0500384 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500386 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500388 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600389 /* This converts from ARGB to RGBA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500390 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500391 png_uint_32 i;
392 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500393
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500394 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500395 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500396 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 *(dp++) = *(sp++);
398 *(dp++) = *(sp++);
399 *(dp++) = *(sp++);
400 *(dp++) = save;
401 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500402 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500403
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500404#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405 else
406 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600407 /* This converts from AARRGGBB to RRGGBBAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500408 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500409 png_uint_32 i;
410 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500411
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500412 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500413 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500414 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500415 save[0] = *(sp++);
416 save[1] = *(sp++);
417 *(dp++) = *(sp++);
418 *(dp++) = *(sp++);
419 *(dp++) = *(sp++);
420 *(dp++) = *(sp++);
421 *(dp++) = *(sp++);
422 *(dp++) = *(sp++);
423 *(dp++) = save[0];
424 *(dp++) = save[1];
425 }
426 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500427#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500428 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500429
Andreas Dilger47a0c421997-05-16 02:46:07 -0500430 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500431 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500432 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500433 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600434 /* This converts from AG to GA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500435 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500436 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500437 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500438
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500439 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500440 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500441 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500442 *(dp++) = *(sp++);
443 *(dp++) = save;
444 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500445 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500446
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500447#ifdef PNG_WRITE_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500448 else
449 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600450 /* This converts from AAGG to GGAA */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500451 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500452 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500453 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500454
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500455 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500457 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500458 save[0] = *(sp++);
459 save[1] = *(sp++);
460 *(dp++) = *(sp++);
461 *(dp++) = *(sp++);
462 *(dp++) = save[0];
463 *(dp++) = save[1];
464 }
465 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500466#endif /* PNG_WRITE_16BIT_SUPPORTED */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500467 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500468 }
469}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500470#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500471
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500472#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500473void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600474png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
475{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500476 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500477
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600478 {
479 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
480 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600481 if (row_info->bit_depth == 8)
482 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600483 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600484 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500485 png_uint_32 i;
486 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500487
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500488 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600489 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500490 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600491 *(dp++) = *(sp++);
492 *(dp++) = *(sp++);
493 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600494 */
495 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500496 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600497 }
498 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500499
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500500#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600501 else
502 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600503 /* This inverts the alpha channel in RRGGBBAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600504 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500505 png_uint_32 i;
506 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600507
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500508 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600509 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500510 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600511 *(dp++) = *(sp++);
512 *(dp++) = *(sp++);
513 *(dp++) = *(sp++);
514 *(dp++) = *(sp++);
515 *(dp++) = *(sp++);
516 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600517 */
518 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500519 *(dp++) = (png_byte)(255 - *(sp++));
520 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600521 }
522 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500523#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600524 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500525
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600526 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
527 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600528 if (row_info->bit_depth == 8)
529 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600530 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600531 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500532 png_uint_32 i;
533 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600534
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500535 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600536 {
537 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500538 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600539 }
540 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500541
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500542#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600543 else
544 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600545 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600546 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500547 png_uint_32 i;
548 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600549
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500550 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600551 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500552 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600553 *(dp++) = *(sp++);
554 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600555 */
556 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500557 *(dp++) = (png_byte)(255 - *(sp++));
558 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600559 }
560 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500561#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600562 }
563 }
564}
565#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600566
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500567#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500568/* Undoes intrapixel differencing */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600569void /* PRIVATE */
570png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
571{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500572 png_debug(1, "in png_do_write_intrapixel");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500573
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500574 if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600575 {
576 int bytes_per_pixel;
577 png_uint_32 row_width = row_info->width;
578 if (row_info->bit_depth == 8)
579 {
580 png_bytep rp;
581 png_uint_32 i;
582
583 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
584 bytes_per_pixel = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500585
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600586 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
587 bytes_per_pixel = 4;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500588
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600589 else
590 return;
591
592 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
593 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600594 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
595 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600596 }
597 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500598
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500599#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600600 else if (row_info->bit_depth == 16)
601 {
602 png_bytep rp;
603 png_uint_32 i;
604
605 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
606 bytes_per_pixel = 6;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500607
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600608 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
609 bytes_per_pixel = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500610
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600611 else
612 return;
613
614 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
615 {
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600616 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
617 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
618 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500619 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
620 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600621 *(rp ) = (png_byte)((red >> 8) & 0xff);
622 *(rp + 1) = (png_byte)(red & 0xff);
623 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
624 *(rp + 5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600625 }
626 }
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500627#endif /* PNG_WRITE_16BIT_SUPPORTED */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600628 }
629}
630#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500631#endif /* PNG_WRITE_SUPPORTED */