BitMiracle.LibJpeg.Classic.Internal.jpeg_marker_reader.get_dht C# (CSharp) Method

get_dht() private method

Process a DHT marker
private get_dht ( ) : bool
return bool
        private bool get_dht()
        {
            int length;
            if (!m_cinfo.m_src.GetTwoBytes(out length))
                return false;

            length -= 2;

            byte[] bits = new byte[17];
            byte[] huffval = new byte[256];
            while (length > 16)
            {
                int index;
                if (!m_cinfo.m_src.GetByte(out index))
                    return false;

                m_cinfo.TRACEMS(1, J_MESSAGE_CODE.JTRC_DHT, index);

                bits[0] = 0;
                int count = 0;
                for (int i = 1; i <= 16; i++)
                {
                    int temp = 0;
                    if (!m_cinfo.m_src.GetByte(out temp))
                        return false;

                    bits[i] = (byte)temp;
                    count += bits[i];
                }

                length -= 1 + 16;

                m_cinfo.TRACEMS(2, J_MESSAGE_CODE.JTRC_HUFFBITS, bits[1], bits[2], bits[3], bits[4], bits[5], bits[6], bits[7], bits[8]);
                m_cinfo.TRACEMS(2, J_MESSAGE_CODE.JTRC_HUFFBITS, bits[9], bits[10], bits[11], bits[12], bits[13], bits[14], bits[15], bits[16]);

                /* Here we just do minimal validation of the counts to avoid walking
                 * off the end of our table space. huff_entropy_decoder will check more carefully.
                 */
                if (count > 256 || count > length)
                    m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_BAD_HUFF_TABLE);

                for (int i = 0; i < count; i++)
                {
                    int temp = 0;
                    if (!m_cinfo.m_src.GetByte(out temp))
                        return false;

                    huffval[i] = (byte)temp;
                }

                length -= count;

                JHUFF_TBL htblptr = null;
                if ((index & 0x10) != 0)
                {
                    /* AC table definition */
                    index -= 0x10;
                    if (m_cinfo.m_ac_huff_tbl_ptrs[index] == null)
                        m_cinfo.m_ac_huff_tbl_ptrs[index] = new JHUFF_TBL();

                    htblptr = m_cinfo.m_ac_huff_tbl_ptrs[index];
                }
                else
                {
                    /* DC table definition */
                    if (m_cinfo.m_dc_huff_tbl_ptrs[index] == null)
                        m_cinfo.m_dc_huff_tbl_ptrs[index] = new JHUFF_TBL();

                    htblptr = m_cinfo.m_dc_huff_tbl_ptrs[index];
                }

                if (index < 0 || index >= JpegConstants.NUM_HUFF_TBLS)
                    m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_DHT_INDEX, index);

                Buffer.BlockCopy(bits, 0, htblptr.Bits, 0, htblptr.Bits.Length);
                Buffer.BlockCopy(huffval, 0, htblptr.Huffval, 0, htblptr.Huffval.Length);
            }

            if (length != 0)
                m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_BAD_LENGTH);

            return true;
        }