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]++;
}