fix(raw): match LibRaw on RAF and unpacked NEF layouts; drop frame copies

Cross-checking synthetic files with LibRaw's dcraw_emu showed it reads
uncompressed RAF samples as little-endian words and unpacked data under
the Nikon compression tag as big-endian words shifted left by four; the
readers and test writers now do the same (CR2, lossless NEF, ARW2 and
PEF already decoded identically, ~86 dB). The default crop and the
final orientation step no longer copy a full frame when they change
nothing, lowering the 24 MP peak by two frame copies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 11:43:45 +00:00
1 parent 0f3fbfdad4
commit bcf0510a8e
6 files changed
+72 -43

No files matched your search

@@ -43,7 +43,7 @@ public sealed class NativeRawTests(TestContext context)
[Theory]
[Case(NefEncoding.Lossless, 14)]
[Case(NefEncoding.Lossless, 12)]
[Case(NefEncoding.Unpacked, 14)]
[Case(NefEncoding.Unpacked, 12)]
[Case(NefEncoding.Packed, 12)]
public void NefEncodings(NefEncoding encoding, int bits)
{
@@ -191,7 +191,7 @@ internal static class NativeRawWriters
}
case NefEncoding.Unpacked:
data = new byte[s.Samples.Length * 2];
for (var i = 0; i < s.Samples.Length; i++) BinaryPrimitives.WriteUInt16BigEndian(data.AsSpan(2 * i), s.Samples[i]);
for (var i = 0; i < s.Samples.Length; i++) BinaryPrimitives.WriteUInt16BigEndian(data.AsSpan(2 * i), (ushort)(s.Samples[i] << 4));
break;
default:
compression = 1;
@@ -436,7 +436,7 @@ internal static class NativeRawWriters
Record(0x2FF0, [.. Box.U16(302), .. Box.U16((ushort)double.Round(302 * s.Neutral.Y / s.Neutral.X)), .. Box.U16(302), .. Box.U16((ushort)double.Round(302 * s.Neutral.Y / s.Neutral.Z))]);
byte[] directory = [.. Box.U32(6), .. records];
var data = new byte[s.Samples.Length * 2];
for (var i = 0; i < s.Samples.Length; i++) BinaryPrimitives.WriteUInt16BigEndian(data.AsSpan(2 * i), s.Samples[i]);
for (var i = 0; i < s.Samples.Length; i++) BinaryPrimitives.WriteUInt16LittleEndian(data.AsSpan(2 * i), s.Samples[i]);
var fuji = new IfdSpec().Long(0xF001, (uint)s.Width).Long(0xF002, (uint)s.Height).Long(0xF003, 14)
.Long(0xF00A, [.. Enumerable.Repeat(1024u, 36)]).Blob(0xF007, 0xF008, data);
var block = TiffFile.Build(new IfdSpec().Child(0xF000, fuji), littleEndian: false);