13 #ifdef PNG_WRITE_SUPPORTED
61 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
66 if (png_ptr->sig_bytes < 3)
67 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
83 if (png_ptr == NULL)
return;
101 if (png_ptr == NULL)
return;
123 if (png_ptr == NULL)
return;
140 if (png_ptr == NULL)
return;
148 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
184 return((
int)text_len);
189 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
191 png_snprintf(msg, 50,
"Unknown compression type %d", compression);
214 png_ptr->zstream.avail_in = (
uInt)text_len;
215 png_ptr->zstream.next_in = (
Bytef *)text;
216 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
217 png_ptr->zstream.next_out = (
Bytef *)png_ptr->zbuf;
227 if (png_ptr->zstream.msg != NULL)
228 png_error(png_ptr, png_ptr->zstream.msg);
233 if (!(png_ptr->zstream.avail_out))
269 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
270 png_ptr->zstream.next_out = png_ptr->zbuf;
273 }
while (png_ptr->zstream.avail_in);
284 if (!(png_ptr->zstream.avail_out))
321 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
322 png_ptr->zstream.next_out = png_ptr->zbuf;
328 if (png_ptr->zstream.msg != NULL)
329 png_error(png_ptr, png_ptr->zstream.msg);
337 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
338 text_len += png_ptr->zbuf_size - (
png_size_t)png_ptr->zstream.avail_out;
340 return((
int)text_len);
369 if (png_ptr->zstream.avail_out < (
png_uint_32)png_ptr->zbuf_size)
371 (
png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
375 png_ptr->zstream.data_type =
Z_BINARY;
388 #ifdef PNG_USE_LOCAL_ARRAYS
406 case 16: png_ptr->channels = 1;
break;
407 default:
png_error(png_ptr,
"Invalid bit depth for grayscale image");
412 png_error(png_ptr,
"Invalid bit depth for RGB image");
413 png_ptr->channels = 3;
421 case 8: png_ptr->channels = 1;
break;
422 default:
png_error(png_ptr,
"Invalid bit depth for paletted image");
427 png_error(png_ptr,
"Invalid bit depth for grayscale+alpha image");
428 png_ptr->channels = 2;
432 png_error(png_ptr,
"Invalid bit depth for RGBA image");
433 png_ptr->channels = 4;
436 png_error(png_ptr,
"Invalid image color type specified");
441 png_warning(png_ptr,
"Invalid compression type specified");
457 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
464 png_warning(png_ptr,
"Invalid filter type specified");
468 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
472 png_warning(png_ptr,
"Invalid interlace type specified");
482 png_ptr->interlaced = (
png_byte)interlace_type;
483 #if defined(PNG_MNG_FEATURES_SUPPORTED)
484 png_ptr->filter_type = (
png_byte)filter_type;
487 png_ptr->width =
width;
491 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
width);
493 png_ptr->usr_width = png_ptr->width;
494 png_ptr->usr_bit_depth = png_ptr->bit_depth;
495 png_ptr->usr_channels = png_ptr->channels;
512 png_ptr->zstream.opaque = (
voidpf)png_ptr;
513 if (!(png_ptr->do_filter))
516 png_ptr->bit_depth < 8)
521 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
528 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
530 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
531 png_ptr->zlib_mem_level = 8;
532 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
533 png_ptr->zlib_window_bits = 15;
534 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
535 png_ptr->zlib_method = 8;
536 ret =
deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
537 png_ptr->zlib_method, png_ptr->zlib_window_bits,
538 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
542 "zlib failed to initialize compressor -- version error");
544 "zlib failed to initialize compressor -- stream error");
546 "zlib failed to initialize compressor -- mem error");
547 png_error(png_ptr,
"zlib failed to initialize compressor");
549 png_ptr->zstream.next_out = png_ptr->zbuf;
550 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
553 png_ptr->zstream.data_type =
Z_BINARY;
565 #ifdef PNG_USE_LOCAL_ARRAYS
577 num_pal == 0) || num_pal > 256)
581 png_error(png_ptr,
"Invalid number of colors in palette");
585 png_warning(png_ptr,
"Invalid number of colors in palette");
593 "Ignoring request to write a PLTE chunk in grayscale PNG");
598 png_debug1(3,
"num_palette = %d\n", png_ptr->num_palette);
602 #ifndef PNG_NO_POINTER_INDEXING
603 for (
i = 0, pal_ptr =
palette;
i < num_pal;
i++, pal_ptr++)
605 buf[0] = pal_ptr->red;
606 buf[1] = pal_ptr->green;
607 buf[2] = pal_ptr->blue;
613 for (
i = 0;
i < num_pal;
i++)
615 buf[0] = pal_ptr[
i].red;
616 buf[1] = pal_ptr[
i].green;
617 buf[2] = pal_ptr[
i].blue;
629 #ifdef PNG_USE_LOCAL_ARRAYS
639 unsigned int z_cmf =
data[0];
640 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
646 png_ptr->height < 16384 && png_ptr->width < 16384)
648 png_uint_32 uncompressed_idat_size = png_ptr->height *
650 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
651 unsigned int z_cinfo = z_cmf >> 4;
652 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
653 while (uncompressed_idat_size <= half_z_window_size &&
654 half_z_window_size >= 256)
657 half_z_window_size >>= 1;
659 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
670 "Invalid zlib compression method or flags in IDAT");
681 #ifdef PNG_USE_LOCAL_ARRAYS
690 #if defined(PNG_WRITE_gAMA_SUPPORTED)
692 #ifdef PNG_FLOATING_POINT_SUPPORTED
696 #ifdef PNG_USE_LOCAL_ARRAYS
709 #ifdef PNG_FIXED_POINT_SUPPORTED
713 #ifdef PNG_USE_LOCAL_ARRAYS
726 #if defined(PNG_WRITE_sRGB_SUPPORTED)
731 #ifdef PNG_USE_LOCAL_ARRAYS
739 "Invalid sRGB rendering intent specified");
745 #if defined(PNG_WRITE_iCCP_SUPPORTED)
751 #ifdef PNG_USE_LOCAL_ARRAYS
757 int embedded_profile_len = 0;
770 png_warning(png_ptr,
"Empty keyword in iCCP chunk");
775 png_warning(png_ptr,
"Unknown compression type in iCCP chunk");
781 embedded_profile_len =
787 if (profile_len < embedded_profile_len)
790 "Embedded profile length too large in iCCP chunk");
794 if (profile_len > embedded_profile_len)
797 "Truncating profile to actual length in iCCP chunk");
798 profile_len = embedded_profile_len;
808 new_name[name_len + 1] = 0x00;
820 #if defined(PNG_WRITE_sPLT_SUPPORTED)
825 #ifdef PNG_USE_LOCAL_ARRAYS
831 int entry_size = (spalette->depth == 8 ? 6 : 10);
832 int palette_size = entry_size * spalette->nentries;
834 #ifdef PNG_NO_POINTER_INDEXING
840 spalette->name, &new_name))==0)
842 png_warning(png_ptr,
"Empty keyword in sPLT chunk");
854 #ifndef PNG_NO_POINTER_INDEXING
855 for (ep = spalette->entries; ep<spalette->
entries + spalette->nentries; ep++)
857 if (spalette->depth == 8)
876 ep=spalette->entries;
877 for (
i=0;
i>spalette->nentries;
i++)
879 if (spalette->depth == 8)
904 #if defined(PNG_WRITE_sBIT_SUPPORTED)
909 #ifdef PNG_USE_LOCAL_ARRAYS
922 png_ptr->usr_bit_depth);
923 if (sbit->red == 0 || sbit->red > maxbits ||
924 sbit->green == 0 || sbit->green > maxbits ||
925 sbit->blue == 0 || sbit->blue > maxbits)
927 png_warning(png_ptr,
"Invalid sBIT depth specified");
931 buf[1] = sbit->green;
937 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
939 png_warning(png_ptr,
"Invalid sBIT depth specified");
948 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
950 png_warning(png_ptr,
"Invalid sBIT depth specified");
960 #if defined(PNG_WRITE_cHRM_SUPPORTED)
962 #ifdef PNG_FLOATING_POINT_SUPPORTED
968 #ifdef PNG_USE_LOCAL_ARRAYS
976 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
979 png_warning(png_ptr,
"Invalid cHRM white point specified");
980 #if !defined(PNG_NO_CONSOLE_IO)
990 if (
red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
992 png_warning(png_ptr,
"Invalid cHRM red point specified");
1000 if (
green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
1002 png_warning(png_ptr,
"Invalid cHRM green point specified");
1010 if (
blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
1012 png_warning(png_ptr,
"Invalid cHRM blue point specified");
1023 #ifdef PNG_FIXED_POINT_SUPPORTED
1030 #ifdef PNG_USE_LOCAL_ARRAYS
1039 png_warning(png_ptr,
"Invalid fixed cHRM white point specified");
1040 #if !defined(PNG_NO_CONSOLE_IO)
1041 fprintf(stderr,
"white_x=%ld, white_y=%ld\n", (
unsigned long)
white_x,
1051 png_warning(png_ptr,
"Invalid cHRM fixed red point specified");
1059 png_warning(png_ptr,
"Invalid fixed cHRM green point specified");
1067 png_warning(png_ptr,
"Invalid fixed cHRM blue point specified");
1078 #if defined(PNG_WRITE_tRNS_SUPPORTED)
1084 #ifdef PNG_USE_LOCAL_ARRAYS
1092 if (num_trans <= 0 || num_trans > (
int)png_ptr->num_palette)
1094 png_warning(png_ptr,
"Invalid number of transparent colors specified");
1104 if (
tran->gray >= (1 << png_ptr->bit_depth))
1107 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1119 if (png_ptr->bit_depth == 8 && (
buf[0] |
buf[2] |
buf[4]))
1122 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1129 png_warning(png_ptr,
"Can't write tRNS with an alpha channel");
1134 #if defined(PNG_WRITE_bKGD_SUPPORTED)
1139 #ifdef PNG_USE_LOCAL_ARRAYS
1149 (png_ptr->num_palette ||
1152 back->index > png_ptr->num_palette)
1154 png_warning(png_ptr,
"Invalid background palette index");
1157 buf[0] = back->index;
1165 if (png_ptr->bit_depth == 8 && (
buf[0] |
buf[2] |
buf[4]))
1168 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1175 if (back->gray >= (1 << png_ptr->bit_depth))
1178 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1187 #if defined(PNG_WRITE_hIST_SUPPORTED)
1192 #ifdef PNG_USE_LOCAL_ARRAYS
1199 if (num_hist > (
int)png_ptr->num_palette)
1201 png_debug2(3,
"num_hist = %d, num_palette = %d\n", num_hist,
1202 png_ptr->num_palette);
1203 png_warning(png_ptr,
"Invalid number of histogram entries specified");
1209 for (
i = 0;
i < num_hist;
i++)
1218 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1219 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
1241 if (key == NULL || (key_len =
png_strlen(key)) == 0)
1247 png_debug1(2,
"Keyword to be checked is '%s'\n", key);
1250 if (*new_key == NULL)
1252 png_warning(png_ptr,
"Out of memory while procesing keyword");
1257 for (kp = key, dp = *new_key; *kp !=
'\0'; kp++, dp++)
1262 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
1266 "invalid keyword character 0x%02X", (
png_byte)*kp);
1269 png_warning(png_ptr,
"invalid character in keyword");
1281 kp = *new_key + key_len - 1;
1284 png_warning(png_ptr,
"trailing spaces removed from keyword");
1297 png_warning(png_ptr,
"leading spaces removed from keyword");
1306 png_debug1(2,
"Checking for multiple internal spaces in '%s'\n", kp);
1309 for (kflag = 0, dp = *new_key; *kp !=
'\0'; kp++)
1311 if (*kp ==
' ' && kflag == 0)
1316 else if (*kp ==
' ')
1329 png_warning(png_ptr,
"extra interior spaces removed from keyword");
1340 png_warning(png_ptr,
"keyword length must be 1 - 79 characters");
1349 #if defined(PNG_WRITE_tEXt_SUPPORTED)
1355 #ifdef PNG_USE_LOCAL_ARRAYS
1364 png_warning(png_ptr,
"Empty keyword in tEXt chunk");
1368 if (text == NULL || *text ==
'\0')
1392 #if defined(PNG_WRITE_zTXt_SUPPORTED)
1398 #ifdef PNG_USE_LOCAL_ARRAYS
1416 png_warning(png_ptr,
"Empty keyword in zTXt chunk");
1453 #if defined(PNG_WRITE_iTXt_SUPPORTED)
1459 #ifdef PNG_USE_LOCAL_ARRAYS
1462 png_size_t lang_len, key_len, lang_key_len, text_len;
1476 png_warning(png_ptr,
"Empty keyword in iTXt chunk");
1479 if (lang == NULL || (lang_len =
png_check_keyword(png_ptr, lang, &new_lang))==0)
1481 png_warning(png_ptr,
"Empty language field in iTXt chunk");
1486 if (lang_key == NULL)
1544 #if defined(PNG_WRITE_oFFs_SUPPORTED)
1550 #ifdef PNG_USE_LOCAL_ARRAYS
1557 png_warning(png_ptr,
"Unrecognized unit type for oFFs chunk");
1566 #if defined(PNG_WRITE_pCAL_SUPPORTED)
1572 #ifdef PNG_USE_LOCAL_ARRAYS
1575 png_size_t purpose_len, units_len, total_len;
1583 png_warning(png_ptr,
"Unrecognized equation type for pCAL chunk");
1586 png_debug1(3,
"pCAL purpose length = %d\n", (
int)purpose_len);
1588 png_debug1(3,
"pCAL units length = %d\n", (
int)units_len);
1589 total_len = purpose_len + units_len + 10;
1599 png_debug2(3,
"pCAL parameter %d length = %lu\n",
i,
1600 (
unsigned long) params_len[
i]);
1604 png_debug1(3,
"pCAL total length = %d\n", (
int)total_len);
1628 #if defined(PNG_WRITE_sCAL_SUPPORTED)
1630 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
1634 #ifdef PNG_USE_LOCAL_ARRAYS
1643 #if defined(_WIN32_WCE)
1648 swprintf(wc_buf, TEXT(
"%12.12e"),
width);
1649 wc_len = wcslen(wc_buf);
1650 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1,
buf + 1, wc_len, NULL, NULL);
1651 total_len = wc_len + 2;
1652 swprintf(wc_buf, TEXT(
"%12.12e"),
height);
1653 wc_len = wcslen(wc_buf);
1654 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1,
buf + total_len, wc_len,
1656 total_len += wc_len;
1665 png_debug1(3,
"sCAL total length = %u\n", (
unsigned int)total_len);
1669 #ifdef PNG_FIXED_POINT_SUPPORTED
1674 #ifdef PNG_USE_LOCAL_ARRAYS
1684 total_len = wlen + hlen + 2;
1687 png_warning(png_ptr,
"Can't write sCAL (buffer too small)");
1695 png_debug1(3,
"sCAL total length = %u\n", (
unsigned int)total_len);
1702 #if defined(PNG_WRITE_pHYs_SUPPORTED)
1709 #ifdef PNG_USE_LOCAL_ARRAYS
1716 png_warning(png_ptr,
"Unrecognized unit type for pHYs chunk");
1726 #if defined(PNG_WRITE_tIME_SUPPORTED)
1733 #ifdef PNG_USE_LOCAL_ARRAYS
1743 png_warning(png_ptr,
"Invalid time specified for tIME chunk");
1762 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1763 #ifdef PNG_USE_LOCAL_ARRAYS
1782 png_debug(1,
"in png_write_start_row\n");
1784 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
1791 #ifndef PNG_NO_WRITE_FILTER
1831 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1833 if (png_ptr->interlaced)
1835 if (!(png_ptr->transformations & PNG_INTERLACE))
1837 png_ptr->num_rows = (png_ptr->height +
png_pass_yinc[0] - 1 -
1839 png_ptr->usr_width = (png_ptr->width +
png_pass_inc[0] - 1 -
1844 png_ptr->num_rows = png_ptr->height;
1845 png_ptr->usr_width = png_ptr->width;
1851 png_ptr->num_rows = png_ptr->height;
1852 png_ptr->usr_width = png_ptr->width;
1854 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
1855 png_ptr->zstream.next_out = png_ptr->zbuf;
1862 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1863 #ifdef PNG_USE_LOCAL_ARRAYS
1882 png_debug(1,
"in png_write_finish_row\n");
1884 png_ptr->row_number++;
1887 if (png_ptr->row_number < png_ptr->num_rows)
1890 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1892 if (png_ptr->interlaced)
1894 png_ptr->row_number = 0;
1895 if (png_ptr->transformations & PNG_INTERLACE)
1905 if (png_ptr->pass >= 7)
1907 png_ptr->usr_width = (png_ptr->width +
1911 png_ptr->num_rows = (png_ptr->height +
1915 if (png_ptr->transformations & PNG_INTERLACE)
1917 }
while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1922 if (png_ptr->pass < 7)
1924 if (png_ptr->prev_row != NULL)
1926 (
png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1927 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
1943 if (!(png_ptr->zstream.avail_out))
1946 png_ptr->zstream.next_out = png_ptr->zbuf;
1947 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
1952 if (png_ptr->zstream.msg != NULL)
1953 png_error(png_ptr, png_ptr->zstream.msg);
1960 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
1963 png_ptr->zstream.avail_out);
1967 png_ptr->zstream.data_type =
Z_BINARY;
1970 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
1981 #ifdef PNG_USE_LOCAL_ARRAYS
1991 png_debug(1,
"in png_do_write_interlace\n");
1993 #if defined(PNG_USELESS_TESTS_SUPPORTED)
1994 if (
row != NULL && row_info != NULL && pass < 6)
2000 switch (row_info->pixel_depth)
2019 value = (
int)(*sp >> (7 - (
int)(
i & 0x07))) & 0x01;
2020 d |= (
value << shift);
2053 value = (*sp >> ((3 - (
int)(
i & 0x03)) << 1)) & 0x03;
2054 d |= (
value << shift);
2086 value = (*sp >> ((1 - (
int)(
i & 0x01)) << 2)) & 0x0f;
2087 d |= (
value << shift);
2113 pixel_bytes = (row_info->pixel_depth >> 3);
2131 row_info->width = (row_info->width +
2135 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2145 #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
2146 #define PNG_HISHIFT 10
2147 #define PNG_LOMASK ((png_uint_32)0xffffL)
2148 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
2153 #ifndef PNG_NO_WRITE_FILTER
2156 png_byte filter_to_do = png_ptr->do_filter;
2158 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2159 int num_p_filters = (
int)png_ptr->num_prev_filters;
2162 png_debug(1,
"in png_write_find_filter\n");
2164 bpp = (row_info->pixel_depth + 7) >> 3;
2166 prev_row = png_ptr->prev_row;
2168 best_row = png_ptr->row_buf;
2169 #ifndef PNG_NO_WRITE_FILTER
2206 for (
i = 0, rp = row_buf + 1;
i < row_bytes;
i++, rp++)
2209 sum += (v < 128) ? v : 256 - v;
2212 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2221 for (j = 0; j < num_p_filters; j++)
2225 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2227 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2256 for (
i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1;
i < bpp;
2261 for (lp = row_buf + 1;
i < row_bytes;
2262 i++, rp++, lp++, dp++)
2266 best_row = png_ptr->sub_row;
2276 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2288 for (j = 0; j < num_p_filters; j++)
2292 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2294 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2311 for (
i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1;
i < bpp;
2316 sum += (v < 128) ? v : 256 - v;
2318 for (lp = row_buf + 1;
i < row_bytes;
2319 i++, rp++, lp++, dp++)
2321 v = *dp = (
png_byte)(((
int)*rp - (
int)*lp) & 0xff);
2323 sum += (v < 128) ? v : 256 - v;
2329 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2337 for (j = 0; j < num_p_filters; j++)
2341 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
2343 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
2363 best_row = png_ptr->sub_row;
2373 for (
i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2374 pp = prev_row + 1;
i < row_bytes;
2375 i++, rp++, pp++, dp++)
2379 best_row = png_ptr->up_row;
2390 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2398 for (j = 0; j < num_p_filters; j++)
2402 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2404 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2421 for (
i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2422 pp = prev_row + 1;
i < row_bytes;
i++)
2424 v = *dp++ = (
png_byte)(((
int)*rp++ - (
int)*pp++) & 0xff);
2426 sum += (v < 128) ? v : 256 - v;
2432 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2440 for (j = 0; j < num_p_filters; j++)
2444 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2446 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2466 best_row = png_ptr->up_row;
2475 for (
i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2476 pp = prev_row + 1;
i < bpp;
i++)
2478 *dp++ = (
png_byte)(((
int)*rp++ - ((
int)*pp++ / 2)) & 0xff);
2480 for (lp = row_buf + 1;
i < row_bytes;
i++)
2482 *dp++ = (
png_byte)(((
int)*rp++ - (((
int)*pp++ + (
int)*lp++) / 2))
2485 best_row = png_ptr->avg_row;
2495 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2503 for (j = 0; j < num_p_filters; j++)
2507 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2509 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2526 for (
i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2527 pp = prev_row + 1;
i < bpp;
i++)
2529 v = *dp++ = (
png_byte)(((
int)*rp++ - ((
int)*pp++ / 2)) & 0xff);
2531 sum += (v < 128) ? v : 256 - v;
2533 for (lp = row_buf + 1;
i < row_bytes;
i++)
2536 (
png_byte)(((
int)*rp++ - (((
int)*pp++ + (
int)*lp++) / 2)) & 0xff);
2538 sum += (v < 128) ? v : 256 - v;
2544 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2552 for (j = 0; j < num_p_filters; j++)
2556 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2558 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2578 best_row = png_ptr->avg_row;
2587 for (
i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2588 pp = prev_row + 1;
i < bpp;
i++)
2590 *dp++ = (
png_byte)(((
int)*rp++ - (
int)*pp++) & 0xff);
2593 for (lp = row_buf + 1, cp = prev_row + 1;
i < row_bytes;
i++)
2595 int a,
b,
c, pa, pb, pc, p;
2609 pa = p < 0 ? -p : p;
2610 pb = pc < 0 ? -pc : pc;
2611 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2614 p = (pa <= pb && pa <=pc) ?
a : (pb <= pc) ?
b :
c;
2616 *dp++ = (
png_byte)(((
int)*rp++ - p) & 0xff);
2618 best_row = png_ptr->paeth_row;
2628 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2636 for (j = 0; j < num_p_filters; j++)
2640 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2642 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2659 for (
i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2660 pp = prev_row + 1;
i < bpp;
i++)
2662 v = *dp++ = (
png_byte)(((
int)*rp++ - (
int)*pp++) & 0xff);
2664 sum += (v < 128) ? v : 256 - v;
2667 for (lp = row_buf + 1, cp = prev_row + 1;
i < row_bytes;
i++)
2669 int a,
b,
c, pa, pb, pc, p;
2675 #ifndef PNG_SLOW_PAETH
2683 pa = p < 0 ? -p : p;
2684 pb = pc < 0 ? -pc : pc;
2685 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2687 p = (pa <= pb && pa <=pc) ?
a : (pb <= pc) ?
b :
c;
2693 if (pa <= pb && pa <= pc)
2701 v = *dp++ = (
png_byte)(((
int)*rp++ - p) & 0xff);
2703 sum += (v < 128) ? v : 256 - v;
2709 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2717 for (j = 0; j < num_p_filters; j++)
2721 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2723 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2742 best_row = png_ptr->paeth_row;
2750 #ifndef PNG_NO_WRITE_FILTER
2751 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2753 if (png_ptr->num_prev_filters > 0)
2756 for (j = 1; j < num_p_filters; j++)
2758 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
2760 png_ptr->prev_filters[j] = best_row[0];
2771 png_debug(1,
"in png_write_filtered_row\n");
2772 png_debug1(2,
"filter = %d\n", filtered_row[0]);
2775 png_ptr->zstream.next_in = filtered_row;
2776 png_ptr->zstream.avail_in = (
uInt)png_ptr->row_info.rowbytes + 1;
2787 if (png_ptr->zstream.msg != NULL)
2788 png_error(png_ptr, png_ptr->zstream.msg);
2794 if (!(png_ptr->zstream.avail_out))
2798 png_ptr->zstream.next_out = png_ptr->zbuf;
2799 png_ptr->zstream.avail_out = (
uInt)png_ptr->zbuf_size;
2802 }
while (png_ptr->zstream.avail_in);
2805 if (png_ptr->prev_row != NULL)
2809 tptr = png_ptr->prev_row;
2810 png_ptr->prev_row = png_ptr->row_buf;
2811 png_ptr->row_buf = tptr;
2817 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
2818 png_ptr->flush_rows++;
2820 if (png_ptr->flush_dist > 0 &&
2821 png_ptr->flush_rows >= png_ptr->flush_dist)