diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 54ac9dfd83..a8378e0969 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -208,7 +208,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// Compresssed RLE8 stream is uncompressed by /// /// The pixel format. - /// The to assign the palette to. + /// The to assign the palette to. /// The containing the colors. /// The width of the bitmap. /// The height of the bitmap. @@ -319,7 +319,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// Reads the color palette from the stream. /// /// The pixel format. - /// The to assign the palette to. + /// The to assign the palette to. /// The containing the colors. /// The width of the bitmap. /// The height of the bitmap. @@ -383,7 +383,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// Reads the 16 bit color palette from the stream /// /// The pixel format. - /// The to assign the palette to. + /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. @@ -424,7 +424,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// Reads the 24 bit color palette from the stream /// /// The pixel format. - /// The to assign the palette to. + /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. @@ -449,7 +449,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// Reads the 32 bit color palette from the stream /// /// The pixel format. - /// The to assign the palette to. + /// The to assign the palette to. /// The width of the bitmap. /// The height of the bitmap. /// Whether the bitmap is inverted. diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 80ca2b2254..1864737c83 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -115,7 +115,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// /// The pixel format. /// The to write to. - /// The containing pixel data. + /// The containing pixel data. private void Write32Bit(Stream stream, Buffer2D pixels) where TPixel : struct, IPixel { @@ -135,7 +135,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// /// The pixel format. /// The to write to. - /// The containing pixel data. + /// The containing pixel data. private void Write24Bit(Stream stream, Buffer2D pixels) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/ImageExtensions.Internal.cs b/src/ImageSharp/ImageExtensions.Internal.cs new file mode 100644 index 0000000000..6bbc6ec52f --- /dev/null +++ b/src/ImageSharp/ImageExtensions.Internal.cs @@ -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 +{ + /// + /// Contains internal extensions for + /// + public static partial class ImageExtensions + { + /// + /// Locks the image providing access to the pixels. + /// + /// It is imperative that the accessor is correctly disposed off after use. + /// + /// + /// The type of the pixel. + /// The image. + /// + /// The + /// + internal static Buffer2D GetRootFramePixelBuffer(this Image image) + where TPixel : struct, IPixel + { + return image.Frames.RootFrame.PixelBuffer; + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/ImageFrame{TPixel}.cs b/src/ImageSharp/ImageFrame{TPixel}.cs index 4fb09f0a96..7fd60949b7 100644 --- a/src/ImageSharp/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/ImageFrame{TPixel}.cs @@ -216,19 +216,10 @@ namespace SixLabors.ImageSharp /// It is imperative that the accessor is correctly disposed off after use. /// /// - /// The - internal PixelAccessor Lock() + /// The + internal Buffer2D Lock() { - return new PixelAccessor(this); - } - - /// - /// Copies the pixels to a of the same size. - /// - /// The target pixel buffer accessor. - internal void CopyTo(PixelAccessor target) - { - this.CopyTo(target.PixelBuffer); + return this.PixelBuffer; } /// diff --git a/src/ImageSharp/PixelAccessorExtensions.cs b/src/ImageSharp/PixelAccessorExtensions.cs deleted file mode 100644 index b628c05f8b..0000000000 --- a/src/ImageSharp/PixelAccessorExtensions.cs +++ /dev/null @@ -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 -{ - /// - /// Helper methods fro acccess pixel accessors - /// - internal static class PixelAccessorExtensions - { - /// - /// Locks the image providing access to the pixels. - /// - /// It is imperative that the accessor is correctly disposed off after use. - /// - /// - /// The type of the pixel. - /// The frame. - /// - /// The - /// - internal static PixelAccessor Lock(this IPixelSource frame) - where TPixel : struct, IPixel - { - return new PixelAccessor(frame); - } - - /// - /// Locks the image providing access to the pixels. - /// - /// It is imperative that the accessor is correctly disposed off after use. - /// - /// - /// The type of the pixel. - /// The image. - /// - /// The - /// - internal static PixelAccessor Lock(this Image image) - where TPixel : struct, IPixel - { - return image.Frames.RootFrame.Lock(); - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/PixelAccessor{TPixel}.cs b/src/ImageSharp/PixelAccessor{TPixel}.cs index 7ca066ff15..f4b79715be 100644 --- a/src/ImageSharp/PixelAccessor{TPixel}.cs +++ b/src/ImageSharp/PixelAccessor{TPixel}.cs @@ -103,15 +103,6 @@ namespace SixLabors.ImageSharp return oldPixels; } - /// - /// Copies the pixels to another of the same size. - /// - /// The target pixel buffer accessor. - internal void CopyTo(PixelAccessor target) - { - this.PixelBuffer.GetSpan().CopyTo(target.PixelBuffer.GetSpan()); - } - /// /// Sets the pixel buffer in an unsafe manor this should not be used unless you know what its doing!!! /// diff --git a/tests/ImageSharp.Benchmarks/Codecs/CopyPixels.cs b/tests/ImageSharp.Benchmarks/Codecs/CopyPixels.cs index 610c356b7a..8bf87fb628 100644 --- a/tests/ImageSharp.Benchmarks/Codecs/CopyPixels.cs +++ b/tests/ImageSharp.Benchmarks/Codecs/CopyPixels.cs @@ -22,23 +22,21 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs using (var source = new Image(1024, 768)) using (var target = new Image(1024, 768)) { - using (PixelAccessor sourcePixels = source.Lock()) - using (PixelAccessor targetPixels = target.Lock()) - { - Parallel.For( - 0, - source.Height, - Configuration.Default.ParallelOptions, - y => + Buffer2D sourcePixels = source.GetRootFramePixelBuffer(); + Buffer2D targetPixels = target.GetRootFramePixelBuffer(); + Parallel.For( + 0, + source.Height, + Configuration.Default.ParallelOptions, + y => + { + for (int x = 0; x < source.Width; x++) { - for (int x = 0; x < source.Width; x++) - { - targetPixels[x, y] = sourcePixels[x, y]; - } - }); + targetPixels[x, y] = sourcePixels[x, y]; + } + }); - return targetPixels[0, 0]; - } + return targetPixels[0, 0]; } } @@ -48,14 +46,13 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs using (var source = new Image(1024, 768)) using (var target = new Image(1024, 768)) { - using (PixelAccessor sourcePixels = source.Lock()) - using (PixelAccessor targetPixels = target.Lock()) - { - Parallel.For( - 0, - source.Height, - Configuration.Default.ParallelOptions, - y => + Buffer2D sourcePixels = source.GetRootFramePixelBuffer(); + Buffer2D targetPixels = target.GetRootFramePixelBuffer(); + Parallel.For( + 0, + source.Height, + Configuration.Default.ParallelOptions, + y => { Span sourceRow = sourcePixels.GetRowSpan(y); Span targetRow = targetPixels.GetRowSpan(y); @@ -66,8 +63,7 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs } }); - return targetPixels[0, 0]; - } + return targetPixels[0, 0]; } } diff --git a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs index fe0678aca4..5fe8b2785d 100644 --- a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs +++ b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs @@ -57,20 +57,19 @@ namespace SixLabors.ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp BulkVectorConvert")] public CoreSize BulkVectorConvert() { - using (Image image = new Image(800, 800)) + using (var image = new Image(800, 800)) { using (IBuffer amounts = Configuration.Default.MemoryAllocator.Allocate(image.Width)) { amounts.GetSpan().Fill(1); - - using (PixelAccessor pixels = image.Lock()) + + Buffer2D pixels = image.GetRootFramePixelBuffer(); + for (int y = 0; y < image.Height; y++) { - for (int y = 0; y < image.Height; y++) - { - Span span = pixels.GetRowSpan(y); - this.BulkVectorConvert(span, span, span, amounts.GetSpan()); - } + Span span = pixels.GetRowSpan(y); + this.BulkVectorConvert(span, span, span, amounts.GetSpan()); } + return new CoreSize(image.Width, image.Height); } } @@ -79,18 +78,16 @@ namespace SixLabors.ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp BulkPixelConvert")] public CoreSize BulkPixelConvert() { - using (Image image = new Image(800, 800)) + using (var image = new Image(800, 800)) { using (IBuffer amounts = Configuration.Default.MemoryAllocator.Allocate(image.Width)) { amounts.GetSpan().Fill(1); - using (PixelAccessor pixels = image.Lock()) + Buffer2D pixels = image.GetRootFramePixelBuffer(); + for (int y = 0; y < image.Height; y++) { - for (int y = 0; y < image.Height; y++) - { - Span span = pixels.GetRowSpan(y); - this.BulkPixelConvert(span, span, span, amounts.GetSpan()); - } + Span span = pixels.GetRowSpan(y); + this.BulkPixelConvert(span, span, span, amounts.GetSpan()); } return new CoreSize(image.Width, image.Height); diff --git a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs index b2fa47893b..edfc2f8787 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs @@ -103,7 +103,7 @@ namespace SixLabors.ImageSharp.Benchmarks int width = maxX - minX; using (IBuffer rowColors = Configuration.Default.MemoryAllocator.Allocate(width)) - using (PixelAccessor sourcePixels = source.Lock()) + using (Buffer2D sourcePixels = source.Lock()) { rowColors.GetSpan().Fill(glowColor); diff --git a/tests/ImageSharp.Tests/Drawing/BeziersTests.cs b/tests/ImageSharp.Tests/Drawing/BeziersTests.cs index daa640a0b0..6dc2fae894 100644 --- a/tests/ImageSharp.Tests/Drawing/BeziersTests.cs +++ b/tests/ImageSharp.Tests/Drawing/BeziersTests.cs @@ -11,6 +11,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Drawing { + using SixLabors.Memory; + public class Beziers : FileTestBase { [Fact] @@ -19,32 +21,30 @@ namespace SixLabors.ImageSharp.Tests.Drawing string path = TestEnvironment.CreateOutputDirectory("Drawing", "BezierLine"); using (var image = new Image(500, 500)) { - image.Mutate(x => x.BackgroundColor(Rgba32.Blue) - .DrawBeziers(Rgba32.HotPink, 5, - new SixLabors.Primitives.PointF[] { - new Vector2(10, 400), - new Vector2(30, 10), - new Vector2(240, 30), - new Vector2(300, 400) - })); + image.Mutate( + x => x.BackgroundColor(Rgba32.Blue).DrawBeziers( + Rgba32.HotPink, + 5, + new SixLabors.Primitives.PointF[] + { + new Vector2(10, 400), new Vector2(30, 10), new Vector2(240, 30), new Vector2(300, 400) + })); image.Save($"{path}/Simple.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - //top of curve - Assert.Equal(Rgba32.HotPink, sourcePixels[138, 115]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + //top of curve + Assert.Equal(Rgba32.HotPink, sourcePixels[138, 115]); - //start points - Assert.Equal(Rgba32.HotPink, sourcePixels[10, 395]); - Assert.Equal(Rgba32.HotPink, sourcePixels[300, 395]); + //start points + Assert.Equal(Rgba32.HotPink, sourcePixels[10, 395]); + Assert.Equal(Rgba32.HotPink, sourcePixels[300, 395]); - //curve points should not be never be set - Assert.Equal(Rgba32.Blue, sourcePixels[30, 10]); - Assert.Equal(Rgba32.Blue, sourcePixels[240, 30]); + //curve points should not be never be set + Assert.Equal(Rgba32.Blue, sourcePixels[30, 10]); + Assert.Equal(Rgba32.Blue, sourcePixels[240, 30]); - // inside shape should be empty - Assert.Equal(Rgba32.Blue, sourcePixels[200, 250]); - } + // inside shape should be empty + Assert.Equal(Rgba32.Blue, sourcePixels[200, 250]); } } @@ -58,36 +58,34 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x.BackgroundColor(Rgba32.Blue) - .DrawBeziers(color, - 10, - new SixLabors.Primitives.PointF[]{ - new Vector2(10, 400), - new Vector2(30, 10), - new Vector2(240, 30), - new Vector2(300, 400) - })); + image.Mutate( + x => x.BackgroundColor(Rgba32.Blue).DrawBeziers( + color, + 10, + new SixLabors.Primitives.PointF[] + { + new Vector2(10, 400), new Vector2(30, 10), new Vector2(240, 30), new Vector2(300, 400) + })); image.Save($"{path}/Opacity.png"); //shift background color towards foreground color by the opacity amount - var mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); + var mergedColor = new Rgba32( + Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); - using (PixelAccessor sourcePixels = image.Lock()) - { - // top of curve - Assert.Equal(mergedColor, sourcePixels[138, 115]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + // top of curve + Assert.Equal(mergedColor, sourcePixels[138, 115]); - // start points - Assert.Equal(mergedColor, sourcePixels[10, 395]); - Assert.Equal(mergedColor, sourcePixels[300, 395]); + // start points + Assert.Equal(mergedColor, sourcePixels[10, 395]); + Assert.Equal(mergedColor, sourcePixels[300, 395]); - // curve points should not be never be set - Assert.Equal(Rgba32.Blue, sourcePixels[30, 10]); - Assert.Equal(Rgba32.Blue, sourcePixels[240, 30]); + // curve points should not be never be set + Assert.Equal(Rgba32.Blue, sourcePixels[30, 10]); + Assert.Equal(Rgba32.Blue, sourcePixels[240, 30]); - // inside shape should be empty - Assert.Equal(Rgba32.Blue, sourcePixels[200, 250]); - } + // inside shape should be empty + Assert.Equal(Rgba32.Blue, sourcePixels[200, 250]); } } } diff --git a/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs b/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs index 7e75f52b20..c1865e9307 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs +++ b/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs @@ -12,6 +12,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Drawing { + using SixLabors.Memory; + public class DrawPathTests : FileTestBase { [Fact] @@ -24,26 +26,23 @@ namespace SixLabors.ImageSharp.Tests.Drawing new Vector2(10, 10), new Vector2(200, 150), new Vector2(50, 300)); - var bazierSegment = new CubicBezierLineSegment(new Vector2(50, 300), + var bazierSegment = new CubicBezierLineSegment( + new Vector2(50, 300), new Vector2(500, 500), new Vector2(60, 10), new Vector2(10, 400)); var p = new Path(linerSegemnt, bazierSegment); - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Draw(Rgba32.HotPink, 5, p)); + image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, p)); image.Save($"{path}/Simple.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]); - Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); + Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); - Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); - } + Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); } } @@ -71,22 +70,19 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Draw(color, 10, p)); + image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(color, 10, p)); image.Save($"{path}/Opacity.png"); //shift background color towards forground color by the opacity amount - var mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); + var mergedColor = new Rgba32( + Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(mergedColor, sourcePixels[11, 11]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(mergedColor, sourcePixels[11, 11]); - Assert.Equal(mergedColor, sourcePixels[199, 149]); + Assert.Equal(mergedColor, sourcePixels[199, 149]); - Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); - } + Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); } } @@ -102,15 +98,15 @@ namespace SixLabors.ImageSharp.Tests.Drawing for (int i = 0; i < 300; i += 20) { - image.Mutate(x => x.DrawLines(pen, new SixLabors.Primitives.PointF[] { new Vector2(100, 2), new Vector2(-10, i) })); + image.Mutate( + x => x.DrawLines( + pen, + new SixLabors.Primitives.PointF[] { new Vector2(100, 2), new Vector2(-10, i) })); } - image - .Save($"{path}/ClippedLines.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.White, sourcePixels[0, 90]); - } + image.Save($"{path}/ClippedLines.png"); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.White, sourcePixels[0, 90]); } } } diff --git a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs index 5b47e78351..29b78220d0 100644 --- a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs +++ b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs @@ -12,6 +12,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Drawing { + using SixLabors.Memory; + public class FillPatternBrushTests : FileTestBase { private void Test(string name, Rgba32 background, IBrush brush, Rgba32[,] expectedPattern) @@ -19,36 +21,33 @@ namespace SixLabors.ImageSharp.Tests.Drawing string path = TestEnvironment.CreateOutputDirectory("Fill", "PatternBrush"); using (var image = new Image(20, 20)) { - image.Mutate(x => x - .Fill(background) - .Fill(brush)); + image.Mutate(x => x.Fill(background).Fill(brush)); image.Save($"{path}/{name}.png"); - using (PixelAccessor sourcePixels = image.Lock()) + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + // lets pick random spots to start checking + var r = new Random(); + var expectedPatternFast = new DenseMatrix(expectedPattern); + int xStride = expectedPatternFast.Columns; + int yStride = expectedPatternFast.Rows; + int offsetX = r.Next(image.Width / xStride) * xStride; + int offsetY = r.Next(image.Height / yStride) * yStride; + for (int x = 0; x < xStride; x++) { - // lets pick random spots to start checking - var r = new Random(); - var expectedPatternFast = new DenseMatrix(expectedPattern); - int xStride = expectedPatternFast.Columns; - int yStride = expectedPatternFast.Rows; - int offsetX = r.Next(image.Width / xStride) * xStride; - int offsetY = r.Next(image.Height / yStride) * yStride; - for (int x = 0; x < xStride; x++) + for (int y = 0; y < yStride; y++) { - for (int y = 0; y < yStride; y++) + int actualX = x + offsetX; + int actualY = y + offsetY; + Rgba32 expected = expectedPatternFast[y, x]; // inverted pattern + Rgba32 actual = sourcePixels[actualX, actualY]; + if (expected != actual) { - int actualX = x + offsetX; - int actualY = y + offsetY; - Rgba32 expected = expectedPatternFast[y, x]; // inverted pattern - Rgba32 actual = sourcePixels[actualX, actualY]; - if (expected != actual) - { - Assert.True(false, $"Expected {expected} but found {actual} at ({actualX},{actualY})"); - } + Assert.True(false, $"Expected {expected} but found {actual} at ({actualX},{actualY})"); } } } + image.Mutate(x => x.Resize(80, 80)); image.Save($"{path}/{name}x4.png"); } diff --git a/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs index 09ed469083..ecd1c06e8f 100644 --- a/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs @@ -13,6 +13,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Drawing { + using SixLabors.Memory; + public class LineComplexPolygonTests : FileTestBase { [Fact] @@ -32,34 +34,29 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1))); + image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1))); image.Save($"{path}/Simple.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[10, 10]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[10, 10]); - Assert.Equal(Rgba32.HotPink, sourcePixels[200, 150]); + Assert.Equal(Rgba32.HotPink, sourcePixels[200, 150]); - Assert.Equal(Rgba32.HotPink, sourcePixels[50, 300]); + Assert.Equal(Rgba32.HotPink, sourcePixels[50, 300]); + Assert.Equal(Rgba32.HotPink, sourcePixels[37, 85]); - Assert.Equal(Rgba32.HotPink, sourcePixels[37, 85]); + Assert.Equal(Rgba32.HotPink, sourcePixels[93, 85]); - Assert.Equal(Rgba32.HotPink, sourcePixels[93, 85]); + Assert.Equal(Rgba32.HotPink, sourcePixels[65, 137]); - Assert.Equal(Rgba32.HotPink, sourcePixels[65, 137]); + Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); - Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); + //inside hole + Assert.Equal(Rgba32.Blue, sourcePixels[57, 99]); - //inside hole - Assert.Equal(Rgba32.Blue, sourcePixels[57, 99]); - - //inside shape - Assert.Equal(Rgba32.Blue, sourcePixels[100, 192]); - } + //inside shape + Assert.Equal(Rgba32.Blue, sourcePixels[100, 192]); } } @@ -79,32 +76,27 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1))); + image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1))); image.Save($"{path}/SimpleVanishHole.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[10, 10]); - Assert.Equal(Rgba32.HotPink, sourcePixels[200, 150]); - Assert.Equal(Rgba32.HotPink, sourcePixels[50, 300]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[10, 10]); + Assert.Equal(Rgba32.HotPink, sourcePixels[200, 150]); + Assert.Equal(Rgba32.HotPink, sourcePixels[50, 300]); + //Assert.Equal(Color.HotPink, sourcePixels[37, 85]); - //Assert.Equal(Color.HotPink, sourcePixels[37, 85]); + //Assert.Equal(Color.HotPink, sourcePixels[93, 85]); - //Assert.Equal(Color.HotPink, sourcePixels[93, 85]); + //Assert.Equal(Color.HotPink, sourcePixels[65, 137]); - //Assert.Equal(Color.HotPink, sourcePixels[65, 137]); + Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); - Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); + //inside hole + Assert.Equal(Rgba32.Blue, sourcePixels[57, 99]); - //inside hole - Assert.Equal(Rgba32.Blue, sourcePixels[57, 99]); - - //inside shape - Assert.Equal(Rgba32.Blue, sourcePixels[100, 192]); - } + //inside shape + Assert.Equal(Rgba32.Blue, sourcePixels[100, 192]); } } @@ -124,25 +116,21 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1))); + image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 5, simplePath.Clip(hole1))); image.Save($"{path}/SimpleOverlapping.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[10, 10]); - Assert.Equal(Rgba32.HotPink, sourcePixels[200, 150]); - Assert.Equal(Rgba32.HotPink, sourcePixels[50, 300]); - Assert.Equal(Rgba32.Blue, sourcePixels[130, 41]); - Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[10, 10]); + Assert.Equal(Rgba32.HotPink, sourcePixels[200, 150]); + Assert.Equal(Rgba32.HotPink, sourcePixels[50, 300]); + Assert.Equal(Rgba32.Blue, sourcePixels[130, 41]); + Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); - //inside hole - Assert.Equal(Rgba32.Blue, sourcePixels[57, 99]); + //inside hole + Assert.Equal(Rgba32.Blue, sourcePixels[57, 99]); - //inside shape - Assert.Equal(Rgba32.Blue, sourcePixels[100, 192]); - } + //inside shape + Assert.Equal(Rgba32.Blue, sourcePixels[100, 192]); } } @@ -186,30 +174,27 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Draw(color, 5, simplePath.Clip(hole1))); + image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Draw(color, 5, simplePath.Clip(hole1))); image.Save($"{path}/Opacity.png"); //shift background color towards forground color by the opacity amount - var mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); - - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(mergedColor, sourcePixels[10, 10]); - Assert.Equal(mergedColor, sourcePixels[200, 150]); - Assert.Equal(mergedColor, sourcePixels[50, 300]); - Assert.Equal(mergedColor, sourcePixels[37, 85]); - Assert.Equal(mergedColor, sourcePixels[93, 85]); - Assert.Equal(mergedColor, sourcePixels[65, 137]); - Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); - - //inside hole - Assert.Equal(Rgba32.Blue, sourcePixels[57, 99]); - - //inside shape - Assert.Equal(Rgba32.Blue, sourcePixels[100, 192]); - } + var mergedColor = new Rgba32( + Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); + + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(mergedColor, sourcePixels[10, 10]); + Assert.Equal(mergedColor, sourcePixels[200, 150]); + Assert.Equal(mergedColor, sourcePixels[50, 300]); + Assert.Equal(mergedColor, sourcePixels[37, 85]); + Assert.Equal(mergedColor, sourcePixels[93, 85]); + Assert.Equal(mergedColor, sourcePixels[65, 137]); + Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); + + //inside hole + Assert.Equal(Rgba32.Blue, sourcePixels[57, 99]); + + //inside shape + Assert.Equal(Rgba32.Blue, sourcePixels[100, 192]); } } } diff --git a/tests/ImageSharp.Tests/Drawing/LineTests.cs b/tests/ImageSharp.Tests/Drawing/LineTests.cs index 6128756c5c..28b59746f6 100644 --- a/tests/ImageSharp.Tests/Drawing/LineTests.cs +++ b/tests/ImageSharp.Tests/Drawing/LineTests.cs @@ -13,6 +13,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Drawing { + using SixLabors.Memory; + public class LineTests : FileTestBase { [Fact] @@ -21,24 +23,21 @@ namespace SixLabors.ImageSharp.Tests.Drawing string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines"); using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .DrawLines(Rgba32.HotPink, 5, - new SixLabors.Primitives.PointF[]{ - new Vector2(10, 10), - new Vector2(200, 150), - new Vector2(50, 300) - })); + image.Mutate( + x => x.BackgroundColor(Rgba32.Blue).DrawLines( + Rgba32.HotPink, + 5, + new Vector2(10, 10), + new Vector2(200, 150), + new Vector2(50, 300))); image.Save($"{path}/Simple.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]); - Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); + Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); - Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); - } + Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); } } @@ -48,27 +47,22 @@ namespace SixLabors.ImageSharp.Tests.Drawing string path = TestEnvironment.CreateOutputDirectory("Drawing", "Lines"); using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .DrawLines( + image.Mutate( + x => x.BackgroundColor(Rgba32.Blue).DrawLines( new GraphicsOptions(false), Rgba32.HotPink, 5, - new SixLabors.Primitives.PointF[] { - new Vector2(10, 10), - new Vector2(200, 150), - new Vector2(50, 300) - })); + new Vector2(10, 10), + new Vector2(200, 150), + new Vector2(50, 300))); image.Save($"{path}/Simple_noantialias.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]); - Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); + Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); - Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); - } + Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); } } @@ -151,26 +145,25 @@ namespace SixLabors.ImageSharp.Tests.Drawing var image = new Image(500, 500); - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .DrawLines(color, 10, new SixLabors.Primitives.PointF[] { - new Vector2(10, 10), - new Vector2(200, 150), - new Vector2(50, 300) - })); + image.Mutate( + x => x.BackgroundColor(Rgba32.Blue).DrawLines( + color, + 10, + new Vector2(10, 10), + new Vector2(200, 150), + new Vector2(50, 300))); image.Save($"{path}/Opacity.png"); //shift background color towards forground color by the opacity amount - var mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); + var mergedColor = + new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(mergedColor, sourcePixels[11, 11]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(mergedColor, sourcePixels[11, 11]); - Assert.Equal(mergedColor, sourcePixels[199, 149]); + Assert.Equal(mergedColor, sourcePixels[199, 149]); - Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); - } + Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); } [Fact] @@ -180,27 +173,24 @@ namespace SixLabors.ImageSharp.Tests.Drawing var image = new Image(500, 500); - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .DrawLines(Rgba32.HotPink, 10, new SixLabors.Primitives.PointF[] { - new Vector2(10, 10), - new Vector2(200, 10), - new Vector2(200, 150), - new Vector2(10, 150) - })); + image.Mutate( + x => x.BackgroundColor(Rgba32.Blue).DrawLines( + Rgba32.HotPink, + 10, + new Vector2(10, 10), + new Vector2(200, 10), + new Vector2(200, 150), + new Vector2(10, 150))); image.Save($"{path}/Rectangle.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]); - Assert.Equal(Rgba32.HotPink, sourcePixels[198, 10]); + Assert.Equal(Rgba32.HotPink, sourcePixels[198, 10]); - Assert.Equal(Rgba32.Blue, sourcePixels[10, 50]); + Assert.Equal(Rgba32.Blue, sourcePixels[10, 50]); - Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); - } + Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); } - } } diff --git a/tests/ImageSharp.Tests/Drawing/PolygonTests.cs b/tests/ImageSharp.Tests/Drawing/PolygonTests.cs index f6d9bf2131..c7a0531c92 100644 --- a/tests/ImageSharp.Tests/Drawing/PolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/PolygonTests.cs @@ -12,6 +12,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Drawing { + using SixLabors.Memory; + public class PolygonTests : FileTestBase { [Fact] @@ -21,26 +23,23 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (Image image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .DrawPolygon(Rgba32.HotPink, 5, - new SixLabors.Primitives.PointF[] { - new Vector2(10, 10), - new Vector2(200, 150), - new Vector2(50, 300) - })); + image.Mutate( + x => x.BackgroundColor(Rgba32.Blue).DrawPolygon( + Rgba32.HotPink, + 5, + new Vector2(10, 10), + new Vector2(200, 150), + new Vector2(50, 300))); image.Save($"{path}/Simple.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[9, 9]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[9, 9]); - Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); + Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); - Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); + Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); - Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); - } + Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); } } @@ -48,7 +47,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing public void ImageShouldBeOverlayedPolygonOutlineWithOpacity() { string path = TestEnvironment.CreateOutputDirectory("Drawing", "Polygons"); - SixLabors.Primitives.PointF[] simplePath = new SixLabors.Primitives.PointF[] { + PointF[] simplePath = { new Vector2(10, 10), new Vector2(200, 150), new Vector2(50, 300) @@ -58,24 +57,21 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (Image image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .DrawPolygon(color, 10, simplePath)); + image.Mutate(x => x.BackgroundColor(Rgba32.Blue).DrawPolygon(color, 10, simplePath)); image.Save($"{path}/Opacity.png"); //shift background color towards forground color by the opacity amount - Rgba32 mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); + Rgba32 mergedColor = new Rgba32( + Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(mergedColor, sourcePixels[9, 9]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(mergedColor, sourcePixels[9, 9]); - Assert.Equal(mergedColor, sourcePixels[199, 149]); + Assert.Equal(mergedColor, sourcePixels[199, 149]); - Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); + Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); - Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); - } + Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); } } @@ -86,23 +82,20 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (Image image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Draw(Rgba32.HotPink, 10, new Rectangle(10, 10, 190, 140))); + image.Mutate( + x => x.BackgroundColor(Rgba32.Blue).Draw(Rgba32.HotPink, 10, new Rectangle(10, 10, 190, 140))); image.Save($"{path}/Rectangle.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[8, 8]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[8, 8]); - Assert.Equal(Rgba32.HotPink, sourcePixels[198, 10]); + Assert.Equal(Rgba32.HotPink, sourcePixels[198, 10]); - Assert.Equal(Rgba32.HotPink, sourcePixels[10, 50]); + Assert.Equal(Rgba32.HotPink, sourcePixels[10, 50]); - Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); + Assert.Equal(Rgba32.Blue, sourcePixels[50, 50]); - Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); - } + Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); } } } diff --git a/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs index 8ff27fd72b..42b0dc1e6f 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs @@ -13,6 +13,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Drawing { + using SixLabors.Memory; + public class SolidComplexPolygonTests : FileTestBase { [Fact] @@ -32,18 +34,14 @@ namespace SixLabors.ImageSharp.Tests.Drawing // var clipped = new Rectangle(10, 10, 100, 100).Clip(new Rectangle(20, 0, 20, 20)); using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Fill(Rgba32.HotPink, clipped)); + image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, clipped)); image.Save($"{path}/Simple.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[20, 35]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[20, 35]); - //inside hole - Assert.Equal(Rgba32.Blue, sourcePixels[60, 100]); - } + //inside hole + Assert.Equal(Rgba32.Blue, sourcePixels[60, 100]); } } @@ -64,18 +62,14 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Fill(Rgba32.HotPink, simplePath.Clip(hole1))); + image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, simplePath.Clip(hole1))); image.Save($"{path}/SimpleOverlapping.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[20, 35]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[20, 35]); - //inside hole - Assert.Equal(Rgba32.Blue, sourcePixels[60, 100]); - } + //inside hole + Assert.Equal(Rgba32.Blue, sourcePixels[60, 100]); } } @@ -97,21 +91,18 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Fill(color, simplePath.Clip(hole1))); + image.Mutate(x => x.BackgroundColor(Rgba32.Blue).Fill(color, simplePath.Clip(hole1))); image.Save($"{path}/Opacity.png"); //shift background color towards forground color by the opacity amount - var mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); + var mergedColor = new Rgba32( + Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(mergedColor, sourcePixels[20, 35]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(mergedColor, sourcePixels[20, 35]); - //inside hole - Assert.Equal(Rgba32.Blue, sourcePixels[60, 100]); - } + //inside hole + Assert.Equal(Rgba32.Blue, sourcePixels[60, 100]); } } } diff --git a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs index 4d6281a3c2..b39cc46329 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs @@ -15,6 +15,8 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Drawing { + using SixLabors.Memory; + public class SolidPolygonTests : FileTestBase { [Fact] @@ -29,14 +31,11 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .FillPolygon(new GraphicsOptions(true), Rgba32.HotPink, simplePath)); + image.Mutate(x => x.FillPolygon(new GraphicsOptions(true), Rgba32.HotPink, simplePath)); image.Save($"{path}/Simple.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[81, 145]); - } + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[81, 145]); } } @@ -52,14 +51,12 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .FillPolygon(new GraphicsOptions(true), Brushes.Horizontal(Rgba32.HotPink), simplePath)); + image.Mutate( + x => x.FillPolygon(new GraphicsOptions(true), Brushes.Horizontal(Rgba32.HotPink), simplePath)); image.Save($"{path}/Pattern.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[81, 145]); - } + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[81, 145]); } } @@ -75,21 +72,21 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .FillPolygon(new GraphicsOptions(false), Rgba32.HotPink, simplePath)); + image.Mutate( + x => x.BackgroundColor(Rgba32.Blue).FillPolygon( + new GraphicsOptions(false), + Rgba32.HotPink, + simplePath)); image.Save($"{path}/Simple_NoAntialias.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.True(Rgba32.HotPink == sourcePixels[11, 11], "[11, 11] wrong"); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.True(Rgba32.HotPink == sourcePixels[11, 11], "[11, 11] wrong"); - Assert.True(Rgba32.HotPink == sourcePixels[199, 149], "[199, 149] wrong"); + Assert.True(Rgba32.HotPink == sourcePixels[199, 149], "[199, 149] wrong"); - Assert.True(Rgba32.HotPink == sourcePixels[50, 50], "[50, 50] wrong"); + Assert.True(Rgba32.HotPink == sourcePixels[50, 50], "[50, 50] wrong"); - Assert.True(Rgba32.Blue == sourcePixels[2, 2], "[2, 2] wrong"); - } + Assert.True(Rgba32.Blue == sourcePixels[2, 2], "[2, 2] wrong"); } } @@ -128,18 +125,15 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .FillPolygon(color, simplePath)); + image.Mutate(x => x.BackgroundColor(Rgba32.Blue).FillPolygon(color, simplePath)); image.Save($"{path}/Opacity.png"); //shift background color towards forground color by the opacity amount - var mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); + var mergedColor = new Rgba32( + Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); - } + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); } } @@ -150,23 +144,22 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(500, 500)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Fill(Rgba32.HotPink, new SixLabors.Shapes.RectangularPolygon(10, 10, 190, 140))); + image.Mutate( + x => x.BackgroundColor(Rgba32.Blue).Fill( + Rgba32.HotPink, + new SixLabors.Shapes.RectangularPolygon(10, 10, 190, 140))); image.Save($"{path}/Rectangle.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.HotPink, sourcePixels[11, 11]); - Assert.Equal(Rgba32.HotPink, sourcePixels[198, 10]); + Assert.Equal(Rgba32.HotPink, sourcePixels[198, 10]); - Assert.Equal(Rgba32.HotPink, sourcePixels[10, 50]); + Assert.Equal(Rgba32.HotPink, sourcePixels[10, 50]); - Assert.Equal(Rgba32.HotPink, sourcePixels[50, 50]); + Assert.Equal(Rgba32.HotPink, sourcePixels[50, 50]); - Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); - } + Assert.Equal(Rgba32.Blue, sourcePixels[2, 2]); } } @@ -177,17 +170,14 @@ namespace SixLabors.ImageSharp.Tests.Drawing using (var image = new Image(100, 100)) { - image.Mutate(x => x - .BackgroundColor(Rgba32.Blue) - .Fill(Rgba32.HotPink, new RegularPolygon(50, 50, 3, 30))); + image.Mutate( + x => x.BackgroundColor(Rgba32.Blue).Fill(Rgba32.HotPink, new RegularPolygon(50, 50, 3, 30))); image.Save($"{path}/Triangle.png"); - using (PixelAccessor sourcePixels = image.Lock()) - { - Assert.Equal(Rgba32.Blue, sourcePixels[30, 65]); + Buffer2D sourcePixels = image.GetRootFramePixelBuffer(); + Assert.Equal(Rgba32.Blue, sourcePixels[30, 65]); - Assert.Equal(Rgba32.HotPink, sourcePixels[50, 50]); - } + Assert.Equal(Rgba32.HotPink, sourcePixels[50, 50]); } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/GenericBlock8x8Tests.cs b/tests/ImageSharp.Tests/Formats/Jpg/GenericBlock8x8Tests.cs index 05ded4341d..956ade5022 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/GenericBlock8x8Tests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/GenericBlock8x8Tests.cs @@ -10,24 +10,24 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Formats.Jpg { + using SixLabors.Memory; + public class GenericBlock8x8Tests { public static Image CreateTestImage() where TPixel : struct, IPixel { var image = new Image(10, 10); - using (PixelAccessor pixels = image.Lock()) + Buffer2D pixels = image.GetRootFramePixelBuffer(); + for (int i = 0; i < 10; i++) { - for (int i = 0; i < 10; i++) + for (int j = 0; j < 10; j++) { - for (int j = 0; j < 10; j++) - { - var rgba = new Rgba32((byte)(i + 1), (byte)(j + 1), (byte)200, (byte)255); - var color = default(TPixel); - color.PackFromRgba32(rgba); - - pixels[i, j] = color; - } + var rgba = new Rgba32((byte)(i + 1), (byte)(j + 1), (byte)200, (byte)255); + var color = default(TPixel); + color.PackFromRgba32(rgba); + + pixels[i, j] = color; } } diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs index f04f891f8f..4dcfcd4b78 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/TestPatternProvider.cs @@ -9,6 +9,8 @@ using SixLabors.ImageSharp.PixelFormats; namespace SixLabors.ImageSharp.Tests { + using SixLabors.Memory; + public abstract partial class TestImageProvider where TPixel : struct, IPixel { @@ -54,19 +56,18 @@ namespace SixLabors.ImageSharp.Tests private static void DrawTestPattern(Image image) { // first lets split the image into 4 quadrants - using (PixelAccessor pixels = image.Lock()) - { - BlackWhiteChecker(pixels); // top left - VerticalBars(pixels); // top right - TransparentGradients(pixels); // bottom left - Rainbow(pixels); // bottom right - } + Buffer2D pixels = image.GetRootFramePixelBuffer(); + BlackWhiteChecker(pixels); // top left + VerticalBars(pixels); // top right + TransparentGradients(pixels); // bottom left + Rainbow(pixels); // bottom right } + /// /// Fills the top right quadrant with alternating solid vertical bars. /// /// - private static void VerticalBars(PixelAccessor pixels) + private static void VerticalBars(Buffer2D pixels) { // topLeft int left = pixels.Width / 2; @@ -103,7 +104,7 @@ namespace SixLabors.ImageSharp.Tests /// fills the top left quadrant with a black and white checker board. /// /// - private static void BlackWhiteChecker(PixelAccessor pixels) + private static void BlackWhiteChecker(Buffer2D pixels) { // topLeft int left = 0; @@ -142,7 +143,7 @@ namespace SixLabors.ImageSharp.Tests /// Fills the bottom left quadrent with 3 horizental bars in Red, Green and Blue with a alpha gradient from left (transparent) to right (solid). /// /// - private static void TransparentGradients(PixelAccessor pixels) + private static void TransparentGradients(Buffer2D pixels) { // topLeft int left = 0; @@ -187,7 +188,7 @@ namespace SixLabors.ImageSharp.Tests /// A better algorithm could be used but it works /// /// - private static void Rainbow(PixelAccessor pixels) + private static void Rainbow(Buffer2D pixels) { int left = pixels.Width / 2; int right = pixels.Width; diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs index ba7f6ad31d..03ab422e56 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtils.cs @@ -15,6 +15,7 @@ using SixLabors.Primitives; namespace SixLabors.ImageSharp.Tests { using SixLabors.ImageSharp.Advanced; + using SixLabors.Memory; /// /// Various utility and extension methods. @@ -63,36 +64,30 @@ namespace SixLabors.ImageSharp.Tests var rgb1 = default(Rgb24); var rgb2 = default(Rgb24); - using (PixelAccessor pixA = a.Lock()) + Buffer2D pixA = a.GetRootFramePixelBuffer(); + Buffer2D pixB = b.GetRootFramePixelBuffer(); + for (int y = 0; y < a.Height; y++) { - using (PixelAccessor pixB = b.Lock()) + for (int x = 0; x < a.Width; x++) { - for (int y = 0; y < a.Height; y++) + TPixel ca = pixA[x, y]; + TPixel cb = pixB[x, y]; + + if (compareAlpha) + { + if (!ca.Equals(cb)) + { + return false; + } + } + else { - for (int x = 0; x < a.Width; x++) + ca.ToRgb24(ref rgb1); + cb.ToRgb24(ref rgb2); + + if (rgb1.R != rgb2.R || rgb1.G != rgb2.G || rgb1.B != rgb2.B) { - TPixel ca = pixA[x, y]; - TPixel cb = pixB[x, y]; - - if (compareAlpha) - { - if (!ca.Equals(cb)) - { - return false; - } - } - else - { - ca.ToRgb24(ref rgb1); - cb.ToRgb24(ref rgb2); - - if (rgb1.R != rgb2.R || - rgb1.G != rgb2.G || - rgb1.B != rgb2.B) - { - return false; - } - } + return false; } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index c5d9a72481..06c77235b2 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -13,6 +13,8 @@ using SixLabors.ImageSharp.Formats; namespace SixLabors.ImageSharp.Tests { + using SixLabors.Memory; + public class TestImageProviderTests { public TestImageProviderTests(ITestOutputHelper output) @@ -282,19 +284,17 @@ namespace SixLabors.ImageSharp.Tests var rgba = default(Rgba32); - using (PixelAccessor pixels = img.Lock()) + Buffer2D pixels = img.GetRootFramePixelBuffer(); + for (int y = 0; y < pixels.Height; y++) { - for (int y = 0; y < pixels.Height; y++) + for (int x = 0; x < pixels.Width; x++) { - for (int x = 0; x < pixels.Width; x++) - { - pixels[x, y].ToRgba32(ref rgba); - - Assert.Equal(255, rgba.R); - Assert.Equal(100, rgba.G); - Assert.Equal(50, rgba.B); - Assert.Equal(200, rgba.A); - } + pixels[x, y].ToRgba32(ref rgba); + + Assert.Equal(255, rgba.R); + Assert.Equal(100, rgba.G); + Assert.Equal(50, rgba.B); + Assert.Equal(200, rgba.A); } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index a451402c29..cab61bc59d 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -14,6 +14,7 @@ using Xunit.Abstractions; namespace SixLabors.ImageSharp.Tests { using SixLabors.ImageSharp.Processing.Effects; + using SixLabors.Memory; public class TestUtilityExtensionsTests { @@ -29,20 +30,18 @@ namespace SixLabors.ImageSharp.Tests { var image = new Image(10, 10); - using (PixelAccessor pixels = image.Lock()) + Buffer2D pixels = image.GetRootFramePixelBuffer(); + for (int i = 0; i < 10; i++) { - for (int i = 0; i < 10; i++) + for (int j = 0; j < 10; j++) { - for (int j = 0; j < 10; j++) - { - var v = new Vector4(i, j, 0, 1); - v /= 10; + var v = new Vector4(i, j, 0, 1); + v /= 10; - var color = default(TPixel); - color.PackFromVector4(v); + var color = default(TPixel); + color.PackFromVector4(v); - pixels[i, j] = color; - } + pixels[i, j] = color; } }