Skip to content
Merged
Next Next commit
Fix bugs in the calculation of the weight array
  • Loading branch information
ayshih committed Jan 8, 2026
commit 71bb56fde11d15608f869ba655c0aa00311ac5bf
14 changes: 14 additions & 0 deletions extern/agg24-svn/include/agg_image_filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#ifndef AGG_IMAGE_FILTERS_INCLUDED
#define AGG_IMAGE_FILTERS_INCLUDED

#define MPL_FIX_IMAGE_FILTER_LUT_BUGS

#include "agg_array.h"
#include "agg_math.h"

Expand Down Expand Up @@ -53,6 +55,7 @@ namespace agg
double r = filter.radius();
realloc_lut(r);
unsigned i;
#ifndef MPL_FIX_IMAGE_FILTER_LUT_BUGS
unsigned pivot = diameter() << (image_subpixel_shift - 1);
for(i = 0; i < pivot; i++)
{
Expand All @@ -63,6 +66,17 @@ namespace agg
}
unsigned end = (diameter() << image_subpixel_shift) - 1;
m_weight_array[0] = m_weight_array[end];

@ayshih ayshih Jun 18, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is one of the bugs. 0 and end are different distances from pivot, so the corresponding weights typically should not be the same.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the new version under a macro guard, as was done in #28122, so that we keep a track of what's the original Agg and what we changed on top of it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, done

@ayshih ayshih Jun 18, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that I can't add a preprocessor definition in _image_resample.h a la #28122. There is buggy code in agg_image_filters.cpp that needs to be skipped, but since it is a CPP file rather than a H file, it gets compiled separately with no awareness of _image_resample.h. I have put the preprocessor definition (MPL_FIX_IMAGE_FILTER_LUT_BUGS) here at the top of agg_image_filters.h, which is lower level than would be nice, but at least still makes it clear what is custom code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also put the define in the corresponding meson.build if you prefer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's a nicer approach. Done!

#else
unsigned pivot = (diameter() << (image_subpixel_shift - 1)) - 1;
for(i = 0; i <= pivot + 1; i++)
{
double x = double(i) / double(image_subpixel_scale);
double y = filter.calc_weight(x);
int16 value = iround(y * image_filter_scale);
m_weight_array[pivot + i] = value;
if(i <= pivot) m_weight_array[pivot - i] = value;
}
#endif
if(normalization)
{
normalize();
Expand Down
2 changes: 2 additions & 0 deletions extern/agg24-svn/src/agg_image_filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ namespace agg
}
}

#ifndef MPL_FIX_IMAGE_FILTER_LUT_BUGS

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following code block is removed because it not only has the same bugs as the code in agg_image_filters.h, but also it shouldn't even exist because it modifies the weight array after it has been normalized, thus potentially destroying the normalization.

unsigned pivot = m_diameter << (image_subpixel_shift - 1);

for(i = 0; i < pivot; i++)
Expand All @@ -96,6 +97,7 @@ namespace agg
}
unsigned end = (diameter() << image_subpixel_shift) - 1;
m_weight_array[0] = m_weight_array[end];
#endif
}


Expand Down
19 changes: 13 additions & 6 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,19 +1663,26 @@ def test__resample_valid_output():
[(np.array([[0.1, 0.3, 0.2]]), mimage.NEAREST,
np.array([[0.1, 0.1, 0.1, 0.3, 0.3, 0.3, 0.3, 0.2, 0.2, 0.2]])),
(np.array([[0.1, 0.3, 0.2]]), mimage.BILINEAR,
np.array([[0.1, 0.1, 0.15078125, 0.21096191, 0.27033691,
0.28476562, 0.2546875, 0.22460938, 0.20002441, 0.20002441]])),
np.array([[0.1, 0.1, 0.15, 0.21, 0.27, 0.285, 0.255, 0.225, 0.2, 0.2]])),
(np.array([[0.1, 0.9]]), mimage.BILINEAR,
np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.14, 0.22, 0.3, 0.38, 0.46,
0.54, 0.62, 0.7, 0.78, 0.86, 0.9, 0.9, 0.9, 0.9, 0.9]])),
(np.array([[0.1, 0.1]]), mimage.BILINEAR, np.full((1, 10), 0.1)),
]
)
def test_resample_nonaffine(data, interpolation, expected):
# Test that equivalent affine and nonaffine transforms resample the same
# Test that both affine and nonaffine transforms resample to the correct answer

# If the array is constant, the tolerance can be tight
# Otherwise, the tolerance is limited by the subpixel approach in the agg backend
atol = 0 if np.all(data == data.ravel()[0]) else 2e-3

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

affine_result = np.empty_like(expected)
mimage.resample(data, affine_result, affine_transform, interpolation=interpolation)
assert_allclose(affine_result, expected)
assert_allclose(affine_result, expected, atol=atol)

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

def inverted(self):
return self
return self
nonaffine_transform = NonAffineIdentityTransform() + affine_transform

nonaffine_result = np.empty_like(expected)
mimage.resample(data, nonaffine_result, nonaffine_transform,
interpolation=interpolation)
assert_allclose(nonaffine_result, expected, atol=5e-3)
assert_allclose(nonaffine_result, expected, atol=atol)


def test_axesimage_get_shape():
Expand Down