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
16 #ifdef PNG_WRITE_SUPPORTED
18 /* Transform the data according to the user's wishes. The order of
19 * transformations is significant.
22 png_do_write_transformations(png_structp png_ptr
)
24 png_debug(1, "in png_do_write_transformations");
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
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 */
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,
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);
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
);
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);
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,
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);
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);
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);
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);
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).
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
)
114 png_uint_32 row_width
= row_info
->width
;
121 for (i
= 0; i
< row_width
; i
++)
151 png_uint_32 row_width
= row_info
->width
;
158 for (i
= 0; i
< row_width
; i
++)
162 value
= (png_byte
)(*sp
& 0x03);
163 v
|= (value
<< shift
);
190 png_uint_32 row_width
= row_info
->width
;
197 for (i
= 0; i
< row_width
; i
++)
201 value
= (png_byte
)(*sp
& 0x0f);
202 v
|= (value
<< shift
);
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
,
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
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];
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
;
261 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->green
;
262 shift_dec
[channels
] = bit_depth
->green
;
265 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->blue
;
266 shift_dec
[channels
] = bit_depth
->blue
;
272 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->gray
;
273 shift_dec
[channels
] = bit_depth
->gray
;
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
;
284 /* With low row depths, could only be grayscale, so one channel */
285 if (row_info
->bit_depth
< 8)
290 png_size_t row_bytes
= row_info
->rowbytes
;
292 if (bit_depth
->gray
== 1 && row_info
->bit_depth
== 2)
295 else if (row_info
->bit_depth
== 4 && bit_depth
->gray
== 3)
301 for (i
= 0; i
< row_bytes
; i
++, bp
++)
309 for (j
= shift_start
[0]; j
> -shift_dec
[0]; j
-= shift_dec
[0])
312 *bp
|= (png_byte
)((v
<< j
) & 0xff);
315 *bp
|= (png_byte
)((v
>> (-j
)) & mask
);
320 else if (row_info
->bit_depth
== 8)
324 png_uint_32 istop
= channels
* row_info
->width
;
326 for (i
= 0; i
< istop
; i
++, bp
++)
331 int c
= (int)(i
%channels
);
336 for (j
= shift_start
[c
]; j
> -shift_dec
[c
]; j
-= shift_dec
[c
])
339 *bp
|= (png_byte
)((v
<< j
) & 0xff);
342 *bp
|= (png_byte
)((v
>> (-j
)) & 0xff);
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
;
359 v
= (png_uint_16
)(((png_uint_16
)(*bp
) << 8) + *(bp
+ 1));
362 for (j
= shift_start
[c
]; j
> -shift_dec
[c
]; j
-= shift_dec
[c
])
365 value
|= (png_uint_16
)((v
<< j
) & (png_uint_16
)0xffff);
368 value
|= (png_uint_16
)((v
>> (-j
)) & (png_uint_16
)0xffff);
370 *bp
++ = (png_byte
)(value
>> 8);
371 *bp
++ = (png_byte
)(value
& 0xff);
378 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
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 */
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
++);
404 #ifdef PNG_WRITE_16BIT_SUPPORTED
407 /* This converts from AARRGGBB to RRGGBBAA */
410 png_uint_32 row_width
= row_info
->width
;
412 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
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 */
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
++);
447 #ifdef PNG_WRITE_16BIT_SUPPORTED
450 /* This converts from AAGG to GGAA */
453 png_uint_32 row_width
= row_info
->width
;
455 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
466 #endif /* PNG_WRITE_16BIT_SUPPORTED */
472 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
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 */
486 png_uint_32 row_width
= row_info
->width
;
488 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
496 *(dp
++) = (png_byte
)(255 - *(sp
++));
500 #ifdef PNG_WRITE_16BIT_SUPPORTED
503 /* This inverts the alpha channel in RRGGBBAA */
506 png_uint_32 row_width
= row_info
->width
;
508 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
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 */
533 png_uint_32 row_width
= row_info
->width
;
535 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
538 *(dp
++) = (png_byte
)(255 - *(sp
++));
542 #ifdef PNG_WRITE_16BIT_SUPPORTED
545 /* This inverts the alpha channel in GGAA */
548 png_uint_32 row_width
= row_info
->width
;
550 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
557 *(dp
++) = (png_byte
)(255 - *(sp
++));
558 *(dp
++) = (png_byte
)(255 - *(sp
++));
561 #endif /* PNG_WRITE_16BIT_SUPPORTED */
567 #ifdef PNG_MNG_FEATURES_SUPPORTED
568 /* Undoes intrapixel differencing */
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
))
577 png_uint_32 row_width
= row_info
->width
;
578 if (row_info
->bit_depth
== 8)
583 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
586 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
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)
605 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
608 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
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 */