Bumping manifests a=b2g-bump
[gecko.git] / media / libpng / pngwtran.c
blob09562a787d0c957ba1fbcf4303d5c2fc78593221
2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * Last changed in libpng 1.6.15 [November 20, 2014]
5 * Copyright (c) 1998-2014 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 #include "pngpriv.h"
16 #ifdef PNG_WRITE_SUPPORTED
17 #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
19 #ifdef PNG_WRITE_PACK_SUPPORTED
20 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
21 * row_info bit depth should be 8 (one pixel per byte). The channels
22 * should be 1 (this only happens on grayscale and paletted images).
24 static void
25 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
27 png_debug(1, "in png_do_pack");
29 if (row_info->bit_depth == 8 &&
30 row_info->channels == 1)
32 switch ((int)bit_depth)
34 case 1:
36 png_bytep sp, dp;
37 int mask, v;
38 png_uint_32 i;
39 png_uint_32 row_width = row_info->width;
41 sp = row;
42 dp = row;
43 mask = 0x80;
44 v = 0;
46 for (i = 0; i < row_width; i++)
48 if (*sp != 0)
49 v |= mask;
51 sp++;
53 if (mask > 1)
54 mask >>= 1;
56 else
58 mask = 0x80;
59 *dp = (png_byte)v;
60 dp++;
61 v = 0;
65 if (mask != 0x80)
66 *dp = (png_byte)v;
68 break;
71 case 2:
73 png_bytep sp, dp;
74 int shift, v;
75 png_uint_32 i;
76 png_uint_32 row_width = row_info->width;
78 sp = row;
79 dp = row;
80 shift = 6;
81 v = 0;
83 for (i = 0; i < row_width; i++)
85 png_byte value;
87 value = (png_byte)(*sp & 0x03);
88 v |= (value << shift);
90 if (shift == 0)
92 shift = 6;
93 *dp = (png_byte)v;
94 dp++;
95 v = 0;
98 else
99 shift -= 2;
101 sp++;
104 if (shift != 6)
105 *dp = (png_byte)v;
107 break;
110 case 4:
112 png_bytep sp, dp;
113 int shift, v;
114 png_uint_32 i;
115 png_uint_32 row_width = row_info->width;
117 sp = row;
118 dp = row;
119 shift = 4;
120 v = 0;
122 for (i = 0; i < row_width; i++)
124 png_byte value;
126 value = (png_byte)(*sp & 0x0f);
127 v |= (value << shift);
129 if (shift == 0)
131 shift = 4;
132 *dp = (png_byte)v;
133 dp++;
134 v = 0;
137 else
138 shift -= 4;
140 sp++;
143 if (shift != 4)
144 *dp = (png_byte)v;
146 break;
149 default:
150 break;
153 row_info->bit_depth = (png_byte)bit_depth;
154 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
155 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
156 row_info->width);
159 #endif
161 #ifdef PNG_WRITE_SHIFT_SUPPORTED
162 /* Shift pixel values to take advantage of whole range. Pass the
163 * true number of bits in bit_depth. The row should be packed
164 * according to row_info->bit_depth. Thus, if you had a row of
165 * bit depth 4, but the pixels only had values from 0 to 7, you
166 * would pass 3 as bit_depth, and this routine would translate the
167 * data to 0 to 15.
169 static void
170 png_do_shift(png_row_infop row_info, png_bytep row,
171 png_const_color_8p bit_depth)
173 png_debug(1, "in png_do_shift");
175 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
177 int shift_start[4], shift_dec[4];
178 int channels = 0;
180 if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
182 shift_start[channels] = row_info->bit_depth - bit_depth->red;
183 shift_dec[channels] = bit_depth->red;
184 channels++;
186 shift_start[channels] = row_info->bit_depth - bit_depth->green;
187 shift_dec[channels] = bit_depth->green;
188 channels++;
190 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
191 shift_dec[channels] = bit_depth->blue;
192 channels++;
195 else
197 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
198 shift_dec[channels] = bit_depth->gray;
199 channels++;
202 if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
204 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
205 shift_dec[channels] = bit_depth->alpha;
206 channels++;
209 /* With low row depths, could only be grayscale, so one channel */
210 if (row_info->bit_depth < 8)
212 png_bytep bp = row;
213 png_size_t i;
214 unsigned int mask;
215 png_size_t row_bytes = row_info->rowbytes;
217 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
218 mask = 0x55;
220 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
221 mask = 0x11;
223 else
224 mask = 0xff;
226 for (i = 0; i < row_bytes; i++, bp++)
228 int j;
229 unsigned int v, out;
231 v = *bp;
232 out = 0;
234 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
236 if (j > 0)
237 out |= v << j;
239 else
240 out |= (v >> (-j)) & mask;
243 *bp = (png_byte)(out & 0xff);
247 else if (row_info->bit_depth == 8)
249 png_bytep bp = row;
250 png_uint_32 i;
251 png_uint_32 istop = channels * row_info->width;
253 for (i = 0; i < istop; i++, bp++)
256 const unsigned int c = i%channels;
257 int j;
258 unsigned int v, out;
260 v = *bp;
261 out = 0;
263 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
265 if (j > 0)
266 out |= v << j;
268 else
269 out |= v >> (-j);
272 *bp = (png_byte)(out & 0xff);
276 else
278 png_bytep bp;
279 png_uint_32 i;
280 png_uint_32 istop = channels * row_info->width;
282 for (bp = row, i = 0; i < istop; i++)
284 const unsigned int c = i%channels;
285 int j;
286 unsigned int value, v;
288 v = png_get_uint_16(bp);
289 value = 0;
291 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
293 if (j > 0)
294 value |= v << j;
296 else
297 value |= v >> (-j);
299 *bp++ = (png_byte)((value >> 8) & 0xff);
300 *bp++ = (png_byte)(value & 0xff);
305 #endif
307 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
308 static void
309 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
311 png_debug(1, "in png_do_write_swap_alpha");
314 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
316 if (row_info->bit_depth == 8)
318 /* This converts from ARGB to RGBA */
319 png_bytep sp, dp;
320 png_uint_32 i;
321 png_uint_32 row_width = row_info->width;
323 for (i = 0, sp = dp = row; i < row_width; i++)
325 png_byte save = *(sp++);
326 *(dp++) = *(sp++);
327 *(dp++) = *(sp++);
328 *(dp++) = *(sp++);
329 *(dp++) = save;
333 #ifdef PNG_WRITE_16BIT_SUPPORTED
334 else
336 /* This converts from AARRGGBB to RRGGBBAA */
337 png_bytep sp, dp;
338 png_uint_32 i;
339 png_uint_32 row_width = row_info->width;
341 for (i = 0, sp = dp = row; i < row_width; i++)
343 png_byte save[2];
344 save[0] = *(sp++);
345 save[1] = *(sp++);
346 *(dp++) = *(sp++);
347 *(dp++) = *(sp++);
348 *(dp++) = *(sp++);
349 *(dp++) = *(sp++);
350 *(dp++) = *(sp++);
351 *(dp++) = *(sp++);
352 *(dp++) = save[0];
353 *(dp++) = save[1];
356 #endif /* WRITE_16BIT */
359 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
361 if (row_info->bit_depth == 8)
363 /* This converts from AG to GA */
364 png_bytep sp, dp;
365 png_uint_32 i;
366 png_uint_32 row_width = row_info->width;
368 for (i = 0, sp = dp = row; i < row_width; i++)
370 png_byte save = *(sp++);
371 *(dp++) = *(sp++);
372 *(dp++) = save;
376 #ifdef PNG_WRITE_16BIT_SUPPORTED
377 else
379 /* This converts from AAGG to GGAA */
380 png_bytep sp, dp;
381 png_uint_32 i;
382 png_uint_32 row_width = row_info->width;
384 for (i = 0, sp = dp = row; i < row_width; i++)
386 png_byte save[2];
387 save[0] = *(sp++);
388 save[1] = *(sp++);
389 *(dp++) = *(sp++);
390 *(dp++) = *(sp++);
391 *(dp++) = save[0];
392 *(dp++) = save[1];
395 #endif /* WRITE_16BIT */
399 #endif
401 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
402 static void
403 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
405 png_debug(1, "in png_do_write_invert_alpha");
408 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
410 if (row_info->bit_depth == 8)
412 /* This inverts the alpha channel in RGBA */
413 png_bytep sp, dp;
414 png_uint_32 i;
415 png_uint_32 row_width = row_info->width;
417 for (i = 0, sp = dp = row; i < row_width; i++)
419 /* Does nothing
420 *(dp++) = *(sp++);
421 *(dp++) = *(sp++);
422 *(dp++) = *(sp++);
424 sp+=3; dp = sp;
425 *(dp++) = (png_byte)(255 - *(sp++));
429 #ifdef PNG_WRITE_16BIT_SUPPORTED
430 else
432 /* This inverts the alpha channel in RRGGBBAA */
433 png_bytep sp, dp;
434 png_uint_32 i;
435 png_uint_32 row_width = row_info->width;
437 for (i = 0, sp = dp = row; i < row_width; i++)
439 /* Does nothing
440 *(dp++) = *(sp++);
441 *(dp++) = *(sp++);
442 *(dp++) = *(sp++);
443 *(dp++) = *(sp++);
444 *(dp++) = *(sp++);
445 *(dp++) = *(sp++);
447 sp+=6; dp = sp;
448 *(dp++) = (png_byte)(255 - *(sp++));
449 *(dp++) = (png_byte)(255 - *(sp++));
452 #endif /* WRITE_16BIT */
455 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
457 if (row_info->bit_depth == 8)
459 /* This inverts the alpha channel in GA */
460 png_bytep sp, dp;
461 png_uint_32 i;
462 png_uint_32 row_width = row_info->width;
464 for (i = 0, sp = dp = row; i < row_width; i++)
466 *(dp++) = *(sp++);
467 *(dp++) = (png_byte)(255 - *(sp++));
471 #ifdef PNG_WRITE_16BIT_SUPPORTED
472 else
474 /* This inverts the alpha channel in GGAA */
475 png_bytep sp, dp;
476 png_uint_32 i;
477 png_uint_32 row_width = row_info->width;
479 for (i = 0, sp = dp = row; i < row_width; i++)
481 /* Does nothing
482 *(dp++) = *(sp++);
483 *(dp++) = *(sp++);
485 sp+=2; dp = sp;
486 *(dp++) = (png_byte)(255 - *(sp++));
487 *(dp++) = (png_byte)(255 - *(sp++));
490 #endif /* WRITE_16BIT */
494 #endif
496 /* Transform the data according to the user's wishes. The order of
497 * transformations is significant.
499 void /* PRIVATE */
500 png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
502 png_debug(1, "in png_do_write_transformations");
504 if (png_ptr == NULL)
505 return;
507 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
508 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
509 if (png_ptr->write_user_transform_fn != NULL)
510 (*(png_ptr->write_user_transform_fn)) /* User write transform
511 function */
512 (png_ptr, /* png_ptr */
513 row_info, /* row_info: */
514 /* png_uint_32 width; width of row */
515 /* png_size_t rowbytes; number of bytes in row */
516 /* png_byte color_type; color type of pixels */
517 /* png_byte bit_depth; bit depth of samples */
518 /* png_byte channels; number of channels (1-4) */
519 /* png_byte pixel_depth; bits per pixel (depth*channels) */
520 png_ptr->row_buf + 1); /* start of pixel data for row */
521 #endif
523 #ifdef PNG_WRITE_FILLER_SUPPORTED
524 if ((png_ptr->transformations & PNG_FILLER) != 0)
525 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
526 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
527 #endif
529 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
530 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
531 png_do_packswap(row_info, png_ptr->row_buf + 1);
532 #endif
534 #ifdef PNG_WRITE_PACK_SUPPORTED
535 if ((png_ptr->transformations & PNG_PACK) != 0)
536 png_do_pack(row_info, png_ptr->row_buf + 1,
537 (png_uint_32)png_ptr->bit_depth);
538 #endif
540 #ifdef PNG_WRITE_SWAP_SUPPORTED
541 # ifdef PNG_16BIT_SUPPORTED
542 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
543 png_do_swap(row_info, png_ptr->row_buf + 1);
544 # endif
545 #endif
547 #ifdef PNG_WRITE_SHIFT_SUPPORTED
548 if ((png_ptr->transformations & PNG_SHIFT) != 0)
549 png_do_shift(row_info, png_ptr->row_buf + 1,
550 &(png_ptr->shift));
551 #endif
553 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
554 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
555 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
556 #endif
558 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
559 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
560 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
561 #endif
563 #ifdef PNG_WRITE_BGR_SUPPORTED
564 if ((png_ptr->transformations & PNG_BGR) != 0)
565 png_do_bgr(row_info, png_ptr->row_buf + 1);
566 #endif
568 #ifdef PNG_WRITE_INVERT_SUPPORTED
569 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
570 png_do_invert(row_info, png_ptr->row_buf + 1);
571 #endif
573 #endif /* WRITE_TRANSFORMS */
574 #endif /* WRITE */