Example #1
0
        /// <summary>
        /// Creates and initializes an array of <see cref="OrigHuffmanTree" /> instances of size <see cref="NumberOfTrees" />
        /// </summary>
        /// <returns>An array of <see cref="OrigHuffmanTree" /> instances representing the Huffman tables</returns>
        public static                            OrigHuffmanTree[] CreateHuffmanTrees()
        {
            OrigHuffmanTree[] result = new OrigHuffmanTree[NumberOfTrees];
            for (int i = 0; i < MaxTc + 1; i++)
            {
                for (int j = 0; j < MaxTh + 1; j++)
                {
                    result[(i * ThRowSize) + j].Init();
                }
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Extracts the next Huffman-coded value from the bit-stream into result, decoded according to the given value.
        /// </summary>
        /// <param name="huffmanTree">The huffman value</param>
        /// <param name="result">The decoded <see cref="byte" /></param>
        /// <returns>The <see cref="OrigDecoderErrorCode"/></returns>
        public OrigDecoderErrorCode DecodeHuffmanUnsafe(ref OrigHuffmanTree huffmanTree, out int result)
        {
            result = 0;

            if (huffmanTree.Length == 0)
            {
                DecoderThrowHelper.ThrowImageFormatException.UninitializedHuffmanTable();
            }

            if (this.Bits.UnreadBits < 8)
            {
                this.LastErrorCode = this.Bits.Ensure8BitsUnsafe(ref this);

                if (this.LastErrorCode == OrigDecoderErrorCode.NoError)
                {
                    int lutIndex = (this.Bits.Accumulator >> (this.Bits.UnreadBits - OrigHuffmanTree.LutSizeLog2)) & 0xFF;
                    int v        = huffmanTree.Lut[lutIndex];

                    if (v != 0)
                    {
                        int n = (v & 0xFF) - 1;
                        this.Bits.UnreadBits -= n;
                        this.Bits.Mask      >>= n;
                        result = v >> 8;
                        return(this.LastErrorCode);
                    }
                }
                else
                {
                    this.UnreadByteStuffedByte();
                    return(this.LastErrorCode);
                }
            }

            int code = 0;

            for (int i = 0; i < OrigHuffmanTree.MaxCodeLength; i++)
            {
                if (this.Bits.UnreadBits == 0)
                {
                    this.LastErrorCode = this.Bits.EnsureNBitsUnsafe(1, ref this);

                    if (this.HasError)
                    {
                        return(this.LastErrorCode);
                    }
                }

                if ((this.Bits.Accumulator & this.Bits.Mask) != 0)
                {
                    code |= 1;
                }

                this.Bits.UnreadBits--;
                this.Bits.Mask >>= 1;

                if (code <= huffmanTree.MaxCodes[i])
                {
                    result = huffmanTree.GetValue(code, i);
                    return(this.LastErrorCode = OrigDecoderErrorCode.NoError);
                }

                code <<= 1;
            }

            // Unrecoverable error, throwing:
            DecoderThrowHelper.ThrowImageFormatException.BadHuffmanCode();

            // DUMMY RETURN! C# doesn't know we have thrown an exception!
            return(OrigDecoderErrorCode.NoError);
        }