Browse Source

Address code review comments.

pull/1610/head
Petar Tasev 5 years ago
parent
commit
0871b85bdf
  1. 2
      src/ImageSharp/Image.WrapMemory.cs
  2. 1
      src/ImageSharp/Memory/ByteMemoryOwner{T}.cs
  3. 29
      tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs

2
src/ImageSharp/Image.WrapMemory.cs

@ -332,7 +332,7 @@ namespace SixLabors.ImageSharp
var pixelMemoryOwner = new ByteMemoryOwner<TPixel>(byteMemoryOwner);
Guard.IsTrue(pixelMemoryOwner.Memory.Length >= width * height, nameof(pixelMemoryOwner), "The length of the input memory is less than the specified image size");
Guard.IsTrue(pixelMemoryOwner.Memory.Length >= (long)width * height, nameof(pixelMemoryOwner), "The length of the input memory is less than the specified image size");
var memorySource = MemoryGroup<TPixel>.Wrap(pixelMemoryOwner);
return new Image<TPixel>(configuration, memorySource, width, height, metadata);

1
src/ImageSharp/Memory/ByteMemoryOwner{T}.cs

@ -49,7 +49,6 @@ namespace SixLabors.ImageSharp.Memory
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
this.Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}

29
tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs

@ -380,11 +380,11 @@ namespace SixLabors.ImageSharp.Tests
private class TestMemoryOwner<T> : IMemoryOwner<T>
{
public bool Disposed { get; private set; }
public Memory<T> Memory { get; set; }
public void Dispose()
{
}
public void Dispose() => this.Disposed = true;
}
[Theory]
@ -433,10 +433,29 @@ namespace SixLabors.ImageSharp.Tests
[InlineData(2048, 32, 32)]
public void WrapMemory_IMemoryOwnerOfByte_ValidSize(int size, int height, int width)
{
var array = new byte[size * Unsafe.SizeOf<Rgba32>()];
var random = new Random();
var pixelSize = Unsafe.SizeOf<Rgba32>();
var array = new byte[size * pixelSize];
var memory = new TestMemoryOwner<byte> { Memory = array };
Image.WrapMemory<Rgba32>(memory, height, width);
using (var img = Image.WrapMemory<Rgba32>(memory, width, height))
{
Assert.Equal(width, img.Width);
Assert.Equal(height, img.Height);
for (int i = 0; i < height; ++i)
{
var arrayIndex = pixelSize * width * i;
var expected = (byte)random.Next(0, 256);
Span<Rgba32> rowSpan = img.GetPixelRowSpan(i);
array[arrayIndex] = expected;
Assert.Equal(expected, rowSpan[0].R);
}
}
Assert.True(memory.Disposed);
}
[Theory]

Loading…
Cancel
Save