17 #define JPEG_INTERNALS
40 #ifndef NO_STRUCT_ASSIGN
41 #define ASSIGN_STATE(dest,src) ((dest) = (src))
43 #if MAX_COMPS_IN_SCAN == 4
44 #define ASSIGN_STATE(dest,src) \
45 ((dest).put_buffer = (src).put_buffer, \
46 (dest).put_bits = (src).put_bits, \
47 (dest).last_dc_val[0] = (src).last_dc_val[0], \
48 (dest).last_dc_val[1] = (src).last_dc_val[1], \
49 (dest).last_dc_val[2] = (src).last_dc_val[2], \
50 (dest).last_dc_val[3] = (src).last_dc_val[3])
68 #ifdef ENTROPY_OPT_SUPPORTED
92 #ifdef ENTROPY_OPT_SUPPORTED
109 int ci, dctbl, actbl;
112 if (gather_statistics) {
113 #ifdef ENTROPY_OPT_SUPPORTED
117 ERREXIT(cinfo, JERR_NOT_COMPILED);
128 if (gather_statistics) {
129 #ifdef ENTROPY_OPT_SUPPORTED
133 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
135 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
184 int p,
i, l, lastp, si, maxsymbol;
186 unsigned int huffcode[257];
197 isDC ? cinfo->dc_huff_tbl_ptrs[
tblno] : cinfo->ac_huff_tbl_ptrs[
tblno];
211 for (l = 1; l <= 16; l++) {
213 if (i < 0 || p + i > 256)
214 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
216 huffsize[p++] = (char) l;
227 while (huffsize[p]) {
228 while (((
int) huffsize[p]) == si) {
229 huffcode[p++] =
code;
236 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
255 maxsymbol =
isDC ? 15 : 255;
257 for (p = 0; p < lastp; p++) {
259 if (i < 0 || i > maxsymbol || dtbl->
ehufsi[
i])
260 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
270 #define emit_byte(state,val,action) \
271 { *(state)->next_output_byte++ = (JOCTET) (val); \
272 if (--(state)->free_in_buffer == 0) \
273 if (! dump_buffer(state)) \
283 if (! (*dest->empty_output_buffer) (state->cinfo))
307 register int put_bits = state->cur.put_bits;
311 ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
317 put_buffer <<= 24 - put_bits;
319 put_buffer |= state->cur.put_buffer;
321 while (put_bits >= 8) {
322 int c = (
int) ((put_buffer >> 16) & 0xFF);
332 state->cur.put_buffer = put_buffer;
333 state->cur.put_bits = put_bits;
344 state->cur.put_buffer = 0;
345 state->cur.put_bits = 0;
356 register int temp, temp2;
358 register int k, r,
i;
362 temp = temp2 = block[0] - last_dc_val;
381 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
403 if (!
emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
421 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
425 if (!
emit_bits(state, actbl->ehufco[
i], actbl->ehufsi[
i]))
439 if (!
emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
462 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
463 state->cur.last_dc_val[ci] = 0;
484 state.next_output_byte = cinfo->dest->next_output_byte;
485 state.free_in_buffer = cinfo->dest->free_in_buffer;
490 if (cinfo->restart_interval) {
497 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
498 ci = cinfo->MCU_membership[blkn];
499 compptr = cinfo->cur_comp_info[ci];
501 MCU_data[blkn][0], state.cur.last_dc_val[ci],
506 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
510 cinfo->dest->next_output_byte = state.next_output_byte;
511 cinfo->dest->free_in_buffer = state.free_in_buffer;
515 if (cinfo->restart_interval) {
540 state.free_in_buffer = cinfo->dest->free_in_buffer;
546 ERREXIT(cinfo, JERR_CANT_SUSPEND);
549 cinfo->dest->next_output_byte = state.next_output_byte;
550 cinfo->dest->free_in_buffer = state.free_in_buffer;
566 #ifdef ENTROPY_OPT_SUPPORTED
573 long dc_counts[],
long ac_counts[])
581 temp = block[0] - last_dc_val;
595 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
624 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
627 ac_counts[(r << 4) +
nbits]++;
652 if (cinfo->restart_interval) {
655 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
663 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
664 ci = cinfo->MCU_membership[blkn];
665 compptr = cinfo->cur_comp_info[ci];
719 for (
i = 0;
i < 257;
i++)
735 for (
i = 0;
i <= 256;
i++) {
746 for (
i = 0;
i <= 256;
i++) {
763 while (others[c1] >= 0) {
772 while (others[c2] >= 0) {
779 for (
i = 0;
i <= 256;
i++) {
784 ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
802 while (bits[
i] > 0) {
828 for (j = 0; j <= 255; j++) {
829 if (codesize[j] ==
i) {
849 int ci, dctbl, actbl;
861 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
862 compptr = cinfo->cur_comp_info[ci];
865 if (! did_dc[dctbl]) {
866 htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
867 if (*htblptr == NULL)
870 did_dc[dctbl] =
TRUE;
872 if (! did_ac[actbl]) {
873 htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
874 if (*htblptr == NULL)
877 did_ac[actbl] =
TRUE;
900 entropy->
pub.start_pass = start_pass_huff;
905 #ifdef ENTROPY_OPT_SUPPORTED