Fix convolution sampling for bounds smaller than the kernel radius#3152
Merged
Conversation
KernelSamplingMap.CorrectBorder sliced the offset span by radius * kernelSize, so bounds narrower than the kernel radius threw ArgumentOutOfRangeException (GaussianBlur of a 130x8 region with sigma 10, for example), and bounds exactly equal to the radius let Bounce offsets escape the sampling range by folding an overshoot of a full extent to min - 1. Route bounds no larger than the radius through an exact per-mode correction that folds each offset into range: clamp for Repeat, periodic reflection for Mirror and Bounce, and true modulo for Wrap. The single-pass corrections are unchanged for larger bounds.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prerequisites
Description
This pull request improves the handling of border conditions in convolution operations when the image bounds are smaller than the kernel radius. It introduces a new method to ensure all sampling offsets are correctly folded into range for all border wrapping modes, and adds comprehensive tests to verify this behavior.
Border handling improvements
CorrectBorderExactmethod inKernelSamplingMap.csto handle cases where the image bounds are smaller than the kernel radius, ensuring all offsets are folded exactly according to the selectedBorderWrappingMode. This prevents out-of-bounds access and guarantees correct sampling for small images. [1] [2]CorrectBorderto callCorrectBorderExactwhen the bounds are no larger than the kernel radius, improving reliability for edge cases.Testing enhancements
KernelSamplingMapTeststo verify that, for all border wrapping modes, sampling offsets stay within bounds when the image is smaller than the kernel radius.GaussianBlurTestto ensure that applying a large-radius Gaussian blur to a solid-color image with small bounds leaves the color unchanged for all border modes, confirming correct border handling.GaussianBlurTest.csto support the new tests.