Browse Source

PixelAccessor<T> is now a meaningless thin wrapper around Buffer2D<T>

pull/475/head
Anton Firszov 8 years ago
parent
commit
45cca93bbe
  1. 77
      src/ImageSharp/Image/PixelAccessor{TPixel}.cs
  2. 4
      src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs
  3. 23
      src/ImageSharp/Quantizers/Quantize.cs

77
src/ImageSharp/Image/PixelAccessor{TPixel}.cs

@ -17,25 +17,6 @@ namespace SixLabors.ImageSharp
internal sealed class PixelAccessor<TPixel> : IDisposable, IBuffer2D<TPixel>
where TPixel : struct, IPixel<TPixel>
{
#pragma warning disable SA1401 // Fields must be private
/// <summary>
/// The <see cref="Buffer2D{T}"/> containing the pixel data.
/// </summary>
internal Buffer2D<TPixel> PixelBuffer;
private bool ownedBuffer;
#pragma warning restore SA1401 // Fields must be private
/// <summary>
/// A value indicating whether this instance of the given entity has been disposed.
/// </summary>
/// <value><see langword="true"/> if this instance has been disposed; otherwise, <see langword="false"/>.</value>
/// <remarks>
/// 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.
/// </remarks>
private bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="PixelAccessor{TPixel}"/> class.
/// </summary>
@ -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);
}
/// <summary>
/// Initializes a new instance of the <see cref="PixelAccessor{TPixel}"/> class.
/// Gets the <see cref="Buffer2D{T}"/> containing the pixel data.
/// </summary>
/// <param name="memoryManager">The <see cref="MemoryManager"/> to use for buffer allocations.</param>
/// <param name="width">The width of the image represented by the pixel buffer.</param>
/// <param name="height">The height of the image represented by the pixel buffer.</param>
public PixelAccessor(MemoryManager memoryManager, int width, int height)
: this(width, height, memoryManager.Allocate2D<TPixel>(width, height, true), true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PixelAccessor{TPixel}" /> class.
/// </summary>
/// <param name="width">The width of the image represented by the pixel buffer.</param>
/// <param name="height">The height of the image represented by the pixel buffer.</param>
/// <param name="pixels">The pixel buffer.</param>
/// <param name="ownedBuffer">if set to <c>true</c> [owned buffer].</param>
private PixelAccessor(int width, int height, Buffer2D<TPixel> pixels, bool ownedBuffer)
{
Guard.NotNull(pixels, nameof(pixels));
Guard.MustBeGreaterThan(width, 0, nameof(width));
Guard.MustBeGreaterThan(height, 0, nameof(height));
this.SetPixelBufferUnsafe(pixels, ownedBuffer);
}
/// <summary>
/// Finalizes an instance of the <see cref="PixelAccessor{TPixel}"/> class.
/// </summary>
~PixelAccessor()
{
this.Dispose();
}
internal Buffer2D<TPixel> PixelBuffer { get; private set; }
/// <summary>
/// Gets the size of a single pixel in the number of bytes.
@ -132,22 +83,6 @@ namespace SixLabors.ImageSharp
/// <inheritdoc />
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);
}
/// <summary>
@ -167,7 +102,7 @@ namespace SixLabors.ImageSharp
internal Buffer2D<TPixel> SwapBufferOwnership(Buffer2D<TPixel> pixels)
{
Buffer2D<TPixel> 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!!!
/// </summary>
/// <param name="pixels">The pixel buffer</param>
/// <param name="ownedBuffer">if set to <c>true</c> then this instance ownes the buffer and thus should dispose of it afterwards.</param>
private void SetPixelBufferUnsafe(Buffer2D<TPixel> pixels, bool ownedBuffer)
private void SetPixelBufferUnsafe(Buffer2D<TPixel> pixels)
{
this.PixelBuffer = pixels;
this.ownedBuffer = ownedBuffer;
this.Width = pixels.Width;
this.Height = pixels.Height;

4
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<TPixel>(configuration.MemoryManager, width, height))
using (Buffer2D<TPixel> targetPixels = configuration.MemoryManager.Allocate2D<TPixel>(source.Size()))
{
Parallel.For(
0,
@ -112,7 +112,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
}
});
source.SwapPixelsBuffers(targetPixels);
Buffer2D<TPixel>.SwapContents(source.PixelBuffer, targetPixels);
}
}
}

23
src/ImageSharp/Quantizers/Quantize.cs

@ -9,6 +9,8 @@ using SixLabors.ImageSharp.Quantizers;
namespace SixLabors.ImageSharp
{
using SixLabors.ImageSharp.Memory;
/// <summary>
/// Extension methods for the <see cref="Image{TPixel}"/> type.
/// </summary>
@ -61,23 +63,26 @@ namespace SixLabors.ImageSharp
QuantizedImage<TPixel> quantized = quantizer.Quantize(img.Frames.RootFrame, maxColors);
int palleteCount = quantized.Palette.Length - 1;
using (var pixels = new PixelAccessor<TPixel>(source.MemoryManager, quantized.Width, quantized.Height))
using (Buffer2D<TPixel> pixels = source.MemoryManager.Allocate2D<TPixel>(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<TPixel> 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<TPixel>.SwapContents(img.Frames[0].PixelBuffer, pixels);
}
});
}

Loading…
Cancel
Save