blob: 5fd0962bbba8b89d196f271e7df24b522c3fb1e9 [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-Pehrson668af4e2009-06-24 06:35:59 -05004 * Last changed in libpng 1.4.0 [June 24, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 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 *
9 * This code is released under the zlib-libpng license.
10 * For conditions of distribution and use, see copyright notice in png.h
11 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
Guy Schalnat0d580581995-07-20 02:43:20 -050014#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050015#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050016#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050017
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-Pehrson08a33431998-03-07 06:06:55 -060029#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
30 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-Pehrson4bb4d012009-05-20 12:45:29 -050032 (*(png_ptr->write_user_transform_fn)) /* User write transform function */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -060033 (png_ptr, /* png_ptr */
34 &(png_ptr->row_info), /* row_info: */
35 /* png_uint_32 width; width of row */
36 /* png_uint_32 rowbytes; number of bytes in row */
37 /* png_byte color_type; color type of pixels */
38 /* png_byte bit_depth; bit depth of samples */
39 /* png_byte channels; number of channels (1-4) */
40 /* png_byte pixel_depth; bits per pixel (depth*channels) */
41 png_ptr->row_buf + 1); /* start of pixel data for row */
42#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050043#if defined(PNG_WRITE_FILLER_SUPPORTED)
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -060044 if (png_ptr->transformations & PNG_FILLER)
Andreas Dilger47a0c421997-05-16 02:46:07 -050045 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
Glenn Randers-Pehrson40936072004-11-20 11:18:40 -060046 png_ptr->flags);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050047#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060048#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
49 if (png_ptr->transformations & PNG_PACKSWAP)
50 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
51#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050052#if defined(PNG_WRITE_PACK_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050053 if (png_ptr->transformations & PNG_PACK)
54 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
Andreas Dilger47a0c421997-05-16 02:46:07 -050055 (png_uint_32)png_ptr->bit_depth);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050056#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060057#if defined(PNG_WRITE_SWAP_SUPPORTED)
58 if (png_ptr->transformations & PNG_SWAP_BYTES)
59 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
60#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050061#if defined(PNG_WRITE_SHIFT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050062 if (png_ptr->transformations & PNG_SHIFT)
63 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
64 &(png_ptr->shift));
Guy Schalnat51f0eb41995-09-26 05:22:39 -050065#endif
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -060066#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
67 if (png_ptr->transformations & PNG_SWAP_ALPHA)
68 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
69#endif
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060070#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
71 if (png_ptr->transformations & PNG_INVERT_ALPHA)
72 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
73#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050074#if defined(PNG_WRITE_BGR_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050075 if (png_ptr->transformations & PNG_BGR)
76 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050077#endif
78#if defined(PNG_WRITE_INVERT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050079 if (png_ptr->transformations & PNG_INVERT_MONO)
80 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050081#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050082}
83
Guy Schalnat51f0eb41995-09-26 05:22:39 -050084#if defined(PNG_WRITE_PACK_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060085/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
86 * row_info bit depth should be 8 (one pixel per byte). The channels
87 * should be 1 (this only happens on grayscale and paletted images).
88 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050089void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050090png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050091{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050092 png_debug(1, "in png_do_pack");
Andreas Dilger47a0c421997-05-16 02:46:07 -050093 if (row_info->bit_depth == 8 &&
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{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500215 png_debug(1, "in png_do_shift");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500216 if (
Guy Schalnat0d580581995-07-20 02:43:20 -0500217 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
218 {
219 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500220 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500221
Guy Schalnat0d580581995-07-20 02:43:20 -0500222 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
223 {
224 shift_start[channels] = row_info->bit_depth - bit_depth->red;
225 shift_dec[channels] = bit_depth->red;
226 channels++;
227 shift_start[channels] = row_info->bit_depth - bit_depth->green;
228 shift_dec[channels] = bit_depth->green;
229 channels++;
230 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
231 shift_dec[channels] = bit_depth->blue;
232 channels++;
233 }
234 else
235 {
236 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
237 shift_dec[channels] = bit_depth->gray;
238 channels++;
239 }
240 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
241 {
242 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
243 shift_dec[channels] = bit_depth->alpha;
244 channels++;
245 }
246
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500247 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500248 if (row_info->bit_depth < 8)
249 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500250 png_bytep bp = row;
251 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500252 png_byte mask;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500253 png_uint_32 row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500254
255 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
256 mask = 0x55;
257 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
258 mask = 0x11;
259 else
260 mask = 0xff;
261
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500262 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500263 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500264 png_uint_16 v;
265 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500266
267 v = *bp;
268 *bp = 0;
269 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
270 {
271 if (j > 0)
272 *bp |= (png_byte)((v << j) & 0xff);
273 else
274 *bp |= (png_byte)((v >> (-j)) & mask);
275 }
276 }
277 }
278 else if (row_info->bit_depth == 8)
279 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500280 png_bytep bp = row;
281 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500282 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500283
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500284 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500285 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500286
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500287 png_uint_16 v;
288 int j;
289 int c = (int)(i%channels);
290
291 v = *bp;
292 *bp = 0;
293 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500294 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500295 if (j > 0)
296 *bp |= (png_byte)((v << j) & 0xff);
297 else
298 *bp |= (png_byte)((v >> (-j)) & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500299 }
300 }
301 }
302 else
303 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600304 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500305 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500306 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500307
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500308 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500309 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500310 int c = (int)(i%channels);
311 png_uint_16 value, v;
312 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500313
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500314 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500315 value = 0;
316 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500317 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500318 if (j > 0)
319 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
320 else
321 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500322 }
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500323 *bp++ = (png_byte)(value >> 8);
324 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500325 }
326 }
327 }
328}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500329#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500330
Andreas Dilger47a0c421997-05-16 02:46:07 -0500331#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500332void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500333png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500334{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500335 png_debug(1, "in png_do_write_swap_alpha");
Guy Schalnat0d580581995-07-20 02:43:20 -0500336 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500338 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500339 /* This converts from ARGB to RGBA */
340 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500341 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500342 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500343 png_uint_32 i;
344 png_uint_32 row_width = row_info->width;
345 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500346 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500347 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500348 *(dp++) = *(sp++);
349 *(dp++) = *(sp++);
350 *(dp++) = *(sp++);
351 *(dp++) = save;
352 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500353 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500354 /* This converts from AARRGGBB to RRGGBBAA */
355 else
356 {
357 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500358 png_uint_32 i;
359 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500360
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500361 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500362 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500363 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500364 save[0] = *(sp++);
365 save[1] = *(sp++);
366 *(dp++) = *(sp++);
367 *(dp++) = *(sp++);
368 *(dp++) = *(sp++);
369 *(dp++) = *(sp++);
370 *(dp++) = *(sp++);
371 *(dp++) = *(sp++);
372 *(dp++) = save[0];
373 *(dp++) = save[1];
374 }
375 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500376 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500378 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500379 /* This converts from AG to GA */
380 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500381 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500384 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500386 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500388 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500389 *(dp++) = *(sp++);
390 *(dp++) = save;
391 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500392 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500393 /* This converts from AAGG to GGAA */
394 else
395 {
396 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500398 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500399
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500400 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500401 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500402 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403 save[0] = *(sp++);
404 save[1] = *(sp++);
405 *(dp++) = *(sp++);
406 *(dp++) = *(sp++);
407 *(dp++) = save[0];
408 *(dp++) = save[1];
409 }
410 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500411 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500412 }
413}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500414#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500415
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600416#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500417void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600418png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
419{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500420 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600421 {
422 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
423 {
424 /* This inverts the alpha channel in RGBA */
425 if (row_info->bit_depth == 8)
426 {
427 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500428 png_uint_32 i;
429 png_uint_32 row_width = row_info->width;
430 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600431 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500432 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600433 *(dp++) = *(sp++);
434 *(dp++) = *(sp++);
435 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600436 */
437 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500438 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600439 }
440 }
441 /* This inverts the alpha channel in RRGGBBAA */
442 else
443 {
444 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500445 png_uint_32 i;
446 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600447
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500448 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600449 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500450 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600451 *(dp++) = *(sp++);
452 *(dp++) = *(sp++);
453 *(dp++) = *(sp++);
454 *(dp++) = *(sp++);
455 *(dp++) = *(sp++);
456 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600457 */
458 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500459 *(dp++) = (png_byte)(255 - *(sp++));
460 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600461 }
462 }
463 }
464 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
465 {
466 /* This inverts the alpha channel in GA */
467 if (row_info->bit_depth == 8)
468 {
469 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500470 png_uint_32 i;
471 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600472
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500473 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600474 {
475 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500476 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600477 }
478 }
479 /* This inverts the alpha channel in GGAA */
480 else
481 {
482 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500483 png_uint_32 i;
484 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600485
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500486 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600487 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500488 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600489 *(dp++) = *(sp++);
490 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600491 */
492 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500493 *(dp++) = (png_byte)(255 - *(sp++));
494 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600495 }
496 }
497 }
498 }
499}
500#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600501
502#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500503/* Undoes intrapixel differencing */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600504void /* PRIVATE */
505png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
506{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500507 png_debug(1, "in png_do_write_intrapixel");
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600508 if (
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600509 (row_info->color_type & PNG_COLOR_MASK_COLOR))
510 {
511 int bytes_per_pixel;
512 png_uint_32 row_width = row_info->width;
513 if (row_info->bit_depth == 8)
514 {
515 png_bytep rp;
516 png_uint_32 i;
517
518 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
519 bytes_per_pixel = 3;
520 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
521 bytes_per_pixel = 4;
522 else
523 return;
524
525 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
526 {
527 *(rp) = (png_byte)((*rp - *(rp+1))&0xff);
528 *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
529 }
530 }
531 else if (row_info->bit_depth == 16)
532 {
533 png_bytep rp;
534 png_uint_32 i;
535
536 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
537 bytes_per_pixel = 6;
538 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
539 bytes_per_pixel = 8;
540 else
541 return;
542
543 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
544 {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500545 png_uint_32 s0 = (*(rp ) << 8) | *(rp+1);
546 png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3);
547 png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500548 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
549 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500550 *(rp ) = (png_byte)((red >> 8) & 0xff);
551 *(rp+1) = (png_byte)(red & 0xff);
552 *(rp+4) = (png_byte)((blue >> 8) & 0xff);
553 *(rp+5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600554 }
555 }
556 }
557}
558#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500559#endif /* PNG_WRITE_SUPPORTED */