2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * Last changed in libpng 1.5.6 [November 3, 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
16 #ifdef PNG_WRITE_SUPPORTED
18 #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
19 /* Transform the data according to the user's wishes. The order of
20 * transformations is significant.
23 png_do_write_transformations(png_structp png_ptr
, png_row_infop row_info
)
25 png_debug(1, "in png_do_write_transformations");
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
35 (png_ptr
, /* png_ptr */
36 row_info
, /* row_info: */
37 /* png_uint_32 width; width of row */
38 /* png_size_t 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 */
46 #ifdef PNG_WRITE_FILLER_SUPPORTED
47 if (png_ptr
->transformations
& PNG_FILLER
)
48 png_do_strip_channel(row_info
, png_ptr
->row_buf
+ 1,
49 !(png_ptr
->flags
& PNG_FLAG_FILLER_AFTER
));
52 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
53 if (png_ptr
->transformations
& PNG_PACKSWAP
)
54 png_do_packswap(row_info
, png_ptr
->row_buf
+ 1);
57 #ifdef PNG_WRITE_PACK_SUPPORTED
58 if (png_ptr
->transformations
& PNG_PACK
)
59 png_do_pack(row_info
, png_ptr
->row_buf
+ 1,
60 (png_uint_32
)png_ptr
->bit_depth
);
63 #ifdef PNG_WRITE_SWAP_SUPPORTED
64 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
65 png_do_swap(row_info
, png_ptr
->row_buf
+ 1);
68 #ifdef PNG_WRITE_SHIFT_SUPPORTED
69 if (png_ptr
->transformations
& PNG_SHIFT
)
70 png_do_shift(row_info
, png_ptr
->row_buf
+ 1,
74 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
75 if (png_ptr
->transformations
& PNG_SWAP_ALPHA
)
76 png_do_write_swap_alpha(row_info
, png_ptr
->row_buf
+ 1);
79 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
80 if (png_ptr
->transformations
& PNG_INVERT_ALPHA
)
81 png_do_write_invert_alpha(row_info
, png_ptr
->row_buf
+ 1);
84 #ifdef PNG_WRITE_BGR_SUPPORTED
85 if (png_ptr
->transformations
& PNG_BGR
)
86 png_do_bgr(row_info
, png_ptr
->row_buf
+ 1);
89 #ifdef PNG_WRITE_INVERT_SUPPORTED
90 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
91 png_do_invert(row_info
, png_ptr
->row_buf
+ 1);
95 #ifdef PNG_WRITE_PACK_SUPPORTED
96 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
97 * row_info bit depth should be 8 (one pixel per byte). The channels
98 * should be 1 (this only happens on grayscale and paletted images).
101 png_do_pack(png_row_infop row_info
, png_bytep row
, png_uint_32 bit_depth
)
103 png_debug(1, "in png_do_pack");
105 if (row_info
->bit_depth
== 8 &&
106 row_info
->channels
== 1)
108 switch ((int)bit_depth
)
115 png_uint_32 row_width
= row_info
->width
;
122 for (i
= 0; i
< row_width
; i
++)
152 png_uint_32 row_width
= row_info
->width
;
159 for (i
= 0; i
< row_width
; i
++)
163 value
= (png_byte
)(*sp
& 0x03);
164 v
|= (value
<< shift
);
191 png_uint_32 row_width
= row_info
->width
;
198 for (i
= 0; i
< row_width
; i
++)
202 value
= (png_byte
)(*sp
& 0x0f);
203 v
|= (value
<< shift
);
229 row_info
->bit_depth
= (png_byte
)bit_depth
;
230 row_info
->pixel_depth
= (png_byte
)(bit_depth
* row_info
->channels
);
231 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
,
237 #ifdef PNG_WRITE_SHIFT_SUPPORTED
238 /* Shift pixel values to take advantage of whole range. Pass the
239 * true number of bits in bit_depth. The row should be packed
240 * according to row_info->bit_depth. Thus, if you had a row of
241 * bit depth 4, but the pixels only had values from 0 to 7, you
242 * would pass 3 as bit_depth, and this routine would translate the
246 png_do_shift(png_row_infop row_info
, png_bytep row
,
247 png_const_color_8p bit_depth
)
249 png_debug(1, "in png_do_shift");
251 if (row_info
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
253 int shift_start
[4], shift_dec
[4];
256 if (row_info
->color_type
& PNG_COLOR_MASK_COLOR
)
258 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->red
;
259 shift_dec
[channels
] = bit_depth
->red
;
262 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->green
;
263 shift_dec
[channels
] = bit_depth
->green
;
266 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->blue
;
267 shift_dec
[channels
] = bit_depth
->blue
;
273 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->gray
;
274 shift_dec
[channels
] = bit_depth
->gray
;
278 if (row_info
->color_type
& PNG_COLOR_MASK_ALPHA
)
280 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->alpha
;
281 shift_dec
[channels
] = bit_depth
->alpha
;
285 /* With low row depths, could only be grayscale, so one channel */
286 if (row_info
->bit_depth
< 8)
291 png_size_t row_bytes
= row_info
->rowbytes
;
293 if (bit_depth
->gray
== 1 && row_info
->bit_depth
== 2)
296 else if (row_info
->bit_depth
== 4 && bit_depth
->gray
== 3)
302 for (i
= 0; i
< row_bytes
; i
++, bp
++)
310 for (j
= shift_start
[0]; j
> -shift_dec
[0]; j
-= shift_dec
[0])
313 *bp
|= (png_byte
)((v
<< j
) & 0xff);
316 *bp
|= (png_byte
)((v
>> (-j
)) & mask
);
321 else if (row_info
->bit_depth
== 8)
325 png_uint_32 istop
= channels
* row_info
->width
;
327 for (i
= 0; i
< istop
; i
++, bp
++)
332 int c
= (int)(i%channels
);
337 for (j
= shift_start
[c
]; j
> -shift_dec
[c
]; j
-= shift_dec
[c
])
340 *bp
|= (png_byte
)((v
<< j
) & 0xff);
343 *bp
|= (png_byte
)((v
>> (-j
)) & 0xff);
352 png_uint_32 istop
= channels
* row_info
->width
;
354 for (bp
= row
, i
= 0; i
< istop
; i
++)
356 int c
= (int)(i%channels
);
357 png_uint_16 value
, v
;
360 v
= (png_uint_16
)(((png_uint_16
)(*bp
) << 8) + *(bp
+ 1));
363 for (j
= shift_start
[c
]; j
> -shift_dec
[c
]; j
-= shift_dec
[c
])
366 value
|= (png_uint_16
)((v
<< j
) & (png_uint_16
)0xffff);
369 value
|= (png_uint_16
)((v
>> (-j
)) & (png_uint_16
)0xffff);
371 *bp
++ = (png_byte
)(value
>> 8);
372 *bp
++ = (png_byte
)(value
& 0xff);
379 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
381 png_do_write_swap_alpha(png_row_infop row_info
, png_bytep row
)
383 png_debug(1, "in png_do_write_swap_alpha");
386 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
388 if (row_info
->bit_depth
== 8)
390 /* This converts from ARGB to RGBA */
393 png_uint_32 row_width
= row_info
->width
;
395 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
397 png_byte save
= *(sp
++);
405 #ifdef PNG_WRITE_16BIT_SUPPORTED
408 /* This converts from AARRGGBB to RRGGBBAA */
411 png_uint_32 row_width
= row_info
->width
;
413 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
428 #endif /* PNG_WRITE_16BIT_SUPPORTED */
431 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
433 if (row_info
->bit_depth
== 8)
435 /* This converts from AG to GA */
438 png_uint_32 row_width
= row_info
->width
;
440 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
442 png_byte save
= *(sp
++);
448 #ifdef PNG_WRITE_16BIT_SUPPORTED
451 /* This converts from AAGG to GGAA */
454 png_uint_32 row_width
= row_info
->width
;
456 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
467 #endif /* PNG_WRITE_16BIT_SUPPORTED */
473 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
475 png_do_write_invert_alpha(png_row_infop row_info
, png_bytep row
)
477 png_debug(1, "in png_do_write_invert_alpha");
480 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
482 if (row_info
->bit_depth
== 8)
484 /* This inverts the alpha channel in RGBA */
487 png_uint_32 row_width
= row_info
->width
;
489 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
497 *(dp
++) = (png_byte
)(255 - *(sp
++));
501 #ifdef PNG_WRITE_16BIT_SUPPORTED
504 /* This inverts the alpha channel in RRGGBBAA */
507 png_uint_32 row_width
= row_info
->width
;
509 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
520 *(dp
++) = (png_byte
)(255 - *(sp
++));
521 *(dp
++) = (png_byte
)(255 - *(sp
++));
524 #endif /* PNG_WRITE_16BIT_SUPPORTED */
527 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
529 if (row_info
->bit_depth
== 8)
531 /* This inverts the alpha channel in GA */
534 png_uint_32 row_width
= row_info
->width
;
536 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
539 *(dp
++) = (png_byte
)(255 - *(sp
++));
543 #ifdef PNG_WRITE_16BIT_SUPPORTED
546 /* This inverts the alpha channel in GGAA */
549 png_uint_32 row_width
= row_info
->width
;
551 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
558 *(dp
++) = (png_byte
)(255 - *(sp
++));
559 *(dp
++) = (png_byte
)(255 - *(sp
++));
562 #endif /* PNG_WRITE_16BIT_SUPPORTED */
567 #endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */
569 #ifdef PNG_MNG_FEATURES_SUPPORTED
570 /* Undoes intrapixel differencing */
572 png_do_write_intrapixel(png_row_infop row_info
, png_bytep row
)
574 png_debug(1, "in png_do_write_intrapixel");
576 if ((row_info
->color_type
& PNG_COLOR_MASK_COLOR
))
579 png_uint_32 row_width
= row_info
->width
;
580 if (row_info
->bit_depth
== 8)
585 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
588 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
594 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
596 *(rp
) = (png_byte
)((*rp
- *(rp
+ 1)) & 0xff);
597 *(rp
+ 2) = (png_byte
)((*(rp
+ 2) - *(rp
+ 1)) & 0xff);
601 #ifdef PNG_WRITE_16BIT_SUPPORTED
602 else if (row_info
->bit_depth
== 16)
607 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
610 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
616 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
618 png_uint_32 s0
= (*(rp
) << 8) | *(rp
+ 1);
619 png_uint_32 s1
= (*(rp
+ 2) << 8) | *(rp
+ 3);
620 png_uint_32 s2
= (*(rp
+ 4) << 8) | *(rp
+ 5);
621 png_uint_32 red
= (png_uint_32
)((s0
- s1
) & 0xffffL
);
622 png_uint_32 blue
= (png_uint_32
)((s2
- s1
) & 0xffffL
);
623 *(rp
) = (png_byte
)((red
>> 8) & 0xff);
624 *(rp
+ 1) = (png_byte
)(red
& 0xff);
625 *(rp
+ 4) = (png_byte
)((blue
>> 8) & 0xff);
626 *(rp
+ 5) = (png_byte
)(blue
& 0xff);
629 #endif /* PNG_WRITE_16BIT_SUPPORTED */
632 #endif /* PNG_MNG_FEATURES_SUPPORTED */
633 #endif /* PNG_WRITE_SUPPORTED */