src/java.desktop/share/native/libjavajpeg/jchuff.c
author jiefu
Fri, 15 Nov 2019 20:39:26 +0800
changeset 59110 8c4c358272a9
parent 50361 071f1fe0df5f
permissions -rw-r--r--
8234232: [TESTBUG] gc/shenandoah/jvmti/TestHeapDump.java fails with -Xcomp Reviewed-by: zgu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * jchuff.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * Copyright (C) 1991-1997, Thomas G. Lane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This file is part of the Independent JPEG Group's software.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * For conditions of distribution and use, see the accompanying README file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * This file contains Huffman entropy encoding routines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * Much of the complexity here has to do with supporting output suspension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * If the data destination module demands suspension, we want to be able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * back up to the start of the current MCU.  To do this, we copy state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * variables into local working storage, and update them back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * permanent JPEG objects only upon successful completion of an MCU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
#define JPEG_INTERNALS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
#include "jinclude.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
#include "jpeglib.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
#include "jchuff.h"             /* Declarations shared with jcphuff.c */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/* Expanded entropy encoder object for Huffman encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * The savable_state subrecord contains fields that change within an MCU,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * but must not be updated permanently until we complete the MCU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
  INT32 put_buffer;             /* current bit-accumulation buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  int put_bits;                 /* # of bits now in it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
} savable_state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/* This macro is to work around compilers with missing or broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * structure assignment.  You'll need to fix this code if you have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * such a compiler and you change MAX_COMPS_IN_SCAN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#ifndef NO_STRUCT_ASSIGN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#define ASSIGN_STATE(dest,src)  ((dest) = (src))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#if MAX_COMPS_IN_SCAN == 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#define ASSIGN_STATE(dest,src)  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        ((dest).put_buffer = (src).put_buffer, \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
         (dest).put_bits = (src).put_bits, \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
         (dest).last_dc_val[0] = (src).last_dc_val[0], \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
         (dest).last_dc_val[1] = (src).last_dc_val[1], \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
         (dest).last_dc_val[2] = (src).last_dc_val[2], \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
         (dest).last_dc_val[3] = (src).last_dc_val[3])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  struct jpeg_entropy_encoder pub; /* public fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  savable_state saved;          /* Bit buffer & DC state at start of MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  /* These fields are NOT loaded into local working state. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
  int next_restart_num;         /* next restart number to write (0-7) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  /* Pointers to derived tables (these workspaces have image lifespan) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
#ifdef ENTROPY_OPT_SUPPORTED    /* Statistics tables for optimization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  long * dc_count_ptrs[NUM_HUFF_TBLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
  long * ac_count_ptrs[NUM_HUFF_TBLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
} huff_entropy_encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
typedef huff_entropy_encoder * huff_entropy_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
/* Working state while writing an MCU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * This struct contains all the fields that are needed by subroutines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
  JOCTET * next_output_byte;    /* => next byte to write in buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
  size_t free_in_buffer;        /* # of byte spaces remaining in buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
  savable_state cur;            /* Current bit buffer & DC state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
  j_compress_ptr cinfo;         /* dump_buffer needs access to this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
} working_state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
/* Forward declarations */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                        JBLOCKROW *MCU_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
#ifdef ENTROPY_OPT_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                          JBLOCKROW *MCU_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * Initialize for a Huffman-compressed scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * If gather_statistics is TRUE, we do not output anything during the scan,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * just count the Huffman symbols used and generate Huffman code tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
  int ci, dctbl, actbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
  jpeg_component_info * compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
  if (gather_statistics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
#ifdef ENTROPY_OPT_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    entropy->pub.encode_mcu = encode_mcu_gather;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    entropy->pub.finish_pass = finish_pass_gather;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    ERREXIT(cinfo, JERR_NOT_COMPILED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    entropy->pub.encode_mcu = encode_mcu_huff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    entropy->pub.finish_pass = finish_pass_huff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    dctbl = compptr->dc_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    actbl = compptr->ac_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    if (gather_statistics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
#ifdef ENTROPY_OPT_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
      /* Check for invalid table indexes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
      /* (make_c_derived_tbl does this in the other path) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
      if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
      if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
      /* Allocate and zero the statistics tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
      if (entropy->dc_count_ptrs[dctbl] == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        entropy->dc_count_ptrs[dctbl] = (long *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
          (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                      257 * SIZEOF(long));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
      MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
      if (entropy->ac_count_ptrs[actbl] == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        entropy->ac_count_ptrs[actbl] = (long *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
          (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                      257 * SIZEOF(long));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
      MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
      /* Compute derived values for Huffman tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
      /* We may do this more than once for a table, but it's not expensive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
      jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                              & entropy->dc_derived_tbls[dctbl]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
      jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                              & entropy->ac_derived_tbls[actbl]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /* Initialize DC predictions to 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    entropy->saved.last_dc_val[ci] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
  /* Initialize bit buffer to empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
  entropy->saved.put_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
  entropy->saved.put_bits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
  /* Initialize restart stuff */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
  entropy->restarts_to_go = cinfo->restart_interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
  entropy->next_restart_num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * Compute the derived values for a Huffman table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * This routine also performs some validation checks on the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * Note this is also used by jcphuff.c.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                         c_derived_tbl ** pdtbl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
  JHUFF_TBL *htbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
  c_derived_tbl *dtbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
  int p, i, l, lastp, si, maxsymbol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
  char huffsize[257];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
  unsigned int huffcode[257];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
  unsigned int code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
  /* Note that huffsize[] and huffcode[] are filled in code-length order,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
   * paralleling the order of the symbols themselves in htbl->huffval[].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
  /* Find the input Huffman table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
  htbl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
  if (htbl == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
  /* Allocate a workspace if we haven't already done so. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
  if (*pdtbl == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    *pdtbl = (c_derived_tbl *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                  SIZEOF(c_derived_tbl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
  dtbl = *pdtbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
  /* Figure C.1: make table of Huffman code length for each symbol */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
  p = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
  for (l = 1; l <= 16; l++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    i = (int) htbl->bits[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    if (i < 0 || p + i > 256)   /* protect against table overrun */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    while (i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
      huffsize[p++] = (char) l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
  huffsize[p] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
  lastp = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
  /* Figure C.2: generate the codes themselves */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
  /* We also validate that the counts represent a legal Huffman code tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
  code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
  si = huffsize[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
  p = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
  while (huffsize[p]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    while (((int) huffsize[p]) == si) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
      huffcode[p++] = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
      code++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /* code is now 1 more than the last code used for codelength si; but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * it must still fit in si bits, since no code is allowed to be all ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    if (((INT32) code) >= (((INT32) 1) << si))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    code <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    si++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
  /* Figure C.3: generate encoding tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
  /* These are code and size indexed by symbol value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
  /* Set all codeless symbols to have code length 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
   * this lets us detect duplicate VAL entries here, and later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
   * allows emit_bits to detect any attempt to emit such symbols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
  MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
  /* This is also a convenient place to check for out-of-range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
   * and duplicated VAL entries.  We allow 0..255 for AC symbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
   * but only 0..15 for DC.  (We could constrain them further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
   * based on data depth and mode, but this seems enough.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
  maxsymbol = isDC ? 15 : 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
  for (p = 0; p < lastp; p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    i = htbl->huffval[p];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    dtbl->ehufco[i] = huffcode[p];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    dtbl->ehufsi[i] = huffsize[p];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
/* Outputting bytes to the file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
/* Emit a byte, taking 'action' if must suspend. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
#define emit_byte(state,val,action)  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        { *(state)->next_output_byte++ = (JOCTET) (val);  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
          if (--(state)->free_in_buffer == 0)  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (! dump_buffer(state))  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
              { action; } }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
LOCAL(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
dump_buffer (working_state * state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
/* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
  struct jpeg_destination_mgr * dest = state->cinfo->dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
  if (! (*dest->empty_output_buffer) (state->cinfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
  /* After a successful buffer dump, must reset buffer pointers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
  state->next_output_byte = dest->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
  state->free_in_buffer = dest->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
/* Outputting bits to the file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
/* Only the right 24 bits of put_buffer are used; the valid bits are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 * left-justified in this part.  At most 16 bits can be passed to emit_bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * in one call, and we never retain more than 7 bits in put_buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * between calls, so 24 bits are sufficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
INLINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
LOCAL(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
emit_bits (working_state * state, unsigned int code, int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
/* Emit some bits; return TRUE if successful, FALSE if must suspend */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
  /* This routine is heavily used, so it's worth coding tightly. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
  register INT32 put_buffer = (INT32) code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
  register int put_bits = state->cur.put_bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
  /* if size is 0, caller used an invalid Huffman table entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
  if (size == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
  put_bits += size;             /* new number of bits in buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
  put_buffer <<= 24 - put_bits; /* align incoming bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
  put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
  while (put_bits >= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    int c = (int) ((put_buffer >> 16) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    emit_byte(state, c, return FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    if (c == 0xFF) {            /* need to stuff a zero byte? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
      emit_byte(state, 0, return FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    put_buffer <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    put_bits -= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
  state->cur.put_buffer = put_buffer; /* update state variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
  state->cur.put_bits = put_bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
LOCAL(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
flush_bits (working_state * state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
  if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
  state->cur.put_buffer = 0;    /* and reset bit-buffer to empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
  state->cur.put_bits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
/* Encode a single block's worth of coefficients */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
LOCAL(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                  c_derived_tbl *dctbl, c_derived_tbl *actbl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
  register int temp, temp2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
  register int nbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
  register int k, r, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
  /* Encode the DC coefficient difference per section F.1.2.1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
  temp = temp2 = block[0] - last_dc_val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
  if (temp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    temp = -temp;               /* temp is abs value of input */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /* For a negative input, want temp2 = bitwise complement of abs(input) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /* This code assumes we are on a two's complement machine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    temp2--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
  /* Find the number of bits needed for the magnitude of the coefficient */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
  nbits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
  while (temp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    nbits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    temp >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
  /* Check for out-of-range coefficient values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
   * Since we're encoding a difference, the range limit is twice as much.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
  if (nbits > MAX_COEF_BITS+1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
  /* Emit the Huffman-coded symbol for the number of bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
  if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
  /* Emit that number of bits of the value, if positive, */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
  /* or the complement of its magnitude, if negative. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
  if (nbits)                    /* emit_bits rejects calls with size 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    if (! emit_bits(state, (unsigned int) temp2, nbits))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
      return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
  /* Encode the AC coefficients per section F.1.2.2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
  r = 0;                        /* r = run length of zeros */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
  for (k = 1; k < DCTSIZE2; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    if ((temp = block[jpeg_natural_order[k]]) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
      r++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
      while (r > 15) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
          return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        r -= 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
      temp2 = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
      if (temp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        temp = -temp;           /* temp is abs value of input */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        /* This code assumes we are on a two's complement machine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        temp2--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
      /* Find the number of bits needed for the magnitude of the coefficient */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
      nbits = 1;                /* there must be at least one 1 bit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
      while ((temp >>= 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        nbits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
      /* Check for out-of-range coefficient values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
      if (nbits > MAX_COEF_BITS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
      /* Emit Huffman symbol for run length / number of bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
      i = (r << 4) + nbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
      if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
      /* Emit that number of bits of the value, if positive, */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
      /* or the complement of its magnitude, if negative. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
      if (! emit_bits(state, (unsigned int) temp2, nbits))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
      r = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
  /* If the last coef(s) were zero, emit an end-of-block code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
  if (r > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
      return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
 * Emit a restart marker & resynchronize predictions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
LOCAL(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
emit_restart (working_state * state, int restart_num)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
  int ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
  if (! flush_bits(state))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
  emit_byte(state, 0xFF, return FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
  /* Re-initialize DC predictions to 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    state->cur.last_dc_val[ci] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
  /* The restart counter is not updated until we successfully write the MCU. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
 * Encode and output one MCU's worth of Huffman-compressed coefficients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
METHODDEF(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
  working_state state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
  int blkn, ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
  jpeg_component_info * compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
  /* Load up working state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
  state.next_output_byte = cinfo->dest->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
  state.free_in_buffer = cinfo->dest->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
  ASSIGN_STATE(state.cur, entropy->saved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
  state.cinfo = cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
  /* Emit restart marker if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
  if (cinfo->restart_interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    if (entropy->restarts_to_go == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
      if (! emit_restart(&state, entropy->next_restart_num))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
  /* Encode the MCU data blocks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    ci = cinfo->MCU_membership[blkn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    if (! encode_one_block(&state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                           MCU_data[blkn][0], state.cur.last_dc_val[ci],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                           entropy->dc_derived_tbls[compptr->dc_tbl_no],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                           entropy->ac_derived_tbls[compptr->ac_tbl_no]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
      return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /* Update last_dc_val */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
  /* Completed MCU, so update state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
  cinfo->dest->next_output_byte = state.next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
  cinfo->dest->free_in_buffer = state.free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
  ASSIGN_STATE(entropy->saved, state.cur);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
  /* Update restart-interval state too */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
  if (cinfo->restart_interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    if (entropy->restarts_to_go == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
      entropy->restarts_to_go = cinfo->restart_interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
      entropy->next_restart_num++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
      entropy->next_restart_num &= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    entropy->restarts_to_go--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
 * Finish up at the end of a Huffman-compressed scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
finish_pass_huff (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
  working_state state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
  /* Load up working state ... flush_bits needs it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
  state.next_output_byte = cinfo->dest->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
  state.free_in_buffer = cinfo->dest->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
  ASSIGN_STATE(state.cur, entropy->saved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
  state.cinfo = cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
  /* Flush out the last data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
  if (! flush_bits(&state))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    ERREXIT(cinfo, JERR_CANT_SUSPEND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
  /* Update state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
  cinfo->dest->next_output_byte = state.next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
  cinfo->dest->free_in_buffer = state.free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
  ASSIGN_STATE(entropy->saved, state.cur);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
 * Huffman coding optimization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
 * We first scan the supplied data and count the number of uses of each symbol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
 * that is to be Huffman-coded. (This process MUST agree with the code above.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
 * Then we build a Huffman coding tree for the observed counts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
 * Symbols which are not needed at all for the particular image are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
 * assigned any code, which saves space in the DHT marker as well as in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
 * the compressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
#ifdef ENTROPY_OPT_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
/* Process a single block's worth of coefficients */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                 long dc_counts[], long ac_counts[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
  register int temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
  register int nbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
  register int k, r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
  /* Encode the DC coefficient difference per section F.1.2.1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
  temp = block[0] - last_dc_val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
  if (temp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    temp = -temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
  /* Find the number of bits needed for the magnitude of the coefficient */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
  nbits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
  while (temp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    nbits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    temp >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
  /* Check for out-of-range coefficient values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
   * Since we're encoding a difference, the range limit is twice as much.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
  if (nbits > MAX_COEF_BITS+1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    ERREXIT(cinfo, JERR_BAD_DCT_COEF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
  /* Count the Huffman symbol for the number of bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
  dc_counts[nbits]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
  /* Encode the AC coefficients per section F.1.2.2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
  r = 0;                        /* r = run length of zeros */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
  for (k = 1; k < DCTSIZE2; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    if ((temp = block[jpeg_natural_order[k]]) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
      r++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
      while (r > 15) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        ac_counts[0xF0]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        r -= 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
      /* Find the number of bits needed for the magnitude of the coefficient */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
      if (temp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        temp = -temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
      /* Find the number of bits needed for the magnitude of the coefficient */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
      nbits = 1;                /* there must be at least one 1 bit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
      while ((temp >>= 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        nbits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
      /* Check for out-of-range coefficient values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
      if (nbits > MAX_COEF_BITS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        ERREXIT(cinfo, JERR_BAD_DCT_COEF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
      /* Count Huffman symbol for run length / number of bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
      ac_counts[(r << 4) + nbits]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
      r = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
  /* If the last coef(s) were zero, emit an end-of-block code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
  if (r > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    ac_counts[0]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
 * No data is actually output, so no suspension return is possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
METHODDEF(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
  int blkn, ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
  jpeg_component_info * compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
  /* Take care of restart intervals if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
  if (cinfo->restart_interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    if (entropy->restarts_to_go == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
      /* Re-initialize DC predictions to 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
      for (ci = 0; ci < cinfo->comps_in_scan; ci++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        entropy->saved.last_dc_val[ci] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
      /* Update restart state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
      entropy->restarts_to_go = cinfo->restart_interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    entropy->restarts_to_go--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    ci = cinfo->MCU_membership[blkn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    entropy->dc_count_ptrs[compptr->dc_tbl_no],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    entropy->ac_count_ptrs[compptr->ac_tbl_no]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
 * Generate the best Huffman code table for the given counts, fill htbl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
 * Note this is also used by jcphuff.c.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
 * The JPEG standard requires that no symbol be assigned a codeword of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
 * one bits (so that padding bits added at the end of a compressed segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
 * can't look like a valid code).  Because of the canonical ordering of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
 * codewords, this just means that there must be an unused slot in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
 * longest codeword length category.  Section K.2 of the JPEG spec suggests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
 * reserving such a slot by pretending that symbol 256 is a valid symbol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
 * with count 1.  In theory that's not optimal; giving it count zero but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
 * including it in the symbol set anyway should give a better Huffman code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
 * But the theoretically better code actually seems to come out worse in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
 * practice, because it produces more all-ones bytes (which incur stuffed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
 * zero bytes in the final file).  In any case the difference is tiny.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
 * The JPEG standard requires Huffman codes to be no more than 16 bits long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
 * If some symbols have a very small but nonzero probability, the Huffman tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
 * must be adjusted to meet the code length restriction.  We currently use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
 * the adjustment method suggested in JPEG section K.2.  This method is *not*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
 * optimal; it may not choose the best possible limited-length code.  But
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
 * typically only very-low-frequency symbols will be given less-than-optimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
 * lengths, so the code is almost optimal.  Experimental comparisons against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
 * an optimal limited-length-code algorithm indicate that the difference is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
 * microscopic --- usually less than a hundredth of a percent of total size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
 * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
#define MAX_CLEN 32             /* assumed maximum initial code length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
  UINT8 bits[MAX_CLEN+1];       /* bits[k] = # of symbols with code length k */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
  int codesize[257];            /* codesize[k] = code length of symbol k */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
  int others[257];              /* next symbol in current branch of tree */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
  int c1, c2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
  int p, i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
  long v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
  /* This algorithm is explained in section K.2 of the JPEG standard */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
  MEMZERO(bits, SIZEOF(bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
  MEMZERO(codesize, SIZEOF(codesize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
  for (i = 0; i < 257; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    others[i] = -1;             /* init links to empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
  freq[256] = 1;                /* make sure 256 has a nonzero count */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
  /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
   * that no real symbol is given code-value of all ones, because 256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
   * will be placed last in the largest codeword category.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
  /* Huffman's basic algorithm to assign optimal code lengths to symbols */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
  for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    /* Find the smallest nonzero frequency, set c1 = its symbol */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /* In case of ties, take the larger symbol number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    c1 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    v = 1000000000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    for (i = 0; i <= 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
      if (freq[i] && freq[i] <= v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        v = freq[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        c1 = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /* Find the next smallest nonzero frequency, set c2 = its symbol */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    /* In case of ties, take the larger symbol number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    c2 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    v = 1000000000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    for (i = 0; i <= 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
      if (freq[i] && freq[i] <= v && i != c1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        v = freq[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        c2 = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /* Done if we've merged everything into one frequency */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    if (c2 < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    /* Else merge the two counts/trees */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    freq[c1] += freq[c2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    freq[c2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    /* Increment the codesize of everything in c1's tree branch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    codesize[c1]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    while (others[c1] >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
      c1 = others[c1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
      codesize[c1]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    others[c1] = c2;            /* chain c2 onto c1's tree branch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /* Increment the codesize of everything in c2's tree branch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    codesize[c2]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    while (others[c2] >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
      c2 = others[c2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
      codesize[c2]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
  /* Now count the number of symbols of each code length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
  for (i = 0; i <= 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    if (codesize[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
      /* The JPEG standard seems to think that this can't happen, */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
      /* but I'm paranoid... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
      if (codesize[i] > MAX_CLEN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
      bits[codesize[i]]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
  /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
   * Huffman procedure assigned any such lengths, we must adjust the coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
   * Here is what the JPEG spec says about how this next bit works:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
   * Since symbols are paired for the longest Huffman code, the symbols are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
   * removed from this length category two at a time.  The prefix for the pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
   * (which is one bit shorter) is allocated to one of the pair; then,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
   * skipping the BITS entry for that prefix length, a code word from the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
   * shortest nonzero BITS entry is converted into a prefix for two code words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
   * one bit longer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
  for (i = MAX_CLEN; i > 16; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    while (bits[i] > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
      j = i - 2;                /* find length of new prefix to be used */
50361
071f1fe0df5f 8200052: libjavajpeg: Fix compile warning in jchuff.c
prr
parents: 47216
diff changeset
   808
      while (bits[j] == 0) {
071f1fe0df5f 8200052: libjavajpeg: Fix compile warning in jchuff.c
prr
parents: 47216
diff changeset
   809
        if (j == 0)
071f1fe0df5f 8200052: libjavajpeg: Fix compile warning in jchuff.c
prr
parents: 47216
diff changeset
   810
          ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        j--;
50361
071f1fe0df5f 8200052: libjavajpeg: Fix compile warning in jchuff.c
prr
parents: 47216
diff changeset
   812
      }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
      bits[i] -= 2;             /* remove two symbols */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
      bits[i-1]++;              /* one goes in this length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
      bits[j+1] += 2;           /* two new symbols in this length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
      bits[j]--;                /* symbol of this length is now a prefix */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
  /* Remove the count for the pseudo-symbol 256 from the largest codelength */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
  while (bits[i] == 0)          /* find largest codelength still in use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
  bits[i]--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
  /* Return final symbol counts (only for lengths 0..16) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
  MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
  /* Return a list of the symbols sorted by code length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
  /* It's not real clear to me why we don't need to consider the codelength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
   * changes made above, but the JPEG spec seems to think this works.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
  p = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
  for (i = 1; i <= MAX_CLEN; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    for (j = 0; j <= 255; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
      if (codesize[j] == i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        htbl->huffval[p] = (UINT8) j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
  /* Set sent_table FALSE so updated table will be written to JPEG file. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
  htbl->sent_table = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
 * Finish up a statistics-gathering pass and create the new Huffman tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
finish_pass_gather (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
  int ci, dctbl, actbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
  jpeg_component_info * compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
  JHUFF_TBL **htblptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
  boolean did_dc[NUM_HUFF_TBLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
  boolean did_ac[NUM_HUFF_TBLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
  /* It's important not to apply jpeg_gen_optimal_table more than once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
   * per table, because it clobbers the input frequency counts!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
  MEMZERO(did_dc, SIZEOF(did_dc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
  MEMZERO(did_ac, SIZEOF(did_ac));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    dctbl = compptr->dc_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    actbl = compptr->ac_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    if (! did_dc[dctbl]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
      htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
      if (*htblptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
      did_dc[dctbl] = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    if (! did_ac[actbl]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
      htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
      if (*htblptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
      did_ac[actbl] = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
#endif /* ENTROPY_OPT_SUPPORTED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
 * Module initialization routine for Huffman entropy encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
jinit_huff_encoder (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
  huff_entropy_ptr entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
  entropy = (huff_entropy_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                                SIZEOF(huff_entropy_encoder));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
  entropy->pub.start_pass = start_pass_huff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
  /* Mark tables unallocated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
#ifdef ENTROPY_OPT_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
}