15#ifdef PNG_WRITE_SUPPORTED
16#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
18#ifdef PNG_WRITE_PACK_SUPPORTED
24png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
26 png_debug(1,
"in png_do_pack");
28 if (row_info->bit_depth == 8 &&
29 row_info->channels == 1)
31 switch ((
int)bit_depth)
38 png_uint_32 row_width = row_info->width;
45 for (i = 0; i < row_width; i++)
76 png_uint_32 row_width = row_info->width;
83 for (i = 0; i < row_width; i++)
87 value = (png_byte)(*sp & 0x03);
88 v |= (value << shift);
116 png_uint_32 row_width = row_info->width;
123 for (i = 0; i < row_width; i++)
127 value = (png_byte)(*sp & 0x0f);
128 v |= (value << shift);
154 row_info->bit_depth = (png_byte)bit_depth;
155 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
156 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
162#ifdef PNG_WRITE_SHIFT_SUPPORTED
171png_do_shift(png_row_infop row_info, png_bytep row,
172 png_const_color_8p bit_depth)
174 png_debug(1,
"in png_do_shift");
176 if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
178 int shift_start[4], shift_dec[4];
179 unsigned int channels = 0;
181 if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
183 shift_start[channels] = row_info->bit_depth - bit_depth->red;
184 shift_dec[channels] = bit_depth->red;
187 shift_start[channels] = row_info->bit_depth - bit_depth->green;
188 shift_dec[channels] = bit_depth->green;
191 shift_start[channels] = row_info->bit_depth - bit_depth->blue;
192 shift_dec[channels] = bit_depth->blue;
198 shift_start[channels] = row_info->bit_depth - bit_depth->gray;
199 shift_dec[channels] = bit_depth->gray;
203 if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
205 shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
206 shift_dec[channels] = bit_depth->alpha;
211 if (row_info->bit_depth < 8)
216 size_t row_bytes = row_info->rowbytes;
218 if (bit_depth->gray == 1 && row_info->bit_depth == 2)
221 else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
227 for (i = 0; i < row_bytes; i++, bp++)
235 for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
241 out |= (v >> (-j)) & mask;
244 *bp = (png_byte)(out & 0xff);
248 else if (row_info->bit_depth == 8)
252 png_uint_32 istop = channels * row_info->width;
254 for (i = 0; i < istop; i++, bp++)
256 unsigned int c = i%channels;
263 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
272 *bp = (png_byte)(out & 0xff);
280 png_uint_32 istop = channels * row_info->width;
282 for (bp = row, i = 0; i < istop; i++)
284 unsigned int c = i%channels;
286 unsigned int value, v;
288 v = png_get_uint_16(bp);
291 for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
299 *bp++ = (png_byte)((value >> 8) & 0xff);
300 *bp++ = (png_byte)(value & 0xff);
307#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
309png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
311 png_debug(1,
"in png_do_write_swap_alpha");
314 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
316 if (row_info->bit_depth == 8)
321 png_uint_32 row_width = row_info->width;
323 for (i = 0, sp = dp = row; i < row_width; i++)
325 png_byte save = *(sp++);
333#ifdef PNG_WRITE_16BIT_SUPPORTED
339 png_uint_32 row_width = row_info->width;
341 for (i = 0, sp = dp = row; i < row_width; i++)
359 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
361 if (row_info->bit_depth == 8)
366 png_uint_32 row_width = row_info->width;
368 for (i = 0, sp = dp = row; i < row_width; i++)
370 png_byte save = *(sp++);
376#ifdef PNG_WRITE_16BIT_SUPPORTED
382 png_uint_32 row_width = row_info->width;
384 for (i = 0, sp = dp = row; i < row_width; i++)
401#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
403png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
405 png_debug(1,
"in png_do_write_invert_alpha");
408 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
410 if (row_info->bit_depth == 8)
415 png_uint_32 row_width = row_info->width;
417 for (i = 0, sp = dp = row; i < row_width; i++)
425 *dp = (png_byte)(255 - *(sp++));
429#ifdef PNG_WRITE_16BIT_SUPPORTED
435 png_uint_32 row_width = row_info->width;
437 for (i = 0, sp = dp = row; i < row_width; i++)
448 *(dp++) = (png_byte)(255 - *(sp++));
449 *dp = (png_byte)(255 - *(sp++));
455 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
457 if (row_info->bit_depth == 8)
462 png_uint_32 row_width = row_info->width;
464 for (i = 0, sp = dp = row; i < row_width; i++)
467 *(dp++) = (png_byte)(255 - *(sp++));
471#ifdef PNG_WRITE_16BIT_SUPPORTED
477 png_uint_32 row_width = row_info->width;
479 for (i = 0, sp = dp = row; i < row_width; i++)
486 *(dp++) = (png_byte)(255 - *(sp++));
487 *dp = (png_byte)(255 - *(sp++));
500png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
502 png_debug(1,
"in png_do_write_transformations");
507#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
508 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
509 if (png_ptr->write_user_transform_fn != NULL)
510 (*(png_ptr->write_user_transform_fn))
520 png_ptr->row_buf + 1);
523#ifdef PNG_WRITE_FILLER_SUPPORTED
524 if ((png_ptr->transformations & PNG_FILLER) != 0)
525 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
526 !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
529#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
530 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
531 png_do_packswap(row_info, png_ptr->row_buf + 1);
534#ifdef PNG_WRITE_PACK_SUPPORTED
535 if ((png_ptr->transformations & PNG_PACK) != 0)
536 png_do_pack(row_info, png_ptr->row_buf + 1,
537 (png_uint_32)png_ptr->bit_depth);
540#ifdef PNG_WRITE_SWAP_SUPPORTED
541# ifdef PNG_16BIT_SUPPORTED
542 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
543 png_do_swap(row_info, png_ptr->row_buf + 1);
547#ifdef PNG_WRITE_SHIFT_SUPPORTED
548 if ((png_ptr->transformations & PNG_SHIFT) != 0)
549 png_do_shift(row_info, png_ptr->row_buf + 1,
553#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
554 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
555 png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
558#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
559 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
560 png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
563#ifdef PNG_WRITE_BGR_SUPPORTED
564 if ((png_ptr->transformations & PNG_BGR) != 0)
565 png_do_bgr(row_info, png_ptr->row_buf + 1);
568#ifdef PNG_WRITE_INVERT_SUPPORTED
569 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
570 png_do_invert(row_info, png_ptr->row_buf + 1);