Add unit tests for SaliencySmartCropper
The golden fixtures only prove determinism; these assert the crop actually biases toward the edge-rich side on both axes, reaches the source edge for an edge-hugging subject, and always returns an in-bounds rect at the target aspect. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
36782f15a1
commit
4695b65a1b
1 file changed
+112
@@ -0,0 +1,112 @@
|
||||
using FrameProcessor.ImagePipeline.SmartCrop;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
|
||||
namespace FrameProcessor.Tests;
|
||||
|
||||
public class SaliencySmartCropperTests
|
||||
{
|
||||
public enum Side { Left, Right, Top, Bottom }
|
||||
|
||||
private static readonly SaliencySmartCropper Cropper = new(NullLogger<SaliencySmartCropper>.Instance);
|
||||
|
||||
[Fact]
|
||||
public void SameAspect_ReturnsFullBounds()
|
||||
{
|
||||
using var image = CreateHalfCheckerboard(1600, 1200, Side.Right);
|
||||
var rect = Cropper.ComputeCropRect(image, 800, 600);
|
||||
Assert.Equal(image.Bounds, rect);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(Side.Right)]
|
||||
[InlineData(Side.Left)]
|
||||
public void WideSource_BiasesTowardEdgeRichSide(Side side)
|
||||
{
|
||||
using var image = CreateHalfCheckerboard(1600, 900, side);
|
||||
var rect = Cropper.ComputeCropRect(image, 1600, 1200);
|
||||
|
||||
var centeredX = (image.Width - rect.Width) / 2;
|
||||
if (side == Side.Right)
|
||||
{
|
||||
Assert.True(rect.X > centeredX, $"Expected crop right of center, got X={rect.X}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.True(rect.X < centeredX, $"Expected crop left of center, got X={rect.X}");
|
||||
}
|
||||
AssertValidCrop(image, rect, 1600, 1200);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(Side.Bottom)]
|
||||
[InlineData(Side.Top)]
|
||||
public void TallSource_BiasesTowardEdgeRichSide(Side side)
|
||||
{
|
||||
using var image = CreateHalfCheckerboard(900, 1600, side);
|
||||
var rect = Cropper.ComputeCropRect(image, 1600, 1200);
|
||||
|
||||
var centeredY = (image.Height - rect.Height) / 2;
|
||||
if (side == Side.Bottom)
|
||||
{
|
||||
Assert.True(rect.Y > centeredY, $"Expected crop below center, got Y={rect.Y}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.True(rect.Y < centeredY, $"Expected crop above center, got Y={rect.Y}");
|
||||
}
|
||||
AssertValidCrop(image, rect, 1600, 1200);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EdgeHuggingSubject_CropReachesSourceEdge()
|
||||
{
|
||||
using var image = CreateHalfCheckerboard(1600, 900, Side.Right);
|
||||
var rect = Cropper.ComputeCropRect(image, 1600, 1200);
|
||||
Assert.Equal(image.Width, rect.Right);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PortraitTargetOnLandscapeSource_ReturnsValidRect()
|
||||
{
|
||||
using var image = CreateHalfCheckerboard(1600, 1200, Side.Right);
|
||||
var rect = Cropper.ComputeCropRect(image, 1200, 1600);
|
||||
AssertValidCrop(image, rect, 1200, 1600);
|
||||
}
|
||||
|
||||
private static void AssertValidCrop(Image image, Rectangle rect, int targetWidth, int targetHeight)
|
||||
{
|
||||
Assert.True(image.Bounds.Contains(rect), $"Crop {rect} outside source bounds {image.Bounds}");
|
||||
var expectedAspect = (double)targetWidth / targetHeight;
|
||||
var actualAspect = (double)rect.Width / rect.Height;
|
||||
Assert.InRange(actualAspect, expectedAspect * 0.99, expectedAspect * 1.01);
|
||||
}
|
||||
|
||||
private static Image<Rgba32> CreateHalfCheckerboard(int width, int height, Side side)
|
||||
{
|
||||
var image = new Image<Rgba32>(width, height);
|
||||
var gray = new Rgba32(0x80, 0x80, 0x80, 0xFF);
|
||||
var black = new Rgba32(0x00, 0x00, 0x00, 0xFF);
|
||||
var white = new Rgba32(0xFF, 0xFF, 0xFF, 0xFF);
|
||||
image.ProcessPixelRows(accessor =>
|
||||
{
|
||||
for (var y = 0; y < accessor.Height; y++)
|
||||
{
|
||||
var row = accessor.GetRowSpan(y);
|
||||
for (var x = 0; x < row.Length; x++)
|
||||
{
|
||||
var edgy = side switch
|
||||
{
|
||||
Side.Left => x < width / 2,
|
||||
Side.Right => x >= width / 2,
|
||||
Side.Top => y < height / 2,
|
||||
_ => y >= height / 2,
|
||||
};
|
||||
row[x] = !edgy ? gray : (((x / 8) + (y / 8)) & 1) == 0 ? black : white;
|
||||
}
|
||||
}
|
||||
});
|
||||
return image;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user