Browse Source

Improve readability

af/merge-core
James Jackson-South 7 years ago
parent
commit
2c4c26b2d3
  1. 4
      src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanBuffer.cs
  2. 26
      src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanTable.cs
  3. 10
      src/ImageSharp/Formats/Jpeg/JpegConstants.cs

4
src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanBuffer.cs

@ -85,8 +85,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
{ {
// Attempt to load at least the minimum number of required bits into the buffer. // Attempt to load at least the minimum number of required bits into the buffer.
// We fail to do so only if we hit a marker or reach the end of the input stream. // We fail to do so only if we hit a marker or reach the end of the input stream.
this.remainingBits += 48; this.remainingBits += JpegConstants.Huffman.MinBits;
this.data = (this.data << 48) | this.GetBytes(); this.data = (this.data << JpegConstants.Huffman.MinBits) | this.GetBytes();
} }
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]

26
src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanTable.cs

@ -82,12 +82,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
// Figure C.1: make table of Huffman code length for each symbol // Figure C.1: make table of Huffman code length for each symbol
int p = 0; int p = 0;
for (int l = 1; l <= 16; l++) for (int j = 1; j <= 16; j++)
{ {
int i = this.Sizes[l]; int i = this.Sizes[j];
while (i-- != 0) while (i-- != 0)
{ {
huffSize[p++] = (char)l; huffSize[p++] = (char)j;
} }
} }
@ -111,20 +111,19 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
// Figure F.15: generate decoding tables for bit-sequential decoding // Figure F.15: generate decoding tables for bit-sequential decoding
p = 0; p = 0;
for (int l = 1; l <= 16; l++) for (int j = 1; j <= 16; j++)
{ {
if (this.Sizes[l] != 0) if (this.Sizes[j] != 0)
{ {
int offset = p - (int)huffCode[p]; this.ValOffset[j] = p - (int)huffCode[p];
this.ValOffset[l] = offset; p += this.Sizes[j];
p += this.Sizes[l]; this.MaxCode[j] = huffCode[p - 1]; // Maximum code of length l
this.MaxCode[l] = huffCode[p - 1]; // Maximum code of length l this.MaxCode[j] <<= JpegConstants.Huffman.RegisterSize - j; // Left justify
this.MaxCode[l] <<= 64 - l; // Left justify this.MaxCode[j] |= (1ul << (JpegConstants.Huffman.RegisterSize - j)) - 1;
this.MaxCode[l] |= (1ul << (64 - l)) - 1;
} }
else else
{ {
this.MaxCode[l] = 0; this.MaxCode[j] = 0;
} }
} }
@ -142,11 +141,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
p = 0; p = 0;
for (int length = 1; length <= JpegConstants.Huffman.LookupBits; length++) for (int length = 1; length <= JpegConstants.Huffman.LookupBits; length++)
{ {
int jShift = JpegConstants.Huffman.LookupBits - length;
for (int i = 1; i <= this.Sizes[length]; i++, p++) for (int i = 1; i <= this.Sizes[length]; i++, p++)
{ {
// length = current code's length, p = its index in huffCode[] & Values[]. // length = current code's length, p = its index in huffCode[] & Values[].
// Generate left-justified code followed by all possible bit sequences // Generate left-justified code followed by all possible bit sequences
int lookBits = (int)(huffCode[p] << (JpegConstants.Huffman.LookupBits - length)); int lookBits = (int)(huffCode[p] << jShift);
for (int ctr = 1 << (JpegConstants.Huffman.LookupBits - length); ctr > 0; ctr--) for (int ctr = 1 << (JpegConstants.Huffman.LookupBits - length); ctr > 0; ctr--)
{ {
this.LookaheadSize[lookBits] = (byte)length; this.LookaheadSize[lookBits] = (byte)length;

10
src/ImageSharp/Formats/Jpeg/JpegConstants.cs

@ -1,7 +1,8 @@
// Copyright (c) Six Labors and contributors. // Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System.Collections.Generic; using System.Collections.Generic;
using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder;
namespace SixLabors.ImageSharp.Formats.Jpeg namespace SixLabors.ImageSharp.Formats.Jpeg
{ {
@ -249,6 +250,11 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
/// </summary> /// </summary>
public const int RegisterSize = 64; public const int RegisterSize = 64;
/// <summary>
/// The minimum number of bits required by the <see cref="HuffmanScanBuffer"/>.
/// </summary>
public const int MinBits = 48;
/// <summary> /// <summary>
/// If the next Huffman code is no more than this number of bits, we can obtain its length /// If the next Huffman code is no more than this number of bits, we can obtain its length
/// and the corresponding symbol directly from this tables. /// and the corresponding symbol directly from this tables.
@ -266,4 +272,4 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
public const int LookupSize = 1 << LookupBits; public const int LookupSize = 1 << LookupBits;
} }
} }
} }

Loading…
Cancel
Save