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/jcphuff.c
Views: 11784
/*1* jcphuff.c2*3* Copyright (C) 1995-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 for progressive JPEG.8*9* We do not support output suspension in this module, since the library10* currently does not allow multiple-scan files to be written with output11* suspension.12*/1314#define JPEG_INTERNALS15#include "jinclude.h"16#include "jpeglib.h"17#include "jchuff.h" /* Declarations shared with jchuff.c */1819#ifdef C_PROGRESSIVE_SUPPORTED2021/* Expanded entropy encoder object for progressive Huffman encoding. */2223typedef struct {24struct jpeg_entropy_encoder pub; /* public fields */2526/* Mode flag: TRUE for optimization, FALSE for actual data output */27boolean gather_statistics;2829/* Bit-level coding status.30* next_output_byte/free_in_buffer are local copies of cinfo->dest fields.31*/32JOCTET * next_output_byte; /* => next byte to write in buffer */33size_t free_in_buffer; /* # of byte spaces remaining in buffer */34INT32 put_buffer; /* current bit-accumulation buffer */35int put_bits; /* # of bits now in it */36j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */3738/* Coding status for DC components */39int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */4041/* Coding status for AC components */42int ac_tbl_no; /* the table number of the single component */43unsigned int EOBRUN; /* run length of EOBs */44unsigned int BE; /* # of buffered correction bits before MCU */45char * bit_buffer; /* buffer for correction bits (1 per char) */46/* packing correction bits tightly would save some space but cost time... */4748unsigned int restarts_to_go; /* MCUs left in this restart interval */49int next_restart_num; /* next restart number to write (0-7) */5051/* Pointers to derived tables (these workspaces have image lifespan).52* Since any one scan codes only DC or only AC, we only need one set53* of tables, not one for DC and one for AC.54*/55c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];5657/* Statistics tables for optimization; again, one set is enough */58long * count_ptrs[NUM_HUFF_TBLS];59} phuff_entropy_encoder;6061typedef phuff_entropy_encoder * phuff_entropy_ptr;6263/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit64* buffer can hold. Larger sizes may slightly improve compression, but65* 1000 is already well into the realm of overkill.66* The minimum safe size is 64 bits.67*/6869#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */7071/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.72* We assume that int right shift is unsigned if INT32 right shift is,73* which should be safe.74*/7576#ifdef RIGHT_SHIFT_IS_UNSIGNED77#define ISHIFT_TEMPS int ishift_temp;78#define IRIGHT_SHIFT(x,shft) \79((ishift_temp = (x)) < 0 ? \80(ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \81(ishift_temp >> (shft)))82#else83#define ISHIFT_TEMPS84#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))85#endif8687/* Forward declarations */88METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,89JBLOCKROW *MCU_data));90METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,91JBLOCKROW *MCU_data));92METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,93JBLOCKROW *MCU_data));94METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,95JBLOCKROW *MCU_data));96METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));97METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));9899100/*101* Initialize for a Huffman-compressed scan using progressive JPEG.102*/103104METHODDEF(void)105start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)106{107phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;108boolean is_DC_band;109int ci, tbl;110jpeg_component_info * compptr;111112entropy->cinfo = cinfo;113entropy->gather_statistics = gather_statistics;114115is_DC_band = (cinfo->Ss == 0);116117/* We assume jcmaster.c already validated the scan parameters. */118119/* Select execution routines */120if (cinfo->Ah == 0) {121if (is_DC_band)122entropy->pub.encode_mcu = encode_mcu_DC_first;123else124entropy->pub.encode_mcu = encode_mcu_AC_first;125} else {126if (is_DC_band)127entropy->pub.encode_mcu = encode_mcu_DC_refine;128else {129entropy->pub.encode_mcu = encode_mcu_AC_refine;130/* AC refinement needs a correction bit buffer */131if (entropy->bit_buffer == NULL)132entropy->bit_buffer = (char *)133(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,134MAX_CORR_BITS * SIZEOF(char));135}136}137if (gather_statistics)138entropy->pub.finish_pass = finish_pass_gather_phuff;139else140entropy->pub.finish_pass = finish_pass_phuff;141142/* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1143* for AC coefficients.144*/145for (ci = 0; ci < cinfo->comps_in_scan; ci++) {146compptr = cinfo->cur_comp_info[ci];147/* Initialize DC predictions to 0 */148entropy->last_dc_val[ci] = 0;149/* Get table index */150if (is_DC_band) {151if (cinfo->Ah != 0) /* DC refinement needs no table */152continue;153tbl = compptr->dc_tbl_no;154} else {155entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;156}157if (gather_statistics) {158/* Check for invalid table index */159/* (make_c_derived_tbl does this in the other path) */160if (tbl < 0 || tbl >= NUM_HUFF_TBLS)161ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);162/* Allocate and zero the statistics tables */163/* Note that jpeg_gen_optimal_table expects 257 entries in each table! */164if (entropy->count_ptrs[tbl] == NULL)165entropy->count_ptrs[tbl] = (long *)166(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,167257 * SIZEOF(long));168MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));169} else {170/* Compute derived values for Huffman table */171/* We may do this more than once for a table, but it's not expensive */172jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,173& entropy->derived_tbls[tbl]);174}175}176177/* Initialize AC stuff */178entropy->EOBRUN = 0;179entropy->BE = 0;180181/* Initialize bit buffer to empty */182entropy->put_buffer = 0;183entropy->put_bits = 0;184185/* Initialize restart stuff */186entropy->restarts_to_go = cinfo->restart_interval;187entropy->next_restart_num = 0;188}189190191/* Outputting bytes to the file.192* NB: these must be called only when actually outputting,193* that is, entropy->gather_statistics == FALSE.194*/195196/* Emit a byte */197#define emit_byte(entropy,val) \198{ *(entropy)->next_output_byte++ = (JOCTET) (val); \199if (--(entropy)->free_in_buffer == 0) \200dump_buffer(entropy); }201202203LOCAL(void)204dump_buffer (phuff_entropy_ptr entropy)205/* Empty the output buffer; we do not support suspension in this module. */206{207struct jpeg_destination_mgr * dest = entropy->cinfo->dest;208209if (! (*dest->empty_output_buffer) (entropy->cinfo))210ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);211/* After a successful buffer dump, must reset buffer pointers */212entropy->next_output_byte = dest->next_output_byte;213entropy->free_in_buffer = dest->free_in_buffer;214}215216217/* Outputting bits to the file */218219/* Only the right 24 bits of put_buffer are used; the valid bits are220* left-justified in this part. At most 16 bits can be passed to emit_bits221* in one call, and we never retain more than 7 bits in put_buffer222* between calls, so 24 bits are sufficient.223*/224225INLINE226LOCAL(void)227emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)228/* Emit some bits, unless we are in gather mode */229{230/* This routine is heavily used, so it's worth coding tightly. */231register INT32 put_buffer = (INT32) code;232register int put_bits = entropy->put_bits;233234/* if size is 0, caller used an invalid Huffman table entry */235if (size == 0)236ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);237238if (entropy->gather_statistics)239return; /* do nothing if we're only getting stats */240241put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */242243put_bits += size; /* new number of bits in buffer */244245put_buffer <<= 24 - put_bits; /* align incoming bits */246247put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */248249while (put_bits >= 8) {250int c = (int) ((put_buffer >> 16) & 0xFF);251252emit_byte(entropy, c);253if (c == 0xFF) { /* need to stuff a zero byte? */254emit_byte(entropy, 0);255}256put_buffer <<= 8;257put_bits -= 8;258}259260entropy->put_buffer = put_buffer; /* update variables */261entropy->put_bits = put_bits;262}263264265LOCAL(void)266flush_bits (phuff_entropy_ptr entropy)267{268emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */269entropy->put_buffer = 0; /* and reset bit-buffer to empty */270entropy->put_bits = 0;271}272273274/*275* Emit (or just count) a Huffman symbol.276*/277278INLINE279LOCAL(void)280emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)281{282if (entropy->gather_statistics)283entropy->count_ptrs[tbl_no][symbol]++;284else {285c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];286emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);287}288}289290291/*292* Emit bits from a correction bit buffer.293*/294295LOCAL(void)296emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,297unsigned int nbits)298{299if (entropy->gather_statistics)300return; /* no real work */301302while (nbits > 0) {303emit_bits(entropy, (unsigned int) (*bufstart), 1);304bufstart++;305nbits--;306}307}308309310/*311* Emit any pending EOBRUN symbol.312*/313314LOCAL(void)315emit_eobrun (phuff_entropy_ptr entropy)316{317register int temp, nbits;318319if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */320temp = entropy->EOBRUN;321nbits = 0;322while ((temp >>= 1))323nbits++;324/* safety check: shouldn't happen given limited correction-bit buffer */325if (nbits > 14)326ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);327328emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);329if (nbits)330emit_bits(entropy, entropy->EOBRUN, nbits);331332entropy->EOBRUN = 0;333334/* Emit any buffered correction bits */335emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);336entropy->BE = 0;337}338}339340341/*342* Emit a restart marker & resynchronize predictions.343*/344345LOCAL(void)346emit_restart (phuff_entropy_ptr entropy, int restart_num)347{348int ci;349350emit_eobrun(entropy);351352if (! entropy->gather_statistics) {353flush_bits(entropy);354emit_byte(entropy, 0xFF);355emit_byte(entropy, JPEG_RST0 + restart_num);356}357358if (entropy->cinfo->Ss == 0) {359/* Re-initialize DC predictions to 0 */360for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)361entropy->last_dc_val[ci] = 0;362} else {363/* Re-initialize all AC-related fields to 0 */364entropy->EOBRUN = 0;365entropy->BE = 0;366}367}368369370/*371* MCU encoding for DC initial scan (either spectral selection,372* or first pass of successive approximation).373*/374375METHODDEF(boolean)376encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)377{378phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;379register int temp, temp2;380register int nbits;381int blkn, ci;382int Al = cinfo->Al;383JBLOCKROW block;384jpeg_component_info * compptr;385ISHIFT_TEMPS386387entropy->next_output_byte = cinfo->dest->next_output_byte;388entropy->free_in_buffer = cinfo->dest->free_in_buffer;389390/* Emit restart marker if needed */391if (cinfo->restart_interval)392if (entropy->restarts_to_go == 0)393emit_restart(entropy, entropy->next_restart_num);394395/* Encode the MCU data blocks */396for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {397block = MCU_data[blkn];398ci = cinfo->MCU_membership[blkn];399compptr = cinfo->cur_comp_info[ci];400401/* Compute the DC value after the required point transform by Al.402* This is simply an arithmetic right shift.403*/404temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);405406/* DC differences are figured on the point-transformed values. */407temp = temp2 - entropy->last_dc_val[ci];408entropy->last_dc_val[ci] = temp2;409410/* Encode the DC coefficient difference per section G.1.2.1 */411temp2 = temp;412if (temp < 0) {413temp = -temp; /* temp is abs value of input */414/* For a negative input, want temp2 = bitwise complement of abs(input) */415/* This code assumes we are on a two's complement machine */416temp2--;417}418419/* Find the number of bits needed for the magnitude of the coefficient */420nbits = 0;421while (temp) {422nbits++;423temp >>= 1;424}425/* Check for out-of-range coefficient values.426* Since we're encoding a difference, the range limit is twice as much.427*/428if (nbits > MAX_COEF_BITS+1)429ERREXIT(cinfo, JERR_BAD_DCT_COEF);430431/* Count/emit the Huffman-coded symbol for the number of bits */432emit_symbol(entropy, compptr->dc_tbl_no, nbits);433434/* Emit that number of bits of the value, if positive, */435/* or the complement of its magnitude, if negative. */436if (nbits) /* emit_bits rejects calls with size 0 */437emit_bits(entropy, (unsigned int) temp2, nbits);438}439440cinfo->dest->next_output_byte = entropy->next_output_byte;441cinfo->dest->free_in_buffer = entropy->free_in_buffer;442443/* Update restart-interval state too */444if (cinfo->restart_interval) {445if (entropy->restarts_to_go == 0) {446entropy->restarts_to_go = cinfo->restart_interval;447entropy->next_restart_num++;448entropy->next_restart_num &= 7;449}450entropy->restarts_to_go--;451}452453return TRUE;454}455456457/*458* MCU encoding for AC initial scan (either spectral selection,459* or first pass of successive approximation).460*/461462METHODDEF(boolean)463encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)464{465phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;466register int temp, temp2;467register int nbits;468register int r, k;469int Se = cinfo->Se;470int Al = cinfo->Al;471JBLOCKROW block;472473entropy->next_output_byte = cinfo->dest->next_output_byte;474entropy->free_in_buffer = cinfo->dest->free_in_buffer;475476/* Emit restart marker if needed */477if (cinfo->restart_interval)478if (entropy->restarts_to_go == 0)479emit_restart(entropy, entropy->next_restart_num);480481/* Encode the MCU data block */482block = MCU_data[0];483484/* Encode the AC coefficients per section G.1.2.2, fig. G.3 */485486r = 0; /* r = run length of zeros */487488for (k = cinfo->Ss; k <= Se; k++) {489if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {490r++;491continue;492}493/* We must apply the point transform by Al. For AC coefficients this494* is an integer division with rounding towards 0. To do this portably495* in C, we shift after obtaining the absolute value; so the code is496* interwoven with finding the abs value (temp) and output bits (temp2).497*/498if (temp < 0) {499temp = -temp; /* temp is abs value of input */500temp >>= Al; /* apply the point transform */501/* For a negative coef, want temp2 = bitwise complement of abs(coef) */502temp2 = ~temp;503} else {504temp >>= Al; /* apply the point transform */505temp2 = temp;506}507/* Watch out for case that nonzero coef is zero after point transform */508if (temp == 0) {509r++;510continue;511}512513/* Emit any pending EOBRUN */514if (entropy->EOBRUN > 0)515emit_eobrun(entropy);516/* if run length > 15, must emit special run-length-16 codes (0xF0) */517while (r > 15) {518emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);519r -= 16;520}521522/* Find the number of bits needed for the magnitude of the coefficient */523nbits = 1; /* there must be at least one 1 bit */524while ((temp >>= 1))525nbits++;526/* Check for out-of-range coefficient values */527if (nbits > MAX_COEF_BITS)528ERREXIT(cinfo, JERR_BAD_DCT_COEF);529530/* Count/emit Huffman symbol for run length / number of bits */531emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);532533/* Emit that number of bits of the value, if positive, */534/* or the complement of its magnitude, if negative. */535emit_bits(entropy, (unsigned int) temp2, nbits);536537r = 0; /* reset zero run length */538}539540if (r > 0) { /* If there are trailing zeroes, */541entropy->EOBRUN++; /* count an EOB */542if (entropy->EOBRUN == 0x7FFF)543emit_eobrun(entropy); /* force it out to avoid overflow */544}545546cinfo->dest->next_output_byte = entropy->next_output_byte;547cinfo->dest->free_in_buffer = entropy->free_in_buffer;548549/* Update restart-interval state too */550if (cinfo->restart_interval) {551if (entropy->restarts_to_go == 0) {552entropy->restarts_to_go = cinfo->restart_interval;553entropy->next_restart_num++;554entropy->next_restart_num &= 7;555}556entropy->restarts_to_go--;557}558559return TRUE;560}561562563/*564* MCU encoding for DC successive approximation refinement scan.565* Note: we assume such scans can be multi-component, although the spec566* is not very clear on the point.567*/568569METHODDEF(boolean)570encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)571{572phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;573register int temp;574int blkn;575int Al = cinfo->Al;576JBLOCKROW block;577578entropy->next_output_byte = cinfo->dest->next_output_byte;579entropy->free_in_buffer = cinfo->dest->free_in_buffer;580581/* Emit restart marker if needed */582if (cinfo->restart_interval)583if (entropy->restarts_to_go == 0)584emit_restart(entropy, entropy->next_restart_num);585586/* Encode the MCU data blocks */587for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {588block = MCU_data[blkn];589590/* We simply emit the Al'th bit of the DC coefficient value. */591temp = (*block)[0];592emit_bits(entropy, (unsigned int) (temp >> Al), 1);593}594595cinfo->dest->next_output_byte = entropy->next_output_byte;596cinfo->dest->free_in_buffer = entropy->free_in_buffer;597598/* Update restart-interval state too */599if (cinfo->restart_interval) {600if (entropy->restarts_to_go == 0) {601entropy->restarts_to_go = cinfo->restart_interval;602entropy->next_restart_num++;603entropy->next_restart_num &= 7;604}605entropy->restarts_to_go--;606}607608return TRUE;609}610611612/*613* MCU encoding for AC successive approximation refinement scan.614*/615616METHODDEF(boolean)617encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)618{619phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;620register int temp;621register int r, k;622int EOB;623char *BR_buffer;624unsigned int BR;625int Se = cinfo->Se;626int Al = cinfo->Al;627JBLOCKROW block;628int absvalues[DCTSIZE2];629630entropy->next_output_byte = cinfo->dest->next_output_byte;631entropy->free_in_buffer = cinfo->dest->free_in_buffer;632633/* Emit restart marker if needed */634if (cinfo->restart_interval)635if (entropy->restarts_to_go == 0)636emit_restart(entropy, entropy->next_restart_num);637638/* Encode the MCU data block */639block = MCU_data[0];640641/* It is convenient to make a pre-pass to determine the transformed642* coefficients' absolute values and the EOB position.643*/644EOB = 0;645for (k = cinfo->Ss; k <= Se; k++) {646temp = (*block)[jpeg_natural_order[k]];647/* We must apply the point transform by Al. For AC coefficients this648* is an integer division with rounding towards 0. To do this portably649* in C, we shift after obtaining the absolute value.650*/651if (temp < 0)652temp = -temp; /* temp is abs value of input */653temp >>= Al; /* apply the point transform */654absvalues[k] = temp; /* save abs value for main pass */655if (temp == 1)656EOB = k; /* EOB = index of last newly-nonzero coef */657}658659/* Encode the AC coefficients per section G.1.2.3, fig. G.7 */660661r = 0; /* r = run length of zeros */662BR = 0; /* BR = count of buffered bits added now */663BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */664665for (k = cinfo->Ss; k <= Se; k++) {666if ((temp = absvalues[k]) == 0) {667r++;668continue;669}670671/* Emit any required ZRLs, but not if they can be folded into EOB */672while (r > 15 && k <= EOB) {673/* emit any pending EOBRUN and the BE correction bits */674emit_eobrun(entropy);675/* Emit ZRL */676emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);677r -= 16;678/* Emit buffered correction bits that must be associated with ZRL */679emit_buffered_bits(entropy, BR_buffer, BR);680BR_buffer = entropy->bit_buffer; /* BE bits are gone now */681BR = 0;682}683684/* If the coef was previously nonzero, it only needs a correction bit.685* NOTE: a straight translation of the spec's figure G.7 would suggest686* that we also need to test r > 15. But if r > 15, we can only get here687* if k > EOB, which implies that this coefficient is not 1.688*/689if (temp > 1) {690/* The correction bit is the next bit of the absolute value. */691BR_buffer[BR++] = (char) (temp & 1);692continue;693}694695/* Emit any pending EOBRUN and the BE correction bits */696emit_eobrun(entropy);697698/* Count/emit Huffman symbol for run length / number of bits */699emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);700701/* Emit output bit for newly-nonzero coef */702temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;703emit_bits(entropy, (unsigned int) temp, 1);704705/* Emit buffered correction bits that must be associated with this code */706emit_buffered_bits(entropy, BR_buffer, BR);707BR_buffer = entropy->bit_buffer; /* BE bits are gone now */708BR = 0;709r = 0; /* reset zero run length */710}711712if (r > 0 || BR > 0) { /* If there are trailing zeroes, */713entropy->EOBRUN++; /* count an EOB */714entropy->BE += BR; /* concat my correction bits to older ones */715/* We force out the EOB if we risk either:716* 1. overflow of the EOB counter;717* 2. overflow of the correction bit buffer during the next MCU.718*/719if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))720emit_eobrun(entropy);721}722723cinfo->dest->next_output_byte = entropy->next_output_byte;724cinfo->dest->free_in_buffer = entropy->free_in_buffer;725726/* Update restart-interval state too */727if (cinfo->restart_interval) {728if (entropy->restarts_to_go == 0) {729entropy->restarts_to_go = cinfo->restart_interval;730entropy->next_restart_num++;731entropy->next_restart_num &= 7;732}733entropy->restarts_to_go--;734}735736return TRUE;737}738739740/*741* Finish up at the end of a Huffman-compressed progressive scan.742*/743744METHODDEF(void)745finish_pass_phuff (j_compress_ptr cinfo)746{747phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;748749entropy->next_output_byte = cinfo->dest->next_output_byte;750entropy->free_in_buffer = cinfo->dest->free_in_buffer;751752/* Flush out any buffered data */753emit_eobrun(entropy);754flush_bits(entropy);755756cinfo->dest->next_output_byte = entropy->next_output_byte;757cinfo->dest->free_in_buffer = entropy->free_in_buffer;758}759760761/*762* Finish up a statistics-gathering pass and create the new Huffman tables.763*/764765METHODDEF(void)766finish_pass_gather_phuff (j_compress_ptr cinfo)767{768phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;769boolean is_DC_band;770int ci, tbl;771jpeg_component_info * compptr;772JHUFF_TBL **htblptr;773boolean did[NUM_HUFF_TBLS];774775/* Flush out buffered data (all we care about is counting the EOB symbol) */776emit_eobrun(entropy);777778is_DC_band = (cinfo->Ss == 0);779780/* It's important not to apply jpeg_gen_optimal_table more than once781* per table, because it clobbers the input frequency counts!782*/783MEMZERO(did, SIZEOF(did));784785for (ci = 0; ci < cinfo->comps_in_scan; ci++) {786compptr = cinfo->cur_comp_info[ci];787if (is_DC_band) {788if (cinfo->Ah != 0) /* DC refinement needs no table */789continue;790tbl = compptr->dc_tbl_no;791} else {792tbl = compptr->ac_tbl_no;793}794if (! did[tbl]) {795if (is_DC_band)796htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];797else798htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];799if (*htblptr == NULL)800*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);801jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);802did[tbl] = TRUE;803}804}805}806807808/*809* Module initialization routine for progressive Huffman entropy encoding.810*/811812GLOBAL(void)813jinit_phuff_encoder (j_compress_ptr cinfo)814{815phuff_entropy_ptr entropy;816int i;817818entropy = (phuff_entropy_ptr)819(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,820SIZEOF(phuff_entropy_encoder));821cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;822entropy->pub.start_pass = start_pass_phuff;823824/* Mark tables unallocated */825for (i = 0; i < NUM_HUFF_TBLS; i++) {826entropy->derived_tbls[i] = NULL;827entropy->count_ptrs[i] = NULL;828}829entropy->bit_buffer = NULL; /* needed only in AC refinement scan */830}831832#endif /* C_PROGRESSIVE_SUPPORTED */833834835