feat(codecs): add managed JPEG decoder and pixel conversions
Baseline, extended 12-bit, progressive and lossless (SOF3) Huffman decoding; 1/3/4 components with Adobe CMYK/YCCK; all integral sampling factors with libjpeg fancy upsampling; restart resynchronisation; DCT-domain scaling. Full-size blocks use a port of libjpeg's ISLOW IDCT, so output is bit-identical to djpeg on the cjpeg fixtures. PixelConvert maps interleaved 8/16-bit samples to linear ImageBuffers and back, gray/RGB, Exif orientation and clamping. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
70 files changed
+2618
No files matched your search
@@ -0,0 +1,79 @@
|
||||
using Titano.Core.Codecs.Jpeg;
|
||||
using Titano.Testing;
|
||||
using static Titano.Core.Tests.Codecs.CodecTestData;
|
||||
|
||||
namespace Titano.Core.Tests.Codecs;
|
||||
|
||||
/// <summary>Decoder output against libjpeg-turbo's djpeg (accurate integer IDCT) on cjpeg-produced fixtures.</summary>
|
||||
public sealed class JpegDecoderTests(TestContext context)
|
||||
{
|
||||
[Theory]
|
||||
[Case("b444", "4:4:4")]
|
||||
[Case("b422", "4:2:2")]
|
||||
[Case("b420", "4:2:0")]
|
||||
[Case("b440", "4:4:0")]
|
||||
[Case("b411", "4:1:1")]
|
||||
[Case("p420", "4:2:0")]
|
||||
[Case("p444", "4:4:4")]
|
||||
[Case("r420", "4:2:0")]
|
||||
[Case("r444", "4:4:4")]
|
||||
public void MatchesDjpeg(string name, string sampling)
|
||||
{
|
||||
var data = Fixture($"jpeg/{name}.jpg");
|
||||
var reference = ReadPnm(Fixture($"jpeg/{name}.ppm"));
|
||||
var image = JpegDecoder.Decode(data);
|
||||
Assert.Equal(reference.Width, image.Width);
|
||||
Assert.Equal(reference.Height, image.Height);
|
||||
Assert.Equal(3, image.Components);
|
||||
Assert.Equal(sampling, image.Info.SamplingNotation);
|
||||
Assert.Empty(image.Warnings);
|
||||
var diff = MaxAbsDiff(image.Pixels8, reference.Samples);
|
||||
context.Log($"{name}: max |diff| = {diff}");
|
||||
Assert.LessOrEqual(diff, 2);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Case("gray")]
|
||||
[Case("pgray")]
|
||||
public void GrayMatchesDjpeg(string name)
|
||||
{
|
||||
var image = JpegDecoder.Decode(Fixture($"jpeg/{name}.jpg"));
|
||||
var reference = ReadPnm(Fixture($"jpeg/{name}.pgm"));
|
||||
Assert.Equal(1, image.Components);
|
||||
Assert.Equal(JpegColorSpace.Gray, image.Info.ColorSpace);
|
||||
Assert.LessOrEqual(MaxAbsDiff(image.Pixels8, reference.Samples), 2);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReplicatedUpsamplingMatchesDjpegNoSmooth()
|
||||
{
|
||||
var image = JpegDecoder.Decode(Fixture("jpeg/b420.jpg"), new JpegDecodeOptions { FancyUpsampling = false });
|
||||
Assert.LessOrEqual(MaxAbsDiff(image.Pixels8, ReadPnm(Fixture("jpeg/b420-nosmooth.ppm")).Samples), 2);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadInfoParsesHeadersOnly()
|
||||
{
|
||||
var info = JpegDecoder.ReadInfo(Fixture("jpeg/p420.jpg"));
|
||||
Assert.Equal(61, info.Width);
|
||||
Assert.Equal(45, info.Height);
|
||||
Assert.Equal(JpegProcess.Progressive, info.Process);
|
||||
Assert.Equal(JpegColorSpace.YCbCr, info.ColorSpace);
|
||||
Assert.Equal(8, info.Precision);
|
||||
var restart = JpegDecoder.ReadInfo(Fixture("jpeg/r444.jpg"));
|
||||
Assert.Equal(5, restart.RestartInterval);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AdobeYcckIsConvertedToRgb()
|
||||
{
|
||||
var image = JpegDecoder.Decode(Fixture("jpeg/cmyk.jpg"));
|
||||
Assert.Equal(JpegColorSpace.Ycck, image.Info.ColorSpace);
|
||||
Assert.Equal(4, image.Info.Components);
|
||||
Assert.Equal(3, image.Components);
|
||||
var source = ReadPnm(Fixture("src.ppm"));
|
||||
var psnr = Psnr(image.Pixels8, source.Bytes);
|
||||
context.Log($"YCCK round trip PSNR {psnr:F1} dB");
|
||||
Assert.Greater(psnr, 30);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user