blob: 3070f65277b52a3bcc3b45c9d6954cb215c57b6d [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-Pehrsonda009802009-08-15 13:25:47 -05004 * Last changed in libpng 1.4.0 [August 15, 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 *
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
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");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050093
Andreas Dilger47a0c421997-05-16 02:46:07 -050094 if (row_info->bit_depth == 8 &&
Guy Schalnat0d580581995-07-20 02:43:20 -050095 row_info->channels == 1)
96 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050097 switch ((int)bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -050098 {
99 case 1:
100 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101 png_bytep sp, dp;
102 int mask, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500103 png_uint_32 i;
104 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500105
106 sp = row;
107 dp = row;
108 mask = 0x80;
109 v = 0;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600110
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500111 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500112 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500113 if (*sp != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500114 v |= mask;
115 sp++;
116 if (mask > 1)
117 mask >>= 1;
118 else
119 {
120 mask = 0x80;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600121 *dp = (png_byte)v;
122 dp++;
Guy Schalnat0d580581995-07-20 02:43:20 -0500123 v = 0;
124 }
125 }
126 if (mask != 0x80)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600127 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500128 break;
129 }
130 case 2:
131 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500132 png_bytep sp, dp;
133 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500134 png_uint_32 i;
135 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500136
137 sp = row;
138 dp = row;
139 shift = 6;
140 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500141 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500142 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500143 png_byte value;
144
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600145 value = (png_byte)(*sp & 0x03);
Guy Schalnat0d580581995-07-20 02:43:20 -0500146 v |= (value << shift);
147 if (shift == 0)
148 {
149 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600150 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500151 dp++;
152 v = 0;
153 }
154 else
155 shift -= 2;
156 sp++;
157 }
158 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600159 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500160 break;
161 }
162 case 4:
163 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500164 png_bytep sp, dp;
165 int shift, v;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500166 png_uint_32 i;
167 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500168
169 sp = row;
170 dp = row;
171 shift = 4;
172 v = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500173 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500174 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500175 png_byte value;
176
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600177 value = (png_byte)(*sp & 0x0f);
Guy Schalnat0d580581995-07-20 02:43:20 -0500178 v |= (value << shift);
179
180 if (shift == 0)
181 {
182 shift = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600183 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500184 dp++;
185 v = 0;
186 }
187 else
188 shift -= 4;
189
190 sp++;
191 }
192 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600193 *dp = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -0500194 break;
195 }
196 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197 row_info->bit_depth = (png_byte)bit_depth;
198 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500199 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
200 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500201 }
202}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500203#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500204
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500205#if defined(PNG_WRITE_SHIFT_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600206/* Shift pixel values to take advantage of whole range. Pass the
207 * true number of bits in bit_depth. The row should be packed
208 * according to row_info->bit_depth. Thus, if you had a row of
209 * bit depth 4, but the pixels only had values from 0 to 7, you
210 * would pass 3 as bit_depth, and this routine would translate the
211 * data to 0 to 15.
212 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500213void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600214png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -0500215{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500216 png_debug(1, "in png_do_shift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500217
Andreas Dilger47a0c421997-05-16 02:46:07 -0500218 if (
Guy Schalnat0d580581995-07-20 02:43:20 -0500219 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
220 {
221 int shift_start[4], shift_dec[4];
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500222 int channels = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -0500223
Guy Schalnat0d580581995-07-20 02:43:20 -0500224 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
225 {
226 shift_start[channels] = row_info->bit_depth - bit_depth->red;
227 shift_dec[channels] = bit_depth->red;
228 channels++;
229 shift_start[channels] = row_info->bit_depth - bit_depth->green;
230 shift_dec[channels] = bit_depth->green;
231 channels++;
232 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
233 shift_dec[channels] = bit_depth->blue;
234 channels++;
235 }
236 else
237 {
238 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
239 shift_dec[channels] = bit_depth->gray;
240 channels++;
241 }
242 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
243 {
244 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
245 shift_dec[channels] = bit_depth->alpha;
246 channels++;
247 }
248
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500249 /* With low row depths, could only be grayscale, so one channel */
Guy Schalnat0d580581995-07-20 02:43:20 -0500250 if (row_info->bit_depth < 8)
251 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500252 png_bytep bp = row;
253 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500254 png_byte mask;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500255 png_uint_32 row_bytes = row_info->rowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500256
257 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
258 mask = 0x55;
259 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
260 mask = 0x11;
261 else
262 mask = 0xff;
263
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500264 for (i = 0; i < row_bytes; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500265 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500266 png_uint_16 v;
267 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500268
269 v = *bp;
270 *bp = 0;
271 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
272 {
273 if (j > 0)
274 *bp |= (png_byte)((v << j) & 0xff);
275 else
276 *bp |= (png_byte)((v >> (-j)) & mask);
277 }
278 }
279 }
280 else if (row_info->bit_depth == 8)
281 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500282 png_bytep bp = row;
283 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500284 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500285
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500286 for (i = 0; i < istop; i++, bp++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500287 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500288
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500289 png_uint_16 v;
290 int j;
291 int c = (int)(i%channels);
292
293 v = *bp;
294 *bp = 0;
295 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500296 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500297 if (j > 0)
298 *bp |= (png_byte)((v << j) & 0xff);
299 else
300 *bp |= (png_byte)((v >> (-j)) & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500301 }
302 }
303 }
304 else
305 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600306 png_bytep bp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500307 png_uint_32 i;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500308 png_uint_32 istop = channels * row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -0500309
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500310 for (bp = row, i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500311 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500312 int c = (int)(i%channels);
313 png_uint_16 value, v;
314 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500315
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500316 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500317 value = 0;
318 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
Guy Schalnat0d580581995-07-20 02:43:20 -0500319 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500320 if (j > 0)
321 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
322 else
323 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500324 }
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500325 *bp++ = (png_byte)(value >> 8);
326 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -0500327 }
328 }
329 }
330}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500331#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500332
Andreas Dilger47a0c421997-05-16 02:46:07 -0500333#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500334void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500335png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500336{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500337 png_debug(1, "in png_do_write_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500338
Guy Schalnat0d580581995-07-20 02:43:20 -0500339 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500340 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -0500341 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500342 /* This converts from ARGB to RGBA */
343 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500344 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500345 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500346 png_uint_32 i;
347 png_uint_32 row_width = row_info->width;
348 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500349 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500350 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500351 *(dp++) = *(sp++);
352 *(dp++) = *(sp++);
353 *(dp++) = *(sp++);
354 *(dp++) = save;
355 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500356 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500357 /* This converts from AARRGGBB to RRGGBBAA */
358 else
359 {
360 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500361 png_uint_32 i;
362 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500364 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500365 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500366 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500367 save[0] = *(sp++);
368 save[1] = *(sp++);
369 *(dp++) = *(sp++);
370 *(dp++) = *(sp++);
371 *(dp++) = *(sp++);
372 *(dp++) = *(sp++);
373 *(dp++) = *(sp++);
374 *(dp++) = *(sp++);
375 *(dp++) = save[0];
376 *(dp++) = save[1];
377 }
378 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500379 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500380 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500381 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 /* This converts from AG to GA */
383 if (row_info->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500384 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500387 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500389 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500390 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500391 png_byte save = *(sp++);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392 *(dp++) = *(sp++);
393 *(dp++) = save;
394 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500395 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500396 /* This converts from AAGG to GGAA */
397 else
398 {
399 png_bytep sp, dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500400 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500401 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500403 for (i = 0, sp = dp = row; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500404 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500405 png_byte save[2];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500406 save[0] = *(sp++);
407 save[1] = *(sp++);
408 *(dp++) = *(sp++);
409 *(dp++) = *(sp++);
410 *(dp++) = save[0];
411 *(dp++) = save[1];
412 }
413 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500414 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500415 }
416}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500417#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500418
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600419#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500420void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600421png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
422{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500423 png_debug(1, "in png_do_write_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500424
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600425 {
426 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
427 {
428 /* This inverts the alpha channel in RGBA */
429 if (row_info->bit_depth == 8)
430 {
431 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500432 png_uint_32 i;
433 png_uint_32 row_width = row_info->width;
434 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600435 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500436 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600437 *(dp++) = *(sp++);
438 *(dp++) = *(sp++);
439 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600440 */
441 sp+=3; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500442 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600443 }
444 }
445 /* This inverts the alpha channel in RRGGBBAA */
446 else
447 {
448 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500449 png_uint_32 i;
450 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600451
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500452 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600453 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500454 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600455 *(dp++) = *(sp++);
456 *(dp++) = *(sp++);
457 *(dp++) = *(sp++);
458 *(dp++) = *(sp++);
459 *(dp++) = *(sp++);
460 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600461 */
462 sp+=6; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500463 *(dp++) = (png_byte)(255 - *(sp++));
464 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600465 }
466 }
467 }
468 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
469 {
470 /* This inverts the alpha channel in GA */
471 if (row_info->bit_depth == 8)
472 {
473 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500474 png_uint_32 i;
475 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600476
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500477 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600478 {
479 *(dp++) = *(sp++);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500480 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600481 }
482 }
483 /* This inverts the alpha channel in GGAA */
484 else
485 {
486 png_bytep sp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500487 png_uint_32 i;
488 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600489
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500490 for (i = 0, sp = dp = row; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600491 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500492 /* Does nothing
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600493 *(dp++) = *(sp++);
494 *(dp++) = *(sp++);
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600495 */
496 sp+=2; dp = sp;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500497 *(dp++) = (png_byte)(255 - *(sp++));
498 *(dp++) = (png_byte)(255 - *(sp++));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600499 }
500 }
501 }
502 }
503}
504#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600505
506#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500507/* Undoes intrapixel differencing */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600508void /* PRIVATE */
509png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
510{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500511 png_debug(1, "in png_do_write_intrapixel");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500512
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600513 if (
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600514 (row_info->color_type & PNG_COLOR_MASK_COLOR))
515 {
516 int bytes_per_pixel;
517 png_uint_32 row_width = row_info->width;
518 if (row_info->bit_depth == 8)
519 {
520 png_bytep rp;
521 png_uint_32 i;
522
523 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
524 bytes_per_pixel = 3;
525 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
526 bytes_per_pixel = 4;
527 else
528 return;
529
530 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
531 {
532 *(rp) = (png_byte)((*rp - *(rp+1))&0xff);
533 *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
534 }
535 }
536 else if (row_info->bit_depth == 16)
537 {
538 png_bytep rp;
539 png_uint_32 i;
540
541 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
542 bytes_per_pixel = 6;
543 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
544 bytes_per_pixel = 8;
545 else
546 return;
547
548 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
549 {
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500550 png_uint_32 s0 = (*(rp ) << 8) | *(rp+1);
551 png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3);
552 png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500553 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
554 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500555 *(rp ) = (png_byte)((red >> 8) & 0xff);
556 *(rp+1) = (png_byte)(red & 0xff);
557 *(rp+4) = (png_byte)((blue >> 8) & 0xff);
558 *(rp+5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600559 }
560 }
561 }
562}
563#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500564#endif /* PNG_WRITE_SUPPORTED */