Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71bb56f

Browse files
committed
Fix bugs in the calculation of the weight array
1 parent 5d219d0 commit 71bb56f

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

extern/agg24-svn/include/agg_image_filters.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#ifndef AGG_IMAGE_FILTERS_INCLUDED
2121
#define AGG_IMAGE_FILTERS_INCLUDED
2222

23+
#define MPL_FIX_IMAGE_FILTER_LUT_BUGS
24+
2325
#include "agg_array.h"
2426
#include "agg_math.h"
2527

@@ -53,6 +55,7 @@ namespace agg
5355
double r = filter.radius();
5456
realloc_lut(r);
5557
unsigned i;
58+
#ifndef MPL_FIX_IMAGE_FILTER_LUT_BUGS
5659
unsigned pivot = diameter() << (image_subpixel_shift - 1);
5760
for(i = 0; i < pivot; i++)
5861
{
@@ -63,6 +66,17 @@ namespace agg
6366
}
6467
unsigned end = (diameter() << image_subpixel_shift) - 1;
6568
m_weight_array[0] = m_weight_array[end];
69+
#else
70+
unsigned pivot = (diameter() << (image_subpixel_shift - 1)) - 1;
71+
for(i = 0; i <= pivot + 1; i++)
72+
{
73+
double x = double(i) / double(image_subpixel_scale);
74+
double y = filter.calc_weight(x);
75+
int16 value = iround(y * image_filter_scale);
76+
m_weight_array[pivot + i] = value;
77+
if(i <= pivot) m_weight_array[pivot - i] = value;
78+
}
79+
#endif
6680
if(normalization)
6781
{
6882
normalize();

extern/agg24-svn/src/agg_image_filters.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ namespace agg
8888
}
8989
}
9090

91+
#ifndef MPL_FIX_IMAGE_FILTER_LUT_BUGS
9192
unsigned pivot = m_diameter << (image_subpixel_shift - 1);
9293

9394
for(i = 0; i < pivot; i++)
@@ -96,6 +97,7 @@ namespace agg
9697
}
9798
unsigned end = (diameter() << image_subpixel_shift) - 1;
9899
m_weight_array[0] = m_weight_array[end];
100+
#endif
99101
}
100102

101103

lib/matplotlib/tests/test_image.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,19 +1663,26 @@ def test__resample_valid_output():
16631663
[(np.array([[0.1, 0.3, 0.2]]), mimage.NEAREST,
16641664
np.array([[0.1, 0.1, 0.1, 0.3, 0.3, 0.3, 0.3, 0.2, 0.2, 0.2]])),
16651665
(np.array([[0.1, 0.3, 0.2]]), mimage.BILINEAR,
1666-
np.array([[0.1, 0.1, 0.15078125, 0.21096191, 0.27033691,
1667-
0.28476562, 0.2546875, 0.22460938, 0.20002441, 0.20002441]])),
1666+
np.array([[0.1, 0.1, 0.15, 0.21, 0.27, 0.285, 0.255, 0.225, 0.2, 0.2]])),
1667+
(np.array([[0.1, 0.9]]), mimage.BILINEAR,
1668+
np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.14, 0.22, 0.3, 0.38, 0.46,
1669+
0.54, 0.62, 0.7, 0.78, 0.86, 0.9, 0.9, 0.9, 0.9, 0.9]])),
1670+
(np.array([[0.1, 0.1]]), mimage.BILINEAR, np.full((1, 10), 0.1)),
16681671
]
16691672
)
16701673
def test_resample_nonaffine(data, interpolation, expected):
1671-
# Test that equivalent affine and nonaffine transforms resample the same
1674+
# Test that both affine and nonaffine transforms resample to the correct answer
1675+
1676+
# If the array is constant, the tolerance can be tight
1677+
# Otherwise, the tolerance is limited by the subpixel approach in the agg backend
1678+
atol = 0 if np.all(data == data.ravel()[0]) else 2e-3
16721679

16731680
# Create a simple affine transform for scaling the input array
16741681
affine_transform = Affine2D().scale(sx=expected.shape[1] / data.shape[1], sy=1)
16751682

16761683
affine_result = np.empty_like(expected)
16771684
mimage.resample(data, affine_result, affine_transform, interpolation=interpolation)
1678-
assert_allclose(affine_result, expected)
1685+
assert_allclose(affine_result, expected, atol=atol)
16791686

16801687
# Create a nonaffine version of the same transform
16811688
# by compositing with a nonaffine identity transform
@@ -1684,13 +1691,13 @@ class NonAffineIdentityTransform(Transform):
16841691
output_dims = 2
16851692

16861693
def inverted(self):
1687-
return self
1694+
return self
16881695
nonaffine_transform = NonAffineIdentityTransform() + affine_transform
16891696

16901697
nonaffine_result = np.empty_like(expected)
16911698
mimage.resample(data, nonaffine_result, nonaffine_transform,
16921699
interpolation=interpolation)
1693-
assert_allclose(nonaffine_result, expected, atol=5e-3)
1700+
assert_allclose(nonaffine_result, expected, atol=atol)
16941701

16951702

16961703
def test_axesimage_get_shape():

0 commit comments

Comments
 (0)