blob: 3f5f67544b06e4746f72f1edd31d7b791a86a331 [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-Pehrson3a5a7592006-04-15 14:14:42 -05004 * Last changed in libpng 1.2.9 April 14, 2006
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06006 * Copyright (c) 1998-2006 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Guy Schalnat0d580581995-07-20 02:43:20 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050012#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050013#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050014
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050015/* Transform the data according to the user's wishes. The order of
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060016 * transformations is significant.
17 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050018void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -060019png_do_write_transformations(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050020{
Andreas Dilger47a0c421997-05-16 02:46:07 -050021 png_debug(1, "in png_do_write_transformations\n");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060022
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060023 if (png_ptr == NULL)
24 return;
25
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060026#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
27 if (png_ptr->transformations & PNG_USER_TRANSFORM)
28 if(png_ptr->write_user_transform_fn != NULL)
29 (*(png_ptr->write_user_transform_fn)) /* user write transform function */
30 (png_ptr, /* png_ptr */
31 &(png_ptr->row_info), /* row_info: */
32 /* png_uint_32 width; width of row */
33 /* png_uint_32 rowbytes; number of bytes in row */
34 /* png_byte color_type; color type of pixels */
35 /* png_byte bit_depth; bit depth of samples */
36 /* png_byte channels; number of channels (1-4) */
37 /* png_byte pixel_depth; bits per pixel (depth*channels) */
38 png_ptr->row_buf + 1); /* start of pixel data for row */
39#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050040#if defined(PNG_WRITE_FILLER_SUPPORTED)
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -060041 if (png_ptr->transformations & PNG_FILLER)
Andreas Dilger47a0c421997-05-16 02:46:07 -050042 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson40936072004-11-20 11:18:40 -060043 png_ptr->flags);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050044#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060045#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
46 if (png_ptr->transformations & PNG_PACKSWAP)
47 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
48#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050049#if defined(PNG_WRITE_PACK_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050050 if (png_ptr->transformations & PNG_PACK)
51 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
Andreas Dilger47a0c421997-05-16 02:46:07 -050052 (png_uint_32)png_ptr->bit_depth);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050053#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060054#if defined(PNG_WRITE_SWAP_SUPPORTED)
55 if (png_ptr->transformations & PNG_SWAP_BYTES)
56 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
57#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050058#if defined(PNG_WRITE_SHIFT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050059 if (png_ptr->transformations & PNG_SHIFT)
60 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
61 &(png_ptr->shift));
Guy Schalnat51f0eb41995-09-26 05:22:39 -050062#endif
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060063#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
64 if (png_ptr->transformations & PNG_SWAP_ALPHA)
65 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
66#endif
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060067#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
68 if (png_ptr->transformations & PNG_INVERT_ALPHA)
69 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
70#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050071#if defined(PNG_WRITE_BGR_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050072 if (png_ptr->transformations & PNG_BGR)
73 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050074#endif
75#if defined(PNG_WRITE_INVERT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050076 if (png_ptr->transformations & PNG_INVERT_MONO)
77 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050078#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050079}
80
Guy Schalnat51f0eb41995-09-26 05:22:39 -050081#if defined(PNG_WRITE_PACK_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060082/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
83 * row_info bit depth should be 8 (one pixel per byte). The channels
84 * should be 1 (this only happens on grayscale and paletted images).
85 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050086void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050087png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050088{
Andreas Dilger47a0c421997-05-16 02:46:07 -050089 png_debug(1, "in png_do_pack\n");
90 if (row_info->bit_depth == 8 &&
91#if defined(PNG_USELESS_TESTS_SUPPORTED)
92 row != NULL && row_info != NULL &&
93#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050094 row_info->channels == 1)
95 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050096 switch ((int)bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050097 {
98 case 1:
99 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100 png_bytep sp, dp;
101 int mask, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500102 png_uint_32 i;
103 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500104
105 sp = row;
106 dp = row;
107 mask = 0x80;
108 v = 0;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600109
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500110 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500111 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112 if (*sp != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500113 v |= mask;
114 sp++;
115 if (mask > 1)
116 mask >>= 1;
117 else
118 {
119 mask = 0x80;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600120 *dp = (png_byte)v;
121 dp++;
Guy Schalnat0d580581995-07-20 02:43:20 -0500122 v = 0;
123 }
124 }
125 if (mask != 0x80)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600126 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500127 break;
128 }
129 case 2:
130 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500131 png_bytep sp, dp;
132 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500133 png_uint_32 i;
134 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500135
136 sp = row;
137 dp = row;
138 shift = 6;
139 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500140 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500141 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500142 png_byte value;
143
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600144 value = (png_byte)(*sp & 0x03);
Guy Schalnat0d580581995-07-20 02:43:20 -0500145 v |= (value << shift);
146 if (shift == 0)
147 {
148 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600149 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500150 dp++;
151 v = 0;
152 }
153 else
154 shift -= 2;
155 sp++;
156 }
157 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600158 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500159 break;
160 }
161 case 4:
162 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500163 png_bytep sp, dp;
164 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500165 png_uint_32 i;
166 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500167
168 sp = row;
169 dp = row;
170 shift = 4;
171 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500172 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500173 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500174 png_byte value;
175
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600176 value = (png_byte)(*sp & 0x0f);
Guy Schalnat0d580581995-07-20 02:43:20 -0500177 v |= (value << shift);
178
179 if (shift == 0)
180 {
181 shift = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600182 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500183 dp++;
184 v = 0;
185 }
186 else
187 shift -= 4;
188
189 sp++;
190 }
191 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600192 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500193 break;
194 }
195 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500196 row_info->bit_depth = (png_byte)bit_depth;
197 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500198 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
199 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500200 }
201}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500202#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500203
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500204#if defined(PNG_WRITE_SHIFT_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600205/* Shift pixel values to take advantage of whole range. Pass the
206 * true number of bits in bit_depth. The row should be packed
207 * according to row_info->bit_depth. Thus, if you had a row of
208 * bit depth 4, but the pixels only had values from 0 to 7, you
209 * would pass 3 as bit_depth, and this routine would translate the
210 * data to 0 to 15.
211 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500212void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600213png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500214{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500215 png_debug(1, "in png_do_shift\n");
216#if defined(PNG_USELESS_TESTS_SUPPORTED)
217 if (row != NULL && row_info != NULL &&
218#else
219 if (
220#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500221 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
222 {
223 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500224 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500225
Guy Schalnat0d580581995-07-20 02:43:20 -0500226 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
227 {
228 shift_start[channels] = row_info->bit_depth - bit_depth->red;
229 shift_dec[channels] = bit_depth->red;
230 channels++;
231 shift_start[channels] = row_info->bit_depth - bit_depth->green;
232 shift_dec[channels] = bit_depth->green;
233 channels++;
234 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
235 shift_dec[channels] = bit_depth->blue;
236 channels++;
237 }
238 else
239 {
240 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
241 shift_dec[channels] = bit_depth->gray;
242 channels++;
243 }
244 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
245 {
246 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
247 shift_dec[channels] = bit_depth->alpha;
248 channels++;
249 }
250
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600251 /* with low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500252 if (row_info->bit_depth < 8)
253 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500254 png_bytep bp = row;
255 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500256 png_byte mask;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500257 png_uint_32 row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500258
259 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
260 mask = 0x55;
261 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
262 mask = 0x11;
263 else
264 mask = 0xff;
265
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500266 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500267 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268 png_uint_16 v;
269 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500270
271 v = *bp;
272 *bp = 0;
273 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
274 {
275 if (j > 0)
276 *bp |= (png_byte)((v << j) & 0xff);
277 else
278 *bp |= (png_byte)((v >> (-j)) & mask);
279 }
280 }
281 }
282 else if (row_info->bit_depth == 8)
283 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500284 png_bytep bp = row;
285 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500286 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500287
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500288 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500289 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500290
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500291 png_uint_16 v;
292 int j;
293 int c = (int)(i%channels);
294
295 v = *bp;
296 *bp = 0;
297 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500298 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500299 if (j > 0)
300 *bp |= (png_byte)((v << j) & 0xff);
301 else
302 *bp |= (png_byte)((v >> (-j)) & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500303 }
304 }
305 }
306 else
307 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600308 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500309 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500310 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500311
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500312 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500313 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500314 int c = (int)(i%channels);
315 png_uint_16 value, v;
316 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500317
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500318 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500319 value = 0;
320 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500321 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500322 if (j > 0)
323 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
324 else
325 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500326 }
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500327 *bp++ = (png_byte)(value >> 8);
328 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500329 }
330 }
331 }
332}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500333#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500334
Andreas Dilger47a0c421997-05-16 02:46:07 -0500335#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500336void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500338{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500339 png_debug(1, "in png_do_write_swap_alpha\n");
340#if defined(PNG_USELESS_TESTS_SUPPORTED)
341 if (row != NULL && row_info != NULL)
342#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500343 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500345 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500346 /* This converts from ARGB to RGBA */
347 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500348 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500349 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500350 png_uint_32 i;
351 png_uint_32 row_width = row_info->width;
352 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500353 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500354 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500355 *(dp++) = *(sp++);
356 *(dp++) = *(sp++);
357 *(dp++) = *(sp++);
358 *(dp++) = save;
359 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500360 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500361 /* This converts from AARRGGBB to RRGGBBAA */
362 else
363 {
364 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500365 png_uint_32 i;
366 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500367
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500368 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500370 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500371 save[0] = *(sp++);
372 save[1] = *(sp++);
373 *(dp++) = *(sp++);
374 *(dp++) = *(sp++);
375 *(dp++) = *(sp++);
376 *(dp++) = *(sp++);
377 *(dp++) = *(sp++);
378 *(dp++) = *(sp++);
379 *(dp++) = save[0];
380 *(dp++) = save[1];
381 }
382 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500383 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500384 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500385 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386 /* This converts from AG to GA */
387 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500388 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500389 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500390 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500391 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500393 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500394 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500395 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500396 *(dp++) = *(sp++);
397 *(dp++) = save;
398 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500399 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500400 /* This converts from AAGG to GGAA */
401 else
402 {
403 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500404 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500405 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500406
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500407 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500408 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500409 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500410 save[0] = *(sp++);
411 save[1] = *(sp++);
412 *(dp++) = *(sp++);
413 *(dp++) = *(sp++);
414 *(dp++) = save[0];
415 *(dp++) = save[1];
416 }
417 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500418 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500419 }
420}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500421#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500422
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600423#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500424void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600425png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
426{
427 png_debug(1, "in png_do_write_invert_alpha\n");
428#if defined(PNG_USELESS_TESTS_SUPPORTED)
429 if (row != NULL && row_info != NULL)
430#endif
431 {
432 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
433 {
434 /* This inverts the alpha channel in RGBA */
435 if (row_info->bit_depth == 8)
436 {
437 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500438 png_uint_32 i;
439 png_uint_32 row_width = row_info->width;
440 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600441 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600442 /* does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600443 *(dp++) = *(sp++);
444 *(dp++) = *(sp++);
445 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600446 */
447 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500448 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600449 }
450 }
451 /* This inverts the alpha channel in RRGGBBAA */
452 else
453 {
454 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500455 png_uint_32 i;
456 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600457
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500458 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600459 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600460 /* does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600461 *(dp++) = *(sp++);
462 *(dp++) = *(sp++);
463 *(dp++) = *(sp++);
464 *(dp++) = *(sp++);
465 *(dp++) = *(sp++);
466 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600467 */
468 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500469 *(dp++) = (png_byte)(255 - *(sp++));
470 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600471 }
472 }
473 }
474 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
475 {
476 /* This inverts the alpha channel in GA */
477 if (row_info->bit_depth == 8)
478 {
479 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500480 png_uint_32 i;
481 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600482
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500483 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600484 {
485 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500486 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600487 }
488 }
489 /* This inverts the alpha channel in GGAA */
490 else
491 {
492 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500493 png_uint_32 i;
494 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600495
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500496 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600497 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600498 /* does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600499 *(dp++) = *(sp++);
500 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600501 */
502 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500503 *(dp++) = (png_byte)(255 - *(sp++));
504 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600505 }
506 }
507 }
508 }
509}
510#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600511
512#if defined(PNG_MNG_FEATURES_SUPPORTED)
513/* undoes intrapixel differencing */
514void /* PRIVATE */
515png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
516{
517 png_debug(1, "in png_do_write_intrapixel\n");
518 if (
519#if defined(PNG_USELESS_TESTS_SUPPORTED)
520 row != NULL && row_info != NULL &&
521#endif
522 (row_info->color_type & PNG_COLOR_MASK_COLOR))
523 {
524 int bytes_per_pixel;
525 png_uint_32 row_width = row_info->width;
526 if (row_info->bit_depth == 8)
527 {
528 png_bytep rp;
529 png_uint_32 i;
530
531 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
532 bytes_per_pixel = 3;
533 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
534 bytes_per_pixel = 4;
535 else
536 return;
537
538 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
539 {
540 *(rp) = (png_byte)((*rp - *(rp+1))&0xff);
541 *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
542 }
543 }
544 else if (row_info->bit_depth == 16)
545 {
546 png_bytep rp;
547 png_uint_32 i;
548
549 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
550 bytes_per_pixel = 6;
551 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
552 bytes_per_pixel = 8;
553 else
554 return;
555
556 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
557 {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500558 png_uint_32 s0 = (*(rp ) << 8) | *(rp+1);
559 png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3);
560 png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5);
561 png_uint_32 red = (png_uint_32)((s0-s1) & 0xffffL);
562 png_uint_32 blue = (png_uint_32)((s2-s1) & 0xffffL);
563 *(rp ) = (png_byte)((red >> 8) & 0xff);
564 *(rp+1) = (png_byte)(red & 0xff);
565 *(rp+4) = (png_byte)((blue >> 8) & 0xff);
566 *(rp+5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600567 }
568 }
569 }
570}
571#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500572#endif /* PNG_WRITE_SUPPORTED */