Browse Source

ProcessPixelRows tests

af/UniformUnmanagedMemoryPoolMemoryAllocator-02-MemoryGuards
Anton Firszov 4 years ago
parent
commit
f81008ef54
  1. 5
      src/ImageSharp/PixelAccessor{TPixel}.cs
  2. 81
      tests/ImageSharp.Tests/Image/ImageTests.cs

5
src/ImageSharp/PixelAccessor{TPixel}.cs

@ -45,6 +45,11 @@ namespace SixLabors.ImageSharp
public ref struct PixelAccessor<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
/// <summary>
/// Gets the width of the backing <see cref="Image{TPixel}"/>.
/// </summary>
public int Width { get; }
/// <summary>
/// Gets the height of the backing <see cref="Image{TPixel}"/>.
/// </summary>

81
tests/ImageSharp.Tests/Image/ImageTests.cs

@ -176,6 +176,87 @@ namespace SixLabors.ImageSharp.Tests
}
}
public class ProcessPixelRows
{
[Fact]
public void PixelAccessorDimensionsAreCorrect()
{
using var image = new Image<Rgb24>(123, 456);
image.ProcessPixelRows(accessor =>
{
Assert.Equal(123, accessor.Width);
Assert.Equal(456, accessor.Height);
});
}
[Fact]
public void WritesImagePixels()
{
using var image = new Image<L16>(256, 256);
image.ProcessPixelRows(accessor =>
{
for (int y = 0; y < accessor.Height; y++)
{
Span<L16> row = accessor.GetRowSpan(y);
for (int x = 0; x < row.Length; x++)
{
row[x] = new L16((ushort)(x * y));
}
}
});
Buffer2D<L16> buffer = image.Frames.RootFrame.PixelBuffer;
for (int y = 0; y < 256; y++)
{
Span<L16> row = buffer.GetRowSpan(y);
for (int x = 0; x < 256; x++)
{
int actual = row[x].PackedValue;
Assert.Equal(x * y, actual);
}
}
}
[Fact]
public void CopyImagePixels()
{
using var img1 = new Image<L16>(256, 256);
Buffer2D<L16> buffer = img1.Frames.RootFrame.PixelBuffer;
for (int y = 0; y < 256; y++)
{
Span<L16> row = buffer.GetRowSpan(y);
for (int x = 0; x < 256; x++)
{
row[x] = new L16((ushort)(x * y));
}
}
using var img2 = new Image<L16>(256, 256);
img1.ProcessPixelRows(img2, (accessor1, accessor2) =>
{
for (int y = 0; y < accessor1.Height; y++)
{
Span<L16> row1 = accessor1.GetRowSpan(y);
Span<L16> row2 = accessor2.GetRowSpan(accessor2.Height - y - 1);
row1.CopyTo(row2);
}
});
buffer = img2.Frames.RootFrame.PixelBuffer;
for (int y = 0; y < 256; y++)
{
Span<L16> row = buffer.GetRowSpan(y);
for (int x = 0; x < 256; x++)
{
int actual = row[x].PackedValue;
Assert.Equal(x * (256 - y - 1), actual);
}
}
}
}
public class Dispose
{
private readonly Configuration configuration = Configuration.CreateDefaultInstance();

Loading…
Cancel
Save