Path: blob/master/src/java.desktop/share/native/libjavajpeg/jcphuff.c
41152 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jcphuff.c6*7* Copyright (C) 1995-1997, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains Huffman entropy encoding routines for progressive JPEG.12*13* We do not support output suspension in this module, since the library14* currently does not allow multiple-scan files to be written with output15* suspension.16*/1718#define JPEG_INTERNALS19#include "jinclude.h"20#include "jpeglib.h"21#include "jchuff.h" /* Declarations shared with jchuff.c */2223#ifdef C_PROGRESSIVE_SUPPORTED2425/* Expanded entropy encoder object for progressive Huffman encoding. */2627typedef struct {28struct jpeg_entropy_encoder pub; /* public fields */2930/* Mode flag: TRUE for optimization, FALSE for actual data output */31boolean gather_statistics;3233/* Bit-level coding status.34* next_output_byte/free_in_buffer are local copies of cinfo->dest fields.35*/36JOCTET * next_output_byte; /* => next byte to write in buffer */37size_t free_in_buffer; /* # of byte spaces remaining in buffer */38INT32 put_buffer; /* current bit-accumulation buffer */39int put_bits; /* # of bits now in it */40j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */4142/* Coding status for DC components */43int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */4445/* Coding status for AC components */46int ac_tbl_no; /* the table number of the single component */47unsigned int EOBRUN; /* run length of EOBs */48unsigned int BE; /* # of buffered correction bits before MCU */49char * bit_buffer; /* buffer for correction bits (1 per char) */50/* packing correction bits tightly would save some space but cost time... */5152unsigned int restarts_to_go; /* MCUs left in this restart interval */53int next_restart_num; /* next restart number to write (0-7) */5455/* Pointers to derived tables (these workspaces have image lifespan).56* Since any one scan codes only DC or only AC, we only need one set57* of tables, not one for DC and one for AC.58*/59c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];6061/* Statistics tables for optimization; again, one set is enough */62long * count_ptrs[NUM_HUFF_TBLS];63} phuff_entropy_encoder;6465typedef phuff_entropy_encoder * phuff_entropy_ptr;6667/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit68* buffer can hold. Larger sizes may slightly improve compression, but69* 1000 is already well into the realm of overkill.70* The minimum safe size is 64 bits.71*/7273#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */7475/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.76* We assume that int right shift is unsigned if INT32 right shift is,77* which should be safe.78*/7980#ifdef RIGHT_SHIFT_IS_UNSIGNED81#define ISHIFT_TEMPS int ishift_temp;82#define IRIGHT_SHIFT(x,shft) \83((ishift_temp = (x)) < 0 ? \84(ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \85(ishift_temp >> (shft)))86#else87#define ISHIFT_TEMPS88#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))89#endif9091/* Forward declarations */92METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,93JBLOCKROW *MCU_data));94METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,95JBLOCKROW *MCU_data));96METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,97JBLOCKROW *MCU_data));98METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,99JBLOCKROW *MCU_data));100METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));101METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));102103104/*105* Initialize for a Huffman-compressed scan using progressive JPEG.106*/107108METHODDEF(void)109start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)110{111phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;112boolean is_DC_band;113int ci, tbl;114jpeg_component_info * compptr;115116entropy->cinfo = cinfo;117entropy->gather_statistics = gather_statistics;118119is_DC_band = (cinfo->Ss == 0);120121/* We assume jcmaster.c already validated the scan parameters. */122123/* Select execution routines */124if (cinfo->Ah == 0) {125if (is_DC_band)126entropy->pub.encode_mcu = encode_mcu_DC_first;127else128entropy->pub.encode_mcu = encode_mcu_AC_first;129} else {130if (is_DC_band)131entropy->pub.encode_mcu = encode_mcu_DC_refine;132else {133entropy->pub.encode_mcu = encode_mcu_AC_refine;134/* AC refinement needs a correction bit buffer */135if (entropy->bit_buffer == NULL)136entropy->bit_buffer = (char *)137(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,138MAX_CORR_BITS * SIZEOF(char));139}140}141if (gather_statistics)142entropy->pub.finish_pass = finish_pass_gather_phuff;143else144entropy->pub.finish_pass = finish_pass_phuff;145146/* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1147* for AC coefficients.148*/149for (ci = 0; ci < cinfo->comps_in_scan; ci++) {150compptr = cinfo->cur_comp_info[ci];151/* Initialize DC predictions to 0 */152entropy->last_dc_val[ci] = 0;153/* Get table index */154if (is_DC_band) {155if (cinfo->Ah != 0) /* DC refinement needs no table */156continue;157tbl = compptr->dc_tbl_no;158} else {159entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;160}161if (gather_statistics) {162/* Check for invalid table index */163/* (make_c_derived_tbl does this in the other path) */164if (tbl < 0 || tbl >= NUM_HUFF_TBLS)165ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);166/* Allocate and zero the statistics tables */167/* Note that jpeg_gen_optimal_table expects 257 entries in each table! */168if (entropy->count_ptrs[tbl] == NULL)169entropy->count_ptrs[tbl] = (long *)170(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,171257 * SIZEOF(long));172MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));173} else {174/* Compute derived values for Huffman table */175/* We may do this more than once for a table, but it's not expensive */176jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,177& entropy->derived_tbls[tbl]);178}179}180181/* Initialize AC stuff */182entropy->EOBRUN = 0;183entropy->BE = 0;184185/* Initialize bit buffer to empty */186entropy->put_buffer = 0;187entropy->put_bits = 0;188189/* Initialize restart stuff */190entropy->restarts_to_go = cinfo->restart_interval;191entropy->next_restart_num = 0;192}193194195/* Outputting bytes to the file.196* NB: these must be called only when actually outputting,197* that is, entropy->gather_statistics == FALSE.198*/199200/* Emit a byte */201#define emit_byte(entropy,val) \202{ *(entropy)->next_output_byte++ = (JOCTET) (val); \203if (--(entropy)->free_in_buffer == 0) \204dump_buffer(entropy); }205206207LOCAL(void)208dump_buffer (phuff_entropy_ptr entropy)209/* Empty the output buffer; we do not support suspension in this module. */210{211struct jpeg_destination_mgr * dest = entropy->cinfo->dest;212213if (! (*dest->empty_output_buffer) (entropy->cinfo))214ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);215/* After a successful buffer dump, must reset buffer pointers */216entropy->next_output_byte = dest->next_output_byte;217entropy->free_in_buffer = dest->free_in_buffer;218}219220221/* Outputting bits to the file */222223/* Only the right 24 bits of put_buffer are used; the valid bits are224* left-justified in this part. At most 16 bits can be passed to emit_bits225* in one call, and we never retain more than 7 bits in put_buffer226* between calls, so 24 bits are sufficient.227*/228229INLINE230LOCAL(void)231emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)232/* Emit some bits, unless we are in gather mode */233{234/* This routine is heavily used, so it's worth coding tightly. */235register INT32 put_buffer = (INT32) code;236register int put_bits = entropy->put_bits;237238/* if size is 0, caller used an invalid Huffman table entry */239if (size == 0)240ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);241242if (entropy->gather_statistics)243return; /* do nothing if we're only getting stats */244245put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */246247put_bits += size; /* new number of bits in buffer */248249put_buffer <<= 24 - put_bits; /* align incoming bits */250251put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */252253while (put_bits >= 8) {254int c = (int) ((put_buffer >> 16) & 0xFF);255256emit_byte(entropy, c);257if (c == 0xFF) { /* need to stuff a zero byte? */258emit_byte(entropy, 0);259}260put_buffer <<= 8;261put_bits -= 8;262}263264entropy->put_buffer = put_buffer; /* update variables */265entropy->put_bits = put_bits;266}267268269LOCAL(void)270flush_bits (phuff_entropy_ptr entropy)271{272emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */273entropy->put_buffer = 0; /* and reset bit-buffer to empty */274entropy->put_bits = 0;275}276277278/*279* Emit (or just count) a Huffman symbol.280*/281282INLINE283LOCAL(void)284emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)285{286if (entropy->gather_statistics)287entropy->count_ptrs[tbl_no][symbol]++;288else {289c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];290emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);291}292}293294295/*296* Emit bits from a correction bit buffer.297*/298299LOCAL(void)300emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,301unsigned int nbits)302{303if (entropy->gather_statistics)304return; /* no real work */305306while (nbits > 0) {307emit_bits(entropy, (unsigned int) (*bufstart), 1);308bufstart++;309nbits--;310}311}312313314/*315* Emit any pending EOBRUN symbol.316*/317318LOCAL(void)319emit_eobrun (phuff_entropy_ptr entropy)320{321register int temp, nbits;322323if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */324temp = entropy->EOBRUN;325nbits = 0;326while ((temp >>= 1))327nbits++;328/* safety check: shouldn't happen given limited correction-bit buffer */329if (nbits > 14)330ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);331332emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);333if (nbits)334emit_bits(entropy, entropy->EOBRUN, nbits);335336entropy->EOBRUN = 0;337338/* Emit any buffered correction bits */339emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);340entropy->BE = 0;341}342}343344345/*346* Emit a restart marker & resynchronize predictions.347*/348349LOCAL(void)350emit_restart (phuff_entropy_ptr entropy, int restart_num)351{352int ci;353354emit_eobrun(entropy);355356if (! entropy->gather_statistics) {357flush_bits(entropy);358emit_byte(entropy, 0xFF);359emit_byte(entropy, JPEG_RST0 + restart_num);360}361362if (entropy->cinfo->Ss == 0) {363/* Re-initialize DC predictions to 0 */364for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)365entropy->last_dc_val[ci] = 0;366} else {367/* Re-initialize all AC-related fields to 0 */368entropy->EOBRUN = 0;369entropy->BE = 0;370}371}372373374/*375* MCU encoding for DC initial scan (either spectral selection,376* or first pass of successive approximation).377*/378379METHODDEF(boolean)380encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)381{382phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;383register int temp, temp2;384register int nbits;385int blkn, ci;386int Al = cinfo->Al;387JBLOCKROW block;388jpeg_component_info * compptr;389ISHIFT_TEMPS390391entropy->next_output_byte = cinfo->dest->next_output_byte;392entropy->free_in_buffer = cinfo->dest->free_in_buffer;393394/* Emit restart marker if needed */395if (cinfo->restart_interval)396if (entropy->restarts_to_go == 0)397emit_restart(entropy, entropy->next_restart_num);398399/* Encode the MCU data blocks */400for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {401block = MCU_data[blkn];402ci = cinfo->MCU_membership[blkn];403compptr = cinfo->cur_comp_info[ci];404405/* Compute the DC value after the required point transform by Al.406* This is simply an arithmetic right shift.407*/408temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);409410/* DC differences are figured on the point-transformed values. */411temp = temp2 - entropy->last_dc_val[ci];412entropy->last_dc_val[ci] = temp2;413414/* Encode the DC coefficient difference per section G.1.2.1 */415temp2 = temp;416if (temp < 0) {417temp = -temp; /* temp is abs value of input */418/* For a negative input, want temp2 = bitwise complement of abs(input) */419/* This code assumes we are on a two's complement machine */420temp2--;421}422423/* Find the number of bits needed for the magnitude of the coefficient */424nbits = 0;425while (temp) {426nbits++;427temp >>= 1;428}429/* Check for out-of-range coefficient values.430* Since we're encoding a difference, the range limit is twice as much.431*/432if (nbits > MAX_COEF_BITS+1)433ERREXIT(cinfo, JERR_BAD_DCT_COEF);434435/* Count/emit the Huffman-coded symbol for the number of bits */436emit_symbol(entropy, compptr->dc_tbl_no, nbits);437438/* Emit that number of bits of the value, if positive, */439/* or the complement of its magnitude, if negative. */440if (nbits) /* emit_bits rejects calls with size 0 */441emit_bits(entropy, (unsigned int) temp2, nbits);442}443444cinfo->dest->next_output_byte = entropy->next_output_byte;445cinfo->dest->free_in_buffer = entropy->free_in_buffer;446447/* Update restart-interval state too */448if (cinfo->restart_interval) {449if (entropy->restarts_to_go == 0) {450entropy->restarts_to_go = cinfo->restart_interval;451entropy->next_restart_num++;452entropy->next_restart_num &= 7;453}454entropy->restarts_to_go--;455}456457return TRUE;458}459460461/*462* MCU encoding for AC initial scan (either spectral selection,463* or first pass of successive approximation).464*/465466METHODDEF(boolean)467encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)468{469phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;470register int temp, temp2;471register int nbits;472register int r, k;473int Se = cinfo->Se;474int Al = cinfo->Al;475JBLOCKROW block;476477entropy->next_output_byte = cinfo->dest->next_output_byte;478entropy->free_in_buffer = cinfo->dest->free_in_buffer;479480/* Emit restart marker if needed */481if (cinfo->restart_interval)482if (entropy->restarts_to_go == 0)483emit_restart(entropy, entropy->next_restart_num);484485/* Encode the MCU data block */486block = MCU_data[0];487488/* Encode the AC coefficients per section G.1.2.2, fig. G.3 */489490r = 0; /* r = run length of zeros */491492for (k = cinfo->Ss; k <= Se; k++) {493if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {494r++;495continue;496}497/* We must apply the point transform by Al. For AC coefficients this498* is an integer division with rounding towards 0. To do this portably499* in C, we shift after obtaining the absolute value; so the code is500* interwoven with finding the abs value (temp) and output bits (temp2).501*/502if (temp < 0) {503temp = -temp; /* temp is abs value of input */504temp >>= Al; /* apply the point transform */505/* For a negative coef, want temp2 = bitwise complement of abs(coef) */506temp2 = ~temp;507} else {508temp >>= Al; /* apply the point transform */509temp2 = temp;510}511/* Watch out for case that nonzero coef is zero after point transform */512if (temp == 0) {513r++;514continue;515}516517/* Emit any pending EOBRUN */518if (entropy->EOBRUN > 0)519emit_eobrun(entropy);520/* if run length > 15, must emit special run-length-16 codes (0xF0) */521while (r > 15) {522emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);523r -= 16;524}525526/* Find the number of bits needed for the magnitude of the coefficient */527nbits = 1; /* there must be at least one 1 bit */528while ((temp >>= 1))529nbits++;530/* Check for out-of-range coefficient values */531if (nbits > MAX_COEF_BITS)532ERREXIT(cinfo, JERR_BAD_DCT_COEF);533534/* Count/emit Huffman symbol for run length / number of bits */535emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);536537/* Emit that number of bits of the value, if positive, */538/* or the complement of its magnitude, if negative. */539emit_bits(entropy, (unsigned int) temp2, nbits);540541r = 0; /* reset zero run length */542}543544if (r > 0) { /* If there are trailing zeroes, */545entropy->EOBRUN++; /* count an EOB */546if (entropy->EOBRUN == 0x7FFF)547emit_eobrun(entropy); /* force it out to avoid overflow */548}549550cinfo->dest->next_output_byte = entropy->next_output_byte;551cinfo->dest->free_in_buffer = entropy->free_in_buffer;552553/* Update restart-interval state too */554if (cinfo->restart_interval) {555if (entropy->restarts_to_go == 0) {556entropy->restarts_to_go = cinfo->restart_interval;557entropy->next_restart_num++;558entropy->next_restart_num &= 7;559}560entropy->restarts_to_go--;561}562563return TRUE;564}565566567/*568* MCU encoding for DC successive approximation refinement scan.569* Note: we assume such scans can be multi-component, although the spec570* is not very clear on the point.571*/572573METHODDEF(boolean)574encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)575{576phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;577register int temp;578int blkn;579int Al = cinfo->Al;580JBLOCKROW block;581582entropy->next_output_byte = cinfo->dest->next_output_byte;583entropy->free_in_buffer = cinfo->dest->free_in_buffer;584585/* Emit restart marker if needed */586if (cinfo->restart_interval)587if (entropy->restarts_to_go == 0)588emit_restart(entropy, entropy->next_restart_num);589590/* Encode the MCU data blocks */591for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {592block = MCU_data[blkn];593594/* We simply emit the Al'th bit of the DC coefficient value. */595temp = (*block)[0];596emit_bits(entropy, (unsigned int) (temp >> Al), 1);597}598599cinfo->dest->next_output_byte = entropy->next_output_byte;600cinfo->dest->free_in_buffer = entropy->free_in_buffer;601602/* Update restart-interval state too */603if (cinfo->restart_interval) {604if (entropy->restarts_to_go == 0) {605entropy->restarts_to_go = cinfo->restart_interval;606entropy->next_restart_num++;607entropy->next_restart_num &= 7;608}609entropy->restarts_to_go--;610}611612return TRUE;613}614615616/*617* MCU encoding for AC successive approximation refinement scan.618*/619620METHODDEF(boolean)621encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)622{623phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;624register int temp;625register int r, k;626int EOB;627char *BR_buffer;628unsigned int BR;629int Se = cinfo->Se;630int Al = cinfo->Al;631JBLOCKROW block;632int absvalues[DCTSIZE2];633634entropy->next_output_byte = cinfo->dest->next_output_byte;635entropy->free_in_buffer = cinfo->dest->free_in_buffer;636637/* Emit restart marker if needed */638if (cinfo->restart_interval)639if (entropy->restarts_to_go == 0)640emit_restart(entropy, entropy->next_restart_num);641642/* Encode the MCU data block */643block = MCU_data[0];644645/* It is convenient to make a pre-pass to determine the transformed646* coefficients' absolute values and the EOB position.647*/648EOB = 0;649for (k = cinfo->Ss; k <= Se; k++) {650temp = (*block)[jpeg_natural_order[k]];651/* We must apply the point transform by Al. For AC coefficients this652* is an integer division with rounding towards 0. To do this portably653* in C, we shift after obtaining the absolute value.654*/655if (temp < 0)656temp = -temp; /* temp is abs value of input */657temp >>= Al; /* apply the point transform */658absvalues[k] = temp; /* save abs value for main pass */659if (temp == 1)660EOB = k; /* EOB = index of last newly-nonzero coef */661}662663/* Encode the AC coefficients per section G.1.2.3, fig. G.7 */664665r = 0; /* r = run length of zeros */666BR = 0; /* BR = count of buffered bits added now */667BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */668669for (k = cinfo->Ss; k <= Se; k++) {670if ((temp = absvalues[k]) == 0) {671r++;672continue;673}674675/* Emit any required ZRLs, but not if they can be folded into EOB */676while (r > 15 && k <= EOB) {677/* emit any pending EOBRUN and the BE correction bits */678emit_eobrun(entropy);679/* Emit ZRL */680emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);681r -= 16;682/* Emit buffered correction bits that must be associated with ZRL */683emit_buffered_bits(entropy, BR_buffer, BR);684BR_buffer = entropy->bit_buffer; /* BE bits are gone now */685BR = 0;686}687688/* If the coef was previously nonzero, it only needs a correction bit.689* NOTE: a straight translation of the spec's figure G.7 would suggest690* that we also need to test r > 15. But if r > 15, we can only get here691* if k > EOB, which implies that this coefficient is not 1.692*/693if (temp > 1) {694/* The correction bit is the next bit of the absolute value. */695BR_buffer[BR++] = (char) (temp & 1);696continue;697}698699/* Emit any pending EOBRUN and the BE correction bits */700emit_eobrun(entropy);701702/* Count/emit Huffman symbol for run length / number of bits */703emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);704705/* Emit output bit for newly-nonzero coef */706temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;707emit_bits(entropy, (unsigned int) temp, 1);708709/* Emit buffered correction bits that must be associated with this code */710emit_buffered_bits(entropy, BR_buffer, BR);711BR_buffer = entropy->bit_buffer; /* BE bits are gone now */712BR = 0;713r = 0; /* reset zero run length */714}715716if (r > 0 || BR > 0) { /* If there are trailing zeroes, */717entropy->EOBRUN++; /* count an EOB */718entropy->BE += BR; /* concat my correction bits to older ones */719/* We force out the EOB if we risk either:720* 1. overflow of the EOB counter;721* 2. overflow of the correction bit buffer during the next MCU.722*/723if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))724emit_eobrun(entropy);725}726727cinfo->dest->next_output_byte = entropy->next_output_byte;728cinfo->dest->free_in_buffer = entropy->free_in_buffer;729730/* Update restart-interval state too */731if (cinfo->restart_interval) {732if (entropy->restarts_to_go == 0) {733entropy->restarts_to_go = cinfo->restart_interval;734entropy->next_restart_num++;735entropy->next_restart_num &= 7;736}737entropy->restarts_to_go--;738}739740return TRUE;741}742743744/*745* Finish up at the end of a Huffman-compressed progressive scan.746*/747748METHODDEF(void)749finish_pass_phuff (j_compress_ptr cinfo)750{751phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;752753entropy->next_output_byte = cinfo->dest->next_output_byte;754entropy->free_in_buffer = cinfo->dest->free_in_buffer;755756/* Flush out any buffered data */757emit_eobrun(entropy);758flush_bits(entropy);759760cinfo->dest->next_output_byte = entropy->next_output_byte;761cinfo->dest->free_in_buffer = entropy->free_in_buffer;762}763764765/*766* Finish up a statistics-gathering pass and create the new Huffman tables.767*/768769METHODDEF(void)770finish_pass_gather_phuff (j_compress_ptr cinfo)771{772phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;773boolean is_DC_band;774int ci, tbl;775jpeg_component_info * compptr;776JHUFF_TBL **htblptr;777boolean did[NUM_HUFF_TBLS];778779/* Flush out buffered data (all we care about is counting the EOB symbol) */780emit_eobrun(entropy);781782is_DC_band = (cinfo->Ss == 0);783784/* It's important not to apply jpeg_gen_optimal_table more than once785* per table, because it clobbers the input frequency counts!786*/787MEMZERO(did, SIZEOF(did));788789for (ci = 0; ci < cinfo->comps_in_scan; ci++) {790compptr = cinfo->cur_comp_info[ci];791if (is_DC_band) {792if (cinfo->Ah != 0) /* DC refinement needs no table */793continue;794tbl = compptr->dc_tbl_no;795} else {796tbl = compptr->ac_tbl_no;797}798if (! did[tbl]) {799if (is_DC_band)800htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];801else802htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];803if (*htblptr == NULL)804*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);805jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);806did[tbl] = TRUE;807}808}809}810811812/*813* Module initialization routine for progressive Huffman entropy encoding.814*/815816GLOBAL(void)817jinit_phuff_encoder (j_compress_ptr cinfo)818{819phuff_entropy_ptr entropy;820int i;821822entropy = (phuff_entropy_ptr)823(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,824SIZEOF(phuff_entropy_encoder));825cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;826entropy->pub.start_pass = start_pass_phuff;827828/* Mark tables unallocated */829for (i = 0; i < NUM_HUFF_TBLS; i++) {830entropy->derived_tbls[i] = NULL;831entropy->count_ptrs[i] = NULL;832}833entropy->bit_buffer = NULL; /* needed only in AC refinement scan */834}835836#endif /* C_PROGRESSIVE_SUPPORTED */837838839