|
|
|
@ -5,6 +5,7 @@ |
|
|
|
|
|
|
|
namespace ImageSharp.Formats.Jpg |
|
|
|
{ |
|
|
|
using System; |
|
|
|
using System.Buffers; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -33,44 +34,68 @@ namespace ImageSharp.Formats.Jpg |
|
|
|
public Block8x8F Block; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The <see cref="ArrayPool{T}"/> used to pool data in <see cref="JpegDecoderCore.DecodedBlocks"/>.
|
|
|
|
/// Should always clean arrays when returning!
|
|
|
|
/// Store the block data into a <see cref="DecodedBlockMemento"/> at the given index of an <see cref="DecodedBlockMemento.Array"/>.
|
|
|
|
/// </summary>
|
|
|
|
private static readonly ArrayPool<DecodedBlockMemento> ArrayPool = ArrayPool<DecodedBlockMemento>.Create(); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Rent an array of <see cref="DecodedBlockMemento"/>-s from the pool.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="size">The requested array size</param>
|
|
|
|
/// <returns>An array of <see cref="DecodedBlockMemento"/>-s</returns>
|
|
|
|
public static DecodedBlockMemento[] RentArray(int size) |
|
|
|
/// <param name="blockArray">The array <see cref="DecodedBlockMemento.Array"/></param>
|
|
|
|
/// <param name="index">The index in the array</param>
|
|
|
|
/// <param name="bx">X coordinate of the block</param>
|
|
|
|
/// <param name="by">Y coordinate of the block</param>
|
|
|
|
/// <param name="block">The <see cref="Block8x8F"/></param>
|
|
|
|
public static void Store(ref DecodedBlockMemento.Array blockArray, int index, int bx, int by, ref Block8x8F block) |
|
|
|
{ |
|
|
|
return ArrayPool.Rent(size); |
|
|
|
} |
|
|
|
if (index >= blockArray.Count) |
|
|
|
{ |
|
|
|
throw new IndexOutOfRangeException("Block index is out of range in DecodedBlockMemento.Store()!"); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the <see cref="DecodedBlockMemento"/> array to the pool.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="blockArray">The <see cref="DecodedBlockMemento"/> array</param>
|
|
|
|
public static void ReturnArray(DecodedBlockMemento[] blockArray) |
|
|
|
{ |
|
|
|
ArrayPool.Return(blockArray, true); |
|
|
|
blockArray.Buffer[index].Initialized = true; |
|
|
|
blockArray.Buffer[index].Bx = bx; |
|
|
|
blockArray.Buffer[index].By = by; |
|
|
|
blockArray.Buffer[index].Block = block; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Store the block data into a <see cref="DecodedBlockMemento"/> at the given index.
|
|
|
|
/// Because <see cref="System.Array.Length"/> has no information for rented arrays, we need to store the count and the buffer separately.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="blockArray">The array of <see cref="DecodedBlockMemento"/></param>
|
|
|
|
/// <param name="index">The index in the array</param>
|
|
|
|
/// <param name="bx">X coordinate of the block</param>
|
|
|
|
/// <param name="by">Y coordinate of the block</param>
|
|
|
|
/// <param name="block">The <see cref="Block8x8F"/></param>
|
|
|
|
public static void Store(DecodedBlockMemento[] blockArray, int index, int bx, int by, ref Block8x8F block) |
|
|
|
public struct Array : IDisposable |
|
|
|
{ |
|
|
|
blockArray[index].Initialized = true; |
|
|
|
blockArray[index].Bx = bx; |
|
|
|
blockArray[index].By = by; |
|
|
|
blockArray[index].Block = block; |
|
|
|
/// <summary>
|
|
|
|
/// The <see cref="ArrayPool{T}"/> used to pool data in <see cref="JpegDecoderCore.DecodedBlocks"/>.
|
|
|
|
/// Should always clean arrays when returning!
|
|
|
|
/// </summary>
|
|
|
|
private static readonly ArrayPool<DecodedBlockMemento> ArrayPool = ArrayPool<DecodedBlockMemento>.Create(); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="Array"/> struct. Rents a buffer.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="count">The number of valid <see cref="DecodedBlockMemento"/>-s</param>
|
|
|
|
public Array(int count) |
|
|
|
{ |
|
|
|
this.Count = count; |
|
|
|
this.Buffer = ArrayPool.Rent(count); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the number of actual <see cref="DecodedBlockMemento"/>-s inside <see cref="Buffer"/>
|
|
|
|
/// </summary>
|
|
|
|
public int Count { get; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the rented buffer.
|
|
|
|
/// </summary>
|
|
|
|
public DecodedBlockMemento[] Buffer { get; private set; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the rented buffer to the pool.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose() |
|
|
|
{ |
|
|
|
if (this.Buffer != null) |
|
|
|
{ |
|
|
|
ArrayPool.Return(this.Buffer, true); |
|
|
|
this.Buffer = null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |