2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * Last changed in libpng 1.6.18 [July 23, 2015]
5 * Copyright (c) 1998-2015 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
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).
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
)
39 png_uint_32 row_width
= row_info
->width
;
46 for (i
= 0; i
< row_width
; i
++)
77 png_uint_32 row_width
= row_info
->width
;
84 for (i
= 0; i
< row_width
; i
++)
88 value
= (png_byte
)(*sp
& 0x03);
89 v
|= (value
<< shift
);
117 png_uint_32 row_width
= row_info
->width
;
124 for (i
= 0; i
< row_width
; i
++)
128 value
= (png_byte
)(*sp
& 0x0f);
129 v
|= (value
<< shift
);
155 row_info
->bit_depth
= (png_byte
)bit_depth
;
156 row_info
->pixel_depth
= (png_byte
)(bit_depth
* row_info
->channels
);
157 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
,
163 #ifdef PNG_WRITE_SHIFT_SUPPORTED
164 /* Shift pixel values to take advantage of whole range. Pass the
165 * true number of bits in bit_depth. The row should be packed
166 * according to row_info->bit_depth. Thus, if you had a row of
167 * bit depth 4, but the pixels only had values from 0 to 7, you
168 * would pass 3 as bit_depth, and this routine would translate the
172 png_do_shift(png_row_infop row_info
, png_bytep row
,
173 png_const_color_8p bit_depth
)
175 png_debug(1, "in png_do_shift");
177 if (row_info
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
179 int shift_start
[4], shift_dec
[4];
182 if ((row_info
->color_type
& PNG_COLOR_MASK_COLOR
) != 0)
184 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->red
;
185 shift_dec
[channels
] = bit_depth
->red
;
188 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->green
;
189 shift_dec
[channels
] = bit_depth
->green
;
192 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->blue
;
193 shift_dec
[channels
] = bit_depth
->blue
;
199 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->gray
;
200 shift_dec
[channels
] = bit_depth
->gray
;
204 if ((row_info
->color_type
& PNG_COLOR_MASK_ALPHA
) != 0)
206 shift_start
[channels
] = row_info
->bit_depth
- bit_depth
->alpha
;
207 shift_dec
[channels
] = bit_depth
->alpha
;
211 /* With low row depths, could only be grayscale, so one channel */
212 if (row_info
->bit_depth
< 8)
217 png_size_t row_bytes
= row_info
->rowbytes
;
219 if (bit_depth
->gray
== 1 && row_info
->bit_depth
== 2)
222 else if (row_info
->bit_depth
== 4 && bit_depth
->gray
== 3)
228 for (i
= 0; i
< row_bytes
; i
++, bp
++)
236 for (j
= shift_start
[0]; j
> -shift_dec
[0]; j
-= shift_dec
[0])
242 out
|= (v
>> (-j
)) & mask
;
245 *bp
= (png_byte
)(out
& 0xff);
249 else if (row_info
->bit_depth
== 8)
253 png_uint_32 istop
= channels
* row_info
->width
;
255 for (i
= 0; i
< istop
; i
++, bp
++)
258 const unsigned int c
= i
%channels
;
265 for (j
= shift_start
[c
]; j
> -shift_dec
[c
]; j
-= shift_dec
[c
])
274 *bp
= (png_byte
)(out
& 0xff);
282 png_uint_32 istop
= channels
* row_info
->width
;
284 for (bp
= row
, i
= 0; i
< istop
; i
++)
286 const unsigned int c
= i
%channels
;
288 unsigned int value
, v
;
290 v
= png_get_uint_16(bp
);
293 for (j
= shift_start
[c
]; j
> -shift_dec
[c
]; j
-= shift_dec
[c
])
301 *bp
++ = (png_byte
)((value
>> 8) & 0xff);
302 *bp
++ = (png_byte
)(value
& 0xff);
309 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
311 png_do_write_swap_alpha(png_row_infop row_info
, png_bytep row
)
313 png_debug(1, "in png_do_write_swap_alpha");
316 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
318 if (row_info
->bit_depth
== 8)
320 /* This converts from ARGB to RGBA */
323 png_uint_32 row_width
= row_info
->width
;
325 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
327 png_byte save
= *(sp
++);
335 #ifdef PNG_WRITE_16BIT_SUPPORTED
338 /* This converts from AARRGGBB to RRGGBBAA */
341 png_uint_32 row_width
= row_info
->width
;
343 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
358 #endif /* WRITE_16BIT */
361 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
363 if (row_info
->bit_depth
== 8)
365 /* This converts from AG to GA */
368 png_uint_32 row_width
= row_info
->width
;
370 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
372 png_byte save
= *(sp
++);
378 #ifdef PNG_WRITE_16BIT_SUPPORTED
381 /* This converts from AAGG to GGAA */
384 png_uint_32 row_width
= row_info
->width
;
386 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
397 #endif /* WRITE_16BIT */
403 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
405 png_do_write_invert_alpha(png_row_infop row_info
, png_bytep row
)
407 png_debug(1, "in png_do_write_invert_alpha");
410 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
412 if (row_info
->bit_depth
== 8)
414 /* This inverts the alpha channel in RGBA */
417 png_uint_32 row_width
= row_info
->width
;
419 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
427 *dp
= (png_byte
)(255 - *(sp
++));
431 #ifdef PNG_WRITE_16BIT_SUPPORTED
434 /* This inverts the alpha channel in RRGGBBAA */
437 png_uint_32 row_width
= row_info
->width
;
439 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
450 *(dp
++) = (png_byte
)(255 - *(sp
++));
451 *dp
= (png_byte
)(255 - *(sp
++));
454 #endif /* WRITE_16BIT */
457 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
459 if (row_info
->bit_depth
== 8)
461 /* This inverts the alpha channel in GA */
464 png_uint_32 row_width
= row_info
->width
;
466 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
469 *(dp
++) = (png_byte
)(255 - *(sp
++));
473 #ifdef PNG_WRITE_16BIT_SUPPORTED
476 /* This inverts the alpha channel in GGAA */
479 png_uint_32 row_width
= row_info
->width
;
481 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
488 *(dp
++) = (png_byte
)(255 - *(sp
++));
489 *dp
= (png_byte
)(255 - *(sp
++));
492 #endif /* WRITE_16BIT */
498 /* Transform the data according to the user's wishes. The order of
499 * transformations is significant.
502 png_do_write_transformations(png_structrp png_ptr
, png_row_infop row_info
)
504 png_debug(1, "in png_do_write_transformations");
509 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
510 if ((png_ptr
->transformations
& PNG_USER_TRANSFORM
) != 0)
511 if (png_ptr
->write_user_transform_fn
!= NULL
)
512 (*(png_ptr
->write_user_transform_fn
)) /* User write transform
514 (png_ptr
, /* png_ptr */
515 row_info
, /* row_info: */
516 /* png_uint_32 width; width of row */
517 /* png_size_t rowbytes; number of bytes in row */
518 /* png_byte color_type; color type of pixels */
519 /* png_byte bit_depth; bit depth of samples */
520 /* png_byte channels; number of channels (1-4) */
521 /* png_byte pixel_depth; bits per pixel (depth*channels) */
522 png_ptr
->row_buf
+ 1); /* start of pixel data for row */
525 #ifdef PNG_WRITE_FILLER_SUPPORTED
526 if ((png_ptr
->transformations
& PNG_FILLER
) != 0)
527 png_do_strip_channel(row_info
, png_ptr
->row_buf
+ 1,
528 !(png_ptr
->flags
& PNG_FLAG_FILLER_AFTER
));
531 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
532 if ((png_ptr
->transformations
& PNG_PACKSWAP
) != 0)
533 png_do_packswap(row_info
, png_ptr
->row_buf
+ 1);
536 #ifdef PNG_WRITE_PACK_SUPPORTED
537 if ((png_ptr
->transformations
& PNG_PACK
) != 0)
538 png_do_pack(row_info
, png_ptr
->row_buf
+ 1,
539 (png_uint_32
)png_ptr
->bit_depth
);
542 #ifdef PNG_WRITE_SWAP_SUPPORTED
543 # ifdef PNG_16BIT_SUPPORTED
544 if ((png_ptr
->transformations
& PNG_SWAP_BYTES
) != 0)
545 png_do_swap(row_info
, png_ptr
->row_buf
+ 1);
549 #ifdef PNG_WRITE_SHIFT_SUPPORTED
550 if ((png_ptr
->transformations
& PNG_SHIFT
) != 0)
551 png_do_shift(row_info
, png_ptr
->row_buf
+ 1,
555 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
556 if ((png_ptr
->transformations
& PNG_SWAP_ALPHA
) != 0)
557 png_do_write_swap_alpha(row_info
, png_ptr
->row_buf
+ 1);
560 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
561 if ((png_ptr
->transformations
& PNG_INVERT_ALPHA
) != 0)
562 png_do_write_invert_alpha(row_info
, png_ptr
->row_buf
+ 1);
565 #ifdef PNG_WRITE_BGR_SUPPORTED
566 if ((png_ptr
->transformations
& PNG_BGR
) != 0)
567 png_do_bgr(row_info
, png_ptr
->row_buf
+ 1);
570 #ifdef PNG_WRITE_INVERT_SUPPORTED
571 if ((png_ptr
->transformations
& PNG_INVERT_MONO
) != 0)
572 png_do_invert(row_info
, png_ptr
->row_buf
+ 1);
575 #endif /* WRITE_TRANSFORMS */