//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageSharp.Formats.Jpg
{
using System;
using System.Buffers;
///
/// A structure to store unprocessed instances and their coordinates while scanning the image.
///
internal struct DecodedBlockMemento
{
///
/// A value indicating whether the instance is initialized.
///
public bool Initialized;
///
/// X coordinate of the current block, in units of 8x8. (The third block in the first row has (bx, by) = (2, 0))
///
public int Bx;
///
/// Y coordinate of the current block, in units of 8x8. (The third block in the first row has (bx, by) = (2, 0))
///
public int By;
///
/// The
///
public Block8x8F Block;
///
/// Store the block data into a at the given index of an .
///
/// The array
/// The index in the array
/// X coordinate of the block
/// Y coordinate of the block
/// The
public static void Store(ref DecodedBlockMemento.Array blockArray, int index, int bx, int by, ref Block8x8F block)
{
if (index >= blockArray.Count)
{
throw new IndexOutOfRangeException("Block index is out of range in DecodedBlockMemento.Store()!");
}
blockArray.Buffer[index].Initialized = true;
blockArray.Buffer[index].Bx = bx;
blockArray.Buffer[index].By = by;
blockArray.Buffer[index].Block = block;
}
///
/// Because has no information for rented arrays, we need to store the count and the buffer separately.
///
public struct Array : IDisposable
{
///
/// The used to pool data in .
/// Should always clean arrays when returning!
///
private static readonly ArrayPool ArrayPool = ArrayPool.Create();
///
/// Initializes a new instance of the struct. Rents a buffer.
///
/// The number of valid -s
public Array(int count)
{
this.Count = count;
this.Buffer = ArrayPool.Rent(count);
}
///
/// Gets the number of actual -s inside
///
public int Count { get; }
///
/// Gets the rented buffer.
///
public DecodedBlockMemento[] Buffer { get; private set; }
///
/// Returns the rented buffer to the pool.
///
public void Dispose()
{
if (this.Buffer != null)
{
ArrayPool.Return(this.Buffer, true);
this.Buffer = null;
}
}
}
}
}