From 2e87f7ac9ac20b85af10ee31aa13a311ffb00ead Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Tue, 25 May 2021 23:07:55 +0200 Subject: [PATCH] Add Tiff format to the default Configuration --- src/ImageSharp/Configuration.cs | 9 +++-- .../Formats/Tiff/ConfigurationExtensions.cs | 23 ----------- .../Codecs/DecodeTiff.cs | 4 +- .../Codecs/EncodeTiff.cs | 2 - tests/ImageSharp.Tests/ConfigurationTests.cs | 4 +- .../Formats/GeneralFormatTests.cs | 11 ++++- .../Formats/ImageFormatManagerTests.cs | 3 ++ .../Formats/Tiff/ImageExtensionsTest.cs | 40 ++++++++----------- .../Formats/Tiff/TiffDecoderTests.cs | 14 ++----- .../Formats/Tiff/TiffEncoderTests.cs | 26 +++++------- .../Formats/Tiff/TiffMetadataTests.cs | 14 ++----- 11 files changed, 53 insertions(+), 97 deletions(-) delete mode 100644 src/ImageSharp/Formats/Tiff/ConfigurationExtensions.cs diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index 8a13ad82d..49b7aa79b 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -10,6 +10,7 @@ using SixLabors.ImageSharp.Formats.Gif; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Formats.Tga; +using SixLabors.ImageSharp.Formats.Tiff; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Processing; @@ -39,7 +40,7 @@ namespace SixLabors.ImageSharp /// /// Initializes a new instance of the class. /// - /// A collection of configuration modules to register + /// A collection of configuration modules to register. public Configuration(params IConfigurationModule[] configurationModules) { if (configurationModules != null) @@ -77,7 +78,7 @@ namespace SixLabors.ImageSharp /// /// Gets or sets the size of the buffer to use when working with streams. - /// Intitialized with by default. + /// Initialized with by default. /// public int StreamProcessingBufferSize { @@ -180,6 +181,7 @@ namespace SixLabors.ImageSharp /// /// . /// . + /// . /// /// The default configuration of . internal static Configuration CreateDefaultInstance() @@ -189,7 +191,8 @@ namespace SixLabors.ImageSharp new JpegConfigurationModule(), new GifConfigurationModule(), new BmpConfigurationModule(), - new TgaConfigurationModule()); + new TgaConfigurationModule(), + new TiffConfigurationModule()); } } } diff --git a/src/ImageSharp/Formats/Tiff/ConfigurationExtensions.cs b/src/ImageSharp/Formats/Tiff/ConfigurationExtensions.cs deleted file mode 100644 index 49f87e090..000000000 --- a/src/ImageSharp/Formats/Tiff/ConfigurationExtensions.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) Six Labors. -// Licensed under the Apache License, Version 2.0. - -namespace SixLabors.ImageSharp.Formats.Tiff -{ - /// - /// Helper methods for the Configuration. - /// - public static class ConfigurationExtensions - { - /// - /// Registers the tiff format detector, encoder and decoder. - /// - /// The configuration. - public static void AddTiff(this Configuration configuration) - { - configuration.ImageFormatsManager.AddImageFormat(TiffFormat.Instance); - configuration.ImageFormatsManager.AddImageFormatDetector(new TiffImageFormatDetector()); - configuration.ImageFormatsManager.SetDecoder(TiffFormat.Instance, new TiffDecoder()); - configuration.ImageFormatsManager.SetEncoder(TiffFormat.Instance, new TiffEncoder()); - } - } -} diff --git a/tests/ImageSharp.Benchmarks/Codecs/DecodeTiff.cs b/tests/ImageSharp.Benchmarks/Codecs/DecodeTiff.cs index b388b5d70..e77bb8b3e 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/DecodeTiff.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/DecodeTiff.cs @@ -9,7 +9,6 @@ using System.IO; using BenchmarkDotNet.Attributes; -using SixLabors.ImageSharp.Formats.Tiff; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests; @@ -23,7 +22,7 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs [Config(typeof(Config.ShortMultiFramework))] public class DecodeTiff { - private string prevImage = null; + private string prevImage; private byte[] data; @@ -68,7 +67,6 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs if (this.configuration == null) { this.configuration = new Configuration(); - this.configuration.AddTiff(); this.configuration.StreamProcessingBufferSize = BufferSize; } } diff --git a/tests/ImageSharp.Benchmarks/Codecs/EncodeTiff.cs b/tests/ImageSharp.Benchmarks/Codecs/EncodeTiff.cs index 7154b2310..39055faf5 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/EncodeTiff.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/EncodeTiff.cs @@ -43,8 +43,6 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs if (this.core == null) { this.configuration = new Configuration(); - this.configuration.AddTiff(); - this.core = Image.Load(this.configuration, this.TestImageFullPath); this.drawing = System.Drawing.Image.FromFile(this.TestImageFullPath); } diff --git a/tests/ImageSharp.Tests/ConfigurationTests.cs b/tests/ImageSharp.Tests/ConfigurationTests.cs index 655e98c7f..3ad8ef2f8 100644 --- a/tests/ImageSharp.Tests/ConfigurationTests.cs +++ b/tests/ImageSharp.Tests/ConfigurationTests.cs @@ -21,11 +21,11 @@ namespace SixLabors.ImageSharp.Tests public Configuration DefaultConfiguration { get; } - private readonly int expectedDefaultConfigurationCount = 5; + private readonly int expectedDefaultConfigurationCount = 6; public ConfigurationTests() { - // the shallow copy of configuration should behave exactly like the default configuration, + // The shallow copy of configuration should behave exactly like the default configuration, // so by using the copy, we test both the default and the copy. this.DefaultConfiguration = Configuration.CreateDefaultInstance().Clone(); this.ConfigurationEmpty = new Configuration(); diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index a171d6d52..f34fc9c78 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Tests.Formats public class GeneralFormatTests { /// - /// A collection made up of one file for each image format + /// A collection made up of one file for each image format. /// public static readonly IEnumerable DefaultFiles = new[] @@ -149,6 +149,11 @@ namespace SixLabors.ImageSharp.Tests.Formats { image.SaveAsTga(output); } + + using (FileStream output = File.OpenWrite(Path.Combine(path, $"{file.FileNameWithoutExtension}.tiff"))) + { + image.SaveAsTga(output); + } } } } @@ -196,6 +201,10 @@ namespace SixLabors.ImageSharp.Tests.Formats [InlineData(100, 100, "tga")] [InlineData(100, 10, "tga")] [InlineData(10, 100, "tga")] + [InlineData(100, 100, "tiff")] + [InlineData(100, 10, "tiff")] + [InlineData(10, 100, "tiff")] + public void CanIdentifyImageLoadedFromBytes(int width, int height, string extension) { using (var image = Image.LoadPixelData(new Rgba32[width * height], width, height)) diff --git a/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs b/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs index dea8c62e1..1e00bfff8 100644 --- a/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs +++ b/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs @@ -11,6 +11,7 @@ using SixLabors.ImageSharp.Formats.Gif; using SixLabors.ImageSharp.Formats.Jpeg; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Formats.Tga; +using SixLabors.ImageSharp.Formats.Tiff; using SixLabors.ImageSharp.PixelFormats; using Xunit; @@ -36,12 +37,14 @@ namespace SixLabors.ImageSharp.Tests.Formats Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType().Count()); + Assert.Equal(1, this.DefaultFormatsManager.ImageEncoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); + Assert.Equal(1, this.DefaultFormatsManager.ImageDecoders.Select(item => item.Value).OfType().Count()); } [Fact] diff --git a/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs index e2aeeb9e8..3365a1eb3 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs @@ -13,26 +13,18 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff [Trait("Format", "Tiff")] public class ImageExtensionsTest { - private readonly Configuration configuration; - - public ImageExtensionsTest() - { - this.configuration = new Configuration(); - this.configuration.AddTiff(); - } - [Fact] public void SaveAsTiff_Path() { string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsTiff_Path.tiff"); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { image.SaveAsTiff(file); } - using (Image.Load(this.configuration, file, out IImageFormat mime)) + using (Image.Load(file, out IImageFormat mime)) { Assert.Equal("image/tiff", mime.DefaultMimeType); } @@ -44,12 +36,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsTiffAsync_Path.tiff"); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { await image.SaveAsTiffAsync(file); } - using (Image.Load(this.configuration, file, out IImageFormat mime)) + using (Image.Load(file, out IImageFormat mime)) { Assert.Equal("image/tiff", mime.DefaultMimeType); } @@ -61,12 +53,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsTiff_Path_Encoder.tiff"); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { image.SaveAsTiff(file, new TiffEncoder()); } - using (Image.Load(this.configuration, file, out IImageFormat mime)) + using (Image.Load(file, out IImageFormat mime)) { Assert.Equal("image/tiff", mime.DefaultMimeType); } @@ -78,12 +70,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsTiffAsync_Path_Encoder.tiff"); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { await image.SaveAsTiffAsync(file, new TiffEncoder()); } - using (Image.Load(this.configuration, file, out IImageFormat mime)) + using (Image.Load(file, out IImageFormat mime)) { Assert.Equal("image/tiff", mime.DefaultMimeType); } @@ -94,14 +86,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff { using var memoryStream = new MemoryStream(); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { image.SaveAsTiff(memoryStream); } memoryStream.Position = 0; - using (Image.Load(this.configuration, memoryStream, out IImageFormat mime)) + using (Image.Load(memoryStream, out IImageFormat mime)) { Assert.Equal("image/tiff", mime.DefaultMimeType); } @@ -112,14 +104,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff { using var memoryStream = new MemoryStream(); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { await image.SaveAsTiffAsync(memoryStream); } memoryStream.Position = 0; - using (Image.Load(this.configuration, memoryStream, out IImageFormat mime)) + using (Image.Load(memoryStream, out IImageFormat mime)) { Assert.Equal("image/tiff", mime.DefaultMimeType); } @@ -130,14 +122,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff { using var memoryStream = new MemoryStream(); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { image.SaveAsTiff(memoryStream, new TiffEncoder()); } memoryStream.Position = 0; - using (Image.Load(this.configuration, memoryStream, out IImageFormat mime)) + using (Image.Load(memoryStream, out IImageFormat mime)) { Assert.Equal("image/tiff", mime.DefaultMimeType); } @@ -148,14 +140,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff { using var memoryStream = new MemoryStream(); - using (var image = new Image(this.configuration, 10, 10)) + using (var image = new Image(10, 10)) { await image.SaveAsTiffAsync(memoryStream, new TiffEncoder()); } memoryStream.Position = 0; - using (Image.Load(this.configuration, memoryStream, out IImageFormat mime)) + using (Image.Load(memoryStream, out IImageFormat mime)) { Assert.Equal("image/tiff", mime.DefaultMimeType); } diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs index bffb60302..c35311a2a 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs @@ -28,14 +28,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff private static MagickReferenceDecoder ReferenceDecoder => new MagickReferenceDecoder(); - private readonly Configuration configuration; - - public TiffDecoderTests() - { - this.configuration = new Configuration(); - this.configuration.AddTiff(); - } - [Theory] [WithFileCollection(nameof(NotSupportedImages), PixelTypes.Rgba32)] public void ThrowsNotSupported(TestImageProvider provider) @@ -50,7 +42,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff var testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - IImageInfo info = Image.Identify(this.configuration, stream); + IImageInfo info = Image.Identify(stream); Assert.Equal(expectedPixelSize, info.PixelType?.BitsPerPixel); Assert.Equal(expectedWidth, info.Width); @@ -70,14 +62,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff var testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - IImageInfo info = Image.Identify(this.configuration, stream); + IImageInfo info = Image.Identify(stream); Assert.NotNull(info.Metadata); Assert.Equal(expectedByteOrder, info.Metadata.GetTiffMetadata().ByteOrder); stream.Seek(0, SeekOrigin.Begin); - using var img = Image.Load(this.configuration, stream); + using var img = Image.Load(stream); Assert.Equal(expectedByteOrder, img.Metadata.GetTiffMetadata().ByteOrder); } } diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs index a40ca04bd..546508ca5 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffEncoderTests.cs @@ -22,14 +22,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff { private static readonly IImageDecoder ReferenceDecoder = new MagickReferenceDecoder(); - private static readonly Configuration Configuration; - - static TiffEncoderTests() - { - Configuration = new Configuration(); - Configuration.AddTiff(); - } - [Theory] [InlineData(null, TiffBitsPerPixel.Bit24)] [InlineData(TiffPhotometricInterpretation.Rgb, TiffBitsPerPixel.Bit24)] @@ -55,7 +47,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff // assert memStream.Position = 0; - using var output = Image.Load(Configuration, memStream); + using var output = Image.Load(memStream); TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata(); Assert.Equal(expectedBitsPerPixel, frameMetaData.BitsPerPixel); Assert.Equal(TiffCompression.None, frameMetaData.Compression); @@ -78,7 +70,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff // assert memStream.Position = 0; - using var output = Image.Load(Configuration, memStream); + using var output = Image.Load(memStream); TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata(); Assert.Equal(bitsPerPixel, frameMetaData.BitsPerPixel); @@ -117,7 +109,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff // assert memStream.Position = 0; - using var output = Image.Load(Configuration, memStream); + using var output = Image.Load(memStream); TiffFrameMetadata rootFrameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata(); Assert.Equal(expectedBitsPerPixel, rootFrameMetaData.BitsPerPixel); Assert.Equal(expectedCompression, rootFrameMetaData.Compression); @@ -143,7 +135,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff // assert memStream.Position = 0; - using var output = Image.Load(Configuration, memStream); + using var output = Image.Load(memStream); TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata(); Assert.Equal(expectedBitsPerPixel, frameMetaData.BitsPerPixel); } @@ -162,7 +154,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff // assert memStream.Position = 0; - using var output = Image.Load(Configuration, memStream); + using var output = Image.Load(memStream); TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata(); Assert.Equal(expectedBitsPerPixel, frameMetaData.BitsPerPixel); } @@ -185,7 +177,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff // assert memStream.Position = 0; - using var output = Image.Load(Configuration, memStream); + using var output = Image.Load(memStream); Assert.Equal(expectedCompression, output.Frames.RootFrame.Metadata.GetTiffMetadata().Compression); } @@ -207,7 +199,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff // assert memStream.Position = 0; - using var output = Image.Load(Configuration, memStream); + using var output = Image.Load(memStream); TiffFrameMetadata frameMetadata = output.Frames.RootFrame.Metadata.GetTiffMetadata(); Assert.Equal(expectedPredictor, frameMetadata.Predictor); } @@ -230,7 +222,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff // assert memStream.Position = 0; - using var output = Image.Load(Configuration, memStream); + using var output = Image.Load(memStream); TiffFrameMetadata frameMetaData = output.Frames.RootFrame.Metadata.GetTiffMetadata(); Assert.Equal(TiffBitsPerPixel.Bit1, frameMetaData.BitsPerPixel); Assert.Equal(expectedCompression, frameMetaData.Compression); @@ -422,7 +414,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff // assert memStream.Position = 0; - using var output = Image.Load(Configuration, memStream); + using var output = Image.Load(memStream); ExifProfile exifProfileOutput = output.Frames.RootFrame.Metadata.ExifProfile; TiffFrameMetadata outputMeta = output.Frames.RootFrame.Metadata.GetTiffMetadata(); ImageFrame rootFrame = output.Frames.RootFrame; diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs index f52b74e83..3aded7b0e 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs @@ -20,16 +20,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff [Trait("Format", "Tiff")] public class TiffMetadataTests { - private readonly Configuration configuration; - private static TiffDecoder TiffDecoder => new TiffDecoder(); - public TiffMetadataTests() - { - this.configuration = new Configuration(); - this.configuration.AddTiff(); - } - [Fact] public void TiffMetadata_CloneIsDeep() { @@ -89,7 +81,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff var testFile = TestFile.Create(imagePath); using var stream = new MemoryStream(testFile.Bytes, false); - IImageInfo imageInfo = Image.Identify(this.configuration, stream); + IImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); TiffMetadata tiffMetadata = imageInfo.Metadata.GetTiffMetadata(); @@ -105,7 +97,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff var testFile = TestFile.Create(imagePath); using var stream = new MemoryStream(testFile.Bytes, false); - IImageInfo imageInfo = Image.Identify(this.configuration, stream); + IImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); TiffMetadata tiffMetadata = imageInfo.Metadata.GetTiffMetadata(); @@ -265,7 +257,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff // Assert ms.Position = 0; - using var encodedImage = Image.Load(this.configuration, ms); + using var encodedImage = Image.Load(ms); ImageMetadata encodedImageMetaData = encodedImage.Metadata; ImageFrame rootFrameEncodedImage = encodedImage.Frames.RootFrame;