diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs
new file mode 100644
index 0000000000..13a570dbcb
--- /dev/null
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs
@@ -0,0 +1,48 @@
+// Copyright (c) Six Labors.
+// Licensed under the Apache License, Version 2.0.
+
+using System;
+using SixLabors.ImageSharp.Formats.Tiff.Utils;
+using SixLabors.ImageSharp.Memory;
+using SixLabors.ImageSharp.PixelFormats;
+
+namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
+{
+ ///
+ /// Implements the 'BlackIsZero' photometric interpretation for 16-bit grayscale images.
+ ///
+ internal class BlackIsZero16TiffColor : TiffBaseColorDecoder
+ where TPixel : unmanaged, IPixel
+ {
+ private readonly bool isBigEndian;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// if set to true decodes the pixel data as big endian, otherwise as little endian.
+ public BlackIsZero16TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian;
+
+ ///
+ public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height)
+ {
+ var color = default(TPixel);
+
+ int offset = 0;
+
+ var l16 = default(L16);
+ for (int y = top; y < top + height; y++)
+ {
+ for (int x = left; x < left + width; x++)
+ {
+ ushort intensity = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian);
+ offset += 2;
+
+ l16.PackedValue = intensity;
+ color.FromL16(l16);
+
+ pixels[x, y] = color;
+ }
+ }
+ }
+ }
+}
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs
index 635be95f4a..b0106cc096 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs
@@ -3,6 +3,7 @@
using System;
using System.Buffers.Binary;
+using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
@@ -36,11 +37,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
for (int x = left; x < left + width; x++)
{
- ulong r = this.ConvertToShort(data.Slice(offset, 2));
+ ulong r = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian);
offset += 2;
- ulong g = this.ConvertToShort(data.Slice(offset, 2));
+ ulong g = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian);
offset += 2;
- ulong b = this.ConvertToShort(data.Slice(offset, 2));
+ ulong b = TiffUtils.ConvertToShort(data.Slice(offset, 2), this.isBigEndian);
offset += 2;
rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48);
@@ -50,9 +51,5 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
}
}
}
-
- private ushort ConvertToShort(ReadOnlySpan buffer) => this.isBigEndian
- ? BinaryPrimitives.ReadUInt16BigEndian(buffer)
- : BinaryPrimitives.ReadUInt16LittleEndian(buffer);
}
}
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs
index 8e711d3eb6..ad9c478d23 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs
@@ -52,6 +52,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new BlackIsZero8TiffColor();
+ case TiffColorType.BlackIsZero16:
+ DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 16, "bitsPerSample");
+ DebugGuard.IsTrue(colorMap == null, "colorMap");
+ return new BlackIsZero16TiffColor(byteOrder == ByteOrder.BigEndian);
+
case TiffColorType.Rgb:
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new RgbTiffColor(bitsPerSample);
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs
index 517926c239..3c003cf92d 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs
@@ -28,6 +28,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
///
BlackIsZero8,
+ ///
+ /// Grayscale: 0 is imaged as black. The maximum value is imaged as white. Optimized implementation for 16-bit images.
+ ///
+ BlackIsZero16,
+
///
/// Grayscale: 0 is imaged as white. The maximum value is imaged as black.
///
diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs
index 288f01cd1c..0ac9fd7c9f 100644
--- a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs
+++ b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs
@@ -154,6 +154,12 @@ namespace SixLabors.ImageSharp.Formats.Tiff
switch (bitsPerChannel)
{
+ case 16:
+ {
+ options.ColorType = TiffColorType.BlackIsZero16;
+ break;
+ }
+
case 8:
{
options.ColorType = TiffColorType.BlackIsZero8;
diff --git a/src/ImageSharp/Formats/Tiff/Utils/TiffUtils.cs b/src/ImageSharp/Formats/Tiff/Utils/TiffUtils.cs
new file mode 100644
index 0000000000..5a31b231ec
--- /dev/null
+++ b/src/ImageSharp/Formats/Tiff/Utils/TiffUtils.cs
@@ -0,0 +1,18 @@
+// Copyright (c) Six Labors.
+// Licensed under the Apache License, Version 2.0.
+
+using System;
+using System.Buffers.Binary;
+
+namespace SixLabors.ImageSharp.Formats.Tiff.Utils
+{
+ ///
+ /// Helper methods for TIFF decoding.
+ ///
+ internal static class TiffUtils
+ {
+ public static ushort ConvertToShort(ReadOnlySpan buffer, bool isBigEndian) => isBigEndian
+ ? BinaryPrimitives.ReadUInt16BigEndian(buffer)
+ : BinaryPrimitives.ReadUInt16LittleEndian(buffer);
+ }
+}
diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
index ab53ca1561..95d5437e24 100644
--- a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
@@ -140,6 +140,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff
where TPixel : unmanaged, IPixel => TestTiffDecoder(provider);
[Theory]
+ [WithFile(Flower16BitGrayLittleEndian, PixelTypes.Rgba32)]
[WithFile(Flower16BitGray, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_16Bit(TestImageProvider provider)
where TPixel : unmanaged, IPixel => TestTiffDecoder(provider);
diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs
index c54c82d7da..d5aeba0bba 100644
--- a/tests/ImageSharp.Tests/TestImages.cs
+++ b/tests/ImageSharp.Tests/TestImages.cs
@@ -582,6 +582,7 @@ namespace SixLabors.ImageSharp.Tests
public const string Flower12BitGray = "Tiff/flower-minisblack-12.tiff";
public const string Flower14BitGray = "Tiff/flower-minisblack-14.tiff";
public const string Flower16BitGray = "Tiff/flower-minisblack-16.tiff";
+ public const string Flower16BitGrayLittleEndian = "Tiff/flower-minisblack-16_lsb.tiff";
public const string Issues1716Rgb161616BitLittleEndian = "Tiff/Issues/Issue1716.tiff";
public const string SmallRgbDeflate = "Tiff/rgb_small_deflate.tiff";
diff --git a/tests/Images/Input/Tiff/flower-minisblack-16_lsb.tiff b/tests/Images/Input/Tiff/flower-minisblack-16_lsb.tiff
new file mode 100644
index 0000000000..62061bfaf6
--- /dev/null
+++ b/tests/Images/Input/Tiff/flower-minisblack-16_lsb.tiff
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:c3806304a5453a6ec8a6795bc77b967b9aa8593288af36bbf9802f22ee27869e
+size 6588