2 /* pngwtran.c - transforms the data in a row for PNG writers
4 * Copyright (c) 2018 Cosmin Truta
5 * Copyright (c) 1998-2002,2004,2006-2016,2018 Glenn Randers-Pehrson
6 * Copyright (c) 1996-1997 Andreas Dilger
7 * 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];
180 unsigned int channels
= 0;
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 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
++)
257 unsigned int c
= i
%channels
;
264 for (j
= shift_start
[c
]; j
> -shift_dec
[c
]; j
-= shift_dec
[c
])
273 *bp
= (png_byte
)(out
& 0xff);
281 png_uint_32 istop
= channels
* row_info
->width
;
283 for (bp
= row
, i
= 0; i
< istop
; i
++)
285 unsigned int c
= i
%channels
;
287 unsigned int value
, v
;
289 v
= png_get_uint_16(bp
);
292 for (j
= shift_start
[c
]; j
> -shift_dec
[c
]; j
-= shift_dec
[c
])
300 *bp
++ = (png_byte
)((value
>> 8) & 0xff);
301 *bp
++ = (png_byte
)(value
& 0xff);
308 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
310 png_do_write_swap_alpha(png_row_infop row_info
, png_bytep row
)
312 png_debug(1, "in png_do_write_swap_alpha");
315 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
317 if (row_info
->bit_depth
== 8)
319 /* This converts from ARGB to RGBA */
322 png_uint_32 row_width
= row_info
->width
;
324 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
326 png_byte save
= *(sp
++);
334 #ifdef PNG_WRITE_16BIT_SUPPORTED
337 /* This converts from AARRGGBB to RRGGBBAA */
340 png_uint_32 row_width
= row_info
->width
;
342 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
357 #endif /* WRITE_16BIT */
360 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
362 if (row_info
->bit_depth
== 8)
364 /* This converts from AG to GA */
367 png_uint_32 row_width
= row_info
->width
;
369 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
371 png_byte save
= *(sp
++);
377 #ifdef PNG_WRITE_16BIT_SUPPORTED
380 /* This converts from AAGG to GGAA */
383 png_uint_32 row_width
= row_info
->width
;
385 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
396 #endif /* WRITE_16BIT */
402 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
404 png_do_write_invert_alpha(png_row_infop row_info
, png_bytep row
)
406 png_debug(1, "in png_do_write_invert_alpha");
409 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
411 if (row_info
->bit_depth
== 8)
413 /* This inverts the alpha channel in RGBA */
416 png_uint_32 row_width
= row_info
->width
;
418 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
426 *dp
= (png_byte
)(255 - *(sp
++));
430 #ifdef PNG_WRITE_16BIT_SUPPORTED
433 /* This inverts the alpha channel in RRGGBBAA */
436 png_uint_32 row_width
= row_info
->width
;
438 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
449 *(dp
++) = (png_byte
)(255 - *(sp
++));
450 *dp
= (png_byte
)(255 - *(sp
++));
453 #endif /* WRITE_16BIT */
456 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
458 if (row_info
->bit_depth
== 8)
460 /* This inverts the alpha channel in GA */
463 png_uint_32 row_width
= row_info
->width
;
465 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
468 *(dp
++) = (png_byte
)(255 - *(sp
++));
472 #ifdef PNG_WRITE_16BIT_SUPPORTED
475 /* This inverts the alpha channel in GGAA */
478 png_uint_32 row_width
= row_info
->width
;
480 for (i
= 0, sp
= dp
= row
; i
< row_width
; i
++)
487 *(dp
++) = (png_byte
)(255 - *(sp
++));
488 *dp
= (png_byte
)(255 - *(sp
++));
491 #endif /* WRITE_16BIT */
497 /* Transform the data according to the user's wishes. The order of
498 * transformations is significant.
501 png_do_write_transformations(png_structrp png_ptr
, png_row_infop row_info
)
503 png_debug(1, "in png_do_write_transformations");
508 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
509 if ((png_ptr
->transformations
& PNG_USER_TRANSFORM
) != 0)
510 if (png_ptr
->write_user_transform_fn
!= NULL
)
511 (*(png_ptr
->write_user_transform_fn
)) /* User write transform
513 (png_ptr
, /* png_ptr */
514 row_info
, /* row_info: */
515 /* png_uint_32 width; width of row */
516 /* size_t rowbytes; number of bytes in row */
517 /* png_byte color_type; color type of pixels */
518 /* png_byte bit_depth; bit depth of samples */
519 /* png_byte channels; number of channels (1-4) */
520 /* png_byte pixel_depth; bits per pixel (depth*channels) */
521 png_ptr
->row_buf
+ 1); /* start of pixel data for row */
524 #ifdef PNG_WRITE_FILLER_SUPPORTED
525 if ((png_ptr
->transformations
& PNG_FILLER
) != 0)
526 png_do_strip_channel(row_info
, png_ptr
->row_buf
+ 1,
527 !(png_ptr
->flags
& PNG_FLAG_FILLER_AFTER
));
530 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
531 if ((png_ptr
->transformations
& PNG_PACKSWAP
) != 0)
532 png_do_packswap(row_info
, png_ptr
->row_buf
+ 1);
535 #ifdef PNG_WRITE_PACK_SUPPORTED
536 if ((png_ptr
->transformations
& PNG_PACK
) != 0)
537 png_do_pack(row_info
, png_ptr
->row_buf
+ 1,
538 (png_uint_32
)png_ptr
->bit_depth
);
541 #ifdef PNG_WRITE_SWAP_SUPPORTED
542 # ifdef PNG_16BIT_SUPPORTED
543 if ((png_ptr
->transformations
& PNG_SWAP_BYTES
) != 0)
544 png_do_swap(row_info
, png_ptr
->row_buf
+ 1);
548 #ifdef PNG_WRITE_SHIFT_SUPPORTED
549 if ((png_ptr
->transformations
& PNG_SHIFT
) != 0)
550 png_do_shift(row_info
, png_ptr
->row_buf
+ 1,
554 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
555 if ((png_ptr
->transformations
& PNG_SWAP_ALPHA
) != 0)
556 png_do_write_swap_alpha(row_info
, png_ptr
->row_buf
+ 1);
559 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
560 if ((png_ptr
->transformations
& PNG_INVERT_ALPHA
) != 0)
561 png_do_write_invert_alpha(row_info
, png_ptr
->row_buf
+ 1);
564 #ifdef PNG_WRITE_BGR_SUPPORTED
565 if ((png_ptr
->transformations
& PNG_BGR
) != 0)
566 png_do_bgr(row_info
, png_ptr
->row_buf
+ 1);
569 #ifdef PNG_WRITE_INVERT_SUPPORTED
570 if ((png_ptr
->transformations
& PNG_INVERT_MONO
) != 0)
571 png_do_invert(row_info
, png_ptr
->row_buf
+ 1);
574 #endif /* WRITE_TRANSFORMS */