From cabce9c49a28b91bc734f1164ecca303fdce68fa Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 17 Jun 2018 19:23:57 +0200 Subject: [PATCH 1/7] clean-up and isolate image load tests --- .../Formats/ImageFormatManagerTests.cs | 6 +- .../Image/ImageDiscoverMimeType.cs | 1 - .../Image/ImageTests.ImageLoadTestBase.cs | 86 +++++ .../Image/ImageTests.LoadPixelData.cs | 31 ++ .../Image/ImageTests.Load_BasicCases.cs | 63 ---- .../Image/ImageTests.Load_ComplexCases.cs | 338 ------------------ .../Image/ImageTests.Load_FileSystemPath.cs | 96 +++++ .../Image/ImageTests.Load_FromBytes.cs | 64 ++++ .../Image/ImageTests.Load_FromStream.cs | 104 ++++++ tests/ImageSharp.Tests/TestFormat.cs | 13 +- 10 files changed, 389 insertions(+), 413 deletions(-) create mode 100644 tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs create mode 100644 tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs delete mode 100644 tests/ImageSharp.Tests/Image/ImageTests.Load_BasicCases.cs delete mode 100644 tests/ImageSharp.Tests/Image/ImageTests.Load_ComplexCases.cs create mode 100644 tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs create mode 100644 tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs create mode 100644 tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs diff --git a/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs b/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs index a6f6600f0..f10a4ce84 100644 --- a/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs +++ b/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs @@ -20,12 +20,12 @@ namespace SixLabors.ImageSharp.Tests { public class ImageFormatManagerTests { - public ImageFormatManager FormatsManagerEmpty { get; private set; } - public ImageFormatManager DefaultFormatsManager { get; private set; } + public ImageFormatManager FormatsManagerEmpty { get; } + public ImageFormatManager DefaultFormatsManager { get; } public ImageFormatManagerTests() { - this.DefaultFormatsManager = Configuration.Default.ImageFormatsManager; + this.DefaultFormatsManager = Configuration.CreateDefaultInstance().ImageFormatsManager; this.FormatsManagerEmpty = new ImageFormatManager(); } diff --git a/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs b/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs index 1a2275062..b05d83204 100644 --- a/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs +++ b/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs @@ -42,7 +42,6 @@ namespace SixLabors.ImageSharp.Tests this.LocalConfiguration.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector); - TestFormat.RegisterGlobalTestFormat(); this.Marker = Guid.NewGuid().ToByteArray(); this.DataStream = TestFormat.GlobalTestFormat.CreateStream(this.Marker); diff --git a/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs new file mode 100644 index 000000000..86e73fb9f --- /dev/null +++ b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs @@ -0,0 +1,86 @@ +namespace SixLabors.ImageSharp.Tests +{ + using System; + using System.IO; + + using Moq; + + using SixLabors.ImageSharp.Formats; + using SixLabors.ImageSharp.IO; + using SixLabors.ImageSharp.PixelFormats; + + public partial class ImageTests + { + public abstract class ImageLoadTestBase : IDisposable + { + + + protected Image returnImage; + + protected Mock localDecoder; + + + + protected IImageFormatDetector localMimeTypeDetector; + + protected Mock localImageFormatMock; + + public Configuration LocalConfiguration { get; } + + public TestFormat TestFormat { get; } = new TestFormat(); + + /// + /// Gets the top-level configuration in the context of this test case. + /// It has registered. + /// + public Configuration TopLevelConfiguration { get; } + + public byte[] Marker { get; private set; } + + public MemoryStream DataStream { get; private set; } + + public byte[] DecodedData { get; private set; } + + protected ImageLoadTestBase() + { + this.returnImage = new Image(1, 1); + + this.localImageFormatMock = new Mock(); + + this.localDecoder = new Mock(); + this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); + this.localDecoder.Setup(x => x.Decode(It.IsAny(), It.IsAny())) + + .Callback((c, s) => + { + using (var ms = new MemoryStream()) + { + s.CopyTo(ms); + this.DecodedData = ms.ToArray(); + } + }) + .Returns(this.returnImage); + + + + this.LocalConfiguration = new Configuration + { + }; + this.LocalConfiguration.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector); + this.LocalConfiguration.ImageFormatsManager.SetDecoder(this.localImageFormatMock.Object, this.localDecoder.Object); + + this.TopLevelConfiguration = new Configuration(this.TestFormat); + + this.Marker = Guid.NewGuid().ToByteArray(); + this.DataStream = this.TestFormat.CreateStream(this.Marker); + + + } + public void Dispose() + { + // clean up the global object; + this.returnImage?.Dispose(); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs b/tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs new file mode 100644 index 000000000..e45602a9e --- /dev/null +++ b/tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs @@ -0,0 +1,31 @@ +namespace SixLabors.ImageSharp.Tests +{ + using SixLabors.ImageSharp.PixelFormats; + + using Xunit; + + public partial class ImageTests + { + public class LoadPixelData + { + [Fact] + public void LoadFromPixelData_Bytes() + { + var img = Image.LoadPixelData(new byte[] { + 0,0,0,255, // 0,0 + 255,255,255,255, // 0,1 + 255,255,255,255, // 1,0 + 0,0,0,255, // 1,1 + }, 2, 2); + + Assert.NotNull(img); + Assert.Equal(Rgba32.Black, img[0, 0]); + Assert.Equal(Rgba32.White, img[0, 1]); + + Assert.Equal(Rgba32.White, img[1, 0]); + Assert.Equal(Rgba32.Black, img[1, 1]); + } + + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_BasicCases.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_BasicCases.cs deleted file mode 100644 index e442b5654..000000000 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_BasicCases.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; -using SixLabors.ImageSharp.PixelFormats; -using Xunit; -// ReSharper disable InconsistentNaming - -namespace SixLabors.ImageSharp.Tests -{ - public partial class ImageTests - { - public class Load_BasicCases - { - [Fact] - public void ByteArray() - { - Assert.Throws(() => - { - Image.Load((byte[])null); - }); - - var file = TestFile.Create(TestImages.Bmp.Car); - using (var image = Image.Load(file.Bytes)) - { - Assert.Equal(600, image.Width); - Assert.Equal(450, image.Height); - } - } - - [Fact] - public void FileSystemPath() - { - var file = TestFile.Create(TestImages.Bmp.Car); - using (var image = Image.Load(file.FullPath)) - { - Assert.Equal(600, image.Width); - Assert.Equal(450, image.Height); - } - } - - [Fact] - public void FileSystemPath_FileNotFound() - { - System.IO.FileNotFoundException ex = Assert.Throws( - () => - { - Image.Load(Guid.NewGuid().ToString()); - }); - } - - [Fact] - public void FileSystemPath_NullPath() - { - ArgumentNullException ex = Assert.Throws( - () => - { - Image.Load((string)null); - }); - } - } - } -} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_ComplexCases.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_ComplexCases.cs deleted file mode 100644 index 957e5c40a..000000000 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_ComplexCases.cs +++ /dev/null @@ -1,338 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; -using System.IO; - -using Moq; - -using SixLabors.ImageSharp.Advanced; -using SixLabors.ImageSharp.Formats; -using SixLabors.ImageSharp.IO; -using SixLabors.ImageSharp.PixelFormats; - -using Xunit; -// ReSharper disable InconsistentNaming - -namespace SixLabors.ImageSharp.Tests -{ - public partial class ImageTests - { - /// - /// Tests the class. - /// - public class Load_ComplexCases : IDisposable - { - private readonly Mock fileSystem; - private readonly Image returnImage; - private readonly Mock localDecoder; - private readonly string FilePath; - private readonly IImageFormatDetector localMimeTypeDetector; - private readonly Mock localImageFormatMock; - - public Configuration LocalConfiguration { get; private set; } - public byte[] Marker { get; private set; } - public MemoryStream DataStream { get; private set; } - public byte[] DecodedData { get; private set; } - - public Load_ComplexCases() - { - this.returnImage = new Image(1, 1); - - this.localImageFormatMock = new Mock(); - - this.localDecoder = new Mock(); - this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); - this.localDecoder.Setup(x => x.Decode(It.IsAny(), It.IsAny())) - - .Callback((c, s) => - { - using (var ms = new MemoryStream()) - { - s.CopyTo(ms); - this.DecodedData = ms.ToArray(); - } - }) - .Returns(this.returnImage); - - this.fileSystem = new Mock(); - - this.LocalConfiguration = new Configuration - { - FileSystem = this.fileSystem.Object - }; - this.LocalConfiguration.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector); - this.LocalConfiguration.ImageFormatsManager.SetDecoder(this.localImageFormatMock.Object, this.localDecoder.Object); - - TestFormat.RegisterGlobalTestFormat(); - this.Marker = Guid.NewGuid().ToByteArray(); - this.DataStream = TestFormat.GlobalTestFormat.CreateStream(this.Marker); - - this.FilePath = Guid.NewGuid().ToString(); - this.fileSystem.Setup(x => x.OpenRead(this.FilePath)).Returns(this.DataStream); - - TestFileSystem.RegisterGlobalTestFormat(); - TestFileSystem.Global.AddFile(this.FilePath, this.DataStream); - } - - [Fact] - public void LoadFromStream() - { - var img = Image.Load(this.DataStream); - - Assert.NotNull(img); - - TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); - } - - [Fact] - public void LoadFromNoneSeekableStream() - { - var stream = new NoneSeekableStream(this.DataStream); - var img = Image.Load(stream); - - Assert.NotNull(img); - - TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); - } - - [Fact] - public void LoadFromStreamWithType() - { - var img = Image.Load(this.DataStream); - - Assert.NotNull(img); - Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); - - TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); - } - - - [Fact] - public void LoadFromStreamWithConfig() - { - Stream stream = new MemoryStream(); - var img = Image.Load(this.LocalConfiguration, stream); - - Assert.NotNull(img); - - this.localDecoder.Verify(x => x.Decode(this.LocalConfiguration, stream)); - } - - [Fact] - public void LoadFromStreamWithTypeAndConfig() - { - Stream stream = new MemoryStream(); - var img = Image.Load(this.LocalConfiguration, stream); - - Assert.NotNull(img); - Assert.Equal(this.returnImage, img); - - this.localDecoder.Verify(x => x.Decode(this.LocalConfiguration, stream)); - } - - - [Fact] - public void LoadFromStreamWithDecoder() - { - Stream stream = new MemoryStream(); - var img = Image.Load(stream, this.localDecoder.Object); - - Assert.NotNull(img); - this.localDecoder.Verify(x => x.Decode(Configuration.Default, stream)); - } - - [Fact] - public void LoadFromStreamWithTypeAndDecoder() - { - Stream stream = new MemoryStream(); - var img = Image.Load(stream, this.localDecoder.Object); - - Assert.NotNull(img); - Assert.Equal(this.returnImage, img); - this.localDecoder.Verify(x => x.Decode(Configuration.Default, stream)); - } - - [Fact] - public void LoadFromBytes() - { - var img = Image.Load(this.DataStream.ToArray()); - - Assert.NotNull(img); - - TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); - } - - [Fact] - public void LoadFromBytesWithType() - { - var img = Image.Load(this.DataStream.ToArray()); - - Assert.NotNull(img); - Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); - - TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); - - } - - [Fact] - public void LoadFromBytesWithConfig() - { - var img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); - - Assert.NotNull(img); - - this.localDecoder.Verify(x => x.Decode(this.LocalConfiguration, It.IsAny())); - - Assert.Equal(this.DataStream.ToArray(), this.DecodedData); - } - - [Fact] - public void LoadFromBytesWithTypeAndConfig() - { - var img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); - - Assert.NotNull(img); - Assert.Equal(this.returnImage, img); - - this.localDecoder.Verify(x => x.Decode(this.LocalConfiguration, It.IsAny())); - - Assert.Equal(this.DataStream.ToArray(), this.DecodedData); - } - - [Fact] - public void LoadFromBytesWithDecoder() - { - var img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); - - Assert.NotNull(img); - this.localDecoder.Verify(x => x.Decode(Configuration.Default, It.IsAny())); - Assert.Equal(this.DataStream.ToArray(), this.DecodedData); - } - - [Fact] - public void LoadFromBytesWithTypeAndDecoder() - { - var img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); - - Assert.NotNull(img); - Assert.Equal(this.returnImage, img); - this.localDecoder.Verify(x => x.Decode(Configuration.Default, It.IsAny())); - Assert.Equal(this.DataStream.ToArray(), this.DecodedData); - } - - [Fact] - public void LoadFromFile() - { - var img = Image.Load(this.DataStream); - - Assert.NotNull(img); - - TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); - } - - [Fact] - public void LoadFromFileWithType() - { - var img = Image.Load(this.DataStream); - - Assert.NotNull(img); - Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); - - TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); - } - - [Fact] - public void LoadFromFileWithConfig() - { - var img = Image.Load(this.LocalConfiguration, this.FilePath); - - Assert.NotNull(img); - - this.localDecoder.Verify(x => x.Decode(this.LocalConfiguration, this.DataStream)); - } - - [Fact] - public void LoadFromFileWithTypeAndConfig() - { - var img = Image.Load(this.LocalConfiguration, this.FilePath); - - Assert.NotNull(img); - Assert.Equal(this.returnImage, img); - - this.localDecoder.Verify(x => x.Decode(this.LocalConfiguration, this.DataStream)); - } - - [Fact] - public void LoadFromFileWithDecoder() - { - var img = Image.Load(this.FilePath, this.localDecoder.Object); - - Assert.NotNull(img); - this.localDecoder.Verify(x => x.Decode(Configuration.Default, this.DataStream)); - } - - [Fact] - public void LoadFromFileWithTypeAndDecoder() - { - var img = Image.Load(this.FilePath, this.localDecoder.Object); - - Assert.NotNull(img); - Assert.Equal(this.returnImage, img); - this.localDecoder.Verify(x => x.Decode(Configuration.Default, this.DataStream)); - } - - [Fact] - public void LoadFromPixelData_Pixels() - { - var img = Image.LoadPixelData(new Rgba32[] { - Rgba32.Black, Rgba32.White, - Rgba32.White, Rgba32.Black, - }, 2, 2); - - Assert.NotNull(img); - Assert.Equal(Rgba32.Black, img[0, 0]); - Assert.Equal(Rgba32.White, img[0, 1]); - - Assert.Equal(Rgba32.White, img[1, 0]); - Assert.Equal(Rgba32.Black, img[1, 1]); - } - - [Fact] - public void LoadFromPixelData_Bytes() - { - var img = Image.LoadPixelData(new byte[] { - 0,0,0,255, // 0,0 - 255,255,255,255, // 0,1 - 255,255,255,255, // 1,0 - 0,0,0,255, // 1,1 - }, 2, 2); - - Assert.NotNull(img); - Assert.Equal(Rgba32.Black, img[0, 0]); - Assert.Equal(Rgba32.White, img[0, 1]); - - Assert.Equal(Rgba32.White, img[1, 0]); - Assert.Equal(Rgba32.Black, img[1, 1]); - } - - - [Fact] - public void LoadsImageWithoutThrowingCrcException() - { - var image1Provider = TestImageProvider.File(TestImages.Png.VersioningImage1); - - using (Image img = image1Provider.GetImage()) - { - Assert.Equal(166036, img.Frames.RootFrame.GetPixelSpan().Length); - } - } - - public void Dispose() - { - // clean up the global object; - this.returnImage?.Dispose(); - } - } - } -} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs new file mode 100644 index 000000000..70e8f729f --- /dev/null +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs @@ -0,0 +1,96 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using SixLabors.ImageSharp.PixelFormats; +using Xunit; +// ReSharper disable InconsistentNaming + +namespace SixLabors.ImageSharp.Tests +{ + using Moq; + + using SixLabors.ImageSharp.IO; + + public partial class ImageTests + { + public class Load_FileSystemPath : ImageLoadTestBase + { + private readonly string filePath = Guid.NewGuid().ToString(); + private readonly Mock localFileSystemMock = new Mock(); + private readonly TestFileSystem topLevelFileSystem = new TestFileSystem(); + + public Load_FileSystemPath() + { + this.localFileSystemMock.Setup(x => x.OpenRead(this.filePath)).Returns(this.DataStream); + + this.topLevelFileSystem.AddFile(this.filePath, this.DataStream); + this.LocalConfiguration.FileSystem = this.localFileSystemMock.Object; + this.TopLevelConfiguration.FileSystem = this.topLevelFileSystem; + } + + [Fact] + public void BasicCase() + { + var img = Image.Load(this.TopLevelConfiguration, this.filePath); + + Assert.NotNull(img); + + this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); + } + + [Fact] + public void UseLocalConfiguration() + { + var img = Image.Load(this.LocalConfiguration, this.filePath); + + Assert.NotNull(img); + + this.localDecoder.Verify(x => x.Decode(this.LocalConfiguration, this.DataStream)); + } + + [Fact] + public void UseCustomDecoder() + { + var img = Image.Load(this.TopLevelConfiguration, this.filePath, this.localDecoder.Object); + + Assert.NotNull(img); + this.localDecoder.Verify(x => x.Decode(this.TopLevelConfiguration, this.DataStream)); + } + + + [Fact] + public void UseGlobalConfigration() + { + var file = TestFile.Create(TestImages.Bmp.Car); + using (var image = Image.Load(file.FullPath)) + { + Assert.Equal(600, image.Width); + Assert.Equal(450, image.Height); + } + } + + [Fact] + public void WhenFileNotFound_Throws() + { + System.IO.FileNotFoundException ex = Assert.Throws( + () => + { + Image.Load(Guid.NewGuid().ToString()); + }); + } + + [Fact] + public void WhenPathIsNull_Throws() + { + ArgumentNullException ex = Assert.Throws( + () => + { + Image.Load((string)null); + }); + } + } + + + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs new file mode 100644 index 000000000..23738758d --- /dev/null +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs @@ -0,0 +1,64 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.IO; +using Moq; +using SixLabors.ImageSharp.PixelFormats; +using Xunit; + +// ReSharper disable InconsistentNaming +namespace SixLabors.ImageSharp.Tests +{ + public partial class ImageTests + { + public class Load_FromBytes : ImageLoadTestBase + { + [Fact] + public void BasicCase() + { + var img = Image.Load(this.TopLevelConfiguration, this.DataStream.ToArray()); + + Assert.NotNull(img); + Assert.Equal(this.TestFormat.Sample(), img); + + this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); + } + + [Fact] + public void NonDefaultPixelType() + { + var img = Image.Load(this.TopLevelConfiguration, this.DataStream.ToArray()); + + Assert.NotNull(img); + Assert.Equal(this.TestFormat.Sample(), img); + + this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); + } + + [Fact] + public void UseLocalConfiguration() + { + var img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); + + Assert.NotNull(img); + + this.localDecoder.Verify(x => x.Decode(this.LocalConfiguration, It.IsAny())); + + Assert.Equal(this.DataStream.ToArray(), this.DecodedData); + } + + [Fact] + public void UseCustomDecoder() + { + var img = Image.Load( + this.TopLevelConfiguration, + this.DataStream.ToArray(), + this.localDecoder.Object); + + Assert.NotNull(img); + this.localDecoder.Verify(x => x.Decode(this.TopLevelConfiguration, It.IsAny())); + Assert.Equal(this.DataStream.ToArray(), this.DecodedData); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs new file mode 100644 index 000000000..1b880a461 --- /dev/null +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs @@ -0,0 +1,104 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.IO; + +using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.PixelFormats; + +using Xunit; +// ReSharper disable InconsistentNaming + +namespace SixLabors.ImageSharp.Tests +{ + public partial class ImageTests + { + /// + /// Tests the class. + /// + public class Load_FromStream : ImageLoadTestBase + { + [Fact] + public void BasicCase() + { + var img = Image.Load(this.TopLevelConfiguration, this.DataStream); + + Assert.NotNull(img); + Assert.Equal(this.TestFormat.Sample(), img); + + this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); + } + + [Fact] + public void NonDefaultPixelTypeImage() + { + var img = Image.Load(this.TopLevelConfiguration, this.DataStream); + + Assert.NotNull(img); + Assert.Equal(this.TestFormat.Sample(), img); + + this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); + } + + [Fact] + public void NonSeekableStream() + { + var stream = new NoneSeekableStream(this.DataStream); + var img = Image.Load(this.TopLevelConfiguration, stream); + + Assert.NotNull(img); + + this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); + } + + [Fact] + public void UseLocalConfiguration() + { + Stream stream = new MemoryStream(); + var img = Image.Load(this.LocalConfiguration, stream); + + Assert.NotNull(img); + + this.localDecoder.Verify(x => x.Decode(this.LocalConfiguration, stream)); + } + + [Fact] + public void UseCustomDecoder() + { + Stream stream = new MemoryStream(); + var img = Image.Load(this.TopLevelConfiguration, stream, this.localDecoder.Object); + + Assert.NotNull(img); + this.localDecoder.Verify(x => x.Decode(this.TopLevelConfiguration, stream)); + } + + [Fact] + public void LoadFromPixelData_Pixels() + { + var img = Image.LoadPixelData(new Rgba32[] { + Rgba32.Black, Rgba32.White, + Rgba32.White, Rgba32.Black, + }, 2, 2); + + Assert.NotNull(img); + Assert.Equal(Rgba32.Black, img[0, 0]); + Assert.Equal(Rgba32.White, img[0, 1]); + + Assert.Equal(Rgba32.White, img[1, 0]); + Assert.Equal(Rgba32.Black, img[1, 1]); + } + + // TODO: This should be a png decoder test! + [Fact] + public void LoadsImageWithoutThrowingCrcException() + { + var image1Provider = TestImageProvider.File(TestImages.Png.VersioningImage1); + + using (Image img = image1Provider.GetImage()) + { + Assert.Equal(166036, img.Frames.RootFrame.GetPixelSpan().Length); + } + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestFormat.cs b/tests/ImageSharp.Tests/TestFormat.cs index 70e6c498a..64357a17e 100644 --- a/tests/ImageSharp.Tests/TestFormat.cs +++ b/tests/ImageSharp.Tests/TestFormat.cs @@ -18,13 +18,10 @@ namespace SixLabors.ImageSharp.Tests /// public class TestFormat : IConfigurationModule, IImageFormat { + // We should not change Configuration.Default in individual tests! + // Create new configuration instances with new Configuration(TestFormat.GlobalTestFormat) instead! public static TestFormat GlobalTestFormat { get; } = new TestFormat(); - public static void RegisterGlobalTestFormat() - { - Configuration.Default.Configure(GlobalTestFormat); - } - public TestFormat() { this.Encoder = new TestEncoder(this); @@ -155,12 +152,12 @@ namespace SixLabors.ImageSharp.Tests private TestFormat testFormat; - public int HeaderSize => testFormat.HeaderSize; + public int HeaderSize => this.testFormat.HeaderSize; public IImageFormat DetectFormat(ReadOnlySpan header) { - if (testFormat.IsSupportedFileFormat(header)) - return testFormat; + if (this.testFormat.IsSupportedFileFormat(header)) + return this.testFormat; return null; } From 78bb139128f5ccb1b0276216f507fd541afc9634 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 17 Jun 2018 19:50:59 +0200 Subject: [PATCH 2/7] common test cases for Image.Load(byte) and Image.Load(span) overloads --- src/ImageSharp/Image.FromBytes.cs | 113 ++++++++++++++---- .../Image/ImageTests.ImageLoadTestBase.cs | 11 +- .../Image/ImageTests.Load_FromBytes.cs | 86 ++++++++++--- 3 files changed, 161 insertions(+), 49 deletions(-) diff --git a/src/ImageSharp/Image.FromBytes.cs b/src/ImageSharp/Image.FromBytes.cs index 44c53d776..7e55fe787 100644 --- a/src/ImageSharp/Image.FromBytes.cs +++ b/src/ImageSharp/Image.FromBytes.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. +using System; using System.IO; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.PixelFormats; @@ -15,7 +16,7 @@ namespace SixLabors.ImageSharp /// /// By reading the header on the provided byte array this calculates the images format. /// - /// The byte array containing image data to read the header from. + /// The byte array containing encoded image data to read the header from. /// The format or null if none found. public static IImageFormat DetectFormat(byte[] data) { @@ -26,7 +27,7 @@ namespace SixLabors.ImageSharp /// By reading the header on the provided byte array this calculates the images format. /// /// The configuration. - /// The byte array containing image data to read the header from. + /// The byte array containing encoded image data to read the header from. /// The mime type or null if none found. public static IImageFormat DetectFormat(Configuration config, byte[] data) { @@ -37,30 +38,30 @@ namespace SixLabors.ImageSharp } /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// /// The byte array containing image data. /// A new . public static Image Load(byte[] data) => Load(Configuration.Default, data); /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// - /// The byte array containing image data. + /// The byte array containing encoded image data. /// The mime type of the decoded image. /// A new . public static Image Load(byte[] data, out IImageFormat format) => Load(Configuration.Default, data, out format); /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// /// The config for the decoder. - /// The byte array containing image data. + /// The byte array containing encoded image data. /// A new . public static Image Load(Configuration config, byte[] data) => Load(config, data); /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// /// The config for the decoder. /// The byte array containing image data. @@ -69,15 +70,15 @@ namespace SixLabors.ImageSharp public static Image Load(Configuration config, byte[] data, out IImageFormat format) => Load(config, data, out format); /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// - /// The byte array containing image data. + /// The byte array containing encoded image data. /// The decoder. /// A new . public static Image Load(byte[] data, IImageDecoder decoder) => Load(data, decoder); /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// /// The config for the decoder. /// The byte array containing image data. @@ -86,9 +87,9 @@ namespace SixLabors.ImageSharp public static Image Load(Configuration config, byte[] data, IImageDecoder decoder) => Load(config, data, decoder); /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// - /// The byte array containing image data. + /// The byte array containing encoded image data. /// The pixel format. /// A new . public static Image Load(byte[] data) @@ -96,7 +97,7 @@ namespace SixLabors.ImageSharp => Load(Configuration.Default, data); /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// /// The byte array containing image data. /// The mime type of the decoded image. @@ -107,10 +108,10 @@ namespace SixLabors.ImageSharp => Load(Configuration.Default, data, out format); /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// /// The configuration options. - /// The byte array containing image data. + /// The byte array containing encoded image data. /// The pixel format. /// A new . public static Image Load(Configuration config, byte[] data) @@ -123,11 +124,11 @@ namespace SixLabors.ImageSharp } /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// /// The configuration options. - /// The byte array containing image data. - /// The mime type of the decoded image. + /// The byte array containing encoded image data. + /// The of the decoded image. /// The pixel format. /// A new . public static Image Load(Configuration config, byte[] data, out IImageFormat format) @@ -140,9 +141,9 @@ namespace SixLabors.ImageSharp } /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// - /// The byte array containing image data. + /// The byte array containing encoded image data. /// The decoder. /// The pixel format. /// A new . @@ -156,10 +157,10 @@ namespace SixLabors.ImageSharp } /// - /// Create a new instance of the class from the given byte array. + /// Load a new instance of from the given encoded byte array. /// /// The Configuration. - /// The byte array containing image data. + /// The byte array containing encoded image data. /// The decoder. /// The pixel format. /// A new . @@ -171,5 +172,71 @@ namespace SixLabors.ImageSharp return Load(config, memoryStream, decoder); } } + + /// + /// Load a new instance of from the given encoded byte span. + /// + /// The byte span containing image data. + /// A new . + public static Image Load(ReadOnlySpan data) => Load(Configuration.Default, data); + + /// + /// Load a new instance of from the given encoded byte span. + /// + /// The config for the decoder. + /// The byte span containing encoded image data. + /// A new . + public static Image Load(Configuration config, ReadOnlySpan data) => Load(config, data); + + /// + /// Load a new instance of from the given encoded byte span. + /// + /// The byte span containing encoded image data. + /// The pixel format. + /// A new . + public static Image Load(ReadOnlySpan data) + where TPixel : struct, IPixel + => Load(Configuration.Default, data); + + /// + /// Load a new instance of from the given encoded byte span. + /// + /// The configuration options. + /// The byte span containing encoded image data. + /// The pixel format. + /// A new . + public static Image Load(Configuration config, ReadOnlySpan data) + where TPixel : struct, IPixel + { + throw new NotImplementedException(); + } + + /// + /// Load a new instance of from the given encoded byte span. + /// + /// The Configuration. + /// The byte span containing image data. + /// The decoder. + /// The pixel format. + /// A new . + public static Image Load(Configuration config, ReadOnlySpan data, IImageDecoder decoder) + where TPixel : struct, IPixel + { + throw new NotImplementedException(); + } + + /// + /// Load a new instance of from the given encoded byte span. + /// + /// The configuration options. + /// The byte span containing image data. + /// The of the decoded image. + /// The pixel format. + /// A new . + public static Image Load(Configuration config, ReadOnlySpan data, out IImageFormat format) + where TPixel : struct, IPixel + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs index 86e73fb9f..8e7d56dde 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs @@ -13,14 +13,10 @@ { public abstract class ImageLoadTestBase : IDisposable { - - protected Image returnImage; protected Mock localDecoder; - - protected IImageFormatDetector localMimeTypeDetector; protected Mock localImageFormatMock; @@ -35,9 +31,9 @@ /// public Configuration TopLevelConfiguration { get; } - public byte[] Marker { get; private set; } + public byte[] Marker { get; } - public MemoryStream DataStream { get; private set; } + public MemoryStream DataStream { get; } public byte[] DecodedData { get; private set; } @@ -50,7 +46,6 @@ this.localDecoder = new Mock(); this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); this.localDecoder.Setup(x => x.Decode(It.IsAny(), It.IsAny())) - .Callback((c, s) => { using (var ms = new MemoryStream()) @@ -61,8 +56,6 @@ }) .Returns(this.returnImage); - - this.LocalConfiguration = new Configuration { }; diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs index 23738758d..b1041a93d 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs @@ -1,9 +1,13 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. - +using System; using System.IO; + using Moq; + using SixLabors.ImageSharp.PixelFormats; +using SixLabors.Primitives; + using Xunit; // ReSharper disable InconsistentNaming @@ -13,10 +17,22 @@ namespace SixLabors.ImageSharp.Tests { public class Load_FromBytes : ImageLoadTestBase { - [Fact] - public void BasicCase() + private byte[] ByteArray => this.DataStream.ToArray(); + + private ReadOnlySpan ByteSpan => this.ByteArray.AsSpan(); + + private byte[] ActualImageBytes => TestFile.Create(TestImages.Bmp.F).Bytes; + + private ReadOnlySpan ActualImageSpan => this.ActualImageBytes.AsSpan(); + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void BasicCase(bool useSpan) { - var img = Image.Load(this.TopLevelConfiguration, this.DataStream.ToArray()); + Image img = useSpan + ? Image.Load(this.TopLevelConfiguration, this.ByteSpan) + : Image.Load(this.TopLevelConfiguration, this.ByteArray); Assert.NotNull(img); Assert.Equal(this.TestFormat.Sample(), img); @@ -24,10 +40,14 @@ namespace SixLabors.ImageSharp.Tests this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); } - [Fact] - public void NonDefaultPixelType() + [Theory] + [InlineData(false)] + [InlineData(true)] + public void NonDefaultPixelType(bool useSpan) { - var img = Image.Load(this.TopLevelConfiguration, this.DataStream.ToArray()); + Image img = useSpan + ? Image.Load(this.TopLevelConfiguration, this.ByteSpan) + : Image.Load(this.TopLevelConfiguration, this.ByteArray); Assert.NotNull(img); Assert.Equal(this.TestFormat.Sample(), img); @@ -35,10 +55,14 @@ namespace SixLabors.ImageSharp.Tests this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); } - [Fact] - public void UseLocalConfiguration() + [Theory] + [InlineData(false)] + [InlineData(true)] + public void UseLocalConfiguration(bool useSpan) { - var img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); + Image img = useSpan + ? Image.Load(this.LocalConfiguration, this.ByteSpan) + : Image.Load(this.LocalConfiguration, this.ByteArray); Assert.NotNull(img); @@ -47,18 +71,46 @@ namespace SixLabors.ImageSharp.Tests Assert.Equal(this.DataStream.ToArray(), this.DecodedData); } - [Fact] - public void UseCustomDecoder() + [Theory] + [InlineData(false)] + [InlineData(true)] + public void UseCustomDecoder(bool useSpan) { - var img = Image.Load( - this.TopLevelConfiguration, - this.DataStream.ToArray(), - this.localDecoder.Object); - + Image img = useSpan + ? Image.Load( + this.TopLevelConfiguration, + this.ByteSpan, + this.localDecoder.Object) + : Image.Load( + this.TopLevelConfiguration, + this.ByteArray, + this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(this.TopLevelConfiguration, It.IsAny())); Assert.Equal(this.DataStream.ToArray(), this.DecodedData); } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void UseGlobalConfiguration(bool useSpan) + { + Image img = useSpan ? Image.Load(this.ActualImageSpan) : Image.Load(this.ActualImageBytes); + + Assert.Equal(new Size(108, 202), img.Size()); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public void UseGlobalConfiguration_NonDefaultPixelType(bool useSpan) + { + Image img = useSpan + ? Image.Load(this.ActualImageSpan) + : Image.Load(this.ActualImageBytes); + + Assert.Equal(new Size(108, 202), img.Size()); + } } } } \ No newline at end of file From 31b77898552b0018ec40c8d01152ef182c9bfd7e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 17 Jun 2018 20:53:21 +0200 Subject: [PATCH 3/7] reference UnmanagedMemoryStream and implement ReadOnlySpan overloads of Image.Load() --- src/ImageSharp/Image.FromBytes.cs | 39 ++++++++++++++++++++++++++----- src/ImageSharp/ImageSharp.csproj | 3 +++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/ImageSharp/Image.FromBytes.cs b/src/ImageSharp/Image.FromBytes.cs index 7e55fe787..8365dbd50 100644 --- a/src/ImageSharp/Image.FromBytes.cs +++ b/src/ImageSharp/Image.FromBytes.cs @@ -173,6 +173,8 @@ namespace SixLabors.ImageSharp } } +#if !NETSTANDARD1_1 + /// /// Load a new instance of from the given encoded byte span. /// @@ -205,10 +207,16 @@ namespace SixLabors.ImageSharp /// The byte span containing encoded image data. /// The pixel format. /// A new . - public static Image Load(Configuration config, ReadOnlySpan data) + public static unsafe Image Load(Configuration config, ReadOnlySpan data) where TPixel : struct, IPixel { - throw new NotImplementedException(); + fixed (byte* ptr = &data.GetPinnableReference()) + { + using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) + { + return Load(config, stream); + } + } } /// @@ -219,10 +227,19 @@ namespace SixLabors.ImageSharp /// The decoder. /// The pixel format. /// A new . - public static Image Load(Configuration config, ReadOnlySpan data, IImageDecoder decoder) + public static unsafe Image Load( + Configuration config, + ReadOnlySpan data, + IImageDecoder decoder) where TPixel : struct, IPixel { - throw new NotImplementedException(); + fixed (byte* ptr = &data.GetPinnableReference()) + { + using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) + { + return Load(config, stream, decoder); + } + } } /// @@ -233,10 +250,20 @@ namespace SixLabors.ImageSharp /// The of the decoded image. /// The pixel format. /// A new . - public static Image Load(Configuration config, ReadOnlySpan data, out IImageFormat format) + public static unsafe Image Load( + Configuration config, + ReadOnlySpan data, + out IImageFormat format) where TPixel : struct, IPixel { - throw new NotImplementedException(); + fixed (byte* ptr = &data.GetPinnableReference()) + { + using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) + { + return Load(config, stream, out format); + } + } } +#endif } } \ No newline at end of file diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index b1934faa6..9860486e3 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -44,6 +44,9 @@ + + + From e63535ee6939151a3c5021956cc52eeff2a112d2 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 17 Jun 2018 21:27:10 +0200 Subject: [PATCH 4/7] refactor tests for DetectFormat --- .../Image/ImageDiscoverMimeType.cs | 104 ------------------ .../Image/ImageTests.DetectFormat.cs | 90 +++++++++++++++ .../Image/ImageTests.ImageLoadTestBase.cs | 12 +- .../Image/ImageTests.Load_FileSystemPath.cs | 19 +--- .../Image/ImageTests.Load_FromBytes.cs | 19 ++-- .../Image/ImageTests.Load_FromStream.cs | 14 +++ 6 files changed, 129 insertions(+), 129 deletions(-) delete mode 100644 tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs create mode 100644 tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs diff --git a/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs b/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs deleted file mode 100644 index b05d83204..000000000 --- a/tests/ImageSharp.Tests/Image/ImageDiscoverMimeType.cs +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; -using System.IO; -using SixLabors.ImageSharp.Formats; -using SixLabors.ImageSharp.IO; -using Moq; -using Xunit; - -namespace SixLabors.ImageSharp.Tests -{ - /// - /// Tests the class. - /// - public class DiscoverImageFormatTests - { - private readonly Mock fileSystem; - private readonly string FilePath; - private readonly IImageFormatDetector localMimeTypeDetector; - private readonly Mock localImageFormatMock; - - public IImageFormat localImageFormat => this.localImageFormatMock.Object; - public Configuration LocalConfiguration { get; private set; } - public byte[] Marker { get; private set; } - public MemoryStream DataStream { get; private set; } - public byte[] DecodedData { get; private set; } - private const string localMimeType = "image/local"; - - public DiscoverImageFormatTests() - { - this.localImageFormatMock = new Mock(); - - this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); - - this.fileSystem = new Mock(); - - this.LocalConfiguration = new Configuration - { - FileSystem = this.fileSystem.Object - }; - - this.LocalConfiguration.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector); - - this.Marker = Guid.NewGuid().ToByteArray(); - this.DataStream = TestFormat.GlobalTestFormat.CreateStream(this.Marker); - - this.FilePath = Guid.NewGuid().ToString(); - this.fileSystem.Setup(x => x.OpenRead(this.FilePath)).Returns(this.DataStream); - - TestFileSystem.RegisterGlobalTestFormat(); - TestFileSystem.Global.AddFile(this.FilePath, this.DataStream); - } - - [Fact] - public void DiscoverImageFormatByteArray() - { - IImageFormat type = Image.DetectFormat(this.DataStream.ToArray()); - Assert.Equal(TestFormat.GlobalTestFormat, type); - } - - [Fact] - public void DiscoverImageFormatByteArray_WithConfig() - { - IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream.ToArray()); - Assert.Equal(this.localImageFormat, type); - } - - [Fact] - public void DiscoverImageFormatFile() - { - IImageFormat type = Image.DetectFormat(this.FilePath); - Assert.Equal(TestFormat.GlobalTestFormat, type); - } - - [Fact] - public void DiscoverImageFormatFilePath_WithConfig() - { - IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.FilePath); - Assert.Equal(this.localImageFormat, type); - } - - [Fact] - public void DiscoverImageFormatStream() - { - IImageFormat type = Image.DetectFormat(this.DataStream); - Assert.Equal(TestFormat.GlobalTestFormat, type); - } - - [Fact] - public void DiscoverImageFormatFileStream_WithConfig() - { - IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream); - Assert.Equal(this.localImageFormat, type); - } - - [Fact] - public void DiscoverImageFormatNoDetectorsRegisterdShouldReturnNull() - { - IImageFormat type = Image.DetectFormat(new Configuration(), this.DataStream); - Assert.Null(type); - } - } -} diff --git a/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs b/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs new file mode 100644 index 000000000..c067bf29f --- /dev/null +++ b/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs @@ -0,0 +1,90 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.IO; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.IO; +using Moq; +using Xunit; +// ReSharper disable InconsistentNaming + +namespace SixLabors.ImageSharp.Tests +{ + public partial class ImageTests + { + /// + /// Tests the class. + /// + public class DetectFormat : ImageLoadTestBase + { + private static readonly string ActualImagePath = TestFile.GetInputFileFullPath(TestImages.Bmp.F); + + private byte[] ActualImageBytes => TestFile.Create(TestImages.Bmp.F).Bytes; + + private ReadOnlySpan ActualImageSpan => this.ActualImageBytes.AsSpan(); + + private byte[] ByteArray => this.DataStream.ToArray(); + + private ReadOnlySpan ByteSpan => this.ByteArray.AsSpan(); + + private IImageFormat LocalImageFormat => this.localImageFormatMock.Object; + + private static readonly IImageFormat ExpectedGlobalFormat = + Configuration.Default.ImageFormatsManager.FindFormatByFileExtension("bmp"); + + [Fact] + public void FromBytes_GlobalConfiguration() + { + IImageFormat type = Image.DetectFormat(this.ActualImageBytes); + + Assert.Equal(ExpectedGlobalFormat, type); + } + + [Fact] + public void FromBytes_CustomConfiguration() + { + IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.ByteArray); + Assert.Equal(this.LocalImageFormat, type); + } + + [Fact] + public void FromFileSystemPath_GlobalConfiguration() + { + IImageFormat type = Image.DetectFormat(ActualImagePath); + Assert.Equal(ExpectedGlobalFormat, type); + } + + [Fact] + public void FromFileSystemPath_CustomConfiguration() + { + IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.MockFilePath); + Assert.Equal(this.LocalImageFormat, type); + } + + [Fact] + public void FromStream_GlobalConfiguration() + { + using (var stream = new MemoryStream(this.ActualImageBytes)) + { + IImageFormat type = Image.DetectFormat(stream); + Assert.Equal(ExpectedGlobalFormat, type); + } + } + + [Fact] + public void FromStream_CustomConfiguration() + { + IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.DataStream); + Assert.Equal(this.LocalImageFormat, type); + } + + [Fact] + public void WhenNoMatchingFormatFound_ReturnsNull() + { + IImageFormat type = Image.DetectFormat(new Configuration(), this.DataStream); + Assert.Null(type); + } + } + } +} diff --git a/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs index 8e7d56dde..983d478cc 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs @@ -21,6 +21,12 @@ protected Mock localImageFormatMock; + protected readonly string MockFilePath = Guid.NewGuid().ToString(); + + internal readonly Mock localFileSystemMock = new Mock(); + + protected readonly TestFileSystem topLevelFileSystem = new TestFileSystem(); + public Configuration LocalConfiguration { get; } public TestFormat TestFormat { get; } = new TestFormat(); @@ -67,8 +73,12 @@ this.Marker = Guid.NewGuid().ToByteArray(); this.DataStream = this.TestFormat.CreateStream(this.Marker); - + this.localFileSystemMock.Setup(x => x.OpenRead(this.MockFilePath)).Returns(this.DataStream); + this.topLevelFileSystem.AddFile(this.MockFilePath, this.DataStream); + this.LocalConfiguration.FileSystem = this.localFileSystemMock.Object; + this.TopLevelConfiguration.FileSystem = this.topLevelFileSystem; } + public void Dispose() { // clean up the global object; diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs index 70e8f729f..1a21d3d10 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath.cs @@ -16,23 +16,10 @@ namespace SixLabors.ImageSharp.Tests { public class Load_FileSystemPath : ImageLoadTestBase { - private readonly string filePath = Guid.NewGuid().ToString(); - private readonly Mock localFileSystemMock = new Mock(); - private readonly TestFileSystem topLevelFileSystem = new TestFileSystem(); - - public Load_FileSystemPath() - { - this.localFileSystemMock.Setup(x => x.OpenRead(this.filePath)).Returns(this.DataStream); - - this.topLevelFileSystem.AddFile(this.filePath, this.DataStream); - this.LocalConfiguration.FileSystem = this.localFileSystemMock.Object; - this.TopLevelConfiguration.FileSystem = this.topLevelFileSystem; - } - [Fact] public void BasicCase() { - var img = Image.Load(this.TopLevelConfiguration, this.filePath); + var img = Image.Load(this.TopLevelConfiguration, this.MockFilePath); Assert.NotNull(img); @@ -42,7 +29,7 @@ namespace SixLabors.ImageSharp.Tests [Fact] public void UseLocalConfiguration() { - var img = Image.Load(this.LocalConfiguration, this.filePath); + var img = Image.Load(this.LocalConfiguration, this.MockFilePath); Assert.NotNull(img); @@ -52,7 +39,7 @@ namespace SixLabors.ImageSharp.Tests [Fact] public void UseCustomDecoder() { - var img = Image.Load(this.TopLevelConfiguration, this.filePath, this.localDecoder.Object); + var img = Image.Load(this.TopLevelConfiguration, this.MockFilePath, this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(this.TopLevelConfiguration, this.DataStream)); diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs index b1041a93d..b23c9d67c 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs @@ -95,9 +95,11 @@ namespace SixLabors.ImageSharp.Tests [InlineData(true)] public void UseGlobalConfiguration(bool useSpan) { - Image img = useSpan ? Image.Load(this.ActualImageSpan) : Image.Load(this.ActualImageBytes); - - Assert.Equal(new Size(108, 202), img.Size()); + using (Image img = + useSpan ? Image.Load(this.ActualImageSpan) : Image.Load(this.ActualImageBytes)) + { + Assert.Equal(new Size(108, 202), img.Size()); + } } [Theory] @@ -105,11 +107,12 @@ namespace SixLabors.ImageSharp.Tests [InlineData(true)] public void UseGlobalConfiguration_NonDefaultPixelType(bool useSpan) { - Image img = useSpan - ? Image.Load(this.ActualImageSpan) - : Image.Load(this.ActualImageBytes); - - Assert.Equal(new Size(108, 202), img.Size()); + using (Image img = useSpan + ? Image.Load(this.ActualImageSpan) + : Image.Load(this.ActualImageBytes)) + { + Assert.Equal(new Size(108, 202), img.Size()); + } } } } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs index 1b880a461..7664c88d9 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs @@ -11,6 +11,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests { + using SixLabors.Primitives; + public partial class ImageTests { /// @@ -29,6 +31,18 @@ namespace SixLabors.ImageSharp.Tests this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); } + [Fact] + public void UseGlobalConfiguration() + { + byte[] data = TestFile.Create(TestImages.Bmp.F).Bytes; + + using (var stream = new MemoryStream(data)) + using (var img = Image.Load(stream)) + { + Assert.Equal(new Size(108, 202), img.Size()); + } + } + [Fact] public void NonDefaultPixelTypeImage() { From ccb35136634307d88f286b27550a5ee8a1279ee5 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 17 Jun 2018 21:32:03 +0200 Subject: [PATCH 5/7] ReadOnlySpan overloads for Image.DetectFormat() --- src/ImageSharp/Image.FromBytes.cs | 27 +++++++++++++++++++ .../Image/ImageTests.DetectFormat.cs | 23 +++++++++++----- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Image.FromBytes.cs b/src/ImageSharp/Image.FromBytes.cs index 8365dbd50..98a39193d 100644 --- a/src/ImageSharp/Image.FromBytes.cs +++ b/src/ImageSharp/Image.FromBytes.cs @@ -175,6 +175,33 @@ namespace SixLabors.ImageSharp #if !NETSTANDARD1_1 + /// + /// By reading the header on the provided byte array this calculates the images format. + /// + /// The byte array containing encoded image data to read the header from. + /// The format or null if none found. + public static IImageFormat DetectFormat(ReadOnlySpan data) + { + return DetectFormat(Configuration.Default, data); + } + + /// + /// By reading the header on the provided byte array this calculates the images format. + /// + /// The configuration. + /// The byte array containing encoded image data to read the header from. + /// The mime type or null if none found. + public static unsafe IImageFormat DetectFormat(Configuration config, ReadOnlySpan data) + { + fixed (byte* ptr = &data.GetPinnableReference()) + { + using (var stream = new UnmanagedMemoryStream(ptr, data.Length)) + { + return DetectFormat(config, stream); + } + } + } + /// /// Load a new instance of from the given encoded byte span. /// diff --git a/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs b/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs index c067bf29f..9d709d488 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs @@ -33,18 +33,27 @@ namespace SixLabors.ImageSharp.Tests private static readonly IImageFormat ExpectedGlobalFormat = Configuration.Default.ImageFormatsManager.FindFormatByFileExtension("bmp"); - [Fact] - public void FromBytes_GlobalConfiguration() + [Theory] + [InlineData(false)] + [InlineData(true)] + public void FromBytes_GlobalConfiguration(bool useSpan) { - IImageFormat type = Image.DetectFormat(this.ActualImageBytes); - + IImageFormat type = useSpan + ? Image.DetectFormat(this.ActualImageSpan) + : Image.DetectFormat(this.ActualImageBytes); + Assert.Equal(ExpectedGlobalFormat, type); } - [Fact] - public void FromBytes_CustomConfiguration() + [Theory] + [InlineData(false)] + [InlineData(true)] + public void FromBytes_CustomConfiguration(bool useSpan) { - IImageFormat type = Image.DetectFormat(this.LocalConfiguration, this.ByteArray); + IImageFormat type = useSpan + ? Image.DetectFormat(this.LocalConfiguration, this.ByteArray.AsSpan()) + : Image.DetectFormat(this.LocalConfiguration, this.ByteArray); + Assert.Equal(this.LocalImageFormat, type); } From 7583a4841530b97b18e855642943e6c466ba34fd Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 17 Jun 2018 22:01:14 +0200 Subject: [PATCH 6/7] better LoadPixelData tests --- .../Image/ImageTests.ImageLoadTestBase.cs | 19 +++--- .../Image/ImageTests.LoadPixelData.cs | 63 ++++++++++++++----- .../Image/ImageTests.Load_FromBytes.cs | 1 + .../Image/ImageTests.Load_FromStream.cs | 16 ----- 4 files changed, 58 insertions(+), 41 deletions(-) diff --git a/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs index 983d478cc..aabc3f50e 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.ImageLoadTestBase.cs @@ -1,14 +1,17 @@ -namespace SixLabors.ImageSharp.Tests -{ - using System; - using System.IO; +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.IO; - using Moq; +using Moq; - using SixLabors.ImageSharp.Formats; - using SixLabors.ImageSharp.IO; - using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.IO; +using SixLabors.ImageSharp.PixelFormats; +namespace SixLabors.ImageSharp.Tests +{ public partial class ImageTests { public abstract class ImageLoadTestBase : IDisposable diff --git a/tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs b/tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs index e45602a9e..7a5fa8729 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs @@ -1,31 +1,60 @@ -namespace SixLabors.ImageSharp.Tests -{ - using SixLabors.ImageSharp.PixelFormats; +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. - using Xunit; +using System; +using SixLabors.ImageSharp.PixelFormats; +using Xunit; +namespace SixLabors.ImageSharp.Tests +{ public partial class ImageTests { public class LoadPixelData { - [Fact] - public void LoadFromPixelData_Bytes() + [Theory] + [InlineData(false)] + [InlineData(true)] + public void FromPixels(bool useSpan) { - var img = Image.LoadPixelData(new byte[] { - 0,0,0,255, // 0,0 - 255,255,255,255, // 0,1 - 255,255,255,255, // 1,0 - 0,0,0,255, // 1,1 - }, 2, 2); + Rgba32[] data = { Rgba32.Black, Rgba32.White, Rgba32.White, Rgba32.Black, }; - Assert.NotNull(img); - Assert.Equal(Rgba32.Black, img[0, 0]); - Assert.Equal(Rgba32.White, img[0, 1]); + using (Image img = useSpan + ? Image.LoadPixelData(data.AsSpan(), 2, 2) + : Image.LoadPixelData(data, 2, 2)) + { + Assert.NotNull(img); + Assert.Equal(Rgba32.Black, img[0, 0]); + Assert.Equal(Rgba32.White, img[0, 1]); - Assert.Equal(Rgba32.White, img[1, 0]); - Assert.Equal(Rgba32.Black, img[1, 1]); + Assert.Equal(Rgba32.White, img[1, 0]); + Assert.Equal(Rgba32.Black, img[1, 1]); + } } + [Theory] + [InlineData(false)] + [InlineData(true)] + public void FromBytes(bool useSpan) + { + byte[] data = + { + 0, 0, 0, 255, // 0,0 + 255, 255, 255, 255, // 0,1 + 255, 255, 255, 255, // 1,0 + 0, 0, 0, 255, // 1,1 + }; + using (Image img = useSpan + ? Image.LoadPixelData(data.AsSpan(), 2, 2) + : Image.LoadPixelData(data, 2, 2)) + { + Assert.NotNull(img); + Assert.Equal(Rgba32.Black, img[0, 0]); + Assert.Equal(Rgba32.White, img[0, 1]); + + Assert.Equal(Rgba32.White, img[1, 0]); + Assert.Equal(Rgba32.Black, img[1, 1]); + } + } } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs index b23c9d67c..eed1a2825 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes.cs @@ -1,5 +1,6 @@ // Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. + using System; using System.IO; diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs index 7664c88d9..6b6acb1b8 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream.cs @@ -86,22 +86,6 @@ namespace SixLabors.ImageSharp.Tests this.localDecoder.Verify(x => x.Decode(this.TopLevelConfiguration, stream)); } - [Fact] - public void LoadFromPixelData_Pixels() - { - var img = Image.LoadPixelData(new Rgba32[] { - Rgba32.Black, Rgba32.White, - Rgba32.White, Rgba32.Black, - }, 2, 2); - - Assert.NotNull(img); - Assert.Equal(Rgba32.Black, img[0, 0]); - Assert.Equal(Rgba32.White, img[0, 1]); - - Assert.Equal(Rgba32.White, img[1, 0]); - Assert.Equal(Rgba32.Black, img[1, 1]); - } - // TODO: This should be a png decoder test! [Fact] public void LoadsImageWithoutThrowingCrcException() From afc13ad2b0366e1a9a45c723b6ce3b348213663c Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 17 Jun 2018 22:43:31 +0200 Subject: [PATCH 7/7] reference System.IO.UnmanagedMemoryStream only for 1.3 --- src/ImageSharp/ImageSharp.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 9860486e3..777c5016c 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -44,7 +44,7 @@ - +