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
129 lines
4.8 KiB
C#
129 lines
4.8 KiB
C#
using Titano.Core.Imaging;
|
||
using Titano.Testing;
|
||
|
||
namespace Titano.Core.Tests.Imaging;
|
||
|
||
public sealed class PixelConvertTests
|
||
{
|
||
[Theory]
|
||
[Case(1)]
|
||
[Case(2)]
|
||
[Case(3)]
|
||
[Case(4)]
|
||
public void EightBitRoundTripIsExact(int channels)
|
||
{
|
||
const int w = 19, h = 7;
|
||
var pixels = new byte[w * h * channels];
|
||
new Random(channels).NextBytes(pixels);
|
||
var keepAlpha = channels is 2 or 4;
|
||
using var image = PixelConvert.FromInterleaved8(pixels, w, h, channels, outputChannels: keepAlpha ? 4 : 0);
|
||
Assert.Equal(keepAlpha ? 4 : channels == 1 ? 1 : 3, image.Channels);
|
||
var back = PixelConvert.ToInterleaved8(image, channels);
|
||
Assert.SequenceEqual(pixels, back);
|
||
}
|
||
|
||
[Test]
|
||
public void SixteenBitRoundTripWithinOneCode()
|
||
{
|
||
var samples = Enumerable.Range(0, 3 * 4096).Select(i => (ushort)(i * 16 % 65536)).ToArray();
|
||
using var image = PixelConvert.FromInterleaved16(samples, 64, 64, 3);
|
||
var back = PixelConvert.ToInterleaved16(image, 3);
|
||
for (var i = 0; i < samples.Length; i++) Assert.InRange(back[i], samples[i] - 1, samples[i] + 1);
|
||
using var linear = PixelConvert.FromInterleaved16(samples, 64, 64, 3, TransferFunction.Linear);
|
||
Assert.SequenceEqual(samples, PixelConvert.ToInterleaved16(linear, 3, TransferFunction.Linear));
|
||
}
|
||
|
||
[Test]
|
||
public void CustomMaximumScalesTwelveBitData()
|
||
{
|
||
ushort[] samples = [0, 2048, 4095];
|
||
using var image = PixelConvert.FromInterleaved16(samples, 3, 1, 1, TransferFunction.Linear, maxValue: 4095);
|
||
Assert.Approximately(2048 / 4095.0, image[0, 1, 0], 1e-6);
|
||
Assert.Equal(1f, image[0, 2, 0]);
|
||
}
|
||
|
||
[Test]
|
||
public void GrayRgbConversions()
|
||
{
|
||
using var gray = PixelConvert.FromInterleaved8([0, 128, 255], 3, 1, 1);
|
||
using var rgb = PixelConvert.GrayToRgb(gray);
|
||
Assert.Equal(3, rgb.Channels);
|
||
Assert.Equal(gray[0, 1, 0], rgb[2, 1, 0]);
|
||
using var back = PixelConvert.RgbToGray(rgb);
|
||
Assert.Approximately(gray[0, 1, 0], back[0, 1, 0], 1e-6);
|
||
using var colour = PixelConvert.FromInterleaved8([255, 0, 0], 1, 1, 3, TransferFunction.Linear);
|
||
Assert.Equal((byte)Math.Round(ColorSpace.LinearToSrgb(ColorSpace.LumaR) * 255), PixelConvert.ToInterleaved8(colour, 1)[0]);
|
||
}
|
||
|
||
[Theory]
|
||
[Case(1)]
|
||
[Case(2)]
|
||
[Case(3)]
|
||
[Case(4)]
|
||
[Case(5)]
|
||
[Case(6)]
|
||
[Case(7)]
|
||
[Case(8)]
|
||
public void OrientationMatchesExifDefinition(int orientation)
|
||
{
|
||
// A 3×2 image with unique values; Exif row 0 / column 0 placement per orientation (CIPA DC-008 table).
|
||
using var source = new ImageBuffer(3, 2, 1);
|
||
for (var i = 0; i < 6; i++) source.Plane(0)[i] = i;
|
||
using var result = PixelConvert.ApplyOrientation(source, orientation);
|
||
float[] expected = orientation switch
|
||
{
|
||
1 => [0, 1, 2, 3, 4, 5],
|
||
2 => [2, 1, 0, 5, 4, 3],
|
||
3 => [5, 4, 3, 2, 1, 0],
|
||
4 => [3, 4, 5, 0, 1, 2],
|
||
5 => [0, 3, 1, 4, 2, 5],
|
||
6 => [3, 0, 4, 1, 5, 2],
|
||
7 => [5, 2, 4, 1, 3, 0],
|
||
_ => [2, 5, 1, 4, 0, 3],
|
||
};
|
||
Assert.Equal(orientation >= 5 ? 2 : 3, result.Width);
|
||
Assert.SequenceEqual(expected, result.Plane(0).ToArray());
|
||
}
|
||
|
||
[Test]
|
||
public void LargeOrientationUsesTilesCorrectly()
|
||
{
|
||
using var source = new ImageBuffer(301, 157, 3);
|
||
for (var i = 0; i < source.Data.Length; i++) source.Data[i] = i;
|
||
using var rotated = PixelConvert.ApplyOrientation(source, 6);
|
||
using var back = PixelConvert.ApplyOrientation(rotated, 8);
|
||
Assert.SequenceEqual(source.Data.ToArray(), back.Data.ToArray());
|
||
}
|
||
|
||
[Test]
|
||
public void ClampHandlesNaNAndRange()
|
||
{
|
||
var values = Enumerable.Range(0, 37).Select(i => i % 5 == 0 ? float.NaN : i / 10f - 1f).ToArray();
|
||
PixelConvert.Clamp(values);
|
||
for (var i = 0; i < values.Length; i++) Assert.InRange(values[i], 0, 1);
|
||
Assert.Equal(0f, values[5]);
|
||
}
|
||
|
||
[Test]
|
||
public void DitherBreaksBandingButKeepsTheMean()
|
||
{
|
||
using var ramp = new ImageBuffer(256, 64, 1);
|
||
for (var y = 0; y < 64; y++)
|
||
{
|
||
for (var x = 0; x < 256; x++) ramp[0, x, y] = (float)ColorSpace.SrgbToLinear((100 + x / 64.0) / 255);
|
||
}
|
||
var plain = PixelConvert.ToInterleaved8(ramp, 1);
|
||
var dithered = PixelConvert.ToInterleaved8(ramp, 1, dither: true);
|
||
Assert.True(plain.Distinct().Count() <= 5, "a 4-code ramp quantises to few levels");
|
||
for (var x0 = 0; x0 < 256; x0 += 64)
|
||
{
|
||
var mean = 0.0;
|
||
for (var y = 0; y < 64; y++)
|
||
{
|
||
for (var x = x0; x < x0 + 64; x++) mean += dithered[y * 256 + x];
|
||
}
|
||
Assert.Approximately(100 + (x0 + 31.5) / 64.0, mean / (64 * 64), 0.15);
|
||
}
|
||
}
|
||
}
|