Browse Source

Clean up Block and BlockF

pull/33/head
James Jackson-South 9 years ago
parent
commit
7ff69ccce4
  1. 182
      src/ImageSharp/Formats/Jpg/Components/Block.cs

182
src/ImageSharp/Formats/Jpg/Components/Block.cs

@ -2,6 +2,7 @@
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Formats
{
using System;
@ -13,42 +14,24 @@ namespace ImageSharp.Formats
/// </summary>
internal struct Block : IDisposable
{
private static readonly ArrayPool<int> ArrayPool = ArrayPool<int>.Create(BlockSize, 50);
/// <summary>
/// Gets the size of the block.
/// </summary>
public const int BlockSize = 64;
/// <summary>
/// The array of block data.
/// Gets the array of block data.
/// </summary>
public int[] Data;
public void Init()
{
// this.Data = new int[BlockSize];
this.Data = ArrayPool.Rent(BlockSize);
}
public static Block Create()
{
var block = new Block();
block.Init();
return block;
}
public static Block[] CreateArray(int size)
{
Block[] result = new Block[size];
for (int i = 0; i < result.Length; i++)
{
result[i].Init();
}
return result;
}
/// <summary>
/// A pool of reusable buffers.
/// </summary>
private static readonly ArrayPool<int> ArrayPool = ArrayPool<int>.Create(BlockSize, 50);
/// <summary>
/// Gets a value indicating whether the block is initialized
/// </summary>
public bool IsInitialized => this.Data != null;
/// <summary>
@ -73,16 +56,37 @@ namespace ImageSharp.Formats
}
}
// TODO: Refactor Block.Dispose() callers to always use 'using' or 'finally' statement!
public void Dispose()
/// <summary>
/// Creates a new block
/// </summary>
/// <returns>The <see cref="Block"/></returns>
public static Block Create()
{
if (this.Data != null)
Block block = default(Block);
block.Init();
return block;
}
/// <summary>
/// Returns an array of blocks of the given length.
/// </summary>
/// <param name="count">The number to create.</param>
/// <returns>The <see cref="T:Block[]"/></returns>
public static Block[] CreateArray(int count)
{
Block[] result = new Block[count];
for (int i = 0; i < result.Length; i++)
{
ArrayPool.Return(this.Data, true);
this.Data = null;
result[i].Init();
}
return result;
}
/// <summary>
/// Disposes of the collection of blocks
/// </summary>
/// <param name="blocks">The blocks.</param>
public static void DisposeAll(Block[] blocks)
{
for (int i = 0; i < blocks.Length; i++)
@ -91,6 +95,28 @@ namespace ImageSharp.Formats
}
}
/// <summary>
/// Initializes the new block.
/// </summary>
public void Init()
{
this.Data = ArrayPool.Rent(BlockSize);
}
/// <inheritdoc />
public void Dispose()
{
// TODO: Refactor Block.Dispose() callers to always use 'using' or 'finally' statement!
if (this.Data != null)
{
ArrayPool.Return(this.Data, true);
this.Data = null;
}
}
/// <summary>
/// Clears the block data
/// </summary>
public void Clear()
{
for (int i = 0; i < this.Data.Length; i++)
@ -99,6 +125,10 @@ namespace ImageSharp.Formats
}
}
/// <summary>
/// Clones the current block
/// </summary>
/// <returns>The <see cref="Block"/></returns>
public Block Clone()
{
Block clone = Create();
@ -116,8 +146,6 @@ namespace ImageSharp.Formats
/// </summary>
internal struct BlockF : IDisposable
{
private static readonly ArrayPool<float> ArrayPool = ArrayPool<float>.Create(BlockSize, 50);
/// <summary>
/// Size of the block.
/// </summary>
@ -128,30 +156,14 @@ namespace ImageSharp.Formats
/// </summary>
public float[] Data;
public void Init()
{
// this.Data = new int[BlockSize];
this.Data = ArrayPool.Rent(BlockSize);
}
public static BlockF Create()
{
var block = new BlockF();
block.Init();
return block;
}
public static BlockF[] CreateArray(int size)
{
BlockF[] result = new BlockF[size];
for (int i = 0; i < result.Length; i++)
{
result[i].Init();
}
return result;
}
/// <summary>
/// A pool of reusable buffers.
/// </summary>
private static readonly ArrayPool<float> ArrayPool = ArrayPool<float>.Create(BlockSize, 50);
/// <summary>
/// Gets a value indicating whether the block is initialized
/// </summary>
public bool IsInitialized => this.Data != null;
/// <summary>
@ -176,16 +188,37 @@ namespace ImageSharp.Formats
}
}
// TODO: Refactor Block.Dispose() callers to always use 'using' or 'finally' statement!
public void Dispose()
/// <summary>
/// Creates a new block
/// </summary>
/// <returns>The <see cref="BlockF"/></returns>
public static BlockF Create()
{
if (this.Data != null)
var block = default(BlockF);
block.Init();
return block;
}
/// <summary>
/// Returns an array of blocks of the given length.
/// </summary>
/// <param name="count">The number to create.</param>
/// <returns>The <see cref="T:BlockF[]"/></returns>
public static BlockF[] CreateArray(int count)
{
BlockF[] result = new BlockF[count];
for (int i = 0; i < result.Length; i++)
{
ArrayPool.Return(this.Data, true);
this.Data = null;
result[i].Init();
}
return result;
}
/// <summary>
/// Disposes of the collection of blocks
/// </summary>
/// <param name="blocks">The blocks.</param>
public static void DisposeAll(BlockF[] blocks)
{
for (int i = 0; i < blocks.Length; i++)
@ -194,6 +227,9 @@ namespace ImageSharp.Formats
}
}
/// <summary>
/// Clears the block data
/// </summary>
public void Clear()
{
for (int i = 0; i < this.Data.Length; i++)
@ -202,11 +238,35 @@ namespace ImageSharp.Formats
}
}
/// <summary>
/// Clones the current block
/// </summary>
/// <returns>The <see cref="Block"/></returns>
public BlockF Clone()
{
BlockF clone = Create();
Array.Copy(this.Data, clone.Data, BlockSize);
return clone;
}
/// <summary>
/// Initializes the new block.
/// </summary>
public void Init()
{
// this.Data = new int[BlockSize];
this.Data = ArrayPool.Rent(BlockSize);
}
/// <inheritdoc />
public void Dispose()
{
// TODO: Refactor Block.Dispose() callers to always use 'using' or 'finally' statement!
if (this.Data != null)
{
ArrayPool.Return(this.Data, true);
this.Data = null;
}
}
}
}
Loading…
Cancel
Save