diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 3c6e05a8b..dd092e643 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -103,36 +103,42 @@ namespace SixLabors.ImageSharp.Formats.Bmp this.ReadImageHeaders(stream, out bool inverted, out byte[] palette); var image = new Image(this.configuration, this.infoHeader.Width, this.infoHeader.Height); - using (PixelAccessor pixels = image.Lock()) + + Buffer2D pixels = image.GetRootFramePixelBuffer(); + + switch (this.infoHeader.Compression) { - switch (this.infoHeader.Compression) - { - case BmpCompression.RGB: - if (this.infoHeader.BitsPerPixel == 32) - { - this.ReadRgb32(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); - } - else if (this.infoHeader.BitsPerPixel == 24) - { - this.ReadRgb24(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); - } - else if (this.infoHeader.BitsPerPixel == 16) - { - this.ReadRgb16(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); - } - else if (this.infoHeader.BitsPerPixel <= 8) - { - this.ReadRgbPalette(pixels, palette, this.infoHeader.Width, this.infoHeader.Height, this.infoHeader.BitsPerPixel, inverted); - } + case BmpCompression.RGB: + if (this.infoHeader.BitsPerPixel == 32) + { + this.ReadRgb32(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + } + else if (this.infoHeader.BitsPerPixel == 24) + { + this.ReadRgb24(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + } + else if (this.infoHeader.BitsPerPixel == 16) + { + this.ReadRgb16(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); + } + else if (this.infoHeader.BitsPerPixel <= 8) + { + this.ReadRgbPalette( + pixels, + palette, + this.infoHeader.Width, + this.infoHeader.Height, + this.infoHeader.BitsPerPixel, + inverted); + } - break; - case BmpCompression.RLE8: - this.ReadRle8(pixels, palette, this.infoHeader.Width, this.infoHeader.Height, inverted); + break; + case BmpCompression.RLE8: + this.ReadRle8(pixels, palette, this.infoHeader.Width, this.infoHeader.Height, inverted); - break; - default: - throw new NotSupportedException("Does not support this kind of bitmap files."); - } + break; + default: + throw new NotSupportedException("Does not support this kind of bitmap files."); } return image; @@ -202,12 +208,12 @@ 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. /// Whether the bitmap is inverted. - private void ReadRle8(PixelAccessor pixels, byte[] colors, int width, int height, bool inverted) + private void ReadRle8(Buffer2D pixels, byte[] colors, int width, int height, bool inverted) where TPixel : struct, IPixel { var color = default(TPixel); @@ -313,13 +319,13 @@ 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. /// The number of bits per pixel. /// Whether the bitmap is inverted. - private void ReadRgbPalette(PixelAccessor pixels, byte[] colors, int width, int height, int bits, bool inverted) + private void ReadRgbPalette(Buffer2D pixels, byte[] colors, int width, int height, int bits, bool inverted) where TPixel : struct, IPixel { // Pixels per byte (bits per pixel) @@ -377,11 +383,11 @@ 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. - private void ReadRgb16(PixelAccessor pixels, int width, int height, bool inverted) + private void ReadRgb16(Buffer2D pixels, int width, int height, bool inverted) where TPixel : struct, IPixel { int padding = CalculatePadding(width, 2); @@ -418,11 +424,11 @@ 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. - private void ReadRgb24(PixelAccessor pixels, int width, int height, bool inverted) + private void ReadRgb24(Buffer2D pixels, int width, int height, bool inverted) where TPixel : struct, IPixel { int padding = CalculatePadding(width, 3); @@ -443,11 +449,11 @@ 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. - private void ReadRgb32(PixelAccessor pixels, int width, int height, bool inverted) + private void ReadRgb32(Buffer2D pixels, int width, int height, bool inverted) where TPixel : struct, IPixel { int padding = CalculatePadding(width, 4); @@ -574,12 +580,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp this.stream.Read(palette, 0, colorMapSize); } - if (this.infoHeader.Width > int.MaxValue || this.infoHeader.Height > int.MaxValue) - { - throw new ArgumentOutOfRangeException( - $"The input bmp '{this.infoHeader.Width}x{this.infoHeader.Height}' is " - + $"bigger then the max allowed size '{int.MaxValue}x{int.MaxValue}'"); - } + this.infoHeader.VerifyDimensions(); } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index d8bf90c7c..1864737c8 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -92,18 +92,16 @@ namespace SixLabors.ImageSharp.Formats.Bmp private void WriteImage(Stream stream, ImageFrame image) where TPixel : struct, IPixel { - using (PixelAccessor pixels = image.Lock()) + Buffer2D pixels = image.PixelBuffer; + switch (this.bitsPerPixel) { - switch (this.bitsPerPixel) - { - case BmpBitsPerPixel.Pixel32: - this.Write32Bit(stream, pixels); - break; + case BmpBitsPerPixel.Pixel32: + this.Write32Bit(stream, pixels); + break; - case BmpBitsPerPixel.Pixel24: - this.Write24Bit(stream, pixels); - break; - } + case BmpBitsPerPixel.Pixel24: + this.Write24Bit(stream, pixels); + break; } } @@ -117,8 +115,8 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// /// The pixel format. /// The to write to. - /// The containing pixel data. - private void Write32Bit(Stream stream, PixelAccessor pixels) + /// The containing pixel data. + private void Write32Bit(Stream stream, Buffer2D pixels) where TPixel : struct, IPixel { using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 4)) @@ -137,8 +135,8 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// /// The pixel format. /// The to write to. - /// The containing pixel data. - private void Write24Bit(Stream stream, PixelAccessor pixels) + /// The containing pixel data. + private void Write24Bit(Stream stream, Buffer2D pixels) where TPixel : struct, IPixel { using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 3)) diff --git a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs index a088a9b13..a70716a85 100644 --- a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs @@ -157,5 +157,17 @@ namespace SixLabors.ImageSharp.Formats.Bmp dest = this; } + + internal void VerifyDimensions() + { + const int MaximumBmpDimension = 65535; + + if (this.Width > MaximumBmpDimension || this.Height > MaximumBmpDimension) + { + throw new InvalidOperationException( + $"The input bmp '{this.Width}x{this.Height}' is " + + $"bigger then the max allowed size '{MaximumBmpDimension}x{MaximumBmpDimension}'"); + } + } } } \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs index 11b5c60d1..5105e57ab 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs @@ -101,7 +101,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters /// /// The 1-4 sized list of component buffers. /// The row to convert - public ComponentValues(IReadOnlyList> componentBuffers, int row) + public ComponentValues(IReadOnlyList> componentBuffers, int row) { this.ComponentCount = componentBuffers.Count; diff --git a/src/ImageSharp/ImageExtensions.Internal.cs b/src/ImageSharp/ImageExtensions.Internal.cs new file mode 100644 index 000000000..6bbc6ec52 --- /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 4fb09f0a9..bd86b7dee 100644 --- a/src/ImageSharp/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/ImageFrame{TPixel}.cs @@ -210,27 +210,6 @@ namespace SixLabors.ImageSharp return ref this.PixelBuffer[x, y]; } - /// - /// Locks the image providing access to the pixels. - /// - /// It is imperative that the accessor is correctly disposed off after use. - /// - /// - /// The - internal PixelAccessor 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); - } - /// /// Copies the pixels to a of the same size. /// diff --git a/src/ImageSharp/Memory/Buffer2DExtensions.cs b/src/ImageSharp/Memory/Buffer2DExtensions.cs index 58e273123..c27752570 100644 --- a/src/ImageSharp/Memory/Buffer2DExtensions.cs +++ b/src/ImageSharp/Memory/Buffer2DExtensions.cs @@ -8,14 +8,14 @@ using SixLabors.Primitives; namespace SixLabors.Memory { /// - /// Defines extension methods for . + /// Defines extension methods for . /// internal static class Buffer2DExtensions { /// /// Gets a to the backing buffer of . /// - internal static Span GetSpan(this IBuffer2D buffer) + internal static Span GetSpan(this Buffer2D buffer) where T : struct { return buffer.Buffer.GetSpan(); @@ -30,7 +30,7 @@ namespace SixLabors.Memory /// The element type /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Span GetRowSpan(this IBuffer2D buffer, int x, int y) + public static Span GetRowSpan(this Buffer2D buffer, int x, int y) where T : struct { return buffer.GetSpan().Slice((y * buffer.Width) + x, buffer.Width - x); @@ -44,7 +44,7 @@ namespace SixLabors.Memory /// The element type /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Span GetRowSpan(this IBuffer2D buffer, int y) + public static Span GetRowSpan(this Buffer2D buffer, int y) where T : struct { return buffer.GetSpan().Slice(y * buffer.Width, buffer.Width); @@ -58,7 +58,7 @@ namespace SixLabors.Memory /// The element type /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Memory GetRowMemory(this IBuffer2D buffer, int y) + public static Memory GetRowMemory(this Buffer2D buffer, int y) where T : struct { return buffer.Buffer.Memory.Slice(y * buffer.Width, buffer.Width); @@ -68,9 +68,9 @@ namespace SixLabors.Memory /// Returns the size of the buffer. /// /// The element type - /// The + /// The /// The of the buffer - public static Size Size(this IBuffer2D buffer) + public static Size Size(this Buffer2D buffer) where T : struct { return new Size(buffer.Width, buffer.Height); @@ -80,9 +80,9 @@ namespace SixLabors.Memory /// Returns a representing the full area of the buffer. /// /// The element type - /// The + /// The /// The - public static Rectangle FullRectangle(this IBuffer2D buffer) + public static Rectangle FullRectangle(this Buffer2D buffer) where T : struct { return new Rectangle(0, 0, buffer.Width, buffer.Height); @@ -92,13 +92,13 @@ namespace SixLabors.Memory /// Return a to the subarea represented by 'rectangle' /// /// The element type - /// The + /// The /// The rectangle subarea /// The - public static BufferArea GetArea(this IBuffer2D buffer, Rectangle rectangle) + public static BufferArea GetArea(this Buffer2D buffer, Rectangle rectangle) where T : struct => new BufferArea(buffer, rectangle); - public static BufferArea GetArea(this IBuffer2D buffer, int x, int y, int width, int height) + public static BufferArea GetArea(this Buffer2D buffer, int x, int y, int width, int height) where T : struct { var rectangle = new Rectangle(x, y, width, height); @@ -109,9 +109,9 @@ namespace SixLabors.Memory /// Return a to the whole area of 'buffer' /// /// The element type - /// The + /// The /// The - public static BufferArea GetArea(this IBuffer2D buffer) + public static BufferArea GetArea(this Buffer2D buffer) where T : struct => new BufferArea(buffer); } } \ No newline at end of file diff --git a/src/ImageSharp/Memory/Buffer2D{T}.cs b/src/ImageSharp/Memory/Buffer2D{T}.cs index b62156ffb..f8d75b54c 100644 --- a/src/ImageSharp/Memory/Buffer2D{T}.cs +++ b/src/ImageSharp/Memory/Buffer2D{T}.cs @@ -12,7 +12,7 @@ namespace SixLabors.Memory /// interpreted as a 2D region of x elements. /// /// The value type. - internal class Buffer2D : IBuffer2D, IDisposable + internal class Buffer2D : IDisposable where T : struct { /// @@ -28,10 +28,14 @@ namespace SixLabors.Memory this.Height = height; } - /// + /// + /// Gets the width. + /// public int Width { get; private set; } - /// + /// + /// Gets the height. + /// public int Height { get; private set; } /// diff --git a/src/ImageSharp/Memory/BufferArea{T}.cs b/src/ImageSharp/Memory/BufferArea{T}.cs index 96036f6ee..6a2146fd2 100644 --- a/src/ImageSharp/Memory/BufferArea{T}.cs +++ b/src/ImageSharp/Memory/BufferArea{T}.cs @@ -18,7 +18,7 @@ namespace SixLabors.Memory public readonly Rectangle Rectangle; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferArea(IBuffer2D destinationBuffer, Rectangle rectangle) + public BufferArea(Buffer2D destinationBuffer, Rectangle rectangle) { ImageSharp.DebugGuard.MustBeGreaterThanOrEqualTo(rectangle.X, 0, nameof(rectangle)); ImageSharp.DebugGuard.MustBeGreaterThanOrEqualTo(rectangle.Y, 0, nameof(rectangle)); @@ -30,15 +30,15 @@ namespace SixLabors.Memory } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferArea(IBuffer2D destinationBuffer) + public BufferArea(Buffer2D destinationBuffer) : this(destinationBuffer, destinationBuffer.FullRectangle()) { } /// - /// Gets the being pointed by this instance. + /// Gets the being pointed by this instance. /// - public IBuffer2D DestinationBuffer { get; } + public Buffer2D DestinationBuffer { get; } /// /// Gets the size of the area. diff --git a/src/ImageSharp/Memory/IBuffer2D{T}.cs b/src/ImageSharp/Memory/IBuffer2D{T}.cs deleted file mode 100644 index a92f30dd5..000000000 --- a/src/ImageSharp/Memory/IBuffer2D{T}.cs +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System; - -namespace SixLabors.Memory -{ - /// - /// An interface that represents a contigous buffer of value type objects - /// interpreted as a 2D region of x elements. - /// - /// The value type. - internal interface IBuffer2D - where T : struct - { - /// - /// Gets the width. - /// - int Width { get; } - - /// - /// Gets the height. - /// - int Height { get; } - - /// - /// Gets the contigous buffer being wrapped. - /// - IBuffer Buffer { get; } - } -} \ No newline at end of file diff --git a/src/ImageSharp/PixelAccessorExtensions.cs b/src/ImageSharp/PixelAccessorExtensions.cs deleted file mode 100644 index b628c05f8..000000000 --- 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 deleted file mode 100644 index 7ca066ff1..000000000 --- a/src/ImageSharp/PixelAccessor{TPixel}.cs +++ /dev/null @@ -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 -{ - /// - /// Provides per-pixel access to generic pixels. - /// - /// The pixel format. - internal sealed class PixelAccessor : IDisposable, IBuffer2D - where TPixel : struct, IPixel - { - /// - /// Initializes a new instance of the class. - /// - /// The image to provide pixel access for. - public PixelAccessor(IPixelSource 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); - } - - /// - /// Gets the containing the pixel data. - /// - internal Buffer2D PixelBuffer { get; private set; } - - /// - /// Gets the size of a single pixel in the number of bytes. - /// - public int PixelSize { get; private set; } - - /// - /// Gets the width of one row in the number of bytes. - /// - public int RowStride { get; private set; } - - /// - public int Width { get; private set; } - - /// - public int Height { get; private set; } - - public IBuffer Buffer => this.PixelBuffer.Buffer; - - /// - /// Gets or sets the pixel at the specified position. - /// - /// The x-coordinate of the pixel. Must be greater than or equal to zero and less than the width of the image. - /// The y-coordinate of the pixel. Must be greater than or equal to zero and less than the height of the image. - /// The at the specified position. - 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 span = this.GetSpan(); - span[(y * this.Width) + x] = value; - } - } - - /// - public void Dispose() - { - } - - /// - /// Resets all the pixels to it's initial value. - /// - public void Reset() - { - this.PixelBuffer.Buffer.Clear(); - } - - /// - /// Sets the pixel buffer in an unsafe manner. This should not be used unless you know what its doing!!! - /// - /// The pixels. - /// Returns the old pixel data thats has gust been replaced. - /// If is true then caller is responsible for ensuring is called. - internal Buffer2D SwapBufferOwnership(Buffer2D pixels) - { - Buffer2D oldPixels = this.PixelBuffer; - this.SetPixelBufferUnsafe(pixels); - 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!!! - /// - /// The pixel buffer - private void SetPixelBufferUnsafe(Buffer2D pixels) - { - this.PixelBuffer = pixels; - - this.Width = pixels.Width; - this.Height = pixels.Height; - this.PixelSize = Unsafe.SizeOf(); - this.RowStride = this.Width * this.PixelSize; - } - - /// - /// Checks the coordinates to ensure they are within bounds. - /// - /// The x-coordinate of the pixel. Must be greater than zero and less than the width of the image. - /// The y-coordinate of the pixel. Must be greater than zero and less than the height of the image. - /// - /// Thrown if the coordinates are not within the bounds of the image. - /// - [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."); - } - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Processing/Convolution/Processors/EdgeDetectorCompassProcessor.cs b/src/ImageSharp/Processing/Convolution/Processors/EdgeDetectorCompassProcessor.cs index bcedd7a3c..b78145089 100644 --- a/src/ImageSharp/Processing/Convolution/Processors/EdgeDetectorCompassProcessor.cs +++ b/src/ImageSharp/Processing/Convolution/Processors/EdgeDetectorCompassProcessor.cs @@ -3,11 +3,14 @@ using System; using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Threading.Tasks; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Primitives; using SixLabors.ImageSharp.Processing.Filters.Processors; using SixLabors.ImageSharp.Processing.Processors; +using SixLabors.Memory; using SixLabors.Primitives; namespace SixLabors.ImageSharp.Processing.Convolution.Processors @@ -128,27 +131,35 @@ namespace SixLabors.ImageSharp.Processing.Convolution.Processors { new ConvolutionProcessor(kernels[i]).Apply(pass, sourceRectangle, configuration); - using (PixelAccessor passPixels = pass.Lock()) - using (PixelAccessor targetPixels = source.Lock()) - { - Parallel.For( - minY, - maxY, - configuration.ParallelOptions, - y => + Buffer2D passPixels = pass.PixelBuffer; + Buffer2D targetPixels = source.PixelBuffer; + + Parallel.For( + minY, + maxY, + configuration.ParallelOptions, + y => { int offsetY = y - shiftY; + + ref TPixel passPixelsBase = ref MemoryMarshal.GetReference(passPixels.GetRowSpan(offsetY)); + ref TPixel targetPixelsBase = ref MemoryMarshal.GetReference(targetPixels.GetRowSpan(offsetY)); + for (int x = minX; x < maxX; x++) { int offsetX = x - shiftX; // Grab the max components of the two pixels - TPixel packed = default(TPixel); - packed.PackFromVector4(Vector4.Max(passPixels[offsetX, offsetY].ToVector4(), targetPixels[offsetX, offsetY].ToVector4())); - targetPixels[offsetX, offsetY] = packed; + ref TPixel currentPassPixel = ref Unsafe.Add(ref passPixelsBase, offsetX); + ref TPixel currentTargetPixel = ref Unsafe.Add(ref targetPixelsBase, offsetX); + + var pixelValue = Vector4.Max( + currentPassPixel.ToVector4(), + currentTargetPixel.ToVector4()); + + currentTargetPixel.PackFromVector4(pixelValue); } }); - } } } } diff --git a/tests/ImageSharp.Benchmarks/Codecs/CopyPixels.cs b/tests/ImageSharp.Benchmarks/Codecs/CopyPixels.cs index 610c356b7..8bf87fb62 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 fe0678aca..5fe8b2785 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 b2fa47893..ce17481c4 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs @@ -52,7 +52,7 @@ namespace SixLabors.ImageSharp.Benchmarks } internal class GlowProcessorParallel : ImageProcessor - where TPixel : struct, IPixel + where TPixel : struct, IPixel { /// /// Initializes a new instance of the class. @@ -103,8 +103,8 @@ namespace SixLabors.ImageSharp.Benchmarks int width = maxX - minX; using (IBuffer rowColors = Configuration.Default.MemoryAllocator.Allocate(width)) - using (PixelAccessor sourcePixels = source.Lock()) { + Buffer2D sourcePixels = source.PixelBuffer; rowColors.GetSpan().Fill(glowColor); Parallel.For( diff --git a/tests/ImageSharp.Tests/Drawing/BeziersTests.cs b/tests/ImageSharp.Tests/Drawing/BeziersTests.cs index daa640a0b..6dc2fae89 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 7e75f52b2..c1865e930 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 5b47e7835..29b78220d 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 09ed46908..ecd1c06e8 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 6128756c5..28b59746f 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 f6d9bf213..c7a0531c9 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 8ff27fd72..42b0dc1e6 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 4d6281a3c..b39cc4632 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 05ded4341..956ade502 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 f04f891f8..4dcfcd4b7 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 ba7f6ad31..03ab422e5 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 c5d9a7248..06c77235b 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 a451402c2..cab61bc59 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; } }