src/java.desktop/share/native/libjavajpeg/jcphuff.c
author jiefu
Fri, 15 Nov 2019 20:39:26 +0800
changeset 59110 8c4c358272a9
parent 47216 71c04702a3d5
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
 * jcphuff.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * Copyright (C) 1995-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 for progressive JPEG.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * We do not support output suspension in this module, since the library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * currently does not allow multiple-scan files to be written with output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * suspension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
#define JPEG_INTERNALS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
#include "jinclude.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
#include "jpeglib.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
#include "jchuff.h"             /* Declarations shared with jchuff.c */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
#ifdef C_PROGRESSIVE_SUPPORTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/* Expanded entropy encoder object for progressive Huffman encoding. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
  struct jpeg_entropy_encoder pub; /* public fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
  /* Mode flag: TRUE for optimization, FALSE for actual data output */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
  boolean gather_statistics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
  /* Bit-level coding status.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
   * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  JOCTET * next_output_byte;    /* => next byte to write in buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  size_t free_in_buffer;        /* # of byte spaces remaining in buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  INT32 put_buffer;             /* current bit-accumulation buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  int put_bits;                 /* # of bits now in it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  j_compress_ptr cinfo;         /* link to cinfo (needed for dump_buffer) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  /* Coding status for DC components */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  /* Coding status for AC components */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  int ac_tbl_no;                /* the table number of the single component */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  unsigned int EOBRUN;          /* run length of EOBs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  unsigned int BE;              /* # of buffered correction bits before MCU */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  char * bit_buffer;            /* buffer for correction bits (1 per char) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  /* packing correction bits tightly would save some space but cost time... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  unsigned int restarts_to_go;  /* MCUs left in this restart interval */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  int next_restart_num;         /* next restart number to write (0-7) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  /* Pointers to derived tables (these workspaces have image lifespan).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
   * Since any one scan codes only DC or only AC, we only need one set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
   * of tables, not one for DC and one for AC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  /* Statistics tables for optimization; again, one set is enough */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  long * count_ptrs[NUM_HUFF_TBLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
} phuff_entropy_encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
typedef phuff_entropy_encoder * phuff_entropy_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
/* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * buffer can hold.  Larger sizes may slightly improve compression, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * 1000 is already well into the realm of overkill.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * The minimum safe size is 64 bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#define MAX_CORR_BITS  1000     /* Max # of correction bits I can buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
/* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * We assume that int right shift is unsigned if INT32 right shift is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * which should be safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
#ifdef RIGHT_SHIFT_IS_UNSIGNED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
#define ISHIFT_TEMPS    int ishift_temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
#define IRIGHT_SHIFT(x,shft)  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        ((ishift_temp = (x)) < 0 ? \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         (ishift_temp >> (shft)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
#define ISHIFT_TEMPS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
#define IRIGHT_SHIFT(x,shft)    ((x) >> (shft))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
#endif
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_DC_first 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(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                            JBLOCKROW *MCU_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
METHODDEF(boolean) encode_mcu_DC_refine 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(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                             JBLOCKROW *MCU_data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * Initialize for a Huffman-compressed scan using progressive JPEG.
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_phuff (j_compress_ptr cinfo, boolean gather_statistics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
  boolean is_DC_band;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
  int ci, tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
  jpeg_component_info * compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
  entropy->cinfo = cinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
  entropy->gather_statistics = gather_statistics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
  is_DC_band = (cinfo->Ss == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
  /* We assume jcmaster.c already validated the scan parameters. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
  /* Select execution routines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
  if (cinfo->Ah == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    if (is_DC_band)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
      entropy->pub.encode_mcu = encode_mcu_DC_first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
      entropy->pub.encode_mcu = encode_mcu_AC_first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    if (is_DC_band)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
      entropy->pub.encode_mcu = encode_mcu_DC_refine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
      entropy->pub.encode_mcu = encode_mcu_AC_refine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
      /* AC refinement needs a correction bit buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
      if (entropy->bit_buffer == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        entropy->bit_buffer = (char *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
          (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                      MAX_CORR_BITS * SIZEOF(char));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
  if (gather_statistics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    entropy->pub.finish_pass = finish_pass_gather_phuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
  else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    entropy->pub.finish_pass = finish_pass_phuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
  /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
   * for AC coefficients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /* Initialize DC predictions to 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    entropy->last_dc_val[ci] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /* Get table index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    if (is_DC_band) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
      if (cinfo->Ah != 0)       /* DC refinement needs no table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
      tbl = compptr->dc_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
      entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    if (gather_statistics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
      /* Check for invalid table index */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
      /* (make_c_derived_tbl does this in the other path) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
      if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
      /* Allocate and zero the statistics tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
      /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
      if (entropy->count_ptrs[tbl] == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        entropy->count_ptrs[tbl] = (long *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
          (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                      257 * SIZEOF(long));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
      MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
      /* Compute derived values for Huffman table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
      /* We may do this more than once for a table, but it's not expensive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
      jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                              & entropy->derived_tbls[tbl]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
  /* Initialize AC stuff */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
  entropy->EOBRUN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
  entropy->BE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
  /* Initialize bit buffer to empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
  entropy->put_buffer = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
  entropy->put_bits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
  /* Initialize restart stuff */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
  entropy->restarts_to_go = cinfo->restart_interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
  entropy->next_restart_num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
/* Outputting bytes to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * NB: these must be called only when actually outputting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * that is, entropy->gather_statistics == FALSE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
/* Emit a byte */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
#define emit_byte(entropy,val)  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        { *(entropy)->next_output_byte++ = (JOCTET) (val);  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
          if (--(entropy)->free_in_buffer == 0)  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            dump_buffer(entropy); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
dump_buffer (phuff_entropy_ptr entropy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
/* Empty the output buffer; we do not support suspension in this module. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
  struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
  if (! (*dest->empty_output_buffer) (entropy->cinfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
  /* After a successful buffer dump, must reset buffer pointers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
  entropy->next_output_byte = dest->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
  entropy->free_in_buffer = dest->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
/* Outputting bits to the file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
/* Only the right 24 bits of put_buffer are used; the valid bits are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * left-justified in this part.  At most 16 bits can be passed to emit_bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * in one call, and we never retain more than 7 bits in put_buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * between calls, so 24 bits are sufficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
INLINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
/* Emit some bits, unless we are in gather mode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
  /* This routine is heavily used, so it's worth coding tightly. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
  register INT32 put_buffer = (INT32) code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
  register int put_bits = entropy->put_bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
  /* if size is 0, caller used an invalid Huffman table entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
  if (size == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
  if (entropy->gather_statistics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    return;                     /* do nothing if we're only getting stats */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
  put_bits += size;             /* new number of bits in buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
  put_buffer <<= 24 - put_bits; /* align incoming bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
  put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
  while (put_bits >= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    int c = (int) ((put_buffer >> 16) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    emit_byte(entropy, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    if (c == 0xFF) {            /* need to stuff a zero byte? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
      emit_byte(entropy, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    put_buffer <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    put_bits -= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
  entropy->put_buffer = put_buffer; /* update variables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
  entropy->put_bits = put_bits;
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
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
flush_bits (phuff_entropy_ptr entropy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
  emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
  entropy->put_buffer = 0;     /* and reset bit-buffer to empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
  entropy->put_bits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * Emit (or just count) a Huffman symbol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
INLINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
  if (entropy->gather_statistics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    entropy->count_ptrs[tbl_no][symbol]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
  }
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 * Emit bits from a correction bit buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    unsigned int nbits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
  if (entropy->gather_statistics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    return;                     /* no real work */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
  while (nbits > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    emit_bits(entropy, (unsigned int) (*bufstart), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    bufstart++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    nbits--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 * Emit any pending EOBRUN symbol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
emit_eobrun (phuff_entropy_ptr entropy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
  register int temp, nbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
  if (entropy->EOBRUN > 0) {    /* if there is any pending EOBRUN */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    temp = entropy->EOBRUN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    nbits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    while ((temp >>= 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
      nbits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /* safety check: shouldn't happen given limited correction-bit buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    if (nbits > 14)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
      ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    if (nbits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
      emit_bits(entropy, entropy->EOBRUN, nbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    entropy->EOBRUN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /* Emit any buffered correction bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    entropy->BE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
 * Emit a restart marker & resynchronize predictions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
LOCAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
emit_restart (phuff_entropy_ptr entropy, int restart_num)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
  int ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
  emit_eobrun(entropy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
  if (! entropy->gather_statistics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    flush_bits(entropy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    emit_byte(entropy, 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    emit_byte(entropy, JPEG_RST0 + restart_num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
  if (entropy->cinfo->Ss == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /* Re-initialize DC predictions to 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
      entropy->last_dc_val[ci] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /* Re-initialize all AC-related fields to 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    entropy->EOBRUN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    entropy->BE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
 * MCU encoding for DC initial scan (either spectral selection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 * or first pass of successive approximation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
METHODDEF(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
  register int temp, temp2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
  register int nbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
  int blkn, ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
  int Al = cinfo->Al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
  JBLOCKROW block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
  jpeg_component_info * compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
  ISHIFT_TEMPS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
  entropy->next_output_byte = cinfo->dest->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
  /* Emit restart marker if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
  if (cinfo->restart_interval)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    if (entropy->restarts_to_go == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
      emit_restart(entropy, entropy->next_restart_num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
  /* Encode the MCU data blocks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    block = MCU_data[blkn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    ci = cinfo->MCU_membership[blkn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /* Compute the DC value after the required point transform by Al.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * This is simply an arithmetic right shift.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /* DC differences are figured on the point-transformed values. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    temp = temp2 - entropy->last_dc_val[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    entropy->last_dc_val[ci] = temp2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /* Encode the DC coefficient difference per section G.1.2.1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    temp2 = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    if (temp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
      temp = -temp;             /* temp is abs value of input */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
      /* For a negative input, want temp2 = bitwise complement of abs(input) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
      /* This code assumes we are on a two's complement machine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
      temp2--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /* Find the number of bits needed for the magnitude of the coefficient */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    nbits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    while (temp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
      nbits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
      temp >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /* Check for out-of-range coefficient values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Since we're encoding a difference, the range limit is twice as much.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    if (nbits > MAX_COEF_BITS+1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /* Count/emit the Huffman-coded symbol for the number of bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    emit_symbol(entropy, compptr->dc_tbl_no, nbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /* Emit that number of bits of the value, if positive, */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /* or the complement of its magnitude, if negative. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    if (nbits)                  /* emit_bits rejects calls with size 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
      emit_bits(entropy, (unsigned int) temp2, nbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
  cinfo->dest->next_output_byte = entropy->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
  /* Update restart-interval state too */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
  if (cinfo->restart_interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    if (entropy->restarts_to_go == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
      entropy->restarts_to_go = cinfo->restart_interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
      entropy->next_restart_num++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
      entropy->next_restart_num &= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    entropy->restarts_to_go--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
 * MCU encoding for AC initial scan (either spectral selection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
 * or first pass of successive approximation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
METHODDEF(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
  register int temp, temp2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
  register int nbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
  register int r, k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
  int Se = cinfo->Se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
  int Al = cinfo->Al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
  JBLOCKROW block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
  entropy->next_output_byte = cinfo->dest->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
  /* Emit restart marker if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
  if (cinfo->restart_interval)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    if (entropy->restarts_to_go == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
      emit_restart(entropy, entropy->next_restart_num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
  /* Encode the MCU data block */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
  block = MCU_data[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
  /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
  r = 0;                        /* r = run length of zeros */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
  for (k = cinfo->Ss; k <= Se; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
      r++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
      continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    /* We must apply the point transform by Al.  For AC coefficients this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * is an integer division with rounding towards 0.  To do this portably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * in C, we shift after obtaining the absolute value; so the code is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * interwoven with finding the abs value (temp) and output bits (temp2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    if (temp < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
      temp = -temp;             /* temp is abs value of input */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
      temp >>= Al;              /* apply the point transform */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
      /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
      temp2 = ~temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
      temp >>= Al;              /* apply the point transform */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
      temp2 = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /* Watch out for case that nonzero coef is zero after point transform */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    if (temp == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
      r++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
      continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /* Emit any pending EOBRUN */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    if (entropy->EOBRUN > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
      emit_eobrun(entropy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /* if run length > 15, must emit special run-length-16 codes (0xF0) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    while (r > 15) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
      r -= 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /* Find the number of bits needed for the magnitude of the coefficient */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    nbits = 1;                  /* there must be at least one 1 bit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    while ((temp >>= 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
      nbits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /* Check for out-of-range coefficient values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    if (nbits > MAX_COEF_BITS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
      ERREXIT(cinfo, JERR_BAD_DCT_COEF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /* Count/emit Huffman symbol for run length / number of bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /* Emit that number of bits of the value, if positive, */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    /* or the complement of its magnitude, if negative. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    emit_bits(entropy, (unsigned int) temp2, nbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    r = 0;                      /* reset zero run length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
  if (r > 0) {                  /* If there are trailing zeroes, */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    entropy->EOBRUN++;          /* count an EOB */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    if (entropy->EOBRUN == 0x7FFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
      emit_eobrun(entropy);     /* force it out to avoid overflow */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
  cinfo->dest->next_output_byte = entropy->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
  /* Update restart-interval state too */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
  if (cinfo->restart_interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    if (entropy->restarts_to_go == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
      entropy->restarts_to_go = cinfo->restart_interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
      entropy->next_restart_num++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
      entropy->next_restart_num &= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    entropy->restarts_to_go--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
 * MCU encoding for DC successive approximation refinement scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
 * Note: we assume such scans can be multi-component, although the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
 * is not very clear on the point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
METHODDEF(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
  register int temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
  int blkn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
  int Al = cinfo->Al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
  JBLOCKROW block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
  entropy->next_output_byte = cinfo->dest->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
  /* Emit restart marker if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
  if (cinfo->restart_interval)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    if (entropy->restarts_to_go == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
      emit_restart(entropy, entropy->next_restart_num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
  /* Encode the MCU data blocks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    block = MCU_data[blkn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /* We simply emit the Al'th bit of the DC coefficient value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    temp = (*block)[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    emit_bits(entropy, (unsigned int) (temp >> Al), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
  cinfo->dest->next_output_byte = entropy->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
  /* Update restart-interval state too */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
  if (cinfo->restart_interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    if (entropy->restarts_to_go == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
      entropy->restarts_to_go = cinfo->restart_interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
      entropy->next_restart_num++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
      entropy->next_restart_num &= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    entropy->restarts_to_go--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
 * MCU encoding for AC successive approximation refinement scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
METHODDEF(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
  register int temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
  register int r, k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
  int EOB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
  char *BR_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
  unsigned int BR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
  int Se = cinfo->Se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
  int Al = cinfo->Al;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
  JBLOCKROW block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
  int absvalues[DCTSIZE2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
  entropy->next_output_byte = cinfo->dest->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
  /* Emit restart marker if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
  if (cinfo->restart_interval)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    if (entropy->restarts_to_go == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
      emit_restart(entropy, entropy->next_restart_num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
  /* Encode the MCU data block */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
  block = MCU_data[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
  /* It is convenient to make a pre-pass to determine the transformed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
   * coefficients' absolute values and the EOB position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
  EOB = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
  for (k = cinfo->Ss; k <= Se; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    temp = (*block)[jpeg_natural_order[k]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /* We must apply the point transform by Al.  For AC coefficients this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * is an integer division with rounding towards 0.  To do this portably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * in C, we shift after obtaining the absolute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    if (temp < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
      temp = -temp;             /* temp is abs value of input */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    temp >>= Al;                /* apply the point transform */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    absvalues[k] = temp;        /* save abs value for main pass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    if (temp == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
      EOB = k;                  /* EOB = index of last newly-nonzero coef */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
  /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
  r = 0;                        /* r = run length of zeros */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
  BR = 0;                       /* BR = count of buffered bits added now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
  BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
  for (k = cinfo->Ss; k <= Se; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    if ((temp = absvalues[k]) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
      r++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
      continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    /* Emit any required ZRLs, but not if they can be folded into EOB */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    while (r > 15 && k <= EOB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
      /* emit any pending EOBRUN and the BE correction bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
      emit_eobrun(entropy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
      /* Emit ZRL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
      emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
      r -= 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
      /* Emit buffered correction bits that must be associated with ZRL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
      emit_buffered_bits(entropy, BR_buffer, BR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
      BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
      BR = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    /* If the coef was previously nonzero, it only needs a correction bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * NOTE: a straight translation of the spec's figure G.7 would suggest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * that we also need to test r > 15.  But if r > 15, we can only get here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * if k > EOB, which implies that this coefficient is not 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    if (temp > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
      /* The correction bit is the next bit of the absolute value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
      BR_buffer[BR++] = (char) (temp & 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
      continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    /* Emit any pending EOBRUN and the BE correction bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    emit_eobrun(entropy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /* Count/emit Huffman symbol for run length / number of bits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    /* Emit output bit for newly-nonzero coef */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    emit_bits(entropy, (unsigned int) temp, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    /* Emit buffered correction bits that must be associated with this code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    emit_buffered_bits(entropy, BR_buffer, BR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    BR = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    r = 0;                      /* reset zero run length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
  if (r > 0 || BR > 0) {        /* If there are trailing zeroes, */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    entropy->EOBRUN++;          /* count an EOB */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    entropy->BE += BR;          /* concat my correction bits to older ones */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /* We force out the EOB if we risk either:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * 1. overflow of the EOB counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * 2. overflow of the correction bit buffer during the next MCU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
      emit_eobrun(entropy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
  cinfo->dest->next_output_byte = entropy->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
  /* Update restart-interval state too */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
  if (cinfo->restart_interval) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    if (entropy->restarts_to_go == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
      entropy->restarts_to_go = cinfo->restart_interval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
      entropy->next_restart_num++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
      entropy->next_restart_num &= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    entropy->restarts_to_go--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
  return TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
}
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
 * Finish up at the end of a Huffman-compressed progressive scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
finish_pass_phuff (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
  entropy->next_output_byte = cinfo->dest->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
  entropy->free_in_buffer = cinfo->dest->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
  /* Flush out any buffered data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
  emit_eobrun(entropy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
  flush_bits(entropy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
  cinfo->dest->next_output_byte = entropy->next_output_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
  cinfo->dest->free_in_buffer = entropy->free_in_buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
 * Finish up a statistics-gathering pass and create the new Huffman tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
METHODDEF(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
finish_pass_gather_phuff (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
  phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
  boolean is_DC_band;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
  int ci, tbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
  jpeg_component_info * compptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
  JHUFF_TBL **htblptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
  boolean did[NUM_HUFF_TBLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
  /* Flush out buffered data (all we care about is counting the EOB symbol) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
  emit_eobrun(entropy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
  is_DC_band = (cinfo->Ss == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
  /* It's important not to apply jpeg_gen_optimal_table more than once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
   * per table, because it clobbers the input frequency counts!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
  MEMZERO(did, SIZEOF(did));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    compptr = cinfo->cur_comp_info[ci];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    if (is_DC_band) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
      if (cinfo->Ah != 0)       /* DC refinement needs no table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
      tbl = compptr->dc_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
      tbl = compptr->ac_tbl_no;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    if (! did[tbl]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
      if (is_DC_band)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
      else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
      if (*htblptr == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
      jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
      did[tbl] = TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
 * Module initialization routine for progressive Huffman entropy encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
GLOBAL(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
jinit_phuff_encoder (j_compress_ptr cinfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
  phuff_entropy_ptr entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
  int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
  entropy = (phuff_entropy_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                                SIZEOF(phuff_entropy_encoder));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
  entropy->pub.start_pass = start_pass_phuff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
  /* Mark tables unallocated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    entropy->derived_tbls[i] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    entropy->count_ptrs[i] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
  entropy->bit_buffer = NULL;   /* needed only in AC refinement scan */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
#endif /* C_PROGRESSIVE_SUPPORTED */