0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jcphuff.c
0N/A *
0N/A * Copyright (C) 1995-1997, Thomas G. Lane.
0N/A * This file is part of the Independent JPEG Group's software.
0N/A * For conditions of distribution and use, see the accompanying README file.
0N/A *
0N/A * This file contains Huffman entropy encoding routines for progressive JPEG.
0N/A *
0N/A * We do not support output suspension in this module, since the library
0N/A * currently does not allow multiple-scan files to be written with output
0N/A * suspension.
0N/A */
0N/A
0N/A#define JPEG_INTERNALS
0N/A#include "jinclude.h"
0N/A#include "jpeglib.h"
0N/A#include "jchuff.h" /* Declarations shared with jchuff.c */
0N/A
0N/A#ifdef C_PROGRESSIVE_SUPPORTED
0N/A
0N/A/* Expanded entropy encoder object for progressive Huffman encoding. */
0N/A
0N/Atypedef struct {
0N/A struct jpeg_entropy_encoder pub; /* public fields */
0N/A
0N/A /* Mode flag: TRUE for optimization, FALSE for actual data output */
0N/A boolean gather_statistics;
0N/A
0N/A /* Bit-level coding status.
0N/A * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
0N/A */
0N/A JOCTET * next_output_byte; /* => next byte to write in buffer */
0N/A size_t free_in_buffer; /* # of byte spaces remaining in buffer */
0N/A INT32 put_buffer; /* current bit-accumulation buffer */
0N/A int put_bits; /* # of bits now in it */
0N/A j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
0N/A
0N/A /* Coding status for DC components */
0N/A int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
0N/A
0N/A /* Coding status for AC components */
0N/A int ac_tbl_no; /* the table number of the single component */
0N/A unsigned int EOBRUN; /* run length of EOBs */
0N/A unsigned int BE; /* # of buffered correction bits before MCU */
0N/A char * bit_buffer; /* buffer for correction bits (1 per char) */
0N/A /* packing correction bits tightly would save some space but cost time... */
0N/A
0N/A unsigned int restarts_to_go; /* MCUs left in this restart interval */
0N/A int next_restart_num; /* next restart number to write (0-7) */
0N/A
0N/A /* Pointers to derived tables (these workspaces have image lifespan).
0N/A * Since any one scan codes only DC or only AC, we only need one set
0N/A * of tables, not one for DC and one for AC.
0N/A */
0N/A c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
0N/A
0N/A /* Statistics tables for optimization; again, one set is enough */
0N/A long * count_ptrs[NUM_HUFF_TBLS];
0N/A} phuff_entropy_encoder;
0N/A
0N/Atypedef phuff_entropy_encoder * phuff_entropy_ptr;
0N/A
0N/A/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
0N/A * buffer can hold. Larger sizes may slightly improve compression, but
0N/A * 1000 is already well into the realm of overkill.
0N/A * The minimum safe size is 64 bits.
0N/A */
0N/A
0N/A#define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
0N/A
0N/A/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
0N/A * We assume that int right shift is unsigned if INT32 right shift is,
0N/A * which should be safe.
0N/A */
0N/A
0N/A#ifdef RIGHT_SHIFT_IS_UNSIGNED
0N/A#define ISHIFT_TEMPS int ishift_temp;
0N/A#define IRIGHT_SHIFT(x,shft) \
0N/A ((ishift_temp = (x)) < 0 ? \
0N/A (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
0N/A (ishift_temp >> (shft)))
0N/A#else
0N/A#define ISHIFT_TEMPS
0N/A#define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
0N/A#endif
0N/A
0N/A/* Forward declarations */
0N/AMETHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,
0N/A JBLOCKROW *MCU_data));
0N/AMETHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
0N/A JBLOCKROW *MCU_data));
0N/AMETHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,
0N/A JBLOCKROW *MCU_data));
0N/AMETHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
0N/A JBLOCKROW *MCU_data));
0N/AMETHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
0N/AMETHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));
0N/A
0N/A
0N/A/*
0N/A * Initialize for a Huffman-compressed scan using progressive JPEG.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Astart_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
0N/A{
0N/A phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
0N/A boolean is_DC_band;
0N/A int ci, tbl;
0N/A jpeg_component_info * compptr;
0N/A
0N/A entropy->cinfo = cinfo;
0N/A entropy->gather_statistics = gather_statistics;
0N/A
0N/A is_DC_band = (cinfo->Ss == 0);
0N/A
0N/A /* We assume jcmaster.c already validated the scan parameters. */
0N/A
0N/A /* Select execution routines */
0N/A if (cinfo->Ah == 0) {
0N/A if (is_DC_band)
0N/A entropy->pub.encode_mcu = encode_mcu_DC_first;
0N/A else
0N/A entropy->pub.encode_mcu = encode_mcu_AC_first;
0N/A } else {
0N/A if (is_DC_band)
0N/A entropy->pub.encode_mcu = encode_mcu_DC_refine;
0N/A else {
0N/A entropy->pub.encode_mcu = encode_mcu_AC_refine;
0N/A /* AC refinement needs a correction bit buffer */
0N/A if (entropy->bit_buffer == NULL)
0N/A entropy->bit_buffer = (char *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A MAX_CORR_BITS * SIZEOF(char));
0N/A }
0N/A }
0N/A if (gather_statistics)
0N/A entropy->pub.finish_pass = finish_pass_gather_phuff;
0N/A else
0N/A entropy->pub.finish_pass = finish_pass_phuff;
0N/A
0N/A /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
0N/A * for AC coefficients.
0N/A */
0N/A for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
0N/A compptr = cinfo->cur_comp_info[ci];
0N/A /* Initialize DC predictions to 0 */
0N/A entropy->last_dc_val[ci] = 0;
0N/A /* Get table index */
0N/A if (is_DC_band) {
0N/A if (cinfo->Ah != 0) /* DC refinement needs no table */
0N/A continue;
0N/A tbl = compptr->dc_tbl_no;
0N/A } else {
0N/A entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
0N/A }
0N/A if (gather_statistics) {
0N/A /* Check for invalid table index */
0N/A /* (make_c_derived_tbl does this in the other path) */
0N/A if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
0N/A ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
0N/A /* Allocate and zero the statistics tables */
0N/A /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
0N/A if (entropy->count_ptrs[tbl] == NULL)
0N/A entropy->count_ptrs[tbl] = (long *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A 257 * SIZEOF(long));
0N/A MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
0N/A } else {
0N/A /* Compute derived values for Huffman table */
0N/A /* We may do this more than once for a table, but it's not expensive */
0N/A jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
0N/A & entropy->derived_tbls[tbl]);
0N/A }
0N/A }
0N/A
0N/A /* Initialize AC stuff */
0N/A entropy->EOBRUN = 0;
0N/A entropy->BE = 0;
0N/A
0N/A /* Initialize bit buffer to empty */
0N/A entropy->put_buffer = 0;
0N/A entropy->put_bits = 0;
0N/A
0N/A /* Initialize restart stuff */
0N/A entropy->restarts_to_go = cinfo->restart_interval;
0N/A entropy->next_restart_num = 0;
0N/A}
0N/A
0N/A
0N/A/* Outputting bytes to the file.
0N/A * NB: these must be called only when actually outputting,
0N/A * that is, entropy->gather_statistics == FALSE.
0N/A */
0N/A
0N/A/* Emit a byte */
0N/A#define emit_byte(entropy,val) \
0N/A { *(entropy)->next_output_byte++ = (JOCTET) (val); \
0N/A if (--(entropy)->free_in_buffer == 0) \
0N/A dump_buffer(entropy); }
0N/A
0N/A
0N/ALOCAL(void)
0N/Adump_buffer (phuff_entropy_ptr entropy)
0N/A/* Empty the output buffer; we do not support suspension in this module. */
0N/A{
0N/A struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
0N/A
0N/A if (! (*dest->empty_output_buffer) (entropy->cinfo))
0N/A ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
0N/A /* After a successful buffer dump, must reset buffer pointers */
0N/A entropy->next_output_byte = dest->next_output_byte;
0N/A entropy->free_in_buffer = dest->free_in_buffer;
0N/A}
0N/A
0N/A
0N/A/* Outputting bits to the file */
0N/A
0N/A/* Only the right 24 bits of put_buffer are used; the valid bits are
0N/A * left-justified in this part. At most 16 bits can be passed to emit_bits
0N/A * in one call, and we never retain more than 7 bits in put_buffer
0N/A * between calls, so 24 bits are sufficient.
0N/A */
0N/A
0N/AINLINE
0N/ALOCAL(void)
0N/Aemit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
0N/A/* Emit some bits, unless we are in gather mode */
0N/A{
0N/A /* This routine is heavily used, so it's worth coding tightly. */
0N/A register INT32 put_buffer = (INT32) code;
0N/A register int put_bits = entropy->put_bits;
0N/A
0N/A /* if size is 0, caller used an invalid Huffman table entry */
0N/A if (size == 0)
0N/A ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
0N/A
0N/A if (entropy->gather_statistics)
0N/A return; /* do nothing if we're only getting stats */
0N/A
0N/A put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
0N/A
0N/A put_bits += size; /* new number of bits in buffer */
0N/A
0N/A put_buffer <<= 24 - put_bits; /* align incoming bits */
0N/A
0N/A put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
0N/A
0N/A while (put_bits >= 8) {
0N/A int c = (int) ((put_buffer >> 16) & 0xFF);
0N/A
0N/A emit_byte(entropy, c);
0N/A if (c == 0xFF) { /* need to stuff a zero byte? */
0N/A emit_byte(entropy, 0);
0N/A }
0N/A put_buffer <<= 8;
0N/A put_bits -= 8;
0N/A }
0N/A
0N/A entropy->put_buffer = put_buffer; /* update variables */
0N/A entropy->put_bits = put_bits;
0N/A}
0N/A
0N/A
0N/ALOCAL(void)
0N/Aflush_bits (phuff_entropy_ptr entropy)
0N/A{
0N/A emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
0N/A entropy->put_buffer = 0; /* and reset bit-buffer to empty */
0N/A entropy->put_bits = 0;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Emit (or just count) a Huffman symbol.
0N/A */
0N/A
0N/AINLINE
0N/ALOCAL(void)
0N/Aemit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
0N/A{
0N/A if (entropy->gather_statistics)
0N/A entropy->count_ptrs[tbl_no][symbol]++;
0N/A else {
0N/A c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
0N/A emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Emit bits from a correction bit buffer.
0N/A */
0N/A
0N/ALOCAL(void)
0N/Aemit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
0N/A unsigned int nbits)
0N/A{
0N/A if (entropy->gather_statistics)
0N/A return; /* no real work */
0N/A
0N/A while (nbits > 0) {
0N/A emit_bits(entropy, (unsigned int) (*bufstart), 1);
0N/A bufstart++;
0N/A nbits--;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Emit any pending EOBRUN symbol.
0N/A */
0N/A
0N/ALOCAL(void)
0N/Aemit_eobrun (phuff_entropy_ptr entropy)
0N/A{
0N/A register int temp, nbits;
0N/A
0N/A if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
0N/A temp = entropy->EOBRUN;
0N/A nbits = 0;
0N/A while ((temp >>= 1))
0N/A nbits++;
0N/A /* safety check: shouldn't happen given limited correction-bit buffer */
0N/A if (nbits > 14)
0N/A ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
0N/A
0N/A emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
0N/A if (nbits)
0N/A emit_bits(entropy, entropy->EOBRUN, nbits);
0N/A
0N/A entropy->EOBRUN = 0;
0N/A
0N/A /* Emit any buffered correction bits */
0N/A emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
0N/A entropy->BE = 0;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Emit a restart marker & resynchronize predictions.
0N/A */
0N/A
0N/ALOCAL(void)
0N/Aemit_restart (phuff_entropy_ptr entropy, int restart_num)
0N/A{
0N/A int ci;
0N/A
0N/A emit_eobrun(entropy);
0N/A
0N/A if (! entropy->gather_statistics) {
0N/A flush_bits(entropy);
0N/A emit_byte(entropy, 0xFF);
0N/A emit_byte(entropy, JPEG_RST0 + restart_num);
0N/A }
0N/A
0N/A if (entropy->cinfo->Ss == 0) {
0N/A /* Re-initialize DC predictions to 0 */
0N/A for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
0N/A entropy->last_dc_val[ci] = 0;
0N/A } else {
0N/A /* Re-initialize all AC-related fields to 0 */
0N/A entropy->EOBRUN = 0;
0N/A entropy->BE = 0;
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * MCU encoding for DC initial scan (either spectral selection,
0N/A * or first pass of successive approximation).
0N/A */
0N/A
0N/AMETHODDEF(boolean)
0N/Aencode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
0N/A{
0N/A phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
0N/A register int temp, temp2;
0N/A register int nbits;
0N/A int blkn, ci;
0N/A int Al = cinfo->Al;
0N/A JBLOCKROW block;
0N/A jpeg_component_info * compptr;
0N/A ISHIFT_TEMPS
0N/A
0N/A entropy->next_output_byte = cinfo->dest->next_output_byte;
0N/A entropy->free_in_buffer = cinfo->dest->free_in_buffer;
0N/A
0N/A /* Emit restart marker if needed */
0N/A if (cinfo->restart_interval)
0N/A if (entropy->restarts_to_go == 0)
0N/A emit_restart(entropy, entropy->next_restart_num);
0N/A
0N/A /* Encode the MCU data blocks */
0N/A for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
0N/A block = MCU_data[blkn];
0N/A ci = cinfo->MCU_membership[blkn];
0N/A compptr = cinfo->cur_comp_info[ci];
0N/A
0N/A /* Compute the DC value after the required point transform by Al.
0N/A * This is simply an arithmetic right shift.
0N/A */
0N/A temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
0N/A
0N/A /* DC differences are figured on the point-transformed values. */
0N/A temp = temp2 - entropy->last_dc_val[ci];
0N/A entropy->last_dc_val[ci] = temp2;
0N/A
0N/A /* Encode the DC coefficient difference per section G.1.2.1 */
0N/A temp2 = temp;
0N/A if (temp < 0) {
0N/A temp = -temp; /* temp is abs value of input */
0N/A /* For a negative input, want temp2 = bitwise complement of abs(input) */
0N/A /* This code assumes we are on a two's complement machine */
0N/A temp2--;
0N/A }
0N/A
0N/A /* Find the number of bits needed for the magnitude of the coefficient */
0N/A nbits = 0;
0N/A while (temp) {
0N/A nbits++;
0N/A temp >>= 1;
0N/A }
0N/A /* Check for out-of-range coefficient values.
0N/A * Since we're encoding a difference, the range limit is twice as much.
0N/A */
0N/A if (nbits > MAX_COEF_BITS+1)
0N/A ERREXIT(cinfo, JERR_BAD_DCT_COEF);
0N/A
0N/A /* Count/emit the Huffman-coded symbol for the number of bits */
0N/A emit_symbol(entropy, compptr->dc_tbl_no, nbits);
0N/A
0N/A /* Emit that number of bits of the value, if positive, */
0N/A /* or the complement of its magnitude, if negative. */
0N/A if (nbits) /* emit_bits rejects calls with size 0 */
0N/A emit_bits(entropy, (unsigned int) temp2, nbits);
0N/A }
0N/A
0N/A cinfo->dest->next_output_byte = entropy->next_output_byte;
0N/A cinfo->dest->free_in_buffer = entropy->free_in_buffer;
0N/A
0N/A /* Update restart-interval state too */
0N/A if (cinfo->restart_interval) {
0N/A if (entropy->restarts_to_go == 0) {
0N/A entropy->restarts_to_go = cinfo->restart_interval;
0N/A entropy->next_restart_num++;
0N/A entropy->next_restart_num &= 7;
0N/A }
0N/A entropy->restarts_to_go--;
0N/A }
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * MCU encoding for AC initial scan (either spectral selection,
0N/A * or first pass of successive approximation).
0N/A */
0N/A
0N/AMETHODDEF(boolean)
0N/Aencode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
0N/A{
0N/A phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
0N/A register int temp, temp2;
0N/A register int nbits;
0N/A register int r, k;
0N/A int Se = cinfo->Se;
0N/A int Al = cinfo->Al;
0N/A JBLOCKROW block;
0N/A
0N/A entropy->next_output_byte = cinfo->dest->next_output_byte;
0N/A entropy->free_in_buffer = cinfo->dest->free_in_buffer;
0N/A
0N/A /* Emit restart marker if needed */
0N/A if (cinfo->restart_interval)
0N/A if (entropy->restarts_to_go == 0)
0N/A emit_restart(entropy, entropy->next_restart_num);
0N/A
0N/A /* Encode the MCU data block */
0N/A block = MCU_data[0];
0N/A
0N/A /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
0N/A
0N/A r = 0; /* r = run length of zeros */
0N/A
0N/A for (k = cinfo->Ss; k <= Se; k++) {
0N/A if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
0N/A r++;
0N/A continue;
0N/A }
0N/A /* We must apply the point transform by Al. For AC coefficients this
0N/A * is an integer division with rounding towards 0. To do this portably
0N/A * in C, we shift after obtaining the absolute value; so the code is
0N/A * interwoven with finding the abs value (temp) and output bits (temp2).
0N/A */
0N/A if (temp < 0) {
0N/A temp = -temp; /* temp is abs value of input */
0N/A temp >>= Al; /* apply the point transform */
0N/A /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
0N/A temp2 = ~temp;
0N/A } else {
0N/A temp >>= Al; /* apply the point transform */
0N/A temp2 = temp;
0N/A }
0N/A /* Watch out for case that nonzero coef is zero after point transform */
0N/A if (temp == 0) {
0N/A r++;
0N/A continue;
0N/A }
0N/A
0N/A /* Emit any pending EOBRUN */
0N/A if (entropy->EOBRUN > 0)
0N/A emit_eobrun(entropy);
0N/A /* if run length > 15, must emit special run-length-16 codes (0xF0) */
0N/A while (r > 15) {
0N/A emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
0N/A r -= 16;
0N/A }
0N/A
0N/A /* Find the number of bits needed for the magnitude of the coefficient */
0N/A nbits = 1; /* there must be at least one 1 bit */
0N/A while ((temp >>= 1))
0N/A nbits++;
0N/A /* Check for out-of-range coefficient values */
0N/A if (nbits > MAX_COEF_BITS)
0N/A ERREXIT(cinfo, JERR_BAD_DCT_COEF);
0N/A
0N/A /* Count/emit Huffman symbol for run length / number of bits */
0N/A emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
0N/A
0N/A /* Emit that number of bits of the value, if positive, */
0N/A /* or the complement of its magnitude, if negative. */
0N/A emit_bits(entropy, (unsigned int) temp2, nbits);
0N/A
0N/A r = 0; /* reset zero run length */
0N/A }
0N/A
0N/A if (r > 0) { /* If there are trailing zeroes, */
0N/A entropy->EOBRUN++; /* count an EOB */
0N/A if (entropy->EOBRUN == 0x7FFF)
0N/A emit_eobrun(entropy); /* force it out to avoid overflow */
0N/A }
0N/A
0N/A cinfo->dest->next_output_byte = entropy->next_output_byte;
0N/A cinfo->dest->free_in_buffer = entropy->free_in_buffer;
0N/A
0N/A /* Update restart-interval state too */
0N/A if (cinfo->restart_interval) {
0N/A if (entropy->restarts_to_go == 0) {
0N/A entropy->restarts_to_go = cinfo->restart_interval;
0N/A entropy->next_restart_num++;
0N/A entropy->next_restart_num &= 7;
0N/A }
0N/A entropy->restarts_to_go--;
0N/A }
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * MCU encoding for DC successive approximation refinement scan.
0N/A * Note: we assume such scans can be multi-component, although the spec
0N/A * is not very clear on the point.
0N/A */
0N/A
0N/AMETHODDEF(boolean)
0N/Aencode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
0N/A{
0N/A phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
0N/A register int temp;
0N/A int blkn;
0N/A int Al = cinfo->Al;
0N/A JBLOCKROW block;
0N/A
0N/A entropy->next_output_byte = cinfo->dest->next_output_byte;
0N/A entropy->free_in_buffer = cinfo->dest->free_in_buffer;
0N/A
0N/A /* Emit restart marker if needed */
0N/A if (cinfo->restart_interval)
0N/A if (entropy->restarts_to_go == 0)
0N/A emit_restart(entropy, entropy->next_restart_num);
0N/A
0N/A /* Encode the MCU data blocks */
0N/A for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
0N/A block = MCU_data[blkn];
0N/A
0N/A /* We simply emit the Al'th bit of the DC coefficient value. */
0N/A temp = (*block)[0];
0N/A emit_bits(entropy, (unsigned int) (temp >> Al), 1);
0N/A }
0N/A
0N/A cinfo->dest->next_output_byte = entropy->next_output_byte;
0N/A cinfo->dest->free_in_buffer = entropy->free_in_buffer;
0N/A
0N/A /* Update restart-interval state too */
0N/A if (cinfo->restart_interval) {
0N/A if (entropy->restarts_to_go == 0) {
0N/A entropy->restarts_to_go = cinfo->restart_interval;
0N/A entropy->next_restart_num++;
0N/A entropy->next_restart_num &= 7;
0N/A }
0N/A entropy->restarts_to_go--;
0N/A }
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * MCU encoding for AC successive approximation refinement scan.
0N/A */
0N/A
0N/AMETHODDEF(boolean)
0N/Aencode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
0N/A{
0N/A phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
0N/A register int temp;
0N/A register int r, k;
0N/A int EOB;
0N/A char *BR_buffer;
0N/A unsigned int BR;
0N/A int Se = cinfo->Se;
0N/A int Al = cinfo->Al;
0N/A JBLOCKROW block;
0N/A int absvalues[DCTSIZE2];
0N/A
0N/A entropy->next_output_byte = cinfo->dest->next_output_byte;
0N/A entropy->free_in_buffer = cinfo->dest->free_in_buffer;
0N/A
0N/A /* Emit restart marker if needed */
0N/A if (cinfo->restart_interval)
0N/A if (entropy->restarts_to_go == 0)
0N/A emit_restart(entropy, entropy->next_restart_num);
0N/A
0N/A /* Encode the MCU data block */
0N/A block = MCU_data[0];
0N/A
0N/A /* It is convenient to make a pre-pass to determine the transformed
0N/A * coefficients' absolute values and the EOB position.
0N/A */
0N/A EOB = 0;
0N/A for (k = cinfo->Ss; k <= Se; k++) {
0N/A temp = (*block)[jpeg_natural_order[k]];
0N/A /* We must apply the point transform by Al. For AC coefficients this
0N/A * is an integer division with rounding towards 0. To do this portably
0N/A * in C, we shift after obtaining the absolute value.
0N/A */
0N/A if (temp < 0)
0N/A temp = -temp; /* temp is abs value of input */
0N/A temp >>= Al; /* apply the point transform */
0N/A absvalues[k] = temp; /* save abs value for main pass */
0N/A if (temp == 1)
0N/A EOB = k; /* EOB = index of last newly-nonzero coef */
0N/A }
0N/A
0N/A /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
0N/A
0N/A r = 0; /* r = run length of zeros */
0N/A BR = 0; /* BR = count of buffered bits added now */
0N/A BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
0N/A
0N/A for (k = cinfo->Ss; k <= Se; k++) {
0N/A if ((temp = absvalues[k]) == 0) {
0N/A r++;
0N/A continue;
0N/A }
0N/A
0N/A /* Emit any required ZRLs, but not if they can be folded into EOB */
0N/A while (r > 15 && k <= EOB) {
0N/A /* emit any pending EOBRUN and the BE correction bits */
0N/A emit_eobrun(entropy);
0N/A /* Emit ZRL */
0N/A emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
0N/A r -= 16;
0N/A /* Emit buffered correction bits that must be associated with ZRL */
0N/A emit_buffered_bits(entropy, BR_buffer, BR);
0N/A BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
0N/A BR = 0;
0N/A }
0N/A
0N/A /* If the coef was previously nonzero, it only needs a correction bit.
0N/A * NOTE: a straight translation of the spec's figure G.7 would suggest
0N/A * that we also need to test r > 15. But if r > 15, we can only get here
0N/A * if k > EOB, which implies that this coefficient is not 1.
0N/A */
0N/A if (temp > 1) {
0N/A /* The correction bit is the next bit of the absolute value. */
0N/A BR_buffer[BR++] = (char) (temp & 1);
0N/A continue;
0N/A }
0N/A
0N/A /* Emit any pending EOBRUN and the BE correction bits */
0N/A emit_eobrun(entropy);
0N/A
0N/A /* Count/emit Huffman symbol for run length / number of bits */
0N/A emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
0N/A
0N/A /* Emit output bit for newly-nonzero coef */
0N/A temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
0N/A emit_bits(entropy, (unsigned int) temp, 1);
0N/A
0N/A /* Emit buffered correction bits that must be associated with this code */
0N/A emit_buffered_bits(entropy, BR_buffer, BR);
0N/A BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
0N/A BR = 0;
0N/A r = 0; /* reset zero run length */
0N/A }
0N/A
0N/A if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
0N/A entropy->EOBRUN++; /* count an EOB */
0N/A entropy->BE += BR; /* concat my correction bits to older ones */
0N/A /* We force out the EOB if we risk either:
0N/A * 1. overflow of the EOB counter;
0N/A * 2. overflow of the correction bit buffer during the next MCU.
0N/A */
0N/A if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
0N/A emit_eobrun(entropy);
0N/A }
0N/A
0N/A cinfo->dest->next_output_byte = entropy->next_output_byte;
0N/A cinfo->dest->free_in_buffer = entropy->free_in_buffer;
0N/A
0N/A /* Update restart-interval state too */
0N/A if (cinfo->restart_interval) {
0N/A if (entropy->restarts_to_go == 0) {
0N/A entropy->restarts_to_go = cinfo->restart_interval;
0N/A entropy->next_restart_num++;
0N/A entropy->next_restart_num &= 7;
0N/A }
0N/A entropy->restarts_to_go--;
0N/A }
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Finish up at the end of a Huffman-compressed progressive scan.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Afinish_pass_phuff (j_compress_ptr cinfo)
0N/A{
0N/A phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
0N/A
0N/A entropy->next_output_byte = cinfo->dest->next_output_byte;
0N/A entropy->free_in_buffer = cinfo->dest->free_in_buffer;
0N/A
0N/A /* Flush out any buffered data */
0N/A emit_eobrun(entropy);
0N/A flush_bits(entropy);
0N/A
0N/A cinfo->dest->next_output_byte = entropy->next_output_byte;
0N/A cinfo->dest->free_in_buffer = entropy->free_in_buffer;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Finish up a statistics-gathering pass and create the new Huffman tables.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Afinish_pass_gather_phuff (j_compress_ptr cinfo)
0N/A{
0N/A phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
0N/A boolean is_DC_band;
0N/A int ci, tbl;
0N/A jpeg_component_info * compptr;
0N/A JHUFF_TBL **htblptr;
0N/A boolean did[NUM_HUFF_TBLS];
0N/A
0N/A /* Flush out buffered data (all we care about is counting the EOB symbol) */
0N/A emit_eobrun(entropy);
0N/A
0N/A is_DC_band = (cinfo->Ss == 0);
0N/A
0N/A /* It's important not to apply jpeg_gen_optimal_table more than once
0N/A * per table, because it clobbers the input frequency counts!
0N/A */
0N/A MEMZERO(did, SIZEOF(did));
0N/A
0N/A for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
0N/A compptr = cinfo->cur_comp_info[ci];
0N/A if (is_DC_band) {
0N/A if (cinfo->Ah != 0) /* DC refinement needs no table */
0N/A continue;
0N/A tbl = compptr->dc_tbl_no;
0N/A } else {
0N/A tbl = compptr->ac_tbl_no;
0N/A }
0N/A if (! did[tbl]) {
0N/A if (is_DC_band)
0N/A htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
0N/A else
0N/A htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
0N/A if (*htblptr == NULL)
0N/A *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
0N/A jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
0N/A did[tbl] = TRUE;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Module initialization routine for progressive Huffman entropy encoding.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajinit_phuff_encoder (j_compress_ptr cinfo)
0N/A{
0N/A phuff_entropy_ptr entropy;
0N/A int i;
0N/A
0N/A entropy = (phuff_entropy_ptr)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(phuff_entropy_encoder));
0N/A cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
0N/A entropy->pub.start_pass = start_pass_phuff;
0N/A
0N/A /* Mark tables unallocated */
0N/A for (i = 0; i < NUM_HUFF_TBLS; i++) {
0N/A entropy->derived_tbls[i] = NULL;
0N/A entropy->count_ptrs[i] = NULL;
0N/A }
0N/A entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
0N/A}
0N/A
0N/A#endif /* C_PROGRESSIVE_SUPPORTED */