BitMiracle.LibJpeg.Classic.Internal.huff_entropy_decoder.decode_mcu_full C# (CSharp) Method

decode_mcu_full() private method

Decode one MCU's worth of Huffman-compressed coefficients, full-size blocks.
private decode_mcu_full ( JBLOCK MCU_data ) : bool
MCU_data JBLOCK
return bool
        private bool decode_mcu_full(JBLOCK[] MCU_data)
        {
            /* Process restart marker if needed; may have to suspend */
            if (m_cinfo.m_restart_interval != 0)
            {
                if (m_restarts_to_go == 0)
                {
                    if (!process_restart())
                        return false;
                }
            }

            /* If we've run out of data, just leave the MCU set to zeroes.
             * This way, we return uniform gray for the remainder of the segment.
             */
            if (!m_insufficient_data)
            {
                /* Load up working state */
                int get_buffer;
                int bits_left;
                bitread_working_state br_state = new bitread_working_state();
                BITREAD_LOAD_STATE(m_bitstate, out get_buffer, out bits_left, ref br_state);
                savable_state state = new savable_state();
                state.Assign(m_saved);

                /* Outer loop handles each block in the MCU */

                for (int blkn = 0; blkn < m_cinfo.m_blocks_in_MCU; blkn++)
                {
                    /* Decode a single block's worth of coefficients */

                    /* Section F.2.2.1: decode the DC coefficient difference */
                    d_derived_tbl htbl = m_dc_cur_tbls[blkn];
                    int s;
                    if (!HUFF_DECODE(out s, ref br_state, htbl, ref get_buffer, ref bits_left))
                        return false;

                    htbl = m_ac_cur_tbls[blkn];
                    int k = 1;
                    int coef_limit = this.coef_limit[blkn];
                    bool endThisBlock = false;

                    if (coef_limit != 0)
                    {
                        /* Convert DC difference to actual value, update last_dc_val */
                        if (s != 0)
                        {
                            if (!CHECK_BIT_BUFFER(ref br_state, s, ref get_buffer, ref bits_left))
                                return false;

                            int r = GET_BITS(s, get_buffer, ref bits_left);
                            s = HUFF_EXTEND(r, s);
                        }

                        int ci = m_cinfo.m_MCU_membership[blkn];
                        s += state.last_dc_val[ci];
                        state.last_dc_val[ci] = s;

                        /* Output the DC coefficient */
                        MCU_data[blkn][0] = (short)s;

                        /* Section F.2.2.2: decode the AC coefficients */
                        /* Since zeroes are skipped, output area must be cleared beforehand */
                        for (; k < coef_limit; k++)
                        {
                            if (!HUFF_DECODE(out s, ref br_state, htbl, ref get_buffer, ref bits_left))
                                return false;

                            int r = s >> 4;
                            s &= 15;

                            if (s != 0)
                            {
                                k += r;
                                if (!CHECK_BIT_BUFFER(ref br_state, s, ref get_buffer, ref bits_left))
                                    return false;
                                r = GET_BITS(s, get_buffer, ref bits_left);
                                s = HUFF_EXTEND(r, s);

                                /* Output coefficient in natural (dezigzagged) order.
                                 * Note: the extra entries in jpeg_natural_order[] will save us
                                 * if k >= DCTSIZE2, which could happen if the data is corrupted.
                                 */
                                MCU_data[blkn][JpegUtils.jpeg_natural_order[k]] = (short)s;
                            }
                            else
                            {
                                if (r != 15)
                                {
                                    endThisBlock = true;
                                    break;
                                }

                                k += 15;
                            }
                        }
                    }
                    else
                    {
                        if (s != 0)
                        {
                            if (!CHECK_BIT_BUFFER(ref br_state, s, ref get_buffer, ref bits_left))
                                return false;

                            DROP_BITS(s, ref bits_left);
                        }
                    }

                    if (endThisBlock)
                        continue;

                    /* Section F.2.2.2: decode the AC coefficients */
                    /* In this path we just discard the values */
                    for (; k < JpegConstants.DCTSIZE2; k++)
                    {
                        if (!HUFF_DECODE(out s, ref br_state, htbl, ref get_buffer, ref bits_left))
                            return false;

                        int r = s >> 4;
                        s &= 15;

                        if (s != 0)
                        {
                            k += r;
                            if (!CHECK_BIT_BUFFER(ref br_state, s, ref get_buffer, ref bits_left))
                                return false;

                            DROP_BITS(s, ref bits_left);
                        }
                        else
                        {
                            if (r != 15)
                                break;

                            k += 15;
                        }
                    }
                }

                /* Completed MCU, so update state */
                BITREAD_SAVE_STATE(ref m_bitstate, get_buffer, bits_left);
                m_saved.Assign(state);
            }

            /* Account for restart interval (no-op if not using restarts) */
            m_restarts_to_go--;

            return true;
        }