Skip to content

Extend the buffer by a row for memory safety, add tests

Tim Starling requested to merge work/tstarling/scary-interpolation into main

If phi is exactly pi, or if theta is exactly 2pi, the float source image coordinate will be exactly at the edge of the image. When interpolate() takes the floor of that coordinate and adds 1, the resulting integer coordinate will be out of bounds, although with a coefficient of zero. It will read out of bounds memory and multiply it by zero.

So, add a row and a pixel to the input memory buffer. If the x coordinate is out of bounds, it will naturally wrap around to the next row, which is harmless. If the y coordinate is out of bounds, it will now access the extra row, which is zero-filled by resize(). If both the x and y coordinates are out of bounds, it will access the extra pixel.

We could check bounds inside the inner loop instead, but that has a substantial performance penalty.

Adjust the assertions to account for this situation.

Add tests for the range and accuracy of our atan2() approximation. Add tests for rounded interpolation bounds, demonstrating that it can't go more than one pixel outside the image.

Merge request reports