Reads CFA and LinearRaw DNGs (compression 1/7/8/34892), linearises with LinearizationTable, BlackLevel (repeat + deltas) and WhiteLevel over the ActiveArea, applies OpcodeList1-3 (GainMap, WarpRectilinear, FixVignetteRadial, FixBadPixels*, TrimBounds, MapTable/MapPolynomial, Delta/ScalePer*), solves white balance per DNG 1.6 chapter 6 (CCT interpolation, ForwardMatrix, Bradford to D65), demosaics with AHD (Hirakawa & Parks 2005), bilinear, a neighbourhood interpolation for non-Bayer CFAs or binning, and reconstructs clipped highlights. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
95 lines
4.3 KiB
C#
95 lines
4.3 KiB
C#
using System.Buffers.Binary;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using Titano.Core.Codecs.Raw;
|
|
using Titano.Core.Codecs.Tiff;
|
|
using Titano.Core.Metadata.Tiff;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Core.Tests.Raw;
|
|
|
|
/// <summary>24-megapixel Bayer DNG development timing: AHD at full size and binned Draft at half size, all cores.</summary>
|
|
[Serial]
|
|
[Category("Performance")]
|
|
public sealed class RawPerformanceTests(TestContext context)
|
|
{
|
|
private const int W = 6000, H = 4000;
|
|
|
|
[Test]
|
|
[Timeout(60_000)]
|
|
public void Develops24MegapixelDngWithinBudget()
|
|
{
|
|
// A Debug build disables the JIT optimiser in Titano.Core: timings would measure the build, not the pipeline.
|
|
if (typeof(RawDecoder).Assembly.GetCustomAttribute<DebuggableAttribute>()?.IsJITOptimizerDisabled == true)
|
|
TestContext.Skip("timing gate needs an optimised build (dotnet run -c Release)");
|
|
var dng = Mosaic();
|
|
var ahd = Best(dng, RawDecodeOptions.Default, out var size);
|
|
Assert.Equal(W, size.Width);
|
|
var half = Best(dng, new RawDecodeOptions { Quality = DemosaicQuality.Draft, Binning = true }, out var halfSize);
|
|
Assert.Equal(W / 2, halfSize.Width);
|
|
context.Log($"24 MP DNG: AHD full size {ahd.TotalMilliseconds:F0} ms, binned half size {half.TotalMilliseconds:F0} ms ({Environment.ProcessorCount} cores)");
|
|
Console.WriteLine($"[perf] {context.LogText.Trim()}");
|
|
Assert.Less(ahd.TotalSeconds, 4.0);
|
|
Assert.Less(half.TotalSeconds, 1.0);
|
|
}
|
|
|
|
/// <summary>Best of two runs (the machine is shared; the first run also pays JIT warm-up).</summary>
|
|
private static TimeSpan Best(byte[] dng, RawDecodeOptions options, out (int Width, int Height) size)
|
|
{
|
|
var best = TimeSpan.MaxValue;
|
|
size = default;
|
|
for (var run = 0; run < 2; run++)
|
|
{
|
|
var sw = Stopwatch.StartNew();
|
|
using var result = RawDecoder.Decode(new MemoryStream(dng, writable: false), "perf.dng", options);
|
|
best = sw.Elapsed < best ? sw.Elapsed : best;
|
|
size = (result.Image.Width, result.Image.Height);
|
|
}
|
|
return best;
|
|
}
|
|
|
|
/// <summary>Uncompressed 14-bit RGGB DNG with photo-like texture (smooth illumination plus detail and noise).</summary>
|
|
private static byte[] Mosaic()
|
|
{
|
|
var data = new byte[W * H * 2];
|
|
Parallel.For(0, H, y =>
|
|
{
|
|
var state = (uint)(y * 2654435761u + 7);
|
|
for (var x = 0; x < W; x++)
|
|
{
|
|
state ^= state << 13;
|
|
state ^= state >> 17;
|
|
state ^= state << 5;
|
|
var colour = (y & 1) * 2 + (x & 1);
|
|
var level = 1500 + 5000 * (0.5 + 0.5 * double.Sin(x * 0.01) * double.Cos(y * 0.013)) * (colour is 1 or 2 ? 1.0 : 0.6)
|
|
+ 800 * double.Sin(x * 0.4 + y * 0.3) + (state & 127);
|
|
BinaryPrimitives.WriteUInt16LittleEndian(data.AsSpan((y * W + x) * 2), (ushort)double.Clamp(level, 0, 16383));
|
|
}
|
|
});
|
|
using var output = new MemoryStream();
|
|
output.Write("II*\0\0\0\0\0"u8);
|
|
var ifd = new TiffDirectory();
|
|
ifd.Long(TiffTags.NewSubfileType, 0);
|
|
ifd.Long(TiffTags.ImageWidth, W);
|
|
ifd.Long(TiffTags.ImageLength, H);
|
|
ifd.Short(TiffTags.BitsPerSample, 16);
|
|
ifd.Short(TiffTags.Compression, 1);
|
|
ifd.Short(TiffTags.PhotometricInterpretation, 32803);
|
|
ifd.Short(TiffTags.SamplesPerPixel, 1);
|
|
ifd.Long(TiffTags.StripOffsets, 8);
|
|
ifd.Long(TiffTags.StripByteCounts, (uint)data.Length);
|
|
ifd.Long(TiffTags.RowsPerStrip, H);
|
|
ifd.Short(TiffTags.CfaRepeatPatternDim, 2, 2);
|
|
ifd.Bytes(TiffTags.CfaPattern, 0, 1, 1, 2);
|
|
ifd.Bytes(TiffTags.DngVersion, 1, 4, 0, 0);
|
|
ifd.Short(TiffTags.BlackLevel, 1024);
|
|
ifd.Short(TiffTags.WhiteLevel, 16383);
|
|
ifd.SRational(TiffTags.ColorMatrix1, (6722, 10000), (-635, 10000), (-963, 10000), (-4287, 10000), (12460, 10000), (2028, 10000), (-908, 10000), (2162, 10000), (5668, 10000));
|
|
ifd.Short(TiffTags.CalibrationIlluminant1, 21);
|
|
ifd.Rational(TiffTags.AsShotNeutral, (500u, 1000u), (1000u, 1000u), (700u, 1000u));
|
|
output.Write(data);
|
|
TiffWriter.WriteDirectory(output, 0, ifd);
|
|
return output.ToArray();
|
|
}
|
|
}
|