Each container reader builds a RawFrame for the shared pipeline: CR2 sliced lossless JPEG with SensorInfo crop, masked-column black and ColorData WB; NEF uncompressed and Nikon Huffman (lossy type 1, lossless, lossy type 2 incl. split); ARW uncompressed and ARW2 cRAW with tone curve; PEF uncompressed and Pentax Huffman; RAF uncompressed Bayer and X-Trans with FujiIFD. Colour comes from a dcraw adobe_coeff matrix table; bodies without an entry are reported unsupported so the caller uses the embedded preview. Native RAWs detect the sensor's saturation level for clipping only, never rescaling per frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
93 lines
3.9 KiB
C#
93 lines
3.9 KiB
C#
using Titano.Core.Codecs.Raw;
|
|
using Titano.Core.Imaging;
|
|
using Titano.Testing;
|
|
using static Titano.Core.Tests.Raw.NativeRawWriters;
|
|
|
|
namespace Titano.Core.Tests.Raw;
|
|
|
|
/// <summary>Truncated and corrupted RAW files may only raise <see cref="InvalidDataException"/> (or decode).</summary>
|
|
public sealed class RawFuzzTests(TestContext context)
|
|
{
|
|
private const int W = 48, H = 32;
|
|
|
|
private static byte[] Sample(string format)
|
|
{
|
|
using var scene = SyntheticRaw.Smooth(W, H);
|
|
return format switch
|
|
{
|
|
"dng" => SyntheticDng.Write(scene, new DngSpec { TileSize = 16, OpcodeList2 = GainMapList(), OpcodeList3 = new OpcodeListBuilder().Add(3, 0, p => p.F64(0.1).F64(0).F64(0).F64(0).F64(0).F64(0.5).F64(0.5)).Build() }),
|
|
"linear.dng" => SyntheticDng.Write(scene, new DngSpec { Cfa = null, Compression = 8 }),
|
|
"cr2" => Cr2(Sensor(scene, "Canon EOS 5D Mark III", CfaPattern.Bayer("RGGB"), 80, 36, 24, 4, 2048, 16383), new RawRect(24, 4, 24 + W, 4 + H)),
|
|
"nef" => Nef(Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600, 16383), 14, NefEncoding.Lossless),
|
|
"arw" => Arw(Sensor(scene, "Sony ILCE-7M3", CfaPattern.Bayer("RGGB"), 64, H, 0, 0, 512, 16000), [8000, 10400, 12900, 14100], compressed: true),
|
|
"pef" => Pef(Sensor(scene, "Pentax K-1", CfaPattern.Bayer("BGGR"), W, H, 0, 0, 512, 16383), 14, compressed: true),
|
|
_ => Raf(Sensor(scene, "Fujifilm X-T2", DemosaicTests.XTrans, W + 6, H + 6, 3, 3, 1024, 16383), DemosaicTests.XTrans, new RawRect(3, 3, 3 + W, 3 + H)),
|
|
};
|
|
}
|
|
|
|
private static byte[] GainMapList() => new OpcodeListBuilder().Add(9, 0, p =>
|
|
{
|
|
OpcodeListBuilder.Area(p, 0, 0, H, W);
|
|
p.U32(2).U32(2).F64(1).F64(1).F64(0).F64(0).U32(1).F32(1).F32(1.1f).F32(1.2f).F32(1.3f);
|
|
}).Build();
|
|
|
|
[Theory]
|
|
[Case("dng")]
|
|
[Case("linear.dng")]
|
|
[Case("cr2")]
|
|
[Case("nef")]
|
|
[Case("arw")]
|
|
[Case("pef")]
|
|
[Case("raf")]
|
|
public void TruncatedAndCorruptedFilesOnlyRaiseInvalidData(string format)
|
|
{
|
|
var file = Sample(format);
|
|
var name = "fuzz." + format.Split('.').Last();
|
|
var options = new RawDecodeOptions { Quality = DemosaicQuality.Draft, MaxDegreeOfParallelism = 1 };
|
|
using (var clean = RawDecoder.Decode(new MemoryStream(file), name, options)) Assert.Equal(format == "arw" ? 64 : W, clean.Image.Width);
|
|
var random = new Random(format.Length * 7919);
|
|
var outcomes = new Dictionary<string, int>();
|
|
for (var i = 0; i < 120; i++)
|
|
{
|
|
byte[] variant;
|
|
if (i < 40)
|
|
{
|
|
variant = file[..(int)((long)file.Length * i / 40)];
|
|
}
|
|
else
|
|
{
|
|
variant = (byte[])file.Clone();
|
|
var flips = 1 + random.Next(4);
|
|
for (var k = 0; k < flips; k++)
|
|
{
|
|
// Bias corruption toward the headers, where structure lives.
|
|
var at = random.Next(2) == 0 ? random.Next(int.Min(variant.Length, 512)) : random.Next(variant.Length);
|
|
variant[at] = (byte)random.Next(256);
|
|
}
|
|
}
|
|
var outcome = Run(variant, name, options);
|
|
outcomes[outcome] = outcomes.GetValueOrDefault(outcome) + 1;
|
|
}
|
|
context.Log($"{format}: " + string.Join(", ", outcomes.Select(o => $"{o.Key} {o.Value}")));
|
|
}
|
|
|
|
private static string Run(byte[] data, string name, RawDecodeOptions options)
|
|
{
|
|
try
|
|
{
|
|
RawDecoder.Probe(new MemoryStream(data), name);
|
|
using var result = RawDecoder.Decode(new MemoryStream(data), name, options);
|
|
return "decoded";
|
|
}
|
|
catch (InvalidDataException)
|
|
{
|
|
return "invalid";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Assert.Fail($"{ex.GetType().Name}: {ex.Message}\n{ex.StackTrace}");
|
|
return "";
|
|
}
|
|
}
|
|
}
|