11
11
* and license in png.h
14
#define PNG_NO_PEDANTIC_WARNINGS
16
16
#ifdef PNG_WRITE_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)
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
&(png_ptr->row_info), /* row_info: */
37
/* png_uint_32 width; width of row */
38
/* png_uint_32 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 */
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,
50
#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
51
if (png_ptr->transformations & PNG_PACKSWAP)
52
png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
54
#ifdef PNG_WRITE_PACK_SUPPORTED
55
if (png_ptr->transformations & PNG_PACK)
56
png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
57
(png_uint_32)png_ptr->bit_depth);
59
#ifdef PNG_WRITE_SWAP_SUPPORTED
60
if (png_ptr->transformations & PNG_SWAP_BYTES)
61
png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
63
#ifdef PNG_WRITE_SHIFT_SUPPORTED
64
if (png_ptr->transformations & PNG_SHIFT)
65
png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
68
#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
69
if (png_ptr->transformations & PNG_SWAP_ALPHA)
70
png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
72
#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
73
if (png_ptr->transformations & PNG_INVERT_ALPHA)
74
png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
76
#ifdef PNG_WRITE_BGR_SUPPORTED
77
if (png_ptr->transformations & PNG_BGR)
78
png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
80
#ifdef PNG_WRITE_INVERT_SUPPORTED
81
if (png_ptr->transformations & PNG_INVERT_MONO)
82
png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
17
#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
86
19
#ifdef PNG_WRITE_PACK_SUPPORTED
87
20
/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
88
21
* row_info bit depth should be 8 (one pixel per byte). The channels
89
22
* should be 1 (this only happens on grayscale and paletted images).
92
25
png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
94
27
png_debug(1, "in png_do_pack");
215
168
* would pass 3 as bit_depth, and this routine would translate the
216
169
* data to 0 to 15.
219
png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
172
png_do_shift(png_row_infop row_info, png_bytep row,
173
png_const_color_8p bit_depth)
221
175
png_debug(1, "in png_do_shift");
224
row_info->color_type != PNG_COLOR_TYPE_PALETTE)
177
if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
226
179
int shift_start[4], shift_dec[4];
227
180
int channels = 0;
229
if (row_info->color_type & PNG_COLOR_MASK_COLOR)
182
if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
231
184
shift_start[channels] = row_info->bit_depth - bit_depth->red;
232
185
shift_dec[channels] = bit_depth->red;
234
188
shift_start[channels] = row_info->bit_depth - bit_depth->green;
235
189
shift_dec[channels] = bit_depth->green;
237
192
shift_start[channels] = row_info->bit_depth - bit_depth->blue;
238
193
shift_dec[channels] = bit_depth->blue;
243
199
shift_start[channels] = row_info->bit_depth - bit_depth->gray;
244
200
shift_dec[channels] = bit_depth->gray;
247
if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
204
if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
249
206
shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
250
207
shift_dec[channels] = bit_depth->alpha;
502
488
*(dp++) = (png_byte)(255 - *(sp++));
503
*(dp++) = (png_byte)(255 - *(sp++));
489
*dp = (png_byte)(255 - *(sp++));
492
#endif /* WRITE_16BIT */
511
#ifdef PNG_MNG_FEATURES_SUPPORTED
512
/* Undoes intrapixel differencing */
498
/* Transform the data according to the user's wishes. The order of
499
* transformations is significant.
513
501
void /* PRIVATE */
514
png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
502
png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
516
png_debug(1, "in png_do_write_intrapixel");
519
(row_info->color_type & PNG_COLOR_MASK_COLOR))
522
png_uint_32 row_width = row_info->width;
523
if (row_info->bit_depth == 8)
528
if (row_info->color_type == PNG_COLOR_TYPE_RGB)
530
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
535
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
537
*(rp) = (png_byte)((*rp - *(rp+1))&0xff);
538
*(rp+2) = (png_byte)((*(rp+2) - *(rp+1))&0xff);
541
else if (row_info->bit_depth == 16)
546
if (row_info->color_type == PNG_COLOR_TYPE_RGB)
548
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
553
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
555
png_uint_32 s0 = (*(rp ) << 8) | *(rp+1);
556
png_uint_32 s1 = (*(rp+2) << 8) | *(rp+3);
557
png_uint_32 s2 = (*(rp+4) << 8) | *(rp+5);
558
png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
559
png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
560
*(rp ) = (png_byte)((red >> 8) & 0xff);
561
*(rp+1) = (png_byte)(red & 0xff);
562
*(rp+4) = (png_byte)((blue >> 8) & 0xff);
563
*(rp+5) = (png_byte)(blue & 0xff);
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);
568
#endif /* PNG_MNG_FEATURES_SUPPORTED */
569
#endif /* PNG_WRITE_SUPPORTED */
575
#endif /* WRITE_TRANSFORMS */