BitMiracle.LibJpeg.Classic.Internal.huff_entropy_encoder.htest_one_block C# (CSharp) Méthode

htest_one_block() private méthode

Huffman coding optimization. We first scan the supplied data and count the number of uses of each symbol that is to be Huffman-coded. (This process MUST agree with the code above.) Then we build a Huffman coding tree for the observed counts. Symbols which are not needed at all for the particular image are not assigned any code, which saves space in the DHT marker as well as in the compressed data.
private htest_one_block ( short block, int last_dc_val, long dc_counts, long ac_counts ) : void
block short
last_dc_val int
dc_counts long
ac_counts long
Résultat void
        private void htest_one_block(short[] block, int last_dc_val, long[] dc_counts, long[] ac_counts)
        {
            /* Encode the DC coefficient difference per section F.1.2.1 */
            int temp = block[0] - last_dc_val;
            if (temp < 0)
                temp = -temp;

            /* Find the number of bits needed for the magnitude of the coefficient */
            int nbits = 0;
            while (temp != 0)
            {
                nbits++;
                temp >>= 1;
            }

            /* Check for out-of-range coefficient values.
             * Since we're encoding a difference, the range limit is twice as much.
             */
            if (nbits > MAX_COEF_BITS + 1)
                m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_BAD_DCT_COEF);

            /* Count the Huffman symbol for the number of bits */
            dc_counts[nbits]++;

            /* Encode the AC coefficients per section F.1.2.2 */
            int r = 0;          /* r = run length of zeros */
            int Se = m_cinfo.lim_Se;
            int[] natural_order = m_cinfo.natural_order;
            for (int k = 1; k <= Se; k++)
            {
                temp = block[natural_order[k]];
                if (temp == 0)
                {
                    r++;
                }
                else
                {
                    /* if run length > 15, must emit special run-length-16 codes (0xF0) */
                    while (r > 15)
                    {
                        ac_counts[0xF0]++;
                        r -= 16;
                    }

                    /* Find the number of bits needed for the magnitude of the coefficient */
                    if (temp < 0)
                        temp = -temp;

                    /* Find the number of bits needed for the magnitude of the coefficient */
                    nbits = 1;      /* there must be at least one 1 bit */
                    while ((temp >>= 1) != 0)
                        nbits++;

                    /* Check for out-of-range coefficient values */
                    if (nbits > MAX_COEF_BITS)
                        m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_BAD_DCT_COEF);

                    /* Count Huffman symbol for run length / number of bits */
                    ac_counts[(r << 4) + nbits]++;

                    r = 0;
                }
            }

            /* If the last coef(s) were zero, emit an end-of-block code */
            if (r > 0)
                ac_counts[0]++;
        }