(metux) configure.ac: removed broken and obsolete AC_C_CONST
[mirror-ossqm-libpng.git] / pngwtran.c
blob115ca37212dda046c266c0ee825f4674ecc19557
2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * Last changed in libpng 1.5.0 [January 6, 2011]
5 * Copyright (c) 1998-2011 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
18 /* Transform the data according to the user's wishes. The order of
19 * transformations is significant.
21 void /* PRIVATE */
22 png_do_write_transformations(png_structp png_ptr)
24 png_debug(1, "in png_do_write_transformations");
26 if (png_ptr == NULL)
27 return;
29 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
30 if (png_ptr->transformations & PNG_USER_TRANSFORM)
31 if (png_ptr->write_user_transform_fn != NULL)
32 (*(png_ptr->write_user_transform_fn)) /* User write transform
33 function */
34 (png_ptr, /* png_ptr */
35 &(png_ptr->row_info), /* row_info: */
36 /* png_uint_32 width; width of row */
37 /* png_size_t rowbytes; number of bytes in row */
38 /* png_byte color_type; color type of pixels */
39 /* png_byte bit_depth; bit depth of samples */
40 /* png_byte channels; number of channels (1-4) */
41 /* png_byte pixel_depth; bits per pixel (depth*channels) */
42 png_ptr->row_buf + 1); /* start of pixel data for row */
43 #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
51 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
52 if (png_ptr->transformations & PNG_PACKSWAP)
53 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
54 #endif
56 #ifdef PNG_WRITE_PACK_SUPPORTED
57 if (png_ptr->transformations & PNG_PACK)
58 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
59 (png_uint_32)png_ptr->bit_depth);
60 #endif
62 #ifdef PNG_WRITE_SWAP_SUPPORTED
63 if (png_ptr->transformations & PNG_SWAP_BYTES)
64 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
65 #endif
67 #ifdef PNG_WRITE_SHIFT_SUPPORTED
68 if (png_ptr->transformations & PNG_SHIFT)
69 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
70 &(png_ptr->shift));
71 #endif
73 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
74 if (png_ptr->transformations & PNG_SWAP_ALPHA)
75 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
76 #endif
78 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
79 if (png_ptr->transformations & PNG_INVERT_ALPHA)
80 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
81 #endif
83 #ifdef PNG_WRITE_BGR_SUPPORTED
84 if (png_ptr->transformations & PNG_BGR)
85 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
86 #endif
88 #ifdef PNG_WRITE_INVERT_SUPPORTED
89 if (png_ptr->transformations & PNG_INVERT_MONO)
90 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
91 #endif
94 #ifdef PNG_WRITE_PACK_SUPPORTED
95 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
96 * row_info bit depth should be 8 (one pixel per byte). The channels
97 * should be 1 (this only happens on grayscale and paletted images).
99 void /* PRIVATE */
100 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
102 png_debug(1, "in png_do_pack");
104 if (row_info->bit_depth == 8 &&
105 row_info->channels == 1)
107 switch ((int)bit_depth)
109 case 1:
111 png_bytep sp, dp;
112 int mask, v;
113 png_uint_32 i;
114 png_uint_32 row_width = row_info->width;
116 sp = row;
117 dp = row;
118 mask = 0x80;
119 v = 0;
121 for (i = 0; i < row_width; i++)
123 if (*sp != 0)
124 v |= mask;
126 sp++;
128 if (mask > 1)
129 mask >>= 1;
131 else
133 mask = 0x80;
134 *dp = (png_byte)v;
135 dp++;
136 v = 0;
140 if (mask != 0x80)
141 *dp = (png_byte)v;
143 break;
146 case 2:
148 png_bytep sp, dp;
149 int shift, v;
150 png_uint_32 i;
151 png_uint_32 row_width = row_info->width;
153 sp = row;
154 dp = row;
155 shift = 6;
156 v = 0;
158 for (i = 0; i < row_width; i++)
160 png_byte value;
162 value = (png_byte)(*sp & 0x03);
163 v |= (value << shift);
165 if (shift == 0)
167 shift = 6;
168 *dp = (png_byte)v;
169 dp++;
170 v = 0;
173 else
174 shift -= 2;
176 sp++;
179 if (shift != 6)
180 *dp = (png_byte)v;
182 break;
185 case 4:
187 png_bytep sp, dp;
188 int shift, v;
189 png_uint_32 i;
190 png_uint_32 row_width = row_info->width;
192 sp = row;
193 dp = row;
194 shift = 4;
195 v = 0;
197 for (i = 0; i < row_width; i++)
199 png_byte value;
201 value = (png_byte)(*sp & 0x0f);
202 v |= (value << shift);
204 if (shift == 0)
206 shift = 4;
207 *dp = (png_byte)v;
208 dp++;
209 v = 0;
212 else
213 shift -= 4;
215 sp++;
218 if (shift != 4)
219 *dp = (png_byte)v;
221 break;
224 default:
225 break;
228 row_info->bit_depth = (png_byte)bit_depth;
229 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
230 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
231 row_info->width);
234 #endif
236 #ifdef PNG_WRITE_SHIFT_SUPPORTED
237 /* Shift pixel values to take advantage of whole range. Pass the
238 * true number of bits in bit_depth. The row should be packed
239 * according to row_info->bit_depth. Thus, if you had a row of
240 * bit depth 4, but the pixels only had values from 0 to 7, you
241 * would pass 3 as bit_depth, and this routine would translate the
242 * data to 0 to 15.
244 void /* PRIVATE */
245 png_do_shift(png_row_infop row_info, png_bytep row,
246 png_const_color_8p bit_depth)
248 png_debug(1, "in png_do_shift");
250 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
252 int shift_start[4], shift_dec[4];
253 int channels = 0;
255 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
257 shift_start[channels] = row_info->bit_depth - bit_depth->red;
258 shift_dec[channels] = bit_depth->red;
259 channels++;
261 shift_start[channels] = row_info->bit_depth - bit_depth->green;
262 shift_dec[channels] = bit_depth->green;
263 channels++;
265 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
266 shift_dec[channels] = bit_depth->blue;
267 channels++;
270 else
272 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
273 shift_dec[channels] = bit_depth->gray;
274 channels++;
277 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
279 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
280 shift_dec[channels] = bit_depth->alpha;
281 channels++;
284 /* With low row depths, could only be grayscale, so one channel */
285 if (row_info->bit_depth < 8)
287 png_bytep bp = row;
288 png_size_t i;
289 png_byte mask;
290 png_size_t row_bytes = row_info->rowbytes;
292 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
293 mask = 0x55;
295 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
296 mask = 0x11;
298 else
299 mask = 0xff;
301 for (i = 0; i < row_bytes; i++, bp++)
303 png_uint_16 v;
304 int j;
306 v = *bp;
307 *bp = 0;
309 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
311 if (j > 0)
312 *bp |= (png_byte)((v << j) & 0xff);
314 else
315 *bp |= (png_byte)((v >> (-j)) & mask);
320 else if (row_info->bit_depth == 8)
322 png_bytep bp = row;
323 png_uint_32 i;
324 png_uint_32 istop = channels * row_info->width;
326 for (i = 0; i < istop; i++, bp++)
329 png_uint_16 v;
330 int j;
331 int c = (int)(i%channels);
333 v = *bp;
334 *bp = 0;
336 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
338 if (j > 0)
339 *bp |= (png_byte)((v << j) & 0xff);
341 else
342 *bp |= (png_byte)((v >> (-j)) & 0xff);
347 else
349 png_bytep bp;
350 png_uint_32 i;
351 png_uint_32 istop = channels * row_info->width;
353 for (bp = row, i = 0; i < istop; i++)
355 int c = (int)(i%channels);
356 png_uint_16 value, v;
357 int j;
359 v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
360 value = 0;
362 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
364 if (j > 0)
365 value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
367 else
368 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
370 *bp++ = (png_byte)(value >> 8);
371 *bp++ = (png_byte)(value & 0xff);
376 #endif
378 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
379 void /* PRIVATE */
380 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
382 png_debug(1, "in png_do_write_swap_alpha");
385 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
387 if (row_info->bit_depth == 8)
389 /* This converts from ARGB to RGBA */
390 png_bytep sp, dp;
391 png_uint_32 i;
392 png_uint_32 row_width = row_info->width;
394 for (i = 0, sp = dp = row; i < row_width; i++)
396 png_byte save = *(sp++);
397 *(dp++) = *(sp++);
398 *(dp++) = *(sp++);
399 *(dp++) = *(sp++);
400 *(dp++) = save;
404 #ifdef PNG_WRITE_16BIT_SUPPORTED
405 else
407 /* This converts from AARRGGBB to RRGGBBAA */
408 png_bytep sp, dp;
409 png_uint_32 i;
410 png_uint_32 row_width = row_info->width;
412 for (i = 0, sp = dp = row; i < row_width; i++)
414 png_byte save[2];
415 save[0] = *(sp++);
416 save[1] = *(sp++);
417 *(dp++) = *(sp++);
418 *(dp++) = *(sp++);
419 *(dp++) = *(sp++);
420 *(dp++) = *(sp++);
421 *(dp++) = *(sp++);
422 *(dp++) = *(sp++);
423 *(dp++) = save[0];
424 *(dp++) = save[1];
427 #endif /* PNG_WRITE_16BIT_SUPPORTED */
430 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
432 if (row_info->bit_depth == 8)
434 /* This converts from AG to GA */
435 png_bytep sp, dp;
436 png_uint_32 i;
437 png_uint_32 row_width = row_info->width;
439 for (i = 0, sp = dp = row; i < row_width; i++)
441 png_byte save = *(sp++);
442 *(dp++) = *(sp++);
443 *(dp++) = save;
447 #ifdef PNG_WRITE_16BIT_SUPPORTED
448 else
450 /* This converts from AAGG to GGAA */
451 png_bytep sp, dp;
452 png_uint_32 i;
453 png_uint_32 row_width = row_info->width;
455 for (i = 0, sp = dp = row; i < row_width; i++)
457 png_byte save[2];
458 save[0] = *(sp++);
459 save[1] = *(sp++);
460 *(dp++) = *(sp++);
461 *(dp++) = *(sp++);
462 *(dp++) = save[0];
463 *(dp++) = save[1];
466 #endif /* PNG_WRITE_16BIT_SUPPORTED */
470 #endif
472 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
473 void /* PRIVATE */
474 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
476 png_debug(1, "in png_do_write_invert_alpha");
479 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
481 if (row_info->bit_depth == 8)
483 /* This inverts the alpha channel in RGBA */
484 png_bytep sp, dp;
485 png_uint_32 i;
486 png_uint_32 row_width = row_info->width;
488 for (i = 0, sp = dp = row; i < row_width; i++)
490 /* Does nothing
491 *(dp++) = *(sp++);
492 *(dp++) = *(sp++);
493 *(dp++) = *(sp++);
495 sp+=3; dp = sp;
496 *(dp++) = (png_byte)(255 - *(sp++));
500 #ifdef PNG_WRITE_16BIT_SUPPORTED
501 else
503 /* This inverts the alpha channel in RRGGBBAA */
504 png_bytep sp, dp;
505 png_uint_32 i;
506 png_uint_32 row_width = row_info->width;
508 for (i = 0, sp = dp = row; i < row_width; i++)
510 /* Does nothing
511 *(dp++) = *(sp++);
512 *(dp++) = *(sp++);
513 *(dp++) = *(sp++);
514 *(dp++) = *(sp++);
515 *(dp++) = *(sp++);
516 *(dp++) = *(sp++);
518 sp+=6; dp = sp;
519 *(dp++) = (png_byte)(255 - *(sp++));
520 *(dp++) = (png_byte)(255 - *(sp++));
523 #endif /* PNG_WRITE_16BIT_SUPPORTED */
526 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
528 if (row_info->bit_depth == 8)
530 /* This inverts the alpha channel in GA */
531 png_bytep sp, dp;
532 png_uint_32 i;
533 png_uint_32 row_width = row_info->width;
535 for (i = 0, sp = dp = row; i < row_width; i++)
537 *(dp++) = *(sp++);
538 *(dp++) = (png_byte)(255 - *(sp++));
542 #ifdef PNG_WRITE_16BIT_SUPPORTED
543 else
545 /* This inverts the alpha channel in GGAA */
546 png_bytep sp, dp;
547 png_uint_32 i;
548 png_uint_32 row_width = row_info->width;
550 for (i = 0, sp = dp = row; i < row_width; i++)
552 /* Does nothing
553 *(dp++) = *(sp++);
554 *(dp++) = *(sp++);
556 sp+=2; dp = sp;
557 *(dp++) = (png_byte)(255 - *(sp++));
558 *(dp++) = (png_byte)(255 - *(sp++));
561 #endif /* PNG_WRITE_16BIT_SUPPORTED */
565 #endif
567 #ifdef PNG_MNG_FEATURES_SUPPORTED
568 /* Undoes intrapixel differencing */
569 void /* PRIVATE */
570 png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
572 png_debug(1, "in png_do_write_intrapixel");
574 if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
576 int bytes_per_pixel;
577 png_uint_32 row_width = row_info->width;
578 if (row_info->bit_depth == 8)
580 png_bytep rp;
581 png_uint_32 i;
583 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
584 bytes_per_pixel = 3;
586 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
587 bytes_per_pixel = 4;
589 else
590 return;
592 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
594 *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
595 *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
599 #ifdef PNG_WRITE_16BIT_SUPPORTED
600 else if (row_info->bit_depth == 16)
602 png_bytep rp;
603 png_uint_32 i;
605 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
606 bytes_per_pixel = 6;
608 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
609 bytes_per_pixel = 8;
611 else
612 return;
614 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
616 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
617 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
618 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
619 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
620 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
621 *(rp ) = (png_byte)((red >> 8) & 0xff);
622 *(rp + 1) = (png_byte)(red & 0xff);
623 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
624 *(rp + 5) = (png_byte)(blue & 0xff);
627 #endif /* PNG_WRITE_16BIT_SUPPORTED */
630 #endif /* PNG_MNG_FEATURES_SUPPORTED */
631 #endif /* PNG_WRITE_SUPPORTED */