mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
42 changed files with 1236 additions and 1329 deletions
@ -0,0 +1,31 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using SixLabors.Memory; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp |
||||
|
{ |
||||
|
/// <content>
|
||||
|
/// Contains internal extensions for <see cref="Image{TPixel}"/>
|
||||
|
/// </content>
|
||||
|
public static partial class ImageExtensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Locks the image providing access to the pixels.
|
||||
|
/// <remarks>
|
||||
|
/// It is imperative that the accessor is correctly disposed off after use.
|
||||
|
/// </remarks>
|
||||
|
/// </summary>
|
||||
|
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
|
||||
|
/// <param name="image">The image.</param>
|
||||
|
/// <returns>
|
||||
|
/// The <see cref="Buffer2D{TPixel}" />
|
||||
|
/// </returns>
|
||||
|
internal static Buffer2D<TPixel> GetRootFramePixelBuffer<TPixel>(this Image<TPixel> image) |
||||
|
where TPixel : struct, IPixel<TPixel> |
||||
|
{ |
||||
|
return image.Frames.RootFrame.PixelBuffer; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,31 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
|
|
||||
namespace SixLabors.Memory |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// An interface that represents a contigous buffer of value type objects
|
|
||||
/// interpreted as a 2D region of <see cref="Width"/> x <see cref="Height"/> elements.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="T">The value type.</typeparam>
|
|
||||
internal interface IBuffer2D<T> |
|
||||
where T : struct |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Gets the width.
|
|
||||
/// </summary>
|
|
||||
int Width { get; } |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the height.
|
|
||||
/// </summary>
|
|
||||
int Height { get; } |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the contigous buffer being wrapped.
|
|
||||
/// </summary>
|
|
||||
IBuffer<T> Buffer { get; } |
|
||||
} |
|
||||
} |
|
||||
@ -1,48 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using SixLabors.ImageSharp.Advanced; |
|
||||
using SixLabors.ImageSharp.PixelFormats; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Helper methods fro acccess pixel accessors
|
|
||||
/// </summary>
|
|
||||
internal static class PixelAccessorExtensions |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Locks the image providing access to the pixels.
|
|
||||
/// <remarks>
|
|
||||
/// It is imperative that the accessor is correctly disposed off after use.
|
|
||||
/// </remarks>
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
|
|
||||
/// <param name="frame">The frame.</param>
|
|
||||
/// <returns>
|
|
||||
/// The <see cref="PixelAccessor{TPixel}" />
|
|
||||
/// </returns>
|
|
||||
internal static PixelAccessor<TPixel> Lock<TPixel>(this IPixelSource<TPixel> frame) |
|
||||
where TPixel : struct, IPixel<TPixel> |
|
||||
{ |
|
||||
return new PixelAccessor<TPixel>(frame); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Locks the image providing access to the pixels.
|
|
||||
/// <remarks>
|
|
||||
/// It is imperative that the accessor is correctly disposed off after use.
|
|
||||
/// </remarks>
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
|
|
||||
/// <param name="image">The image.</param>
|
|
||||
/// <returns>
|
|
||||
/// The <see cref="PixelAccessor{TPixel}" />
|
|
||||
/// </returns>
|
|
||||
internal static PixelAccessor<TPixel> Lock<TPixel>(this Image<TPixel> image) |
|
||||
where TPixel : struct, IPixel<TPixel> |
|
||||
{ |
|
||||
return image.Frames.RootFrame.Lock(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,151 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
using System.Diagnostics; |
|
||||
using System.Runtime.CompilerServices; |
|
||||
using SixLabors.ImageSharp.Advanced; |
|
||||
using SixLabors.ImageSharp.PixelFormats; |
|
||||
using SixLabors.Memory; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Provides per-pixel access to generic <see cref="Image{TPixel}"/> pixels.
|
|
||||
/// </summary>
|
|
||||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
||||
internal sealed class PixelAccessor<TPixel> : IDisposable, IBuffer2D<TPixel> |
|
||||
where TPixel : struct, IPixel<TPixel> |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Initializes a new instance of the <see cref="PixelAccessor{TPixel}"/> class.
|
|
||||
/// </summary>
|
|
||||
/// <param name="image">The image to provide pixel access for.</param>
|
|
||||
public PixelAccessor(IPixelSource<TPixel> image) |
|
||||
{ |
|
||||
Guard.NotNull(image, nameof(image)); |
|
||||
Guard.MustBeGreaterThan(image.PixelBuffer.Width, 0, "image width"); |
|
||||
Guard.MustBeGreaterThan(image.PixelBuffer.Height, 0, "image height"); |
|
||||
|
|
||||
this.SetPixelBufferUnsafe(image.PixelBuffer); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the <see cref="Buffer2D{T}"/> containing the pixel data.
|
|
||||
/// </summary>
|
|
||||
internal Buffer2D<TPixel> PixelBuffer { get; private set; } |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the size of a single pixel in the number of bytes.
|
|
||||
/// </summary>
|
|
||||
public int PixelSize { get; private set; } |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the width of one row in the number of bytes.
|
|
||||
/// </summary>
|
|
||||
public int RowStride { get; private set; } |
|
||||
|
|
||||
/// <inheritdoc />
|
|
||||
public int Width { get; private set; } |
|
||||
|
|
||||
/// <inheritdoc />
|
|
||||
public int Height { get; private set; } |
|
||||
|
|
||||
public IBuffer<TPixel> Buffer => this.PixelBuffer.Buffer; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets or sets the pixel at the specified position.
|
|
||||
/// </summary>
|
|
||||
/// <param name="x">The x-coordinate of the pixel. Must be greater than or equal to zero and less than the width of the image.</param>
|
|
||||
/// <param name="y">The y-coordinate of the pixel. Must be greater than or equal to zero and less than the height of the image.</param>
|
|
||||
/// <returns>The <see typeparam="TPixel"/> at the specified position.</returns>
|
|
||||
public TPixel this[int x, int y] |
|
||||
{ |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
get |
|
||||
{ |
|
||||
this.CheckCoordinates(x, y); |
|
||||
return this.GetSpan()[(y * this.Width) + x]; |
|
||||
} |
|
||||
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
set |
|
||||
{ |
|
||||
this.CheckCoordinates(x, y); |
|
||||
Span<TPixel> span = this.GetSpan(); |
|
||||
span[(y * this.Width) + x] = value; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc />
|
|
||||
public void Dispose() |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Resets all the pixels to it's initial value.
|
|
||||
/// </summary>
|
|
||||
public void Reset() |
|
||||
{ |
|
||||
this.PixelBuffer.Buffer.Clear(); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Sets the pixel buffer in an unsafe manner. This should not be used unless you know what its doing!!!
|
|
||||
/// </summary>
|
|
||||
/// <param name="pixels">The pixels.</param>
|
|
||||
/// <returns>Returns the old pixel data thats has gust been replaced.</returns>
|
|
||||
/// <remarks>If <see cref="M:PixelAccessor.PooledMemory"/> is true then caller is responsible for ensuring <see cref="M:PixelDataPool.Return()"/> is called.</remarks>
|
|
||||
internal Buffer2D<TPixel> SwapBufferOwnership(Buffer2D<TPixel> pixels) |
|
||||
{ |
|
||||
Buffer2D<TPixel> oldPixels = this.PixelBuffer; |
|
||||
this.SetPixelBufferUnsafe(pixels); |
|
||||
return oldPixels; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Copies the pixels to another <see cref="PixelAccessor{TPixel}"/> of the same size.
|
|
||||
/// </summary>
|
|
||||
/// <param name="target">The target pixel buffer accessor.</param>
|
|
||||
internal void CopyTo(PixelAccessor<TPixel> target) |
|
||||
{ |
|
||||
this.PixelBuffer.GetSpan().CopyTo(target.PixelBuffer.GetSpan()); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Sets the pixel buffer in an unsafe manor this should not be used unless you know what its doing!!!
|
|
||||
/// </summary>
|
|
||||
/// <param name="pixels">The pixel buffer</param>
|
|
||||
private void SetPixelBufferUnsafe(Buffer2D<TPixel> pixels) |
|
||||
{ |
|
||||
this.PixelBuffer = pixels; |
|
||||
|
|
||||
this.Width = pixels.Width; |
|
||||
this.Height = pixels.Height; |
|
||||
this.PixelSize = Unsafe.SizeOf<TPixel>(); |
|
||||
this.RowStride = this.Width * this.PixelSize; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Checks the coordinates to ensure they are within bounds.
|
|
||||
/// </summary>
|
|
||||
/// <param name="x">The x-coordinate of the pixel. Must be greater than zero and less than the width of the image.</param>
|
|
||||
/// <param name="y">The y-coordinate of the pixel. Must be greater than zero and less than the height of the image.</param>
|
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
|
||||
/// Thrown if the coordinates are not within the bounds of the image.
|
|
||||
/// </exception>
|
|
||||
[Conditional("DEBUG")] |
|
||||
private void CheckCoordinates(int x, int y) |
|
||||
{ |
|
||||
if (x < 0 || x >= this.Width) |
|
||||
{ |
|
||||
throw new ArgumentOutOfRangeException(nameof(x), x, $"{x} is outwith the image bounds."); |
|
||||
} |
|
||||
|
|
||||
if (y < 0 || y >= this.Height) |
|
||||
{ |
|
||||
throw new ArgumentOutOfRangeException(nameof(y), y, $"{y} is outwith the image bounds."); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,105 +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 |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Tests the <see cref="Image"/> class.
|
|
||||
/// </summary>
|
|
||||
public class DiscoverImageFormatTests |
|
||||
{ |
|
||||
private readonly Mock<IFileSystem> fileSystem; |
|
||||
private readonly string FilePath; |
|
||||
private readonly IImageFormatDetector localMimeTypeDetector; |
|
||||
private readonly Mock<IImageFormat> 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<IImageFormat>(); |
|
||||
|
|
||||
this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); |
|
||||
|
|
||||
this.fileSystem = new Mock<IFileSystem>(); |
|
||||
|
|
||||
this.LocalConfiguration = new Configuration |
|
||||
{ |
|
||||
FileSystem = this.fileSystem.Object |
|
||||
}; |
|
||||
|
|
||||
this.LocalConfiguration.ImageFormatsManager.AddImageFormatDetector(this.localMimeTypeDetector); |
|
||||
|
|
||||
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 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); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,99 @@ |
|||||
|
// 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 |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Tests the <see cref="Image"/> class.
|
||||
|
/// </summary>
|
||||
|
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<byte> ActualImageSpan => this.ActualImageBytes.AsSpan(); |
||||
|
|
||||
|
private byte[] ByteArray => this.DataStream.ToArray(); |
||||
|
|
||||
|
private ReadOnlySpan<byte> ByteSpan => this.ByteArray.AsSpan(); |
||||
|
|
||||
|
private IImageFormat LocalImageFormat => this.localImageFormatMock.Object; |
||||
|
|
||||
|
private static readonly IImageFormat ExpectedGlobalFormat = |
||||
|
Configuration.Default.ImageFormatsManager.FindFormatByFileExtension("bmp"); |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(false)] |
||||
|
[InlineData(true)] |
||||
|
public void FromBytes_GlobalConfiguration(bool useSpan) |
||||
|
{ |
||||
|
IImageFormat type = useSpan |
||||
|
? Image.DetectFormat(this.ActualImageSpan) |
||||
|
: Image.DetectFormat(this.ActualImageBytes); |
||||
|
|
||||
|
Assert.Equal(ExpectedGlobalFormat, type); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(false)] |
||||
|
[InlineData(true)] |
||||
|
public void FromBytes_CustomConfiguration(bool useSpan) |
||||
|
{ |
||||
|
IImageFormat type = useSpan |
||||
|
? Image.DetectFormat(this.LocalConfiguration, this.ByteArray.AsSpan()) |
||||
|
: 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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,92 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.IO; |
||||
|
|
||||
|
using Moq; |
||||
|
|
||||
|
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 |
||||
|
{ |
||||
|
protected Image<Rgba32> returnImage; |
||||
|
|
||||
|
protected Mock<IImageDecoder> localDecoder; |
||||
|
|
||||
|
protected IImageFormatDetector localMimeTypeDetector; |
||||
|
|
||||
|
protected Mock<IImageFormat> localImageFormatMock; |
||||
|
|
||||
|
protected readonly string MockFilePath = Guid.NewGuid().ToString(); |
||||
|
|
||||
|
internal readonly Mock<IFileSystem> localFileSystemMock = new Mock<IFileSystem>(); |
||||
|
|
||||
|
protected readonly TestFileSystem topLevelFileSystem = new TestFileSystem(); |
||||
|
|
||||
|
public Configuration LocalConfiguration { get; } |
||||
|
|
||||
|
public TestFormat TestFormat { get; } = new TestFormat(); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the top-level configuration in the context of this test case.
|
||||
|
/// It has <see cref="TestFormat"/> registered.
|
||||
|
/// </summary>
|
||||
|
public Configuration TopLevelConfiguration { get; } |
||||
|
|
||||
|
public byte[] Marker { get; } |
||||
|
|
||||
|
public MemoryStream DataStream { get; } |
||||
|
|
||||
|
public byte[] DecodedData { get; private set; } |
||||
|
|
||||
|
protected ImageLoadTestBase() |
||||
|
{ |
||||
|
this.returnImage = new Image<Rgba32>(1, 1); |
||||
|
|
||||
|
this.localImageFormatMock = new Mock<IImageFormat>(); |
||||
|
|
||||
|
this.localDecoder = new Mock<IImageDecoder>(); |
||||
|
this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); |
||||
|
this.localDecoder.Setup(x => x.Decode<Rgba32>(It.IsAny<Configuration>(), It.IsAny<Stream>())) |
||||
|
.Callback<Configuration, Stream>((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); |
||||
|
|
||||
|
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;
|
||||
|
this.returnImage?.Dispose(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,60 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using SixLabors.ImageSharp.PixelFormats; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.Tests |
||||
|
{ |
||||
|
public partial class ImageTests |
||||
|
{ |
||||
|
public class LoadPixelData |
||||
|
{ |
||||
|
[Theory] |
||||
|
[InlineData(false)] |
||||
|
[InlineData(true)] |
||||
|
public void FromPixels(bool useSpan) |
||||
|
{ |
||||
|
Rgba32[] data = { Rgba32.Black, Rgba32.White, Rgba32.White, Rgba32.Black, }; |
||||
|
|
||||
|
using (Image<Rgba32> img = useSpan |
||||
|
? Image.LoadPixelData<Rgba32>(data.AsSpan(), 2, 2) |
||||
|
: Image.LoadPixelData<Rgba32>(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]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[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<Rgba32> img = useSpan |
||||
|
? Image.LoadPixelData<Rgba32>(data.AsSpan(), 2, 2) |
||||
|
: Image.LoadPixelData<Rgba32>(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]); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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<ArgumentNullException>(() => |
|
||||
{ |
|
||||
Image.Load<Rgba32>((byte[])null); |
|
||||
}); |
|
||||
|
|
||||
var file = TestFile.Create(TestImages.Bmp.Car); |
|
||||
using (var image = Image.Load<Rgba32>(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<Rgba32>(file.FullPath)) |
|
||||
{ |
|
||||
Assert.Equal(600, image.Width); |
|
||||
Assert.Equal(450, image.Height); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void FileSystemPath_FileNotFound() |
|
||||
{ |
|
||||
System.IO.FileNotFoundException ex = Assert.Throws<System.IO.FileNotFoundException>( |
|
||||
() => |
|
||||
{ |
|
||||
Image.Load<Rgba32>(Guid.NewGuid().ToString()); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void FileSystemPath_NullPath() |
|
||||
{ |
|
||||
ArgumentNullException ex = Assert.Throws<ArgumentNullException>( |
|
||||
() => |
|
||||
{ |
|
||||
Image.Load<Rgba32>((string)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 |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Tests the <see cref="Image"/> class.
|
|
||||
/// </summary>
|
|
||||
public class Load_ComplexCases : IDisposable |
|
||||
{ |
|
||||
private readonly Mock<IFileSystem> fileSystem; |
|
||||
private readonly Image<Rgba32> returnImage; |
|
||||
private readonly Mock<IImageDecoder> localDecoder; |
|
||||
private readonly string FilePath; |
|
||||
private readonly IImageFormatDetector localMimeTypeDetector; |
|
||||
private readonly Mock<IImageFormat> 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<Rgba32>(1, 1); |
|
||||
|
|
||||
this.localImageFormatMock = new Mock<IImageFormat>(); |
|
||||
|
|
||||
this.localDecoder = new Mock<IImageDecoder>(); |
|
||||
this.localMimeTypeDetector = new MockImageFormatDetector(this.localImageFormatMock.Object); |
|
||||
this.localDecoder.Setup(x => x.Decode<Rgba32>(It.IsAny<Configuration>(), It.IsAny<Stream>())) |
|
||||
|
|
||||
.Callback<Configuration, Stream>((c, s) => |
|
||||
{ |
|
||||
using (var ms = new MemoryStream()) |
|
||||
{ |
|
||||
s.CopyTo(ms); |
|
||||
this.DecodedData = ms.ToArray(); |
|
||||
} |
|
||||
}) |
|
||||
.Returns(this.returnImage); |
|
||||
|
|
||||
this.fileSystem = new Mock<IFileSystem>(); |
|
||||
|
|
||||
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<Rgba32>(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<Rgba32>(stream); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
|
|
||||
TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromStreamWithType() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.DataStream); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
Assert.Equal(TestFormat.GlobalTestFormat.Sample<Rgba32>(), img); |
|
||||
|
|
||||
TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromStreamWithConfig() |
|
||||
{ |
|
||||
Stream stream = new MemoryStream(); |
|
||||
var img = Image.Load<Rgba32>(this.LocalConfiguration, stream); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
|
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.LocalConfiguration, stream)); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromStreamWithTypeAndConfig() |
|
||||
{ |
|
||||
Stream stream = new MemoryStream(); |
|
||||
var img = Image.Load<Rgba32>(this.LocalConfiguration, stream); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
Assert.Equal(this.returnImage, img); |
|
||||
|
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.LocalConfiguration, stream)); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromStreamWithDecoder() |
|
||||
{ |
|
||||
Stream stream = new MemoryStream(); |
|
||||
var img = Image.Load<Rgba32>(stream, this.localDecoder.Object); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(Configuration.Default, stream)); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromStreamWithTypeAndDecoder() |
|
||||
{ |
|
||||
Stream stream = new MemoryStream(); |
|
||||
var img = Image.Load<Rgba32>(stream, this.localDecoder.Object); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
Assert.Equal(this.returnImage, img); |
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(Configuration.Default, stream)); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromBytes() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.DataStream.ToArray()); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
|
|
||||
TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromBytesWithType() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.DataStream.ToArray()); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
Assert.Equal(TestFormat.GlobalTestFormat.Sample<Rgba32>(), img); |
|
||||
|
|
||||
TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); |
|
||||
|
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromBytesWithConfig() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.LocalConfiguration, this.DataStream.ToArray()); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
|
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.LocalConfiguration, It.IsAny<Stream>())); |
|
||||
|
|
||||
Assert.Equal(this.DataStream.ToArray(), this.DecodedData); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromBytesWithTypeAndConfig() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.LocalConfiguration, this.DataStream.ToArray()); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
Assert.Equal(this.returnImage, img); |
|
||||
|
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.LocalConfiguration, It.IsAny<Stream>())); |
|
||||
|
|
||||
Assert.Equal(this.DataStream.ToArray(), this.DecodedData); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromBytesWithDecoder() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.DataStream.ToArray(), this.localDecoder.Object); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(Configuration.Default, It.IsAny<Stream>())); |
|
||||
Assert.Equal(this.DataStream.ToArray(), this.DecodedData); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromBytesWithTypeAndDecoder() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.DataStream.ToArray(), this.localDecoder.Object); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
Assert.Equal(this.returnImage, img); |
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(Configuration.Default, It.IsAny<Stream>())); |
|
||||
Assert.Equal(this.DataStream.ToArray(), this.DecodedData); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromFile() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.DataStream); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
|
|
||||
TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromFileWithType() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.DataStream); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
Assert.Equal(TestFormat.GlobalTestFormat.Sample<Rgba32>(), img); |
|
||||
|
|
||||
TestFormat.GlobalTestFormat.VerifyDecodeCall(this.Marker, Configuration.Default); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromFileWithConfig() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.LocalConfiguration, this.FilePath); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
|
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.LocalConfiguration, this.DataStream)); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromFileWithTypeAndConfig() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.LocalConfiguration, this.FilePath); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
Assert.Equal(this.returnImage, img); |
|
||||
|
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.LocalConfiguration, this.DataStream)); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromFileWithDecoder() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.FilePath, this.localDecoder.Object); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(Configuration.Default, this.DataStream)); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromFileWithTypeAndDecoder() |
|
||||
{ |
|
||||
var img = Image.Load<Rgba32>(this.FilePath, this.localDecoder.Object); |
|
||||
|
|
||||
Assert.NotNull(img); |
|
||||
Assert.Equal(this.returnImage, img); |
|
||||
this.localDecoder.Verify(x => x.Decode<Rgba32>(Configuration.Default, this.DataStream)); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void LoadFromPixelData_Pixels() |
|
||||
{ |
|
||||
var img = Image.LoadPixelData<Rgba32>(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<Rgba32>(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<Rgba32>.File(TestImages.Png.VersioningImage1); |
|
||||
|
|
||||
using (Image<Rgba32> img = image1Provider.GetImage()) |
|
||||
{ |
|
||||
Assert.Equal(166036, img.Frames.RootFrame.GetPixelSpan().Length); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public void Dispose() |
|
||||
{ |
|
||||
// clean up the global object;
|
|
||||
this.returnImage?.Dispose(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,83 @@ |
|||||
|
// 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 |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void BasicCase() |
||||
|
{ |
||||
|
var img = Image.Load<Rgba32>(this.TopLevelConfiguration, this.MockFilePath); |
||||
|
|
||||
|
Assert.NotNull(img); |
||||
|
|
||||
|
this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void UseLocalConfiguration() |
||||
|
{ |
||||
|
var img = Image.Load<Rgba32>(this.LocalConfiguration, this.MockFilePath); |
||||
|
|
||||
|
Assert.NotNull(img); |
||||
|
|
||||
|
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.LocalConfiguration, this.DataStream)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void UseCustomDecoder() |
||||
|
{ |
||||
|
var img = Image.Load<Rgba32>(this.TopLevelConfiguration, this.MockFilePath, this.localDecoder.Object); |
||||
|
|
||||
|
Assert.NotNull(img); |
||||
|
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.TopLevelConfiguration, this.DataStream)); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
[Fact] |
||||
|
public void UseGlobalConfigration() |
||||
|
{ |
||||
|
var file = TestFile.Create(TestImages.Bmp.Car); |
||||
|
using (var image = Image.Load<Rgba32>(file.FullPath)) |
||||
|
{ |
||||
|
Assert.Equal(600, image.Width); |
||||
|
Assert.Equal(450, image.Height); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void WhenFileNotFound_Throws() |
||||
|
{ |
||||
|
System.IO.FileNotFoundException ex = Assert.Throws<System.IO.FileNotFoundException>( |
||||
|
() => |
||||
|
{ |
||||
|
Image.Load<Rgba32>(Guid.NewGuid().ToString()); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void WhenPathIsNull_Throws() |
||||
|
{ |
||||
|
ArgumentNullException ex = Assert.Throws<ArgumentNullException>( |
||||
|
() => |
||||
|
{ |
||||
|
Image.Load<Rgba32>((string)null); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,120 @@ |
|||||
|
// 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
|
||||
|
namespace SixLabors.ImageSharp.Tests |
||||
|
{ |
||||
|
public partial class ImageTests |
||||
|
{ |
||||
|
public class Load_FromBytes : ImageLoadTestBase |
||||
|
{ |
||||
|
private byte[] ByteArray => this.DataStream.ToArray(); |
||||
|
|
||||
|
private ReadOnlySpan<byte> ByteSpan => this.ByteArray.AsSpan(); |
||||
|
|
||||
|
private byte[] ActualImageBytes => TestFile.Create(TestImages.Bmp.F).Bytes; |
||||
|
|
||||
|
private ReadOnlySpan<byte> ActualImageSpan => this.ActualImageBytes.AsSpan(); |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(false)] |
||||
|
[InlineData(true)] |
||||
|
public void BasicCase(bool useSpan) |
||||
|
{ |
||||
|
Image<Rgba32> img = useSpan |
||||
|
? Image.Load(this.TopLevelConfiguration, this.ByteSpan) |
||||
|
: Image.Load(this.TopLevelConfiguration, this.ByteArray); |
||||
|
|
||||
|
Assert.NotNull(img); |
||||
|
Assert.Equal(this.TestFormat.Sample<Rgba32>(), img); |
||||
|
|
||||
|
this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(false)] |
||||
|
[InlineData(true)] |
||||
|
public void NonDefaultPixelType(bool useSpan) |
||||
|
{ |
||||
|
Image<Rgb24> img = useSpan |
||||
|
? Image.Load<Rgb24>(this.TopLevelConfiguration, this.ByteSpan) |
||||
|
: Image.Load<Rgb24>(this.TopLevelConfiguration, this.ByteArray); |
||||
|
|
||||
|
Assert.NotNull(img); |
||||
|
Assert.Equal(this.TestFormat.Sample<Rgb24>(), img); |
||||
|
|
||||
|
this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(false)] |
||||
|
[InlineData(true)] |
||||
|
public void UseLocalConfiguration(bool useSpan) |
||||
|
{ |
||||
|
Image<Rgba32> img = useSpan |
||||
|
? Image.Load<Rgba32>(this.LocalConfiguration, this.ByteSpan) |
||||
|
: Image.Load<Rgba32>(this.LocalConfiguration, this.ByteArray); |
||||
|
|
||||
|
Assert.NotNull(img); |
||||
|
|
||||
|
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.LocalConfiguration, It.IsAny<Stream>())); |
||||
|
|
||||
|
Assert.Equal(this.DataStream.ToArray(), this.DecodedData); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(false)] |
||||
|
[InlineData(true)] |
||||
|
public void UseCustomDecoder(bool useSpan) |
||||
|
{ |
||||
|
Image<Rgba32> img = useSpan |
||||
|
? Image.Load<Rgba32>( |
||||
|
this.TopLevelConfiguration, |
||||
|
this.ByteSpan, |
||||
|
this.localDecoder.Object) |
||||
|
: Image.Load<Rgba32>( |
||||
|
this.TopLevelConfiguration, |
||||
|
this.ByteArray, |
||||
|
this.localDecoder.Object); |
||||
|
Assert.NotNull(img); |
||||
|
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.TopLevelConfiguration, It.IsAny<Stream>())); |
||||
|
Assert.Equal(this.DataStream.ToArray(), this.DecodedData); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData(false)] |
||||
|
[InlineData(true)] |
||||
|
public void UseGlobalConfiguration(bool useSpan) |
||||
|
{ |
||||
|
using (Image<Rgba32> 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) |
||||
|
{ |
||||
|
using (Image<Rgb24> img = useSpan |
||||
|
? Image.Load<Rgb24>(this.ActualImageSpan) |
||||
|
: Image.Load<Rgb24>(this.ActualImageBytes)) |
||||
|
{ |
||||
|
Assert.Equal(new Size(108, 202), img.Size()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,102 @@ |
|||||
|
// 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 |
||||
|
{ |
||||
|
using SixLabors.Primitives; |
||||
|
|
||||
|
public partial class ImageTests |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Tests the <see cref="Image"/> class.
|
||||
|
/// </summary>
|
||||
|
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<Rgba32>(), img); |
||||
|
|
||||
|
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() |
||||
|
{ |
||||
|
var img = Image.Load<Rgb24>(this.TopLevelConfiguration, this.DataStream); |
||||
|
|
||||
|
Assert.NotNull(img); |
||||
|
Assert.Equal(this.TestFormat.Sample<Rgb24>(), img); |
||||
|
|
||||
|
this.TestFormat.VerifyDecodeCall(this.Marker, this.TopLevelConfiguration); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void NonSeekableStream() |
||||
|
{ |
||||
|
var stream = new NoneSeekableStream(this.DataStream); |
||||
|
var img = Image.Load<Rgba32>(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<Rgba32>(this.LocalConfiguration, stream); |
||||
|
|
||||
|
Assert.NotNull(img); |
||||
|
|
||||
|
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.LocalConfiguration, stream)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void UseCustomDecoder() |
||||
|
{ |
||||
|
Stream stream = new MemoryStream(); |
||||
|
var img = Image.Load<Rgba32>(this.TopLevelConfiguration, stream, this.localDecoder.Object); |
||||
|
|
||||
|
Assert.NotNull(img); |
||||
|
this.localDecoder.Verify(x => x.Decode<Rgba32>(this.TopLevelConfiguration, stream)); |
||||
|
} |
||||
|
|
||||
|
// TODO: This should be a png decoder test!
|
||||
|
[Fact] |
||||
|
public void LoadsImageWithoutThrowingCrcException() |
||||
|
{ |
||||
|
var image1Provider = TestImageProvider<Rgba32>.File(TestImages.Png.VersioningImage1); |
||||
|
|
||||
|
using (Image<Rgba32> img = image1Provider.GetImage()) |
||||
|
{ |
||||
|
Assert.Equal(166036, img.Frames.RootFrame.GetPixelSpan().Length); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue