From 45cca93bbe7912021fb6067c80605a5c3cc7ed92 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Fri, 23 Feb 2018 01:43:39 +0100 Subject: [PATCH] PixelAccessor is now a meaningless thin wrapper around Buffer2D --- src/ImageSharp/Image/PixelAccessor{TPixel}.cs | 77 ++----------------- .../Processors/Transforms/FlipProcessor.cs | 4 +- src/ImageSharp/Quantizers/Quantize.cs | 23 +++--- 3 files changed, 21 insertions(+), 83 deletions(-) diff --git a/src/ImageSharp/Image/PixelAccessor{TPixel}.cs b/src/ImageSharp/Image/PixelAccessor{TPixel}.cs index f1c8531cc4..63e4c015c1 100644 --- a/src/ImageSharp/Image/PixelAccessor{TPixel}.cs +++ b/src/ImageSharp/Image/PixelAccessor{TPixel}.cs @@ -17,25 +17,6 @@ namespace SixLabors.ImageSharp internal sealed class PixelAccessor : IDisposable, IBuffer2D where TPixel : struct, IPixel { -#pragma warning disable SA1401 // Fields must be private - /// - /// The containing the pixel data. - /// - internal Buffer2D PixelBuffer; - private bool ownedBuffer; -#pragma warning restore SA1401 // Fields must be private - - /// - /// A value indicating whether this instance of the given entity has been disposed. - /// - /// if this instance has been disposed; otherwise, . - /// - /// If the entity is disposed, it must not be disposed a second time. The isDisposed field is set the first time the entity - /// is disposed. If the isDisposed field is true, then the Dispose() method will not dispose again. This help not to prolong the entity's - /// life in the Garbage Collector. - /// - private bool isDisposed; - /// /// Initializes a new instance of the class. /// @@ -46,43 +27,13 @@ namespace SixLabors.ImageSharp Guard.MustBeGreaterThan(image.PixelBuffer.Width, 0, "image width"); Guard.MustBeGreaterThan(image.PixelBuffer.Height, 0, "image height"); - this.SetPixelBufferUnsafe(image.PixelBuffer, false); + this.SetPixelBufferUnsafe(image.PixelBuffer); } /// - /// Initializes a new instance of the class. + /// Gets the containing the pixel data. /// - /// The to use for buffer allocations. - /// The width of the image represented by the pixel buffer. - /// The height of the image represented by the pixel buffer. - public PixelAccessor(MemoryManager memoryManager, int width, int height) - : this(width, height, memoryManager.Allocate2D(width, height, true), true) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The width of the image represented by the pixel buffer. - /// The height of the image represented by the pixel buffer. - /// The pixel buffer. - /// if set to true [owned buffer]. - private PixelAccessor(int width, int height, Buffer2D pixels, bool ownedBuffer) - { - Guard.NotNull(pixels, nameof(pixels)); - Guard.MustBeGreaterThan(width, 0, nameof(width)); - Guard.MustBeGreaterThan(height, 0, nameof(height)); - - this.SetPixelBufferUnsafe(pixels, ownedBuffer); - } - - /// - /// Finalizes an instance of the class. - /// - ~PixelAccessor() - { - this.Dispose(); - } + internal Buffer2D PixelBuffer { get; private set; } /// /// Gets the size of a single pixel in the number of bytes. @@ -132,22 +83,6 @@ namespace SixLabors.ImageSharp /// public void Dispose() { - if (this.isDisposed || !this.ownedBuffer) - { - return; - } - - // Note disposing is done. - this.isDisposed = true; - - this.PixelBuffer.Dispose(); - - // This object will be cleaned up by the Dispose method. - // Therefore, you should call GC.SuppressFinalize to - // take this object off the finalization queue - // and prevent finalization code for this object - // from executing a second time. - GC.SuppressFinalize(this); } /// @@ -167,7 +102,7 @@ namespace SixLabors.ImageSharp internal Buffer2D SwapBufferOwnership(Buffer2D pixels) { Buffer2D oldPixels = this.PixelBuffer; - this.SetPixelBufferUnsafe(pixels, this.ownedBuffer); + this.SetPixelBufferUnsafe(pixels); return oldPixels; } @@ -184,11 +119,9 @@ namespace SixLabors.ImageSharp /// Sets the pixel buffer in an unsafe manor this should not be used unless you know what its doing!!! /// /// The pixel buffer - /// if set to true then this instance ownes the buffer and thus should dispose of it afterwards. - private void SetPixelBufferUnsafe(Buffer2D pixels, bool ownedBuffer) + private void SetPixelBufferUnsafe(Buffer2D pixels) { this.PixelBuffer = pixels; - this.ownedBuffer = ownedBuffer; this.Width = pixels.Width; this.Height = pixels.Height; diff --git a/src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs index eb16725917..7a6f5d9da3 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs @@ -93,7 +93,7 @@ namespace SixLabors.ImageSharp.Processing.Processors int height = source.Height; int halfWidth = (int)Math.Ceiling(width * .5F); - using (var targetPixels = new PixelAccessor(configuration.MemoryManager, width, height)) + using (Buffer2D targetPixels = configuration.MemoryManager.Allocate2D(source.Size())) { Parallel.For( 0, @@ -112,7 +112,7 @@ namespace SixLabors.ImageSharp.Processing.Processors } }); - source.SwapPixelsBuffers(targetPixels); + Buffer2D.SwapContents(source.PixelBuffer, targetPixels); } } } diff --git a/src/ImageSharp/Quantizers/Quantize.cs b/src/ImageSharp/Quantizers/Quantize.cs index 4052d46857..ee9c4d608c 100644 --- a/src/ImageSharp/Quantizers/Quantize.cs +++ b/src/ImageSharp/Quantizers/Quantize.cs @@ -9,6 +9,8 @@ using SixLabors.ImageSharp.Quantizers; namespace SixLabors.ImageSharp { + using SixLabors.ImageSharp.Memory; + /// /// Extension methods for the type. /// @@ -61,23 +63,26 @@ namespace SixLabors.ImageSharp QuantizedImage quantized = quantizer.Quantize(img.Frames.RootFrame, maxColors); int palleteCount = quantized.Palette.Length - 1; - using (var pixels = new PixelAccessor(source.MemoryManager, quantized.Width, quantized.Height)) + using (Buffer2D pixels = source.MemoryManager.Allocate2D(quantized.Width, quantized.Height)) { Parallel.For( 0, pixels.Height, img.GetConfiguration().ParallelOptions, y => - { - for (int x = 0; x < pixels.Width; x++) { - int i = x + (y * pixels.Width); - TPixel color = quantized.Palette[Math.Min(palleteCount, quantized.Pixels[i])]; - pixels[x, y] = color; - } - }); + Span row = pixels.GetRowSpan(y); + int yy = y * pixels.Width; + for (int x = 0; x < pixels.Width; x++) + { + int i = x + yy; + TPixel color = quantized.Palette[Math.Min(palleteCount, quantized.Pixels[i])]; + row[x] = color; + //pixels[x, y] = color; + } + }); - img.Frames[0].SwapPixelsBuffers(pixels); + Buffer2D.SwapContents(img.Frames[0].PixelBuffer, pixels); } }); }