From 78bb139128f5ccb1b0276216f507fd541afc9634 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 17 Jun 2018 19:50:59 +0200 Subject: [PATCH] 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 44c53d7767..7e55fe7871 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 86e73fb9fe..8e7d56dde0 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 23738758d8..b1041a93d4 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