using Titano.Core.Codecs.Jpeg; using Titano.Core.Imaging; using Titano.Testing; using static Titano.Core.Tests.Codecs.CodecTestData; namespace Titano.Core.Tests.Codecs; /// DCT-domain scaling (1/2, 1/4, 1/8) and the linear-light decode path. public sealed class JpegScalingTests(TestContext context) { private const int W = 256, H = 192; [Theory] [Case(0, 256, 192)] [Case(200, 256, 192)] [Case(128, 128, 96)] [Case(100, 128, 96)] [Case(64, 64, 48)] [Case(33, 64, 48)] [Case(32, 32, 24)] [Case(1, 32, 24)] public void MaxLongEdgePicksSmallestSufficientScale(int maxLongEdge, int width, int height) { var jpeg = JpegEncoder.Encode(SmoothImage(W, H), W, H, 3); var image = JpegDecoder.Decode(jpeg, new JpegDecodeOptions { MaxLongEdge = maxLongEdge }); Assert.Equal(width, image.Width); Assert.Equal(height, image.Height); Assert.Equal(W, image.Info.Width); } [Theory] [Case(2, 38.0)] [Case(4, 34.0)] [Case(8, 30.0)] public void ScaledDecodeApproximatesAreaAverage(int factor, double minPsnr) { var jpeg = JpegEncoder.Encode(SmoothImage(W, H), W, H, 3, new JpegEncodeOptions { Quality = 92 }); var full = JpegDecoder.Decode(jpeg).Pixels8!; var scaled = JpegDecoder.Decode(jpeg, new JpegDecodeOptions { MaxLongEdge = W / factor }); Assert.Equal(W / factor, scaled.Width); int sw = W / factor, sh = H / factor; var expected = new byte[sw * sh * 3]; for (var y = 0; y < sh; y++) { for (var x = 0; x < sw; x++) { for (var c = 0; c < 3; c++) { var sum = 0; for (var dy = 0; dy < factor; dy++) { for (var dx = 0; dx < factor; dx++) sum += full[((y * factor + dy) * W + x * factor + dx) * 3 + c]; } expected[(y * sw + x) * 3 + c] = (byte)((sum + factor * factor / 2) / (factor * factor)); } } } var psnr = Psnr(scaled.Pixels8, expected); context.Log($"1/{factor}: {psnr:F1} dB vs area average"); Assert.Greater(psnr, minPsnr); } [Test] public void ScaledOddSizeKeepsEdges() { var image = JpegDecoder.Decode(Fixture("jpeg/b420.jpg"), new JpegDecodeOptions { MaxLongEdge = 31 }); Assert.Equal(31, image.Width); Assert.Equal(23, image.Height); var reference = ReadPnm(Fixture("src.ppm")); // Bottom-right pixel of the scaled image must still resemble the source corner (no padding garbage). var pixels = image.Pixels8!; var last = pixels.AsSpan(pixels.Length - 3); var src = reference.Samples.AsSpan(reference.Samples.Length - 3); for (var c = 0; c < 3; c++) Assert.InRange(last[c], src[c] - 70, src[c] + 70); } [Test] public void LinearDecodeMatchesLookupOfEightBitDecode() { var data = Fixture("jpeg/b422.jpg"); var bytes = JpegDecoder.Decode(data).Pixels8!; using var linear = JpegDecoder.DecodeToLinear(data); Assert.Equal(3, linear.Channels); for (var i = 0; i < 61 * 45; i += 17) { for (var c = 0; c < 3; c++) Assert.Equal(ColorSpace.Linear8(bytes[i * 3 + c]), linear.Plane(c)[i]); } using var gray = JpegDecoder.DecodeToLinear(Fixture("jpeg/gray.jpg")); Assert.Equal(1, gray.Channels); using var expanded = JpegDecoder.DecodeToLinear(Fixture("jpeg/gray.jpg"), expandGray: true); Assert.Equal(3, expanded.Channels); Assert.SequenceEqual(gray.Plane(0).ToArray(), expanded.Plane(2).ToArray()); } }