Fix convolution sampling for bounds smaller than the kernel radius

Merged
JI
3mo ago
+143-1

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 :cop:.
  • 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.