Fix affine resampling when using nearest-neighbor interpolation · matplotlib/matplotlib@0ecf660 · GitHub
Skip to content

Commit 0ecf660

Browse files
committed
Fix affine resampling when using nearest-neighbor interpolation
The built-in Agg interpolator for determining the nearest-neighbor pixels under an affine transform uses integer-based math, but that results in slight inaccuracies that can become visible at even a minor level of magnification. A custom interpolator that uses floating-point-based math is now used instead.
1 parent 89bbd1c commit 0ecf660

3 files changed

Lines changed: 90 additions & 10 deletions

File tree

‎src/_image_resample.h‎

Lines changed: 3 additions & 2 deletions

‎src/_image_wrapper.cpp‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,12 @@ image_resample(py::array input_array,
167167

168168
if (is_affine) {
169169
convert_trans_affine(transform, params.affine);
170-
// If affine parameters will make subpixels visible, treat as nonaffine instead
171-
if (params.affine.sx >= agg::image_subpixel_scale / 2 || params.affine.sy >= agg::image_subpixel_scale / 2) {
172-
is_affine = false;
173-
params.affine = agg::trans_affine(); // reset to identity affine parameters
174-
}
175-
}
176-
if (!is_affine) {
170+
params.is_affine = is_affine;
171+
} else {
177172
transform_mesh = _get_transform_mesh(transform, output_array.shape());
178173
params.transform_mesh = transform_mesh.data();
174+
params.is_affine = false;
179175
}
180-
params.is_affine = is_affine;
181176
}
182177

183178
if (auto resampler =

‎src/agg_workaround.h‎

Lines changed: 84 additions & 0 deletions

0 commit comments

Comments
 (0)