10 #define JPEG_INTERNALS 33 #ifndef NO_STRUCT_ASSIGN 34 #define ASSIGN_STATE(dest, src) ((dest) = (src)) 36 #if MAX_COMPS_IN_SCAN == 4 37 #define ASSIGN_STATE(dest, src) \ 38 ((dest).put_buffer = (src).put_buffer, (dest).put_bits = (src).put_bits, \ 39 (dest).last_dc_val[0] = (src).last_dc_val[0], \ 40 (dest).last_dc_val[1] = (src).last_dc_val[1], \ 41 (dest).last_dc_val[2] = (src).last_dc_val[2], \ 42 (dest).last_dc_val[3] = (src).last_dc_val[3]) 60 #ifdef ENTROPY_OPT_SUPPORTED 84 #ifdef ENTROPY_OPT_SUPPORTED 100 int ci, dctbl, actbl;
103 if (gather_statistics)
105 #ifdef ENTROPY_OPT_SUPPORTED 109 ERREXIT(cinfo, JERR_NOT_COMPILED);
118 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
120 compptr = cinfo->cur_comp_info[ci];
123 if (gather_statistics)
125 #ifdef ENTROPY_OPT_SUPPORTED 129 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
131 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
137 (
long*)(*cinfo->mem->alloc_small)(
142 (
long*)(*cinfo->mem->alloc_small)(
183 int p, i, l, lastp, si, maxsymbol;
185 unsigned int huffcode[257];
194 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
196 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
197 if (htbl ==
nullptr)
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
200 if (*pdtbl ==
nullptr)
208 for (l = 1; l <= 16; l++)
210 i = (int)htbl->
bits[l];
211 if (i < 0 || p + i > 256)
212 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
213 while (i--) huffsize[
p++] = (char)l;
226 while (((
int)huffsize[
p]) == si)
228 huffcode[
p++] =
code;
236 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
255 maxsymbol = isDC ? 15 : 255;
257 for (
p = 0;
p < lastp;
p++)
260 if (i < 0 || i > maxsymbol || dtbl->
ehufsi[i])
261 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
270 #define emit_byte(state, val, action) \ 272 *(state)->next_output_byte++ = (JOCTET)(val); \ 273 if (--(state)->free_in_buffer == 0) \ 274 if (!dump_buffer(state)) \ 286 if (!(*dest->empty_output_buffer)(state->cinfo))
return FALSE;
308 int put_bits = state->cur.put_bits;
311 if (
size == 0)
ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
318 put_buffer <<= 24 - put_bits;
321 state->cur.put_buffer;
323 while (put_bits >= 8)
325 int c = (int)((put_buffer >> 16) & 0xFF);
336 state->cur.put_buffer = put_buffer;
337 state->cur.put_bits = put_bits;
347 state->cur.put_buffer = 0;
348 state->cur.put_bits = 0;
365 temp = temp2 = block[0] - last_dc_val;
389 if (!
emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
413 if (!
emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
429 while ((temp >>= 1)) nbits++;
434 i = (
r << 4) + nbits;
435 if (!
emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
448 if (!
emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
return FALSE;
468 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
469 state->cur.last_dc_val[ci] = 0;
490 state.next_output_byte = cinfo->dest->next_output_byte;
491 state.free_in_buffer = cinfo->dest->free_in_buffer;
496 if (cinfo->restart_interval)
503 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++)
505 ci = cinfo->MCU_membership[blkn];
506 compptr = cinfo->cur_comp_info[ci];
508 &state, MCU_data[blkn][0], state.cur.last_dc_val[ci],
513 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
517 cinfo->dest->next_output_byte = state.next_output_byte;
518 cinfo->dest->free_in_buffer = state.free_in_buffer;
522 if (cinfo->restart_interval)
548 state.free_in_buffer = cinfo->dest->free_in_buffer;
556 cinfo->dest->next_output_byte = state.next_output_byte;
557 cinfo->dest->free_in_buffer = state.free_in_buffer;
572 #ifdef ENTROPY_OPT_SUPPORTED 587 temp = block[0] - last_dc_val;
588 if (temp < 0) temp = -temp;
627 if (temp < 0) temp = -temp;
632 while ((temp >>= 1)) nbits++;
637 ac_counts[(
r << 4) + nbits]++;
644 if (
r > 0) ac_counts[0]++;
660 if (cinfo->restart_interval)
665 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
673 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++)
675 ci = cinfo->MCU_membership[blkn];
676 compptr = cinfo->cur_comp_info[ci];
730 for (i = 0; i < 257; i++) others[i] = -1;
746 for (i = 0; i <= 256; i++)
748 if (freq[i] && freq[i] <=
v)
759 for (i = 0; i <= 256; i++)
761 if (freq[i] && freq[i] <=
v && i != c1)
772 freq[c1] += freq[c2];
777 while (others[c1] >= 0)
787 while (others[c2] >= 0)
795 for (i = 0; i <= 256; i++)
823 while (bits[j] == 0) j--;
847 for (j = 0; j <= 255; j++)
849 if (codesize[j] == i)
851 htbl->huffval[
p] = (
UINT8)j;
858 htbl->sent_table =
FALSE;
869 int ci, dctbl, actbl;
881 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
883 compptr = cinfo->cur_comp_info[ci];
888 htblptr = &cinfo->dc_huff_tbl_ptrs[dctbl];
889 if (*htblptr ==
nullptr)
893 did_dc[dctbl] =
TRUE;
897 htblptr = &cinfo->ac_huff_tbl_ptrs[actbl];
898 if (*htblptr ==
nullptr)
902 did_ac[actbl] =
TRUE;
928 #ifdef ENTROPY_OPT_SUPPORTED
METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo))
#define emit_byte(state, val, action)
long * ac_count_ptrs[NUM_HUFF_TBLS]
#define MAX_COMPS_IN_SCAN
jpeg_make_c_derived_tbl(j_compress_ptr cinfo, boolean isDC, int tblno, c_derived_tbl **pdtbl)
const int jpeg_natural_order[]
JOCTET * next_output_byte
struct jpeg_common_struct * j_common_ptr
encode_one_block(working_state *state, JCOEFPTR block, int last_dc_val, c_derived_tbl *dctbl, c_derived_tbl *actbl)
#define ERREXIT(cinfo, code)
encode_mcu_huff JPP((j_compress_ptr cinfo, JBLOCKROW *MCU_data))
htest_one_block(j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val, long dc_counts[], long ac_counts[])
encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
finish_pass_gather(j_compress_ptr cinfo)
c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]
INLINE emit_bits(working_state *state, unsigned int code, int size)
flush_bits(working_state *state)
encode_mcu_gather(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
jinit_huff_encoder(j_compress_ptr cinfo)
#define MEMCOPY(dest, src, size)
jpeg_gen_optimal_table(j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[])
huff_entropy_encoder * huff_entropy_ptr
jpeg_alloc_huff_table(j_common_ptr cinfo)
struct jpeg_entropy_encoder pub
#define ASSIGN_STATE(dest, src)
emit_restart(working_state *state, int restart_num)
#define ERREXIT1(cinfo, code, p1)
GLdouble GLdouble GLdouble r
finish_pass_huff(j_compress_ptr cinfo)
int last_dc_val[MAX_COMPS_IN_SCAN]
unsigned int restarts_to_go
dump_buffer(working_state *state)
c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]
JOCTET * next_output_byte
start_pass_huff(j_compress_ptr cinfo, boolean gather_statistics)
#define MEMZERO(target, size)
jpeg_component_info * compptr
long * dc_count_ptrs[NUM_HUFF_TBLS]