/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
*
* Copyright (C) 1995-1997, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains Huffman entropy encoding routines for progressive JPEG.
*
* We do not support output suspension in this module, since the library
* currently does not allow multiple-scan files to be written with output
* suspension.
*/
#define JPEG_INTERNALS
#include "jinclude.h"
#include "jpeglib.h"
#ifdef C_PROGRESSIVE_SUPPORTED
/* Expanded entropy encoder object for progressive Huffman encoding. */
typedef struct {
/* Mode flag: TRUE for optimization, FALSE for actual data output */
/* Bit-level coding status.
* next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
*/
/* Coding status for DC components */
/* Coding status for AC components */
/* packing correction bits tightly would save some space but cost time... */
/* Pointers to derived tables (these workspaces have image lifespan).
* Since any one scan codes only DC or only AC, we only need one set
* of tables, not one for DC and one for AC.
*/
/* Statistics tables for optimization; again, one set is enough */
/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
* buffer can hold. Larger sizes may slightly improve compression, but
* 1000 is already well into the realm of overkill.
* The minimum safe size is 64 bits.
*/
/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
* We assume that int right shift is unsigned if INT32 right shift is,
* which should be safe.
*/
#ifdef RIGHT_SHIFT_IS_UNSIGNED
((ishift_temp = (x)) < 0 ? \
(ishift_temp >> (shft)))
#else
#define ISHIFT_TEMPS
#endif
/* Forward declarations */
/*
* Initialize for a Huffman-compressed scan using progressive JPEG.
*/
METHODDEF(void)
{
/* We assume jcmaster.c already validated the scan parameters. */
/* Select execution routines */
if (is_DC_band)
else
} else {
if (is_DC_band)
else {
/* AC refinement needs a correction bit buffer */
entropy->bit_buffer = (char *)
MAX_CORR_BITS * SIZEOF(char));
}
}
if (gather_statistics)
else
/* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
* for AC coefficients.
*/
/* Initialize DC predictions to 0 */
/* Get table index */
if (is_DC_band) {
continue;
} else {
}
if (gather_statistics) {
/* Check for invalid table index */
/* (make_c_derived_tbl does this in the other path) */
/* Allocate and zero the statistics tables */
/* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
257 * SIZEOF(long));
} else {
/* Compute derived values for Huffman table */
/* We may do this more than once for a table, but it's not expensive */
}
}
/* Initialize AC stuff */
/* Initialize bit buffer to empty */
entropy->put_buffer = 0;
/* Initialize restart stuff */
entropy->next_restart_num = 0;
}
/* Outputting bytes to the file.
* NB: these must be called only when actually outputting,
* that is, entropy->gather_statistics == FALSE.
*/
/* Emit a byte */
if (--(entropy)->free_in_buffer == 0) \
dump_buffer(entropy); }
LOCAL(void)
/* Empty the output buffer; we do not support suspension in this module. */
{
/* After a successful buffer dump, must reset buffer pointers */
}
/* Outputting bits to the file */
/* Only the right 24 bits of put_buffer are used; the valid bits are
* left-justified in this part. At most 16 bits can be passed to emit_bits
* in one call, and we never retain more than 7 bits in put_buffer
* between calls, so 24 bits are sufficient.
*/
LOCAL(void)
/* Emit some bits, unless we are in gather mode */
{
/* This routine is heavily used, so it's worth coding tightly. */
/* if size is 0, caller used an invalid Huffman table entry */
if (size == 0)
if (entropy->gather_statistics)
return; /* do nothing if we're only getting stats */
while (put_bits >= 8) {
if (c == 0xFF) { /* need to stuff a zero byte? */
}
put_buffer <<= 8;
put_bits -= 8;
}
}
LOCAL(void)
{
}
/*
* Emit (or just count) a Huffman symbol.
*/
LOCAL(void)
{
if (entropy->gather_statistics)
else {
}
}
/*
* Emit bits from a correction bit buffer.
*/
LOCAL(void)
unsigned int nbits)
{
if (entropy->gather_statistics)
return; /* no real work */
while (nbits > 0) {
bufstart++;
nbits--;
}
}
/*
* Emit any pending EOBRUN symbol.
*/
LOCAL(void)
{
nbits = 0;
while ((temp >>= 1))
nbits++;
/* safety check: shouldn't happen given limited correction-bit buffer */
if (nbits > 14)
if (nbits)
/* Emit any buffered correction bits */
}
}
/*
* Emit a restart marker & resynchronize predictions.
*/
LOCAL(void)
{
int ci;
if (! entropy->gather_statistics) {
}
/* Re-initialize DC predictions to 0 */
} else {
/* Re-initialize all AC-related fields to 0 */
}
}
/*
* MCU encoding for DC initial scan (either spectral selection,
* or first pass of successive approximation).
*/
{
register int nbits;
/* Emit restart marker if needed */
if (cinfo->restart_interval)
if (entropy->restarts_to_go == 0)
/* Encode the MCU data blocks */
/* Compute the DC value after the required point transform by Al.
* This is simply an arithmetic right shift.
*/
/* DC differences are figured on the point-transformed values. */
/* Encode the DC coefficient difference per section G.1.2.1 */
if (temp < 0) {
/* For a negative input, want temp2 = bitwise complement of abs(input) */
/* This code assumes we are on a two's complement machine */
temp2--;
}
/* Find the number of bits needed for the magnitude of the coefficient */
nbits = 0;
while (temp) {
nbits++;
temp >>= 1;
}
/* Check for out-of-range coefficient values.
* Since we're encoding a difference, the range limit is twice as much.
*/
/* Emit that number of bits of the value, if positive, */
/* or the complement of its magnitude, if negative. */
if (nbits) /* emit_bits rejects calls with size 0 */
}
/* Update restart-interval state too */
if (cinfo->restart_interval) {
if (entropy->restarts_to_go == 0) {
}
}
return TRUE;
}
/*
* MCU encoding for AC initial scan (either spectral selection,
* or first pass of successive approximation).
*/
{
register int nbits;
register int r, k;
/* Emit restart marker if needed */
if (cinfo->restart_interval)
if (entropy->restarts_to_go == 0)
/* Encode the MCU data block */
/* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
r = 0; /* r = run length of zeros */
r++;
continue;
}
/* We must apply the point transform by Al. For AC coefficients this
* is an integer division with rounding towards 0. To do this portably
* in C, we shift after obtaining the absolute value; so the code is
* interwoven with finding the abs value (temp) and output bits (temp2).
*/
if (temp < 0) {
/* For a negative coef, want temp2 = bitwise complement of abs(coef) */
} else {
}
/* Watch out for case that nonzero coef is zero after point transform */
if (temp == 0) {
r++;
continue;
}
/* Emit any pending EOBRUN */
/* if run length > 15, must emit special run-length-16 codes (0xF0) */
while (r > 15) {
r -= 16;
}
/* Find the number of bits needed for the magnitude of the coefficient */
while ((temp >>= 1))
nbits++;
/* Check for out-of-range coefficient values */
if (nbits > MAX_COEF_BITS)
/* Emit that number of bits of the value, if positive, */
/* or the complement of its magnitude, if negative. */
r = 0; /* reset zero run length */
}
if (r > 0) { /* If there are trailing zeroes, */
}
/* Update restart-interval state too */
if (cinfo->restart_interval) {
if (entropy->restarts_to_go == 0) {
}
}
return TRUE;
}
/*
* MCU encoding for DC successive approximation refinement scan.
* Note: we assume such scans can be multi-component, although the spec
* is not very clear on the point.
*/
{
register int temp;
int blkn;
/* Emit restart marker if needed */
if (cinfo->restart_interval)
if (entropy->restarts_to_go == 0)
/* Encode the MCU data blocks */
/* We simply emit the Al'th bit of the DC coefficient value. */
}
/* Update restart-interval state too */
if (cinfo->restart_interval) {
if (entropy->restarts_to_go == 0) {
}
}
return TRUE;
}
/*
* MCU encoding for AC successive approximation refinement scan.
*/
{
register int temp;
register int r, k;
int EOB;
char *BR_buffer;
unsigned int BR;
/* Emit restart marker if needed */
if (cinfo->restart_interval)
if (entropy->restarts_to_go == 0)
/* Encode the MCU data block */
/* It is convenient to make a pre-pass to determine the transformed
* coefficients' absolute values and the EOB position.
*/
EOB = 0;
/* We must apply the point transform by Al. For AC coefficients this
* is an integer division with rounding towards 0. To do this portably
* in C, we shift after obtaining the absolute value.
*/
if (temp < 0)
if (temp == 1)
EOB = k; /* EOB = index of last newly-nonzero coef */
}
/* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
r = 0; /* r = run length of zeros */
BR = 0; /* BR = count of buffered bits added now */
r++;
continue;
}
/* Emit any required ZRLs, but not if they can be folded into EOB */
while (r > 15 && k <= EOB) {
/* emit any pending EOBRUN and the BE correction bits */
/* Emit ZRL */
r -= 16;
/* Emit buffered correction bits that must be associated with ZRL */
BR = 0;
}
/* If the coef was previously nonzero, it only needs a correction bit.
* NOTE: a straight translation of the spec's figure G.7 would suggest
* that we also need to test r > 15. But if r > 15, we can only get here
* if k > EOB, which implies that this coefficient is not 1.
*/
if (temp > 1) {
/* The correction bit is the next bit of the absolute value. */
continue;
}
/* Emit any pending EOBRUN and the BE correction bits */
/* Emit output bit for newly-nonzero coef */
/* Emit buffered correction bits that must be associated with this code */
BR = 0;
r = 0; /* reset zero run length */
}
if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
/* We force out the EOB if we risk either:
* 1. overflow of the EOB counter;
* 2. overflow of the correction bit buffer during the next MCU.
*/
}
/* Update restart-interval state too */
if (cinfo->restart_interval) {
if (entropy->restarts_to_go == 0) {
}
}
return TRUE;
}
/*
* Finish up at the end of a Huffman-compressed progressive scan.
*/
METHODDEF(void)
{
/* Flush out any buffered data */
}
/*
* Finish up a statistics-gathering pass and create the new Huffman tables.
*/
METHODDEF(void)
{
/* Flush out buffered data (all we care about is counting the EOB symbol) */
/* It's important not to apply jpeg_gen_optimal_table more than once
* per table, because it clobbers the input frequency counts!
*/
if (is_DC_band) {
continue;
} else {
}
if (is_DC_band)
else
}
}
}
/*
* Module initialization routine for progressive Huffman entropy encoding.
*/
GLOBAL(void)
{
int i;
/* Mark tables unallocated */
for (i = 0; i < NUM_HUFF_TBLS; i++) {
}
}
#endif /* C_PROGRESSIVE_SUPPORTED */