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

get_dqt() private method

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

            length -= 2;
            while (length > 0)
            {
                length--;

                int n;
                if (!m_cinfo.m_src.GetByte(out n))
                    return false;

                int prec = n >> 4;
                n &= 0x0F;

                m_cinfo.TRACEMS(1, J_MESSAGE_CODE.JTRC_DQT, n, prec);

                if (n >= JpegConstants.NUM_QUANT_TBLS)
                    m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_DQT_INDEX, n);

                if (m_cinfo.m_quant_tbl_ptrs[n] == null)
                    m_cinfo.m_quant_tbl_ptrs[n] = new JQUANT_TBL();

                JQUANT_TBL quant_ptr = m_cinfo.m_quant_tbl_ptrs[n];

                int count;
                if (prec != 0)
                {
                    if (length < JpegConstants.DCTSIZE2 * 2)
                    {
                        /* Initialize full table for safety. */
                        for (int i = 0; i < JpegConstants.DCTSIZE2; i++)
                        {
                            quant_ptr.quantval[i] = 1;
                        }
                        count = length >> 1;
                    }
                    else
                    {
                        count = JpegConstants.DCTSIZE2;
                    }
                }
                else
                {
                    if (length < JpegConstants.DCTSIZE2)
                    {
                        /* Initialize full table for safety. */
                        for (int i = 0; i < JpegConstants.DCTSIZE2; i++)
                        {
                            quant_ptr.quantval[i] = 1;
                        }
                        count = length;
                    }
                    else
                    {
                        count = JpegConstants.DCTSIZE2;
                    }
                }

                int[] natural_order;
                switch (count)
                {
                    case (2 * 2):
                        natural_order = JpegUtils.jpeg_natural_order2;
                        break;
                    case (3 * 3):
                        natural_order = JpegUtils.jpeg_natural_order3;
                        break;
                    case (4 * 4):
                        natural_order = JpegUtils.jpeg_natural_order4;
                        break;
                    case (5 * 5):
                        natural_order = JpegUtils.jpeg_natural_order5;
                        break;
                    case (6 * 6):
                        natural_order = JpegUtils.jpeg_natural_order6;
                        break;
                    case (7 * 7):
                        natural_order = JpegUtils.jpeg_natural_order7;
                        break;
                    default:
                        natural_order = JpegUtils.jpeg_natural_order;
                        break;
                }

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

                        tmp = temp;
                    }
                    else
                    {
                        int temp = 0;
                        if (!m_cinfo.m_src.GetByte(out temp))
                            return false;

                        tmp = temp;
                    }

                    /* We convert the zigzag-order table to natural array order. */
                    quant_ptr.quantval[natural_order[i]] = (short)tmp;
                }

                if (m_cinfo.m_err.m_trace_level >= 2)
                {
                    for (int i = 0; i < JpegConstants.DCTSIZE2; i += 8)
                    {
                        m_cinfo.TRACEMS(2, J_MESSAGE_CODE.JTRC_QUANTVALS, quant_ptr.quantval[i],
                            quant_ptr.quantval[i + 1], quant_ptr.quantval[i + 2],
                            quant_ptr.quantval[i + 3], quant_ptr.quantval[i + 4],
                            quant_ptr.quantval[i + 5], quant_ptr.quantval[i + 6], quant_ptr.quantval[i + 7]);
                    }
                }

                length -= count;
                if (prec != 0)
                    length -= count;
            }

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

            return true;
        }