Files
Chronos/tests/Titano.Core.Tests/Codecs/JpegEncoderTests.cs
T
Claude 990c515b3b feat(codecs): add baseline and lossless JPEG encoders
Baseline encoder: IJG quality scaling, 4:4:4/4:2:2/4:2:0, optional
optimised Huffman tables, restart intervals, caller APP1 segments,
ImageBuffer input with optional ordered dither; parallel colour
conversion and float FDCT per MCU row, sequential entropy coding.
Lossless (SOF3) encoder with predictors 1-7 and point transform for DNG.

Tests: round-trip PSNR gates, monotonic quality, lossless round trips
for every predictor and precision, seeded fuzzing, restart resync,
DCT scaling and a 24 MP single-core decode benchmark.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:44:20 +00:00

310 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Titano.Core.Codecs.Jpeg;
using Titano.Core.Imaging;
using Titano.Testing;
using static Titano.Core.Tests.Codecs.CodecTestData;
namespace Titano.Core.Tests.Codecs;
/// <summary>Encoder → decoder round trips on synthetic content.</summary>
public sealed class JpegEncoderTests(TestContext context)
{
private const int W = 160, H = 120;
/// <summary>The four synthetic content classes: smooth gradients, colour bars, sensor-like noise, text-like edges.</summary>
private static byte[] Content(string kind)
{
var rgb = new byte[W * H * 3];
var rng = new Random(11);
for (var y = 0; y < H; y++)
{
for (var x = 0; x < W; x++)
{
int r, g, b;
switch (kind)
{
case "gradients":
r = x * 255 / (W - 1);
g = y * 255 / (H - 1);
b = (x + y) * 255 / (W + H - 2);
break;
case "bars":
var bar = x / (W / 8);
r = (bar & 4) != 0 ? 235 : 16;
g = (bar & 2) != 0 ? 235 : 16;
b = (bar & 1) != 0 ? 235 : 16;
break;
case "noise":
// Sensor-like: mostly luma noise (σ ≈ 3.7 levels) plus weak chroma noise.
var n = rng.Next(-6, 7);
r = 120 + x / 4 + n + rng.Next(-2, 3);
g = 110 + y / 4 + n + rng.Next(-2, 3);
b = 100 + n + rng.Next(-2, 3);
break;
default:
var ink = (x % 11 is 2 or 3 && y % 16 < 12) || (y % 16 is 5 && x % 11 < 8);
r = g = b = ink ? 24 : 232;
break;
}
var i = (y * W + x) * 3;
rgb[i] = (byte)r;
rgb[i + 1] = (byte)g;
rgb[i + 2] = (byte)b;
}
}
return rgb;
}
private static double RoundTripPsnr(byte[] rgb, JpegEncodeOptions options, int channels = 3)
{
var jpeg = JpegEncoder.Encode(rgb, W, H, channels, options);
var decoded = JpegDecoder.Decode(jpeg);
return Psnr(decoded.Pixels8, rgb);
}
/// <summary>PSNR of Rec.601 luma (Y′). 4:2:0 halves chroma resolution by design, so saturated chroma edges
/// (colour bars) lose a quarter of the step under triangle upsampling in any codec (libjpeg: 26.7 dB RGB on
/// the same bars); the codec's own fidelity is measured on Y′.</summary>
private static double RoundTripLumaPsnr(byte[] rgb, JpegEncodeOptions options)
{
var decoded = JpegDecoder.Decode(JpegEncoder.Encode(rgb, W, H, 3, options)).Pixels8!;
static double[] Luma(byte[] p)
{
var y = new double[p.Length / 3];
for (var i = 0; i < y.Length; i++) y[i] = 0.299 * p[3 * i] + 0.587 * p[3 * i + 1] + 0.114 * p[3 * i + 2];
return y;
}
return Psnr<double>(Luma(decoded), Luma(rgb), 255);
}
[Theory]
[Case("gradients")]
[Case("bars")]
[Case("noise")]
[Case("text")]
public void HighQuality444Above40Db(string kind)
{
var psnr = RoundTripPsnr(Content(kind), new JpegEncodeOptions { Quality = 95, Subsampling = JpegSubsampling.Yuv444 });
context.Log($"{kind} q95 4:4:4: {psnr:F2} dB");
Assert.GreaterOrEqual(psnr, 40);
}
[Theory]
[Case("gradients")]
[Case("bars")]
[Case("noise")]
[Case("text")]
public void Quality90Yuv420Above34Db(string kind)
{
var options = new JpegEncodeOptions { Quality = 90, Subsampling = JpegSubsampling.Yuv420 };
var psnr = RoundTripLumaPsnr(Content(kind), options);
context.Log($"{kind} q90 4:2:0: Y′ {psnr:F2} dB, RGB {RoundTripPsnr(Content(kind), options):F2} dB");
Assert.GreaterOrEqual(psnr, 34);
}
[Test]
public void PsnrAndSizeGrowWithQuality()
{
var rgb = TestCard(W, H);
var psnr = new List<double>();
var sizes = new List<double>();
foreach (var q in new[] { 5, 20, 40, 60, 75, 85, 95, 100 })
{
var jpeg = JpegEncoder.Encode(rgb, W, H, 3, new JpegEncodeOptions { Quality = q, Subsampling = JpegSubsampling.Yuv422 });
sizes.Add(jpeg.Length);
psnr.Add(Psnr(JpegDecoder.Decode(jpeg).Pixels8, rgb));
}
context.Log("PSNR: " + string.Join(", ", psnr.Select(p => p.ToString("F1"))));
Assert.Monotonic(psnr, strict: true);
Assert.Monotonic(sizes, strict: true);
}
[Test]
public void OptimizedHuffmanIsSmallerAndPixelIdentical()
{
var rgb = TestCard(W, H);
var standard = JpegEncoder.Encode(rgb, W, H, 3, new JpegEncodeOptions { Quality = 85 });
var optimized = JpegEncoder.Encode(rgb, W, H, 3, new JpegEncodeOptions { Quality = 85, OptimizeHuffman = true });
context.Log($"standard {standard.Length} B, optimized {optimized.Length} B");
Assert.Less(optimized.Length, standard.Length);
Assert.BytesEqual(JpegDecoder.Decode(standard).Pixels8, JpegDecoder.Decode(optimized).Pixels8);
}
[Theory]
[Case(1)]
[Case(7)]
[Case(40)]
public void RestartIntervalsDecodeIdentically(int interval)
{
var rgb = TestCard(W, H);
var plain = JpegEncoder.Encode(rgb, W, H, 3, new JpegEncodeOptions { Quality = 80 });
var restart = JpegEncoder.Encode(rgb, W, H, 3, new JpegEncodeOptions { Quality = 80, RestartInterval = interval, OptimizeHuffman = true });
Assert.Equal(interval, JpegDecoder.ReadInfo(restart).RestartInterval);
var decoded = JpegDecoder.Decode(restart);
Assert.Empty(decoded.Warnings);
Assert.BytesEqual(JpegDecoder.Decode(plain).Pixels8, decoded.Pixels8);
}
[Test]
public void App1SegmentsFollowSoiAndReplaceJfif()
{
var exif = "Exif\0\0II*\0\b\0\0\0\0\0\0\0"u8.ToArray();
var xmp = "http://ns.adobe.com/xap/1.0/\0<x:xmpmeta/>"u8.ToArray();
var jpeg = JpegEncoder.Encode(TestCard(32, 16), 32, 16, 3, new JpegEncodeOptions { App1Segments = [exif, xmp] });
Assert.Equal(0xE1, jpeg[3]);
Assert.BytesEqual(exif, jpeg.AsSpan(6, exif.Length));
var second = 4 + (jpeg[4] << 8 | jpeg[5]);
Assert.Equal(0xE1, jpeg[second + 1]);
Assert.False(jpeg.AsSpan().IndexOf("JFIF"u8) >= 0, "JFIF must be omitted when APP1 is supplied");
Assert.Equal(32, JpegDecoder.Decode(jpeg).Width);
Assert.Throws<ArgumentException>(() => JpegEncoder.Encode(TestCard(8, 8), 8, 8, 3, new JpegEncodeOptions { App1Segments = [new byte[65534]] }));
}
[Test]
public void GrayBufferEncodesSingleComponent()
{
using var gray = new ImageBuffer(50, 30, 1);
for (var i = 0; i < gray.PlaneLength; i++) gray.Plane(0)[i] = (i % 50) / 49f;
var jpeg = JpegEncoder.Encode(gray, new JpegEncodeOptions { Quality = 95 });
var info = JpegDecoder.ReadInfo(jpeg);
Assert.Equal(1, info.Components);
using var back = JpegDecoder.DecodeToLinear(jpeg);
Assert.Equal(1, back.Channels);
for (var x = 0; x < 50; x += 7) Assert.Approximately(gray[0, x, 15], back[0, x, 15], 0.02);
}
[Test]
public void LinearBufferRoundTripWithDither()
{
using var image = new ImageBuffer(96, 64);
for (var y = 0; y < 64; y++)
{
for (var x = 0; x < 96; x++)
{
image[0, x, y] = x / 95f;
image[1, x, y] = y / 63f * 0.5f;
image[2, x, y] = 0.18f;
}
}
var jpeg = JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 95, Subsampling = JpegSubsampling.Yuv444, Dither = true });
using var back = JpegDecoder.DecodeToLinear(jpeg);
var reference = PixelConvert.ToInterleaved8(image, 3);
var decoded = PixelConvert.ToInterleaved8(back, 3);
var psnr = Psnr(decoded, reference);
context.Log($"linear round trip {psnr:F1} dB");
Assert.Greater(psnr, 40);
}
[Test]
public void TwelveBitExtendedSequentialDecodes()
{
const int w = 40, h = 24;
var samples = new ushort[w * h];
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++) samples[y * w + x] = (ushort)(x * 4095 / (w - 1) ^ (y * 37 % 256));
}
var jpeg = TwelveBitEncoder.EncodeGray(samples, w, h);
var image = JpegDecoder.Decode(jpeg);
Assert.Equal(12, image.BitDepth);
Assert.Equal(JpegProcess.ExtendedSequential, image.Info.Process);
var psnr = Psnr<ushort>(image.Pixels16, samples, 4095);
context.Log($"12-bit PSNR {psnr:F1} dB");
Assert.Greater(psnr, 50);
using var linear = JpegDecoder.DecodeToLinear(jpeg);
Assert.Approximately(ColorSpace.SrgbToLinear(image.Pixels16![5] / 4095.0), linear[0, 5, 0], 1e-4);
}
/// <summary>Minimal SOF1 12-bit gray encoder (quantiser 1, optimal tables) for decoder tests.</summary>
private static class TwelveBitEncoder
{
public static byte[] EncodeGray(ushort[] samples, int width, int height)
{
int bw = (width + 7) / 8, bh = (height + 7) / 8;
var blocks = new short[bw * bh][];
for (var by = 0; by < bh; by++)
{
for (var bx = 0; bx < bw; bx++)
{
var coef = new short[64];
for (var v = 0; v < 8; v++)
{
for (var u = 0; u < 8; u++)
{
double sum = 0;
for (var y = 0; y < 8; y++)
{
for (var x = 0; x < 8; x++)
{
var s = samples[Math.Min(by * 8 + y, height - 1) * width + Math.Min(bx * 8 + x, width - 1)] - 2048.0;
sum += s * JpegTables.Basis[u * 8 + x] * JpegTables.Basis[v * 8 + y];
}
}
coef[v * 8 + u] = (short)Math.Round(sum);
}
}
blocks[by * bw + bx] = coef;
}
}
var dcFreq = new long[256];
var acFreq = new long[256];
Walk(blocks, (dc, s) => (dc ? dcFreq : acFreq)[s]++, null);
var dcCode = JpegHuffmanCode.Optimal(dcFreq);
var acCode = JpegHuffmanCode.Optimal(acFreq);
var writer = new JpegBitWriter(1 << 16);
Walk(blocks, (dc, s) => { var c = dc ? dcCode : acCode; writer.Reserve(8); writer.Write(c.Code[s], c.Length[s]); }, (v, s) => writer.Write((uint)((v < 0 ? v - 1 : v) & ((1 << s) - 1)), s));
writer.Flush();
using var ms = new MemoryStream();
ms.Write([0xFF, 0xD8]);
var dqt = new byte[129];
dqt[0] = 0x10;
for (var k = 0; k < 64; k++) dqt[2 + 2 * k] = 1;
JpegEncoder.Segment(ms, 0xDB, dqt);
JpegEncoder.Segment(ms, 0xC1, [12, (byte)(height >> 8), (byte)height, (byte)(width >> 8), (byte)width, 1, 1, 0x11, 0]);
var dht = new MemoryStream();
dht.WriteByte(0x00);
dht.Write(dcCode.Bits);
dht.Write(dcCode.Values);
dht.WriteByte(0x10);
dht.Write(acCode.Bits);
dht.Write(acCode.Values);
JpegEncoder.Segment(ms, 0xC4, dht.ToArray());
JpegEncoder.Segment(ms, 0xDA, [1, 1, 0x00, 0, 63, 0]);
ms.Write(writer.Written);
ms.Write([0xFF, 0xD9]);
return ms.ToArray();
}
private static void Walk(short[][] blocks, Action<bool, int> symbol, Action<int, int>? bits)
{
var pred = 0;
foreach (var b in blocks)
{
var diff = b[0] - pred;
pred = b[0];
var s = Size(diff);
symbol(true, s);
if (s > 0) bits?.Invoke(diff, s);
var run = 0;
for (var k = 1; k < 64; k++)
{
int v = b[JpegTables.NaturalOrder[k]];
if (v == 0)
{
run++;
continue;
}
for (; run >= 16; run -= 16) symbol(false, 0xF0);
s = Size(v);
symbol(false, run << 4 | s);
bits?.Invoke(v, s);
run = 0;
}
if (run > 0) symbol(false, 0);
}
}
private static int Size(int v) => v == 0 ? 0 : 32 - System.Numerics.BitOperations.LeadingZeroCount((uint)Math.Abs(v));
}
}