Init uninitialized member in ctor.
[chromium-blink-merge.git] / third_party / libpng / pngwtran.c
blob0ce9b9b501ea663c0c9b34f887e6ff17ff6a050c
2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * Last changed in libpng 1.2.43 [February 25, 2010]
5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
14 #define PNG_INTERNAL
15 #define PNG_NO_PEDANTIC_WARNINGS
16 #include "png.h"
17 #ifdef PNG_WRITE_SUPPORTED
19 /* Transform the data according to the user's wishes. The order of
20 * transformations is significant.
22 void /* PRIVATE */
23 png_do_write_transformations(png_structp png_ptr)
25 png_debug(1, "in png_do_write_transformations");
27 if (png_ptr == NULL)
28 return;
30 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
31 if (png_ptr->transformations & PNG_USER_TRANSFORM)
32 if (png_ptr->write_user_transform_fn != NULL)
33 (*(png_ptr->write_user_transform_fn)) /* User write transform
34 function */
35 (png_ptr, /* png_ptr */
36 &(png_ptr->row_info), /* row_info: */
37 /* png_uint_32 width; width of row */
38 /* png_uint_32 rowbytes; number of bytes in row */
39 /* png_byte color_type; color type of pixels */
40 /* png_byte bit_depth; bit depth of samples */
41 /* png_byte channels; number of channels (1-4) */
42 /* png_byte pixel_depth; bits per pixel (depth*channels) */
43 png_ptr->row_buf + 1); /* start of pixel data for row */
44 #endif
45 #ifdef PNG_WRITE_FILLER_SUPPORTED
46 if (png_ptr->transformations & PNG_FILLER)
47 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
48 png_ptr->flags);
49 #endif
50 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
51 if (png_ptr->transformations & PNG_PACKSWAP)
52 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
53 #endif
54 #ifdef PNG_WRITE_PACK_SUPPORTED
55 if (png_ptr->transformations & PNG_PACK)
56 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
57 (png_uint_32)png_ptr->bit_depth);
58 #endif
59 #ifdef PNG_WRITE_SWAP_SUPPORTED
60 if (png_ptr->transformations & PNG_SWAP_BYTES)
61 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
62 #endif
63 #ifdef PNG_WRITE_SHIFT_SUPPORTED
64 if (png_ptr->transformations & PNG_SHIFT)
65 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
66 &(png_ptr->shift));
67 #endif
68 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
69 if (png_ptr->transformations & PNG_SWAP_ALPHA)
70 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
71 #endif
72 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
73 if (png_ptr->transformations & PNG_INVERT_ALPHA)
74 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
75 #endif
76 #ifdef PNG_WRITE_BGR_SUPPORTED
77 if (png_ptr->transformations & PNG_BGR)
78 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
79 #endif
80 #ifdef PNG_WRITE_INVERT_SUPPORTED
81 if (png_ptr->transformations & PNG_INVERT_MONO)
82 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
83 #endif
86 #ifdef PNG_WRITE_PACK_SUPPORTED
87 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
88 * row_info bit depth should be 8 (one pixel per byte). The channels
89 * should be 1 (this only happens on grayscale and paletted images).
91 void /* PRIVATE */
92 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
94 png_debug(1, "in png_do_pack");
96 if (row_info->bit_depth == 8 &&
97 #ifdef PNG_USELESS_TESTS_SUPPORTED
98 row != NULL && row_info != NULL &&
99 #endif
100 row_info->channels == 1)
102 switch ((int)bit_depth)
104 case 1:
106 png_bytep sp, dp;
107 int mask, v;
108 png_uint_32 i;
109 png_uint_32 row_width = row_info->width;
111 sp = row;
112 dp = row;
113 mask = 0x80;
114 v = 0;
116 for (i = 0; i < row_width; i++)
118 if (*sp != 0)
119 v |= mask;
120 sp++;
121 if (mask > 1)
122 mask >>= 1;
123 else
125 mask = 0x80;
126 *dp = (png_byte)v;
127 dp++;
128 v = 0;
131 if (mask != 0x80)
132 *dp = (png_byte)v;
133 break;
135 case 2:
137 png_bytep sp, dp;
138 int shift, v;
139 png_uint_32 i;
140 png_uint_32 row_width = row_info->width;
142 sp = row;
143 dp = row;
144 shift = 6;
145 v = 0;
146 for (i = 0; i < row_width; i++)
148 png_byte value;
150 value = (png_byte)(*sp & 0x03);
151 v |= (value << shift);
152 if (shift == 0)
154 shift = 6;
155 *dp = (png_byte)v;
156 dp++;
157 v = 0;
159 else
160 shift -= 2;
161 sp++;
163 if (shift != 6)
164 *dp = (png_byte)v;
165 break;
167 case 4:
169 png_bytep sp, dp;
170 int shift, v;
171 png_uint_32 i;
172 png_uint_32 row_width = row_info->width;
174 sp = row;
175 dp = row;
176 shift = 4;
177 v = 0;
178 for (i = 0; i < row_width; i++)
180 png_byte value;
182 value = (png_byte)(*sp & 0x0f);
183 v |= (value << shift);
185 if (shift == 0)
187 shift = 4;
188 *dp = (png_byte)v;
189 dp++;
190 v = 0;
192 else
193 shift -= 4;
195 sp++;
197 if (shift != 4)
198 *dp = (png_byte)v;
199 break;
202 row_info->bit_depth = (png_byte)bit_depth;
203 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
204 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
205 row_info->width);
208 #endif
210 #ifdef PNG_WRITE_SHIFT_SUPPORTED
211 /* Shift pixel values to take advantage of whole range. Pass the
212 * true number of bits in bit_depth. The row should be packed
213 * according to row_info->bit_depth. Thus, if you had a row of
214 * bit depth 4, but the pixels only had values from 0 to 7, you
215 * would pass 3 as bit_depth, and this routine would translate the
216 * data to 0 to 15.
218 void /* PRIVATE */
219 png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
221 png_debug(1, "in png_do_shift");
223 #ifdef PNG_USELESS_TESTS_SUPPORTED
224 if (row != NULL && row_info != NULL &&
225 #else
226 if (
227 #endif
228 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
230 int shift_start[4], shift_dec[4];
231 int channels = 0;
233 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
235 shift_start[channels] = row_info->bit_depth - bit_depth->red;
236 shift_dec[channels] = bit_depth->red;
237 channels++;
238 shift_start[channels] = row_info->bit_depth - bit_depth->green;
239 shift_dec[channels] = bit_depth->green;
240 channels++;
241 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
242 shift_dec[channels] = bit_depth->blue;
243 channels++;
245 else
247 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
248 shift_dec[channels] = bit_depth->gray;
249 channels++;
251 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
253 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
254 shift_dec[channels] = bit_depth->alpha;
255 channels++;
258 /* With low row depths, could only be grayscale, so one channel */
259 if (row_info->bit_depth < 8)
261 png_bytep bp = row;
262 png_uint_32 i;
263 png_byte mask;
264 png_uint_32 row_bytes = row_info->rowbytes;
266 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
267 mask = 0x55;
268 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
269 mask = 0x11;
270 else
271 mask = 0xff;
273 for (i = 0; i < row_bytes; i++, bp++)
275 png_uint_16 v;
276 int j;
278 v = *bp;
279 *bp = 0;
280 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
282 if (j > 0)
283 *bp |= (png_byte)((v << j) & 0xff);
284 else
285 *bp |= (png_byte)((v >> (-j)) & mask);
289 else if (row_info->bit_depth == 8)
291 png_bytep bp = row;
292 png_uint_32 i;
293 png_uint_32 istop = channels * row_info->width;
295 for (i = 0; i < istop; i++, bp++)
298 png_uint_16 v;
299 int j;
300 int c = (int)(i%channels);
302 v = *bp;
303 *bp = 0;
304 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
306 if (j > 0)
307 *bp |= (png_byte)((v << j) & 0xff);
308 else
309 *bp |= (png_byte)((v >> (-j)) & 0xff);
313 else
315 png_bytep bp;
316 png_uint_32 i;
317 png_uint_32 istop = channels * row_info->width;
319 for (bp = row, i = 0; i < istop; i++)
321 int c = (int)(i%channels);
322 png_uint_16 value, v;
323 int j;
325 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
326 value = 0;
327 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
329 if (j > 0)
330 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
331 else
332 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
334 *bp++ = (png_byte)(value >> 8);
335 *bp++ = (png_byte)(value & 0xff);
340 #endif
342 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
343 void /* PRIVATE */
344 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
346 png_debug(1, "in png_do_write_swap_alpha");
348 #ifdef PNG_USELESS_TESTS_SUPPORTED
349 if (row != NULL && row_info != NULL)
350 #endif
352 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
354 /* This converts from ARGB to RGBA */
355 if (row_info->bit_depth == 8)
357 png_bytep sp, dp;
358 png_uint_32 i;
359 png_uint_32 row_width = row_info->width;
360 for (i = 0, sp = dp = row; i < row_width; i++)
362 png_byte save = *(sp++);
363 *(dp++) = *(sp++);
364 *(dp++) = *(sp++);
365 *(dp++) = *(sp++);
366 *(dp++) = save;
369 /* This converts from AARRGGBB to RRGGBBAA */
370 else
372 png_bytep sp, dp;
373 png_uint_32 i;
374 png_uint_32 row_width = row_info->width;
376 for (i = 0, sp = dp = row; i < row_width; i++)
378 png_byte save[2];
379 save[0] = *(sp++);
380 save[1] = *(sp++);
381 *(dp++) = *(sp++);
382 *(dp++) = *(sp++);
383 *(dp++) = *(sp++);
384 *(dp++) = *(sp++);
385 *(dp++) = *(sp++);
386 *(dp++) = *(sp++);
387 *(dp++) = save[0];
388 *(dp++) = save[1];
392 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
394 /* This converts from AG to GA */
395 if (row_info->bit_depth == 8)
397 png_bytep sp, dp;
398 png_uint_32 i;
399 png_uint_32 row_width = row_info->width;
401 for (i = 0, sp = dp = row; i < row_width; i++)
403 png_byte save = *(sp++);
404 *(dp++) = *(sp++);
405 *(dp++) = save;
408 /* This converts from AAGG to GGAA */
409 else
411 png_bytep sp, dp;
412 png_uint_32 i;
413 png_uint_32 row_width = row_info->width;
415 for (i = 0, sp = dp = row; i < row_width; i++)
417 png_byte save[2];
418 save[0] = *(sp++);
419 save[1] = *(sp++);
420 *(dp++) = *(sp++);
421 *(dp++) = *(sp++);
422 *(dp++) = save[0];
423 *(dp++) = save[1];
429 #endif
431 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
432 void /* PRIVATE */
433 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
435 png_debug(1, "in png_do_write_invert_alpha");
437 #ifdef PNG_USELESS_TESTS_SUPPORTED
438 if (row != NULL && row_info != NULL)
439 #endif
441 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
443 /* This inverts the alpha channel in RGBA */
444 if (row_info->bit_depth == 8)
446 png_bytep sp, dp;
447 png_uint_32 i;
448 png_uint_32 row_width = row_info->width;
449 for (i = 0, sp = dp = row; i < row_width; i++)
451 /* Does nothing
452 *(dp++) = *(sp++);
453 *(dp++) = *(sp++);
454 *(dp++) = *(sp++);
456 sp+=3; dp = sp;
457 *(dp++) = (png_byte)(255 - *(sp++));
460 /* This inverts the alpha channel in RRGGBBAA */
461 else
463 png_bytep sp, dp;
464 png_uint_32 i;
465 png_uint_32 row_width = row_info->width;
467 for (i = 0, sp = dp = row; i < row_width; i++)
469 /* Does nothing
470 *(dp++) = *(sp++);
471 *(dp++) = *(sp++);
472 *(dp++) = *(sp++);
473 *(dp++) = *(sp++);
474 *(dp++) = *(sp++);
475 *(dp++) = *(sp++);
477 sp+=6; dp = sp;
478 *(dp++) = (png_byte)(255 - *(sp++));
479 *(dp++) = (png_byte)(255 - *(sp++));
483 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
485 /* This inverts the alpha channel in GA */
486 if (row_info->bit_depth == 8)
488 png_bytep sp, dp;
489 png_uint_32 i;
490 png_uint_32 row_width = row_info->width;
492 for (i = 0, sp = dp = row; i < row_width; i++)
494 *(dp++) = *(sp++);
495 *(dp++) = (png_byte)(255 - *(sp++));
498 /* This inverts the alpha channel in GGAA */
499 else
501 png_bytep sp, dp;
502 png_uint_32 i;
503 png_uint_32 row_width = row_info->width;
505 for (i = 0, sp = dp = row; i < row_width; i++)
507 /* Does nothing
508 *(dp++) = *(sp++);
509 *(dp++) = *(sp++);
511 sp+=2; dp = sp;
512 *(dp++) = (png_byte)(255 - *(sp++));
513 *(dp++) = (png_byte)(255 - *(sp++));
519 #endif
521 #ifdef PNG_MNG_FEATURES_SUPPORTED
522 /* Undoes intrapixel differencing */
523 void /* PRIVATE */
524 png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
526 png_debug(1, "in png_do_write_intrapixel");
528 if (
529 #ifdef PNG_USELESS_TESTS_SUPPORTED
530 row != NULL && row_info != NULL &&
531 #endif
532 (row_info->color_type & PNG_COLOR_MASK_COLOR))
534 int bytes_per_pixel;
535 png_uint_32 row_width = row_info->width;
536 if (row_info->bit_depth == 8)
538 png_bytep rp;
539 png_uint_32 i;
541 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
542 bytes_per_pixel = 3;
543 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
544 bytes_per_pixel = 4;
545 else
546 return;
548 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
550 *(rp) = (png_byte)((*rp - *(rp+1))&0xff);
551 *(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
554 else if (row_info->bit_depth == 16)
556 png_bytep rp;
557 png_uint_32 i;
559 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
560 bytes_per_pixel = 6;
561 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
562 bytes_per_pixel = 8;
563 else
564 return;
566 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
568 png_uint_32 s0 = (*(rp ) << 8) | *(rp+1);
569 png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3);
570 png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5);
571 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
572 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
573 *(rp ) = (png_byte)((red >> 8) & 0xff);
574 *(rp+1) = (png_byte)(red & 0xff);
575 *(rp+4) = (png_byte)((blue >> 8) & 0xff);
576 *(rp+5) = (png_byte)(blue & 0xff);
581 #endif /* PNG_MNG_FEATURES_SUPPORTED */
582 #endif /* PNG_WRITE_SUPPORTED */