From 0345d0271ecef71f736290ded435cc736976a397 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 7 Nov 2022 17:33:53 +0100 Subject: [PATCH] Avoid code duplication creating the color decoders --- .../Formats/Tiff/TiffDecoderCore.cs | 74 ++++++++----------- .../Formats/Tiff/TiffDecoderTests.cs | 1 - 2 files changed, 30 insertions(+), 45 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs index 6bdbf442b..15234d8ac 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs @@ -384,16 +384,7 @@ internal class TiffDecoderCore : IImageDecoderInternals } using TiffBaseDecompressor decompressor = this.CreateDecompressor(frame.Width, bitsPerPixel); - - TiffBasePlanarColorDecoder colorDecoder = TiffColorDecoderFactory.CreatePlanar( - this.ColorType, - this.BitsPerSample, - this.ExtraSamplesType, - this.ColorMap, - this.ReferenceBlackAndWhite, - this.YcbcrCoefficients, - this.YcbcrSubSampling, - this.byteOrder); + TiffBasePlanarColorDecoder colorDecoder = this.CreatePlanarColorDecoder(); for (int i = 0; i < stripsPerPlane; i++) { @@ -453,18 +444,7 @@ internal class TiffDecoderCore : IImageDecoderInternals Buffer2D pixels = frame.PixelBuffer; using TiffBaseDecompressor decompressor = this.CreateDecompressor(frame.Width, bitsPerPixel); - - TiffBaseColorDecoder colorDecoder = TiffColorDecoderFactory.Create( - this.configuration, - this.memoryAllocator, - this.ColorType, - this.BitsPerSample, - this.ExtraSamplesType, - this.ColorMap, - this.ReferenceBlackAndWhite, - this.YcbcrCoefficients, - this.YcbcrSubSampling, - this.byteOrder); + TiffBaseColorDecoder colorDecoder = this.CreateChunkyColorDecoder(); for (int stripIndex = 0; stripIndex < stripOffsets.Length; stripIndex++) { @@ -535,16 +515,7 @@ internal class TiffDecoderCore : IImageDecoderInternals } using TiffBaseDecompressor decompressor = this.CreateDecompressor(frame.Width, bitsPerPixel); - - TiffBasePlanarColorDecoder colorDecoder = TiffColorDecoderFactory.CreatePlanar( - this.ColorType, - this.BitsPerSample, - this.ExtraSamplesType, - this.ColorMap, - this.ReferenceBlackAndWhite, - this.YcbcrCoefficients, - this.YcbcrSubSampling, - this.byteOrder); + TiffBasePlanarColorDecoder colorDecoder = this.CreatePlanarColorDecoder(); int tileIndex = 0; int remainingPixelsInColumn = height; @@ -649,18 +620,7 @@ internal class TiffDecoderCore : IImageDecoderInternals Span uncompressedPixelBufferSpan = uncompressedPixelBuffer.GetSpan(); using TiffBaseDecompressor decompressor = this.CreateDecompressor(frame.Width, bitsPerPixel); - - TiffBaseColorDecoder colorDecoder = TiffColorDecoderFactory.Create( - this.configuration, - this.memoryAllocator, - this.ColorType, - this.BitsPerSample, - this.ExtraSamplesType, - this.ColorMap, - this.ReferenceBlackAndWhite, - this.YcbcrCoefficients, - this.YcbcrSubSampling, - this.byteOrder); + TiffBaseColorDecoder colorDecoder = this.CreateChunkyColorDecoder(); int tileIndex = 0; for (int tileY = 0; tileY < tilesDown; tileY++) @@ -700,6 +660,32 @@ internal class TiffDecoderCore : IImageDecoderInternals colorDecoder.Decode(uncompressedPixelBufferSpan, pixels, 0, 0, width, height); } + private TiffBaseColorDecoder CreateChunkyColorDecoder() + where TPixel : unmanaged, IPixel => + TiffColorDecoderFactory.Create( + this.configuration, + this.memoryAllocator, + this.ColorType, + this.BitsPerSample, + this.ExtraSamplesType, + this.ColorMap, + this.ReferenceBlackAndWhite, + this.YcbcrCoefficients, + this.YcbcrSubSampling, + this.byteOrder); + + private TiffBasePlanarColorDecoder CreatePlanarColorDecoder() + where TPixel : unmanaged, IPixel => + TiffColorDecoderFactory.CreatePlanar( + this.ColorType, + this.BitsPerSample, + this.ExtraSamplesType, + this.ColorMap, + this.ReferenceBlackAndWhite, + this.YcbcrCoefficients, + this.YcbcrSubSampling, + this.byteOrder); + private TiffBaseDecompressor CreateDecompressor(int frameWidth, int bitsPerPixel) where TPixel : unmanaged, IPixel => TiffDecompressorsFactory.Create( diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs index 166637a70..ba5b77baf 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs @@ -18,7 +18,6 @@ public class TiffDecoderTests : TiffDecoderBaseTester public static readonly string[] MultiframeTestImages = Multiframes; [Theory] - [WithFile(RgbUncompressedTiled, PixelTypes.Rgba32)] [WithFile(MultiframeDifferentSize, PixelTypes.Rgba32)] [WithFile(MultiframeDifferentVariants, PixelTypes.Rgba32)] public void ThrowsNotSupported(TestImageProvider provider)