Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/vncdll/winvnc/libjpeg/jchuff.c
Views: 11784
/*1* jchuff.c2*3* Copyright (C) 1991-1997, Thomas G. Lane.4* This file is part of the Independent JPEG Group's software.5* For conditions of distribution and use, see the accompanying README file.6*7* This file contains Huffman entropy encoding routines.8*9* Much of the complexity here has to do with supporting output suspension.10* If the data destination module demands suspension, we want to be able to11* back up to the start of the current MCU. To do this, we copy state12* variables into local working storage, and update them back to the13* permanent JPEG objects only upon successful completion of an MCU.14*/1516#define JPEG_INTERNALS17#include "jinclude.h"18#include "jpeglib.h"19#include "jchuff.h" /* Declarations shared with jcphuff.c */202122/* Expanded entropy encoder object for Huffman encoding.23*24* The savable_state subrecord contains fields that change within an MCU,25* but must not be updated permanently until we complete the MCU.26*/2728typedef struct {29INT32 put_buffer; /* current bit-accumulation buffer */30int put_bits; /* # of bits now in it */31int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */32} savable_state;3334/* This macro is to work around compilers with missing or broken35* structure assignment. You'll need to fix this code if you have36* such a compiler and you change MAX_COMPS_IN_SCAN.37*/3839#ifndef NO_STRUCT_ASSIGN40#define ASSIGN_STATE(dest,src) ((dest) = (src))41#else42#if MAX_COMPS_IN_SCAN == 443#define ASSIGN_STATE(dest,src) \44((dest).put_buffer = (src).put_buffer, \45(dest).put_bits = (src).put_bits, \46(dest).last_dc_val[0] = (src).last_dc_val[0], \47(dest).last_dc_val[1] = (src).last_dc_val[1], \48(dest).last_dc_val[2] = (src).last_dc_val[2], \49(dest).last_dc_val[3] = (src).last_dc_val[3])50#endif51#endif525354typedef struct {55struct jpeg_entropy_encoder pub; /* public fields */5657savable_state saved; /* Bit buffer & DC state at start of MCU */5859/* These fields are NOT loaded into local working state. */60unsigned int restarts_to_go; /* MCUs left in this restart interval */61int next_restart_num; /* next restart number to write (0-7) */6263/* Pointers to derived tables (these workspaces have image lifespan) */64c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];65c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];6667#ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */68long * dc_count_ptrs[NUM_HUFF_TBLS];69long * ac_count_ptrs[NUM_HUFF_TBLS];70#endif71} huff_entropy_encoder;7273typedef huff_entropy_encoder * huff_entropy_ptr;7475/* Working state while writing an MCU.76* This struct contains all the fields that are needed by subroutines.77*/7879typedef struct {80JOCTET * next_output_byte; /* => next byte to write in buffer */81size_t free_in_buffer; /* # of byte spaces remaining in buffer */82savable_state cur; /* Current bit buffer & DC state */83j_compress_ptr cinfo; /* dump_buffer needs access to this */84} working_state;858687/* Forward declarations */88METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,89JBLOCKROW *MCU_data));90METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));91#ifdef ENTROPY_OPT_SUPPORTED92METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,93JBLOCKROW *MCU_data));94METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));95#endif969798/*99* Initialize for a Huffman-compressed scan.100* If gather_statistics is TRUE, we do not output anything during the scan,101* just count the Huffman symbols used and generate Huffman code tables.102*/103104METHODDEF(void)105start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)106{107huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;108int ci, dctbl, actbl;109jpeg_component_info * compptr;110111if (gather_statistics) {112#ifdef ENTROPY_OPT_SUPPORTED113entropy->pub.encode_mcu = encode_mcu_gather;114entropy->pub.finish_pass = finish_pass_gather;115#else116ERREXIT(cinfo, JERR_NOT_COMPILED);117#endif118} else {119entropy->pub.encode_mcu = encode_mcu_huff;120entropy->pub.finish_pass = finish_pass_huff;121}122123for (ci = 0; ci < cinfo->comps_in_scan; ci++) {124compptr = cinfo->cur_comp_info[ci];125dctbl = compptr->dc_tbl_no;126actbl = compptr->ac_tbl_no;127if (gather_statistics) {128#ifdef ENTROPY_OPT_SUPPORTED129/* Check for invalid table indexes */130/* (make_c_derived_tbl does this in the other path) */131if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)132ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);133if (actbl < 0 || actbl >= NUM_HUFF_TBLS)134ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);135/* Allocate and zero the statistics tables */136/* Note that jpeg_gen_optimal_table expects 257 entries in each table! */137if (entropy->dc_count_ptrs[dctbl] == NULL)138entropy->dc_count_ptrs[dctbl] = (long *)139(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,140257 * SIZEOF(long));141MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));142if (entropy->ac_count_ptrs[actbl] == NULL)143entropy->ac_count_ptrs[actbl] = (long *)144(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,145257 * SIZEOF(long));146MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));147#endif148} else {149/* Compute derived values for Huffman tables */150/* We may do this more than once for a table, but it's not expensive */151jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,152& entropy->dc_derived_tbls[dctbl]);153jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,154& entropy->ac_derived_tbls[actbl]);155}156/* Initialize DC predictions to 0 */157entropy->saved.last_dc_val[ci] = 0;158}159160/* Initialize bit buffer to empty */161entropy->saved.put_buffer = 0;162entropy->saved.put_bits = 0;163164/* Initialize restart stuff */165entropy->restarts_to_go = cinfo->restart_interval;166entropy->next_restart_num = 0;167}168169170/*171* Compute the derived values for a Huffman table.172* This routine also performs some validation checks on the table.173*174* Note this is also used by jcphuff.c.175*/176177GLOBAL(void)178jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,179c_derived_tbl ** pdtbl)180{181JHUFF_TBL *htbl;182c_derived_tbl *dtbl;183int p, i, l, lastp, si, maxsymbol;184char huffsize[257];185unsigned int huffcode[257];186unsigned int code;187188/* Note that huffsize[] and huffcode[] are filled in code-length order,189* paralleling the order of the symbols themselves in htbl->huffval[].190*/191192/* Find the input Huffman table */193if (tblno < 0 || tblno >= NUM_HUFF_TBLS)194ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);195htbl =196isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];197if (htbl == NULL)198ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);199200/* Allocate a workspace if we haven't already done so. */201if (*pdtbl == NULL)202*pdtbl = (c_derived_tbl *)203(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,204SIZEOF(c_derived_tbl));205dtbl = *pdtbl;206207/* Figure C.1: make table of Huffman code length for each symbol */208209p = 0;210for (l = 1; l <= 16; l++) {211i = (int) htbl->bits[l];212if (i < 0 || p + i > 256) /* protect against table overrun */213ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);214while (i--)215huffsize[p++] = (char) l;216}217huffsize[p] = 0;218lastp = p;219220/* Figure C.2: generate the codes themselves */221/* We also validate that the counts represent a legal Huffman code tree. */222223code = 0;224si = huffsize[0];225p = 0;226while (huffsize[p]) {227while (((int) huffsize[p]) == si) {228huffcode[p++] = code;229code++;230}231/* code is now 1 more than the last code used for codelength si; but232* it must still fit in si bits, since no code is allowed to be all ones.233*/234if (((INT32) code) >= (((INT32) 1) << si))235ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);236code <<= 1;237si++;238}239240/* Figure C.3: generate encoding tables */241/* These are code and size indexed by symbol value */242243/* Set all codeless symbols to have code length 0;244* this lets us detect duplicate VAL entries here, and later245* allows emit_bits to detect any attempt to emit such symbols.246*/247MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));248249/* This is also a convenient place to check for out-of-range250* and duplicated VAL entries. We allow 0..255 for AC symbols251* but only 0..15 for DC. (We could constrain them further252* based on data depth and mode, but this seems enough.)253*/254maxsymbol = isDC ? 15 : 255;255256for (p = 0; p < lastp; p++) {257i = htbl->huffval[p];258if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])259ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);260dtbl->ehufco[i] = huffcode[p];261dtbl->ehufsi[i] = huffsize[p];262}263}264265266/* Outputting bytes to the file */267268/* Emit a byte, taking 'action' if must suspend. */269#define emit_byte(state,val,action) \270{ *(state)->next_output_byte++ = (JOCTET) (val); \271if (--(state)->free_in_buffer == 0) \272if (! dump_buffer(state)) \273{ action; } }274275276LOCAL(boolean)277dump_buffer (working_state * state)278/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */279{280struct jpeg_destination_mgr * dest = state->cinfo->dest;281282if (! (*dest->empty_output_buffer) (state->cinfo))283return FALSE;284/* After a successful buffer dump, must reset buffer pointers */285state->next_output_byte = dest->next_output_byte;286state->free_in_buffer = dest->free_in_buffer;287return TRUE;288}289290291/* Outputting bits to the file */292293/* Only the right 24 bits of put_buffer are used; the valid bits are294* left-justified in this part. At most 16 bits can be passed to emit_bits295* in one call, and we never retain more than 7 bits in put_buffer296* between calls, so 24 bits are sufficient.297*/298299INLINE300LOCAL(boolean)301emit_bits (working_state * state, unsigned int code, int size)302/* Emit some bits; return TRUE if successful, FALSE if must suspend */303{304/* This routine is heavily used, so it's worth coding tightly. */305register INT32 put_buffer = (INT32) code;306register int put_bits = state->cur.put_bits;307308/* if size is 0, caller used an invalid Huffman table entry */309if (size == 0)310ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);311312put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */313314put_bits += size; /* new number of bits in buffer */315316put_buffer <<= 24 - put_bits; /* align incoming bits */317318put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */319320while (put_bits >= 8) {321int c = (int) ((put_buffer >> 16) & 0xFF);322323emit_byte(state, c, return FALSE);324if (c == 0xFF) { /* need to stuff a zero byte? */325emit_byte(state, 0, return FALSE);326}327put_buffer <<= 8;328put_bits -= 8;329}330331state->cur.put_buffer = put_buffer; /* update state variables */332state->cur.put_bits = put_bits;333334return TRUE;335}336337338LOCAL(boolean)339flush_bits (working_state * state)340{341if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */342return FALSE;343state->cur.put_buffer = 0; /* and reset bit-buffer to empty */344state->cur.put_bits = 0;345return TRUE;346}347348349/* Encode a single block's worth of coefficients */350351LOCAL(boolean)352encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,353c_derived_tbl *dctbl, c_derived_tbl *actbl)354{355register int temp, temp2;356register int nbits;357register int k, r, i;358359/* Encode the DC coefficient difference per section F.1.2.1 */360361temp = temp2 = block[0] - last_dc_val;362363if (temp < 0) {364temp = -temp; /* temp is abs value of input */365/* For a negative input, want temp2 = bitwise complement of abs(input) */366/* This code assumes we are on a two's complement machine */367temp2--;368}369370/* Find the number of bits needed for the magnitude of the coefficient */371nbits = 0;372while (temp) {373nbits++;374temp >>= 1;375}376/* Check for out-of-range coefficient values.377* Since we're encoding a difference, the range limit is twice as much.378*/379if (nbits > MAX_COEF_BITS+1)380ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);381382/* Emit the Huffman-coded symbol for the number of bits */383if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))384return FALSE;385386/* Emit that number of bits of the value, if positive, */387/* or the complement of its magnitude, if negative. */388if (nbits) /* emit_bits rejects calls with size 0 */389if (! emit_bits(state, (unsigned int) temp2, nbits))390return FALSE;391392/* Encode the AC coefficients per section F.1.2.2 */393394r = 0; /* r = run length of zeros */395396for (k = 1; k < DCTSIZE2; k++) {397if ((temp = block[jpeg_natural_order[k]]) == 0) {398r++;399} else {400/* if run length > 15, must emit special run-length-16 codes (0xF0) */401while (r > 15) {402if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))403return FALSE;404r -= 16;405}406407temp2 = temp;408if (temp < 0) {409temp = -temp; /* temp is abs value of input */410/* This code assumes we are on a two's complement machine */411temp2--;412}413414/* Find the number of bits needed for the magnitude of the coefficient */415nbits = 1; /* there must be at least one 1 bit */416while ((temp >>= 1))417nbits++;418/* Check for out-of-range coefficient values */419if (nbits > MAX_COEF_BITS)420ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);421422/* Emit Huffman symbol for run length / number of bits */423i = (r << 4) + nbits;424if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))425return FALSE;426427/* Emit that number of bits of the value, if positive, */428/* or the complement of its magnitude, if negative. */429if (! emit_bits(state, (unsigned int) temp2, nbits))430return FALSE;431432r = 0;433}434}435436/* If the last coef(s) were zero, emit an end-of-block code */437if (r > 0)438if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))439return FALSE;440441return TRUE;442}443444445/*446* Emit a restart marker & resynchronize predictions.447*/448449LOCAL(boolean)450emit_restart (working_state * state, int restart_num)451{452int ci;453454if (! flush_bits(state))455return FALSE;456457emit_byte(state, 0xFF, return FALSE);458emit_byte(state, JPEG_RST0 + restart_num, return FALSE);459460/* Re-initialize DC predictions to 0 */461for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)462state->cur.last_dc_val[ci] = 0;463464/* The restart counter is not updated until we successfully write the MCU. */465466return TRUE;467}468469470/*471* Encode and output one MCU's worth of Huffman-compressed coefficients.472*/473474METHODDEF(boolean)475encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)476{477huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;478working_state state;479int blkn, ci;480jpeg_component_info * compptr;481482/* Load up working state */483state.next_output_byte = cinfo->dest->next_output_byte;484state.free_in_buffer = cinfo->dest->free_in_buffer;485ASSIGN_STATE(state.cur, entropy->saved);486state.cinfo = cinfo;487488/* Emit restart marker if needed */489if (cinfo->restart_interval) {490if (entropy->restarts_to_go == 0)491if (! emit_restart(&state, entropy->next_restart_num))492return FALSE;493}494495/* Encode the MCU data blocks */496for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {497ci = cinfo->MCU_membership[blkn];498compptr = cinfo->cur_comp_info[ci];499if (! encode_one_block(&state,500MCU_data[blkn][0], state.cur.last_dc_val[ci],501entropy->dc_derived_tbls[compptr->dc_tbl_no],502entropy->ac_derived_tbls[compptr->ac_tbl_no]))503return FALSE;504/* Update last_dc_val */505state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];506}507508/* Completed MCU, so update state */509cinfo->dest->next_output_byte = state.next_output_byte;510cinfo->dest->free_in_buffer = state.free_in_buffer;511ASSIGN_STATE(entropy->saved, state.cur);512513/* Update restart-interval state too */514if (cinfo->restart_interval) {515if (entropy->restarts_to_go == 0) {516entropy->restarts_to_go = cinfo->restart_interval;517entropy->next_restart_num++;518entropy->next_restart_num &= 7;519}520entropy->restarts_to_go--;521}522523return TRUE;524}525526527/*528* Finish up at the end of a Huffman-compressed scan.529*/530531METHODDEF(void)532finish_pass_huff (j_compress_ptr cinfo)533{534huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;535working_state state;536537/* Load up working state ... flush_bits needs it */538state.next_output_byte = cinfo->dest->next_output_byte;539state.free_in_buffer = cinfo->dest->free_in_buffer;540ASSIGN_STATE(state.cur, entropy->saved);541state.cinfo = cinfo;542543/* Flush out the last data */544if (! flush_bits(&state))545ERREXIT(cinfo, JERR_CANT_SUSPEND);546547/* Update state */548cinfo->dest->next_output_byte = state.next_output_byte;549cinfo->dest->free_in_buffer = state.free_in_buffer;550ASSIGN_STATE(entropy->saved, state.cur);551}552553554/*555* Huffman coding optimization.556*557* We first scan the supplied data and count the number of uses of each symbol558* that is to be Huffman-coded. (This process MUST agree with the code above.)559* Then we build a Huffman coding tree for the observed counts.560* Symbols which are not needed at all for the particular image are not561* assigned any code, which saves space in the DHT marker as well as in562* the compressed data.563*/564565#ifdef ENTROPY_OPT_SUPPORTED566567568/* Process a single block's worth of coefficients */569570LOCAL(void)571htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,572long dc_counts[], long ac_counts[])573{574register int temp;575register int nbits;576register int k, r;577578/* Encode the DC coefficient difference per section F.1.2.1 */579580temp = block[0] - last_dc_val;581if (temp < 0)582temp = -temp;583584/* Find the number of bits needed for the magnitude of the coefficient */585nbits = 0;586while (temp) {587nbits++;588temp >>= 1;589}590/* Check for out-of-range coefficient values.591* Since we're encoding a difference, the range limit is twice as much.592*/593if (nbits > MAX_COEF_BITS+1)594ERREXIT(cinfo, JERR_BAD_DCT_COEF);595596/* Count the Huffman symbol for the number of bits */597dc_counts[nbits]++;598599/* Encode the AC coefficients per section F.1.2.2 */600601r = 0; /* r = run length of zeros */602603for (k = 1; k < DCTSIZE2; k++) {604if ((temp = block[jpeg_natural_order[k]]) == 0) {605r++;606} else {607/* if run length > 15, must emit special run-length-16 codes (0xF0) */608while (r > 15) {609ac_counts[0xF0]++;610r -= 16;611}612613/* Find the number of bits needed for the magnitude of the coefficient */614if (temp < 0)615temp = -temp;616617/* Find the number of bits needed for the magnitude of the coefficient */618nbits = 1; /* there must be at least one 1 bit */619while ((temp >>= 1))620nbits++;621/* Check for out-of-range coefficient values */622if (nbits > MAX_COEF_BITS)623ERREXIT(cinfo, JERR_BAD_DCT_COEF);624625/* Count Huffman symbol for run length / number of bits */626ac_counts[(r << 4) + nbits]++;627628r = 0;629}630}631632/* If the last coef(s) were zero, emit an end-of-block code */633if (r > 0)634ac_counts[0]++;635}636637638/*639* Trial-encode one MCU's worth of Huffman-compressed coefficients.640* No data is actually output, so no suspension return is possible.641*/642643METHODDEF(boolean)644encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)645{646huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;647int blkn, ci;648jpeg_component_info * compptr;649650/* Take care of restart intervals if needed */651if (cinfo->restart_interval) {652if (entropy->restarts_to_go == 0) {653/* Re-initialize DC predictions to 0 */654for (ci = 0; ci < cinfo->comps_in_scan; ci++)655entropy->saved.last_dc_val[ci] = 0;656/* Update restart state */657entropy->restarts_to_go = cinfo->restart_interval;658}659entropy->restarts_to_go--;660}661662for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {663ci = cinfo->MCU_membership[blkn];664compptr = cinfo->cur_comp_info[ci];665htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],666entropy->dc_count_ptrs[compptr->dc_tbl_no],667entropy->ac_count_ptrs[compptr->ac_tbl_no]);668entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];669}670671return TRUE;672}673674675/*676* Generate the best Huffman code table for the given counts, fill htbl.677* Note this is also used by jcphuff.c.678*679* The JPEG standard requires that no symbol be assigned a codeword of all680* one bits (so that padding bits added at the end of a compressed segment681* can't look like a valid code). Because of the canonical ordering of682* codewords, this just means that there must be an unused slot in the683* longest codeword length category. Section K.2 of the JPEG spec suggests684* reserving such a slot by pretending that symbol 256 is a valid symbol685* with count 1. In theory that's not optimal; giving it count zero but686* including it in the symbol set anyway should give a better Huffman code.687* But the theoretically better code actually seems to come out worse in688* practice, because it produces more all-ones bytes (which incur stuffed689* zero bytes in the final file). In any case the difference is tiny.690*691* The JPEG standard requires Huffman codes to be no more than 16 bits long.692* If some symbols have a very small but nonzero probability, the Huffman tree693* must be adjusted to meet the code length restriction. We currently use694* the adjustment method suggested in JPEG section K.2. This method is *not*695* optimal; it may not choose the best possible limited-length code. But696* typically only very-low-frequency symbols will be given less-than-optimal697* lengths, so the code is almost optimal. Experimental comparisons against698* an optimal limited-length-code algorithm indicate that the difference is699* microscopic --- usually less than a hundredth of a percent of total size.700* So the extra complexity of an optimal algorithm doesn't seem worthwhile.701*/702703GLOBAL(void)704jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])705{706#define MAX_CLEN 32 /* assumed maximum initial code length */707UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */708int codesize[257]; /* codesize[k] = code length of symbol k */709int others[257]; /* next symbol in current branch of tree */710int c1, c2;711int p, i, j;712long v;713714/* This algorithm is explained in section K.2 of the JPEG standard */715716MEMZERO(bits, SIZEOF(bits));717MEMZERO(codesize, SIZEOF(codesize));718for (i = 0; i < 257; i++)719others[i] = -1; /* init links to empty */720721freq[256] = 1; /* make sure 256 has a nonzero count */722/* Including the pseudo-symbol 256 in the Huffman procedure guarantees723* that no real symbol is given code-value of all ones, because 256724* will be placed last in the largest codeword category.725*/726727/* Huffman's basic algorithm to assign optimal code lengths to symbols */728729for (;;) {730/* Find the smallest nonzero frequency, set c1 = its symbol */731/* In case of ties, take the larger symbol number */732c1 = -1;733v = 1000000000L;734for (i = 0; i <= 256; i++) {735if (freq[i] && freq[i] <= v) {736v = freq[i];737c1 = i;738}739}740741/* Find the next smallest nonzero frequency, set c2 = its symbol */742/* In case of ties, take the larger symbol number */743c2 = -1;744v = 1000000000L;745for (i = 0; i <= 256; i++) {746if (freq[i] && freq[i] <= v && i != c1) {747v = freq[i];748c2 = i;749}750}751752/* Done if we've merged everything into one frequency */753if (c2 < 0)754break;755756/* Else merge the two counts/trees */757freq[c1] += freq[c2];758freq[c2] = 0;759760/* Increment the codesize of everything in c1's tree branch */761codesize[c1]++;762while (others[c1] >= 0) {763c1 = others[c1];764codesize[c1]++;765}766767others[c1] = c2; /* chain c2 onto c1's tree branch */768769/* Increment the codesize of everything in c2's tree branch */770codesize[c2]++;771while (others[c2] >= 0) {772c2 = others[c2];773codesize[c2]++;774}775}776777/* Now count the number of symbols of each code length */778for (i = 0; i <= 256; i++) {779if (codesize[i]) {780/* The JPEG standard seems to think that this can't happen, */781/* but I'm paranoid... */782if (codesize[i] > MAX_CLEN)783ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);784785bits[codesize[i]]++;786}787}788789/* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure790* Huffman procedure assigned any such lengths, we must adjust the coding.791* Here is what the JPEG spec says about how this next bit works:792* Since symbols are paired for the longest Huffman code, the symbols are793* removed from this length category two at a time. The prefix for the pair794* (which is one bit shorter) is allocated to one of the pair; then,795* skipping the BITS entry for that prefix length, a code word from the next796* shortest nonzero BITS entry is converted into a prefix for two code words797* one bit longer.798*/799800for (i = MAX_CLEN; i > 16; i--) {801while (bits[i] > 0) {802j = i - 2; /* find length of new prefix to be used */803while (bits[j] == 0)804j--;805806bits[i] -= 2; /* remove two symbols */807bits[i-1]++; /* one goes in this length */808bits[j+1] += 2; /* two new symbols in this length */809bits[j]--; /* symbol of this length is now a prefix */810}811}812813/* Remove the count for the pseudo-symbol 256 from the largest codelength */814while (bits[i] == 0) /* find largest codelength still in use */815i--;816bits[i]--;817818/* Return final symbol counts (only for lengths 0..16) */819MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));820821/* Return a list of the symbols sorted by code length */822/* It's not real clear to me why we don't need to consider the codelength823* changes made above, but the JPEG spec seems to think this works.824*/825p = 0;826for (i = 1; i <= MAX_CLEN; i++) {827for (j = 0; j <= 255; j++) {828if (codesize[j] == i) {829htbl->huffval[p] = (UINT8) j;830p++;831}832}833}834835/* Set sent_table FALSE so updated table will be written to JPEG file. */836htbl->sent_table = FALSE;837}838839840/*841* Finish up a statistics-gathering pass and create the new Huffman tables.842*/843844METHODDEF(void)845finish_pass_gather (j_compress_ptr cinfo)846{847huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;848int ci, dctbl, actbl;849jpeg_component_info * compptr;850JHUFF_TBL **htblptr;851boolean did_dc[NUM_HUFF_TBLS];852boolean did_ac[NUM_HUFF_TBLS];853854/* It's important not to apply jpeg_gen_optimal_table more than once855* per table, because it clobbers the input frequency counts!856*/857MEMZERO(did_dc, SIZEOF(did_dc));858MEMZERO(did_ac, SIZEOF(did_ac));859860for (ci = 0; ci < cinfo->comps_in_scan; ci++) {861compptr = cinfo->cur_comp_info[ci];862dctbl = compptr->dc_tbl_no;863actbl = compptr->ac_tbl_no;864if (! did_dc[dctbl]) {865htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];866if (*htblptr == NULL)867*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);868jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);869did_dc[dctbl] = TRUE;870}871if (! did_ac[actbl]) {872htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];873if (*htblptr == NULL)874*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);875jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);876did_ac[actbl] = TRUE;877}878}879}880881882#endif /* ENTROPY_OPT_SUPPORTED */883884885/*886* Module initialization routine for Huffman entropy encoding.887*/888889GLOBAL(void)890jinit_huff_encoder (j_compress_ptr cinfo)891{892huff_entropy_ptr entropy;893int i;894895entropy = (huff_entropy_ptr)896(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,897SIZEOF(huff_entropy_encoder));898cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;899entropy->pub.start_pass = start_pass_huff;900901/* Mark tables unallocated */902for (i = 0; i < NUM_HUFF_TBLS; i++) {903entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;904#ifdef ENTROPY_OPT_SUPPORTED905entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;906#endif907}908}909910911