From 7ff69ccce4fb8fc3dc3dbc6e0eff75c94ab6f219 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Mon, 5 Dec 2016 23:45:28 +1100 Subject: [PATCH] Clean up Block and BlockF --- .../Formats/Jpg/Components/Block.cs | 182 ++++++++++++------ 1 file changed, 121 insertions(+), 61 deletions(-) diff --git a/src/ImageSharp/Formats/Jpg/Components/Block.cs b/src/ImageSharp/Formats/Jpg/Components/Block.cs index 4d4ad4d81..0531153cd 100644 --- a/src/ImageSharp/Formats/Jpg/Components/Block.cs +++ b/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. // + namespace ImageSharp.Formats { using System; @@ -13,42 +14,24 @@ namespace ImageSharp.Formats /// internal struct Block : IDisposable { - private static readonly ArrayPool ArrayPool = ArrayPool.Create(BlockSize, 50); - /// /// Gets the size of the block. /// public const int BlockSize = 64; /// - /// The array of block data. + /// Gets the array of block data. /// 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; - } + /// + /// A pool of reusable buffers. + /// + private static readonly ArrayPool ArrayPool = ArrayPool.Create(BlockSize, 50); + /// + /// Gets a value indicating whether the block is initialized + /// public bool IsInitialized => this.Data != null; /// @@ -73,16 +56,37 @@ namespace ImageSharp.Formats } } - // TODO: Refactor Block.Dispose() callers to always use 'using' or 'finally' statement! - public void Dispose() + /// + /// Creates a new block + /// + /// The + public static Block Create() { - if (this.Data != null) + Block block = default(Block); + block.Init(); + return block; + } + + /// + /// Returns an array of blocks of the given length. + /// + /// The number to create. + /// The + 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; } + /// + /// Disposes of the collection of blocks + /// + /// The blocks. public static void DisposeAll(Block[] blocks) { for (int i = 0; i < blocks.Length; i++) @@ -91,6 +95,28 @@ namespace ImageSharp.Formats } } + /// + /// Initializes the new block. + /// + public void Init() + { + this.Data = ArrayPool.Rent(BlockSize); + } + + /// + 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; + } + } + + /// + /// Clears the block data + /// public void Clear() { for (int i = 0; i < this.Data.Length; i++) @@ -99,6 +125,10 @@ namespace ImageSharp.Formats } } + /// + /// Clones the current block + /// + /// The public Block Clone() { Block clone = Create(); @@ -116,8 +146,6 @@ namespace ImageSharp.Formats /// internal struct BlockF : IDisposable { - private static readonly ArrayPool ArrayPool = ArrayPool.Create(BlockSize, 50); - /// /// Size of the block. /// @@ -128,30 +156,14 @@ namespace ImageSharp.Formats /// 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; - } + /// + /// A pool of reusable buffers. + /// + private static readonly ArrayPool ArrayPool = ArrayPool.Create(BlockSize, 50); + /// + /// Gets a value indicating whether the block is initialized + /// public bool IsInitialized => this.Data != null; /// @@ -176,16 +188,37 @@ namespace ImageSharp.Formats } } - // TODO: Refactor Block.Dispose() callers to always use 'using' or 'finally' statement! - public void Dispose() + /// + /// Creates a new block + /// + /// The + public static BlockF Create() { - if (this.Data != null) + var block = default(BlockF); + block.Init(); + return block; + } + + /// + /// Returns an array of blocks of the given length. + /// + /// The number to create. + /// The + 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; } + /// + /// Disposes of the collection of blocks + /// + /// The blocks. public static void DisposeAll(BlockF[] blocks) { for (int i = 0; i < blocks.Length; i++) @@ -194,6 +227,9 @@ namespace ImageSharp.Formats } } + /// + /// Clears the block data + /// public void Clear() { for (int i = 0; i < this.Data.Length; i++) @@ -202,11 +238,35 @@ namespace ImageSharp.Formats } } + /// + /// Clones the current block + /// + /// The public BlockF Clone() { BlockF clone = Create(); Array.Copy(this.Data, clone.Data, BlockSize); return clone; } + + /// + /// Initializes the new block. + /// + public void Init() + { + // this.Data = new int[BlockSize]; + this.Data = ArrayPool.Rent(BlockSize); + } + + /// + 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; + } + } } } \ No newline at end of file