de-recurse hist_item_sort_halfvar()

Signed-off-by: liubo <liubo1@xfusion.com>
(cherry picked from commit 80fc3ecada9cdad5d09c699ec166fbd1b04381aa)
(cherry picked from commit ee903edc24221a4807cdbd063dc4f4c2b20ce98b)
This commit is contained in:
liubo 2023-10-23 16:51:23 +08:00 committed by openeuler-sync-bot
parent 0b2a813bf5
commit 1f6c81bc8d
2 changed files with 92 additions and 1 deletions

View File

@ -0,0 +1,86 @@
From fd3d56154cc55ab07628f8f2d37e77f60f17874c Mon Sep 17 00:00:00 2001
From: Pascal Massimino <skal@google.com>
Date: Mon, 8 Mar 2021 11:07:29 +0100
Subject: [PATCH] de-recurse hist_item_sort_halfvar()
This function can recurse quite deep when sorting values are
quite flag. We remove the recursion call totally.
The API is slightly changed to return the break_at index directly.
---
mediancut.c | 37 +++++++++++++++++++------------------
1 file changed, 19 insertions(+), 18 deletions(-)
diff --git a/mediancut.c b/mediancut.c
index 2c6d1d8..64a21f2 100644
--- a/mediancut.c
+++ b/mediancut.c
@@ -134,34 +134,36 @@ static void hist_item_sort_range(hist_item base[], unsigned int len, unsigned in
}
/** sorts array to make sum of weights lower than halfvar one side, returns edge between <halfvar and >halfvar parts of the set */
-static hist_item *hist_item_sort_halfvar(hist_item base[], unsigned int len, double *const lowervar, const double halfvar)
+static unsigned int hist_item_sort_halfvar(hist_item base[], unsigned int len, double halfvar)
{
+ unsigned int base_idx = 0; // track base-index
do {
const unsigned int l = qsort_partition(base, len), r = l+1;
// check if sum of left side is smaller than half,
// if it is, then it doesn't need to be sorted
- unsigned int t = 0; double tmpsum = *lowervar;
- while (t <= l && tmpsum < halfvar) tmpsum += base[t++].color_weight;
+ double tmpsum = 0.;
+ for(unsigned int t = 0; t <= l && tmpsum < halfvar; ++t) tmpsum += base[t].color_weight;
- if (tmpsum < halfvar) {
- *lowervar = tmpsum;
- } else {
+ // the split is on the left part
+ if (tmpsum >= halfvar) {
if (l > 0) {
- hist_item *res = hist_item_sort_halfvar(base, l, lowervar, halfvar);
- if (res) return res;
+ len = l;
+ continue;
} else {
- // End of left recursion. This will be executed in order from the first element.
- *lowervar += base[0].color_weight;
- if (*lowervar > halfvar) return &base[0];
+ // End of left recursion;
+ return base_idx;
}
}
-
+ // process the right part
+ halfvar -= tmpsum;
if (len > r) {
- base += r; len -= r; // tail-recursive "call"
+ base += r;
+ base_idx += r;
+ len -= r; // tail-recursive "call"
} else {
- *lowervar += base[r].color_weight;
- return (*lowervar > halfvar) ? &base[r] : NULL;
+ // End of right recursion
+ return base_idx + len;
}
} while(1);
}
@@ -370,12 +372,11 @@ LIQ_PRIVATE colormap *mediancut(histogram *hist, unsigned int newcolors, const d
*/
const double halfvar = prepare_sort(&bv[bi], achv);
- double lowervar=0;
// hist_item_sort_halfvar sorts and sums lowervar at the same time
// returns item to break at …minus one, which does smell like an off-by-one error.
- hist_item *break_p = hist_item_sort_halfvar(&achv[indx], clrs, &lowervar, halfvar);
- unsigned int break_at = MIN(clrs-1, break_p - &achv[indx] + 1);
+ unsigned int break_at = hist_item_sort_halfvar(&achv[indx], clrs, halfvar);
+ break_at = MIN(clrs-1, break_at + 1);
/*
** Split the box.
--
2.42.0.windows.2

View File

@ -1,12 +1,14 @@
Name: libimagequant Name: libimagequant
Version: 2.15.1 Version: 2.15.1
Release: 2 Release: 3
Summary: Palette quantization library Summary: Palette quantization library
License: GPLv3+ and MIT License: GPLv3+ and MIT
URL: https://github.com/ImageOptim/libimagequant URL: https://github.com/ImageOptim/libimagequant
Source0: %{url}/archive/%{version}/%{name}-%{version}.tar.gz Source0: %{url}/archive/%{version}/%{name}-%{version}.tar.gz
Patch0000: libimagequant_solibperm.patch Patch0000: libimagequant_solibperm.patch
Patch0001: 0001-de-recurse-hist_item_sort_halfvar.patch
BuildRequires: gcc BuildRequires: gcc
@ -47,6 +49,9 @@ rm -f %{buildroot}%{_libdir}/%{name}.a
%{_libdir}/pkgconfig/imagequant.pc %{_libdir}/pkgconfig/imagequant.pc
%changelog %changelog
* Mon Oct 23 2023 liubo <liubo1@xfusion.com> - 2.15.1-3
- de-recurse hist_item_sort_halfvar()
* Wed Oct 26 2022 zhouwenpei <zhouwenpei1@h-partners.com> - 2.15.1-2 * Wed Oct 26 2022 zhouwenpei <zhouwenpei1@h-partners.com> - 2.15.1-2
- Rebuild for next release - Rebuild for next release