diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs index a31ff7a9fd..1cddc9b63d 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero16TiffColor{TPixel}.cs @@ -16,11 +16,18 @@ internal class BlackIsZero16TiffColor : TiffBaseColorDecoder { private readonly bool isBigEndian; + private readonly Configuration configuration; + /// /// Initializes a new instance of the class. /// + /// The configuration. /// if set to true decodes the pixel data as big endian, otherwise as little endian. - public BlackIsZero16TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; + public BlackIsZero16TiffColor(Configuration configuration, bool isBigEndian) + { + this.configuration = configuration; + this.isBigEndian = isBigEndian; + } /// public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height) @@ -47,13 +54,14 @@ public override void Decode(ReadOnlySpan data, Buffer2D pixels, in } else { - for (int x = 0; x < pixelRow.Length; x++) - { - ushort intensity = TiffUtils.ConvertToShortLittleEndian(data.Slice(offset, 2)); - offset += 2; + int byteCount = pixelRow.Length * 2; + PixelOperations.Instance.FromL16Bytes( + this.configuration, + data.Slice(offset, byteCount), + pixelRow, + pixelRow.Length); - pixelRow[x] = TiffUtils.ColorFromL16(l16, intensity, color); - } + offset += byteCount; } } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor{TPixel}.cs index ea2608f6fd..f62cf29528 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor{TPixel}.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. using System; -using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -14,22 +13,26 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation internal class BlackIsZero8TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel { + private readonly Configuration configuration; + + public BlackIsZero8TiffColor(Configuration configuration) => this.configuration = configuration; + /// public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height) { - var color = default(TPixel); - int offset = 0; - var l8 = default(L8); for (int y = top; y < top + height; y++) { Span pixelRow = pixels.GetRowSpan(y).Slice(left, width); - for (int x = 0; x < pixelRow.Length; x++) - { - byte intensity = data[offset++]; - pixelRow[x] = TiffUtils.ColorFromL8(l8, intensity, color); - } + int byteCount = pixelRow.Length; + PixelOperations.Instance.FromL8Bytes( + this.configuration, + data.Slice(offset, byteCount), + pixelRow, + pixelRow.Length); + + offset += byteCount; } } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs index 4b34d5a0db..e0d558a149 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb161616TiffColor{TPixel}.cs @@ -16,11 +16,18 @@ internal class Rgb161616TiffColor : TiffBaseColorDecoder { private readonly bool isBigEndian; + private readonly Configuration configuration; + /// /// Initializes a new instance of the class. /// + /// The configuration. /// if set to true decodes the pixel data as big endian, otherwise as little endian. - public Rgb161616TiffColor(bool isBigEndian) => this.isBigEndian = isBigEndian; + public Rgb161616TiffColor(Configuration configuration, bool isBigEndian) + { + this.configuration = configuration; + this.isBigEndian = isBigEndian; + } /// public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height) @@ -53,17 +60,14 @@ public override void Decode(ReadOnlySpan data, Buffer2D pixels, in } 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; + int byteCount = pixelRow.Length * 6; + PixelOperations.Instance.FromRgb48Bytes( + this.configuration, + data.Slice(offset, byteCount), + pixelRow, + pixelRow.Length); - pixelRow[x] = TiffUtils.ColorFromRgba64(rgba, r, g, b, color); - } + offset += byteCount; } } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor{TPixel}.cs index 536dece8e3..2a86eb2ee9 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor{TPixel}.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. using System; - using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -14,29 +13,26 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation internal class Rgb888TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel { + private readonly Configuration configuration; + + public Rgb888TiffColor(Configuration configuration) => this.configuration = configuration; + /// public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height) { - var color = default(TPixel); - int offset = 0; - var rgba = default(Rgba32); for (int y = top; y < top + height; y++) { Span pixelRow = pixels.GetRowSpan(y).Slice(left, width); - - for (int x = 0; x < pixelRow.Length; x++) - { - byte r = data[offset++]; - byte g = data[offset++]; - byte b = data[offset++]; - - rgba.PackedValue = (uint)(r | (g << 8) | (b << 16) | (0xff << 24)); - color.FromRgba32(rgba); - - pixelRow[x] = color; - } + int byteCount = pixelRow.Length * 3; + PixelOperations.Instance.FromRgb24Bytes( + this.configuration, + data.Slice(offset, byteCount), + pixelRow, + pixelRow.Length); + + offset += byteCount; } } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs index 0c93998c48..97d3d86655 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory{TPixel}.cs @@ -8,7 +8,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation internal static class TiffColorDecoderFactory where TPixel : unmanaged, IPixel { - public static TiffBaseColorDecoder Create(TiffColorType colorType, TiffBitsPerSample bitsPerSample, ushort[] colorMap, ByteOrder byteOrder) + public static TiffBaseColorDecoder Create(Configuration configuration, TiffColorType colorType, TiffBitsPerSample bitsPerSample, ushort[] colorMap, ByteOrder byteOrder) { switch (colorType) { @@ -55,12 +55,12 @@ public static TiffBaseColorDecoder Create(TiffColorType colorType, TiffB case TiffColorType.BlackIsZero8: DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 8, "bitsPerSample"); DebugGuard.IsTrue(colorMap == null, "colorMap"); - return new BlackIsZero8TiffColor(); + return new BlackIsZero8TiffColor(configuration); case TiffColorType.BlackIsZero16: DebugGuard.IsTrue(bitsPerSample.Channels == 1 && bitsPerSample.Channel0 == 16, "bitsPerSample"); DebugGuard.IsTrue(colorMap == null, "colorMap"); - return new BlackIsZero16TiffColor(byteOrder == ByteOrder.BigEndian); + return new BlackIsZero16TiffColor(configuration, byteOrder == ByteOrder.BigEndian); case TiffColorType.Rgb: DebugGuard.IsTrue(colorMap == null, "colorMap"); @@ -94,7 +94,7 @@ public static TiffBaseColorDecoder Create(TiffColorType colorType, TiffB && bitsPerSample.Channel0 == 8, "bitsPerSample"); DebugGuard.IsTrue(colorMap == null, "colorMap"); - return new Rgb888TiffColor(); + return new Rgb888TiffColor(configuration); case TiffColorType.Rgb101010: DebugGuard.IsTrue( @@ -134,7 +134,7 @@ public static TiffBaseColorDecoder Create(TiffColorType colorType, TiffB && bitsPerSample.Channel0 == 16, "bitsPerSample"); DebugGuard.IsTrue(colorMap == null, "colorMap"); - return new Rgb161616TiffColor(isBigEndian: byteOrder == ByteOrder.BigEndian); + return new Rgb161616TiffColor(configuration, isBigEndian: byteOrder == ByteOrder.BigEndian); case TiffColorType.PaletteColor: DebugGuard.NotNull(colorMap, "colorMap"); diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs index 011d037796..63185619d4 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs @@ -316,7 +316,7 @@ private void DecodeStripsChunky(ImageFrame frame, int rowsPerStr this.Predictor, this.FaxCompressionOptions); - TiffBaseColorDecoder colorDecoder = TiffColorDecoderFactory.Create(this.ColorType, this.BitsPerSample, this.ColorMap, this.byteOrder); + TiffBaseColorDecoder colorDecoder = TiffColorDecoderFactory.Create(this.Configuration, this.ColorType, this.BitsPerSample, this.ColorMap, this.byteOrder); for (int stripIndex = 0; stripIndex < stripOffsets.Length; stripIndex++) { diff --git a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColorTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColorTests.cs index 769ab850e4..38611c6f37 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColorTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColorTests.cs @@ -188,7 +188,7 @@ public void Decode_WritesPixelData_8Bit(byte[] inputData, int bitsPerSample, int { AssertDecode(expectedResult, pixels => { - new BlackIsZero8TiffColor().Decode(inputData, pixels, left, top, width, height); + new BlackIsZero8TiffColor(Configuration.Default).Decode(inputData, pixels, left, top, width, height); }); } } diff --git a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbTiffColorTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbTiffColorTests.cs index 9adf59e484..f9f6331063 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbTiffColorTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbTiffColorTests.cs @@ -179,7 +179,7 @@ public void Decode_WritesPixelData_8Bit(byte[] inputData, TiffBitsPerSample bits { AssertDecode(expectedResult, pixels => { - new Rgb888TiffColor().Decode(inputData, pixels, left, top, width, height); + new Rgb888TiffColor(Configuration.Default).Decode(inputData, pixels, left, top, width, height); }); } }