From 5fcb5fcf0f60a5240ed78bfbc4478a4c52ba8dc5 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Fri, 7 May 2021 20:10:54 +0200 Subject: [PATCH] Review changes --- .../Compressors/DeflateCompressor.cs | 2 +- .../Compression/Compressors/LzwCompressor.cs | 2 +- .../Compression/Compressors/NoCompressor.cs | 2 +- .../Compressors/PackBitsCompressor.cs | 2 +- .../Compression/Compressors/PackBitsWriter.cs | 4 +- .../Compressors/T4BitCompressor.cs | 8 +-- .../Tiff/Compression/HorizontalPredictor.cs | 4 +- .../Formats/Tiff/Constants/TiffConstants.cs | 5 ++ .../Tiff/Constants/TiffNewSubfileType.cs | 12 ++-- .../Formats/Tiff/Constants/TiffPredictor.cs | 2 +- .../Formats/Tiff/ITiffDecoderOptions.cs | 4 +- .../Formats/Tiff/ITiffEncoderOptions.cs | 5 -- .../BlackIsZero1TiffColor.cs | 6 +- .../BlackIsZero4TiffColor.cs | 6 +- .../BlackIsZero8TiffColor.cs | 6 +- .../BlackIsZeroTiffColor.cs | 2 +- .../PaletteTiffColor.cs | 2 +- .../Rgb888TiffColor.cs | 6 +- .../RgbPlanarTiffColor.cs | 2 +- .../PhotometricInterpretation/RgbTiffColor.cs | 2 +- .../TiffBaseColorDecoder.cs | 16 +---- .../TiffColorDecoderFactory.cs | 2 +- .../TiffColorType.cs | 2 +- .../WhiteIsZero1TiffColor.cs | 6 +- .../WhiteIsZero4TiffColor.cs | 6 +- .../WhiteIsZero8TiffColor.cs | 6 +- .../WhiteIsZeroTiffColor.cs | 2 +- src/ImageSharp/Formats/Tiff/README.md | 5 +- .../Formats/Tiff/TiffDecoderCore.cs | 1 + .../Formats/Tiff/TiffDecoderOptionsParser.cs | 1 + src/ImageSharp/Formats/Tiff/TiffEncoder.cs | 3 - .../Formats/Tiff/TiffEncoderCore.cs | 68 ++++++++++--------- .../Tiff/Writers/TiffBaseColorWriter.cs | 6 ++ .../Formats/Tiff/Writers/TiffStreamWriter.cs | 2 +- .../Metadata/Profiles/Exif/ExifReader.cs | 36 ++++------ .../BlackIsZeroTiffColorTests.cs | 2 +- .../PaletteTiffColorTests.cs | 21 +++--- .../RgbPlanarTiffColorTests.cs | 2 +- .../RgbTiffColorTests.cs | 2 +- .../WhiteIsZeroTiffColorTests.cs | 2 +- .../Formats/Tiff/TiffEncoderTests.cs | 38 +++++------ .../ImageSharp.Tests/ImageSharp.Tests.csproj | 1 - 42 files changed, 136 insertions(+), 178 deletions(-) diff --git a/src/ImageSharp/Formats/Tiff/Compression/Compressors/DeflateCompressor.cs b/src/ImageSharp/Formats/Tiff/Compression/Compressors/DeflateCompressor.cs index ad72b5e73..225036f90 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Compressors/DeflateCompressor.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Compressors/DeflateCompressor.cs @@ -9,7 +9,7 @@ using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors { - internal class DeflateCompressor : TiffBaseCompressor + internal sealed class DeflateCompressor : TiffBaseCompressor { private readonly DeflateCompressionLevel compressionLevel; diff --git a/src/ImageSharp/Formats/Tiff/Compression/Compressors/LzwCompressor.cs b/src/ImageSharp/Formats/Tiff/Compression/Compressors/LzwCompressor.cs index d8a20513d..d2ae9096e 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Compressors/LzwCompressor.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Compressors/LzwCompressor.cs @@ -8,7 +8,7 @@ using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors { - internal class LzwCompressor : TiffBaseCompressor + internal sealed class LzwCompressor : TiffBaseCompressor { private TiffLzwEncoder lzwEncoder; diff --git a/src/ImageSharp/Formats/Tiff/Compression/Compressors/NoCompressor.cs b/src/ImageSharp/Formats/Tiff/Compression/Compressors/NoCompressor.cs index 319ca97d9..79bb2e98f 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Compressors/NoCompressor.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Compressors/NoCompressor.cs @@ -8,7 +8,7 @@ using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors { - internal class NoCompressor : TiffBaseCompressor + internal sealed class NoCompressor : TiffBaseCompressor { public NoCompressor(Stream output, MemoryAllocator memoryAllocator, int width, int bitsPerPixel) : base(output, memoryAllocator, width, bitsPerPixel) diff --git a/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsCompressor.cs b/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsCompressor.cs index 61db21fa8..5a2383187 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsCompressor.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsCompressor.cs @@ -8,7 +8,7 @@ using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors { - internal class PackBitsCompressor : TiffBaseCompressor + internal sealed class PackBitsCompressor : TiffBaseCompressor { private IManagedByteBuffer pixelData; diff --git a/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsWriter.cs b/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsWriter.cs index 73c3f36f8..30d21e54c 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsWriter.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsWriter.cs @@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors while (posInRowSpan < rowSpan.Length) { - var useReplicateRun = IsReplicateRun(rowSpan, posInRowSpan); + bool useReplicateRun = IsReplicateRun(rowSpan, posInRowSpan); if (useReplicateRun) { if (literalRunLength > 0) @@ -29,7 +29,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors } // Write a run with the same bytes. - var runLength = FindRunLength(rowSpan, posInRowSpan, maxRunLength); + int runLength = FindRunLength(rowSpan, posInRowSpan, maxRunLength); WriteRun(rowSpan, posInRowSpan, runLength, compressedRowSpan, bytesWritten); bytesWritten += 2; diff --git a/src/ImageSharp/Formats/Tiff/Compression/Compressors/T4BitCompressor.cs b/src/ImageSharp/Formats/Tiff/Compression/Compressors/T4BitCompressor.cs index 11149007f..a016fb712 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Compressors/T4BitCompressor.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Compressors/T4BitCompressor.cs @@ -13,13 +13,13 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors /// /// Bitwriter for writing compressed CCITT T4 1D data. /// - internal class T4BitCompressor : TiffBaseCompressor + internal sealed class T4BitCompressor : TiffBaseCompressor { private const uint WhiteZeroRunTermCode = 0x35; private const uint BlackZeroRunTermCode = 0x37; - private static readonly List MakeupRunLength = new List() + private static readonly uint[] MakeupRunLength = { 64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024, 1088, 1152, 1216, 1280, 1344, 1408, 1472, 1536, 1600, 1664, 1728, 1792, 1856, 1920, 1984, 2048, 2112, 2176, 2240, 2304, 2368, 2432, 2496, 2560 }; @@ -368,7 +368,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors private uint GetBestFittingMakeupRunLength(uint runLength) { - for (int i = 0; i < MakeupRunLength.Count - 1; i++) + for (int i = 0; i < MakeupRunLength.Length - 1; i++) { if (MakeupRunLength[i] <= runLength && MakeupRunLength[i + 1] > runLength) { @@ -376,7 +376,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors } } - return MakeupRunLength[MakeupRunLength.Count - 1]; + return MakeupRunLength[MakeupRunLength.Length - 1]; } private uint GetTermCode(uint runLength, out uint codeLength, bool isWhiteRun) diff --git a/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs b/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs index 34741cd93..1b1254e3f 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs @@ -95,7 +95,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression private static void Undo8Bit(Span pixelBytes, int width) { - var rowBytesCount = width; + int rowBytesCount = width; int height = pixelBytes.Length / rowBytesCount; for (int y = 0; y < height; y++) { @@ -112,7 +112,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression private static void Undo24Bit(Span pixelBytes, int width) { - var rowBytesCount = width * 3; + int rowBytesCount = width * 3; int height = pixelBytes.Length / rowBytesCount; for (int y = 0; y < height; y++) { diff --git a/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs b/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs index 3553c61c5..a30890a69 100644 --- a/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs +++ b/src/ImageSharp/Formats/Tiff/Constants/TiffConstants.cs @@ -75,6 +75,11 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Constants /// public const int SizeOfDouble = 8; + /// + /// The default strip size is 8k. + /// + public const int DefaultStripSize = 8 * 1024; + /// /// The bits per sample for 1 bit bicolor images. /// diff --git a/src/ImageSharp/Formats/Tiff/Constants/TiffNewSubfileType.cs b/src/ImageSharp/Formats/Tiff/Constants/TiffNewSubfileType.cs index 3b84120a5..4ed6aafbb 100644 --- a/src/ImageSharp/Formats/Tiff/Constants/TiffNewSubfileType.cs +++ b/src/ImageSharp/Formats/Tiff/Constants/TiffNewSubfileType.cs @@ -14,31 +14,31 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Constants /// /// A full-resolution image. /// - FullImage = 0x0000, + FullImage = 0, /// /// Reduced-resolution version of another image in this TIFF file. /// - Preview = 0x0001, + Preview = 1, /// /// A single page of a multi-page image. /// - SinglePage = 0x0002, + SinglePage = 2, /// /// A transparency mask for another image in this TIFF file. /// - TransparencyMask = 0x0004, + TransparencyMask = 4, /// /// Alternative reduced-resolution version of another image in this TIFF file (see DNG specification). /// - AlternativePreview = 0x10000, + AlternativePreview = 65536, /// /// Mixed raster content (see RFC2301). /// - MixedRasterContent = 0x0008 + MixedRasterContent = 8 } } diff --git a/src/ImageSharp/Formats/Tiff/Constants/TiffPredictor.cs b/src/ImageSharp/Formats/Tiff/Constants/TiffPredictor.cs index 5eaa9f1d1..6bde23cac 100644 --- a/src/ImageSharp/Formats/Tiff/Constants/TiffPredictor.cs +++ b/src/ImageSharp/Formats/Tiff/Constants/TiffPredictor.cs @@ -9,7 +9,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Constants public enum TiffPredictor : ushort { /// - /// No prediction scheme used before coding + /// No prediction. /// None = 1, diff --git a/src/ImageSharp/Formats/Tiff/ITiffDecoderOptions.cs b/src/ImageSharp/Formats/Tiff/ITiffDecoderOptions.cs index cee66694b..537238439 100644 --- a/src/ImageSharp/Formats/Tiff/ITiffDecoderOptions.cs +++ b/src/ImageSharp/Formats/Tiff/ITiffDecoderOptions.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. namespace SixLabors.ImageSharp.Formats.Tiff @@ -6,7 +6,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff /// /// Encapsulates the options for the . /// - public interface ITiffDecoderOptions + internal interface ITiffDecoderOptions { /// /// Gets a value indicating whether the metadata should be ignored when the image is being decoded. diff --git a/src/ImageSharp/Formats/Tiff/ITiffEncoderOptions.cs b/src/ImageSharp/Formats/Tiff/ITiffEncoderOptions.cs index 9ebf2f025..03c25ea13 100644 --- a/src/ImageSharp/Formats/Tiff/ITiffEncoderOptions.cs +++ b/src/ImageSharp/Formats/Tiff/ITiffEncoderOptions.cs @@ -43,10 +43,5 @@ namespace SixLabors.ImageSharp.Formats.Tiff /// Gets the quantizer for creating a color palette image. /// IQuantizer Quantizer { get; } - - /// - /// Gets the maximum size of strip (bytes). - /// - int MaxStripBytes { get; } } } diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs index b4d609a6f..6724adec0 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero1TiffColor.cs @@ -6,7 +6,7 @@ using System; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'BlackIsZero' photometric interpretation (optimized for bilevel images). @@ -15,10 +15,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff internal class BlackIsZero1TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel { - public BlackIsZero1TiffColor() - { - } - /// public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height) { diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs index d39e28038..cf59c1222 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero4TiffColor.cs @@ -6,7 +6,7 @@ using System; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'BlackIsZero' photometric interpretation (optimized for 4-bit grayscale images). @@ -14,10 +14,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff internal class BlackIsZero4TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel { - public BlackIsZero4TiffColor() - { - } - /// public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height) { diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs index d62898a08..096f0449b 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero8TiffColor.cs @@ -6,7 +6,7 @@ using System; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'BlackIsZero' photometric interpretation (optimized for 8-bit grayscale images). @@ -14,10 +14,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff internal class BlackIsZero8TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel { - public BlackIsZero8TiffColor() - { - } - /// public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height) { diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor.cs index 271672e25..83cef8e75 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColor.cs @@ -7,7 +7,7 @@ using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'BlackIsZero' photometric interpretation (for all bit depths). diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor.cs index 2aefa2ee7..7ed25f822 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/PaletteTiffColor.cs @@ -7,7 +7,7 @@ using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'PaletteTiffColor' photometric interpretation (for all bit depths). diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs index ad3e0909b..e45863a57 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb888TiffColor.cs @@ -6,7 +6,7 @@ using System; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'RGB' photometric interpretation (optimized for 8-bit full color images). @@ -14,10 +14,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff internal class Rgb888TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel { - public Rgb888TiffColor() - { - } - /// public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height) { diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor.cs index 531fd6509..ba98f829c 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColor.cs @@ -6,7 +6,7 @@ using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'RGB' photometric interpretation with 'Planar' layout (for all bit depths). diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor.cs index ef1bc0a23..816ba67b7 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/RgbTiffColor.cs @@ -7,7 +7,7 @@ using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'RGB' photometric interpretation (for all bit depths). diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffBaseColorDecoder.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffBaseColorDecoder.cs index ad67c463f..c08b26ef1 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffBaseColorDecoder.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffBaseColorDecoder.cs @@ -5,7 +5,7 @@ using System; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// The base class for photometric interpretation decoders. @@ -14,20 +14,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff internal abstract class TiffBaseColorDecoder where TPixel : unmanaged, IPixel { - protected TiffBaseColorDecoder() - { - } - - /* - /// - /// Gets the photometric interpretation value. - /// - /// - /// The photometric interpretation value. - /// - public TiffColorType ColorType { get; } - */ - /// /// Decodes source raw pixel data using the current photometric interpretation. /// diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory.cs index 91ae9c2ab..0a7941dfb 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorDecoderFactory.cs @@ -3,7 +3,7 @@ using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { internal static class TiffColorDecoderFactory where TPixel : unmanaged, IPixel diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs index 8eb0fb4de..a9007b640 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/TiffColorType.cs @@ -1,7 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Provides enumeration of the various TIFF photometric interpretation implementation types. diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs index f7be45cdd..465414257 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero1TiffColor.cs @@ -6,7 +6,7 @@ using System; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'WhiteIsZero' photometric interpretation (optimized for bilevel images). @@ -14,10 +14,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff internal class WhiteIsZero1TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel { - public WhiteIsZero1TiffColor() - { - } - /// public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height) { diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs index 4360b2aa2..dae89db28 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero4TiffColor.cs @@ -6,7 +6,7 @@ using System; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'WhiteIsZero' photometric interpretation (optimized for 4-bit grayscale images). @@ -14,10 +14,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff internal class WhiteIsZero4TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel { - public WhiteIsZero4TiffColor() - { - } - /// public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height) { diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs index e03758e65..1b141f9f6 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero8TiffColor.cs @@ -6,7 +6,7 @@ using System; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'WhiteIsZero' photometric interpretation (optimized for 8-bit grayscale images). @@ -14,10 +14,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff internal class WhiteIsZero8TiffColor : TiffBaseColorDecoder where TPixel : unmanaged, IPixel { - public WhiteIsZero8TiffColor() - { - } - /// public override void Decode(ReadOnlySpan data, Buffer2D pixels, int left, int top, int width, int height) { diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColor.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColor.cs index e4e9179f0..697fe2f07 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColor.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColor.cs @@ -7,7 +7,7 @@ using SixLabors.ImageSharp.Formats.Tiff.Utils; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; -namespace SixLabors.ImageSharp.Formats.Tiff +namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation { /// /// Implements the 'WhiteIsZero' photometric interpretation (for all bit depths). diff --git a/src/ImageSharp/Formats/Tiff/README.md b/src/ImageSharp/Formats/Tiff/README.md index 565ecb6cd..5b116b819 100644 --- a/src/ImageSharp/Formats/Tiff/README.md +++ b/src/ImageSharp/Formats/Tiff/README.md @@ -25,6 +25,9 @@ ## Implementation Status +- The Decoder and Encoder currently only supports a single frame per image. +- Some compression formats are not yet supported. See the list below. + ### Deviations from the TIFF spec (to be fixed) - Decoder @@ -46,7 +49,7 @@ |Lzw | Y | Y | Based on ImageSharp GIF LZW implementation - this code could be modified to be (i) shared, or (ii) optimised for each case | |Old Jpeg | | | We should not even try to support this | |Jpeg (Technote 2) | | | | -|Deflate (Technote 2) | Y | Y | Based on PNG Deflate. | +|Deflate (Technote 2) | Y | Y | Based on PNG Deflate. | |Old Deflate (Technote 2) | | Y | | ### Photometric Interpretation Formats diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs index f3e82f86d..fe6778fc2 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Threading; using SixLabors.ImageSharp.Formats.Tiff.Compression; using SixLabors.ImageSharp.Formats.Tiff.Constants; +using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Metadata; diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs index 6b8e6b84e..aaf4502cd 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderOptionsParser.cs @@ -3,6 +3,7 @@ using SixLabors.ImageSharp.Formats.Tiff.Compression; using SixLabors.ImageSharp.Formats.Tiff.Constants; +using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; using SixLabors.ImageSharp.Metadata.Profiles.Exif; namespace SixLabors.ImageSharp.Formats.Tiff diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoder.cs b/src/ImageSharp/Formats/Tiff/TiffEncoder.cs index bf943a995..b66ba339c 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoder.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoder.cs @@ -36,9 +36,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff /// public IQuantizer Quantizer { get; set; } - /// - public int MaxStripBytes { get; set; } = TiffEncoderCore.DefaultStripSize; - /// public void Encode(Image image, Stream stream) where TPixel : unmanaged, IPixel diff --git a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs index 42300969b..6654a6e4b 100644 --- a/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffEncoderCore.cs @@ -25,8 +25,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff /// internal sealed class TiffEncoderCore : IImageEncoderInternals { - public const int DefaultStripSize = 8 * 1024; - private static readonly ushort ByteOrderMarker = BitConverter.IsLittleEndian ? TiffConstants.ByteOrderLittleEndianShort : TiffConstants.ByteOrderBigEndianShort; @@ -56,11 +54,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff /// private readonly DeflateCompressionLevel compressionLevel; - /// - /// The maximum number of bytes for a strip. - /// - private readonly int maxStripBytes; - /// /// Initializes a new instance of the class. /// @@ -75,7 +68,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff this.HorizontalPredictor = options.HorizontalPredictor; this.CompressionType = options.Compression; this.compressionLevel = options.CompressionLevel; - this.maxStripBytes = options.MaxStripBytes; } /// @@ -120,10 +112,9 @@ namespace SixLabors.ImageSharp.Formats.Tiff this.configuration = image.GetConfiguration(); ImageMetadata metadata = image.Metadata; TiffMetadata tiffMetadata = metadata.GetTiffMetadata(); - this.BitsPerPixel ??= tiffMetadata.BitsPerPixel; this.SetMode(tiffMetadata); - this.SetBitsPerPixel(); + this.SetBitsPerPixel(tiffMetadata); this.SetPhotometricInterpretation(); using (var writer = new TiffStreamWriter(stream)) @@ -176,7 +167,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff image.Width, (int)tiffBitsPerPixel, this.compressionLevel, - this.HorizontalPredictor != TiffPredictor.FloatingPoint ? this.HorizontalPredictor : TiffPredictor.None); + this.HorizontalPredictor == TiffPredictor.Horizontal ? this.HorizontalPredictor : TiffPredictor.None); using TiffBaseColorWriter colorWriter = TiffColorWriterFactory.Create( this.Mode, @@ -210,8 +201,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff DebugGuard.MustBeGreaterThan(height, 0, nameof(height)); DebugGuard.MustBeGreaterThan(bytesPerRow, 0, nameof(bytesPerRow)); - int stripBytes = this.maxStripBytes > 0 ? this.maxStripBytes : DefaultStripSize; - int rowsPerStrip = stripBytes / bytesPerRow; + int rowsPerStrip = TiffConstants.DefaultStripSize / bytesPerRow; return rowsPerStrip > 0 ? (rowsPerStrip < height ? rowsPerStrip : height) : 1; } @@ -293,30 +283,46 @@ namespace SixLabors.ImageSharp.Formats.Tiff } } - if (this.Mode == TiffEncodingMode.Default && this.BitsPerPixel != null) + if (this.Mode == TiffEncodingMode.Default && tiffMetadata.BitsPerPixel != null) { // Preserve input bits per pixel, if no encoding mode was specified. - switch (this.BitsPerPixel) - { - case TiffBitsPerPixel.Bit1: - this.Mode = TiffEncodingMode.BiColor; - break; - case TiffBitsPerPixel.Bit4: - this.Mode = TiffEncodingMode.ColorPalette; - break; - case TiffBitsPerPixel.Bit8: - this.Mode = tiffMetadata.PhotometricInterpretation == TiffPhotometricInterpretation.PaletteColor ? TiffEncodingMode.ColorPalette : TiffEncodingMode.Gray; - - break; - default: - this.Mode = TiffEncodingMode.Rgb; - break; - } + this.SetModeWithBitsPerPixel(tiffMetadata.BitsPerPixel, tiffMetadata.PhotometricInterpretation); + + return; + } + + if (this.BitsPerPixel != null) + { + // The user has specified a bits per pixel, so use that to determine the encoding mode. + this.SetModeWithBitsPerPixel(this.BitsPerPixel, tiffMetadata.PhotometricInterpretation); } } - private void SetBitsPerPixel() + private void SetModeWithBitsPerPixel(TiffBitsPerPixel? bitsPerPixel, TiffPhotometricInterpretation photometricInterpretation) { + switch (bitsPerPixel) + { + case TiffBitsPerPixel.Bit1: + this.Mode = TiffEncodingMode.BiColor; + break; + case TiffBitsPerPixel.Bit4: + this.Mode = TiffEncodingMode.ColorPalette; + break; + case TiffBitsPerPixel.Bit8: + this.Mode = photometricInterpretation == TiffPhotometricInterpretation.PaletteColor + ? TiffEncodingMode.ColorPalette + : TiffEncodingMode.Gray; + + break; + default: + this.Mode = TiffEncodingMode.Rgb; + break; + } + } + + private void SetBitsPerPixel(TiffMetadata tiffMetadata) + { + this.BitsPerPixel ??= tiffMetadata.BitsPerPixel; switch (this.Mode) { case TiffEncodingMode.BiColor: diff --git a/src/ImageSharp/Formats/Tiff/Writers/TiffBaseColorWriter.cs b/src/ImageSharp/Formats/Tiff/Writers/TiffBaseColorWriter.cs index 0a04a5089..232daa18d 100644 --- a/src/ImageSharp/Formats/Tiff/Writers/TiffBaseColorWriter.cs +++ b/src/ImageSharp/Formats/Tiff/Writers/TiffBaseColorWriter.cs @@ -22,8 +22,14 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers this.EntriesCollector = entriesCollector; } + /// + /// Gets the bits per pixel. + /// public abstract int BitsPerPixel { get; } + /// + /// Gets the bytes per row. + /// public int BytesPerRow => ((this.Image.Width * this.BitsPerPixel) + 7) / 8; protected ImageFrame Image { get; } diff --git a/src/ImageSharp/Formats/Tiff/Writers/TiffStreamWriter.cs b/src/ImageSharp/Formats/Tiff/Writers/TiffStreamWriter.cs index 8c1d7b759..05a1ca7a2 100644 --- a/src/ImageSharp/Formats/Tiff/Writers/TiffStreamWriter.cs +++ b/src/ImageSharp/Formats/Tiff/Writers/TiffStreamWriter.cs @@ -10,7 +10,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers /// /// Utility class for writing TIFF data to a . /// - internal class TiffStreamWriter : IDisposable + internal sealed class TiffStreamWriter : IDisposable { private static readonly byte[] PaddingBytes = new byte[4]; diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs index 57a9a8cc6..a867b984e 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs @@ -91,9 +91,6 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif private readonly byte[] buf2 = new byte[2]; private readonly Stream data; - - private bool isBigEndian; - private List invalidTags; private uint exifOffset; @@ -120,11 +117,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif /// public uint ThumbnailOffset { get; protected set; } - public bool IsBigEndian - { - get => this.isBigEndian; - protected set => this.isBigEndian = value; - } + public bool IsBigEndian { get; protected set; } protected abstract void RegisterExtLoader(uint offset, Action loader); @@ -330,7 +323,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif { uint newOffset = this.ConvertToUInt32(this.offsetBuffer); - // Ensure that the new index does not overrun the data + // Ensure that the new index does not overrun the data. if (newOffset > int.MaxValue || (newOffset + size) > this.data.Length) { this.AddInvalidTag(new UnkownExifTag(tag)); @@ -339,7 +332,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif this.RegisterExtLoader(newOffset, () => { - var dataBuffer = new byte[size]; + byte[] dataBuffer = new byte[size]; this.Seek(newOffset); if (this.TryReadSpan(dataBuffer)) { @@ -365,8 +358,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif foreach (IExifValue val in values) { - // sometimes duplicates appear, - // can compare val.Tag == exif.Tag + // Sometimes duplicates appear, can compare val.Tag == exif.Tag if (val == exif) { Debug.WriteLine($"Duplicate Exif tag: tag={exif.Tag}, dataType={exif.DataType}"); @@ -403,11 +395,11 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif return false; } - int readed = this.data.Read(span); - return readed == length; + int read = this.data.Read(span); + return read == length; } - // Known as Long in Exif Specification + // Known as Long in Exif Specification. protected uint ReadUInt32() => this.TryReadSpan(this.buf4) ? this.ConvertToUInt32(this.buf4) @@ -424,7 +416,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif return default; } - long intValue = this.isBigEndian + long intValue = this.IsBigEndian ? BinaryPrimitives.ReadInt64BigEndian(buffer) : BinaryPrimitives.ReadInt64LittleEndian(buffer); @@ -433,13 +425,13 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif private uint ConvertToUInt32(ReadOnlySpan buffer) { - // Known as Long in Exif Specification + // Known as Long in Exif Specification. if (buffer.Length < 4) { return default; } - return this.isBigEndian + return this.IsBigEndian ? BinaryPrimitives.ReadUInt32BigEndian(buffer) : BinaryPrimitives.ReadUInt32LittleEndian(buffer); } @@ -451,7 +443,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif return default; } - return this.isBigEndian + return this.IsBigEndian ? BinaryPrimitives.ReadUInt16BigEndian(buffer) : BinaryPrimitives.ReadUInt16LittleEndian(buffer); } @@ -463,7 +455,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif return default; } - int intValue = this.isBigEndian + int intValue = this.IsBigEndian ? BinaryPrimitives.ReadInt32BigEndian(buffer) : BinaryPrimitives.ReadInt32LittleEndian(buffer); @@ -492,7 +484,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif return default; } - return this.isBigEndian + return this.IsBigEndian ? BinaryPrimitives.ReadInt32BigEndian(buffer) : BinaryPrimitives.ReadInt32LittleEndian(buffer); } @@ -517,7 +509,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif return default; } - return this.isBigEndian + return this.IsBigEndian ? BinaryPrimitives.ReadInt16BigEndian(buffer) : BinaryPrimitives.ReadInt16LittleEndian(buffer); } diff --git a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColorTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColorTests.cs index 780c390f7..579ee0290 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColorTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/BlackIsZeroTiffColorTests.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; -using SixLabors.ImageSharp.Formats.Tiff; +using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; using SixLabors.ImageSharp.PixelFormats; using Xunit; diff --git a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/PaletteTiffColorTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/PaletteTiffColorTests.cs index 823d5f5a5..0da1d8bbd 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/PaletteTiffColorTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/PaletteTiffColorTests.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; -using SixLabors.ImageSharp.Formats.Tiff; +using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; using SixLabors.ImageSharp.PixelFormats; using Xunit; @@ -13,9 +13,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff.PhotometricInterpretation [Trait("Format", "Tiff")] public class PaletteTiffColorTests : PhotometricInterpretationTestBase { - public static uint[][] Palette4ColorPalette { get => GeneratePalette(16); } + public static uint[][] Palette4ColorPalette => GeneratePalette(16); - public static ushort[] Palette4ColorMap { get => GenerateColorMap(Palette4ColorPalette); } + public static ushort[] Palette4ColorMap => GenerateColorMap(Palette4ColorPalette); private static readonly byte[] Palette4Bytes4X4 = { @@ -54,9 +54,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff.PhotometricInterpretation } } - public static uint[][] Palette8ColorPalette { get => GeneratePalette(256); } + public static uint[][] Palette8ColorPalette => GeneratePalette(256); - public static ushort[] Palette8ColorMap { get => GenerateColorMap(Palette8ColorPalette); } + public static ushort[] Palette8ColorMap => GenerateColorMap(Palette8ColorPalette); private static readonly byte[] Palette8Bytes4X4 = { @@ -83,13 +83,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff.PhotometricInterpretation [Theory] [MemberData(nameof(Palette4Data))] [MemberData(nameof(Palette8Data))] - public void Decode_WritesPixelData(byte[] inputData, ushort bitsPerSample, ushort[] colorMap, int left, int top, int width, int height, Rgba32[][] expectedResult) - { - AssertDecode(expectedResult, pixels => - { - new PaletteTiffColor(new[] { bitsPerSample }, colorMap).Decode(inputData, pixels, left, top, width, height); - }); - } + public void Decode_WritesPixelData(byte[] inputData, ushort bitsPerSample, ushort[] colorMap, int left, int top, int width, int height, Rgba32[][] expectedResult) => AssertDecode(expectedResult, pixels => + { + new PaletteTiffColor(new[] { bitsPerSample }, colorMap).Decode(inputData, pixels, left, top, width, height); + }); private static uint[][] GeneratePalette(int count) { diff --git a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColorTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColorTests.cs index 78105e420..abfae6ab4 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColorTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColorTests.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; -using SixLabors.ImageSharp.Formats.Tiff; +using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; diff --git a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbTiffColorTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbTiffColorTests.cs index dd232ccc3..4abde8f17 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbTiffColorTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbTiffColorTests.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; -using SixLabors.ImageSharp.Formats.Tiff; +using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; using SixLabors.ImageSharp.PixelFormats; using Xunit; diff --git a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColorTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColorTests.cs index 716ec8e21..620fddd7d 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColorTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/WhiteIsZeroTiffColorTests.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; -using SixLabors.ImageSharp.Formats.Tiff; +using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; using SixLabors.ImageSharp.PixelFormats; using Xunit; diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs index 20eb49376..c2edb9e1c 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs @@ -309,28 +309,28 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff where TPixel : unmanaged, IPixel => TestTiffEncoderCore(provider, TiffBitsPerPixel.Bit1, TiffEncodingMode.BiColor, TiffCompression.Ccitt1D); [Theory] - [WithFile(GrayscaleUncompressed, PixelTypes.L8, TiffEncodingMode.Gray, TiffCompression.PackBits, 16 * 1024)] - [WithFile(PaletteDeflateMultistrip, PixelTypes.L8, TiffEncodingMode.ColorPalette, TiffCompression.Lzw, 32 * 1024)] - [WithFile(RgbUncompressed, PixelTypes.Rgba32, TiffEncodingMode.Rgb, TiffCompression.Deflate, 64 * 1024)] - [WithFile(RgbUncompressed, PixelTypes.Rgb24, TiffEncodingMode.Rgb, TiffCompression.None, 10 * 1024)] - [WithFile(RgbUncompressed, PixelTypes.Rgba32, TiffEncodingMode.Rgb, TiffCompression.None, 30 * 1024)] - [WithFile(RgbUncompressed, PixelTypes.Rgb48, TiffEncodingMode.Rgb, TiffCompression.None, 70 * 1024)] - public void TiffEncoder_StripLength(TestImageProvider provider, TiffEncodingMode mode, TiffCompression compression, int maxSize) + [WithFile(GrayscaleUncompressed, PixelTypes.L8, TiffEncodingMode.Gray, TiffCompression.PackBits)] + [WithFile(PaletteDeflateMultistrip, PixelTypes.L8, TiffEncodingMode.ColorPalette, TiffCompression.Lzw)] + [WithFile(RgbUncompressed, PixelTypes.Rgba32, TiffEncodingMode.Rgb, TiffCompression.Deflate)] + [WithFile(RgbUncompressed, PixelTypes.Rgb24, TiffEncodingMode.Rgb, TiffCompression.None)] + [WithFile(RgbUncompressed, PixelTypes.Rgba32, TiffEncodingMode.Rgb, TiffCompression.None)] + [WithFile(RgbUncompressed, PixelTypes.Rgb48, TiffEncodingMode.Rgb, TiffCompression.None)] + public void TiffEncoder_StripLength(TestImageProvider provider, TiffEncodingMode mode, TiffCompression compression) where TPixel : unmanaged, IPixel => - TestStripLength(provider, mode, compression, maxSize); + TestStripLength(provider, mode, compression); [Theory] - [WithFile(Calliphora_BiColorUncompressed, PixelTypes.L8, TiffEncodingMode.BiColor, TiffCompression.CcittGroup3Fax, 9 * 1024)] - public void TiffEncoder_StripLength_OutOfBounds(TestImageProvider provider, TiffEncodingMode mode, TiffCompression compression, int maxSize) + [WithFile(Calliphora_BiColorUncompressed, PixelTypes.L8, TiffEncodingMode.BiColor, TiffCompression.CcittGroup3Fax)] + public void TiffEncoder_StripLength_OutOfBounds(TestImageProvider provider, TiffEncodingMode mode, TiffCompression compression) where TPixel : unmanaged, IPixel => //// CcittGroup3Fax compressed data length can be larger than the original length - Assert.Throws(() => TestStripLength(provider, mode, compression, maxSize)); + Assert.Throws(() => TestStripLength(provider, mode, compression)); - private static void TestStripLength(TestImageProvider provider, TiffEncodingMode mode, TiffCompression compression, int maxSize) + private static void TestStripLength(TestImageProvider provider, TiffEncodingMode mode, TiffCompression compression) where TPixel : unmanaged, IPixel { // arrange - var tiffEncoder = new TiffEncoder() { Mode = mode, Compression = compression, MaxStripBytes = maxSize }; + var tiffEncoder = new TiffEncoder() { Mode = mode, Compression = compression }; Image input = provider.GetImage(); using var memStream = new MemoryStream(); TiffFrameMetadata inputMeta = input.Frames.RootFrame.Metadata.GetTiffMetadata(); @@ -349,7 +349,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff foreach (Number sz in meta.StripByteCounts) { - Assert.True((uint)sz <= maxSize); + Assert.True((uint)sz <= TiffConstants.DefaultStripSize); } // For uncompressed more accurate test. @@ -359,9 +359,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff { // The difference must be less than one row. int stripBytes = (int)meta.StripByteCounts[i]; - var widthBytes = (meta.BitsPerPixel + 7) / 8 * (int)meta.Width; + int widthBytes = (meta.BitsPerPixel + 7) / 8 * (int)meta.Width; - Assert.True((maxSize - stripBytes) < widthBytes); + Assert.True((TiffConstants.DefaultStripSize - stripBytes) < widthBytes); } } @@ -370,8 +370,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff provider, (TiffBitsPerPixel)inputMeta.BitsPerPixel, mode, - inputMeta.Compression, - maxStripSize: maxSize); + inputMeta.Compression); } private static void TestTiffEncoderCore( @@ -391,8 +390,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff Mode = mode, BitsPerPixel = bitsPerPixel, Compression = compression, - HorizontalPredictor = predictor, - MaxStripBytes = maxStripSize + HorizontalPredictor = predictor }; // Does DebugSave & load reference CompareToReferenceInput(): diff --git a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj index 0a25ae0f9..b6482455e 100644 --- a/tests/ImageSharp.Tests/ImageSharp.Tests.csproj +++ b/tests/ImageSharp.Tests/ImageSharp.Tests.csproj @@ -23,7 +23,6 @@ -