From f81008ef548d6d41cae122f320102152b5cda481 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 28 Oct 2021 23:58:44 +0200 Subject: [PATCH] ProcessPixelRows tests --- src/ImageSharp/PixelAccessor{TPixel}.cs | 5 ++ tests/ImageSharp.Tests/Image/ImageTests.cs | 81 ++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/ImageSharp/PixelAccessor{TPixel}.cs b/src/ImageSharp/PixelAccessor{TPixel}.cs index 0a7f408af..1f4dbf2eb 100644 --- a/src/ImageSharp/PixelAccessor{TPixel}.cs +++ b/src/ImageSharp/PixelAccessor{TPixel}.cs @@ -45,6 +45,11 @@ namespace SixLabors.ImageSharp public ref struct PixelAccessor where TPixel : unmanaged, IPixel { + /// + /// Gets the width of the backing . + /// + public int Width { get; } + /// /// Gets the height of the backing . /// diff --git a/tests/ImageSharp.Tests/Image/ImageTests.cs b/tests/ImageSharp.Tests/Image/ImageTests.cs index 671fc2b90..709cecc97 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.cs +++ b/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(123, 456); + image.ProcessPixelRows(accessor => + { + Assert.Equal(123, accessor.Width); + Assert.Equal(456, accessor.Height); + }); + } + + [Fact] + public void WritesImagePixels() + { + using var image = new Image(256, 256); + image.ProcessPixelRows(accessor => + { + for (int y = 0; y < accessor.Height; y++) + { + Span row = accessor.GetRowSpan(y); + for (int x = 0; x < row.Length; x++) + { + row[x] = new L16((ushort)(x * y)); + } + } + }); + + Buffer2D buffer = image.Frames.RootFrame.PixelBuffer; + for (int y = 0; y < 256; y++) + { + Span 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(256, 256); + Buffer2D buffer = img1.Frames.RootFrame.PixelBuffer; + for (int y = 0; y < 256; y++) + { + Span row = buffer.GetRowSpan(y); + for (int x = 0; x < 256; x++) + { + row[x] = new L16((ushort)(x * y)); + } + } + + using var img2 = new Image(256, 256); + + img1.ProcessPixelRows(img2, (accessor1, accessor2) => + { + for (int y = 0; y < accessor1.Height; y++) + { + Span row1 = accessor1.GetRowSpan(y); + Span row2 = accessor2.GetRowSpan(accessor2.Height - y - 1); + row1.CopyTo(row2); + } + }); + + buffer = img2.Frames.RootFrame.PixelBuffer; + for (int y = 0; y < 256; y++) + { + Span 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();