From 1b4fc8354cb1e2465d97d210589595ef0a55dd48 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 24 Jan 2017 03:09:52 +0100 Subject: [PATCH] HuffmanTree cleanup --- .../Components/Decoder/HuffmanTree.cs | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/ImageSharp.Formats.Jpeg/Components/Decoder/HuffmanTree.cs b/src/ImageSharp.Formats.Jpeg/Components/Decoder/HuffmanTree.cs index abe7b1106f..236cffbdf2 100644 --- a/src/ImageSharp.Formats.Jpeg/Components/Decoder/HuffmanTree.cs +++ b/src/ImageSharp.Formats.Jpeg/Components/Decoder/HuffmanTree.cs @@ -63,11 +63,6 @@ namespace ImageSharp.Formats.Jpg /// /// Gets the the decoded values, sorted by their encoding. /// - public byte[] Values; - - /// - /// Same as , converted to int-s - /// public int[] ValuesAsInt; /// @@ -87,11 +82,11 @@ namespace ImageSharp.Formats.Jpg /// public int[] Indices; - private static readonly ArrayPool IntBuffer256 = ArrayPool.Create(MaxNCodes, 50); + private static readonly ArrayPool IntPool256 = ArrayPool.Create(MaxNCodes, 50); - private static readonly ArrayPool ByteBuffer256 = ArrayPool.Create(MaxNCodes, 50); + private static readonly ArrayPool BytePool256 = ArrayPool.Create(MaxNCodes, 50); - private static readonly ArrayPool CodesBuffer16 = ArrayPool.Create(MaxCodeLength, 50); + private static readonly ArrayPool CodesPool16 = ArrayPool.Create(MaxCodeLength, 50); /// /// Creates and initializes an array of instances of size @@ -116,12 +111,11 @@ namespace ImageSharp.Formats.Jpg /// public void Dispose() { - IntBuffer256.Return(this.Lut, true); - IntBuffer256.Return(this.ValuesAsInt, true); - ByteBuffer256.Return(this.Values, true); - CodesBuffer16.Return(this.MinCodes, true); - CodesBuffer16.Return(this.MaxCodes, true); - CodesBuffer16.Return(this.Indices, true); + IntPool256.Return(this.Lut, true); + IntPool256.Return(this.ValuesAsInt, true); + CodesPool16.Return(this.MinCodes, true); + CodesPool16.Return(this.MaxCodes, true); + CodesPool16.Return(this.Indices, true); } /// @@ -163,11 +157,20 @@ namespace ImageSharp.Formats.Jpg throw new ImageFormatException("DHT has wrong length"); } - inputProcessor.ReadFull(this.Values, 0, this.Length); + byte[] values = null; + try + { + values = BytePool256.Rent(MaxNCodes); + inputProcessor.ReadFull(values, 0, this.Length); - for (int i = 0; i < this.Values.Length; i++) + for (int i = 0; i < values.Length; i++) + { + this.ValuesAsInt[i] = values[i]; + } + } + finally { - this.ValuesAsInt[i] = this.Values[i]; + BytePool256.Return(values, true); } // Derive the look-up table. @@ -230,11 +233,11 @@ namespace ImageSharp.Formats.Jpg /// Gets the value for the given code and index. /// /// The code - /// The index + /// The code length /// The value - public int GetValue(int code, int i) + public int GetValue(int code, int codeLength) { - return this.ValuesAsInt[this.Indices[i] + code - this.MinCodes[i]]; + return this.ValuesAsInt[this.Indices[codeLength] + code - this.MinCodes[codeLength]]; } /// @@ -242,12 +245,11 @@ namespace ImageSharp.Formats.Jpg /// private void Init() { - this.Lut = IntBuffer256.Rent(MaxNCodes); - this.Values = ByteBuffer256.Rent(MaxNCodes); - this.ValuesAsInt = IntBuffer256.Rent(MaxNCodes); - this.MinCodes = CodesBuffer16.Rent(MaxCodeLength); - this.MaxCodes = CodesBuffer16.Rent(MaxCodeLength); - this.Indices = CodesBuffer16.Rent(MaxCodeLength); + this.Lut = IntPool256.Rent(MaxNCodes); + this.ValuesAsInt = IntPool256.Rent(MaxNCodes); + this.MinCodes = CodesPool16.Rent(MaxCodeLength); + this.MaxCodes = CodesPool16.Rent(MaxCodeLength); + this.Indices = CodesPool16.Rent(MaxCodeLength); } } } \ No newline at end of file