16 #ifdef PNG_WRITE_SUPPORTED
18 #ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED
26 buf[0] = (
png_byte)((i >> 24) & 0xff);
27 buf[1] = (
png_byte)((i >> 16) & 0xff);
28 buf[2] = (
png_byte)((i >> 8) & 0xff);
32 #ifdef PNG_SAVE_INT_32_SUPPORTED
42 buf[0] = (
png_byte)((i >> 24) & 0xff);
43 buf[1] = (
png_byte)((i >> 16) & 0xff);
44 buf[2] = (
png_byte)((i >> 8) & 0xff);
56 buf[0] = (
png_byte)((i >> 8) & 0xff);
70 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
72 #ifdef PNG_IO_STATE_SUPPORTED
81 if (png_ptr->sig_bytes < 3)
95 #if defined(PNG_DEBUG) && (PNG_DEBUG > 0)
97 png_debug2(0,
"Writing %s chunk, length = %lu", buf, (
unsigned long)length);
103 #ifdef PNG_IO_STATE_SUPPORTED
116 png_ptr->chunk_name = chunk_name;
123 #ifdef PNG_IO_STATE_SUPPORTED
151 if (data !=
NULL && length > 0)
168 if (png_ptr ==
NULL)
return;
170 #ifdef PNG_IO_STATE_SUPPORTED
201 png_error(png_ptr,
"length exceeds PNG maxima");
221 if (!(png_ptr->zlib_state & PNG_ZLIB_IN_USE))
224 if (png_ptr->zlib_state != state)
230 if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED)
234 png_ptr->zlib_state = PNG_ZLIB_UNINITIALIZED;
238 if (ret ==
Z_OK)
switch (state)
240 # ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
241 case PNG_ZLIB_FOR_TEXT:
243 png_ptr->zlib_text_level, png_ptr->zlib_text_method,
244 png_ptr->zlib_text_window_bits,
245 png_ptr->zlib_text_mem_level, png_ptr->zlib_text_strategy);
250 case PNG_ZLIB_FOR_IDAT:
251 ret =
deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
252 png_ptr->zlib_method, png_ptr->zlib_window_bits,
253 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
258 png_error(png_ptr,
"invalid zlib state");
262 png_ptr->zlib_state = state;
270 "zlib failed to initialize compressor (");
276 pos =
png_safecat(msg,
sizeof msg, pos,
") version error");
280 pos =
png_safecat(msg,
sizeof msg, pos,
") stream error");
284 pos =
png_safecat(msg,
sizeof msg, pos,
") memory error");
288 pos =
png_safecat(msg,
sizeof msg, pos,
") unknown error");
297 png_ptr->zlib_state |= PNG_ZLIB_IN_USE;
301 png_error(png_ptr,
"zstream already in use (internal error)");
310 if (png_ptr->zlib_state & PNG_ZLIB_IN_USE)
314 png_ptr->zlib_state &= ~PNG_ZLIB_IN_USE;
343 if (png_ptr->zstream.msg)
344 err = png_ptr->zstream.msg;
346 err =
"[no zlib message]";
351 "zlib failed to reset compressor: @1(@2): @3");
356 png_warning(png_ptr,
"zstream not in use (internal error)");
359 #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
379 compression_state *
comp)
383 comp->num_output_ptr = 0;
384 comp->max_output_ptr = 0;
385 comp->output_ptr =
NULL;
387 comp->input_len = text_len;
393 return((
int)text_len);
423 png_ptr->zstream.avail_in = (
uInt)text_len;
426 png_ptr->zstream.next_in = (
Bytef *)text;
427 png_ptr->zstream.avail_out = png_ptr->zbuf_size;
428 png_ptr->zstream.next_out = png_ptr->zbuf;
439 if (png_ptr->zstream.msg !=
NULL)
440 png_error(png_ptr, png_ptr->zstream.msg);
447 if (!(png_ptr->zstream.avail_out))
450 if (comp->num_output_ptr >= comp->max_output_ptr)
454 old_max = comp->max_output_ptr;
455 comp->max_output_ptr = comp->num_output_ptr + 4;
456 if (comp->output_ptr !=
NULL)
460 old_ptr = comp->output_ptr;
462 comp->output_ptr = (
png_bytepp)png_malloc(png_ptr,
472 comp->output_ptr = (
png_bytepp)png_malloc(png_ptr,
478 comp->output_ptr[comp->num_output_ptr] =
482 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
485 comp->num_output_ptr++;
488 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
489 png_ptr->zstream.next_out = png_ptr->zbuf;
492 }
while (png_ptr->zstream.avail_in);
503 if (!(png_ptr->zstream.avail_out))
506 if (comp->num_output_ptr >= comp->max_output_ptr)
510 old_max = comp->max_output_ptr;
511 comp->max_output_ptr = comp->num_output_ptr + 4;
512 if (comp->output_ptr !=
NULL)
516 old_ptr = comp->output_ptr;
519 comp->output_ptr = (
png_bytepp)png_malloc(png_ptr,
530 comp->output_ptr = (
png_bytepp)png_malloc(png_ptr,
536 comp->output_ptr[comp->num_output_ptr] =
540 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
543 comp->num_output_ptr++;
546 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
547 png_ptr->zstream.next_out = png_ptr->zbuf;
553 if (png_ptr->zstream.msg !=
NULL)
554 png_error(png_ptr, png_ptr->zstream.msg);
562 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
564 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
565 text_len += png_ptr->zbuf_size - (
png_size_t)png_ptr->zstream.avail_out;
567 return((
int)text_len);
584 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
588 if (comp->input_len >= 2 && comp->input_len < 16384 && png_ptr->zbuf_size > 1)
596 if (comp->num_output_ptr)
597 z_cmf = comp->output_ptr[0][0];
599 z_cmf = png_ptr->zbuf[0];
601 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
603 unsigned int z_cinfo;
604 unsigned int half_z_window_size;
605 png_size_t uncompressed_text_size = comp->input_len;
607 z_cinfo = z_cmf >> 4;
608 half_z_window_size = 1 << (z_cinfo + 7);
610 while (uncompressed_text_size <= half_z_window_size &&
611 half_z_window_size >= 256)
614 half_z_window_size >>= 1;
617 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
619 if (comp->num_output_ptr)
622 if (comp->output_ptr[0][0] != z_cmf)
626 comp->output_ptr[0][0] = (
png_byte)z_cmf;
627 tmp = comp->output_ptr[0][1] & 0xe0;
628 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
629 comp->output_ptr[0][1] = (
png_byte)tmp;
637 tmp = png_ptr->zbuf[1] & 0xe0;
638 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
645 "Invalid zlib compression method or flags in non-IDAT chunk");
650 for (i = 0; i < comp->num_output_ptr; i++)
655 png_free(png_ptr, comp->output_ptr[i]);
658 if (comp->max_output_ptr != 0)
659 png_free(png_ptr, comp->output_ptr);
662 if (png_ptr->zstream.avail_out < (
png_uint_32)png_ptr->zbuf_size)
664 (
png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
694 #ifdef PNG_WRITE_16BIT_SUPPORTED
697 png_ptr->channels = 1;
break;
701 "Invalid bit depth for grayscale image");
706 #ifdef PNG_WRITE_16BIT_SUPPORTED
707 if (bit_depth != 8 && bit_depth != 16)
711 png_error(png_ptr,
"Invalid bit depth for RGB image");
713 png_ptr->channels = 3;
723 png_ptr->channels = 1;
727 png_error(png_ptr,
"Invalid bit depth for paletted image");
732 if (bit_depth != 8 && bit_depth != 16)
733 png_error(png_ptr,
"Invalid bit depth for grayscale+alpha image");
735 png_ptr->channels = 2;
739 #ifdef PNG_WRITE_16BIT_SUPPORTED
740 if (bit_depth != 8 && bit_depth != 16)
744 png_error(png_ptr,
"Invalid bit depth for RGBA image");
746 png_ptr->channels = 4;
750 png_error(png_ptr,
"Invalid image color type specified");
755 png_warning(png_ptr,
"Invalid compression type specified");
778 png_warning(png_ptr,
"Invalid filter type specified");
782 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
786 png_warning(png_ptr,
"Invalid interlace type specified");
794 png_ptr->bit_depth = (
png_byte)bit_depth;
795 png_ptr->color_type = (
png_byte)color_type;
796 png_ptr->interlaced = (
png_byte)interlace_type;
797 #ifdef PNG_MNG_FEATURES_SUPPORTED
798 png_ptr->filter_type = (
png_byte)filter_type;
800 png_ptr->compression_type = (
png_byte)compression_type;
801 png_ptr->width =
width;
804 png_ptr->pixel_depth = (
png_byte)(bit_depth * png_ptr->channels);
805 png_ptr->rowbytes =
PNG_ROWBYTES(png_ptr->pixel_depth, width);
807 png_ptr->usr_width = png_ptr->width;
808 png_ptr->usr_bit_depth = png_ptr->bit_depth;
809 png_ptr->usr_channels = png_ptr->channels;
816 buf[10] = (
png_byte)compression_type;
824 png_ptr->zstream.zalloc = png_zalloc;
826 png_ptr->zstream.opaque = (
voidpf)png_ptr;
828 if (!(png_ptr->do_filter))
831 png_ptr->bit_depth < 8)
851 png_ptr->zlib_mem_level = 8;
854 png_ptr->zlib_window_bits = 15;
857 png_ptr->zlib_method = 8;
865 png_ptr->zlib_text_level = png_ptr->zlib_level;
868 png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
871 png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
874 png_ptr->zlib_text_method = png_ptr->zlib_method;
877 png_ptr->zlib_text_level = png_ptr->zlib_level;
878 png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
879 png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
880 png_ptr->zlib_text_method = png_ptr->zlib_method;
885 png_ptr->zlib_state = PNG_ZLIB_UNINITIALIZED;
908 num_pal == 0) || num_pal > 256)
912 png_error(png_ptr,
"Invalid number of colors in palette");
917 png_warning(png_ptr,
"Invalid number of colors in palette");
925 "Ignoring request to write a PLTE chunk in grayscale PNG");
931 png_debug1(3,
"num_palette = %d", png_ptr->num_palette);
934 #ifdef PNG_POINTER_INDEXING_SUPPORTED
936 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
938 buf[0] = pal_ptr->red;
939 buf[1] = pal_ptr->green;
940 buf[2] = pal_ptr->blue;
950 for (i = 0; i < num_pal; i++)
952 buf[0] = pal_ptr[
i].red;
953 buf[1] = pal_ptr[
i].green;
954 buf[2] = pal_ptr[
i].blue;
969 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
976 unsigned int z_cmf = data[0];
978 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
986 png_ptr->height < 16384 && png_ptr->width < 16384)
994 unsigned int z_cinfo;
995 unsigned int half_z_window_size;
996 png_uint_32 uncompressed_idat_size = png_ptr->height *
998 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
1004 if (png_ptr->interlaced)
1005 uncompressed_idat_size += ((png_ptr->height + 7)/8) *
1006 (png_ptr->bit_depth < 8 ? 12 : 6);
1008 z_cinfo = z_cmf >> 4;
1009 half_z_window_size = 1 << (z_cinfo + 7);
1011 while (uncompressed_idat_size <= half_z_window_size &&
1012 half_z_window_size >= 256)
1015 half_z_window_size >>= 1;
1018 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
1020 if (data[0] != z_cmf)
1024 tmp = data[1] & 0xe0;
1025 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
1033 "Invalid zlib compression method or flags in IDAT");
1044 png_ptr->zstream.next_out = png_ptr->zbuf;
1045 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
1058 #ifdef PNG_WRITE_gAMA_SUPPORTED
1073 #ifdef PNG_WRITE_sRGB_SUPPORTED
1084 "Invalid sRGB rendering intent specified");
1091 #ifdef PNG_WRITE_iCCP_SUPPORTED
1099 compression_state
comp;
1100 int embedded_profile_len = 0;
1104 comp.num_output_ptr = 0;
1105 comp.max_output_ptr = 0;
1106 comp.output_ptr =
NULL;
1114 png_warning(png_ptr,
"Unknown compression type in iCCP chunk");
1116 if (profile ==
NULL)
1119 if (profile_len > 3)
1120 embedded_profile_len =
1126 if (embedded_profile_len < 0)
1129 "Embedded profile length in iCCP chunk is negative");
1135 if (profile_len < embedded_profile_len)
1138 "Embedded profile length too large in iCCP chunk");
1144 if (profile_len > embedded_profile_len)
1147 "Truncating profile to actual length in iCCP chunk");
1149 profile_len = embedded_profile_len;
1160 new_name[name_len + 1] = 0x00;
1167 comp.input_len = profile_len;
1176 #ifdef PNG_WRITE_sPLT_SUPPORTED
1185 png_size_t palette_size = entry_size * spalette->nentries;
1187 #ifndef PNG_POINTER_INDEXING_SUPPORTED
1206 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1207 for (ep = spalette->entries; ep<spalette->
entries + spalette->nentries; ep++)
1209 if (spalette->depth == 8)
1230 ep=spalette->entries;
1231 for (i = 0; i>spalette->nentries; i++)
1233 if (spalette->depth == 8)
1260 #ifdef PNG_WRITE_sBIT_SUPPORTED
1276 png_ptr->usr_bit_depth);
1278 if (sbit->red == 0 || sbit->red > maxbits ||
1279 sbit->green == 0 || sbit->green > maxbits ||
1280 sbit->blue == 0 || sbit->blue > maxbits)
1282 png_warning(png_ptr,
"Invalid sBIT depth specified");
1287 buf[1] = sbit->green;
1288 buf[2] = sbit->blue;
1294 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
1296 png_warning(png_ptr,
"Invalid sBIT depth specified");
1300 buf[0] = sbit->gray;
1306 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
1308 png_warning(png_ptr,
"Invalid sBIT depth specified");
1312 buf[size++] = sbit->alpha;
1319 #ifdef PNG_WRITE_cHRM_SUPPORTED
1332 #ifdef PNG_CHECK_cHRM_SUPPORTED
1334 green_x, green_y, blue_x, blue_y))
1354 #ifdef PNG_WRITE_tRNS_SUPPORTED
1366 if (num_trans <= 0 || num_trans > (
int)png_ptr->num_palette)
1368 png_warning(png_ptr,
"Invalid number of transparent colors specified");
1379 if (tran->gray >= (1 << png_ptr->bit_depth))
1382 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1397 #ifdef PNG_WRITE_16BIT_SUPPORTED
1398 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1400 if (buf[0] | buf[2] | buf[4])
1404 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1413 png_warning(png_ptr,
"Can't write tRNS with an alpha channel");
1418 #ifdef PNG_WRITE_bKGD_SUPPORTED
1431 (png_ptr->num_palette ||
1434 back->index >= png_ptr->num_palette)
1436 png_warning(png_ptr,
"Invalid background palette index");
1440 buf[0] = back->index;
1449 #ifdef PNG_WRITE_16BIT_SUPPORTED
1450 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1452 if (buf[0] | buf[2] | buf[4])
1456 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1466 if (back->gray >= (1 << png_ptr->bit_depth))
1469 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1480 #ifdef PNG_WRITE_hIST_SUPPORTED
1490 if (num_hist > (
int)png_ptr->num_palette)
1492 png_debug2(3,
"num_hist = %d, num_palette = %d", num_hist,
1493 png_ptr->num_palette);
1495 png_warning(png_ptr,
"Invalid number of histogram entries specified");
1501 for (i = 0; i < num_hist; i++)
1511 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1512 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
1542 png_debug1(2,
"Keyword to be checked is '%s'", key);
1546 if (*new_key ==
NULL)
1548 png_warning(png_ptr,
"Out of memory while procesing keyword");
1553 for (ikp = key, dp = *new_key; *ikp !=
'\0'; ikp++, dp++)
1574 kp = *new_key + key_len - 1;
1577 png_warning(png_ptr,
"trailing spaces removed from keyword");
1590 png_warning(png_ptr,
"leading spaces removed from keyword");
1599 png_debug1(2,
"Checking for multiple internal spaces in '%s'", kp);
1602 for (kflag = 0, dp = *new_key; *kp !=
'\0'; kp++)
1604 if (*kp ==
' ' && kflag == 0)
1610 else if (*kp ==
' ')
1624 png_warning(png_ptr,
"extra interior spaces removed from keyword");
1634 png_warning(png_ptr,
"keyword length must be 1 - 79 characters");
1635 (*new_key)[79] =
'\0';
1643 #ifdef PNG_WRITE_tEXt_SUPPORTED
1657 if (text ==
NULL || *text ==
'\0')
1684 #ifdef PNG_WRITE_zTXt_SUPPORTED
1693 compression_state
comp;
1697 comp.num_output_ptr = 0;
1698 comp.max_output_ptr = 0;
1699 comp.output_ptr =
NULL;
1738 comp.input_len = text_len;
1746 #ifdef PNG_WRITE_iTXt_SUPPORTED
1752 png_size_t lang_len, key_len, lang_key_len, text_len;
1756 compression_state
comp;
1760 comp.num_output_ptr = 0;
1761 comp.max_output_ptr = 0;
1762 comp.output_ptr =
NULL;
1770 png_warning(png_ptr,
"Empty language field in iTXt chunk");
1775 if (lang_key ==
NULL)
1839 #ifdef PNG_WRITE_oFFs_SUPPORTED
1850 png_warning(png_ptr,
"Unrecognized unit type for oFFs chunk");
1859 #ifdef PNG_WRITE_pCAL_SUPPORTED
1866 png_size_t purpose_len, units_len, total_len;
1872 png_debug1(1,
"in png_write_pCAL (%d parameters)", nparams);
1875 png_warning(png_ptr,
"Unrecognized equation type for pCAL chunk");
1878 png_debug1(3,
"pCAL purpose length = %d", (
int)purpose_len);
1879 units_len =
png_strlen(units) + (nparams == 0 ? 0 : 1);
1880 png_debug1(3,
"pCAL units length = %d", (
int)units_len);
1881 total_len = purpose_len + units_len + 10;
1889 for (i = 0; i < nparams; i++)
1891 params_len[
i] =
png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1892 png_debug2(3,
"pCAL parameter %d length = %lu", i,
1893 (
unsigned long)params_len[i]);
1894 total_len += params_len[
i];
1897 png_debug1(3,
"pCAL total length = %d", (
int)total_len);
1909 for (i = 0; i < nparams; i++)
1919 #ifdef PNG_WRITE_sCAL_SUPPORTED
1932 total_len = wlen + hlen + 2;
1936 png_warning(png_ptr,
"Can't write sCAL (buffer too small)");
1944 png_debug1(3,
"sCAL total length = %u", (
unsigned int)total_len);
1949 #ifdef PNG_WRITE_pHYs_SUPPORTED
1961 png_warning(png_ptr,
"Unrecognized unit type for pHYs chunk");
1971 #ifdef PNG_WRITE_tIME_SUPPORTED
1982 if (mod_time->month > 12 || mod_time->month < 1 ||
1983 mod_time->day > 31 || mod_time->day < 1 ||
1984 mod_time->hour > 23 || mod_time->second > 60)
1986 png_warning(png_ptr,
"Invalid time specified for tIME chunk");
1991 buf[2] = mod_time->month;
1992 buf[3] = mod_time->day;
1993 buf[4] = mod_time->hour;
1994 buf[5] = mod_time->minute;
1995 buf[6] = mod_time->second;
2005 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2022 int usr_pixel_depth;
2026 usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth;
2027 buf_size =
PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1;
2030 png_ptr->transformed_pixel_depth = png_ptr->pixel_depth;
2031 png_ptr->maximum_pixel_depth = (
png_byte)usr_pixel_depth;
2034 png_ptr->row_buf = (
png_bytep)png_malloc(png_ptr, buf_size);
2038 #ifdef PNG_WRITE_FILTER_SUPPORTED
2042 png_ptr->sub_row = (
png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
2051 png_ptr->prev_row = (
png_bytep)png_calloc(png_ptr, buf_size);
2055 png_ptr->up_row = (
png_bytep)png_malloc(png_ptr,
2056 png_ptr->rowbytes + 1);
2063 png_ptr->avg_row = (
png_bytep)png_malloc(png_ptr,
2064 png_ptr->rowbytes + 1);
2071 png_ptr->paeth_row = (
png_bytep)png_malloc(png_ptr,
2072 png_ptr->rowbytes + 1);
2079 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2081 if (png_ptr->interlaced)
2085 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
2086 png_pass_ystart[0]) / png_pass_yinc[0];
2088 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
2089 png_pass_start[0]) / png_pass_inc[0];
2094 png_ptr->num_rows = png_ptr->height;
2095 png_ptr->usr_width = png_ptr->width;
2102 png_ptr->num_rows = png_ptr->height;
2103 png_ptr->usr_width = png_ptr->width;
2107 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
2108 png_ptr->zstream.next_out = png_ptr->zbuf;
2115 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2133 png_debug(1,
"in png_write_finish_row");
2136 png_ptr->row_number++;
2139 if (png_ptr->row_number < png_ptr->num_rows)
2142 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2144 if (png_ptr->interlaced)
2146 png_ptr->row_number = 0;
2159 if (png_ptr->pass >= 7)
2162 png_ptr->usr_width = (png_ptr->width +
2163 png_pass_inc[png_ptr->pass] - 1 -
2164 png_pass_start[png_ptr->pass]) /
2165 png_pass_inc[png_ptr->pass];
2167 png_ptr->num_rows = (png_ptr->height +
2168 png_pass_yinc[png_ptr->pass] - 1 -
2169 png_pass_ystart[png_ptr->pass]) /
2170 png_pass_yinc[png_ptr->pass];
2175 }
while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
2180 if (png_ptr->pass < 7)
2182 if (png_ptr->prev_row !=
NULL)
2185 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
2203 if (!(png_ptr->zstream.avail_out))
2206 png_ptr->zstream.next_out = png_ptr->zbuf;
2207 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
2213 if (png_ptr->zstream.msg !=
NULL)
2214 png_error(png_ptr, png_ptr->zstream.msg);
2222 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
2225 png_ptr->zstream.avail_out);
2229 png_ptr->zstream.data_type =
Z_BINARY;
2232 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2251 png_debug(1,
"in png_do_write_interlace");
2257 switch (row_info->pixel_depth)
2273 for (i = png_pass_start[pass]; i < row_width;
2274 i += png_pass_inc[pass])
2277 value = (
int)(*sp >> (7 - (
int)(i & 0x07))) & 0x01;
2278 d |= (value << shift);
2311 for (i = png_pass_start[pass]; i < row_width;
2312 i += png_pass_inc[pass])
2315 value = (*sp >> ((3 - (
int)(i & 0x03)) << 1)) & 0x03;
2316 d |= (value << shift);
2347 for (i = png_pass_start[pass]; i < row_width;
2348 i += png_pass_inc[pass])
2351 value = (*sp >> ((1 - (
int)(i & 0x01)) << 2)) & 0x0f;
2352 d |= (value << shift);
2382 pixel_bytes = (row_info->pixel_depth >> 3);
2385 for (i = png_pass_start[pass]; i < row_width;
2386 i += png_pass_inc[pass])
2402 row_info->width = (row_info->width +
2403 png_pass_inc[pass] - 1 -
2404 png_pass_start[pass]) /
2407 row_info->rowbytes =
PNG_ROWBYTES(row_info->pixel_depth,
2420 #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
2421 #define PNG_HISHIFT 10
2422 #define PNG_LOMASK ((png_uint_32)0xffffL)
2423 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
2428 #ifdef PNG_WRITE_FILTER_SUPPORTED
2431 png_byte filter_to_do = png_ptr->do_filter;
2433 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2434 int num_p_filters = png_ptr->num_prev_filters;
2437 png_debug(1,
"in png_write_find_filter");
2439 #ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2448 bpp = (row_info->pixel_depth + 7) >> 3;
2450 prev_row = png_ptr->prev_row;
2452 best_row = png_ptr->row_buf;
2453 #ifdef PNG_WRITE_FILTER_SUPPORTED
2492 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
2495 sum += (v < 128) ? v : 256 - v;
2498 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2507 for (j = 0; j < num_p_filters; j++)
2511 sumlo = (sumlo * png_ptr->filter_weights[
j]) >>
2514 sumhi = (sumhi * png_ptr->filter_weights[
j]) >>
2546 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2552 for (lp = row_buf + 1; i < row_bytes;
2553 i++, rp++, lp++, dp++)
2558 best_row = png_ptr->sub_row;
2568 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2580 for (j = 0; j < num_p_filters; j++)
2584 lmlo = (lmlo * png_ptr->inv_filter_weights[
j]) >>
2587 lmhi = (lmhi * png_ptr->inv_filter_weights[
j]) >>
2606 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2611 sum += (v < 128) ? v : 256 - v;
2614 for (lp = row_buf + 1; i < row_bytes;
2615 i++, rp++, lp++, dp++)
2617 v = *dp = (
png_byte)(((
int)*rp - (
int)*lp) & 0xff);
2619 sum += (v < 128) ? v : 256 - v;
2625 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2633 for (j = 0; j < num_p_filters; j++)
2637 sumlo = (sumlo * png_ptr->inv_filter_weights[
j]) >>
2640 sumhi = (sumhi * png_ptr->inv_filter_weights[
j]) >>
2662 best_row = png_ptr->sub_row;
2672 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2673 pp = prev_row + 1; i < row_bytes;
2674 i++, rp++, pp++, dp++)
2679 best_row = png_ptr->up_row;
2690 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2698 for (j = 0; j < num_p_filters; j++)
2702 lmlo = (lmlo * png_ptr->inv_filter_weights[
j]) >>
2705 lmhi = (lmhi * png_ptr->inv_filter_weights[
j]) >>
2724 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2725 pp = prev_row + 1; i < row_bytes; i++)
2727 v = *dp++ = (
png_byte)(((
int)*rp++ - (
int)*pp++) & 0xff);
2729 sum += (v < 128) ? v : 256 - v;
2735 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2743 for (j = 0; j < num_p_filters; j++)
2747 sumlo = (sumlo * png_ptr->filter_weights[
j]) >>
2750 sumhi = (sumhi * png_ptr->filter_weights[
j]) >>
2772 best_row = png_ptr->up_row;
2782 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2783 pp = prev_row + 1; i < bpp; i++)
2785 *dp++ = (
png_byte)(((
int)*rp++ - ((
int)*pp++ / 2)) & 0xff);
2788 for (lp = row_buf + 1; i < row_bytes; i++)
2790 *dp++ = (
png_byte)(((
int)*rp++ - (((
int)*pp++ + (
int)*lp++) / 2))
2793 best_row = png_ptr->avg_row;
2803 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2811 for (j = 0; j < num_p_filters; j++)
2815 lmlo = (lmlo * png_ptr->inv_filter_weights[
j]) >>
2818 lmhi = (lmhi * png_ptr->inv_filter_weights[
j]) >>
2837 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2838 pp = prev_row + 1; i < bpp; i++)
2840 v = *dp++ = (
png_byte)(((
int)*rp++ - ((
int)*pp++ / 2)) & 0xff);
2842 sum += (v < 128) ? v : 256 - v;
2845 for (lp = row_buf + 1; i < row_bytes; i++)
2848 (
png_byte)(((
int)*rp++ - (((
int)*pp++ + (
int)*lp++) / 2)) & 0xff);
2850 sum += (v < 128) ? v : 256 - v;
2856 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2864 for (j = 0; j < num_p_filters; j++)
2868 sumlo = (sumlo * png_ptr->filter_weights[
j]) >>
2871 sumhi = (sumhi * png_ptr->filter_weights[
j]) >>
2893 best_row = png_ptr->avg_row;
2903 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2904 pp = prev_row + 1; i < bpp; i++)
2906 *dp++ = (
png_byte)(((
int)*rp++ - (
int)*pp++) & 0xff);
2909 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2911 int a,
b,
c, pa, pb, pc,
p;
2925 pa = p < 0 ? -p :
p;
2926 pb = pc < 0 ? -pc : pc;
2927 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2930 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b :
c;
2932 *dp++ = (
png_byte)(((
int)*rp++ -
p) & 0xff);
2934 best_row = png_ptr->paeth_row;
2944 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2952 for (j = 0; j < num_p_filters; j++)
2956 lmlo = (lmlo * png_ptr->inv_filter_weights[
j]) >>
2959 lmhi = (lmhi * png_ptr->inv_filter_weights[
j]) >>
2978 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2979 pp = prev_row + 1; i < bpp; i++)
2981 v = *dp++ = (
png_byte)(((
int)*rp++ - (
int)*pp++) & 0xff);
2983 sum += (v < 128) ? v : 256 - v;
2986 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2988 int a,
b,
c, pa, pb, pc,
p;
2994 #ifndef PNG_SLOW_PAETH
3002 pa = p < 0 ? -p :
p;
3003 pb = pc < 0 ? -pc : pc;
3004 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
3006 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b :
c;
3013 if (pa <= pb && pa <= pc)
3023 v = *dp++ = (
png_byte)(((
int)*rp++ - p) & 0xff);
3025 sum += (v < 128) ? v : 256 - v;
3031 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
3039 for (j = 0; j < num_p_filters; j++)
3043 sumlo = (sumlo * png_ptr->filter_weights[
j]) >>
3046 sumhi = (sumhi * png_ptr->filter_weights[
j]) >>
3067 best_row = png_ptr->paeth_row;
3075 #ifdef PNG_WRITE_FILTER_SUPPORTED
3076 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
3078 if (png_ptr->num_prev_filters > 0)
3082 for (j = 1; j < num_p_filters; j++)
3084 png_ptr->prev_filters[
j] = png_ptr->prev_filters[j - 1];
3087 png_ptr->prev_filters[
j] = best_row[0];
3099 png_debug(1,
"in png_write_filtered_row");
3101 png_debug1(2,
"filter = %d", filtered_row[0]);
3104 png_ptr->zstream.next_in = filtered_row;
3105 png_ptr->zstream.avail_in = 0;
3119 if (png_ptr->zstream.avail_in == 0)
3130 png_ptr->zstream.avail_in = (
uInt)avail;
3141 if (png_ptr->zstream.msg !=
NULL)
3142 png_error(png_ptr, png_ptr->zstream.msg);
3149 if (!(png_ptr->zstream.avail_out))
3155 }
while (avail > 0 || png_ptr->zstream.avail_in > 0);
3158 if (png_ptr->prev_row !=
NULL)
3162 tptr = png_ptr->prev_row;
3163 png_ptr->prev_row = png_ptr->row_buf;
3164 png_ptr->row_buf = tptr;
3170 #ifdef PNG_WRITE_FLUSH_SUPPORTED
3171 png_ptr->flush_rows++;
3173 if (png_ptr->flush_dist > 0 &&
3174 png_ptr->flush_rows >= png_ptr->flush_dist)
static void png_write_chunk_header(png_structp png_ptr, png_uint_32 chunk_name, png_uint_32 length)
#define PNG_FILTER_VALUE_AVG
#define PNG_TEXT_COMPRESSION_LAST
void png_write_start_row(png_structp png_ptr)
void png_zfree(voidpf png_ptr, voidpf ptr)
#define PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
void png_write_sPLT(png_structp png_ptr, png_const_sPLT_tp spalette)
void PNGAPI png_write_chunk_start(png_structp png_ptr, png_const_bytep chunk_string, png_uint_32 length)
void png_write_PLTE(png_structp png_ptr, png_const_colorp palette, png_uint_32 num_pal)
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
PNG_CONST png_time FAR * png_const_timep
#define PNG_FILTER_HEURISTIC_WEIGHTED
void png_write_sBIT(png_structp png_ptr, png_const_color_8p sbit, int color_type)
png_size_t FAR * png_size_tp
void png_warning_parameter(png_warning_parameters p, int number, png_const_charp string)
void PNGAPI png_save_int_32(png_bytep buf, png_int_32 i)
#define PNG_INTERLACE_NONE
#define PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS
#define PNG_COLOR_TYPE_RGB
#define PNG_COLOR_TYPE_GRAY_ALPHA
void png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
#define PNG_INTERLACE_ADAM7
#define PNG_COLOR_TYPE_PALETTE
#define PNG_FILTER_VALUE_PAETH
png_error(png_ptr,"Missing IHDR before iCCP")
#define PNG_IO_CHUNK_DATA
void png_write_zTXt(png_structp png_ptr, png_const_charp key, png_const_charp text, png_size_t text_len, int compression)
void PNGAPI png_write_chunk(png_structp png_ptr, png_const_bytep chunk_string, png_const_bytep data, png_size_t length)
EGLSurface EGLint EGLint EGLint EGLint height
GLboolean GLboolean GLboolean GLboolean a
#define PNG_COLOR_TYPE_RGB_ALPHA
PNG_CONST png_color FAR * png_const_colorp
EGLImageKHR EGLint * name
static int comp(const void *a, const void *b)
PNG_CONST char FAR * png_const_charp
int ZEXPORT deflateEnd(z_streamp strm)
void png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset, int unit_type)
return Display return Display Bool Bool int d
void png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
#define PNG_NUMBER_FORMAT_02x
PNG_CONST png_color_8 FAR * png_const_color_8p
#define Z_DEFAULT_COMPRESSION
#define PNG_sRGB_INTENT_LAST
#define PNG_WARNING_PARAMETERS(p)
#define PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL
#define PNG_FLAG_ZTXT_CUSTOM_STRATEGY
void png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, png_const_charp units, png_charpp params)
size_t png_safecat(png_charp buffer, size_t bufsize, size_t pos, png_const_charp string)
png_size_t png_alloc_size_t
void png_write_data(png_structp png_ptr, png_const_bytep data, png_size_t length)
void png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit, int unit_type)
#define PNG_MNG_FEATURES_SUPPORTED
png_byte FAR *FAR * png_bytepp
#define PNG_TEXT_COMPRESSION_NONE
GLenum GLenum GLvoid * row
png_struct FAR * png_structp
void png_write_tIME(png_structp png_ptr, png_const_timep mod_time)
void png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x, png_fixed_point blue_y)
void png_write_tEXt(png_structp png_ptr, png_const_charp key, png_const_charp text, png_size_t text_len)
static void png_write_complete_chunk(png_structp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
void png_warning_parameter_signed(png_warning_parameters p, int number, int format, png_int_32 value)
#define PNG_COLOR_MASK_COLOR
static void png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row, png_size_t row_bytes)
#define PNG_ITXT_COMPRESSION_NONE
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
#define PNG_CHUNK_FROM_STRING(s)
void PNGAPI png_write_flush(png_structp png_ptr)
EGLSurface EGLint EGLint EGLint width
void png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
unsigned short png_uint_16
#define PNG_FILTER_VALUE_SUB
void png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int compression_type, int filter_type, int interlace_type)
#define PNG_COLOR_TYPE_GRAY
png_sPLT_entry FAR * png_sPLT_entryp
void PNGAPI png_warning(png_structp png_ptr, png_const_charp warning_message)
int png_check_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x, png_fixed_point blue_y)
#define PNG_NUMBER_FORMAT_d
#define PNG_FLAG_ZTXT_CUSTOM_METHOD
PNG_CONST png_byte FAR * png_const_bytep
#define png_debug1(l, m, p1)
#define PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
#define PNG_FLAG_ZTXT_CUSTOM_LEVEL
#define Z_DEFAULT_STRATEGY
GLclampf GLclampf GLclampf alpha
#define PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS
#define PNG_EQUATION_LAST
#define PNG_RESOLUTION_LAST
static void png_zlib_release(png_structp png_ptr)
PNG_CONST png_color_16 FAR * png_const_color_16p
void png_write_iCCP(png_structp png_ptr, png_const_charp name, int compression_type, png_const_charp profile, int profile_len)
#define PNG_FILTER_VALUE_UP
int ZEXPORT deflate(z_streamp strm, int flush)
void PNGAPI png_save_uint_32(png_bytep buf, png_uint_32 i)
#define PNG_FLAG_ZLIB_CUSTOM_METHOD
png_size_t png_check_keyword(png_structp png_ptr, png_const_charp key, png_charpp new_key)
#define PNG_FLAG_MNG_FILTER_64
#define PNG_FLAG_ZLIB_CUSTOM_LEVEL
static void png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
void PNGAPI png_write_chunk_end(png_structp png_ptr)
#define PNG_FILTER_TYPE_BASE
GLenum GLenum GLenum input
EGLSurface EGLint void ** value
#define PNG_COLOR_MASK_ALPHA
png_row_info FAR * png_row_infop
#define PNG_COMPRESSION_TYPE_BASE
void png_write_sRGB(png_structp png_ptr, int srgb_intent)
GLenum GLuint GLsizei const GLchar * buf
void PNGAPI png_write_sig(png_structp png_ptr)
#define PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL
void png_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha, png_const_color_16p tran, int num_trans, int color_type)
void png_write_IEND(png_structp png_ptr)
void png_write_iTXt(png_structp png_ptr, int compression, png_const_charp key, png_const_charp lang, png_const_charp lang_key, png_const_charp text)
png_int_32 png_fixed_point
char FAR *FAR * png_charpp
GLdouble GLdouble GLdouble b
#define PNG_FILTER_VALUE_NONE
void png_calculate_crc(png_structp png_ptr, png_const_bytep ptr, png_size_t length)
static void png_zlib_claim(png_structp png_ptr, png_uint_32 state)
void PNGAPI png_save_uint_16(png_bytep buf, unsigned int i)
#define PNG_INTRAPIXEL_DIFFERENCING
#define PNG_FLAG_MNG_EMPTY_PLTE
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
void png_write_hIST(png_structp png_ptr, png_const_uint_16p hist, int num_hist)
PNG_CONST png_sPLT_t FAR * png_const_sPLT_tp
int ZEXPORT deflateReset(z_streamp strm)
void png_write_finish_row(png_structp png_ptr)
void PNGAPI png_free(png_structp png_ptr, png_voidp ptr)
#define PNG_ROWBYTES(pixel_bits, width)
#define PNG_HAVE_PNG_SIGNATURE
void png_write_sCAL_s(png_structp png_ptr, int unit, png_const_charp width, png_const_charp height)
void png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
static int png_text_compress(png_structp png_ptr, png_const_charp text, png_size_t text_len, int compression, compression_state *comp)
void png_formatted_warning(png_structp png_ptr, png_warning_parameters p, png_const_charp message)
PNG_CONST png_uint_16 FAR * png_const_uint_16p
void png_reset_crc(png_structp png_ptr)
#define PNG_CSTRING_FROM_CHUNK(s, c)
void png_warning_parameter_unsigned(png_warning_parameters p, int number, int format, png_alloc_size_t value)
void PNGAPI png_write_chunk_data(png_structp png_ptr, png_const_bytep data, png_size_t length)
#define png_debug2(l, m, p1, p2)
void png_write_bKGD(png_structp png_ptr, png_const_color_16p back, int color_type)
#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY