(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
SIZEOF(c_derived_tbl));
dtbl = *pdtbl;
-
+
/* Figure C.1: make table of Huffman code length for each symbol */
p = 0;
}
huffsize[p] = 0;
lastp = p;
-
+
/* Figure C.2: generate the codes themselves */
/* We also validate that the counts represent a legal Huffman code tree. */
code <<= 1;
si++;
}
-
+
/* Figure C.3: generate encoding tables */
/* These are code and size indexed by symbol value */
ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
-
+
put_bits += size; /* new number of bits in buffer */
-
+
put_buffer <<= 24 - put_bits; /* align incoming bits */
put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
-
+
while (put_bits >= 8) {
int c = (int) ((put_buffer >> 16) & 0xFF);
-
+
emit_byte(state, c, return FALSE);
if (c == 0xFF) { /* need to stuff a zero byte? */
emit_byte(state, 0, return FALSE);
register int temp, temp2;
register int nbits;
register int k, r, i;
-
+
/* Encode the DC coefficient difference per section F.1.2.1 */
-
+
temp = temp2 = block[0] - last_dc_val;
if (temp < 0) {
/* This code assumes we are on a two's complement machine */
temp2--;
}
-
+
/* Find the number of bits needed for the magnitude of the coefficient */
nbits = 0;
while (temp) {
*/
if (nbits > MAX_COEF_BITS+1)
ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
-
+
/* Emit the Huffman-coded symbol for the number of bits */
if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
return FALSE;
return FALSE;
/* Encode the AC coefficients per section F.1.2.2 */
-
+
r = 0; /* r = run length of zeros */
-
+
for (k = 1; k < DCTSIZE2; k++) {
if ((temp = block[jpeg_natural_order[k]]) == 0) {
r++;
/* This code assumes we are on a two's complement machine */
temp2--;
}
-
+
/* 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))
/* Check for out-of-range coefficient values */
if (nbits > MAX_COEF_BITS)
ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
-
+
/* Emit Huffman symbol for run length / number of bits */
i = (r << 4) + nbits;
if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
/* or the complement of its magnitude, if negative. */
if (! emit_bits(state, (unsigned int) temp2, nbits))
return FALSE;
-
+
r = 0;
}
}
register int temp;
register int nbits;
register int k, r;
-
+
/* Encode the DC coefficient difference per section F.1.2.1 */
-
+
temp = block[0] - last_dc_val;
if (temp < 0)
temp = -temp;
-
+
/* Find the number of bits needed for the magnitude of the coefficient */
nbits = 0;
while (temp) {
/* Count the Huffman symbol for the number of bits */
dc_counts[nbits]++;
-
+
/* Encode the AC coefficients per section F.1.2.2 */
-
+
r = 0; /* r = run length of zeros */
-
+
for (k = 1; k < DCTSIZE2; k++) {
if ((temp = block[jpeg_natural_order[k]]) == 0) {
r++;
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))
/* Check for out-of-range coefficient values */
if (nbits > MAX_COEF_BITS)
ERREXIT(cinfo, JERR_BAD_DCT_COEF);
-
+
/* Count Huffman symbol for run length / number of bits */
ac_counts[(r << 4) + nbits]++;
-
+
r = 0;
}
}
MEMZERO(codesize, SIZEOF(codesize));
for (i = 0; i < 257; i++)
others[i] = -1; /* init links to empty */
-
+
freq[256] = 1; /* make sure 256 has a nonzero count */
/* Including the pseudo-symbol 256 in the Huffman procedure guarantees
* that no real symbol is given code-value of all ones, because 256
/* Done if we've merged everything into one frequency */
if (c2 < 0)
break;
-
+
/* Else merge the two counts/trees */
freq[c1] += freq[c2];
freq[c2] = 0;
c1 = others[c1];
codesize[c1]++;
}
-
+
others[c1] = c2; /* chain c2 onto c1's tree branch */
-
+
/* Increment the codesize of everything in c2's tree branch */
codesize[c2]++;
while (others[c2] >= 0) {
* shortest nonzero BITS entry is converted into a prefix for two code words
* one bit longer.
*/
-
+
for (i = MAX_CLEN; i > 16; i--) {
while (bits[i] > 0) {
j = i - 2; /* find length of new prefix to be used */
while (bits[j] == 0)
j--;
-
+
bits[i] -= 2; /* remove two symbols */
bits[i-1]++; /* one goes in this length */
bits[j+1] += 2; /* two new symbols in this length */
while (bits[i] == 0) /* find largest codelength still in use */
i--;
bits[i]--;
-
+
/* Return final symbol counts (only for lengths 0..16) */
MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
-
+
/* Return a list of the symbols sorted by code length */
/* It's not real clear to me why we don't need to consider the codelength
* changes made above, but the JPEG spec seems to think this works.