170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * jchuff.c
370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Copyright (C) 1991-1997, Thomas G. Lane.
570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This file is part of the Independent JPEG Group's software.
670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * For conditions of distribution and use, see the accompanying README file.
770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This file contains Huffman entropy encoding routines.
970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
1070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Much of the complexity here has to do with supporting output suspension.
1170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * If the data destination module demands suspension, we want to be able to
1270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * back up to the start of the current MCU.  To do this, we copy state
1370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * variables into local working storage, and update them back to the
1470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * permanent JPEG objects only upon successful completion of an MCU.
1570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
1670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
1770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define JPEG_INTERNALS
1870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include "jinclude.h"
1970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include "jpeglib.h"
2070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include "jchuff.h"		/* Declarations shared with jcphuff.c */
2170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
2270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
2370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Expanded entropy encoder object for Huffman encoding.
2470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
2570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The savable_state subrecord contains fields that change within an MCU,
2670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * but must not be updated permanently until we complete the MCU.
2770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
2870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
2970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef struct {
3070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  INT32 put_buffer;		/* current bit-accumulation buffer */
3170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int put_bits;			/* # of bits now in it */
3270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
3370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} savable_state;
3470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
3570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* This macro is to work around compilers with missing or broken
3670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * structure assignment.  You'll need to fix this code if you have
3770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * such a compiler and you change MAX_COMPS_IN_SCAN.
3870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
3970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
4070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifndef NO_STRUCT_ASSIGN
4170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define ASSIGN_STATE(dest,src)  ((dest) = (src))
4270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#else
4370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#if MAX_COMPS_IN_SCAN == 4
4470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define ASSIGN_STATE(dest,src)  \
4570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	((dest).put_buffer = (src).put_buffer, \
4670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	 (dest).put_bits = (src).put_bits, \
4770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	 (dest).last_dc_val[0] = (src).last_dc_val[0], \
4870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	 (dest).last_dc_val[1] = (src).last_dc_val[1], \
4970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	 (dest).last_dc_val[2] = (src).last_dc_val[2], \
5070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	 (dest).last_dc_val[3] = (src).last_dc_val[3])
5170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
5270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
5370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
5470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
5570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef struct {
5670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  struct jpeg_entropy_encoder pub; /* public fields */
5770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
5870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  savable_state saved;		/* Bit buffer & DC state at start of MCU */
5970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
6070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* These fields are NOT loaded into local working state. */
6170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  unsigned int restarts_to_go;	/* MCUs left in this restart interval */
6270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int next_restart_num;		/* next restart number to write (0-7) */
6370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
6470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Pointers to derived tables (these workspaces have image lifespan) */
6570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
6670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
6770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
6870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef ENTROPY_OPT_SUPPORTED	/* Statistics tables for optimization */
6970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  long * dc_count_ptrs[NUM_HUFF_TBLS];
7070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  long * ac_count_ptrs[NUM_HUFF_TBLS];
7170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
7270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} huff_entropy_encoder;
7370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
7470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef huff_entropy_encoder * huff_entropy_ptr;
7570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
7670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Working state while writing an MCU.
7770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This struct contains all the fields that are needed by subroutines.
7870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
7970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
8070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinetypedef struct {
8170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JOCTET * next_output_byte;	/* => next byte to write in buffer */
8270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  size_t free_in_buffer;	/* # of byte spaces remaining in buffer */
8370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  savable_state cur;		/* Current bit buffer & DC state */
8470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  j_compress_ptr cinfo;		/* dump_buffer needs access to this */
8570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine} working_state;
8670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
8770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
8870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Forward declarations */
8970a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
9070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine					JBLOCKROW *MCU_data));
9170a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
9270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef ENTROPY_OPT_SUPPORTED
9370a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
9470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine					  JBLOCKROW *MCU_data));
9570a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
9670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
9770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
9870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
9970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
10070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Initialize for a Huffman-compressed scan.
10170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * If gather_statistics is TRUE, we do not output anything during the scan,
10270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * just count the Huffman symbols used and generate Huffman code tables.
10370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
10470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
10570a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void)
10670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinestart_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
10770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
10870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
10970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int ci, dctbl, actbl;
11070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_component_info * compptr;
11170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
11270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (gather_statistics) {
11370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef ENTROPY_OPT_SUPPORTED
11470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    entropy->pub.encode_mcu = encode_mcu_gather;
11570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    entropy->pub.finish_pass = finish_pass_gather;
11670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#else
11770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT(cinfo, JERR_NOT_COMPILED);
11870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
11970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  } else {
12070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    entropy->pub.encode_mcu = encode_mcu_huff;
12170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    entropy->pub.finish_pass = finish_pass_huff;
12270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
12370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
12470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
12570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    compptr = cinfo->cur_comp_info[ci];
12670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    dctbl = compptr->dc_tbl_no;
12770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    actbl = compptr->ac_tbl_no;
12870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (gather_statistics) {
12970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef ENTROPY_OPT_SUPPORTED
13070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Check for invalid table indexes */
13170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* (make_c_derived_tbl does this in the other path) */
13270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
13370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
13470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
13570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
13670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Allocate and zero the statistics tables */
13770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
13870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (entropy->dc_count_ptrs[dctbl] == NULL)
13970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	entropy->dc_count_ptrs[dctbl] = (long *)
14070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
14170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine				      257 * SIZEOF(long));
14270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
14370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (entropy->ac_count_ptrs[actbl] == NULL)
14470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	entropy->ac_count_ptrs[actbl] = (long *)
14570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
14670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine				      257 * SIZEOF(long));
14770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
14870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
14970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    } else {
15070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Compute derived values for Huffman tables */
15170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* We may do this more than once for a table, but it's not expensive */
15270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
15370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine			      & entropy->dc_derived_tbls[dctbl]);
15470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
15570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine			      & entropy->ac_derived_tbls[actbl]);
15670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
15770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Initialize DC predictions to 0 */
15870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    entropy->saved.last_dc_val[ci] = 0;
15970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
16070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
16170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Initialize bit buffer to empty */
16270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  entropy->saved.put_buffer = 0;
16370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  entropy->saved.put_bits = 0;
16470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
16570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Initialize restart stuff */
16670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  entropy->restarts_to_go = cinfo->restart_interval;
16770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  entropy->next_restart_num = 0;
16870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
16970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
17070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
17170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
17270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Compute the derived values for a Huffman table.
17370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This routine also performs some validation checks on the table.
17470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
17570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Note this is also used by jcphuff.c.
17670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
17770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
17870a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
17970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
18070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine			 c_derived_tbl ** pdtbl)
18170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
18270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JHUFF_TBL *htbl;
18370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  c_derived_tbl *dtbl;
18470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int p, i, l, lastp, si, maxsymbol;
18570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  char huffsize[257];
18670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  unsigned int huffcode[257];
18770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  unsigned int code;
18870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
18970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Note that huffsize[] and huffcode[] are filled in code-length order,
19070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * paralleling the order of the symbols themselves in htbl->huffval[].
19170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
19270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
19370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Find the input Huffman table */
19470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
19570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
19670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  htbl =
19770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
19870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (htbl == NULL)
19970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
20070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
20170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Allocate a workspace if we haven't already done so. */
20270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (*pdtbl == NULL)
20370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    *pdtbl = (c_derived_tbl *)
20470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
20570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine				  SIZEOF(c_derived_tbl));
20670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  dtbl = *pdtbl;
20770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
20870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Figure C.1: make table of Huffman code length for each symbol */
20970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
21070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  p = 0;
21170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (l = 1; l <= 16; l++) {
21270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    i = (int) htbl->bits[l];
21370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (i < 0 || p + i > 256)	/* protect against table overrun */
21470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
21570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    while (i--)
21670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      huffsize[p++] = (char) l;
21770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
21870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  huffsize[p] = 0;
21970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  lastp = p;
22070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
22170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Figure C.2: generate the codes themselves */
22270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* We also validate that the counts represent a legal Huffman code tree. */
22370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
22470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  code = 0;
22570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  si = huffsize[0];
22670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  p = 0;
22770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  while (huffsize[p]) {
22870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    while (((int) huffsize[p]) == si) {
22970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      huffcode[p++] = code;
23070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      code++;
23170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
23270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* code is now 1 more than the last code used for codelength si; but
23370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     * it must still fit in si bits, since no code is allowed to be all ones.
23470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine     */
23570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (((INT32) code) >= (((INT32) 1) << si))
23670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
23770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    code <<= 1;
23870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    si++;
23970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
24070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
24170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Figure C.3: generate encoding tables */
24270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* These are code and size indexed by symbol value */
24370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
24470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Set all codeless symbols to have code length 0;
24570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * this lets us detect duplicate VAL entries here, and later
24670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * allows emit_bits to detect any attempt to emit such symbols.
24770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
24870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
24970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
25070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* This is also a convenient place to check for out-of-range
25170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * and duplicated VAL entries.  We allow 0..255 for AC symbols
25270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * but only 0..15 for DC.  (We could constrain them further
25370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * based on data depth and mode, but this seems enough.)
25470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
25570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  maxsymbol = isDC ? 15 : 255;
25670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
25770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (p = 0; p < lastp; p++) {
25870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    i = htbl->huffval[p];
25970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
26070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
26170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    dtbl->ehufco[i] = huffcode[p];
26270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    dtbl->ehufsi[i] = huffsize[p];
26370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
26470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
26570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
26670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
26770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Outputting bytes to the file */
26870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
26970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Emit a byte, taking 'action' if must suspend. */
27070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define emit_byte(state,val,action)  \
27170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	{ *(state)->next_output_byte++ = (JOCTET) (val);  \
27270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  if (--(state)->free_in_buffer == 0)  \
27370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	    if (! dump_buffer(state))  \
27470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	      { action; } }
27570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
27670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
27770a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(boolean)
27870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinedump_buffer (working_state * state)
27970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
28070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
28170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  struct jpeg_destination_mgr * dest = state->cinfo->dest;
28270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
28370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (! (*dest->empty_output_buffer) (state->cinfo))
28470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    return FALSE;
28570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* After a successful buffer dump, must reset buffer pointers */
28670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state->next_output_byte = dest->next_output_byte;
28770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state->free_in_buffer = dest->free_in_buffer;
28870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  return TRUE;
28970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
29070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
29170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
29270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Outputting bits to the file */
29370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
29470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Only the right 24 bits of put_buffer are used; the valid bits are
29570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * left-justified in this part.  At most 16 bits can be passed to emit_bits
29670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * in one call, and we never retain more than 7 bits in put_buffer
29770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * between calls, so 24 bits are sufficient.
29870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
29970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
30070a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineINLINE
30170a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(boolean)
30270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineemit_bits (working_state * state, unsigned int code, int size)
30370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Emit some bits; return TRUE if successful, FALSE if must suspend */
30470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
30570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* This routine is heavily used, so it's worth coding tightly. */
30670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  register INT32 put_buffer = (INT32) code;
30770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  register int put_bits = state->cur.put_bits;
30870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
30970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* if size is 0, caller used an invalid Huffman table entry */
31070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (size == 0)
31170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
31270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
31370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
31470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
31570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  put_bits += size;		/* new number of bits in buffer */
31670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
31770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  put_buffer <<= 24 - put_bits; /* align incoming bits */
31870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
31970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
32070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
32170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  while (put_bits >= 8) {
32270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    int c = (int) ((put_buffer >> 16) & 0xFF);
32370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
32470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    emit_byte(state, c, return FALSE);
32570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (c == 0xFF) {		/* need to stuff a zero byte? */
32670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      emit_byte(state, 0, return FALSE);
32770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
32870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    put_buffer <<= 8;
32970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    put_bits -= 8;
33070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
33170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
33270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state->cur.put_buffer = put_buffer; /* update state variables */
33370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state->cur.put_bits = put_bits;
33470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
33570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  return TRUE;
33670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
33770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
33870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
33970a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(boolean)
34070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineflush_bits (working_state * state)
34170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
34270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
34370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    return FALSE;
34470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state->cur.put_buffer = 0;	/* and reset bit-buffer to empty */
34570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state->cur.put_bits = 0;
34670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  return TRUE;
34770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
34870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
34970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
35070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Encode a single block's worth of coefficients */
35170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
35270a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(boolean)
35370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineencode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
35470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine		  c_derived_tbl *dctbl, c_derived_tbl *actbl)
35570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
35670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  register int temp, temp2;
35770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  register int nbits;
35870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  register int k, r, i;
35970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
36070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Encode the DC coefficient difference per section F.1.2.1 */
36170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
36270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  temp = temp2 = block[0] - last_dc_val;
36370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
36470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (temp < 0) {
36570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    temp = -temp;		/* temp is abs value of input */
36670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* For a negative input, want temp2 = bitwise complement of abs(input) */
36770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* This code assumes we are on a two's complement machine */
36870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    temp2--;
36970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
37070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
37170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Find the number of bits needed for the magnitude of the coefficient */
37270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  nbits = 0;
37370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  while (temp) {
37470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    nbits++;
37570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    temp >>= 1;
37670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
37770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Check for out-of-range coefficient values.
37870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Since we're encoding a difference, the range limit is twice as much.
37970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
38070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (nbits > MAX_COEF_BITS+1)
38170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
38270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
38370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Emit the Huffman-coded symbol for the number of bits */
38470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
38570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    return FALSE;
38670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
38770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Emit that number of bits of the value, if positive, */
38870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* or the complement of its magnitude, if negative. */
38970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (nbits)			/* emit_bits rejects calls with size 0 */
39070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (! emit_bits(state, (unsigned int) temp2, nbits))
39170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      return FALSE;
39270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
39370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Encode the AC coefficients per section F.1.2.2 */
39470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
39570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  r = 0;			/* r = run length of zeros */
39670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
39770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (k = 1; k < DCTSIZE2; k++) {
39870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if ((temp = block[jpeg_natural_order[k]]) == 0) {
39970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      r++;
40070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    } else {
40170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
40270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      while (r > 15) {
40370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
40470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	  return FALSE;
40570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	r -= 16;
40670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
40770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
40870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      temp2 = temp;
40970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (temp < 0) {
41070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	temp = -temp;		/* temp is abs value of input */
41170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	/* This code assumes we are on a two's complement machine */
41270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	temp2--;
41370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
41470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
41570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Find the number of bits needed for the magnitude of the coefficient */
41670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      nbits = 1;		/* there must be at least one 1 bit */
41770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      while ((temp >>= 1))
41870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	nbits++;
41970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Check for out-of-range coefficient values */
42070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (nbits > MAX_COEF_BITS)
42170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
42270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
42370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Emit Huffman symbol for run length / number of bits */
42470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      i = (r << 4) + nbits;
42570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
42670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	return FALSE;
42770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
42870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Emit that number of bits of the value, if positive, */
42970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* or the complement of its magnitude, if negative. */
43070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (! emit_bits(state, (unsigned int) temp2, nbits))
43170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	return FALSE;
43270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
43370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      r = 0;
43470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
43570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
43670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
43770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* If the last coef(s) were zero, emit an end-of-block code */
43870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (r > 0)
43970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
44070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      return FALSE;
44170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
44270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  return TRUE;
44370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
44470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
44570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
44670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
44770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Emit a restart marker & resynchronize predictions.
44870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
44970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
45070a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(boolean)
45170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineemit_restart (working_state * state, int restart_num)
45270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
45370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int ci;
45470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
45570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (! flush_bits(state))
45670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    return FALSE;
45770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
45870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  emit_byte(state, 0xFF, return FALSE);
45970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
46070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
46170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Re-initialize DC predictions to 0 */
46270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
46370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    state->cur.last_dc_val[ci] = 0;
46470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
46570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* The restart counter is not updated until we successfully write the MCU. */
46670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
46770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  return TRUE;
46870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
46970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
47070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
47170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
47270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Encode and output one MCU's worth of Huffman-compressed coefficients.
47370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
47470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
47570a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(boolean)
47670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineencode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
47770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
47870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
47970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  working_state state;
48070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int blkn, ci;
48170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_component_info * compptr;
48270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
48370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Load up working state */
48470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state.next_output_byte = cinfo->dest->next_output_byte;
48570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state.free_in_buffer = cinfo->dest->free_in_buffer;
48670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  ASSIGN_STATE(state.cur, entropy->saved);
48770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state.cinfo = cinfo;
48870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
48970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Emit restart marker if needed */
49070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (cinfo->restart_interval) {
49170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (entropy->restarts_to_go == 0)
49270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (! emit_restart(&state, entropy->next_restart_num))
49370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	return FALSE;
49470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
49570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
49670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Encode the MCU data blocks */
49770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
49870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ci = cinfo->MCU_membership[blkn];
49970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    compptr = cinfo->cur_comp_info[ci];
50070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (! encode_one_block(&state,
50170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine			   MCU_data[blkn][0], state.cur.last_dc_val[ci],
50270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine			   entropy->dc_derived_tbls[compptr->dc_tbl_no],
50370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine			   entropy->ac_derived_tbls[compptr->ac_tbl_no]))
50470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      return FALSE;
50570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Update last_dc_val */
50670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
50770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
50870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
50970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Completed MCU, so update state */
51070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->dest->next_output_byte = state.next_output_byte;
51170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->dest->free_in_buffer = state.free_in_buffer;
51270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  ASSIGN_STATE(entropy->saved, state.cur);
51370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
51470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Update restart-interval state too */
51570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (cinfo->restart_interval) {
51670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (entropy->restarts_to_go == 0) {
51770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      entropy->restarts_to_go = cinfo->restart_interval;
51870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      entropy->next_restart_num++;
51970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      entropy->next_restart_num &= 7;
52070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
52170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    entropy->restarts_to_go--;
52270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
52370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
52470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  return TRUE;
52570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
52670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
52770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
52870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
52970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Finish up at the end of a Huffman-compressed scan.
53070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
53170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
53270a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void)
53370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinefinish_pass_huff (j_compress_ptr cinfo)
53470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
53570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
53670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  working_state state;
53770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
53870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Load up working state ... flush_bits needs it */
53970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state.next_output_byte = cinfo->dest->next_output_byte;
54070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state.free_in_buffer = cinfo->dest->free_in_buffer;
54170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  ASSIGN_STATE(state.cur, entropy->saved);
54270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  state.cinfo = cinfo;
54370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
54470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Flush out the last data */
54570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (! flush_bits(&state))
54670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT(cinfo, JERR_CANT_SUSPEND);
54770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
54870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Update state */
54970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->dest->next_output_byte = state.next_output_byte;
55070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->dest->free_in_buffer = state.free_in_buffer;
55170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  ASSIGN_STATE(entropy->saved, state.cur);
55270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
55370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
55470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
55570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
55670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Huffman coding optimization.
55770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
55870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * We first scan the supplied data and count the number of uses of each symbol
55970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * that is to be Huffman-coded. (This process MUST agree with the code above.)
56070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Then we build a Huffman coding tree for the observed counts.
56170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Symbols which are not needed at all for the particular image are not
56270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * assigned any code, which saves space in the DHT marker as well as in
56370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * the compressed data.
56470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
56570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
56670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef ENTROPY_OPT_SUPPORTED
56770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
56870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
56970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Process a single block's worth of coefficients */
57070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
57170a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineLOCAL(void)
57270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinehtest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
57370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine		 long dc_counts[], long ac_counts[])
57470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
57570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  register int temp;
57670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  register int nbits;
57770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  register int k, r;
57870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
57970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Encode the DC coefficient difference per section F.1.2.1 */
58070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
58170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  temp = block[0] - last_dc_val;
58270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (temp < 0)
58370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    temp = -temp;
58470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
58570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Find the number of bits needed for the magnitude of the coefficient */
58670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  nbits = 0;
58770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  while (temp) {
58870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    nbits++;
58970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    temp >>= 1;
59070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
59170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Check for out-of-range coefficient values.
59270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Since we're encoding a difference, the range limit is twice as much.
59370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
59470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (nbits > MAX_COEF_BITS+1)
59570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT(cinfo, JERR_BAD_DCT_COEF);
59670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
59770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Count the Huffman symbol for the number of bits */
59870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  dc_counts[nbits]++;
59970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
60070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Encode the AC coefficients per section F.1.2.2 */
60170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
60270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  r = 0;			/* r = run length of zeros */
60370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
60470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (k = 1; k < DCTSIZE2; k++) {
60570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if ((temp = block[jpeg_natural_order[k]]) == 0) {
60670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      r++;
60770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    } else {
60870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
60970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      while (r > 15) {
61070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	ac_counts[0xF0]++;
61170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	r -= 16;
61270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
61370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
61470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Find the number of bits needed for the magnitude of the coefficient */
61570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (temp < 0)
61670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	temp = -temp;
61770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
61870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Find the number of bits needed for the magnitude of the coefficient */
61970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      nbits = 1;		/* there must be at least one 1 bit */
62070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      while ((temp >>= 1))
62170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	nbits++;
62270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Check for out-of-range coefficient values */
62370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (nbits > MAX_COEF_BITS)
62470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	ERREXIT(cinfo, JERR_BAD_DCT_COEF);
62570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
62670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Count Huffman symbol for run length / number of bits */
62770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      ac_counts[(r << 4) + nbits]++;
62870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
62970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      r = 0;
63070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
63170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
63270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
63370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* If the last coef(s) were zero, emit an end-of-block code */
63470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (r > 0)
63570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ac_counts[0]++;
63670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
63770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
63870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
63970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
64070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Trial-encode one MCU's worth of Huffman-compressed coefficients.
64170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * No data is actually output, so no suspension return is possible.
64270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
64370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
64470a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(boolean)
64570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkineencode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
64670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
64770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
64870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int blkn, ci;
64970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_component_info * compptr;
65070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
65170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Take care of restart intervals if needed */
65270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (cinfo->restart_interval) {
65370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (entropy->restarts_to_go == 0) {
65470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Re-initialize DC predictions to 0 */
65570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      for (ci = 0; ci < cinfo->comps_in_scan; ci++)
65670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	entropy->saved.last_dc_val[ci] = 0;
65770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* Update restart state */
65870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      entropy->restarts_to_go = cinfo->restart_interval;
65970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
66070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    entropy->restarts_to_go--;
66170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
66270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
66370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
66470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ci = cinfo->MCU_membership[blkn];
66570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    compptr = cinfo->cur_comp_info[ci];
66670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
66770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine		    entropy->dc_count_ptrs[compptr->dc_tbl_no],
66870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine		    entropy->ac_count_ptrs[compptr->ac_tbl_no]);
66970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
67070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
67170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
67270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  return TRUE;
67370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
67470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
67570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
67670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
67770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Generate the best Huffman code table for the given counts, fill htbl.
67870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Note this is also used by jcphuff.c.
67970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
68070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The JPEG standard requires that no symbol be assigned a codeword of all
68170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * one bits (so that padding bits added at the end of a compressed segment
68270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * can't look like a valid code).  Because of the canonical ordering of
68370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * codewords, this just means that there must be an unused slot in the
68470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * longest codeword length category.  Section K.2 of the JPEG spec suggests
68570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * reserving such a slot by pretending that symbol 256 is a valid symbol
68670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * with count 1.  In theory that's not optimal; giving it count zero but
68770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * including it in the symbol set anyway should give a better Huffman code.
68870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * But the theoretically better code actually seems to come out worse in
68970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * practice, because it produces more all-ones bytes (which incur stuffed
69070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * zero bytes in the final file).  In any case the difference is tiny.
69170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
69270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The JPEG standard requires Huffman codes to be no more than 16 bits long.
69370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * If some symbols have a very small but nonzero probability, the Huffman tree
69470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * must be adjusted to meet the code length restriction.  We currently use
69570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * the adjustment method suggested in JPEG section K.2.  This method is *not*
69670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * optimal; it may not choose the best possible limited-length code.  But
69770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * typically only very-low-frequency symbols will be given less-than-optimal
69870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * lengths, so the code is almost optimal.  Experimental comparisons against
69970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * an optimal limited-length-code algorithm indicate that the difference is
70070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * microscopic --- usually less than a hundredth of a percent of total size.
70170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
70270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
70370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
70470a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
70570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
70670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
70770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define MAX_CLEN 32		/* assumed maximum initial code length */
70870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  UINT8 bits[MAX_CLEN+1];	/* bits[k] = # of symbols with code length k */
70970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int codesize[257];		/* codesize[k] = code length of symbol k */
71070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int others[257];		/* next symbol in current branch of tree */
71170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int c1, c2;
71270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int p, i, j;
71370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  long v;
71470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
71570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* This algorithm is explained in section K.2 of the JPEG standard */
71670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
71770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  MEMZERO(bits, SIZEOF(bits));
71870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  MEMZERO(codesize, SIZEOF(codesize));
71970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (i = 0; i < 257; i++)
72070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    others[i] = -1;		/* init links to empty */
72170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
72270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  freq[256] = 1;		/* make sure 256 has a nonzero count */
72370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
72470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * that no real symbol is given code-value of all ones, because 256
72570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * will be placed last in the largest codeword category.
72670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
72770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
72870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Huffman's basic algorithm to assign optimal code lengths to symbols */
72970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
73070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (;;) {
73170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Find the smallest nonzero frequency, set c1 = its symbol */
73270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* In case of ties, take the larger symbol number */
73370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    c1 = -1;
73470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    v = 1000000000L;
73570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    for (i = 0; i <= 256; i++) {
73670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (freq[i] && freq[i] <= v) {
73770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	v = freq[i];
73870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	c1 = i;
73970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
74070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
74170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
74270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Find the next smallest nonzero frequency, set c2 = its symbol */
74370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* In case of ties, take the larger symbol number */
74470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    c2 = -1;
74570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    v = 1000000000L;
74670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    for (i = 0; i <= 256; i++) {
74770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (freq[i] && freq[i] <= v && i != c1) {
74870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	v = freq[i];
74970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	c2 = i;
75070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
75170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
75270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
75370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Done if we've merged everything into one frequency */
75470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (c2 < 0)
75570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      break;
75670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
75770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Else merge the two counts/trees */
75870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    freq[c1] += freq[c2];
75970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    freq[c2] = 0;
76070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
76170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Increment the codesize of everything in c1's tree branch */
76270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    codesize[c1]++;
76370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    while (others[c1] >= 0) {
76470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      c1 = others[c1];
76570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      codesize[c1]++;
76670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
76770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
76870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    others[c1] = c2;		/* chain c2 onto c1's tree branch */
76970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
77070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Increment the codesize of everything in c2's tree branch */
77170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    codesize[c2]++;
77270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    while (others[c2] >= 0) {
77370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      c2 = others[c2];
77470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      codesize[c2]++;
77570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
77670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
77770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
77870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Now count the number of symbols of each code length */
77970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (i = 0; i <= 256; i++) {
78070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (codesize[i]) {
78170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* The JPEG standard seems to think that this can't happen, */
78270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* but I'm paranoid... */
78370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (codesize[i] > MAX_CLEN)
78470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
78570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
78670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      bits[codesize[i]]++;
78770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
78870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
78970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
79070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
79170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Huffman procedure assigned any such lengths, we must adjust the coding.
79270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Here is what the JPEG spec says about how this next bit works:
79370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Since symbols are paired for the longest Huffman code, the symbols are
79470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * removed from this length category two at a time.  The prefix for the pair
79570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * (which is one bit shorter) is allocated to one of the pair; then,
79670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * skipping the BITS entry for that prefix length, a code word from the next
79770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * shortest nonzero BITS entry is converted into a prefix for two code words
79870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * one bit longer.
79970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
80070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
80170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (i = MAX_CLEN; i > 16; i--) {
80270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    while (bits[i] > 0) {
80370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      j = i - 2;		/* find length of new prefix to be used */
80470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      while (bits[j] == 0)
80570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	j--;
80670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
80770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      bits[i] -= 2;		/* remove two symbols */
80870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      bits[i-1]++;		/* one goes in this length */
80970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      bits[j+1] += 2;		/* two new symbols in this length */
81070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      bits[j]--;		/* symbol of this length is now a prefix */
81170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
81270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
81370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
81470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Remove the count for the pseudo-symbol 256 from the largest codelength */
81570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  while (bits[i] == 0)		/* find largest codelength still in use */
81670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    i--;
81770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  bits[i]--;
81870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
81970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Return final symbol counts (only for lengths 0..16) */
82070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
82170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
82270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Return a list of the symbols sorted by code length */
82370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* It's not real clear to me why we don't need to consider the codelength
82470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * changes made above, but the JPEG spec seems to think this works.
82570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
82670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  p = 0;
82770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (i = 1; i <= MAX_CLEN; i++) {
82870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    for (j = 0; j <= 255; j++) {
82970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (codesize[j] == i) {
83070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	htbl->huffval[p] = (UINT8) j;
83170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	p++;
83270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
83370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
83470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
83570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
83670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Set sent_table FALSE so updated table will be written to JPEG file. */
83770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  htbl->sent_table = FALSE;
83870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
83970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
84070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
84170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
84270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Finish up a statistics-gathering pass and create the new Huffman tables.
84370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
84470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
84570a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineMETHODDEF(void)
84670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinefinish_pass_gather (j_compress_ptr cinfo)
84770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
84870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
84970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int ci, dctbl, actbl;
85070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_component_info * compptr;
85170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JHUFF_TBL **htblptr;
85270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  boolean did_dc[NUM_HUFF_TBLS];
85370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  boolean did_ac[NUM_HUFF_TBLS];
85470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
85570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* It's important not to apply jpeg_gen_optimal_table more than once
85670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * per table, because it clobbers the input frequency counts!
85770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
85870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  MEMZERO(did_dc, SIZEOF(did_dc));
85970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  MEMZERO(did_ac, SIZEOF(did_ac));
86070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
86170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
86270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    compptr = cinfo->cur_comp_info[ci];
86370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    dctbl = compptr->dc_tbl_no;
86470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    actbl = compptr->ac_tbl_no;
86570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (! did_dc[dctbl]) {
86670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
86770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (*htblptr == NULL)
86870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
86970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
87070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      did_dc[dctbl] = TRUE;
87170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
87270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (! did_ac[actbl]) {
87370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
87470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (*htblptr == NULL)
87570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
87670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
87770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      did_ac[actbl] = TRUE;
87870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
87970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
88070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
88170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
88270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
88370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif /* ENTROPY_OPT_SUPPORTED */
88470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
88570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
88670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
88770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Module initialization routine for Huffman entropy encoding.
88870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
88970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
89070a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
89170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejinit_huff_encoder (j_compress_ptr cinfo)
89270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
89370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  huff_entropy_ptr entropy;
89470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int i;
89570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
89670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  entropy = (huff_entropy_ptr)
89770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
89870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine				SIZEOF(huff_entropy_encoder));
89970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
90070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  entropy->pub.start_pass = start_pass_huff;
90170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
90270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Mark tables unallocated */
90370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (i = 0; i < NUM_HUFF_TBLS; i++) {
90470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
90570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#ifdef ENTROPY_OPT_SUPPORTED
90670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
90770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#endif
90870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
90970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
910