Browse Source

Refactor JpegPixelArea to delegate memory management to Buffer2D<T>.

af/merge-core
Mykhailo Matviiv 9 years ago
parent
commit
0e1d40313a
  1. 38
      src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs

38
src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs

@ -4,7 +4,6 @@
// </copyright> // </copyright>
namespace ImageSharp.Formats.Jpg namespace ImageSharp.Formats.Jpg
{ {
using System.Buffers;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
/// <summary> /// <summary>
@ -15,20 +14,20 @@ namespace ImageSharp.Formats.Jpg
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="JpegPixelArea" /> struct from existing data. /// Initializes a new instance of the <see cref="JpegPixelArea" /> struct from existing data.
/// </summary> /// </summary>
/// <param name="pixels">The pixel array</param> /// <param name="pixels">The pixel buffer</param>
/// <param name="striede">The stride</param> /// <param name="stride">The stride</param>
/// <param name="offset">The offset</param> /// <param name="offset">The offset</param>
public JpegPixelArea(byte[] pixels, int striede, int offset) public JpegPixelArea(Buffer2D<byte> pixels, int stride, int offset)
{ {
this.Stride = striede; this.Stride = stride;
this.Pixels = pixels; this.Pixels = pixels;
this.Offset = offset; this.Offset = offset;
} }
/// <summary> /// <summary>
/// Gets the pixels. /// Gets the pixels buffer.
/// </summary> /// </summary>
public byte[] Pixels { get; private set; } public Buffer2D<byte> Pixels { get; private set; }
/// <summary> /// <summary>
/// Gets a value indicating whether the instance has been initalized. (Is not default(JpegPixelArea)) /// Gets a value indicating whether the instance has been initalized. (Is not default(JpegPixelArea))
@ -36,21 +35,19 @@ namespace ImageSharp.Formats.Jpg
public bool IsInitialized => this.Pixels != null; public bool IsInitialized => this.Pixels != null;
/// <summary> /// <summary>
/// Gets or the stride. /// Gets the stride.
/// </summary> /// </summary>
public int Stride { get; } public int Stride { get; }
/// <summary> /// <summary>
/// Gets or the offset. /// Gets the offset.
/// </summary> /// </summary>
public int Offset { get; } public int Offset { get; }
/// <summary> /// <summary>
/// Gets a <see cref="MutableSpan{T}" /> of bytes to the pixel area /// Gets a <see cref="MutableSpan{T}" /> of bytes to the pixel area
/// </summary> /// </summary>
public MutableSpan<byte> Span => new MutableSpan<byte>(this.Pixels, this.Offset); public MutableSpan<byte> Span => new MutableSpan<byte>(this.Pixels.Array, this.Offset);
private static ArrayPool<byte> BytePool => ArrayPool<byte>.Shared;
/// <summary> /// <summary>
/// Returns the pixel at (x, y) /// Returns the pixel at (x, y)
@ -69,21 +66,16 @@ namespace ImageSharp.Formats.Jpg
/// <summary> /// <summary>
/// Creates a new instance of the <see cref="JpegPixelArea" /> struct. /// Creates a new instance of the <see cref="JpegPixelArea" /> struct.
/// Pixel array will be taken from a pool, this instance will be the owner of it's pixel data, therefore /// Pixel array will be handled by <see cref="Buffer2D{T}"/>, but
/// <see cref="ReturnPooled" /> should be called when the instance is no longer needed. /// <see cref="ReturnPooled" /> can be called when the instance is no longer needed.
/// </summary> /// </summary>
/// <param name="width">The width.</param> /// <param name="width">The width.</param>
/// <param name="height">The height.</param> /// <param name="height">The height.</param>
/// <returns>A <see cref="JpegPixelArea" /> with pooled data</returns> /// <returns>A <see cref="JpegPixelArea" /> with pooled data</returns>
public static JpegPixelArea CreatePooled(int width, int height) public static JpegPixelArea CreatePooled(int width, int height) => new JpegPixelArea(Buffer2D<byte>.CreateClean(width, height), width, 0);
{
int size = width * height;
byte[] pixels = BytePool.Rent(size);
return new JpegPixelArea(pixels, width, 0);
}
/// <summary> /// <summary>
/// Returns <see cref="Pixels" /> to the pool /// Dispose <see cref="Pixels" />.
/// </summary> /// </summary>
public void ReturnPooled() public void ReturnPooled()
{ {
@ -92,7 +84,7 @@ namespace ImageSharp.Formats.Jpg
return; return;
} }
BytePool.Return(this.Pixels); this.Pixels.Dispose();
this.Pixels = null; this.Pixels = null;
} }
@ -129,7 +121,7 @@ namespace ImageSharp.Formats.Jpg
public unsafe void LoadColorsFrom(Block8x8F* block, Block8x8F* temp) public unsafe void LoadColorsFrom(Block8x8F* block, Block8x8F* temp)
{ {
// Level shift by +128, clip to [0, 255], and write to dst. // Level shift by +128, clip to [0, 255], and write to dst.
block->CopyColorsTo(new MutableSpan<byte>(this.Pixels, this.Offset), this.Stride, temp); block->CopyColorsTo(new MutableSpan<byte>(this.Pixels.Array, this.Offset), this.Stride, temp);
} }
} }
} }
Loading…
Cancel
Save