diff --git a/src/Titano.Core/Codecs/Raw/NativeRaw.cs b/src/Titano.Core/Codecs/Raw/NativeRaw.cs
index 342d110b..632c46cb 100644
--- a/src/Titano.Core/Codecs/Raw/NativeRaw.cs
+++ b/src/Titano.Core/Codecs/Raw/NativeRaw.cs
@@ -140,8 +140,9 @@ internal static class NativeRaw
}
}
-/// Uncompressed raw payloads: 16-bit words (either byte order) or MSB-first packed samples (TIFF convention).
-internal sealed class PlainRawLoader(long offset, long length, int width, int height, int bits, bool words, bool littleEndian) : RawLoader
+/// Uncompressed raw payloads: 16-bit words (either byte order, optionally holding the value shifted left by
+/// bits) or MSB-first packed samples (TIFF convention).
+internal sealed class PlainRawLoader(long offset, long length, int width, int height, int bits, bool words, bool littleEndian, int shift = 0) : RawLoader
{
public override RawSamples Load(Stream stream, int maxDegreeOfParallelism, CancellationToken cancellationToken)
{
@@ -157,7 +158,7 @@ internal sealed class PlainRawLoader(long offset, long length, int width, int he
if (words)
{
for (var x = 0; x < width; x++)
- dst[x] = (ushort)((littleEndian ? row[2 * x] | row[2 * x + 1] << 8 : row[2 * x] << 8 | row[2 * x + 1]) & mask);
+ dst[x] = (ushort)((littleEndian ? row[2 * x] | row[2 * x + 1] << 8 : row[2 * x] << 8 | row[2 * x + 1]) >> shift & mask);
return;
}
long bit = 0;
diff --git a/src/Titano.Core/Codecs/Raw/NefReader.cs b/src/Titano.Core/Codecs/Raw/NefReader.cs
index e42d8fc5..1b71277b 100644
--- a/src/Titano.Core/Codecs/Raw/NefReader.cs
+++ b/src/Titano.Core/Codecs/Raw/NefReader.cs
@@ -63,10 +63,14 @@ internal static class NefReader
white = decode.White;
loader = new NikonHuffmanLoader(offset, length, width, height, decode);
}
- else if (compression is CompressionNikon or 1 && length >= (long)width * height * 2)
+ else if (compression == CompressionNikon)
{
- // Nikon writes unpacked "compressed" data big-endian; plain uncompressed follows the TIFF byte order.
- loader = new PlainRawLoader(offset, length, width, height, bits, words: true, littleEndian: compression == 1 && r.LittleEndian);
+ // Unpacked data under the Nikon compression tag: big-endian words holding the value shifted left by 4 (LibRaw).
+ loader = new PlainRawLoader(offset, length, width, height, bits, words: true, littleEndian: false, shift: 4);
+ }
+ else if (compression == 1 && length >= (long)width * height * 2)
+ {
+ loader = new PlainRawLoader(offset, length, width, height, bits, words: true, littleEndian: r.LittleEndian);
}
else if (compression == 1 && length >= ((long)width * bits + 7) / 8 * height)
{
diff --git a/src/Titano.Core/Codecs/Raw/RafReader.cs b/src/Titano.Core/Codecs/Raw/RafReader.cs
index 2219b7ee..c7a34290 100644
--- a/src/Titano.Core/Codecs/Raw/RafReader.cs
+++ b/src/Titano.Core/Codecs/Raw/RafReader.cs
@@ -10,7 +10,8 @@ namespace Titano.Core.Codecs.Raw;
/// gives the raster size (0xF001/0xF002), bit depth (0xF003), the data offset relative to that block (0xF007), its length
/// (0xF008) and black levels (0xF00A); the RAF directory supplies the crop (0x0110 top-left, 0x0111 size), the 6×6
/// colour layout (0x0131, stored reversed, anchored at the raster origin; Bayer bodies describe their 2×2 pattern the same
-/// way) and the as-shot GRGB levels (0x2FF0). Samples are 16-bit words in the block's byte order. Compressed RAFs and
+/// way) and the as-shot GRGB levels (0x2FF0). Samples are little-endian 16-bit words whatever the block's byte order, as
+/// LibRaw reads them. Compressed RAFs and
/// SuperCCD (45°) layouts are not supported and fall back to the preview.
///
internal static class RafReader
@@ -87,7 +88,7 @@ internal static class RafReader
if (g > 0) multipliers = new Vector3D(red / g, 1, blue / g);
}
var metadata = MetadataReader.Read(stream, "frame.raf");
- var loader = new PlainRawLoader(offset, length, width, height, 16, words: true, littleEndian: r.LittleEndian);
+ var loader = new PlainRawLoader(offset, length, width, height, 16, words: true, littleEndian: true);
return NativeRaw.Frame("managed-raf", width, height, cfa, active, black, (1 << bits) - 1, multipliers, metadata.Orientation,
metadata.Make ?? "FUJIFILM", metadata.Model, loader, warnings);
}
diff --git a/src/Titano.Core/Codecs/Raw/RawDeveloper.cs b/src/Titano.Core/Codecs/Raw/RawDeveloper.cs
index 0b106121..33eac0fa 100644
--- a/src/Titano.Core/Codecs/Raw/RawDeveloper.cs
+++ b/src/Titano.Core/Codecs/Raw/RawDeveloper.cs
@@ -123,14 +123,23 @@ internal static class RawDeveloper
DngOpcodes.Apply(list3, image, warnings, cancellationToken);
if (unbalance) ScalePlanes(image.Pixels, wb);
}
- using var cropped = Crop(frame, image);
- ApplyMatrix(cropped, colour.BalancedToRec709, dop, cancellationToken);
- return Finish(frame, cropped, options);
}
- finally
+ catch
{
image.Pixels.Dispose();
+ throw;
}
+ var cropped = Crop(frame, image);
+ try
+ {
+ ApplyMatrix(cropped, colour.BalancedToRec709, dop, cancellationToken);
+ }
+ catch
+ {
+ cropped.Dispose();
+ throw;
+ }
+ return Finish(frame, cropped, options);
}
private static ImageBuffer DevelopMonochrome(RawFrame frame, OpcodeImage stage, RawDecodeOptions options, ICollection warnings, CancellationToken cancellationToken)
@@ -139,40 +148,51 @@ internal static class RawDeveloper
try
{
DngOpcodes.Apply(DngOpcodes.Parse(frame.OpcodeList3, 3, warnings), image, warnings, cancellationToken);
- using var cropped = Crop(frame, image);
- Scale(cropped.Plane(0), (float)double.Exp2(frame.Color.BaselineExposure));
- using var rgb = PixelConvert.GrayToRgb(cropped);
- return Finish(frame, rgb, options);
}
- finally
+ catch
{
image.Pixels.Dispose();
+ throw;
+ }
+ ImageBuffer rgb;
+ using (var cropped = Crop(frame, image))
+ {
+ Scale(cropped.Plane(0), (float)double.Exp2(frame.Color.BaselineExposure));
+ rgb = PixelConvert.GrayToRgb(cropped);
+ }
+ return Finish(frame, rgb, options);
+ }
+
+ /// Default scale, then orientation. Takes ownership of : returns it when nothing
+ /// changes, otherwise a new buffer (the input is disposed).
+ private static ImageBuffer Finish(RawFrame frame, ImageBuffer image, RawDecodeOptions options)
+ {
+ var current = image;
+ try
+ {
+ var (sh, sv) = frame.DefaultScale;
+ if (sh != sv && sh > 0 && sv > 0)
+ {
+ // Only the ratio matters for square output pixels; stretch the axis with the larger factor.
+ var ratio = sh / sv;
+ var (w, h) = ratio > 1 ? ((int)double.Round(current.Width * ratio), current.Height) : (current.Width, (int)double.Round(current.Height / ratio));
+ current = Replace(current, Resampler.Resize(current, int.Max(1, w), int.Max(1, h)));
+ }
+ if (options.ApplyOrientation && frame.Orientation is >= 2 and <= 8)
+ current = Replace(current, PixelConvert.ApplyOrientation(current, frame.Orientation));
+ return current;
+ }
+ catch
+ {
+ current.Dispose();
+ throw;
}
}
- /// Default scale, then orientation; always returns a new buffer.
- private static ImageBuffer Finish(RawFrame frame, ImageBuffer image, RawDecodeOptions options)
+ private static ImageBuffer Replace(ImageBuffer old, ImageBuffer next)
{
- var (sh, sv) = frame.DefaultScale;
- ImageBuffer? scaled = null;
- if (sh != sv && sh > 0 && sv > 0)
- {
- // Only the ratio matters for square output pixels; stretch the axis with the larger factor.
- var ratio = sh / sv;
- var (w, h) = ratio > 1 ? ((int)double.Round(image.Width * ratio), image.Height) : (image.Width, (int)double.Round(image.Height / ratio));
- scaled = Resampler.Resize(image, int.Max(1, w), int.Max(1, h));
- }
- var source = scaled ?? image;
- try
- {
- return options.ApplyOrientation && frame.Orientation is >= 2 and <= 8
- ? PixelConvert.ApplyOrientation(source, frame.Orientation)
- : source.Clone();
- }
- finally
- {
- scaled?.Dispose();
- }
+ old.Dispose();
+ return next;
}
/// Stored values as floats divided by 65535 (the stage-1 domain of the opcodes), sample planes split.
@@ -294,7 +314,8 @@ internal static class RawDeveloper
for (var i = 0; i < values.Length; i++) values[i] *= factor;
}
- /// Default crop (stage coordinates relative to the active area) intersected with what the opcodes left.
+ /// Default crop (stage coordinates relative to the active area) intersected with what the opcodes left. Takes
+ /// ownership of the image's pixels: returns them when nothing is cut, otherwise a new buffer (the input is disposed).
private static ImageBuffer Crop(RawFrame frame, OpcodeImage image)
{
var crop = frame.DefaultCrop ?? new RawRect(0, 0, frame.ActiveArea.Width, frame.ActiveArea.Height);
@@ -305,11 +326,13 @@ internal static class RawDeveloper
var x1 = int.Clamp((int)double.Floor(image.BufferX(area.Right - 1) + 1e-9) + 1, x0 + 1, image.Width);
var y1 = int.Clamp((int)double.Floor(image.BufferY(area.Bottom - 1) + 1e-9) + 1, y0 + 1, image.Height);
var src = image.Pixels;
+ if (x0 == 0 && y0 == 0 && x1 == src.Width && y1 == src.Height) return src;
var dst = new ImageBuffer(x1 - x0, y1 - y0, src.Channels, clear: false);
for (var c = 0; c < src.Channels; c++)
{
for (var y = y0; y < y1; y++) src.Row(c, y).Slice(x0, x1 - x0).CopyTo(dst.Row(c, y - y0));
}
+ src.Dispose();
return dst;
}
diff --git a/tests/Titano.Core.Tests/Raw/NativeRawTests.cs b/tests/Titano.Core.Tests/Raw/NativeRawTests.cs
index 5dd8fae1..f2ecac84 100644
--- a/tests/Titano.Core.Tests/Raw/NativeRawTests.cs
+++ b/tests/Titano.Core.Tests/Raw/NativeRawTests.cs
@@ -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)
{
diff --git a/tests/Titano.Core.Tests/Raw/NativeRawWriters.cs b/tests/Titano.Core.Tests/Raw/NativeRawWriters.cs
index 9fa3bce0..4c566598 100644
--- a/tests/Titano.Core.Tests/Raw/NativeRawWriters.cs
+++ b/tests/Titano.Core.Tests/Raw/NativeRawWriters.cs
@@ -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);