Skip to content

Fix convolution sampling for bounds smaller than the kernel radius#3152

Merged
JimBobSquarePants merged 1 commit into
mainfrom
js/fix-convolution-small-bounds
Jul 14, 2026
Merged

Fix convolution sampling for bounds smaller than the kernel radius#3152
JimBobSquarePants merged 1 commit into
mainfrom
js/fix-convolution-small-bounds

Conversation

@JimBobSquarePants

Copy link
Copy Markdown
Member

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

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

  • Added the CorrectBorderExact method in KernelSamplingMap.cs to handle cases where the image bounds are smaller than the kernel radius, ensuring all offsets are folded exactly according to the selected BorderWrappingMode. This prevents out-of-bounds access and guarantees correct sampling for small images. [1] [2]
  • Updated the logic in CorrectBorder to call CorrectBorderExact when the bounds are no larger than the kernel radius, improving reliability for edge cases.

Testing enhancements

  • Added a new test class KernelSamplingMapTests to verify that, for all border wrapping modes, sampling offsets stay within bounds when the image is smaller than the kernel radius.
  • Added a test in GaussianBlurTest to 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.
  • Included necessary using directives in GaussianBlurTest.cs to support the new tests.

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.
@JimBobSquarePants JimBobSquarePants merged commit 5a0a74f into main Jul 14, 2026
12 checks passed
@JimBobSquarePants JimBobSquarePants deleted the js/fix-convolution-small-bounds branch July 14, 2026 02:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant