Merge pull request #2558 from uploadcare/resize-truncate-coeffs-2

Use round instead of floor to exclude of zero coefficients
This commit is contained in:
wiredfool 2017-06-13 21:03:27 +01:00 committed by GitHub
commit d8f15a0706
2 changed files with 11 additions and 24 deletions

View File

@ -158,14 +158,12 @@ class TestImagingCoreResampleAccuracy(PillowTestCase):
def test_enlarge_hamming(self):
for mode in ['RGBX', 'RGB', 'La', 'L']:
case = self.make_case(mode, (4, 4), 0xe1)
case = case.resize((8, 8), Image.HAMMING)
data = ('e1 e1 ea d1'
'e1 e1 ea d1'
'ea ea f4 d9'
'd1 d1 d9 c4')
case = self.make_case(mode, (2, 2), 0xe1)
case = case.resize((4, 4), Image.HAMMING)
data = ('e1 d2'
'd2 c5')
for channel in case.split():
self.check_case(channel, self.make_sample(data, (8, 8)))
self.check_case(channel, self.make_sample(data, (4, 4)))
def test_enlarge_bicubic(self):
for mode in ['RGBX', 'RGB', 'La', 'L']:

View File

@ -32,6 +32,8 @@ static inline double hamming_filter(double x)
x = -x;
if (x == 0.0)
return 1.0;
if (x >= 1.0)
return 0.0;
x = x * M_PI;
return sin(x) / x * (0.54f + 0.46f * cos(x));
}
@ -166,10 +168,12 @@ precompute_coeffs(int inSize, int outSize, struct filter *filterp,
center = (xx + 0.5) * scale;
ww = 0.0;
ss = 1.0 / filterscale;
xmin = (int) floor(center - support);
// Round the value
xmin = (int) (center - support + 0.5);
if (xmin < 0)
xmin = 0;
xmax = (int) ceil(center + support);
// Round the value
xmax = (int) (center + support + 0.5);
if (xmax > inSize)
xmax = inSize;
xmax -= xmin;
@ -178,21 +182,6 @@ precompute_coeffs(int inSize, int outSize, struct filter *filterp,
double w = filterp->filter((x + xmin - center + 0.5) * ss);
k[x] = w;
ww += w;
// We can skip extreme coefficients if they are zeroes.
if (w == 0) {
// Skip from the start.
if (x == 0) {
// At next loop `x` will be 0.
x -= 1;
// But `w` will not be 0, because it based on `xmin`.
xmin += 1;
xmax -= 1;
} else if (x == xmax - 1) {
// Truncate the last coefficient for current `xx`.
xmax -= 1;
}
}
}
for (x = 0; x < xmax; x++) {
if (ww != 0.0)