Two-pass Lanczos3 (downscale, support stretched by the reduction) and Catmull-Rom (upscale) with normalised, edge-folded weights; exact area-average box path for thumbnails; pass order chosen by cost, Vector<float> inner loops, parallel rows. Tests cover constant images, energy preservation, step ringing against the kernel's exact discrete bound, Exif orientations and dithering. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
157 lines
6.3 KiB
C#
157 lines
6.3 KiB
C#
using System.Diagnostics;
|
||
using Titano.Core.Imaging;
|
||
using Titano.Testing;
|
||
|
||
namespace Titano.Core.Tests.Imaging;
|
||
|
||
public sealed class ResamplerTests(TestContext context)
|
||
{
|
||
private static ImageBuffer Filled(int w, int h, Func<int, int, int, float> value, int channels = 3)
|
||
{
|
||
var image = new ImageBuffer(w, h, channels);
|
||
for (var c = 0; c < channels; c++)
|
||
{
|
||
for (var y = 0; y < h; y++)
|
||
{
|
||
for (var x = 0; x < w; x++) image[c, x, y] = value(c, x, y);
|
||
}
|
||
}
|
||
return image;
|
||
}
|
||
|
||
[Theory]
|
||
[Case(ResampleFilter.Lanczos3, 97, 31)]
|
||
[Case(ResampleFilter.Lanczos3, 400, 260)]
|
||
[Case(ResampleFilter.CatmullRom, 400, 260)]
|
||
[Case(ResampleFilter.CatmullRom, 20, 15)]
|
||
[Case(ResampleFilter.Box, 37, 23)]
|
||
[Case(ResampleFilter.Box, 333, 211)]
|
||
[Case(ResampleFilter.Auto, 300, 40)]
|
||
public void ConstantImageStaysConstant(ResampleFilter filter, int width, int height)
|
||
{
|
||
using var source = Filled(150, 100, (c, _, _) => 0.25f + 0.25f * c);
|
||
using var result = Resampler.Resize(source, width, height, filter);
|
||
Assert.Equal(width, result.Width);
|
||
Assert.Equal(height, result.Height);
|
||
for (var c = 0; c < 3; c++)
|
||
{
|
||
foreach (var v in result.Plane(c)) Assert.Approximately(0.25 + 0.25 * c, v, 1e-5);
|
||
}
|
||
}
|
||
|
||
[Theory]
|
||
[Case(ResampleFilter.Lanczos3)]
|
||
[Case(ResampleFilter.Box)]
|
||
[Case(ResampleFilter.CatmullRom)]
|
||
public void DownscalePreservesEnergy(ResampleFilter filter)
|
||
{
|
||
var rng = new Random(4);
|
||
using var source = Filled(640, 480, (_, x, y) => (float)(rng.NextDouble() * 0.5 + 0.4 * Math.Sin(x * 0.05) * Math.Cos(y * 0.07) + 0.4), 1);
|
||
using var result = Resampler.Resize(source, 213, 157, filter);
|
||
double Mean(ImageBuffer b) => b.Plane(0).ToArray().Average(v => (double)v);
|
||
var ratio = Mean(result) / Mean(source);
|
||
context.Log($"{filter}: mean ratio {ratio:F5}");
|
||
Assert.Approximately(1, ratio, 0.005);
|
||
}
|
||
|
||
[Theory]
|
||
[Case(ResampleFilter.Lanczos3, 0.3)]
|
||
[Case(ResampleFilter.Lanczos3, 2.7)]
|
||
[Case(ResampleFilter.CatmullRom, 3.0)]
|
||
[Case(ResampleFilter.CatmullRom, 0.45)]
|
||
[Case(ResampleFilter.Box, 0.3)]
|
||
public void StepRingingStaysWithinKernelBounds(ResampleFilter filter, double scale)
|
||
{
|
||
const int w = 200;
|
||
using var step = Filled(w, 4, (_, x, _) => x < w / 2 + 0.3 ? 0f : 1f, 1);
|
||
using var result = Resampler.Resize(step, (int)(w * scale), 4, filter);
|
||
var values = result.Row(0, 1).ToArray();
|
||
var bound = StepBound(filter, scale);
|
||
context.Log($"{filter} ×{scale}: range [{values.Min():F4}, {values.Max():F4}], bound {bound:F4}");
|
||
Assert.GreaterOrEqual(values.Min(), -bound - 1e-4);
|
||
Assert.LessOrEqual(values.Max(), 1 + bound + 1e-4);
|
||
if (filter == ResampleFilter.Box)
|
||
{
|
||
Assert.GreaterOrEqual(values.Min(), 0);
|
||
Assert.LessOrEqual(values.Max(), 1 + 1e-6);
|
||
}
|
||
Assert.Approximately(0, values[0], 1e-5);
|
||
Assert.Approximately(1, values[^1], 1e-5);
|
||
}
|
||
|
||
/// <summary>
|
||
/// The "Lanczos bound" for a step at this scale: every output sample is a normalised sum of kernel taps spaced
|
||
/// 1/stretch apart, so on a step it equals a partial sum of those taps. The bound is the largest excursion
|
||
/// outside [0, 1] of any partial sum, over all sub-pixel phases.
|
||
/// </summary>
|
||
private static double StepBound(ResampleFilter filter, double scale)
|
||
{
|
||
if (filter == ResampleFilter.Box) return 0;
|
||
var radius = filter == ResampleFilter.Lanczos3 ? 3.0 : 2.0;
|
||
var stretch = Math.Max(1, 1 / scale);
|
||
var reach = (int)Math.Ceiling(radius * stretch) + 1;
|
||
var worst = 0.0;
|
||
for (var phase = 0.0; phase < 1; phase += 1 / 512.0)
|
||
{
|
||
var taps = new List<double>();
|
||
for (var k = -reach; k <= reach; k++) taps.Add(Kernel(filter, (k + phase) / stretch));
|
||
var total = taps.Sum();
|
||
var running = 0.0;
|
||
foreach (var t in taps)
|
||
{
|
||
running += t / total;
|
||
worst = Math.Max(worst, Math.Max(running - 1, -running));
|
||
}
|
||
}
|
||
return worst;
|
||
}
|
||
|
||
private static double Kernel(ResampleFilter filter, double x)
|
||
{
|
||
var a = Math.Abs(x);
|
||
if (filter == ResampleFilter.Lanczos3) return a < 1e-12 ? 1 : a >= 3 ? 0 : 3 * Math.Sin(Math.PI * a) * Math.Sin(Math.PI * a / 3) / (Math.PI * Math.PI * a * a);
|
||
return a < 1 ? (1.5 * a - 2.5) * a * a + 1 : a < 2 ? ((-0.5 * a + 2.5) * a - 4) * a + 2 : 0;
|
||
}
|
||
|
||
[Test]
|
||
public void ThumbnailFitsAndAveragesAreas()
|
||
{
|
||
using var source = Filled(1200, 800, (c, x, _) => x % 2 == 0 ? 1f : 0f);
|
||
using var thumb = Resampler.Thumbnail(source, 300);
|
||
Assert.Equal(300, thumb.Width);
|
||
Assert.Equal(200, thumb.Height);
|
||
foreach (var v in thumb.Plane(1)) Assert.Approximately(0.5, v, 1e-5);
|
||
Assert.Equal((300, 200), Resampler.FitWithin(1200, 800, 300));
|
||
Assert.Equal((10, 20), Resampler.FitWithin(10, 20, 300));
|
||
using var small = Resampler.Thumbnail(thumb, 1000);
|
||
Assert.Equal(300, small.Width);
|
||
}
|
||
|
||
[Test]
|
||
public void UpscaleInterpolatesThroughSamples()
|
||
{
|
||
using var source = Filled(16, 16, (_, x, y) => x * 0.05f + y * 0.01f, 1);
|
||
using var up = Resampler.Resize(source, 64, 64, ResampleFilter.CatmullRom);
|
||
// Catmull-Rom reproduces linear ramps exactly away from the edges.
|
||
for (var x = 8; x < 56; x++)
|
||
{
|
||
var sx = (x + 0.5) / 4 - 0.5;
|
||
Assert.Approximately(sx * 0.05 + ((20 + 0.5) / 4 - 0.5) * 0.01, up[0, x, 20], 1e-4);
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public void TwentyFourMegapixelThumbnailIsFast()
|
||
{
|
||
using var source = new ImageBuffer(6000, 4000, 3, clear: true);
|
||
var sw = Stopwatch.StartNew();
|
||
using var thumb = Resampler.Thumbnail(source, 320);
|
||
var box = sw.Elapsed;
|
||
sw.Restart();
|
||
using var lanczos = Resampler.Resize(source, 1920, 1280);
|
||
context.Log($"24 MP → 320 px box {box.TotalMilliseconds:F0} ms; → 1920 px Lanczos3 {sw.Elapsed.TotalMilliseconds:F0} ms");
|
||
Assert.Equal(320, thumb.Width);
|
||
Assert.Less(box.TotalSeconds, 2.5);
|
||
}
|
||
}
|