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..a31ff7a9fd
--- /dev/null
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs
@@ -0,0 +1,61 @@
+// 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)
+ {
+ // Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
+ // we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
+ L16 l16 = TiffUtils.L16Default;
+ var color = default(TPixel);
+ color.FromVector4(TiffUtils.Vector4Default);
+
+ int offset = 0;
+ for (int y = top; y < top + height; y++)
+ {
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
+ if (this.isBigEndian)
+ {
+ for (int x = 0; x < pixelRow.Length; x++)
+ {
+ ushort intensity = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
+ offset += 2;
+
+ pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color);
+ }
+ }
+ else
+ {
+ for (int x = 0; x < pixelRow.Length; x++)
+ {
+ ushort intensity = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
+ offset += 2;
+
+ pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color);
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor{TPixel}.cs
index 096f0449bf..ea2608f6fd 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor{TPixel}.cs
@@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System;
-
+using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
@@ -24,14 +24,11 @@ public override void Decode(ReadOnlySpan data, Buffer2D pixels, in
var l8 = default(L8);
for (int y = top; y < top + height; y++)
{
- for (int x = left; x < left + width; x++)
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
+ for (int x = 0; x < pixelRow.Length; x++)
{
byte intensity = data[offset++];
-
- l8.PackedValue = intensity;
- color.FromL8(l8);
-
- pixels[x, y] = color;
+ pixelRow[x] = TiffUtils.ColorFromL8(l8, intensity, color);
}
}
}
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor{TPixel}.cs
index a4e5e45dfb..9956db5230 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor{TPixel}.cs
@@ -34,13 +34,14 @@ public override void Decode(ReadOnlySpan data, Buffer2D pixels, in
for (int y = top; y < top + height; y++)
{
- for (int x = left; x < left + width; x++)
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
+ for (int x = 0; x < pixelRow.Length; x++)
{
int value = bitReader.ReadBits(this.bitsPerSample0);
float intensity = value / this.factor;
color.FromVector4(new Vector4(intensity, intensity, intensity, 1.0f));
- pixels[x, y] = color;
+ pixelRow[x] = color;
}
bitReader.NextRow();
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor{TPixel}.cs
index 796227953e..b392fe1a36 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor{TPixel}.cs
@@ -35,10 +35,11 @@ public override void Decode(ReadOnlySpan data, Buffer2D pixels, in
for (int y = top; y < top + height; y++)
{
- for (int x = left; x < left + width; x++)
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
+ for (int x = 0; x < pixelRow.Length; x++)
{
int index = bitReader.ReadBits(this.bitsPerSample0);
- pixels[x, y] = this.palette[index];
+ pixelRow[x] = this.palette[index];
}
bitReader.NextRow();
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs
index 635be95f4a..4b34d5a0db 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs
@@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System;
-using System.Buffers.Binary;
+using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
@@ -25,34 +25,47 @@ internal class Rgb161616TiffColor : TiffBaseColorDecoder
///
public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height)
{
+ // Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
+ // we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
+ Rgba64 rgba = TiffUtils.Rgba64Default;
var color = default(TPixel);
+ color.FromVector4(TiffUtils.Vector4Default);
int offset = 0;
- var rgba = default(Rgba64);
for (int y = top; y < top + height; y++)
{
- Span pixelRow = pixels.GetRowSpan(y);
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
- for (int x = left; x < left + width; x++)
+ if (this.isBigEndian)
{
- ulong r = this.ConvertToShort(data.Slice(offset, 2));
- offset += 2;
- ulong g = this.ConvertToShort(data.Slice(offset, 2));
- offset += 2;
- ulong b = this.ConvertToShort(data.Slice(offset, 2));
- offset += 2;
+ for (int x = 0; x < pixelRow.Length; x++)
+ {
+ ulong r = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
+ offset += 2;
+ ulong g = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
+ offset += 2;
+ ulong b = TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2));
+ offset += 2;
- rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48);
- color.FromRgba64(rgba);
+ pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
+ }
+ }
+ else
+ {
+ for (int x = 0; x < pixelRow.Length; x++)
+ {
+ ulong r = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
+ offset += 2;
+ ulong g = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
+ offset += 2;
+ ulong b = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2));
+ offset += 2;
- pixelRow[x] = color;
+ pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
+ }
}
}
}
-
- private ushort ConvertToShort(ReadOnlySpan buffer) => this.isBigEndian
- ? BinaryPrimitives.ReadUInt16BigEndian(buffer)
- : BinaryPrimitives.ReadUInt16LittleEndian(buffer);
}
}
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb16PlanarTiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb16PlanarTiffColor{TPixel}.cs
new file mode 100644
index 0000000000..c24fada54c
--- /dev/null
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb16PlanarTiffColor{TPixel}.cs
@@ -0,0 +1,72 @@
+// Copyright (c) Six Labors.
+// Licensed under the Apache License, Version 2.0.
+
+using System;
+using System.Buffers;
+using SixLabors.ImageSharp.Formats.Tiff.Utils;
+using SixLabors.ImageSharp.Memory;
+using SixLabors.ImageSharp.PixelFormats;
+
+namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
+{
+ ///
+ /// Implements the 'RGB' photometric interpretation with 'Planar' layout for all 16 bit.
+ ///
+ internal class Rgb16PlanarTiffColor : TiffBasePlanarColorDecoder
+ 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 Rgb16PlanarTiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian;
+
+ ///
+ public override void Decode(IMemoryOwner[] data, Buffer2D pixels, int left, int top, int width, int height)
+ {
+ // Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
+ // we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
+ Rgba64 rgba = TiffUtils.Rgba64Default;
+ var color = default(TPixel);
+ color.FromVector4(TiffUtils.Vector4Default);
+
+ Span redData = data[0].GetSpan();
+ Span greenData = data[1].GetSpan();
+ Span blueData = data[2].GetSpan();
+
+ int offset = 0;
+ for (int y = top; y < top + height; y++)
+ {
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
+ if (this.isBigEndian)
+ {
+ for (int x = 0; x < pixelRow.Length; x++)
+ {
+ ulong r = TiffUtils.ConvertToShortBigEndian(redData.Slice(offset, 2));
+ ulong g = TiffUtils.ConvertToShortBigEndian(greenData.Slice(offset, 2));
+ ulong b = TiffUtils.ConvertToShortBigEndian(blueData.Slice(offset, 2));
+
+ offset += 2;
+
+ pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
+ }
+ }
+ else
+ {
+ for (int x = 0; x < pixelRow.Length; x++)
+ {
+ ulong r = TiffUtils.ConvertToShortLittleEndian(redData.Slice(offset, 2));
+ ulong g = TiffUtils.ConvertToShortLittleEndian(greenData.Slice(offset, 2));
+ ulong b = TiffUtils.ConvertToShortLittleEndian(blueData.Slice(offset, 2));
+
+ offset += 2;
+
+ pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color);
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor{TPixel}.cs
index e45863a57f..536dece8e3 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor{TPixel}.cs
@@ -24,9 +24,9 @@ public override void Decode(ReadOnlySpan data, Buffer2D pixels, in
var rgba = default(Rgba32);
for (int y = top; y < top + height; y++)
{
- Span pixelRow = pixels.GetRowSpan(y);
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
- for (int x = left; x < left + width; x++)
+ for (int x = 0; x < pixelRow.Length; x++)
{
byte r = data[offset++];
byte g = data[offset++];
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs
index 3400bd65d8..b442c4ae47 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor{TPixel}.cs
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
+using System;
using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Formats.Tiff.Utils;
@@ -12,7 +13,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
///
/// Implements the 'RGB' photometric interpretation with 'Planar' layout (for all bit depths).
///
- internal class RgbPlanarTiffColor
+ internal class RgbPlanarTiffColor : TiffBasePlanarColorDecoder
where TPixel : unmanaged, IPixel
{
private readonly float rFactor;
@@ -47,7 +48,7 @@ public RgbPlanarTiffColor(TiffBitsPerSample bitsPerSample)
/// The y-coordinate of the top of the image block.
/// The width of the image block.
/// The height of the image block.
- public void Decode(IMemoryOwner[] data, Buffer2D pixels, int left, int top, int width, int height)
+ public override void Decode(IMemoryOwner[] data, Buffer2D pixels, int left, int top, int width, int height)
{
var color = default(TPixel);
@@ -57,14 +58,15 @@ public void Decode(IMemoryOwner[] data, Buffer2D pixels, int left,
for (int y = top; y < top + height; y++)
{
- for (int x = left; x < left + width; x++)
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
+ for (int x = 0; x < pixelRow.Length; x++)
{
float r = rBitReader.ReadBits(this.bitsPerSampleR) / this.rFactor;
float g = gBitReader.ReadBits(this.bitsPerSampleG) / this.gFactor;
float b = bBitReader.ReadBits(this.bitsPerSampleB) / this.bFactor;
color.FromVector4(new Vector4(r, g, b, 1.0f));
- pixels[x, y] = color;
+ pixelRow[x] = color;
}
rBitReader.NextRow();
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor{TPixel}.cs
index 259bb8efa7..1377598cc9 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor{TPixel}.cs
@@ -47,14 +47,15 @@ public override void Decode(ReadOnlySpan data, Buffer2D pixels, in
for (int y = top; y < top + height; y++)
{
- for (int x = left; x < left + width; x++)
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
+ for (int x = 0; x < pixelRow.Length; x++)
{
float r = bitReader.ReadBits(this.bitsPerSampleR) / this.rFactor;
float g = bitReader.ReadBits(this.bitsPerSampleG) / this.gFactor;
float b = bitReader.ReadBits(this.bitsPerSampleB) / this.bFactor;
color.FromVector4(new Vector4(r, g, b, 1.0f));
- pixels[x, y] = color;
+ pixelRow[x] = color;
}
bitReader.NextRow();
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffBasePlanarColorDecoder{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffBasePlanarColorDecoder{TPixel}.cs
new file mode 100644
index 0000000000..57d8588cee
--- /dev/null
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffBasePlanarColorDecoder{TPixel}.cs
@@ -0,0 +1,28 @@
+// Copyright (c) Six Labors.
+// Licensed under the Apache License, Version 2.0.
+
+using System.Buffers;
+using SixLabors.ImageSharp.Memory;
+using SixLabors.ImageSharp.PixelFormats;
+
+namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation
+{
+ ///
+ /// The base class for planar color decoders.
+ ///
+ /// The pixel format.
+ internal abstract class TiffBasePlanarColorDecoder
+ where TPixel : unmanaged, IPixel
+ {
+ ///
+ /// Decodes source raw pixel data using the current photometric interpretation.
+ ///
+ /// The buffers to read image data from.
+ /// The image buffer to write pixels to.
+ /// The x-coordinate of the left-hand side of the image block.
+ /// The y-coordinate of the top of the image block.
+ /// The width of the image block.
+ /// The height of the image block.
+ public abstract void Decode(IMemoryOwner[] data, Buffer2D pixels, int left, int top, int width, int height);
+ }
+}
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs
index 8e711d3eb6..0c93998c48 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs
@@ -32,6 +32,11 @@ public static TiffBaseColorDecoder Create(TiffColorType colorType, TiffB
DebugGuard.IsTrue(colorMap == null, "colorMap");
return new WhiteIsZero8TiffColor();
+ case TiffColorType.WhiteIsZero16:
+ DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 16, "bitsPerSample");
+ DebugGuard.IsTrue(colorMap == null, "colorMap");
+ return new WhiteIsZero16TiffColor(byteOrder == ByteOrder.BigEndian);
+
case TiffColorType.BlackIsZero:
DebugGuard.IsTrue(bitsPerSample.Channels == 1, "bitsPerSample");
DebugGuard.IsTrue(colorMap == null, "colorMap");
@@ -52,6 +57,11 @@ public static TiffBaseColorDecoder Create(TiffColorType colorType, TiffB
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);
@@ -135,12 +145,17 @@ public static TiffBaseColorDecoder Create(TiffColorType colorType, TiffB
}
}
- public static RgbPlanarTiffColor CreatePlanar(TiffColorType colorType, TiffBitsPerSample bitsPerSample, ushort[] colorMap)
+ public static TiffBasePlanarColorDecoder CreatePlanar(TiffColorType colorType, TiffBitsPerSample bitsPerSample, ushort[] colorMap, ByteOrder byteOrder)
{
switch (colorType)
{
case TiffColorType.RgbPlanar:
DebugGuard.IsTrue(colorMap == null, "colorMap");
+ if (bitsPerSample.Channel0 == 16 && bitsPerSample.Channel1 == 16 && bitsPerSample.Channel2 == 16)
+ {
+ return new Rgb16PlanarTiffColor(byteOrder == ByteOrder.BigEndian);
+ }
+
return new RgbPlanarTiffColor(bitsPerSample);
default:
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs
index 517926c239..331065d273 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs
@@ -28,6 +28,11 @@ internal enum TiffColorType
///
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.
///
@@ -48,6 +53,11 @@ internal enum TiffColorType
///
WhiteIsZero8,
+ ///
+ /// Grayscale: 0 is imaged as white. The maximum value is imaged as black. Optimized implementation for 16-bit images.
+ ///
+ WhiteIsZero16,
+
///
/// Palette-color.
///
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero16TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero16TiffColor{TPixel}.cs
new file mode 100644
index 0000000000..a6b5151d70
--- /dev/null
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero16TiffColor{TPixel}.cs
@@ -0,0 +1,61 @@
+// 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 'WhiteIsZero' photometric interpretation for 16-bit grayscale images.
+ ///
+ internal class WhiteIsZero16TiffColor : 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 WhiteIsZero16TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian;
+
+ ///
+ public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height)
+ {
+ // Note: due to an issue with netcore 2.1 and default values and unpredictable behavior with those,
+ // we define our own defaults as a workaround. See: https://github.com/dotnet/runtime/issues/55623
+ L16 l16 = TiffUtils.L16Default;
+ var color = default(TPixel);
+ color.FromVector4(TiffUtils.Vector4Default);
+
+ int offset = 0;
+ for (int y = top; y < top + height; y++)
+ {
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
+ if (this.isBigEndian)
+ {
+ for (int x = 0; x < pixelRow.Length; x++)
+ {
+ ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShortBigEndian(data.Slice(offset, 2)));
+ offset += 2;
+
+ pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color);
+ }
+ }
+ else
+ {
+ for (int x = 0; x < pixelRow.Length; x++)
+ {
+ ushort intensity = (ushort)(ushort.MaxValue - TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2)));
+ offset += 2;
+
+ pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color);
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor{TPixel}.cs
index 1b141f9f6a..6a6c2af225 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor{TPixel}.cs
@@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System;
-
+using SixLabors.ImageSharp.Formats.Tiff.Utils;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
@@ -24,14 +24,11 @@ public override void Decode(ReadOnlySpan data, Buffer2D pixels, in
var l8 = default(L8);
for (int y = top; y < top + height; y++)
{
- for (int x = left; x < left + width; x++)
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
+ for (int x = 0; x < pixelRow.Length; x++)
{
byte intensity = (byte)(255 - data[offset++]);
-
- l8.PackedValue = intensity;
- color.FromL8(l8);
-
- pixels[x, y] = color;
+ pixelRow[x] = TiffUtils.ColorFromL8(l8, intensity, color);
}
}
}
diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColor{TPixel}.cs
index 04b6f98e50..9129559647 100644
--- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColor{TPixel}.cs
+++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColor{TPixel}.cs
@@ -34,13 +34,14 @@ public override void Decode(ReadOnlySpan data, Buffer2D pixels, in
for (int y = top; y < top + height; y++)
{
- for (int x = left; x < left + width; x++)
+ Span pixelRow = pixels.GetRowSpan(y).Slice(left, width);
+ for (int x = 0; x < pixelRow.Length; x++)
{
int value = bitReader.ReadBits(this.bitsPerSample0);
float intensity = 1.0f - (value / this.factor);
color.FromVector4(new Vector4(intensity, intensity, intensity, 1.0f));
- pixels[x, y] = color;
+ pixelRow[x] = color;
}
bitReader.NextRow();
diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs
index 484e182c59..72f2336a8c 100644
--- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs
+++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs
@@ -266,7 +266,7 @@ private void DecodeStripsPlanar(ImageFrame frame, int rowsPerStr
using TiffBaseDecompressor decompressor = TiffDecompressorsFactory.Create(this.CompressionType, this.memoryAllocator, this.PhotometricInterpretation, frame.Width, bitsPerPixel, this.Predictor, this.FaxCompressionOptions);
- RgbPlanarTiffColor colorDecoder = TiffColorDecoderFactory.CreatePlanar(this.ColorType, this.BitsPerSample, this.ColorMap);
+ TiffBasePlanarColorDecoder colorDecoder = TiffColorDecoderFactory.CreatePlanar(this.ColorType, this.BitsPerSample, this.ColorMap, this.byteOrder);
for (int i = 0; i < stripsPerPlane; i++)
{
diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs
index 288f01cd1c..14c527a34c 100644
--- a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs
+++ b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs
@@ -111,6 +111,12 @@ private static void ParseColorType(this TiffDecoderCore options, ExifProfile exi
switch (bitsPerChannel)
{
+ case 16:
+ {
+ options.ColorType = TiffColorType.WhiteIsZero16;
+ break;
+ }
+
case 8:
{
options.ColorType = TiffColorType.WhiteIsZero8;
@@ -154,6 +160,12 @@ private static void ParseColorType(this TiffDecoderCore options, ExifProfile exi
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..a0ba40f4e0
--- /dev/null
+++ b/src/ImageSharp/Formats/Tiff/Utils/TiffUtils.cs
@@ -0,0 +1,58 @@
+// Copyright (c) Six Labors.
+// Licensed under the Apache License, Version 2.0.
+
+using System;
+using System.Buffers.Binary;
+using System.Numerics;
+using System.Runtime.CompilerServices;
+using SixLabors.ImageSharp.PixelFormats;
+
+namespace SixLabors.ImageSharp.Formats.Tiff.Utils
+{
+ ///
+ /// Helper methods for TIFF decoding.
+ ///
+ internal static class TiffUtils
+ {
+ public static Vector4 Vector4Default { get; } = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
+
+ public static Rgba64 Rgba64Default { get; } = new Rgba64(0, 0, 0, 0);
+
+ public static L16 L16Default { get; } = new L16(0);
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ushort ConvertToShortBigEndian(ReadOnlySpan buffer) =>
+ BinaryPrimitives.ReadUInt16BigEndian(buffer);
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static ushort ConvertToShortLittleEndian(ReadOnlySpan buffer) =>
+ BinaryPrimitives.ReadUInt16LittleEndian(buffer);
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static TPixel ColorFromL8(L8 l8, byte intensity, TPixel color)
+ where TPixel : unmanaged, IPixel
+ {
+ l8.PackedValue = intensity;
+ color.FromL8(l8);
+ return color;
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static TPixel ColorFromL16(L16 l16, ushort intensity, TPixel color)
+ where TPixel : unmanaged, IPixel
+ {
+ l16.PackedValue = intensity;
+ color.FromL16(l16);
+ return color;
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static TPixel ColorFromRgba64(Rgba64 rgba, ulong r, ulong g, ulong b, TPixel color)
+ where TPixel : unmanaged, IPixel
+ {
+ rgba.PackedValue = r | (g << 16) | (b << 32) | (0xfffful << 48);
+ color.FromRgba64(rgba);
+ return color;
+ }
+ }
+}
diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
index ab53ca1561..3bf1c25f3f 100644
--- a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
@@ -140,7 +140,10 @@ public void TiffDecoder_CanDecode_14Bit(TestImageProvider provid
where TPixel : unmanaged, IPixel => TestTiffDecoder(provider);
[Theory]
+ [WithFile(Flower16BitGrayLittleEndian, PixelTypes.Rgba32)]
[WithFile(Flower16BitGray, PixelTypes.Rgba32)]
+ [WithFile(Flower16BitGrayMinIsWhiteLittleEndian, PixelTypes.Rgba32)]
+ [WithFile(Flower16BitGrayMinIsWhiteBigEndian, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_16Bit(TestImageProvider provider)
where TPixel : unmanaged, IPixel => TestTiffDecoder(provider);
@@ -163,7 +166,9 @@ public void TiffDecoder_CanDecode_42Bit(TestImageProvider provid
[Theory]
[WithFile(FlowerRgb161616Contiguous, PixelTypes.Rgba32)]
+ [WithFile(FlowerRgb161616ContiguousLittleEndian, PixelTypes.Rgba32)]
[WithFile(FlowerRgb161616Planar, PixelTypes.Rgba32)]
+ [WithFile(FlowerRgb161616PlanarLittleEndian, PixelTypes.Rgba32)]
[WithFile(Issues1716Rgb161616BitLittleEndian, PixelTypes.Rgba32)]
public void TiffDecoder_CanDecode_48Bit(TestImageProvider provider)
where TPixel : unmanaged, IPixel => TestTiffDecoder(provider);
diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs
index c54c82d7da..9c24184366 100644
--- a/tests/ImageSharp.Tests/TestImages.cs
+++ b/tests/ImageSharp.Tests/TestImages.cs
@@ -562,7 +562,9 @@ public static class Tiff
public const string Rgb4BitPalette = "Tiff/bike_colorpalette_4bit.tiff";
public const string RgbPaletteDeflate = "Tiff/rgb_palette_deflate.tiff";
public const string FlowerRgb161616Contiguous = "Tiff/flower-rgb-contig-16.tiff";
+ public const string FlowerRgb161616ContiguousLittleEndian = "Tiff/flower-rgb-contig-16_lsb.tiff";
public const string FlowerRgb161616Planar = "Tiff/flower-rgb-planar-16.tiff";
+ public const string FlowerRgb161616PlanarLittleEndian = "Tiff/flower-rgb-planar-16_lsb.tiff";
public const string FlowerRgb141414Contiguous = "Tiff/flower-rgb-contig-14.tiff";
public const string FlowerRgb141414Planar = "Tiff/flower-rgb-planar-14.tiff";
public const string FlowerRgb101010Contiguous = "Tiff/flower-rgb-contig-10.tiff";
@@ -582,6 +584,9 @@ public static class Tiff
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 Flower16BitGrayMinIsWhiteLittleEndian = "Tiff/flower-miniswhite-16_lsb.tiff";
+ public const string Flower16BitGrayMinIsWhiteBigEndian = "Tiff/flower-miniswhite-16.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
diff --git a/tests/Images/Input/Tiff/flower-miniswhite-16.tiff b/tests/Images/Input/Tiff/flower-miniswhite-16.tiff
new file mode 100644
index 0000000000..83266873c5
--- /dev/null
+++ b/tests/Images/Input/Tiff/flower-miniswhite-16.tiff
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:d8f2c2afd8f1645717087bd2edbc3e8a46b88a54a4996c0e9350fdd652b5c382
+size 6588
diff --git a/tests/Images/Input/Tiff/flower-miniswhite-16_lsb.tiff b/tests/Images/Input/Tiff/flower-miniswhite-16_lsb.tiff
new file mode 100644
index 0000000000..ec9ceb1848
--- /dev/null
+++ b/tests/Images/Input/Tiff/flower-miniswhite-16_lsb.tiff
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:435c92b453587e1943940111b66afabf70307beb0e1d65e9701fd9bb753eead2
+size 6588
diff --git a/tests/Images/Input/Tiff/flower-rgb-contig-16_lsb.tiff b/tests/Images/Input/Tiff/flower-rgb-contig-16_lsb.tiff
new file mode 100644
index 0000000000..967d8bbf38
--- /dev/null
+++ b/tests/Images/Input/Tiff/flower-rgb-contig-16_lsb.tiff
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:0951a9c2207eb6864b6a19ec8513a28a874adddb37c3c06b9fd07831372924e3
+size 19150
diff --git a/tests/Images/Input/Tiff/flower-rgb-planar-16_lsb.tiff b/tests/Images/Input/Tiff/flower-rgb-planar-16_lsb.tiff
new file mode 100644
index 0000000000..425ea42efd
--- /dev/null
+++ b/tests/Images/Input/Tiff/flower-rgb-planar-16_lsb.tiff
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:46a60552a7ff37f2c16c43e030e7180872af712f5d9c9c7673e2547049af3da9
+size 19168