jdk/src/java.desktop/share/native/libjavajpeg/jdhuff.c
author phh
Fri, 30 Jun 2017 16:42:49 +0200
changeset 46874 13b399635568
parent 25859 3317bb8137f4
permissions -rw-r--r--
8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8 8182656: Make the required changes in GC code to build on OSX 10 + Xcode 8 8182657: Make the required changes in Runtime code to build on OSX 10 + Xcode 8 8182658: Make the required changes in Compiler code to build on OSX 10 + Xcode 8 Reviewed-by: jwilhelm, ehelin
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
 * jdhuff.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 decoding 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 input suspension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * If the data source module demands suspension, we want to be able to back
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * up to the start of the current MCU.  To do this, we copy state variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * into local working storage, and update them back to the permanent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * storage 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 "jdhuff.h"             /* Declarations shared with jdphuff.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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * Expanded entropy decoder object for Huffman decoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The savable_state subrecord contains fields that change within an MCU,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * but must not be updated permanently until we complete the MCU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
} savable_state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/* This macro is to work around compilers with missing or broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * structure assignment.  You'll need to fix this code if you have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * such a compiler and you change MAX_COMPS_IN_SCAN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#ifndef NO_STRUCT_ASSIGN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#define ASSIGN_STATE(dest,src)  ((dest) = (src))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#if MAX_COMPS_IN_SCAN == 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#define ASSIGN_STATE(dest,src)  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        ((dest).last_dc_val[0] = (src).last_dc_val[0], \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
         (dest).last_dc_val[1] = (src).last_dc_val[1], \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
         (dest).last_dc_val[2] = (src).last_dc_val[2], \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
         (dest).last_dc_val[3] = (src).last_dc_val[3])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  struct jpeg_entropy_decoder pub; /* public fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  /* These fields are loaded into local variables at start of each MCU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
   * In case of suspension, we exit WITHOUT updating them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  bitread_perm_state bitstate;  /* Bit buffer at start of MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  savable_state saved;          /* Other state at start of MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  /* These fields are NOT loaded into local working state. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
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
  d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  /* Precalculated info set up by start_pass for use in decode_mcu: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
  /* Pointers to derived tables to be used for each block within an MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
  d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
  /* Whether we care about the DC and AC coefficient values for each block */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
  boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
} huff_entropy_decoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
typedef huff_entropy_decoder * huff_entropy_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * Initialize for a Huffman-compressed scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
start_pass_huff_decoder (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
  int ci, blkn, dctbl, actbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  jpeg_component_info * compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
  /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
   * This ought to be an error condition, but we make it a warning because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
   * there are some baseline files out there with all zeroes in these bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
      cinfo->Ah != 0 || cinfo->Al != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    dctbl = compptr->dc_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    actbl = compptr->ac_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /* Compute derived values for Huffman tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /* We may do this more than once for a table, but it's not expensive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                            & entropy->dc_derived_tbls[dctbl]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            & entropy->ac_derived_tbls[actbl]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /* Initialize DC predictions to 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    entropy->saved.last_dc_val[ci] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
  /* Precalculate decoding info for each block in an MCU of this scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    ci = cinfo->MCU_membership[blkn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /* Precalculate which table to use for each block */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /* Decide whether we really care about the coefficient values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    if (compptr->component_needed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
      entropy->dc_needed[blkn] = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
      /* we don't need the ACs if producing a 1/8th-size image */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
      entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
      entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  /* Initialize bitread state variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
  entropy->bitstate.bits_left = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
  entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
  entropy->pub.insufficient_data = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
  /* Initialize restart counter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
  entropy->restarts_to_go = cinfo->restart_interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * Compute the derived values for a Huffman table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * This routine also performs some validation checks on the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * Note this is also used by jdphuff.c.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                         d_derived_tbl ** pdtbl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
  JHUFF_TBL *htbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
  d_derived_tbl *dtbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
  int p, i, l, si, numsymbols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
  int lookbits, ctr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
  char huffsize[257];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
  unsigned int huffcode[257];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
  unsigned int code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
  /* Note that huffsize[] and huffcode[] are filled in code-length order,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
   * paralleling the order of the symbols themselves in htbl->huffval[].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
  /* Find the input Huffman table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
  htbl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
  if (htbl == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
  /* Allocate a workspace if we haven't already done so. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
  if (*pdtbl == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    *pdtbl = (d_derived_tbl *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                  SIZEOF(d_derived_tbl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
  dtbl = *pdtbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
  dtbl->pub = htbl;             /* fill in back link */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
  /* Figure C.1: make table of Huffman code length for each symbol */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
  p = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
  for (l = 1; l <= 16; l++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    i = (int) htbl->bits[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    if (i < 0 || p + i > 256)   /* protect against table overrun */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    while (i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
      huffsize[p++] = (char) l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
  huffsize[p] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
  numsymbols = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
  /* Figure C.2: generate the codes themselves */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
  /* We also validate that the counts represent a legal Huffman code tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
  code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
  si = huffsize[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
  p = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
  while (huffsize[p]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    while (((int) huffsize[p]) == si) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
      huffcode[p++] = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
      code++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /* code is now 1 more than the last code used for codelength si; but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * it must still fit in si bits, since no code is allowed to be all ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    if (((INT32) code) >= (((INT32) 1) << si))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
      ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    code <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    si++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
  /* Figure F.15: generate decoding tables for bit-sequential decoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
  p = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
  for (l = 1; l <= 16; l++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    if (htbl->bits[l]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
      /* valoffset[l] = huffval[] index of 1st symbol of code length l,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
       * minus the minimum code of length l
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
      dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
      p += htbl->bits[l];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
      dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
      dtbl->maxcode[l] = -1;    /* -1 if no codes of this length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
  dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
  /* Compute lookahead tables to speed up decoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
   * First we set all the table entries to 0, indicating "too long";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
   * then we iterate through the Huffman codes that are short enough and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
   * fill in all the entries that correspond to bit sequences starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
   * with that code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
  MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
  p = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
  for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      /* l = current code's length, p = its index in huffcode[] & huffval[]. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
      /* Generate left-justified code followed by all possible bit sequences */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
      lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
      for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        dtbl->look_nbits[lookbits] = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        dtbl->look_sym[lookbits] = htbl->huffval[p];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        lookbits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
  /* Validate symbols as being reasonable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
   * For AC tables, we make no check, but accept all byte values 0..255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
   * For DC tables, we require the symbols to be in range 0..15.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
   * (Tighter bounds could be applied depending on the data depth and mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
   * but this is sufficient to ensure safe decoding.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
  if (isDC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    for (i = 0; i < numsymbols; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
      int sym = htbl->huffval[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
      if (sym < 0 || sym > 15)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 * Out-of-line code for bit fetching (shared with jdphuff.c).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 * See jdhuff.h for info about usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 * Note: current values of get_buffer and bits_left are passed as parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * but are returned in the corresponding fields of the state struct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * of get_buffer to be used.  (On machines with wider words, an even larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * buffer could be used.)  However, on some machines 32-bit shifts are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * quite slow and take time proportional to the number of places shifted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 * (This is true with most PC compilers, for instance.)  In this case it may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * be a win to set MIN_GET_BITS to the minimum value of 15.  This reduces the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
#ifdef SLOW_SHIFT_32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
#define MIN_GET_BITS  15        /* minimum allowable value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
#define MIN_GET_BITS  (BIT_BUF_SIZE-7)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
GLOBAL(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
jpeg_fill_bit_buffer (bitread_working_state * state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                      register bit_buf_type get_buffer, register int bits_left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                      int nbits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
/* Load up the bit buffer to a depth of at least nbits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
  /* Copy heavily used state fields into locals (hopefully registers) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
  register const JOCTET * next_input_byte = state->next_input_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
  register size_t bytes_in_buffer = state->bytes_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
  j_decompress_ptr cinfo = state->cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
  /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
  /* (It is assumed that no request will be for more than that many bits.) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
  /* We fail to do so only if we hit a marker or are forced to suspend. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
  if (cinfo->unread_marker == 0) {      /* cannot advance past a marker */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    while (bits_left < MIN_GET_BITS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
      register int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
      /* Attempt to read a byte */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
      if (bytes_in_buffer == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (! (*cinfo->src->fill_input_buffer) (cinfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
          return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        next_input_byte = cinfo->src->next_input_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        bytes_in_buffer = cinfo->src->bytes_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
      bytes_in_buffer--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
      c = GETJOCTET(*next_input_byte++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
      /* If it's 0xFF, check and discard stuffed zero byte */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
      if (c == 0xFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        /* Loop here to discard any padding FF's on terminating marker,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * so that we can save a valid unread_marker value.  NOTE: we will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * accept multiple FF's followed by a 0 as meaning a single FF data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         * byte.  This data pattern is not valid according to the standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
          if (bytes_in_buffer == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (! (*cinfo->src->fill_input_buffer) (cinfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
              return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            next_input_byte = cinfo->src->next_input_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            bytes_in_buffer = cinfo->src->bytes_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
          bytes_in_buffer--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
          c = GETJOCTET(*next_input_byte++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        } while (c == 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (c == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
          /* Found FF/00, which represents an FF data byte */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
          c = 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
          /* Oops, it's actually a marker indicating end of compressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
           * Save the marker code for later use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
           * Fine point: it might appear that we should save the marker into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
           * bitread working state, not straight into permanent state.  But
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
           * once we have hit a marker, we cannot need to suspend within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
           * current MCU, because we will read no more bytes from the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
           * source.  So it is OK to update permanent state right away.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
          cinfo->unread_marker = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
          /* See if we need to insert some fake zero bits. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
          goto no_more_bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
      /* OK, load c into get_buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
      get_buffer = (get_buffer << 8) | c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
      bits_left += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    } /* end while */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
  no_more_bytes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /* We get here if we've read the marker that terminates the compressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * data segment.  There should be enough bits in the buffer register
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * to satisfy the request; if so, no problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    if (nbits > bits_left) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
      /* Uh-oh.  Report corrupted data to user and stuff zeroes into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
       * the data stream, so that we can produce some kind of image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
       * We use a nonvolatile flag to ensure that only one warning message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
       * appears per data segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
      if (! cinfo->entropy->insufficient_data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        WARNMS(cinfo, JWRN_HIT_MARKER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        cinfo->entropy->insufficient_data = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
      /* Fill the buffer with zero bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
      get_buffer <<= MIN_GET_BITS - bits_left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
      bits_left = MIN_GET_BITS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
  /* Unload the local registers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
  state->next_input_byte = next_input_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
  state->bytes_in_buffer = bytes_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
  state->get_buffer = get_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
  state->bits_left = bits_left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
 * Out-of-line code for Huffman code decoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
 * See jdhuff.h for info about usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
GLOBAL(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
jpeg_huff_decode (bitread_working_state * state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                  register bit_buf_type get_buffer, register int bits_left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                  d_derived_tbl * htbl, int min_bits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
  register int l = min_bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
  register INT32 code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
  /* HUFF_DECODE has determined that the code is at least min_bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
  /* bits long, so fetch that many bits in one swoop. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
  CHECK_BIT_BUFFER(*state, l, return -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
  code = GET_BITS(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
  /* Collect the rest of the Huffman code one bit at a time. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
  /* This is per Figure F.16 in the JPEG spec. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
  while (code > htbl->maxcode[l]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    code <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    CHECK_BIT_BUFFER(*state, 1, return -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    code |= GET_BITS(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    l++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
  /* Unload the local registers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
  state->get_buffer = get_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
  state->bits_left = bits_left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
  /* With garbage input we may reach the sentinel value l = 17. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
  if (l > 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    return 0;                   /* fake a zero as the safest result */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
  return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
}
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
 * Figure F.12: extend sign bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
 * On some machines, a shift and add will be faster than a table lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
#ifdef AVOID_TABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
#define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
#define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
static const int extend_test[16] =   /* entry n is 2**(n-1) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
  { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
46874
13b399635568 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
phh
parents: 25859
diff changeset
   458
  { 0,
13b399635568 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
phh
parents: 25859
diff changeset
   459
    (int)(((unsigned)(~0)<<1)  + 1), (int)(((unsigned)(~0)<<2)  + 1),
13b399635568 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
phh
parents: 25859
diff changeset
   460
    (int)(((unsigned)(~0)<<3)  + 1), (int)(((unsigned)(~0)<<4)  + 1),
13b399635568 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
phh
parents: 25859
diff changeset
   461
    (int)(((unsigned)(~0)<<5)  + 1), (int)(((unsigned)(~0)<<6)  + 1),
13b399635568 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
phh
parents: 25859
diff changeset
   462
    (int)(((unsigned)(~0)<<7)  + 1), (int)(((unsigned)(~0)<<8)  + 1),
13b399635568 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
phh
parents: 25859
diff changeset
   463
    (int)(((unsigned)(~0)<<9)  + 1), (int)(((unsigned)(~0)<<10) + 1),
13b399635568 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
phh
parents: 25859
diff changeset
   464
    (int)(((unsigned)(~0)<<11) + 1), (int)(((unsigned)(~0)<<12) + 1),
13b399635568 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
phh
parents: 25859
diff changeset
   465
    (int)(((unsigned)(~0)<<13) + 1), (int)(((unsigned)(~0)<<14) + 1),
13b399635568 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
phh
parents: 25859
diff changeset
   466
    (int)(((unsigned)(~0)<<15) + 1) };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
#endif /* AVOID_TABLES */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
 * Check for a restart marker & resynchronize decoder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
 * Returns FALSE if must suspend.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
LOCAL(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
process_restart (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
  int ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
  /* Throw away any unused bits remaining in bit buffer; */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
  /* include any full bytes in next_marker's count of discarded bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
  cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
  entropy->bitstate.bits_left = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
  /* Advance past the RSTn marker */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
  if (! (*cinfo->marker->read_restart_marker) (cinfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
  /* Re-initialize DC predictions to 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    entropy->saved.last_dc_val[ci] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
  /* Reset restart counter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
  entropy->restarts_to_go = cinfo->restart_interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
  /* Reset out-of-data flag, unless read_restart_marker left us smack up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
   * against a marker.  In that case we will end up treating the next data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
   * segment as empty, and we can avoid producing bogus output pixels by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
   * leaving the flag set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
  if (cinfo->unread_marker == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    entropy->pub.insufficient_data = FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 * Decode and return one MCU's worth of Huffman-compressed coefficients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
 * The coefficients are reordered from zigzag order into natural array order,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
 * but are not dequantized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
 * The i'th block of the MCU is stored into the block pointed to by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
 * MCU_data[i].  WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
 * (Wholesale zeroing is usually a little faster than retail...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
 * Returns FALSE if data source requested suspension.  In that case no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
 * changes have been made to permanent state.  (Exception: some output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
 * coefficients may already have been assigned.  This is harmless for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
 * this module, since we'll just re-assign them on the next call.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
METHODDEF(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
  int blkn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
  BITREAD_STATE_VARS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
  savable_state state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
  /* Process restart marker if needed; may have to suspend */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
  if (cinfo->restart_interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    if (entropy->restarts_to_go == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
      if (! process_restart(cinfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        return FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
  /* If we've run out of data, just leave the MCU set to zeroes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
   * This way, we return uniform gray for the remainder of the segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
  if (! entropy->pub.insufficient_data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /* Load up working state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    ASSIGN_STATE(state, entropy->saved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /* Outer loop handles each block in the MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
      JBLOCKROW block = MCU_data[blkn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
      d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
      d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
      register int s, k, r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
      /* Decode a single block's worth of coefficients */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
      /* Section F.2.2.1: decode the DC coefficient difference */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
      HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
      if (s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        CHECK_BIT_BUFFER(br_state, s, return FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        r = GET_BITS(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        s = HUFF_EXTEND(r, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
      if (entropy->dc_needed[blkn]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        /* Convert DC difference to actual value, update last_dc_val */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        int ci = cinfo->MCU_membership[blkn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        s += state.last_dc_val[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        state.last_dc_val[ci] = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        (*block)[0] = (JCOEF) s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
      if (entropy->ac_needed[blkn]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        /* Section F.2.2.2: decode the AC coefficients */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        /* Since zeroes are skipped, output area must be cleared beforehand */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        for (k = 1; k < DCTSIZE2; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
          HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
          r = s >> 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
          s &= 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
          if (s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            k += r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            CHECK_BIT_BUFFER(br_state, s, return FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            r = GET_BITS(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            s = HUFF_EXTEND(r, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            /* Output coefficient in natural (dezigzagged) order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
             * Note: the extra entries in jpeg_natural_order[] will save us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
             * if k >= DCTSIZE2, which could happen if the data is corrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            (*block)[jpeg_natural_order[k]] = (JCOEF) s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            if (r != 15)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            k += 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        /* Section F.2.2.2: decode the AC coefficients */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        /* In this path we just discard the values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        for (k = 1; k < DCTSIZE2; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
          HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
          r = s >> 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
          s &= 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
          if (s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            k += r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            CHECK_BIT_BUFFER(br_state, s, return FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            DROP_BITS(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            if (r != 15)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            k += 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /* Completed MCU, so update state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    ASSIGN_STATE(entropy->saved, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
  /* Account for restart interval (no-op if not using restarts) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
  entropy->restarts_to_go--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
 * Module initialization routine for Huffman entropy decoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
jinit_huff_decoder (j_decompress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
  huff_entropy_ptr entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
  entropy = (huff_entropy_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                SIZEOF(huff_entropy_decoder));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
  cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
  entropy->pub.start_pass = start_pass_huff_decoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
  entropy->pub.decode_mcu = decode_mcu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
  /* Mark tables unallocated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
}