From f7d5419816d85d0b996c403e788bb707b552524f Mon Sep 17 00:00:00 2001 From: Glenn Randers-Pehrson Date: Fri, 8 Jul 2016 10:09:25 -0500 Subject: [PATCH] [libpng16] Avoid filter-selection heuristic sum calculations in cases where only one filter is a candidate for selection. This trades off code size (added png_setup_*_row_only() functions) for speed. --- ANNOUNCE | 7 +++- CHANGES | 5 +++ pngwutil.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 115 insertions(+), 10 deletions(-) diff --git a/ANNOUNCE b/ANNOUNCE index 669c75395..159c91a07 100644 --- a/ANNOUNCE +++ b/ANNOUNCE @@ -1,4 +1,4 @@ -Libpng 1.6.24beta04 - July 4, 2016 +Libpng 1.6.24beta04 - July 8, 2016 This is not intended to be a public release. It will be replaced within a few weeks by a public version or by another test version. @@ -77,6 +77,11 @@ Version 1.6.24beta03 [July 4, 2016] Relocated misplaced #endif in png.c sRGB profile checking. Fixed two Coverity issues in pngcp.c. +Version 1.6.24beta04 [July 8, 2016] + Avoid filter-selection heuristic sum calculations in cases where only one + filter is a candidate for selection. This trades off code size (added + png_setup_*_row_only() functions) for speed. + Send comments/corrections/commendations to png-mng-implement at lists.sf.net (subscription required; visit https://lists.sourceforge.net/lists/listinfo/png-mng-implement diff --git a/CHANGES b/CHANGES index 63dd06b54..9976f13b1 100644 --- a/CHANGES +++ b/CHANGES @@ -5648,6 +5648,11 @@ Version 1.6.24beta03 [July 4, 2016] Relocated misplaced #endif in png.c sRGB profile checking. Fixed two Coverity issues in pngcp.c. +Version 1.6.24beta04 [July 8, 2016] + Avoid filter-selection heuristic sum calculations in cases where only one + filter is a candidate for selection. This trades off code size (added + png_setup_*_row_only() functions) for speed. + Send comments/corrections/commendations to png-mng-implement at lists.sf.net (subscription required; visit https://lists.sourceforge.net/lists/listinfo/png-mng-implement diff --git a/pngwutil.c b/pngwutil.c index 1f7ec5289..53a2eca4a 100644 --- a/pngwutil.c +++ b/pngwutil.c @@ -2286,6 +2286,28 @@ png_setup_sub_row(png_structrp png_ptr, const png_uint_32 bpp, return (sum); } +static void /* PRIVATE */ +png_setup_sub_row_only(png_structrp png_ptr, const png_uint_32 bpp, + const png_size_t row_bytes) +{ + png_bytep rp, dp, lp; + png_size_t i; + + png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB; + + for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1; i < bpp; + i++, rp++, dp++) + { + *dp = *rp; + } + + for (lp = png_ptr->row_buf + 1; i < row_bytes; + i++, rp++, lp++, dp++) + { + *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); + } +} + static png_size_t /* PRIVATE */ png_setup_up_row(png_structrp png_ptr, const png_size_t row_bytes, const png_size_t lmins) @@ -2314,6 +2336,21 @@ png_setup_up_row(png_structrp png_ptr, const png_size_t row_bytes, return (sum); } +static void /* PRIVATE */ +png_setup_up_row_only(png_structrp png_ptr, const png_size_t row_bytes) +{ + png_bytep rp, dp, pp; + png_size_t i; + + png_ptr->try_row[0] = PNG_FILTER_VALUE_UP; + + for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, + pp = png_ptr->prev_row + 1; i < row_bytes; + i++, rp++, pp++, dp++) + { + *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); + } +} static png_size_t /* PRIVATE */ png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp, @@ -2355,6 +2392,27 @@ png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp, return (sum); } +static void /* PRIVATE */ +png_setup_avg_row_only(png_structrp png_ptr, const png_uint_32 bpp, + const png_size_t row_bytes) +{ + png_bytep rp, dp, pp, lp; + png_uint_32 i; + + png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG; + + for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, + pp = png_ptr->prev_row + 1; i < bpp; i++) + { + *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); + } + + for (lp = png_ptr->row_buf + 1; i < row_bytes; i++) + { + *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) + & 0xff); + } +} static png_size_t /* PRIVATE */ png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp, @@ -2417,6 +2475,48 @@ png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp, return (sum); } +static void /* PRIVATE */ +png_setup_paeth_row_only(png_structrp png_ptr, const png_uint_32 bpp, + const png_size_t row_bytes) +{ + png_bytep rp, dp, pp, cp, lp; + png_size_t i; + + png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH; + + for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, + pp = png_ptr->prev_row + 1; i < bpp; i++) + { + *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); + } + + for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes; + i++) + { + int a, b, c, pa, pb, pc, p; + + b = *pp++; + c = *cp++; + a = *lp++; + + p = b - c; + pc = a - c; + +#ifdef PNG_USE_ABS + pa = abs(p); + pb = abs(pc); + pc = abs(p + pc); +#else + pa = p < 0 ? -p : p; + pb = pc < 0 ? -pc : pc; + pc = (p + pc) < 0 ? -(p + pc) : p + pc; +#endif + + p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; + + *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); + } +} #endif /* WRITE_FILTER */ void /* PRIVATE */ @@ -2508,12 +2608,7 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info) if (filter_to_do == PNG_FILTER_SUB) /* It's the only filter so no testing is needed */ { - /* Passing PNG_SIZE_MAX here and below prevents the 'setup' function - * breaking out of the loop when lmins is exceeded. Optimizing - * compilers should notice that we don't use the returned sum, and - * therefore 'setup' should refrain from calculating and returning "sum". - */ - (void) png_setup_sub_row(png_ptr, bpp, row_bytes, PNG_SIZE_MAX); + png_setup_sub_row_only(png_ptr, bpp, row_bytes); best_row = png_ptr->try_row; } @@ -2539,7 +2634,7 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info) /* Up filter */ if (filter_to_do == PNG_FILTER_UP) { - (void) png_setup_up_row(png_ptr, row_bytes, PNG_SIZE_MAX); + png_setup_up_row_only(png_ptr, row_bytes); best_row = png_ptr->try_row; } @@ -2565,7 +2660,7 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info) /* Avg filter */ if (filter_to_do == PNG_FILTER_AVG) { - (void) png_setup_avg_row(png_ptr, bpp, row_bytes, PNG_SIZE_MAX); + png_setup_avg_row_only(png_ptr, bpp, row_bytes); best_row = png_ptr->try_row; } @@ -2591,7 +2686,7 @@ png_write_find_filter(png_structrp png_ptr, png_row_infop row_info) /* Paeth filter */ if ((filter_to_do == PNG_FILTER_PAETH) != 0) { - (void) png_setup_paeth_row(png_ptr, bpp, row_bytes, PNG_SIZE_MAX); + png_setup_paeth_row_only(png_ptr, bpp, row_bytes); best_row = png_ptr->try_row; }