Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shoaib_ahmed 0:791a779d6220 1 /*
shoaib_ahmed 0:791a779d6220 2 * jchuff.c
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Copyright (C) 1991-1997, Thomas G. Lane.
shoaib_ahmed 0:791a779d6220 5 * Modified 2006-2013 by Guido Vollbeding.
shoaib_ahmed 0:791a779d6220 6 * This file is part of the Independent JPEG Group's software.
shoaib_ahmed 0:791a779d6220 7 * For conditions of distribution and use, see the accompanying README file.
shoaib_ahmed 0:791a779d6220 8 *
shoaib_ahmed 0:791a779d6220 9 * This file contains Huffman entropy encoding routines.
shoaib_ahmed 0:791a779d6220 10 * Both sequential and progressive modes are supported in this single module.
shoaib_ahmed 0:791a779d6220 11 *
shoaib_ahmed 0:791a779d6220 12 * Much of the complexity here has to do with supporting output suspension.
shoaib_ahmed 0:791a779d6220 13 * If the data destination module demands suspension, we want to be able to
shoaib_ahmed 0:791a779d6220 14 * back up to the start of the current MCU. To do this, we copy state
shoaib_ahmed 0:791a779d6220 15 * variables into local working storage, and update them back to the
shoaib_ahmed 0:791a779d6220 16 * permanent JPEG objects only upon successful completion of an MCU.
shoaib_ahmed 0:791a779d6220 17 *
shoaib_ahmed 0:791a779d6220 18 * We do not support output suspension for the progressive JPEG mode, since
shoaib_ahmed 0:791a779d6220 19 * the library currently does not allow multiple-scan files to be written
shoaib_ahmed 0:791a779d6220 20 * with output suspension.
shoaib_ahmed 0:791a779d6220 21 */
shoaib_ahmed 0:791a779d6220 22
shoaib_ahmed 0:791a779d6220 23 #define JPEG_INTERNALS
shoaib_ahmed 0:791a779d6220 24 #include "jinclude.h"
shoaib_ahmed 0:791a779d6220 25 #include "jpeglib.h"
shoaib_ahmed 0:791a779d6220 26
shoaib_ahmed 0:791a779d6220 27
shoaib_ahmed 0:791a779d6220 28 /* The legal range of a DCT coefficient is
shoaib_ahmed 0:791a779d6220 29 * -1024 .. +1023 for 8-bit data;
shoaib_ahmed 0:791a779d6220 30 * -16384 .. +16383 for 12-bit data.
shoaib_ahmed 0:791a779d6220 31 * Hence the magnitude should always fit in 10 or 14 bits respectively.
shoaib_ahmed 0:791a779d6220 32 */
shoaib_ahmed 0:791a779d6220 33
shoaib_ahmed 0:791a779d6220 34 #if BITS_IN_JSAMPLE == 8
shoaib_ahmed 0:791a779d6220 35 #define MAX_COEF_BITS 10
shoaib_ahmed 0:791a779d6220 36 #else
shoaib_ahmed 0:791a779d6220 37 #define MAX_COEF_BITS 14
shoaib_ahmed 0:791a779d6220 38 #endif
shoaib_ahmed 0:791a779d6220 39
shoaib_ahmed 0:791a779d6220 40 /* Derived data constructed for each Huffman table */
shoaib_ahmed 0:791a779d6220 41
shoaib_ahmed 0:791a779d6220 42 typedef struct {
shoaib_ahmed 0:791a779d6220 43 unsigned int ehufco[256]; /* code for each symbol */
shoaib_ahmed 0:791a779d6220 44 char ehufsi[256]; /* length of code for each symbol */
shoaib_ahmed 0:791a779d6220 45 /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */
shoaib_ahmed 0:791a779d6220 46 } c_derived_tbl;
shoaib_ahmed 0:791a779d6220 47
shoaib_ahmed 0:791a779d6220 48
shoaib_ahmed 0:791a779d6220 49 /* Expanded entropy encoder object for Huffman encoding.
shoaib_ahmed 0:791a779d6220 50 *
shoaib_ahmed 0:791a779d6220 51 * The savable_state subrecord contains fields that change within an MCU,
shoaib_ahmed 0:791a779d6220 52 * but must not be updated permanently until we complete the MCU.
shoaib_ahmed 0:791a779d6220 53 */
shoaib_ahmed 0:791a779d6220 54
shoaib_ahmed 0:791a779d6220 55 typedef struct {
shoaib_ahmed 0:791a779d6220 56 INT32 put_buffer; /* current bit-accumulation buffer */
shoaib_ahmed 0:791a779d6220 57 int put_bits; /* # of bits now in it */
shoaib_ahmed 0:791a779d6220 58 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
shoaib_ahmed 0:791a779d6220 59 } savable_state;
shoaib_ahmed 0:791a779d6220 60
shoaib_ahmed 0:791a779d6220 61 /* This macro is to work around compilers with missing or broken
shoaib_ahmed 0:791a779d6220 62 * structure assignment. You'll need to fix this code if you have
shoaib_ahmed 0:791a779d6220 63 * such a compiler and you change MAX_COMPS_IN_SCAN.
shoaib_ahmed 0:791a779d6220 64 */
shoaib_ahmed 0:791a779d6220 65
shoaib_ahmed 0:791a779d6220 66 #ifndef NO_STRUCT_ASSIGN
shoaib_ahmed 0:791a779d6220 67 #define ASSIGN_STATE(dest,src) ((dest) = (src))
shoaib_ahmed 0:791a779d6220 68 #else
shoaib_ahmed 0:791a779d6220 69 #if MAX_COMPS_IN_SCAN == 4
shoaib_ahmed 0:791a779d6220 70 #define ASSIGN_STATE(dest,src) \
shoaib_ahmed 0:791a779d6220 71 ((dest).put_buffer = (src).put_buffer, \
shoaib_ahmed 0:791a779d6220 72 (dest).put_bits = (src).put_bits, \
shoaib_ahmed 0:791a779d6220 73 (dest).last_dc_val[0] = (src).last_dc_val[0], \
shoaib_ahmed 0:791a779d6220 74 (dest).last_dc_val[1] = (src).last_dc_val[1], \
shoaib_ahmed 0:791a779d6220 75 (dest).last_dc_val[2] = (src).last_dc_val[2], \
shoaib_ahmed 0:791a779d6220 76 (dest).last_dc_val[3] = (src).last_dc_val[3])
shoaib_ahmed 0:791a779d6220 77 #endif
shoaib_ahmed 0:791a779d6220 78 #endif
shoaib_ahmed 0:791a779d6220 79
shoaib_ahmed 0:791a779d6220 80
shoaib_ahmed 0:791a779d6220 81 typedef struct {
shoaib_ahmed 0:791a779d6220 82 struct jpeg_entropy_encoder pub; /* public fields */
shoaib_ahmed 0:791a779d6220 83
shoaib_ahmed 0:791a779d6220 84 savable_state saved; /* Bit buffer & DC state at start of MCU */
shoaib_ahmed 0:791a779d6220 85
shoaib_ahmed 0:791a779d6220 86 /* These fields are NOT loaded into local working state. */
shoaib_ahmed 0:791a779d6220 87 unsigned int restarts_to_go; /* MCUs left in this restart interval */
shoaib_ahmed 0:791a779d6220 88 int next_restart_num; /* next restart number to write (0-7) */
shoaib_ahmed 0:791a779d6220 89
shoaib_ahmed 0:791a779d6220 90 /* Pointers to derived tables (these workspaces have image lifespan) */
shoaib_ahmed 0:791a779d6220 91 c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
shoaib_ahmed 0:791a779d6220 92 c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
shoaib_ahmed 0:791a779d6220 93
shoaib_ahmed 0:791a779d6220 94 /* Statistics tables for optimization */
shoaib_ahmed 0:791a779d6220 95 long * dc_count_ptrs[NUM_HUFF_TBLS];
shoaib_ahmed 0:791a779d6220 96 long * ac_count_ptrs[NUM_HUFF_TBLS];
shoaib_ahmed 0:791a779d6220 97
shoaib_ahmed 0:791a779d6220 98 /* Following fields used only in progressive mode */
shoaib_ahmed 0:791a779d6220 99
shoaib_ahmed 0:791a779d6220 100 /* Mode flag: TRUE for optimization, FALSE for actual data output */
shoaib_ahmed 0:791a779d6220 101 boolean gather_statistics;
shoaib_ahmed 0:791a779d6220 102
shoaib_ahmed 0:791a779d6220 103 /* next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
shoaib_ahmed 0:791a779d6220 104 */
shoaib_ahmed 0:791a779d6220 105 JOCTET * next_output_byte; /* => next byte to write in buffer */
shoaib_ahmed 0:791a779d6220 106 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
shoaib_ahmed 0:791a779d6220 107 j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
shoaib_ahmed 0:791a779d6220 108
shoaib_ahmed 0:791a779d6220 109 /* Coding status for AC components */
shoaib_ahmed 0:791a779d6220 110 int ac_tbl_no; /* the table number of the single component */
shoaib_ahmed 0:791a779d6220 111 unsigned int EOBRUN; /* run length of EOBs */
shoaib_ahmed 0:791a779d6220 112 unsigned int BE; /* # of buffered correction bits before MCU */
shoaib_ahmed 0:791a779d6220 113 char * bit_buffer; /* buffer for correction bits (1 per char) */
shoaib_ahmed 0:791a779d6220 114 /* packing correction bits tightly would save some space but cost time... */
shoaib_ahmed 0:791a779d6220 115 } huff_entropy_encoder;
shoaib_ahmed 0:791a779d6220 116
shoaib_ahmed 0:791a779d6220 117 typedef huff_entropy_encoder * huff_entropy_ptr;
shoaib_ahmed 0:791a779d6220 118
shoaib_ahmed 0:791a779d6220 119 /* Working state while writing an MCU (sequential mode).
shoaib_ahmed 0:791a779d6220 120 * This struct contains all the fields that are needed by subroutines.
shoaib_ahmed 0:791a779d6220 121 */
shoaib_ahmed 0:791a779d6220 122
shoaib_ahmed 0:791a779d6220 123 typedef struct {
shoaib_ahmed 0:791a779d6220 124 JOCTET * next_output_byte; /* => next byte to write in buffer */
shoaib_ahmed 0:791a779d6220 125 size_t free_in_buffer; /* # of byte spaces remaining in buffer */
shoaib_ahmed 0:791a779d6220 126 savable_state cur; /* Current bit buffer & DC state */
shoaib_ahmed 0:791a779d6220 127 j_compress_ptr cinfo; /* dump_buffer needs access to this */
shoaib_ahmed 0:791a779d6220 128 } working_state;
shoaib_ahmed 0:791a779d6220 129
shoaib_ahmed 0:791a779d6220 130 /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
shoaib_ahmed 0:791a779d6220 131 * buffer can hold. Larger sizes may slightly improve compression, but
shoaib_ahmed 0:791a779d6220 132 * 1000 is already well into the realm of overkill.
shoaib_ahmed 0:791a779d6220 133 * The minimum safe size is 64 bits.
shoaib_ahmed 0:791a779d6220 134 */
shoaib_ahmed 0:791a779d6220 135
shoaib_ahmed 0:791a779d6220 136 #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
shoaib_ahmed 0:791a779d6220 137
shoaib_ahmed 0:791a779d6220 138 /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
shoaib_ahmed 0:791a779d6220 139 * We assume that int right shift is unsigned if INT32 right shift is,
shoaib_ahmed 0:791a779d6220 140 * which should be safe.
shoaib_ahmed 0:791a779d6220 141 */
shoaib_ahmed 0:791a779d6220 142
shoaib_ahmed 0:791a779d6220 143 #ifdef RIGHT_SHIFT_IS_UNSIGNED
shoaib_ahmed 0:791a779d6220 144 #define ISHIFT_TEMPS int ishift_temp;
shoaib_ahmed 0:791a779d6220 145 #define IRIGHT_SHIFT(x,shft) \
shoaib_ahmed 0:791a779d6220 146 ((ishift_temp = (x)) < 0 ? \
shoaib_ahmed 0:791a779d6220 147 (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
shoaib_ahmed 0:791a779d6220 148 (ishift_temp >> (shft)))
shoaib_ahmed 0:791a779d6220 149 #else
shoaib_ahmed 0:791a779d6220 150 #define ISHIFT_TEMPS
shoaib_ahmed 0:791a779d6220 151 #define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
shoaib_ahmed 0:791a779d6220 152 #endif
shoaib_ahmed 0:791a779d6220 153
shoaib_ahmed 0:791a779d6220 154
shoaib_ahmed 0:791a779d6220 155 /*
shoaib_ahmed 0:791a779d6220 156 * Compute the derived values for a Huffman table.
shoaib_ahmed 0:791a779d6220 157 * This routine also performs some validation checks on the table.
shoaib_ahmed 0:791a779d6220 158 */
shoaib_ahmed 0:791a779d6220 159
shoaib_ahmed 0:791a779d6220 160 LOCAL(void)
shoaib_ahmed 0:791a779d6220 161 jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
shoaib_ahmed 0:791a779d6220 162 c_derived_tbl ** pdtbl)
shoaib_ahmed 0:791a779d6220 163 {
shoaib_ahmed 0:791a779d6220 164 JHUFF_TBL *htbl;
shoaib_ahmed 0:791a779d6220 165 c_derived_tbl *dtbl;
shoaib_ahmed 0:791a779d6220 166 int p, i, l, lastp, si, maxsymbol;
shoaib_ahmed 0:791a779d6220 167 char huffsize[257];
shoaib_ahmed 0:791a779d6220 168 unsigned int huffcode[257];
shoaib_ahmed 0:791a779d6220 169 unsigned int code;
shoaib_ahmed 0:791a779d6220 170
shoaib_ahmed 0:791a779d6220 171 /* Note that huffsize[] and huffcode[] are filled in code-length order,
shoaib_ahmed 0:791a779d6220 172 * paralleling the order of the symbols themselves in htbl->huffval[].
shoaib_ahmed 0:791a779d6220 173 */
shoaib_ahmed 0:791a779d6220 174
shoaib_ahmed 0:791a779d6220 175 /* Find the input Huffman table */
shoaib_ahmed 0:791a779d6220 176 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
shoaib_ahmed 0:791a779d6220 177 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
shoaib_ahmed 0:791a779d6220 178 htbl =
shoaib_ahmed 0:791a779d6220 179 isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
shoaib_ahmed 0:791a779d6220 180 if (htbl == NULL)
shoaib_ahmed 0:791a779d6220 181 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
shoaib_ahmed 0:791a779d6220 182
shoaib_ahmed 0:791a779d6220 183 /* Allocate a workspace if we haven't already done so. */
shoaib_ahmed 0:791a779d6220 184 if (*pdtbl == NULL)
shoaib_ahmed 0:791a779d6220 185 *pdtbl = (c_derived_tbl *)
shoaib_ahmed 0:791a779d6220 186 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 187 SIZEOF(c_derived_tbl));
shoaib_ahmed 0:791a779d6220 188 dtbl = *pdtbl;
shoaib_ahmed 0:791a779d6220 189
shoaib_ahmed 0:791a779d6220 190 /* Figure C.1: make table of Huffman code length for each symbol */
shoaib_ahmed 0:791a779d6220 191
shoaib_ahmed 0:791a779d6220 192 p = 0;
shoaib_ahmed 0:791a779d6220 193 for (l = 1; l <= 16; l++) {
shoaib_ahmed 0:791a779d6220 194 i = (int) htbl->bits[l];
shoaib_ahmed 0:791a779d6220 195 if (i < 0 || p + i > 256) /* protect against table overrun */
shoaib_ahmed 0:791a779d6220 196 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
shoaib_ahmed 0:791a779d6220 197 while (i--)
shoaib_ahmed 0:791a779d6220 198 huffsize[p++] = (char) l;
shoaib_ahmed 0:791a779d6220 199 }
shoaib_ahmed 0:791a779d6220 200 huffsize[p] = 0;
shoaib_ahmed 0:791a779d6220 201 lastp = p;
shoaib_ahmed 0:791a779d6220 202
shoaib_ahmed 0:791a779d6220 203 /* Figure C.2: generate the codes themselves */
shoaib_ahmed 0:791a779d6220 204 /* We also validate that the counts represent a legal Huffman code tree. */
shoaib_ahmed 0:791a779d6220 205
shoaib_ahmed 0:791a779d6220 206 code = 0;
shoaib_ahmed 0:791a779d6220 207 si = huffsize[0];
shoaib_ahmed 0:791a779d6220 208 p = 0;
shoaib_ahmed 0:791a779d6220 209 while (huffsize[p]) {
shoaib_ahmed 0:791a779d6220 210 while (((int) huffsize[p]) == si) {
shoaib_ahmed 0:791a779d6220 211 huffcode[p++] = code;
shoaib_ahmed 0:791a779d6220 212 code++;
shoaib_ahmed 0:791a779d6220 213 }
shoaib_ahmed 0:791a779d6220 214 /* code is now 1 more than the last code used for codelength si; but
shoaib_ahmed 0:791a779d6220 215 * it must still fit in si bits, since no code is allowed to be all ones.
shoaib_ahmed 0:791a779d6220 216 */
shoaib_ahmed 0:791a779d6220 217 if (((INT32) code) >= (((INT32) 1) << si))
shoaib_ahmed 0:791a779d6220 218 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
shoaib_ahmed 0:791a779d6220 219 code <<= 1;
shoaib_ahmed 0:791a779d6220 220 si++;
shoaib_ahmed 0:791a779d6220 221 }
shoaib_ahmed 0:791a779d6220 222
shoaib_ahmed 0:791a779d6220 223 /* Figure C.3: generate encoding tables */
shoaib_ahmed 0:791a779d6220 224 /* These are code and size indexed by symbol value */
shoaib_ahmed 0:791a779d6220 225
shoaib_ahmed 0:791a779d6220 226 /* Set all codeless symbols to have code length 0;
shoaib_ahmed 0:791a779d6220 227 * this lets us detect duplicate VAL entries here, and later
shoaib_ahmed 0:791a779d6220 228 * allows emit_bits to detect any attempt to emit such symbols.
shoaib_ahmed 0:791a779d6220 229 */
shoaib_ahmed 0:791a779d6220 230 MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
shoaib_ahmed 0:791a779d6220 231
shoaib_ahmed 0:791a779d6220 232 /* This is also a convenient place to check for out-of-range
shoaib_ahmed 0:791a779d6220 233 * and duplicated VAL entries. We allow 0..255 for AC symbols
shoaib_ahmed 0:791a779d6220 234 * but only 0..15 for DC. (We could constrain them further
shoaib_ahmed 0:791a779d6220 235 * based on data depth and mode, but this seems enough.)
shoaib_ahmed 0:791a779d6220 236 */
shoaib_ahmed 0:791a779d6220 237 maxsymbol = isDC ? 15 : 255;
shoaib_ahmed 0:791a779d6220 238
shoaib_ahmed 0:791a779d6220 239 for (p = 0; p < lastp; p++) {
shoaib_ahmed 0:791a779d6220 240 i = htbl->huffval[p];
shoaib_ahmed 0:791a779d6220 241 if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
shoaib_ahmed 0:791a779d6220 242 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
shoaib_ahmed 0:791a779d6220 243 dtbl->ehufco[i] = huffcode[p];
shoaib_ahmed 0:791a779d6220 244 dtbl->ehufsi[i] = huffsize[p];
shoaib_ahmed 0:791a779d6220 245 }
shoaib_ahmed 0:791a779d6220 246 }
shoaib_ahmed 0:791a779d6220 247
shoaib_ahmed 0:791a779d6220 248
shoaib_ahmed 0:791a779d6220 249 /* Outputting bytes to the file.
shoaib_ahmed 0:791a779d6220 250 * NB: these must be called only when actually outputting,
shoaib_ahmed 0:791a779d6220 251 * that is, entropy->gather_statistics == FALSE.
shoaib_ahmed 0:791a779d6220 252 */
shoaib_ahmed 0:791a779d6220 253
shoaib_ahmed 0:791a779d6220 254 /* Emit a byte, taking 'action' if must suspend. */
shoaib_ahmed 0:791a779d6220 255 #define emit_byte_s(state,val,action) \
shoaib_ahmed 0:791a779d6220 256 { *(state)->next_output_byte++ = (JOCTET) (val); \
shoaib_ahmed 0:791a779d6220 257 if (--(state)->free_in_buffer == 0) \
shoaib_ahmed 0:791a779d6220 258 if (! dump_buffer_s(state)) \
shoaib_ahmed 0:791a779d6220 259 { action; } }
shoaib_ahmed 0:791a779d6220 260
shoaib_ahmed 0:791a779d6220 261 /* Emit a byte */
shoaib_ahmed 0:791a779d6220 262 #define emit_byte_e(entropy,val) \
shoaib_ahmed 0:791a779d6220 263 { *(entropy)->next_output_byte++ = (JOCTET) (val); \
shoaib_ahmed 0:791a779d6220 264 if (--(entropy)->free_in_buffer == 0) \
shoaib_ahmed 0:791a779d6220 265 dump_buffer_e(entropy); }
shoaib_ahmed 0:791a779d6220 266
shoaib_ahmed 0:791a779d6220 267
shoaib_ahmed 0:791a779d6220 268 LOCAL(boolean)
shoaib_ahmed 0:791a779d6220 269 dump_buffer_s (working_state * state)
shoaib_ahmed 0:791a779d6220 270 /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
shoaib_ahmed 0:791a779d6220 271 {
shoaib_ahmed 0:791a779d6220 272 struct jpeg_destination_mgr * dest = state->cinfo->dest;
shoaib_ahmed 0:791a779d6220 273
shoaib_ahmed 0:791a779d6220 274 if (! (*dest->empty_output_buffer) (state->cinfo))
shoaib_ahmed 0:791a779d6220 275 return FALSE;
shoaib_ahmed 0:791a779d6220 276 /* After a successful buffer dump, must reset buffer pointers */
shoaib_ahmed 0:791a779d6220 277 state->next_output_byte = dest->next_output_byte;
shoaib_ahmed 0:791a779d6220 278 state->free_in_buffer = dest->free_in_buffer;
shoaib_ahmed 0:791a779d6220 279 return TRUE;
shoaib_ahmed 0:791a779d6220 280 }
shoaib_ahmed 0:791a779d6220 281
shoaib_ahmed 0:791a779d6220 282
shoaib_ahmed 0:791a779d6220 283 LOCAL(void)
shoaib_ahmed 0:791a779d6220 284 dump_buffer_e (huff_entropy_ptr entropy)
shoaib_ahmed 0:791a779d6220 285 /* Empty the output buffer; we do not support suspension in this case. */
shoaib_ahmed 0:791a779d6220 286 {
shoaib_ahmed 0:791a779d6220 287 struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
shoaib_ahmed 0:791a779d6220 288
shoaib_ahmed 0:791a779d6220 289 if (! (*dest->empty_output_buffer) (entropy->cinfo))
shoaib_ahmed 0:791a779d6220 290 ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
shoaib_ahmed 0:791a779d6220 291 /* After a successful buffer dump, must reset buffer pointers */
shoaib_ahmed 0:791a779d6220 292 entropy->next_output_byte = dest->next_output_byte;
shoaib_ahmed 0:791a779d6220 293 entropy->free_in_buffer = dest->free_in_buffer;
shoaib_ahmed 0:791a779d6220 294 }
shoaib_ahmed 0:791a779d6220 295
shoaib_ahmed 0:791a779d6220 296
shoaib_ahmed 0:791a779d6220 297 /* Outputting bits to the file */
shoaib_ahmed 0:791a779d6220 298
shoaib_ahmed 0:791a779d6220 299 /* Only the right 24 bits of put_buffer are used; the valid bits are
shoaib_ahmed 0:791a779d6220 300 * left-justified in this part. At most 16 bits can be passed to emit_bits
shoaib_ahmed 0:791a779d6220 301 * in one call, and we never retain more than 7 bits in put_buffer
shoaib_ahmed 0:791a779d6220 302 * between calls, so 24 bits are sufficient.
shoaib_ahmed 0:791a779d6220 303 */
shoaib_ahmed 0:791a779d6220 304
shoaib_ahmed 0:791a779d6220 305 INLINE
shoaib_ahmed 0:791a779d6220 306 LOCAL(boolean)
shoaib_ahmed 0:791a779d6220 307 emit_bits_s (working_state * state, unsigned int code, int size)
shoaib_ahmed 0:791a779d6220 308 /* Emit some bits; return TRUE if successful, FALSE if must suspend */
shoaib_ahmed 0:791a779d6220 309 {
shoaib_ahmed 0:791a779d6220 310 /* This routine is heavily used, so it's worth coding tightly. */
shoaib_ahmed 0:791a779d6220 311 register INT32 put_buffer;
shoaib_ahmed 0:791a779d6220 312 register int put_bits;
shoaib_ahmed 0:791a779d6220 313
shoaib_ahmed 0:791a779d6220 314 /* if size is 0, caller used an invalid Huffman table entry */
shoaib_ahmed 0:791a779d6220 315 if (size == 0)
shoaib_ahmed 0:791a779d6220 316 ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
shoaib_ahmed 0:791a779d6220 317
shoaib_ahmed 0:791a779d6220 318 /* mask off any extra bits in code */
shoaib_ahmed 0:791a779d6220 319 put_buffer = ((INT32) code) & ((((INT32) 1) << size) - 1);
shoaib_ahmed 0:791a779d6220 320
shoaib_ahmed 0:791a779d6220 321 /* new number of bits in buffer */
shoaib_ahmed 0:791a779d6220 322 put_bits = size + state->cur.put_bits;
shoaib_ahmed 0:791a779d6220 323
shoaib_ahmed 0:791a779d6220 324 put_buffer <<= 24 - put_bits; /* align incoming bits */
shoaib_ahmed 0:791a779d6220 325
shoaib_ahmed 0:791a779d6220 326 /* and merge with old buffer contents */
shoaib_ahmed 0:791a779d6220 327 put_buffer |= state->cur.put_buffer;
shoaib_ahmed 0:791a779d6220 328
shoaib_ahmed 0:791a779d6220 329 while (put_bits >= 8) {
shoaib_ahmed 0:791a779d6220 330 int c = (int) ((put_buffer >> 16) & 0xFF);
shoaib_ahmed 0:791a779d6220 331
shoaib_ahmed 0:791a779d6220 332 emit_byte_s(state, c, return FALSE);
shoaib_ahmed 0:791a779d6220 333 if (c == 0xFF) { /* need to stuff a zero byte? */
shoaib_ahmed 0:791a779d6220 334 emit_byte_s(state, 0, return FALSE);
shoaib_ahmed 0:791a779d6220 335 }
shoaib_ahmed 0:791a779d6220 336 put_buffer <<= 8;
shoaib_ahmed 0:791a779d6220 337 put_bits -= 8;
shoaib_ahmed 0:791a779d6220 338 }
shoaib_ahmed 0:791a779d6220 339
shoaib_ahmed 0:791a779d6220 340 state->cur.put_buffer = put_buffer; /* update state variables */
shoaib_ahmed 0:791a779d6220 341 state->cur.put_bits = put_bits;
shoaib_ahmed 0:791a779d6220 342
shoaib_ahmed 0:791a779d6220 343 return TRUE;
shoaib_ahmed 0:791a779d6220 344 }
shoaib_ahmed 0:791a779d6220 345
shoaib_ahmed 0:791a779d6220 346
shoaib_ahmed 0:791a779d6220 347 INLINE
shoaib_ahmed 0:791a779d6220 348 LOCAL(void)
shoaib_ahmed 0:791a779d6220 349 emit_bits_e (huff_entropy_ptr entropy, unsigned int code, int size)
shoaib_ahmed 0:791a779d6220 350 /* Emit some bits, unless we are in gather mode */
shoaib_ahmed 0:791a779d6220 351 {
shoaib_ahmed 0:791a779d6220 352 /* This routine is heavily used, so it's worth coding tightly. */
shoaib_ahmed 0:791a779d6220 353 register INT32 put_buffer;
shoaib_ahmed 0:791a779d6220 354 register int put_bits;
shoaib_ahmed 0:791a779d6220 355
shoaib_ahmed 0:791a779d6220 356 /* if size is 0, caller used an invalid Huffman table entry */
shoaib_ahmed 0:791a779d6220 357 if (size == 0)
shoaib_ahmed 0:791a779d6220 358 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
shoaib_ahmed 0:791a779d6220 359
shoaib_ahmed 0:791a779d6220 360 if (entropy->gather_statistics)
shoaib_ahmed 0:791a779d6220 361 return; /* do nothing if we're only getting stats */
shoaib_ahmed 0:791a779d6220 362
shoaib_ahmed 0:791a779d6220 363 /* mask off any extra bits in code */
shoaib_ahmed 0:791a779d6220 364 put_buffer = ((INT32) code) & ((((INT32) 1) << size) - 1);
shoaib_ahmed 0:791a779d6220 365
shoaib_ahmed 0:791a779d6220 366 /* new number of bits in buffer */
shoaib_ahmed 0:791a779d6220 367 put_bits = size + entropy->saved.put_bits;
shoaib_ahmed 0:791a779d6220 368
shoaib_ahmed 0:791a779d6220 369 put_buffer <<= 24 - put_bits; /* align incoming bits */
shoaib_ahmed 0:791a779d6220 370
shoaib_ahmed 0:791a779d6220 371 /* and merge with old buffer contents */
shoaib_ahmed 0:791a779d6220 372 put_buffer |= entropy->saved.put_buffer;
shoaib_ahmed 0:791a779d6220 373
shoaib_ahmed 0:791a779d6220 374 while (put_bits >= 8) {
shoaib_ahmed 0:791a779d6220 375 int c = (int) ((put_buffer >> 16) & 0xFF);
shoaib_ahmed 0:791a779d6220 376
shoaib_ahmed 0:791a779d6220 377 emit_byte_e(entropy, c);
shoaib_ahmed 0:791a779d6220 378 if (c == 0xFF) { /* need to stuff a zero byte? */
shoaib_ahmed 0:791a779d6220 379 emit_byte_e(entropy, 0);
shoaib_ahmed 0:791a779d6220 380 }
shoaib_ahmed 0:791a779d6220 381 put_buffer <<= 8;
shoaib_ahmed 0:791a779d6220 382 put_bits -= 8;
shoaib_ahmed 0:791a779d6220 383 }
shoaib_ahmed 0:791a779d6220 384
shoaib_ahmed 0:791a779d6220 385 entropy->saved.put_buffer = put_buffer; /* update variables */
shoaib_ahmed 0:791a779d6220 386 entropy->saved.put_bits = put_bits;
shoaib_ahmed 0:791a779d6220 387 }
shoaib_ahmed 0:791a779d6220 388
shoaib_ahmed 0:791a779d6220 389
shoaib_ahmed 0:791a779d6220 390 LOCAL(boolean)
shoaib_ahmed 0:791a779d6220 391 flush_bits_s (working_state * state)
shoaib_ahmed 0:791a779d6220 392 {
shoaib_ahmed 0:791a779d6220 393 if (! emit_bits_s(state, 0x7F, 7)) /* fill any partial byte with ones */
shoaib_ahmed 0:791a779d6220 394 return FALSE;
shoaib_ahmed 0:791a779d6220 395 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
shoaib_ahmed 0:791a779d6220 396 state->cur.put_bits = 0;
shoaib_ahmed 0:791a779d6220 397 return TRUE;
shoaib_ahmed 0:791a779d6220 398 }
shoaib_ahmed 0:791a779d6220 399
shoaib_ahmed 0:791a779d6220 400
shoaib_ahmed 0:791a779d6220 401 LOCAL(void)
shoaib_ahmed 0:791a779d6220 402 flush_bits_e (huff_entropy_ptr entropy)
shoaib_ahmed 0:791a779d6220 403 {
shoaib_ahmed 0:791a779d6220 404 emit_bits_e(entropy, 0x7F, 7); /* fill any partial byte with ones */
shoaib_ahmed 0:791a779d6220 405 entropy->saved.put_buffer = 0; /* and reset bit-buffer to empty */
shoaib_ahmed 0:791a779d6220 406 entropy->saved.put_bits = 0;
shoaib_ahmed 0:791a779d6220 407 }
shoaib_ahmed 0:791a779d6220 408
shoaib_ahmed 0:791a779d6220 409
shoaib_ahmed 0:791a779d6220 410 /*
shoaib_ahmed 0:791a779d6220 411 * Emit (or just count) a Huffman symbol.
shoaib_ahmed 0:791a779d6220 412 */
shoaib_ahmed 0:791a779d6220 413
shoaib_ahmed 0:791a779d6220 414 INLINE
shoaib_ahmed 0:791a779d6220 415 LOCAL(void)
shoaib_ahmed 0:791a779d6220 416 emit_dc_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
shoaib_ahmed 0:791a779d6220 417 {
shoaib_ahmed 0:791a779d6220 418 if (entropy->gather_statistics)
shoaib_ahmed 0:791a779d6220 419 entropy->dc_count_ptrs[tbl_no][symbol]++;
shoaib_ahmed 0:791a779d6220 420 else {
shoaib_ahmed 0:791a779d6220 421 c_derived_tbl * tbl = entropy->dc_derived_tbls[tbl_no];
shoaib_ahmed 0:791a779d6220 422 emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
shoaib_ahmed 0:791a779d6220 423 }
shoaib_ahmed 0:791a779d6220 424 }
shoaib_ahmed 0:791a779d6220 425
shoaib_ahmed 0:791a779d6220 426
shoaib_ahmed 0:791a779d6220 427 INLINE
shoaib_ahmed 0:791a779d6220 428 LOCAL(void)
shoaib_ahmed 0:791a779d6220 429 emit_ac_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
shoaib_ahmed 0:791a779d6220 430 {
shoaib_ahmed 0:791a779d6220 431 if (entropy->gather_statistics)
shoaib_ahmed 0:791a779d6220 432 entropy->ac_count_ptrs[tbl_no][symbol]++;
shoaib_ahmed 0:791a779d6220 433 else {
shoaib_ahmed 0:791a779d6220 434 c_derived_tbl * tbl = entropy->ac_derived_tbls[tbl_no];
shoaib_ahmed 0:791a779d6220 435 emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
shoaib_ahmed 0:791a779d6220 436 }
shoaib_ahmed 0:791a779d6220 437 }
shoaib_ahmed 0:791a779d6220 438
shoaib_ahmed 0:791a779d6220 439
shoaib_ahmed 0:791a779d6220 440 /*
shoaib_ahmed 0:791a779d6220 441 * Emit bits from a correction bit buffer.
shoaib_ahmed 0:791a779d6220 442 */
shoaib_ahmed 0:791a779d6220 443
shoaib_ahmed 0:791a779d6220 444 LOCAL(void)
shoaib_ahmed 0:791a779d6220 445 emit_buffered_bits (huff_entropy_ptr entropy, char * bufstart,
shoaib_ahmed 0:791a779d6220 446 unsigned int nbits)
shoaib_ahmed 0:791a779d6220 447 {
shoaib_ahmed 0:791a779d6220 448 if (entropy->gather_statistics)
shoaib_ahmed 0:791a779d6220 449 return; /* no real work */
shoaib_ahmed 0:791a779d6220 450
shoaib_ahmed 0:791a779d6220 451 while (nbits > 0) {
shoaib_ahmed 0:791a779d6220 452 emit_bits_e(entropy, (unsigned int) (*bufstart), 1);
shoaib_ahmed 0:791a779d6220 453 bufstart++;
shoaib_ahmed 0:791a779d6220 454 nbits--;
shoaib_ahmed 0:791a779d6220 455 }
shoaib_ahmed 0:791a779d6220 456 }
shoaib_ahmed 0:791a779d6220 457
shoaib_ahmed 0:791a779d6220 458
shoaib_ahmed 0:791a779d6220 459 /*
shoaib_ahmed 0:791a779d6220 460 * Emit any pending EOBRUN symbol.
shoaib_ahmed 0:791a779d6220 461 */
shoaib_ahmed 0:791a779d6220 462
shoaib_ahmed 0:791a779d6220 463 LOCAL(void)
shoaib_ahmed 0:791a779d6220 464 emit_eobrun (huff_entropy_ptr entropy)
shoaib_ahmed 0:791a779d6220 465 {
shoaib_ahmed 0:791a779d6220 466 register int temp, nbits;
shoaib_ahmed 0:791a779d6220 467
shoaib_ahmed 0:791a779d6220 468 if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
shoaib_ahmed 0:791a779d6220 469 temp = entropy->EOBRUN;
shoaib_ahmed 0:791a779d6220 470 nbits = 0;
shoaib_ahmed 0:791a779d6220 471 while ((temp >>= 1))
shoaib_ahmed 0:791a779d6220 472 nbits++;
shoaib_ahmed 0:791a779d6220 473 /* safety check: shouldn't happen given limited correction-bit buffer */
shoaib_ahmed 0:791a779d6220 474 if (nbits > 14)
shoaib_ahmed 0:791a779d6220 475 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
shoaib_ahmed 0:791a779d6220 476
shoaib_ahmed 0:791a779d6220 477 emit_ac_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
shoaib_ahmed 0:791a779d6220 478 if (nbits)
shoaib_ahmed 0:791a779d6220 479 emit_bits_e(entropy, entropy->EOBRUN, nbits);
shoaib_ahmed 0:791a779d6220 480
shoaib_ahmed 0:791a779d6220 481 entropy->EOBRUN = 0;
shoaib_ahmed 0:791a779d6220 482
shoaib_ahmed 0:791a779d6220 483 /* Emit any buffered correction bits */
shoaib_ahmed 0:791a779d6220 484 emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
shoaib_ahmed 0:791a779d6220 485 entropy->BE = 0;
shoaib_ahmed 0:791a779d6220 486 }
shoaib_ahmed 0:791a779d6220 487 }
shoaib_ahmed 0:791a779d6220 488
shoaib_ahmed 0:791a779d6220 489
shoaib_ahmed 0:791a779d6220 490 /*
shoaib_ahmed 0:791a779d6220 491 * Emit a restart marker & resynchronize predictions.
shoaib_ahmed 0:791a779d6220 492 */
shoaib_ahmed 0:791a779d6220 493
shoaib_ahmed 0:791a779d6220 494 LOCAL(boolean)
shoaib_ahmed 0:791a779d6220 495 emit_restart_s (working_state * state, int restart_num)
shoaib_ahmed 0:791a779d6220 496 {
shoaib_ahmed 0:791a779d6220 497 int ci;
shoaib_ahmed 0:791a779d6220 498
shoaib_ahmed 0:791a779d6220 499 if (! flush_bits_s(state))
shoaib_ahmed 0:791a779d6220 500 return FALSE;
shoaib_ahmed 0:791a779d6220 501
shoaib_ahmed 0:791a779d6220 502 emit_byte_s(state, 0xFF, return FALSE);
shoaib_ahmed 0:791a779d6220 503 emit_byte_s(state, JPEG_RST0 + restart_num, return FALSE);
shoaib_ahmed 0:791a779d6220 504
shoaib_ahmed 0:791a779d6220 505 /* Re-initialize DC predictions to 0 */
shoaib_ahmed 0:791a779d6220 506 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
shoaib_ahmed 0:791a779d6220 507 state->cur.last_dc_val[ci] = 0;
shoaib_ahmed 0:791a779d6220 508
shoaib_ahmed 0:791a779d6220 509 /* The restart counter is not updated until we successfully write the MCU. */
shoaib_ahmed 0:791a779d6220 510
shoaib_ahmed 0:791a779d6220 511 return TRUE;
shoaib_ahmed 0:791a779d6220 512 }
shoaib_ahmed 0:791a779d6220 513
shoaib_ahmed 0:791a779d6220 514
shoaib_ahmed 0:791a779d6220 515 LOCAL(void)
shoaib_ahmed 0:791a779d6220 516 emit_restart_e (huff_entropy_ptr entropy, int restart_num)
shoaib_ahmed 0:791a779d6220 517 {
shoaib_ahmed 0:791a779d6220 518 int ci;
shoaib_ahmed 0:791a779d6220 519
shoaib_ahmed 0:791a779d6220 520 emit_eobrun(entropy);
shoaib_ahmed 0:791a779d6220 521
shoaib_ahmed 0:791a779d6220 522 if (! entropy->gather_statistics) {
shoaib_ahmed 0:791a779d6220 523 flush_bits_e(entropy);
shoaib_ahmed 0:791a779d6220 524 emit_byte_e(entropy, 0xFF);
shoaib_ahmed 0:791a779d6220 525 emit_byte_e(entropy, JPEG_RST0 + restart_num);
shoaib_ahmed 0:791a779d6220 526 }
shoaib_ahmed 0:791a779d6220 527
shoaib_ahmed 0:791a779d6220 528 if (entropy->cinfo->Ss == 0) {
shoaib_ahmed 0:791a779d6220 529 /* Re-initialize DC predictions to 0 */
shoaib_ahmed 0:791a779d6220 530 for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
shoaib_ahmed 0:791a779d6220 531 entropy->saved.last_dc_val[ci] = 0;
shoaib_ahmed 0:791a779d6220 532 } else {
shoaib_ahmed 0:791a779d6220 533 /* Re-initialize all AC-related fields to 0 */
shoaib_ahmed 0:791a779d6220 534 entropy->EOBRUN = 0;
shoaib_ahmed 0:791a779d6220 535 entropy->BE = 0;
shoaib_ahmed 0:791a779d6220 536 }
shoaib_ahmed 0:791a779d6220 537 }
shoaib_ahmed 0:791a779d6220 538
shoaib_ahmed 0:791a779d6220 539
shoaib_ahmed 0:791a779d6220 540 /*
shoaib_ahmed 0:791a779d6220 541 * MCU encoding for DC initial scan (either spectral selection,
shoaib_ahmed 0:791a779d6220 542 * or first pass of successive approximation).
shoaib_ahmed 0:791a779d6220 543 */
shoaib_ahmed 0:791a779d6220 544
shoaib_ahmed 0:791a779d6220 545 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 546 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shoaib_ahmed 0:791a779d6220 547 {
shoaib_ahmed 0:791a779d6220 548 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 549 register int temp, temp2;
shoaib_ahmed 0:791a779d6220 550 register int nbits;
shoaib_ahmed 0:791a779d6220 551 int blkn, ci, tbl;
shoaib_ahmed 0:791a779d6220 552 ISHIFT_TEMPS
shoaib_ahmed 0:791a779d6220 553
shoaib_ahmed 0:791a779d6220 554 entropy->next_output_byte = cinfo->dest->next_output_byte;
shoaib_ahmed 0:791a779d6220 555 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
shoaib_ahmed 0:791a779d6220 556
shoaib_ahmed 0:791a779d6220 557 /* Emit restart marker if needed */
shoaib_ahmed 0:791a779d6220 558 if (cinfo->restart_interval)
shoaib_ahmed 0:791a779d6220 559 if (entropy->restarts_to_go == 0)
shoaib_ahmed 0:791a779d6220 560 emit_restart_e(entropy, entropy->next_restart_num);
shoaib_ahmed 0:791a779d6220 561
shoaib_ahmed 0:791a779d6220 562 /* Encode the MCU data blocks */
shoaib_ahmed 0:791a779d6220 563 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
shoaib_ahmed 0:791a779d6220 564 ci = cinfo->MCU_membership[blkn];
shoaib_ahmed 0:791a779d6220 565 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
shoaib_ahmed 0:791a779d6220 566
shoaib_ahmed 0:791a779d6220 567 /* Compute the DC value after the required point transform by Al.
shoaib_ahmed 0:791a779d6220 568 * This is simply an arithmetic right shift.
shoaib_ahmed 0:791a779d6220 569 */
shoaib_ahmed 0:791a779d6220 570 temp = IRIGHT_SHIFT((int) (MCU_data[blkn][0][0]), cinfo->Al);
shoaib_ahmed 0:791a779d6220 571
shoaib_ahmed 0:791a779d6220 572 /* DC differences are figured on the point-transformed values. */
shoaib_ahmed 0:791a779d6220 573 temp2 = temp - entropy->saved.last_dc_val[ci];
shoaib_ahmed 0:791a779d6220 574 entropy->saved.last_dc_val[ci] = temp;
shoaib_ahmed 0:791a779d6220 575
shoaib_ahmed 0:791a779d6220 576 /* Encode the DC coefficient difference per section G.1.2.1 */
shoaib_ahmed 0:791a779d6220 577 temp = temp2;
shoaib_ahmed 0:791a779d6220 578 if (temp < 0) {
shoaib_ahmed 0:791a779d6220 579 temp = -temp; /* temp is abs value of input */
shoaib_ahmed 0:791a779d6220 580 /* For a negative input, want temp2 = bitwise complement of abs(input) */
shoaib_ahmed 0:791a779d6220 581 /* This code assumes we are on a two's complement machine */
shoaib_ahmed 0:791a779d6220 582 temp2--;
shoaib_ahmed 0:791a779d6220 583 }
shoaib_ahmed 0:791a779d6220 584
shoaib_ahmed 0:791a779d6220 585 /* Find the number of bits needed for the magnitude of the coefficient */
shoaib_ahmed 0:791a779d6220 586 nbits = 0;
shoaib_ahmed 0:791a779d6220 587 while (temp) {
shoaib_ahmed 0:791a779d6220 588 nbits++;
shoaib_ahmed 0:791a779d6220 589 temp >>= 1;
shoaib_ahmed 0:791a779d6220 590 }
shoaib_ahmed 0:791a779d6220 591 /* Check for out-of-range coefficient values.
shoaib_ahmed 0:791a779d6220 592 * Since we're encoding a difference, the range limit is twice as much.
shoaib_ahmed 0:791a779d6220 593 */
shoaib_ahmed 0:791a779d6220 594 if (nbits > MAX_COEF_BITS+1)
shoaib_ahmed 0:791a779d6220 595 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
shoaib_ahmed 0:791a779d6220 596
shoaib_ahmed 0:791a779d6220 597 /* Count/emit the Huffman-coded symbol for the number of bits */
shoaib_ahmed 0:791a779d6220 598 emit_dc_symbol(entropy, tbl, nbits);
shoaib_ahmed 0:791a779d6220 599
shoaib_ahmed 0:791a779d6220 600 /* Emit that number of bits of the value, if positive, */
shoaib_ahmed 0:791a779d6220 601 /* or the complement of its magnitude, if negative. */
shoaib_ahmed 0:791a779d6220 602 if (nbits) /* emit_bits rejects calls with size 0 */
shoaib_ahmed 0:791a779d6220 603 emit_bits_e(entropy, (unsigned int) temp2, nbits);
shoaib_ahmed 0:791a779d6220 604 }
shoaib_ahmed 0:791a779d6220 605
shoaib_ahmed 0:791a779d6220 606 cinfo->dest->next_output_byte = entropy->next_output_byte;
shoaib_ahmed 0:791a779d6220 607 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
shoaib_ahmed 0:791a779d6220 608
shoaib_ahmed 0:791a779d6220 609 /* Update restart-interval state too */
shoaib_ahmed 0:791a779d6220 610 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 611 if (entropy->restarts_to_go == 0) {
shoaib_ahmed 0:791a779d6220 612 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 613 entropy->next_restart_num++;
shoaib_ahmed 0:791a779d6220 614 entropy->next_restart_num &= 7;
shoaib_ahmed 0:791a779d6220 615 }
shoaib_ahmed 0:791a779d6220 616 entropy->restarts_to_go--;
shoaib_ahmed 0:791a779d6220 617 }
shoaib_ahmed 0:791a779d6220 618
shoaib_ahmed 0:791a779d6220 619 return TRUE;
shoaib_ahmed 0:791a779d6220 620 }
shoaib_ahmed 0:791a779d6220 621
shoaib_ahmed 0:791a779d6220 622
shoaib_ahmed 0:791a779d6220 623 /*
shoaib_ahmed 0:791a779d6220 624 * MCU encoding for AC initial scan (either spectral selection,
shoaib_ahmed 0:791a779d6220 625 * or first pass of successive approximation).
shoaib_ahmed 0:791a779d6220 626 */
shoaib_ahmed 0:791a779d6220 627
shoaib_ahmed 0:791a779d6220 628 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 629 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shoaib_ahmed 0:791a779d6220 630 {
shoaib_ahmed 0:791a779d6220 631 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 632 const int * natural_order;
shoaib_ahmed 0:791a779d6220 633 JBLOCKROW block;
shoaib_ahmed 0:791a779d6220 634 register int temp, temp2;
shoaib_ahmed 0:791a779d6220 635 register int nbits;
shoaib_ahmed 0:791a779d6220 636 register int r, k;
shoaib_ahmed 0:791a779d6220 637 int Se, Al;
shoaib_ahmed 0:791a779d6220 638
shoaib_ahmed 0:791a779d6220 639 entropy->next_output_byte = cinfo->dest->next_output_byte;
shoaib_ahmed 0:791a779d6220 640 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
shoaib_ahmed 0:791a779d6220 641
shoaib_ahmed 0:791a779d6220 642 /* Emit restart marker if needed */
shoaib_ahmed 0:791a779d6220 643 if (cinfo->restart_interval)
shoaib_ahmed 0:791a779d6220 644 if (entropy->restarts_to_go == 0)
shoaib_ahmed 0:791a779d6220 645 emit_restart_e(entropy, entropy->next_restart_num);
shoaib_ahmed 0:791a779d6220 646
shoaib_ahmed 0:791a779d6220 647 Se = cinfo->Se;
shoaib_ahmed 0:791a779d6220 648 Al = cinfo->Al;
shoaib_ahmed 0:791a779d6220 649 natural_order = cinfo->natural_order;
shoaib_ahmed 0:791a779d6220 650
shoaib_ahmed 0:791a779d6220 651 /* Encode the MCU data block */
shoaib_ahmed 0:791a779d6220 652 block = MCU_data[0];
shoaib_ahmed 0:791a779d6220 653
shoaib_ahmed 0:791a779d6220 654 /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
shoaib_ahmed 0:791a779d6220 655
shoaib_ahmed 0:791a779d6220 656 r = 0; /* r = run length of zeros */
shoaib_ahmed 0:791a779d6220 657
shoaib_ahmed 0:791a779d6220 658 for (k = cinfo->Ss; k <= Se; k++) {
shoaib_ahmed 0:791a779d6220 659 if ((temp = (*block)[natural_order[k]]) == 0) {
shoaib_ahmed 0:791a779d6220 660 r++;
shoaib_ahmed 0:791a779d6220 661 continue;
shoaib_ahmed 0:791a779d6220 662 }
shoaib_ahmed 0:791a779d6220 663 /* We must apply the point transform by Al. For AC coefficients this
shoaib_ahmed 0:791a779d6220 664 * is an integer division with rounding towards 0. To do this portably
shoaib_ahmed 0:791a779d6220 665 * in C, we shift after obtaining the absolute value; so the code is
shoaib_ahmed 0:791a779d6220 666 * interwoven with finding the abs value (temp) and output bits (temp2).
shoaib_ahmed 0:791a779d6220 667 */
shoaib_ahmed 0:791a779d6220 668 if (temp < 0) {
shoaib_ahmed 0:791a779d6220 669 temp = -temp; /* temp is abs value of input */
shoaib_ahmed 0:791a779d6220 670 temp >>= Al; /* apply the point transform */
shoaib_ahmed 0:791a779d6220 671 /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
shoaib_ahmed 0:791a779d6220 672 temp2 = ~temp;
shoaib_ahmed 0:791a779d6220 673 } else {
shoaib_ahmed 0:791a779d6220 674 temp >>= Al; /* apply the point transform */
shoaib_ahmed 0:791a779d6220 675 temp2 = temp;
shoaib_ahmed 0:791a779d6220 676 }
shoaib_ahmed 0:791a779d6220 677 /* Watch out for case that nonzero coef is zero after point transform */
shoaib_ahmed 0:791a779d6220 678 if (temp == 0) {
shoaib_ahmed 0:791a779d6220 679 r++;
shoaib_ahmed 0:791a779d6220 680 continue;
shoaib_ahmed 0:791a779d6220 681 }
shoaib_ahmed 0:791a779d6220 682
shoaib_ahmed 0:791a779d6220 683 /* Emit any pending EOBRUN */
shoaib_ahmed 0:791a779d6220 684 if (entropy->EOBRUN > 0)
shoaib_ahmed 0:791a779d6220 685 emit_eobrun(entropy);
shoaib_ahmed 0:791a779d6220 686 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
shoaib_ahmed 0:791a779d6220 687 while (r > 15) {
shoaib_ahmed 0:791a779d6220 688 emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
shoaib_ahmed 0:791a779d6220 689 r -= 16;
shoaib_ahmed 0:791a779d6220 690 }
shoaib_ahmed 0:791a779d6220 691
shoaib_ahmed 0:791a779d6220 692 /* Find the number of bits needed for the magnitude of the coefficient */
shoaib_ahmed 0:791a779d6220 693 nbits = 1; /* there must be at least one 1 bit */
shoaib_ahmed 0:791a779d6220 694 while ((temp >>= 1))
shoaib_ahmed 0:791a779d6220 695 nbits++;
shoaib_ahmed 0:791a779d6220 696 /* Check for out-of-range coefficient values */
shoaib_ahmed 0:791a779d6220 697 if (nbits > MAX_COEF_BITS)
shoaib_ahmed 0:791a779d6220 698 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
shoaib_ahmed 0:791a779d6220 699
shoaib_ahmed 0:791a779d6220 700 /* Count/emit Huffman symbol for run length / number of bits */
shoaib_ahmed 0:791a779d6220 701 emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
shoaib_ahmed 0:791a779d6220 702
shoaib_ahmed 0:791a779d6220 703 /* Emit that number of bits of the value, if positive, */
shoaib_ahmed 0:791a779d6220 704 /* or the complement of its magnitude, if negative. */
shoaib_ahmed 0:791a779d6220 705 emit_bits_e(entropy, (unsigned int) temp2, nbits);
shoaib_ahmed 0:791a779d6220 706
shoaib_ahmed 0:791a779d6220 707 r = 0; /* reset zero run length */
shoaib_ahmed 0:791a779d6220 708 }
shoaib_ahmed 0:791a779d6220 709
shoaib_ahmed 0:791a779d6220 710 if (r > 0) { /* If there are trailing zeroes, */
shoaib_ahmed 0:791a779d6220 711 entropy->EOBRUN++; /* count an EOB */
shoaib_ahmed 0:791a779d6220 712 if (entropy->EOBRUN == 0x7FFF)
shoaib_ahmed 0:791a779d6220 713 emit_eobrun(entropy); /* force it out to avoid overflow */
shoaib_ahmed 0:791a779d6220 714 }
shoaib_ahmed 0:791a779d6220 715
shoaib_ahmed 0:791a779d6220 716 cinfo->dest->next_output_byte = entropy->next_output_byte;
shoaib_ahmed 0:791a779d6220 717 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
shoaib_ahmed 0:791a779d6220 718
shoaib_ahmed 0:791a779d6220 719 /* Update restart-interval state too */
shoaib_ahmed 0:791a779d6220 720 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 721 if (entropy->restarts_to_go == 0) {
shoaib_ahmed 0:791a779d6220 722 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 723 entropy->next_restart_num++;
shoaib_ahmed 0:791a779d6220 724 entropy->next_restart_num &= 7;
shoaib_ahmed 0:791a779d6220 725 }
shoaib_ahmed 0:791a779d6220 726 entropy->restarts_to_go--;
shoaib_ahmed 0:791a779d6220 727 }
shoaib_ahmed 0:791a779d6220 728
shoaib_ahmed 0:791a779d6220 729 return TRUE;
shoaib_ahmed 0:791a779d6220 730 }
shoaib_ahmed 0:791a779d6220 731
shoaib_ahmed 0:791a779d6220 732
shoaib_ahmed 0:791a779d6220 733 /*
shoaib_ahmed 0:791a779d6220 734 * MCU encoding for DC successive approximation refinement scan.
shoaib_ahmed 0:791a779d6220 735 * Note: we assume such scans can be multi-component,
shoaib_ahmed 0:791a779d6220 736 * although the spec is not very clear on the point.
shoaib_ahmed 0:791a779d6220 737 */
shoaib_ahmed 0:791a779d6220 738
shoaib_ahmed 0:791a779d6220 739 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 740 encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shoaib_ahmed 0:791a779d6220 741 {
shoaib_ahmed 0:791a779d6220 742 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 743 int Al, blkn;
shoaib_ahmed 0:791a779d6220 744
shoaib_ahmed 0:791a779d6220 745 entropy->next_output_byte = cinfo->dest->next_output_byte;
shoaib_ahmed 0:791a779d6220 746 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
shoaib_ahmed 0:791a779d6220 747
shoaib_ahmed 0:791a779d6220 748 /* Emit restart marker if needed */
shoaib_ahmed 0:791a779d6220 749 if (cinfo->restart_interval)
shoaib_ahmed 0:791a779d6220 750 if (entropy->restarts_to_go == 0)
shoaib_ahmed 0:791a779d6220 751 emit_restart_e(entropy, entropy->next_restart_num);
shoaib_ahmed 0:791a779d6220 752
shoaib_ahmed 0:791a779d6220 753 Al = cinfo->Al;
shoaib_ahmed 0:791a779d6220 754
shoaib_ahmed 0:791a779d6220 755 /* Encode the MCU data blocks */
shoaib_ahmed 0:791a779d6220 756 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
shoaib_ahmed 0:791a779d6220 757 /* We simply emit the Al'th bit of the DC coefficient value. */
shoaib_ahmed 0:791a779d6220 758 emit_bits_e(entropy, (unsigned int) (MCU_data[blkn][0][0] >> Al), 1);
shoaib_ahmed 0:791a779d6220 759 }
shoaib_ahmed 0:791a779d6220 760
shoaib_ahmed 0:791a779d6220 761 cinfo->dest->next_output_byte = entropy->next_output_byte;
shoaib_ahmed 0:791a779d6220 762 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
shoaib_ahmed 0:791a779d6220 763
shoaib_ahmed 0:791a779d6220 764 /* Update restart-interval state too */
shoaib_ahmed 0:791a779d6220 765 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 766 if (entropy->restarts_to_go == 0) {
shoaib_ahmed 0:791a779d6220 767 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 768 entropy->next_restart_num++;
shoaib_ahmed 0:791a779d6220 769 entropy->next_restart_num &= 7;
shoaib_ahmed 0:791a779d6220 770 }
shoaib_ahmed 0:791a779d6220 771 entropy->restarts_to_go--;
shoaib_ahmed 0:791a779d6220 772 }
shoaib_ahmed 0:791a779d6220 773
shoaib_ahmed 0:791a779d6220 774 return TRUE;
shoaib_ahmed 0:791a779d6220 775 }
shoaib_ahmed 0:791a779d6220 776
shoaib_ahmed 0:791a779d6220 777
shoaib_ahmed 0:791a779d6220 778 /*
shoaib_ahmed 0:791a779d6220 779 * MCU encoding for AC successive approximation refinement scan.
shoaib_ahmed 0:791a779d6220 780 */
shoaib_ahmed 0:791a779d6220 781
shoaib_ahmed 0:791a779d6220 782 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 783 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shoaib_ahmed 0:791a779d6220 784 {
shoaib_ahmed 0:791a779d6220 785 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 786 const int * natural_order;
shoaib_ahmed 0:791a779d6220 787 JBLOCKROW block;
shoaib_ahmed 0:791a779d6220 788 register int temp;
shoaib_ahmed 0:791a779d6220 789 register int r, k;
shoaib_ahmed 0:791a779d6220 790 int Se, Al;
shoaib_ahmed 0:791a779d6220 791 int EOB;
shoaib_ahmed 0:791a779d6220 792 char *BR_buffer;
shoaib_ahmed 0:791a779d6220 793 unsigned int BR;
shoaib_ahmed 0:791a779d6220 794 int absvalues[DCTSIZE2];
shoaib_ahmed 0:791a779d6220 795
shoaib_ahmed 0:791a779d6220 796 entropy->next_output_byte = cinfo->dest->next_output_byte;
shoaib_ahmed 0:791a779d6220 797 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
shoaib_ahmed 0:791a779d6220 798
shoaib_ahmed 0:791a779d6220 799 /* Emit restart marker if needed */
shoaib_ahmed 0:791a779d6220 800 if (cinfo->restart_interval)
shoaib_ahmed 0:791a779d6220 801 if (entropy->restarts_to_go == 0)
shoaib_ahmed 0:791a779d6220 802 emit_restart_e(entropy, entropy->next_restart_num);
shoaib_ahmed 0:791a779d6220 803
shoaib_ahmed 0:791a779d6220 804 Se = cinfo->Se;
shoaib_ahmed 0:791a779d6220 805 Al = cinfo->Al;
shoaib_ahmed 0:791a779d6220 806 natural_order = cinfo->natural_order;
shoaib_ahmed 0:791a779d6220 807
shoaib_ahmed 0:791a779d6220 808 /* Encode the MCU data block */
shoaib_ahmed 0:791a779d6220 809 block = MCU_data[0];
shoaib_ahmed 0:791a779d6220 810
shoaib_ahmed 0:791a779d6220 811 /* It is convenient to make a pre-pass to determine the transformed
shoaib_ahmed 0:791a779d6220 812 * coefficients' absolute values and the EOB position.
shoaib_ahmed 0:791a779d6220 813 */
shoaib_ahmed 0:791a779d6220 814 EOB = 0;
shoaib_ahmed 0:791a779d6220 815 for (k = cinfo->Ss; k <= Se; k++) {
shoaib_ahmed 0:791a779d6220 816 temp = (*block)[natural_order[k]];
shoaib_ahmed 0:791a779d6220 817 /* We must apply the point transform by Al. For AC coefficients this
shoaib_ahmed 0:791a779d6220 818 * is an integer division with rounding towards 0. To do this portably
shoaib_ahmed 0:791a779d6220 819 * in C, we shift after obtaining the absolute value.
shoaib_ahmed 0:791a779d6220 820 */
shoaib_ahmed 0:791a779d6220 821 if (temp < 0)
shoaib_ahmed 0:791a779d6220 822 temp = -temp; /* temp is abs value of input */
shoaib_ahmed 0:791a779d6220 823 temp >>= Al; /* apply the point transform */
shoaib_ahmed 0:791a779d6220 824 absvalues[k] = temp; /* save abs value for main pass */
shoaib_ahmed 0:791a779d6220 825 if (temp == 1)
shoaib_ahmed 0:791a779d6220 826 EOB = k; /* EOB = index of last newly-nonzero coef */
shoaib_ahmed 0:791a779d6220 827 }
shoaib_ahmed 0:791a779d6220 828
shoaib_ahmed 0:791a779d6220 829 /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
shoaib_ahmed 0:791a779d6220 830
shoaib_ahmed 0:791a779d6220 831 r = 0; /* r = run length of zeros */
shoaib_ahmed 0:791a779d6220 832 BR = 0; /* BR = count of buffered bits added now */
shoaib_ahmed 0:791a779d6220 833 BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
shoaib_ahmed 0:791a779d6220 834
shoaib_ahmed 0:791a779d6220 835 for (k = cinfo->Ss; k <= Se; k++) {
shoaib_ahmed 0:791a779d6220 836 if ((temp = absvalues[k]) == 0) {
shoaib_ahmed 0:791a779d6220 837 r++;
shoaib_ahmed 0:791a779d6220 838 continue;
shoaib_ahmed 0:791a779d6220 839 }
shoaib_ahmed 0:791a779d6220 840
shoaib_ahmed 0:791a779d6220 841 /* Emit any required ZRLs, but not if they can be folded into EOB */
shoaib_ahmed 0:791a779d6220 842 while (r > 15 && k <= EOB) {
shoaib_ahmed 0:791a779d6220 843 /* emit any pending EOBRUN and the BE correction bits */
shoaib_ahmed 0:791a779d6220 844 emit_eobrun(entropy);
shoaib_ahmed 0:791a779d6220 845 /* Emit ZRL */
shoaib_ahmed 0:791a779d6220 846 emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
shoaib_ahmed 0:791a779d6220 847 r -= 16;
shoaib_ahmed 0:791a779d6220 848 /* Emit buffered correction bits that must be associated with ZRL */
shoaib_ahmed 0:791a779d6220 849 emit_buffered_bits(entropy, BR_buffer, BR);
shoaib_ahmed 0:791a779d6220 850 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
shoaib_ahmed 0:791a779d6220 851 BR = 0;
shoaib_ahmed 0:791a779d6220 852 }
shoaib_ahmed 0:791a779d6220 853
shoaib_ahmed 0:791a779d6220 854 /* If the coef was previously nonzero, it only needs a correction bit.
shoaib_ahmed 0:791a779d6220 855 * NOTE: a straight translation of the spec's figure G.7 would suggest
shoaib_ahmed 0:791a779d6220 856 * that we also need to test r > 15. But if r > 15, we can only get here
shoaib_ahmed 0:791a779d6220 857 * if k > EOB, which implies that this coefficient is not 1.
shoaib_ahmed 0:791a779d6220 858 */
shoaib_ahmed 0:791a779d6220 859 if (temp > 1) {
shoaib_ahmed 0:791a779d6220 860 /* The correction bit is the next bit of the absolute value. */
shoaib_ahmed 0:791a779d6220 861 BR_buffer[BR++] = (char) (temp & 1);
shoaib_ahmed 0:791a779d6220 862 continue;
shoaib_ahmed 0:791a779d6220 863 }
shoaib_ahmed 0:791a779d6220 864
shoaib_ahmed 0:791a779d6220 865 /* Emit any pending EOBRUN and the BE correction bits */
shoaib_ahmed 0:791a779d6220 866 emit_eobrun(entropy);
shoaib_ahmed 0:791a779d6220 867
shoaib_ahmed 0:791a779d6220 868 /* Count/emit Huffman symbol for run length / number of bits */
shoaib_ahmed 0:791a779d6220 869 emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
shoaib_ahmed 0:791a779d6220 870
shoaib_ahmed 0:791a779d6220 871 /* Emit output bit for newly-nonzero coef */
shoaib_ahmed 0:791a779d6220 872 temp = ((*block)[natural_order[k]] < 0) ? 0 : 1;
shoaib_ahmed 0:791a779d6220 873 emit_bits_e(entropy, (unsigned int) temp, 1);
shoaib_ahmed 0:791a779d6220 874
shoaib_ahmed 0:791a779d6220 875 /* Emit buffered correction bits that must be associated with this code */
shoaib_ahmed 0:791a779d6220 876 emit_buffered_bits(entropy, BR_buffer, BR);
shoaib_ahmed 0:791a779d6220 877 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
shoaib_ahmed 0:791a779d6220 878 BR = 0;
shoaib_ahmed 0:791a779d6220 879 r = 0; /* reset zero run length */
shoaib_ahmed 0:791a779d6220 880 }
shoaib_ahmed 0:791a779d6220 881
shoaib_ahmed 0:791a779d6220 882 if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
shoaib_ahmed 0:791a779d6220 883 entropy->EOBRUN++; /* count an EOB */
shoaib_ahmed 0:791a779d6220 884 entropy->BE += BR; /* concat my correction bits to older ones */
shoaib_ahmed 0:791a779d6220 885 /* We force out the EOB if we risk either:
shoaib_ahmed 0:791a779d6220 886 * 1. overflow of the EOB counter;
shoaib_ahmed 0:791a779d6220 887 * 2. overflow of the correction bit buffer during the next MCU.
shoaib_ahmed 0:791a779d6220 888 */
shoaib_ahmed 0:791a779d6220 889 if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
shoaib_ahmed 0:791a779d6220 890 emit_eobrun(entropy);
shoaib_ahmed 0:791a779d6220 891 }
shoaib_ahmed 0:791a779d6220 892
shoaib_ahmed 0:791a779d6220 893 cinfo->dest->next_output_byte = entropy->next_output_byte;
shoaib_ahmed 0:791a779d6220 894 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
shoaib_ahmed 0:791a779d6220 895
shoaib_ahmed 0:791a779d6220 896 /* Update restart-interval state too */
shoaib_ahmed 0:791a779d6220 897 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 898 if (entropy->restarts_to_go == 0) {
shoaib_ahmed 0:791a779d6220 899 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 900 entropy->next_restart_num++;
shoaib_ahmed 0:791a779d6220 901 entropy->next_restart_num &= 7;
shoaib_ahmed 0:791a779d6220 902 }
shoaib_ahmed 0:791a779d6220 903 entropy->restarts_to_go--;
shoaib_ahmed 0:791a779d6220 904 }
shoaib_ahmed 0:791a779d6220 905
shoaib_ahmed 0:791a779d6220 906 return TRUE;
shoaib_ahmed 0:791a779d6220 907 }
shoaib_ahmed 0:791a779d6220 908
shoaib_ahmed 0:791a779d6220 909
shoaib_ahmed 0:791a779d6220 910 /* Encode a single block's worth of coefficients */
shoaib_ahmed 0:791a779d6220 911
shoaib_ahmed 0:791a779d6220 912 LOCAL(boolean)
shoaib_ahmed 0:791a779d6220 913 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
shoaib_ahmed 0:791a779d6220 914 c_derived_tbl *dctbl, c_derived_tbl *actbl)
shoaib_ahmed 0:791a779d6220 915 {
shoaib_ahmed 0:791a779d6220 916 register int temp, temp2;
shoaib_ahmed 0:791a779d6220 917 register int nbits;
shoaib_ahmed 0:791a779d6220 918 register int r, k;
shoaib_ahmed 0:791a779d6220 919 int Se = state->cinfo->lim_Se;
shoaib_ahmed 0:791a779d6220 920 const int * natural_order = state->cinfo->natural_order;
shoaib_ahmed 0:791a779d6220 921
shoaib_ahmed 0:791a779d6220 922 /* Encode the DC coefficient difference per section F.1.2.1 */
shoaib_ahmed 0:791a779d6220 923
shoaib_ahmed 0:791a779d6220 924 temp = temp2 = block[0] - last_dc_val;
shoaib_ahmed 0:791a779d6220 925
shoaib_ahmed 0:791a779d6220 926 if (temp < 0) {
shoaib_ahmed 0:791a779d6220 927 temp = -temp; /* temp is abs value of input */
shoaib_ahmed 0:791a779d6220 928 /* For a negative input, want temp2 = bitwise complement of abs(input) */
shoaib_ahmed 0:791a779d6220 929 /* This code assumes we are on a two's complement machine */
shoaib_ahmed 0:791a779d6220 930 temp2--;
shoaib_ahmed 0:791a779d6220 931 }
shoaib_ahmed 0:791a779d6220 932
shoaib_ahmed 0:791a779d6220 933 /* Find the number of bits needed for the magnitude of the coefficient */
shoaib_ahmed 0:791a779d6220 934 nbits = 0;
shoaib_ahmed 0:791a779d6220 935 while (temp) {
shoaib_ahmed 0:791a779d6220 936 nbits++;
shoaib_ahmed 0:791a779d6220 937 temp >>= 1;
shoaib_ahmed 0:791a779d6220 938 }
shoaib_ahmed 0:791a779d6220 939 /* Check for out-of-range coefficient values.
shoaib_ahmed 0:791a779d6220 940 * Since we're encoding a difference, the range limit is twice as much.
shoaib_ahmed 0:791a779d6220 941 */
shoaib_ahmed 0:791a779d6220 942 if (nbits > MAX_COEF_BITS+1)
shoaib_ahmed 0:791a779d6220 943 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
shoaib_ahmed 0:791a779d6220 944
shoaib_ahmed 0:791a779d6220 945 /* Emit the Huffman-coded symbol for the number of bits */
shoaib_ahmed 0:791a779d6220 946 if (! emit_bits_s(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
shoaib_ahmed 0:791a779d6220 947 return FALSE;
shoaib_ahmed 0:791a779d6220 948
shoaib_ahmed 0:791a779d6220 949 /* Emit that number of bits of the value, if positive, */
shoaib_ahmed 0:791a779d6220 950 /* or the complement of its magnitude, if negative. */
shoaib_ahmed 0:791a779d6220 951 if (nbits) /* emit_bits rejects calls with size 0 */
shoaib_ahmed 0:791a779d6220 952 if (! emit_bits_s(state, (unsigned int) temp2, nbits))
shoaib_ahmed 0:791a779d6220 953 return FALSE;
shoaib_ahmed 0:791a779d6220 954
shoaib_ahmed 0:791a779d6220 955 /* Encode the AC coefficients per section F.1.2.2 */
shoaib_ahmed 0:791a779d6220 956
shoaib_ahmed 0:791a779d6220 957 r = 0; /* r = run length of zeros */
shoaib_ahmed 0:791a779d6220 958
shoaib_ahmed 0:791a779d6220 959 for (k = 1; k <= Se; k++) {
shoaib_ahmed 0:791a779d6220 960 if ((temp2 = block[natural_order[k]]) == 0) {
shoaib_ahmed 0:791a779d6220 961 r++;
shoaib_ahmed 0:791a779d6220 962 } else {
shoaib_ahmed 0:791a779d6220 963 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
shoaib_ahmed 0:791a779d6220 964 while (r > 15) {
shoaib_ahmed 0:791a779d6220 965 if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
shoaib_ahmed 0:791a779d6220 966 return FALSE;
shoaib_ahmed 0:791a779d6220 967 r -= 16;
shoaib_ahmed 0:791a779d6220 968 }
shoaib_ahmed 0:791a779d6220 969
shoaib_ahmed 0:791a779d6220 970 temp = temp2;
shoaib_ahmed 0:791a779d6220 971 if (temp < 0) {
shoaib_ahmed 0:791a779d6220 972 temp = -temp; /* temp is abs value of input */
shoaib_ahmed 0:791a779d6220 973 /* This code assumes we are on a two's complement machine */
shoaib_ahmed 0:791a779d6220 974 temp2--;
shoaib_ahmed 0:791a779d6220 975 }
shoaib_ahmed 0:791a779d6220 976
shoaib_ahmed 0:791a779d6220 977 /* Find the number of bits needed for the magnitude of the coefficient */
shoaib_ahmed 0:791a779d6220 978 nbits = 1; /* there must be at least one 1 bit */
shoaib_ahmed 0:791a779d6220 979 while ((temp >>= 1))
shoaib_ahmed 0:791a779d6220 980 nbits++;
shoaib_ahmed 0:791a779d6220 981 /* Check for out-of-range coefficient values */
shoaib_ahmed 0:791a779d6220 982 if (nbits > MAX_COEF_BITS)
shoaib_ahmed 0:791a779d6220 983 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
shoaib_ahmed 0:791a779d6220 984
shoaib_ahmed 0:791a779d6220 985 /* Emit Huffman symbol for run length / number of bits */
shoaib_ahmed 0:791a779d6220 986 temp = (r << 4) + nbits;
shoaib_ahmed 0:791a779d6220 987 if (! emit_bits_s(state, actbl->ehufco[temp], actbl->ehufsi[temp]))
shoaib_ahmed 0:791a779d6220 988 return FALSE;
shoaib_ahmed 0:791a779d6220 989
shoaib_ahmed 0:791a779d6220 990 /* Emit that number of bits of the value, if positive, */
shoaib_ahmed 0:791a779d6220 991 /* or the complement of its magnitude, if negative. */
shoaib_ahmed 0:791a779d6220 992 if (! emit_bits_s(state, (unsigned int) temp2, nbits))
shoaib_ahmed 0:791a779d6220 993 return FALSE;
shoaib_ahmed 0:791a779d6220 994
shoaib_ahmed 0:791a779d6220 995 r = 0;
shoaib_ahmed 0:791a779d6220 996 }
shoaib_ahmed 0:791a779d6220 997 }
shoaib_ahmed 0:791a779d6220 998
shoaib_ahmed 0:791a779d6220 999 /* If the last coef(s) were zero, emit an end-of-block code */
shoaib_ahmed 0:791a779d6220 1000 if (r > 0)
shoaib_ahmed 0:791a779d6220 1001 if (! emit_bits_s(state, actbl->ehufco[0], actbl->ehufsi[0]))
shoaib_ahmed 0:791a779d6220 1002 return FALSE;
shoaib_ahmed 0:791a779d6220 1003
shoaib_ahmed 0:791a779d6220 1004 return TRUE;
shoaib_ahmed 0:791a779d6220 1005 }
shoaib_ahmed 0:791a779d6220 1006
shoaib_ahmed 0:791a779d6220 1007
shoaib_ahmed 0:791a779d6220 1008 /*
shoaib_ahmed 0:791a779d6220 1009 * Encode and output one MCU's worth of Huffman-compressed coefficients.
shoaib_ahmed 0:791a779d6220 1010 */
shoaib_ahmed 0:791a779d6220 1011
shoaib_ahmed 0:791a779d6220 1012 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 1013 encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shoaib_ahmed 0:791a779d6220 1014 {
shoaib_ahmed 0:791a779d6220 1015 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 1016 working_state state;
shoaib_ahmed 0:791a779d6220 1017 int blkn, ci;
shoaib_ahmed 0:791a779d6220 1018 jpeg_component_info * compptr;
shoaib_ahmed 0:791a779d6220 1019
shoaib_ahmed 0:791a779d6220 1020 /* Load up working state */
shoaib_ahmed 0:791a779d6220 1021 state.next_output_byte = cinfo->dest->next_output_byte;
shoaib_ahmed 0:791a779d6220 1022 state.free_in_buffer = cinfo->dest->free_in_buffer;
shoaib_ahmed 0:791a779d6220 1023 ASSIGN_STATE(state.cur, entropy->saved);
shoaib_ahmed 0:791a779d6220 1024 state.cinfo = cinfo;
shoaib_ahmed 0:791a779d6220 1025
shoaib_ahmed 0:791a779d6220 1026 /* Emit restart marker if needed */
shoaib_ahmed 0:791a779d6220 1027 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 1028 if (entropy->restarts_to_go == 0)
shoaib_ahmed 0:791a779d6220 1029 if (! emit_restart_s(&state, entropy->next_restart_num))
shoaib_ahmed 0:791a779d6220 1030 return FALSE;
shoaib_ahmed 0:791a779d6220 1031 }
shoaib_ahmed 0:791a779d6220 1032
shoaib_ahmed 0:791a779d6220 1033 /* Encode the MCU data blocks */
shoaib_ahmed 0:791a779d6220 1034 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
shoaib_ahmed 0:791a779d6220 1035 ci = cinfo->MCU_membership[blkn];
shoaib_ahmed 0:791a779d6220 1036 compptr = cinfo->cur_comp_info[ci];
shoaib_ahmed 0:791a779d6220 1037 if (! encode_one_block(&state,
shoaib_ahmed 0:791a779d6220 1038 MCU_data[blkn][0], state.cur.last_dc_val[ci],
shoaib_ahmed 0:791a779d6220 1039 entropy->dc_derived_tbls[compptr->dc_tbl_no],
shoaib_ahmed 0:791a779d6220 1040 entropy->ac_derived_tbls[compptr->ac_tbl_no]))
shoaib_ahmed 0:791a779d6220 1041 return FALSE;
shoaib_ahmed 0:791a779d6220 1042 /* Update last_dc_val */
shoaib_ahmed 0:791a779d6220 1043 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
shoaib_ahmed 0:791a779d6220 1044 }
shoaib_ahmed 0:791a779d6220 1045
shoaib_ahmed 0:791a779d6220 1046 /* Completed MCU, so update state */
shoaib_ahmed 0:791a779d6220 1047 cinfo->dest->next_output_byte = state.next_output_byte;
shoaib_ahmed 0:791a779d6220 1048 cinfo->dest->free_in_buffer = state.free_in_buffer;
shoaib_ahmed 0:791a779d6220 1049 ASSIGN_STATE(entropy->saved, state.cur);
shoaib_ahmed 0:791a779d6220 1050
shoaib_ahmed 0:791a779d6220 1051 /* Update restart-interval state too */
shoaib_ahmed 0:791a779d6220 1052 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 1053 if (entropy->restarts_to_go == 0) {
shoaib_ahmed 0:791a779d6220 1054 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 1055 entropy->next_restart_num++;
shoaib_ahmed 0:791a779d6220 1056 entropy->next_restart_num &= 7;
shoaib_ahmed 0:791a779d6220 1057 }
shoaib_ahmed 0:791a779d6220 1058 entropy->restarts_to_go--;
shoaib_ahmed 0:791a779d6220 1059 }
shoaib_ahmed 0:791a779d6220 1060
shoaib_ahmed 0:791a779d6220 1061 return TRUE;
shoaib_ahmed 0:791a779d6220 1062 }
shoaib_ahmed 0:791a779d6220 1063
shoaib_ahmed 0:791a779d6220 1064
shoaib_ahmed 0:791a779d6220 1065 /*
shoaib_ahmed 0:791a779d6220 1066 * Finish up at the end of a Huffman-compressed scan.
shoaib_ahmed 0:791a779d6220 1067 */
shoaib_ahmed 0:791a779d6220 1068
shoaib_ahmed 0:791a779d6220 1069 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 1070 finish_pass_huff (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 1071 {
shoaib_ahmed 0:791a779d6220 1072 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 1073 working_state state;
shoaib_ahmed 0:791a779d6220 1074
shoaib_ahmed 0:791a779d6220 1075 if (cinfo->progressive_mode) {
shoaib_ahmed 0:791a779d6220 1076 entropy->next_output_byte = cinfo->dest->next_output_byte;
shoaib_ahmed 0:791a779d6220 1077 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
shoaib_ahmed 0:791a779d6220 1078
shoaib_ahmed 0:791a779d6220 1079 /* Flush out any buffered data */
shoaib_ahmed 0:791a779d6220 1080 emit_eobrun(entropy);
shoaib_ahmed 0:791a779d6220 1081 flush_bits_e(entropy);
shoaib_ahmed 0:791a779d6220 1082
shoaib_ahmed 0:791a779d6220 1083 cinfo->dest->next_output_byte = entropy->next_output_byte;
shoaib_ahmed 0:791a779d6220 1084 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
shoaib_ahmed 0:791a779d6220 1085 } else {
shoaib_ahmed 0:791a779d6220 1086 /* Load up working state ... flush_bits needs it */
shoaib_ahmed 0:791a779d6220 1087 state.next_output_byte = cinfo->dest->next_output_byte;
shoaib_ahmed 0:791a779d6220 1088 state.free_in_buffer = cinfo->dest->free_in_buffer;
shoaib_ahmed 0:791a779d6220 1089 ASSIGN_STATE(state.cur, entropy->saved);
shoaib_ahmed 0:791a779d6220 1090 state.cinfo = cinfo;
shoaib_ahmed 0:791a779d6220 1091
shoaib_ahmed 0:791a779d6220 1092 /* Flush out the last data */
shoaib_ahmed 0:791a779d6220 1093 if (! flush_bits_s(&state))
shoaib_ahmed 0:791a779d6220 1094 ERREXIT(cinfo, JERR_CANT_SUSPEND);
shoaib_ahmed 0:791a779d6220 1095
shoaib_ahmed 0:791a779d6220 1096 /* Update state */
shoaib_ahmed 0:791a779d6220 1097 cinfo->dest->next_output_byte = state.next_output_byte;
shoaib_ahmed 0:791a779d6220 1098 cinfo->dest->free_in_buffer = state.free_in_buffer;
shoaib_ahmed 0:791a779d6220 1099 ASSIGN_STATE(entropy->saved, state.cur);
shoaib_ahmed 0:791a779d6220 1100 }
shoaib_ahmed 0:791a779d6220 1101 }
shoaib_ahmed 0:791a779d6220 1102
shoaib_ahmed 0:791a779d6220 1103
shoaib_ahmed 0:791a779d6220 1104 /*
shoaib_ahmed 0:791a779d6220 1105 * Huffman coding optimization.
shoaib_ahmed 0:791a779d6220 1106 *
shoaib_ahmed 0:791a779d6220 1107 * We first scan the supplied data and count the number of uses of each symbol
shoaib_ahmed 0:791a779d6220 1108 * that is to be Huffman-coded. (This process MUST agree with the code above.)
shoaib_ahmed 0:791a779d6220 1109 * Then we build a Huffman coding tree for the observed counts.
shoaib_ahmed 0:791a779d6220 1110 * Symbols which are not needed at all for the particular image are not
shoaib_ahmed 0:791a779d6220 1111 * assigned any code, which saves space in the DHT marker as well as in
shoaib_ahmed 0:791a779d6220 1112 * the compressed data.
shoaib_ahmed 0:791a779d6220 1113 */
shoaib_ahmed 0:791a779d6220 1114
shoaib_ahmed 0:791a779d6220 1115
shoaib_ahmed 0:791a779d6220 1116 /* Process a single block's worth of coefficients */
shoaib_ahmed 0:791a779d6220 1117
shoaib_ahmed 0:791a779d6220 1118 LOCAL(void)
shoaib_ahmed 0:791a779d6220 1119 htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
shoaib_ahmed 0:791a779d6220 1120 long dc_counts[], long ac_counts[])
shoaib_ahmed 0:791a779d6220 1121 {
shoaib_ahmed 0:791a779d6220 1122 register int temp;
shoaib_ahmed 0:791a779d6220 1123 register int nbits;
shoaib_ahmed 0:791a779d6220 1124 register int r, k;
shoaib_ahmed 0:791a779d6220 1125 int Se = cinfo->lim_Se;
shoaib_ahmed 0:791a779d6220 1126 const int * natural_order = cinfo->natural_order;
shoaib_ahmed 0:791a779d6220 1127
shoaib_ahmed 0:791a779d6220 1128 /* Encode the DC coefficient difference per section F.1.2.1 */
shoaib_ahmed 0:791a779d6220 1129
shoaib_ahmed 0:791a779d6220 1130 temp = block[0] - last_dc_val;
shoaib_ahmed 0:791a779d6220 1131 if (temp < 0)
shoaib_ahmed 0:791a779d6220 1132 temp = -temp;
shoaib_ahmed 0:791a779d6220 1133
shoaib_ahmed 0:791a779d6220 1134 /* Find the number of bits needed for the magnitude of the coefficient */
shoaib_ahmed 0:791a779d6220 1135 nbits = 0;
shoaib_ahmed 0:791a779d6220 1136 while (temp) {
shoaib_ahmed 0:791a779d6220 1137 nbits++;
shoaib_ahmed 0:791a779d6220 1138 temp >>= 1;
shoaib_ahmed 0:791a779d6220 1139 }
shoaib_ahmed 0:791a779d6220 1140 /* Check for out-of-range coefficient values.
shoaib_ahmed 0:791a779d6220 1141 * Since we're encoding a difference, the range limit is twice as much.
shoaib_ahmed 0:791a779d6220 1142 */
shoaib_ahmed 0:791a779d6220 1143 if (nbits > MAX_COEF_BITS+1)
shoaib_ahmed 0:791a779d6220 1144 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
shoaib_ahmed 0:791a779d6220 1145
shoaib_ahmed 0:791a779d6220 1146 /* Count the Huffman symbol for the number of bits */
shoaib_ahmed 0:791a779d6220 1147 dc_counts[nbits]++;
shoaib_ahmed 0:791a779d6220 1148
shoaib_ahmed 0:791a779d6220 1149 /* Encode the AC coefficients per section F.1.2.2 */
shoaib_ahmed 0:791a779d6220 1150
shoaib_ahmed 0:791a779d6220 1151 r = 0; /* r = run length of zeros */
shoaib_ahmed 0:791a779d6220 1152
shoaib_ahmed 0:791a779d6220 1153 for (k = 1; k <= Se; k++) {
shoaib_ahmed 0:791a779d6220 1154 if ((temp = block[natural_order[k]]) == 0) {
shoaib_ahmed 0:791a779d6220 1155 r++;
shoaib_ahmed 0:791a779d6220 1156 } else {
shoaib_ahmed 0:791a779d6220 1157 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
shoaib_ahmed 0:791a779d6220 1158 while (r > 15) {
shoaib_ahmed 0:791a779d6220 1159 ac_counts[0xF0]++;
shoaib_ahmed 0:791a779d6220 1160 r -= 16;
shoaib_ahmed 0:791a779d6220 1161 }
shoaib_ahmed 0:791a779d6220 1162
shoaib_ahmed 0:791a779d6220 1163 /* Find the number of bits needed for the magnitude of the coefficient */
shoaib_ahmed 0:791a779d6220 1164 if (temp < 0)
shoaib_ahmed 0:791a779d6220 1165 temp = -temp;
shoaib_ahmed 0:791a779d6220 1166
shoaib_ahmed 0:791a779d6220 1167 /* Find the number of bits needed for the magnitude of the coefficient */
shoaib_ahmed 0:791a779d6220 1168 nbits = 1; /* there must be at least one 1 bit */
shoaib_ahmed 0:791a779d6220 1169 while ((temp >>= 1))
shoaib_ahmed 0:791a779d6220 1170 nbits++;
shoaib_ahmed 0:791a779d6220 1171 /* Check for out-of-range coefficient values */
shoaib_ahmed 0:791a779d6220 1172 if (nbits > MAX_COEF_BITS)
shoaib_ahmed 0:791a779d6220 1173 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
shoaib_ahmed 0:791a779d6220 1174
shoaib_ahmed 0:791a779d6220 1175 /* Count Huffman symbol for run length / number of bits */
shoaib_ahmed 0:791a779d6220 1176 ac_counts[(r << 4) + nbits]++;
shoaib_ahmed 0:791a779d6220 1177
shoaib_ahmed 0:791a779d6220 1178 r = 0;
shoaib_ahmed 0:791a779d6220 1179 }
shoaib_ahmed 0:791a779d6220 1180 }
shoaib_ahmed 0:791a779d6220 1181
shoaib_ahmed 0:791a779d6220 1182 /* If the last coef(s) were zero, emit an end-of-block code */
shoaib_ahmed 0:791a779d6220 1183 if (r > 0)
shoaib_ahmed 0:791a779d6220 1184 ac_counts[0]++;
shoaib_ahmed 0:791a779d6220 1185 }
shoaib_ahmed 0:791a779d6220 1186
shoaib_ahmed 0:791a779d6220 1187
shoaib_ahmed 0:791a779d6220 1188 /*
shoaib_ahmed 0:791a779d6220 1189 * Trial-encode one MCU's worth of Huffman-compressed coefficients.
shoaib_ahmed 0:791a779d6220 1190 * No data is actually output, so no suspension return is possible.
shoaib_ahmed 0:791a779d6220 1191 */
shoaib_ahmed 0:791a779d6220 1192
shoaib_ahmed 0:791a779d6220 1193 METHODDEF(boolean)
shoaib_ahmed 0:791a779d6220 1194 encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
shoaib_ahmed 0:791a779d6220 1195 {
shoaib_ahmed 0:791a779d6220 1196 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 1197 int blkn, ci;
shoaib_ahmed 0:791a779d6220 1198 jpeg_component_info * compptr;
shoaib_ahmed 0:791a779d6220 1199
shoaib_ahmed 0:791a779d6220 1200 /* Take care of restart intervals if needed */
shoaib_ahmed 0:791a779d6220 1201 if (cinfo->restart_interval) {
shoaib_ahmed 0:791a779d6220 1202 if (entropy->restarts_to_go == 0) {
shoaib_ahmed 0:791a779d6220 1203 /* Re-initialize DC predictions to 0 */
shoaib_ahmed 0:791a779d6220 1204 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
shoaib_ahmed 0:791a779d6220 1205 entropy->saved.last_dc_val[ci] = 0;
shoaib_ahmed 0:791a779d6220 1206 /* Update restart state */
shoaib_ahmed 0:791a779d6220 1207 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 1208 }
shoaib_ahmed 0:791a779d6220 1209 entropy->restarts_to_go--;
shoaib_ahmed 0:791a779d6220 1210 }
shoaib_ahmed 0:791a779d6220 1211
shoaib_ahmed 0:791a779d6220 1212 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
shoaib_ahmed 0:791a779d6220 1213 ci = cinfo->MCU_membership[blkn];
shoaib_ahmed 0:791a779d6220 1214 compptr = cinfo->cur_comp_info[ci];
shoaib_ahmed 0:791a779d6220 1215 htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
shoaib_ahmed 0:791a779d6220 1216 entropy->dc_count_ptrs[compptr->dc_tbl_no],
shoaib_ahmed 0:791a779d6220 1217 entropy->ac_count_ptrs[compptr->ac_tbl_no]);
shoaib_ahmed 0:791a779d6220 1218 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
shoaib_ahmed 0:791a779d6220 1219 }
shoaib_ahmed 0:791a779d6220 1220
shoaib_ahmed 0:791a779d6220 1221 return TRUE;
shoaib_ahmed 0:791a779d6220 1222 }
shoaib_ahmed 0:791a779d6220 1223
shoaib_ahmed 0:791a779d6220 1224
shoaib_ahmed 0:791a779d6220 1225 /*
shoaib_ahmed 0:791a779d6220 1226 * Generate the best Huffman code table for the given counts, fill htbl.
shoaib_ahmed 0:791a779d6220 1227 *
shoaib_ahmed 0:791a779d6220 1228 * The JPEG standard requires that no symbol be assigned a codeword of all
shoaib_ahmed 0:791a779d6220 1229 * one bits (so that padding bits added at the end of a compressed segment
shoaib_ahmed 0:791a779d6220 1230 * can't look like a valid code). Because of the canonical ordering of
shoaib_ahmed 0:791a779d6220 1231 * codewords, this just means that there must be an unused slot in the
shoaib_ahmed 0:791a779d6220 1232 * longest codeword length category. Section K.2 of the JPEG spec suggests
shoaib_ahmed 0:791a779d6220 1233 * reserving such a slot by pretending that symbol 256 is a valid symbol
shoaib_ahmed 0:791a779d6220 1234 * with count 1. In theory that's not optimal; giving it count zero but
shoaib_ahmed 0:791a779d6220 1235 * including it in the symbol set anyway should give a better Huffman code.
shoaib_ahmed 0:791a779d6220 1236 * But the theoretically better code actually seems to come out worse in
shoaib_ahmed 0:791a779d6220 1237 * practice, because it produces more all-ones bytes (which incur stuffed
shoaib_ahmed 0:791a779d6220 1238 * zero bytes in the final file). In any case the difference is tiny.
shoaib_ahmed 0:791a779d6220 1239 *
shoaib_ahmed 0:791a779d6220 1240 * The JPEG standard requires Huffman codes to be no more than 16 bits long.
shoaib_ahmed 0:791a779d6220 1241 * If some symbols have a very small but nonzero probability, the Huffman tree
shoaib_ahmed 0:791a779d6220 1242 * must be adjusted to meet the code length restriction. We currently use
shoaib_ahmed 0:791a779d6220 1243 * the adjustment method suggested in JPEG section K.2. This method is *not*
shoaib_ahmed 0:791a779d6220 1244 * optimal; it may not choose the best possible limited-length code. But
shoaib_ahmed 0:791a779d6220 1245 * typically only very-low-frequency symbols will be given less-than-optimal
shoaib_ahmed 0:791a779d6220 1246 * lengths, so the code is almost optimal. Experimental comparisons against
shoaib_ahmed 0:791a779d6220 1247 * an optimal limited-length-code algorithm indicate that the difference is
shoaib_ahmed 0:791a779d6220 1248 * microscopic --- usually less than a hundredth of a percent of total size.
shoaib_ahmed 0:791a779d6220 1249 * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
shoaib_ahmed 0:791a779d6220 1250 */
shoaib_ahmed 0:791a779d6220 1251
shoaib_ahmed 0:791a779d6220 1252 LOCAL(void)
shoaib_ahmed 0:791a779d6220 1253 jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
shoaib_ahmed 0:791a779d6220 1254 {
shoaib_ahmed 0:791a779d6220 1255 #define MAX_CLEN 32 /* assumed maximum initial code length */
shoaib_ahmed 0:791a779d6220 1256 UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
shoaib_ahmed 0:791a779d6220 1257 int codesize[257]; /* codesize[k] = code length of symbol k */
shoaib_ahmed 0:791a779d6220 1258 int others[257]; /* next symbol in current branch of tree */
shoaib_ahmed 0:791a779d6220 1259 int c1, c2;
shoaib_ahmed 0:791a779d6220 1260 int p, i, j;
shoaib_ahmed 0:791a779d6220 1261 long v;
shoaib_ahmed 0:791a779d6220 1262
shoaib_ahmed 0:791a779d6220 1263 /* This algorithm is explained in section K.2 of the JPEG standard */
shoaib_ahmed 0:791a779d6220 1264
shoaib_ahmed 0:791a779d6220 1265 MEMZERO(bits, SIZEOF(bits));
shoaib_ahmed 0:791a779d6220 1266 MEMZERO(codesize, SIZEOF(codesize));
shoaib_ahmed 0:791a779d6220 1267 for (i = 0; i < 257; i++)
shoaib_ahmed 0:791a779d6220 1268 others[i] = -1; /* init links to empty */
shoaib_ahmed 0:791a779d6220 1269
shoaib_ahmed 0:791a779d6220 1270 freq[256] = 1; /* make sure 256 has a nonzero count */
shoaib_ahmed 0:791a779d6220 1271 /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
shoaib_ahmed 0:791a779d6220 1272 * that no real symbol is given code-value of all ones, because 256
shoaib_ahmed 0:791a779d6220 1273 * will be placed last in the largest codeword category.
shoaib_ahmed 0:791a779d6220 1274 */
shoaib_ahmed 0:791a779d6220 1275
shoaib_ahmed 0:791a779d6220 1276 /* Huffman's basic algorithm to assign optimal code lengths to symbols */
shoaib_ahmed 0:791a779d6220 1277
shoaib_ahmed 0:791a779d6220 1278 for (;;) {
shoaib_ahmed 0:791a779d6220 1279 /* Find the smallest nonzero frequency, set c1 = its symbol */
shoaib_ahmed 0:791a779d6220 1280 /* In case of ties, take the larger symbol number */
shoaib_ahmed 0:791a779d6220 1281 c1 = -1;
shoaib_ahmed 0:791a779d6220 1282 v = 1000000000L;
shoaib_ahmed 0:791a779d6220 1283 for (i = 0; i <= 256; i++) {
shoaib_ahmed 0:791a779d6220 1284 if (freq[i] && freq[i] <= v) {
shoaib_ahmed 0:791a779d6220 1285 v = freq[i];
shoaib_ahmed 0:791a779d6220 1286 c1 = i;
shoaib_ahmed 0:791a779d6220 1287 }
shoaib_ahmed 0:791a779d6220 1288 }
shoaib_ahmed 0:791a779d6220 1289
shoaib_ahmed 0:791a779d6220 1290 /* Find the next smallest nonzero frequency, set c2 = its symbol */
shoaib_ahmed 0:791a779d6220 1291 /* In case of ties, take the larger symbol number */
shoaib_ahmed 0:791a779d6220 1292 c2 = -1;
shoaib_ahmed 0:791a779d6220 1293 v = 1000000000L;
shoaib_ahmed 0:791a779d6220 1294 for (i = 0; i <= 256; i++) {
shoaib_ahmed 0:791a779d6220 1295 if (freq[i] && freq[i] <= v && i != c1) {
shoaib_ahmed 0:791a779d6220 1296 v = freq[i];
shoaib_ahmed 0:791a779d6220 1297 c2 = i;
shoaib_ahmed 0:791a779d6220 1298 }
shoaib_ahmed 0:791a779d6220 1299 }
shoaib_ahmed 0:791a779d6220 1300
shoaib_ahmed 0:791a779d6220 1301 /* Done if we've merged everything into one frequency */
shoaib_ahmed 0:791a779d6220 1302 if (c2 < 0)
shoaib_ahmed 0:791a779d6220 1303 break;
shoaib_ahmed 0:791a779d6220 1304
shoaib_ahmed 0:791a779d6220 1305 /* Else merge the two counts/trees */
shoaib_ahmed 0:791a779d6220 1306 freq[c1] += freq[c2];
shoaib_ahmed 0:791a779d6220 1307 freq[c2] = 0;
shoaib_ahmed 0:791a779d6220 1308
shoaib_ahmed 0:791a779d6220 1309 /* Increment the codesize of everything in c1's tree branch */
shoaib_ahmed 0:791a779d6220 1310 codesize[c1]++;
shoaib_ahmed 0:791a779d6220 1311 while (others[c1] >= 0) {
shoaib_ahmed 0:791a779d6220 1312 c1 = others[c1];
shoaib_ahmed 0:791a779d6220 1313 codesize[c1]++;
shoaib_ahmed 0:791a779d6220 1314 }
shoaib_ahmed 0:791a779d6220 1315
shoaib_ahmed 0:791a779d6220 1316 others[c1] = c2; /* chain c2 onto c1's tree branch */
shoaib_ahmed 0:791a779d6220 1317
shoaib_ahmed 0:791a779d6220 1318 /* Increment the codesize of everything in c2's tree branch */
shoaib_ahmed 0:791a779d6220 1319 codesize[c2]++;
shoaib_ahmed 0:791a779d6220 1320 while (others[c2] >= 0) {
shoaib_ahmed 0:791a779d6220 1321 c2 = others[c2];
shoaib_ahmed 0:791a779d6220 1322 codesize[c2]++;
shoaib_ahmed 0:791a779d6220 1323 }
shoaib_ahmed 0:791a779d6220 1324 }
shoaib_ahmed 0:791a779d6220 1325
shoaib_ahmed 0:791a779d6220 1326 /* Now count the number of symbols of each code length */
shoaib_ahmed 0:791a779d6220 1327 for (i = 0; i <= 256; i++) {
shoaib_ahmed 0:791a779d6220 1328 if (codesize[i]) {
shoaib_ahmed 0:791a779d6220 1329 /* The JPEG standard seems to think that this can't happen, */
shoaib_ahmed 0:791a779d6220 1330 /* but I'm paranoid... */
shoaib_ahmed 0:791a779d6220 1331 if (codesize[i] > MAX_CLEN)
shoaib_ahmed 0:791a779d6220 1332 ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
shoaib_ahmed 0:791a779d6220 1333
shoaib_ahmed 0:791a779d6220 1334 bits[codesize[i]]++;
shoaib_ahmed 0:791a779d6220 1335 }
shoaib_ahmed 0:791a779d6220 1336 }
shoaib_ahmed 0:791a779d6220 1337
shoaib_ahmed 0:791a779d6220 1338 /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
shoaib_ahmed 0:791a779d6220 1339 * Huffman procedure assigned any such lengths, we must adjust the coding.
shoaib_ahmed 0:791a779d6220 1340 * Here is what the JPEG spec says about how this next bit works:
shoaib_ahmed 0:791a779d6220 1341 * Since symbols are paired for the longest Huffman code, the symbols are
shoaib_ahmed 0:791a779d6220 1342 * removed from this length category two at a time. The prefix for the pair
shoaib_ahmed 0:791a779d6220 1343 * (which is one bit shorter) is allocated to one of the pair; then,
shoaib_ahmed 0:791a779d6220 1344 * skipping the BITS entry for that prefix length, a code word from the next
shoaib_ahmed 0:791a779d6220 1345 * shortest nonzero BITS entry is converted into a prefix for two code words
shoaib_ahmed 0:791a779d6220 1346 * one bit longer.
shoaib_ahmed 0:791a779d6220 1347 */
shoaib_ahmed 0:791a779d6220 1348
shoaib_ahmed 0:791a779d6220 1349 for (i = MAX_CLEN; i > 16; i--) {
shoaib_ahmed 0:791a779d6220 1350 while (bits[i] > 0) {
shoaib_ahmed 0:791a779d6220 1351 j = i - 2; /* find length of new prefix to be used */
shoaib_ahmed 0:791a779d6220 1352 while (bits[j] == 0)
shoaib_ahmed 0:791a779d6220 1353 j--;
shoaib_ahmed 0:791a779d6220 1354
shoaib_ahmed 0:791a779d6220 1355 bits[i] -= 2; /* remove two symbols */
shoaib_ahmed 0:791a779d6220 1356 bits[i-1]++; /* one goes in this length */
shoaib_ahmed 0:791a779d6220 1357 bits[j+1] += 2; /* two new symbols in this length */
shoaib_ahmed 0:791a779d6220 1358 bits[j]--; /* symbol of this length is now a prefix */
shoaib_ahmed 0:791a779d6220 1359 }
shoaib_ahmed 0:791a779d6220 1360 }
shoaib_ahmed 0:791a779d6220 1361
shoaib_ahmed 0:791a779d6220 1362 /* Remove the count for the pseudo-symbol 256 from the largest codelength */
shoaib_ahmed 0:791a779d6220 1363 while (bits[i] == 0) /* find largest codelength still in use */
shoaib_ahmed 0:791a779d6220 1364 i--;
shoaib_ahmed 0:791a779d6220 1365 bits[i]--;
shoaib_ahmed 0:791a779d6220 1366
shoaib_ahmed 0:791a779d6220 1367 /* Return final symbol counts (only for lengths 0..16) */
shoaib_ahmed 0:791a779d6220 1368 MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
shoaib_ahmed 0:791a779d6220 1369
shoaib_ahmed 0:791a779d6220 1370 /* Return a list of the symbols sorted by code length */
shoaib_ahmed 0:791a779d6220 1371 /* It's not real clear to me why we don't need to consider the codelength
shoaib_ahmed 0:791a779d6220 1372 * changes made above, but the JPEG spec seems to think this works.
shoaib_ahmed 0:791a779d6220 1373 */
shoaib_ahmed 0:791a779d6220 1374 p = 0;
shoaib_ahmed 0:791a779d6220 1375 for (i = 1; i <= MAX_CLEN; i++) {
shoaib_ahmed 0:791a779d6220 1376 for (j = 0; j <= 255; j++) {
shoaib_ahmed 0:791a779d6220 1377 if (codesize[j] == i) {
shoaib_ahmed 0:791a779d6220 1378 htbl->huffval[p] = (UINT8) j;
shoaib_ahmed 0:791a779d6220 1379 p++;
shoaib_ahmed 0:791a779d6220 1380 }
shoaib_ahmed 0:791a779d6220 1381 }
shoaib_ahmed 0:791a779d6220 1382 }
shoaib_ahmed 0:791a779d6220 1383
shoaib_ahmed 0:791a779d6220 1384 /* Set sent_table FALSE so updated table will be written to JPEG file. */
shoaib_ahmed 0:791a779d6220 1385 htbl->sent_table = FALSE;
shoaib_ahmed 0:791a779d6220 1386 }
shoaib_ahmed 0:791a779d6220 1387
shoaib_ahmed 0:791a779d6220 1388
shoaib_ahmed 0:791a779d6220 1389 /*
shoaib_ahmed 0:791a779d6220 1390 * Finish up a statistics-gathering pass and create the new Huffman tables.
shoaib_ahmed 0:791a779d6220 1391 */
shoaib_ahmed 0:791a779d6220 1392
shoaib_ahmed 0:791a779d6220 1393 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 1394 finish_pass_gather (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 1395 {
shoaib_ahmed 0:791a779d6220 1396 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 1397 int ci, tbl;
shoaib_ahmed 0:791a779d6220 1398 jpeg_component_info * compptr;
shoaib_ahmed 0:791a779d6220 1399 JHUFF_TBL **htblptr;
shoaib_ahmed 0:791a779d6220 1400 boolean did_dc[NUM_HUFF_TBLS];
shoaib_ahmed 0:791a779d6220 1401 boolean did_ac[NUM_HUFF_TBLS];
shoaib_ahmed 0:791a779d6220 1402
shoaib_ahmed 0:791a779d6220 1403 /* It's important not to apply jpeg_gen_optimal_table more than once
shoaib_ahmed 0:791a779d6220 1404 * per table, because it clobbers the input frequency counts!
shoaib_ahmed 0:791a779d6220 1405 */
shoaib_ahmed 0:791a779d6220 1406 if (cinfo->progressive_mode)
shoaib_ahmed 0:791a779d6220 1407 /* Flush out buffered data (all we care about is counting the EOB symbol) */
shoaib_ahmed 0:791a779d6220 1408 emit_eobrun(entropy);
shoaib_ahmed 0:791a779d6220 1409
shoaib_ahmed 0:791a779d6220 1410 MEMZERO(did_dc, SIZEOF(did_dc));
shoaib_ahmed 0:791a779d6220 1411 MEMZERO(did_ac, SIZEOF(did_ac));
shoaib_ahmed 0:791a779d6220 1412
shoaib_ahmed 0:791a779d6220 1413 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
shoaib_ahmed 0:791a779d6220 1414 compptr = cinfo->cur_comp_info[ci];
shoaib_ahmed 0:791a779d6220 1415 /* DC needs no table for refinement scan */
shoaib_ahmed 0:791a779d6220 1416 if (cinfo->Ss == 0 && cinfo->Ah == 0) {
shoaib_ahmed 0:791a779d6220 1417 tbl = compptr->dc_tbl_no;
shoaib_ahmed 0:791a779d6220 1418 if (! did_dc[tbl]) {
shoaib_ahmed 0:791a779d6220 1419 htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
shoaib_ahmed 0:791a779d6220 1420 if (*htblptr == NULL)
shoaib_ahmed 0:791a779d6220 1421 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
shoaib_ahmed 0:791a779d6220 1422 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[tbl]);
shoaib_ahmed 0:791a779d6220 1423 did_dc[tbl] = TRUE;
shoaib_ahmed 0:791a779d6220 1424 }
shoaib_ahmed 0:791a779d6220 1425 }
shoaib_ahmed 0:791a779d6220 1426 /* AC needs no table when not present */
shoaib_ahmed 0:791a779d6220 1427 if (cinfo->Se) {
shoaib_ahmed 0:791a779d6220 1428 tbl = compptr->ac_tbl_no;
shoaib_ahmed 0:791a779d6220 1429 if (! did_ac[tbl]) {
shoaib_ahmed 0:791a779d6220 1430 htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
shoaib_ahmed 0:791a779d6220 1431 if (*htblptr == NULL)
shoaib_ahmed 0:791a779d6220 1432 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
shoaib_ahmed 0:791a779d6220 1433 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[tbl]);
shoaib_ahmed 0:791a779d6220 1434 did_ac[tbl] = TRUE;
shoaib_ahmed 0:791a779d6220 1435 }
shoaib_ahmed 0:791a779d6220 1436 }
shoaib_ahmed 0:791a779d6220 1437 }
shoaib_ahmed 0:791a779d6220 1438 }
shoaib_ahmed 0:791a779d6220 1439
shoaib_ahmed 0:791a779d6220 1440
shoaib_ahmed 0:791a779d6220 1441 /*
shoaib_ahmed 0:791a779d6220 1442 * Initialize for a Huffman-compressed scan.
shoaib_ahmed 0:791a779d6220 1443 * If gather_statistics is TRUE, we do not output anything during the scan,
shoaib_ahmed 0:791a779d6220 1444 * just count the Huffman symbols used and generate Huffman code tables.
shoaib_ahmed 0:791a779d6220 1445 */
shoaib_ahmed 0:791a779d6220 1446
shoaib_ahmed 0:791a779d6220 1447 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 1448 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
shoaib_ahmed 0:791a779d6220 1449 {
shoaib_ahmed 0:791a779d6220 1450 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
shoaib_ahmed 0:791a779d6220 1451 int ci, tbl;
shoaib_ahmed 0:791a779d6220 1452 jpeg_component_info * compptr;
shoaib_ahmed 0:791a779d6220 1453
shoaib_ahmed 0:791a779d6220 1454 if (gather_statistics)
shoaib_ahmed 0:791a779d6220 1455 entropy->pub.finish_pass = finish_pass_gather;
shoaib_ahmed 0:791a779d6220 1456 else
shoaib_ahmed 0:791a779d6220 1457 entropy->pub.finish_pass = finish_pass_huff;
shoaib_ahmed 0:791a779d6220 1458
shoaib_ahmed 0:791a779d6220 1459 if (cinfo->progressive_mode) {
shoaib_ahmed 0:791a779d6220 1460 entropy->cinfo = cinfo;
shoaib_ahmed 0:791a779d6220 1461 entropy->gather_statistics = gather_statistics;
shoaib_ahmed 0:791a779d6220 1462
shoaib_ahmed 0:791a779d6220 1463 /* We assume jcmaster.c already validated the scan parameters. */
shoaib_ahmed 0:791a779d6220 1464
shoaib_ahmed 0:791a779d6220 1465 /* Select execution routine */
shoaib_ahmed 0:791a779d6220 1466 if (cinfo->Ah == 0) {
shoaib_ahmed 0:791a779d6220 1467 if (cinfo->Ss == 0)
shoaib_ahmed 0:791a779d6220 1468 entropy->pub.encode_mcu = encode_mcu_DC_first;
shoaib_ahmed 0:791a779d6220 1469 else
shoaib_ahmed 0:791a779d6220 1470 entropy->pub.encode_mcu = encode_mcu_AC_first;
shoaib_ahmed 0:791a779d6220 1471 } else {
shoaib_ahmed 0:791a779d6220 1472 if (cinfo->Ss == 0)
shoaib_ahmed 0:791a779d6220 1473 entropy->pub.encode_mcu = encode_mcu_DC_refine;
shoaib_ahmed 0:791a779d6220 1474 else {
shoaib_ahmed 0:791a779d6220 1475 entropy->pub.encode_mcu = encode_mcu_AC_refine;
shoaib_ahmed 0:791a779d6220 1476 /* AC refinement needs a correction bit buffer */
shoaib_ahmed 0:791a779d6220 1477 if (entropy->bit_buffer == NULL)
shoaib_ahmed 0:791a779d6220 1478 entropy->bit_buffer = (char *)
shoaib_ahmed 0:791a779d6220 1479 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 1480 MAX_CORR_BITS * SIZEOF(char));
shoaib_ahmed 0:791a779d6220 1481 }
shoaib_ahmed 0:791a779d6220 1482 }
shoaib_ahmed 0:791a779d6220 1483
shoaib_ahmed 0:791a779d6220 1484 /* Initialize AC stuff */
shoaib_ahmed 0:791a779d6220 1485 entropy->ac_tbl_no = cinfo->cur_comp_info[0]->ac_tbl_no;
shoaib_ahmed 0:791a779d6220 1486 entropy->EOBRUN = 0;
shoaib_ahmed 0:791a779d6220 1487 entropy->BE = 0;
shoaib_ahmed 0:791a779d6220 1488 } else {
shoaib_ahmed 0:791a779d6220 1489 if (gather_statistics)
shoaib_ahmed 0:791a779d6220 1490 entropy->pub.encode_mcu = encode_mcu_gather;
shoaib_ahmed 0:791a779d6220 1491 else
shoaib_ahmed 0:791a779d6220 1492 entropy->pub.encode_mcu = encode_mcu_huff;
shoaib_ahmed 0:791a779d6220 1493 }
shoaib_ahmed 0:791a779d6220 1494
shoaib_ahmed 0:791a779d6220 1495 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
shoaib_ahmed 0:791a779d6220 1496 compptr = cinfo->cur_comp_info[ci];
shoaib_ahmed 0:791a779d6220 1497 /* DC needs no table for refinement scan */
shoaib_ahmed 0:791a779d6220 1498 if (cinfo->Ss == 0 && cinfo->Ah == 0) {
shoaib_ahmed 0:791a779d6220 1499 tbl = compptr->dc_tbl_no;
shoaib_ahmed 0:791a779d6220 1500 if (gather_statistics) {
shoaib_ahmed 0:791a779d6220 1501 /* Check for invalid table index */
shoaib_ahmed 0:791a779d6220 1502 /* (make_c_derived_tbl does this in the other path) */
shoaib_ahmed 0:791a779d6220 1503 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
shoaib_ahmed 0:791a779d6220 1504 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
shoaib_ahmed 0:791a779d6220 1505 /* Allocate and zero the statistics tables */
shoaib_ahmed 0:791a779d6220 1506 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
shoaib_ahmed 0:791a779d6220 1507 if (entropy->dc_count_ptrs[tbl] == NULL)
shoaib_ahmed 0:791a779d6220 1508 entropy->dc_count_ptrs[tbl] = (long *)
shoaib_ahmed 0:791a779d6220 1509 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 1510 257 * SIZEOF(long));
shoaib_ahmed 0:791a779d6220 1511 MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long));
shoaib_ahmed 0:791a779d6220 1512 } else {
shoaib_ahmed 0:791a779d6220 1513 /* Compute derived values for Huffman tables */
shoaib_ahmed 0:791a779d6220 1514 /* We may do this more than once for a table, but it's not expensive */
shoaib_ahmed 0:791a779d6220 1515 jpeg_make_c_derived_tbl(cinfo, TRUE, tbl,
shoaib_ahmed 0:791a779d6220 1516 & entropy->dc_derived_tbls[tbl]);
shoaib_ahmed 0:791a779d6220 1517 }
shoaib_ahmed 0:791a779d6220 1518 /* Initialize DC predictions to 0 */
shoaib_ahmed 0:791a779d6220 1519 entropy->saved.last_dc_val[ci] = 0;
shoaib_ahmed 0:791a779d6220 1520 }
shoaib_ahmed 0:791a779d6220 1521 /* AC needs no table when not present */
shoaib_ahmed 0:791a779d6220 1522 if (cinfo->Se) {
shoaib_ahmed 0:791a779d6220 1523 tbl = compptr->ac_tbl_no;
shoaib_ahmed 0:791a779d6220 1524 if (gather_statistics) {
shoaib_ahmed 0:791a779d6220 1525 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
shoaib_ahmed 0:791a779d6220 1526 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
shoaib_ahmed 0:791a779d6220 1527 if (entropy->ac_count_ptrs[tbl] == NULL)
shoaib_ahmed 0:791a779d6220 1528 entropy->ac_count_ptrs[tbl] = (long *)
shoaib_ahmed 0:791a779d6220 1529 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 1530 257 * SIZEOF(long));
shoaib_ahmed 0:791a779d6220 1531 MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long));
shoaib_ahmed 0:791a779d6220 1532 } else {
shoaib_ahmed 0:791a779d6220 1533 jpeg_make_c_derived_tbl(cinfo, FALSE, tbl,
shoaib_ahmed 0:791a779d6220 1534 & entropy->ac_derived_tbls[tbl]);
shoaib_ahmed 0:791a779d6220 1535 }
shoaib_ahmed 0:791a779d6220 1536 }
shoaib_ahmed 0:791a779d6220 1537 }
shoaib_ahmed 0:791a779d6220 1538
shoaib_ahmed 0:791a779d6220 1539 /* Initialize bit buffer to empty */
shoaib_ahmed 0:791a779d6220 1540 entropy->saved.put_buffer = 0;
shoaib_ahmed 0:791a779d6220 1541 entropy->saved.put_bits = 0;
shoaib_ahmed 0:791a779d6220 1542
shoaib_ahmed 0:791a779d6220 1543 /* Initialize restart stuff */
shoaib_ahmed 0:791a779d6220 1544 entropy->restarts_to_go = cinfo->restart_interval;
shoaib_ahmed 0:791a779d6220 1545 entropy->next_restart_num = 0;
shoaib_ahmed 0:791a779d6220 1546 }
shoaib_ahmed 0:791a779d6220 1547
shoaib_ahmed 0:791a779d6220 1548
shoaib_ahmed 0:791a779d6220 1549 /*
shoaib_ahmed 0:791a779d6220 1550 * Module initialization routine for Huffman entropy encoding.
shoaib_ahmed 0:791a779d6220 1551 */
shoaib_ahmed 0:791a779d6220 1552
shoaib_ahmed 0:791a779d6220 1553 GLOBAL(void)
shoaib_ahmed 0:791a779d6220 1554 jinit_huff_encoder (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 1555 {
shoaib_ahmed 0:791a779d6220 1556 huff_entropy_ptr entropy;
shoaib_ahmed 0:791a779d6220 1557 int i;
shoaib_ahmed 0:791a779d6220 1558
shoaib_ahmed 0:791a779d6220 1559 entropy = (huff_entropy_ptr)
shoaib_ahmed 0:791a779d6220 1560 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 1561 SIZEOF(huff_entropy_encoder));
shoaib_ahmed 0:791a779d6220 1562 cinfo->entropy = &entropy->pub;
shoaib_ahmed 0:791a779d6220 1563 entropy->pub.start_pass = start_pass_huff;
shoaib_ahmed 0:791a779d6220 1564
shoaib_ahmed 0:791a779d6220 1565 /* Mark tables unallocated */
shoaib_ahmed 0:791a779d6220 1566 for (i = 0; i < NUM_HUFF_TBLS; i++) {
shoaib_ahmed 0:791a779d6220 1567 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
shoaib_ahmed 0:791a779d6220 1568 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
shoaib_ahmed 0:791a779d6220 1569 }
shoaib_ahmed 0:791a779d6220 1570
shoaib_ahmed 0:791a779d6220 1571 if (cinfo->progressive_mode)
shoaib_ahmed 0:791a779d6220 1572 entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
shoaib_ahmed 0:791a779d6220 1573 }