jchuff.c

Go to the documentation of this file.
00001 /*
00002  * jchuff.c
00003  *
00004  * Copyright (C) 1991-1997, Thomas G. Lane.
00005  * Modified 2006-2009 by Guido Vollbeding.
00006  * This file is part of the Independent JPEG Group's software.
00007  * For conditions of distribution and use, see the accompanying README file.
00008  *
00009  * This file contains Huffman entropy encoding routines.
00010  * Both sequential and progressive modes are supported in this single module.
00011  *
00012  * Much of the complexity here has to do with supporting output suspension.
00013  * If the data destination module demands suspension, we want to be able to
00014  * back up to the start of the current MCU.  To do this, we copy state
00015  * variables into local working storage, and update them back to the
00016  * permanent JPEG objects only upon successful completion of an MCU.
00017  *
00018  * We do not support output suspension for the progressive JPEG mode, since
00019  * the library currently does not allow multiple-scan files to be written
00020  * with output suspension.
00021  */
00022 
00023 #define JPEG_INTERNALS
00024 #include "jinclude.h"
00025 #include "jpeglib.h"
00026 
00027 
00028 /* The legal range of a DCT coefficient is
00029  *  -1024 .. +1023  for 8-bit data;
00030  * -16384 .. +16383 for 12-bit data.
00031  * Hence the magnitude should always fit in 10 or 14 bits respectively.
00032  */
00033 
00034 #if BITS_IN_JSAMPLE == 8
00035 #define MAX_COEF_BITS 10
00036 #else
00037 #define MAX_COEF_BITS 14
00038 #endif
00039 
00040 /* Derived data constructed for each Huffman table */
00041 
00042 typedef struct {
00043   unsigned int ehufco[256];     /* code for each symbol */
00044   char ehufsi[256];             /* length of code for each symbol */
00045   /* If no code has been allocated for a symbol S, ehufsi[S] contains 0 */
00046 } c_derived_tbl;
00047 
00048 
00049 /* Expanded entropy encoder object for Huffman encoding.
00050  *
00051  * The savable_state subrecord contains fields that change within an MCU,
00052  * but must not be updated permanently until we complete the MCU.
00053  */
00054 
00055 typedef struct {
00056   INT32 put_buffer;             /* current bit-accumulation buffer */
00057   int put_bits;                 /* # of bits now in it */
00058   int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
00059 } savable_state;
00060 
00061 /* This macro is to work around compilers with missing or broken
00062  * structure assignment.  You'll need to fix this code if you have
00063  * such a compiler and you change MAX_COMPS_IN_SCAN.
00064  */
00065 
00066 #ifndef NO_STRUCT_ASSIGN
00067 #define ASSIGN_STATE(dest,src)  ((dest) = (src))
00068 #else
00069 #if MAX_COMPS_IN_SCAN == 4
00070 #define ASSIGN_STATE(dest,src)  \
00071         ((dest).put_buffer = (src).put_buffer, \
00072          (dest).put_bits = (src).put_bits, \
00073          (dest).last_dc_val[0] = (src).last_dc_val[0], \
00074          (dest).last_dc_val[1] = (src).last_dc_val[1], \
00075          (dest).last_dc_val[2] = (src).last_dc_val[2], \
00076          (dest).last_dc_val[3] = (src).last_dc_val[3])
00077 #endif
00078 #endif
00079 
00080 
00081 typedef struct {
00082   struct jpeg_entropy_encoder pub; /* public fields */
00083 
00084   savable_state saved;          /* Bit buffer & DC state at start of MCU */
00085 
00086   /* These fields are NOT loaded into local working state. */
00087   unsigned int restarts_to_go;  /* MCUs left in this restart interval */
00088   int next_restart_num;         /* next restart number to write (0-7) */
00089 
00090   /* Pointers to derived tables (these workspaces have image lifespan) */
00091   c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
00092   c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
00093 
00094   /* Statistics tables for optimization */
00095   long * dc_count_ptrs[NUM_HUFF_TBLS];
00096   long * ac_count_ptrs[NUM_HUFF_TBLS];
00097 
00098   /* Following fields used only in progressive mode */
00099 
00100   /* Mode flag: TRUE for optimization, FALSE for actual data output */
00101   boolean gather_statistics;
00102 
00103   /* next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
00104    */
00105   JOCTET * next_output_byte;    /* => next byte to write in buffer */
00106   size_t free_in_buffer;        /* # of byte spaces remaining in buffer */
00107   j_compress_ptr cinfo;         /* link to cinfo (needed for dump_buffer) */
00108 
00109   /* Coding status for AC components */
00110   int ac_tbl_no;                /* the table number of the single component */
00111   unsigned int EOBRUN;          /* run length of EOBs */
00112   unsigned int BE;              /* # of buffered correction bits before MCU */
00113   char * bit_buffer;            /* buffer for correction bits (1 per char) */
00114   /* packing correction bits tightly would save some space but cost time... */
00115 } huff_entropy_encoder;
00116 
00117 typedef huff_entropy_encoder * huff_entropy_ptr;
00118 
00119 /* Working state while writing an MCU (sequential mode).
00120  * This struct contains all the fields that are needed by subroutines.
00121  */
00122 
00123 typedef struct {
00124   JOCTET * next_output_byte;    /* => next byte to write in buffer */
00125   size_t free_in_buffer;        /* # of byte spaces remaining in buffer */
00126   savable_state cur;            /* Current bit buffer & DC state */
00127   j_compress_ptr cinfo;         /* dump_buffer needs access to this */
00128 } working_state;
00129 
00130 /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
00131  * buffer can hold.  Larger sizes may slightly improve compression, but
00132  * 1000 is already well into the realm of overkill.
00133  * The minimum safe size is 64 bits.
00134  */
00135 
00136 #define MAX_CORR_BITS  1000     /* Max # of correction bits I can buffer */
00137 
00138 /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
00139  * We assume that int right shift is unsigned if INT32 right shift is,
00140  * which should be safe.
00141  */
00142 
00143 #ifdef RIGHT_SHIFT_IS_UNSIGNED
00144 #define ISHIFT_TEMPS    int ishift_temp;
00145 #define IRIGHT_SHIFT(x,shft)  \
00146         ((ishift_temp = (x)) < 0 ? \
00147          (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
00148          (ishift_temp >> (shft)))
00149 #else
00150 #define ISHIFT_TEMPS
00151 #define IRIGHT_SHIFT(x,shft)    ((x) >> (shft))
00152 #endif
00153 
00154 
00155 /*
00156  * Compute the derived values for a Huffman table.
00157  * This routine also performs some validation checks on the table.
00158  */
00159 
00160 LOCAL(void)
00161 jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
00162                          c_derived_tbl ** pdtbl)
00163 {
00164   JHUFF_TBL *htbl;
00165   c_derived_tbl *dtbl;
00166   int p, i, l, lastp, si, maxsymbol;
00167   char huffsize[257];
00168   unsigned int huffcode[257];
00169   unsigned int code;
00170 
00171   /* Note that huffsize[] and huffcode[] are filled in code-length order,
00172    * paralleling the order of the symbols themselves in htbl->huffval[].
00173    */
00174 
00175   /* Find the input Huffman table */
00176   if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
00177     ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
00178   htbl =
00179     isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
00180   if (htbl == NULL)
00181     ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
00182 
00183   /* Allocate a workspace if we haven't already done so. */
00184   if (*pdtbl == NULL)
00185     *pdtbl = (c_derived_tbl *)
00186       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00187                                   SIZEOF(c_derived_tbl));
00188   dtbl = *pdtbl;
00189   
00190   /* Figure C.1: make table of Huffman code length for each symbol */
00191 
00192   p = 0;
00193   for (l = 1; l <= 16; l++) {
00194     i = (int) htbl->bits[l];
00195     if (i < 0 || p + i > 256)   /* protect against table overrun */
00196       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
00197     while (i--)
00198       huffsize[p++] = (char) l;
00199   }
00200   huffsize[p] = 0;
00201   lastp = p;
00202   
00203   /* Figure C.2: generate the codes themselves */
00204   /* We also validate that the counts represent a legal Huffman code tree. */
00205 
00206   code = 0;
00207   si = huffsize[0];
00208   p = 0;
00209   while (huffsize[p]) {
00210     while (((int) huffsize[p]) == si) {
00211       huffcode[p++] = code;
00212       code++;
00213     }
00214     /* code is now 1 more than the last code used for codelength si; but
00215      * it must still fit in si bits, since no code is allowed to be all ones.
00216      */
00217     if (((INT32) code) >= (((INT32) 1) << si))
00218       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
00219     code <<= 1;
00220     si++;
00221   }
00222   
00223   /* Figure C.3: generate encoding tables */
00224   /* These are code and size indexed by symbol value */
00225 
00226   /* Set all codeless symbols to have code length 0;
00227    * this lets us detect duplicate VAL entries here, and later
00228    * allows emit_bits to detect any attempt to emit such symbols.
00229    */
00230   MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
00231 
00232   /* This is also a convenient place to check for out-of-range
00233    * and duplicated VAL entries.  We allow 0..255 for AC symbols
00234    * but only 0..15 for DC.  (We could constrain them further
00235    * based on data depth and mode, but this seems enough.)
00236    */
00237   maxsymbol = isDC ? 15 : 255;
00238 
00239   for (p = 0; p < lastp; p++) {
00240     i = htbl->huffval[p];
00241     if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
00242       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
00243     dtbl->ehufco[i] = huffcode[p];
00244     dtbl->ehufsi[i] = huffsize[p];
00245   }
00246 }
00247 
00248 
00249 /* Outputting bytes to the file.
00250  * NB: these must be called only when actually outputting,
00251  * that is, entropy->gather_statistics == FALSE.
00252  */
00253 
00254 /* Emit a byte, taking 'action' if must suspend. */
00255 #define emit_byte_s(state,val,action)  \
00256         { *(state)->next_output_byte++ = (JOCTET) (val);  \
00257           if (--(state)->free_in_buffer == 0)  \
00258             if (! dump_buffer_s(state))  \
00259               { action; } }
00260 
00261 /* Emit a byte */
00262 #define emit_byte_e(entropy,val)  \
00263         { *(entropy)->next_output_byte++ = (JOCTET) (val);  \
00264           if (--(entropy)->free_in_buffer == 0)  \
00265             dump_buffer_e(entropy); }
00266 
00267 
00268 LOCAL(boolean)
00269 dump_buffer_s (working_state * state)
00270 /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
00271 {
00272   struct jpeg_destination_mgr * dest = state->cinfo->dest;
00273 
00274   if (! (*dest->empty_output_buffer) (state->cinfo))
00275     return FALSE;
00276   /* After a successful buffer dump, must reset buffer pointers */
00277   state->next_output_byte = dest->next_output_byte;
00278   state->free_in_buffer = dest->free_in_buffer;
00279   return TRUE;
00280 }
00281 
00282 
00283 LOCAL(void)
00284 dump_buffer_e (huff_entropy_ptr entropy)
00285 /* Empty the output buffer; we do not support suspension in this case. */
00286 {
00287   struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
00288 
00289   if (! (*dest->empty_output_buffer) (entropy->cinfo))
00290     ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
00291   /* After a successful buffer dump, must reset buffer pointers */
00292   entropy->next_output_byte = dest->next_output_byte;
00293   entropy->free_in_buffer = dest->free_in_buffer;
00294 }
00295 
00296 
00297 /* Outputting bits to the file */
00298 
00299 /* Only the right 24 bits of put_buffer are used; the valid bits are
00300  * left-justified in this part.  At most 16 bits can be passed to emit_bits
00301  * in one call, and we never retain more than 7 bits in put_buffer
00302  * between calls, so 24 bits are sufficient.
00303  */
00304 
00305 INLINE
00306 LOCAL(boolean)
00307 emit_bits_s (working_state * state, unsigned int code, int size)
00308 /* Emit some bits; return TRUE if successful, FALSE if must suspend */
00309 {
00310   /* This routine is heavily used, so it's worth coding tightly. */
00311   register INT32 put_buffer = (INT32) code;
00312   register int put_bits = state->cur.put_bits;
00313 
00314   /* if size is 0, caller used an invalid Huffman table entry */
00315   if (size == 0)
00316     ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
00317 
00318   put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
00319   
00320   put_bits += size;             /* new number of bits in buffer */
00321   
00322   put_buffer <<= 24 - put_bits; /* align incoming bits */
00323 
00324   put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
00325   
00326   while (put_bits >= 8) {
00327     int c = (int) ((put_buffer >> 16) & 0xFF);
00328     
00329     emit_byte_s(state, c, return FALSE);
00330     if (c == 0xFF) {            /* need to stuff a zero byte? */
00331       emit_byte_s(state, 0, return FALSE);
00332     }
00333     put_buffer <<= 8;
00334     put_bits -= 8;
00335   }
00336 
00337   state->cur.put_buffer = put_buffer; /* update state variables */
00338   state->cur.put_bits = put_bits;
00339 
00340   return TRUE;
00341 }
00342 
00343 
00344 INLINE
00345 LOCAL(void)
00346 emit_bits_e (huff_entropy_ptr entropy, unsigned int code, int size)
00347 /* Emit some bits, unless we are in gather mode */
00348 {
00349   /* This routine is heavily used, so it's worth coding tightly. */
00350   register INT32 put_buffer = (INT32) code;
00351   register int put_bits = entropy->saved.put_bits;
00352 
00353   /* if size is 0, caller used an invalid Huffman table entry */
00354   if (size == 0)
00355     ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
00356 
00357   if (entropy->gather_statistics)
00358     return;                     /* do nothing if we're only getting stats */
00359 
00360   put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
00361   
00362   put_bits += size;             /* new number of bits in buffer */
00363 
00364   put_buffer <<= 24 - put_bits; /* align incoming bits */
00365 
00366   /* and merge with old buffer contents */
00367   put_buffer |= entropy->saved.put_buffer;
00368 
00369   while (put_bits >= 8) {
00370     int c = (int) ((put_buffer >> 16) & 0xFF);
00371 
00372     emit_byte_e(entropy, c);
00373     if (c == 0xFF) {            /* need to stuff a zero byte? */
00374       emit_byte_e(entropy, 0);
00375     }
00376     put_buffer <<= 8;
00377     put_bits -= 8;
00378   }
00379 
00380   entropy->saved.put_buffer = put_buffer; /* update variables */
00381   entropy->saved.put_bits = put_bits;
00382 }
00383 
00384 
00385 LOCAL(boolean)
00386 flush_bits_s (working_state * state)
00387 {
00388   if (! emit_bits_s(state, 0x7F, 7)) /* fill any partial byte with ones */
00389     return FALSE;
00390   state->cur.put_buffer = 0;         /* and reset bit-buffer to empty */
00391   state->cur.put_bits = 0;
00392   return TRUE;
00393 }
00394 
00395 
00396 LOCAL(void)
00397 flush_bits_e (huff_entropy_ptr entropy)
00398 {
00399   emit_bits_e(entropy, 0x7F, 7); /* fill any partial byte with ones */
00400   entropy->saved.put_buffer = 0; /* and reset bit-buffer to empty */
00401   entropy->saved.put_bits = 0;
00402 }
00403 
00404 
00405 /*
00406  * Emit (or just count) a Huffman symbol.
00407  */
00408 
00409 INLINE
00410 LOCAL(void)
00411 emit_dc_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
00412 {
00413   if (entropy->gather_statistics)
00414     entropy->dc_count_ptrs[tbl_no][symbol]++;
00415   else {
00416     c_derived_tbl * tbl = entropy->dc_derived_tbls[tbl_no];
00417     emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
00418   }
00419 }
00420 
00421 
00422 INLINE
00423 LOCAL(void)
00424 emit_ac_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
00425 {
00426   if (entropy->gather_statistics)
00427     entropy->ac_count_ptrs[tbl_no][symbol]++;
00428   else {
00429     c_derived_tbl * tbl = entropy->ac_derived_tbls[tbl_no];
00430     emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
00431   }
00432 }
00433 
00434 
00435 /*
00436  * Emit bits from a correction bit buffer.
00437  */
00438 
00439 LOCAL(void)
00440 emit_buffered_bits (huff_entropy_ptr entropy, char * bufstart,
00441                     unsigned int nbits)
00442 {
00443   if (entropy->gather_statistics)
00444     return;                     /* no real work */
00445 
00446   while (nbits > 0) {
00447     emit_bits_e(entropy, (unsigned int) (*bufstart), 1);
00448     bufstart++;
00449     nbits--;
00450   }
00451 }
00452 
00453 
00454 /*
00455  * Emit any pending EOBRUN symbol.
00456  */
00457 
00458 LOCAL(void)
00459 emit_eobrun (huff_entropy_ptr entropy)
00460 {
00461   register int temp, nbits;
00462 
00463   if (entropy->EOBRUN > 0) {    /* if there is any pending EOBRUN */
00464     temp = entropy->EOBRUN;
00465     nbits = 0;
00466     while ((temp >>= 1))
00467       nbits++;
00468     /* safety check: shouldn't happen given limited correction-bit buffer */
00469     if (nbits > 14)
00470       ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
00471 
00472     emit_ac_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
00473     if (nbits)
00474       emit_bits_e(entropy, entropy->EOBRUN, nbits);
00475 
00476     entropy->EOBRUN = 0;
00477 
00478     /* Emit any buffered correction bits */
00479     emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
00480     entropy->BE = 0;
00481   }
00482 }
00483 
00484 
00485 /*
00486  * Emit a restart marker & resynchronize predictions.
00487  */
00488 
00489 LOCAL(boolean)
00490 emit_restart_s (working_state * state, int restart_num)
00491 {
00492   int ci;
00493 
00494   if (! flush_bits_s(state))
00495     return FALSE;
00496 
00497   emit_byte_s(state, 0xFF, return FALSE);
00498   emit_byte_s(state, JPEG_RST0 + restart_num, return FALSE);
00499 
00500   /* Re-initialize DC predictions to 0 */
00501   for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
00502     state->cur.last_dc_val[ci] = 0;
00503 
00504   /* The restart counter is not updated until we successfully write the MCU. */
00505 
00506   return TRUE;
00507 }
00508 
00509 
00510 LOCAL(void)
00511 emit_restart_e (huff_entropy_ptr entropy, int restart_num)
00512 {
00513   int ci;
00514 
00515   emit_eobrun(entropy);
00516 
00517   if (! entropy->gather_statistics) {
00518     flush_bits_e(entropy);
00519     emit_byte_e(entropy, 0xFF);
00520     emit_byte_e(entropy, JPEG_RST0 + restart_num);
00521   }
00522 
00523   if (entropy->cinfo->Ss == 0) {
00524     /* Re-initialize DC predictions to 0 */
00525     for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
00526       entropy->saved.last_dc_val[ci] = 0;
00527   } else {
00528     /* Re-initialize all AC-related fields to 0 */
00529     entropy->EOBRUN = 0;
00530     entropy->BE = 0;
00531   }
00532 }
00533 
00534 
00535 /*
00536  * MCU encoding for DC initial scan (either spectral selection,
00537  * or first pass of successive approximation).
00538  */
00539 
00540 METHODDEF(boolean)
00541 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00542 {
00543   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
00544   register int temp, temp2;
00545   register int nbits;
00546   int blkn, ci;
00547   int Al = cinfo->Al;
00548   JBLOCKROW block;
00549   jpeg_component_info * compptr;
00550   ISHIFT_TEMPS
00551 
00552   entropy->next_output_byte = cinfo->dest->next_output_byte;
00553   entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00554 
00555   /* Emit restart marker if needed */
00556   if (cinfo->restart_interval)
00557     if (entropy->restarts_to_go == 0)
00558       emit_restart_e(entropy, entropy->next_restart_num);
00559 
00560   /* Encode the MCU data blocks */
00561   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00562     block = MCU_data[blkn];
00563     ci = cinfo->MCU_membership[blkn];
00564     compptr = cinfo->cur_comp_info[ci];
00565 
00566     /* Compute the DC value after the required point transform by Al.
00567      * This is simply an arithmetic right shift.
00568      */
00569     temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
00570 
00571     /* DC differences are figured on the point-transformed values. */
00572     temp = temp2 - entropy->saved.last_dc_val[ci];
00573     entropy->saved.last_dc_val[ci] = temp2;
00574 
00575     /* Encode the DC coefficient difference per section G.1.2.1 */
00576     temp2 = temp;
00577     if (temp < 0) {
00578       temp = -temp;             /* temp is abs value of input */
00579       /* For a negative input, want temp2 = bitwise complement of abs(input) */
00580       /* This code assumes we are on a two's complement machine */
00581       temp2--;
00582     }
00583     
00584     /* Find the number of bits needed for the magnitude of the coefficient */
00585     nbits = 0;
00586     while (temp) {
00587       nbits++;
00588       temp >>= 1;
00589     }
00590     /* Check for out-of-range coefficient values.
00591      * Since we're encoding a difference, the range limit is twice as much.
00592      */
00593     if (nbits > MAX_COEF_BITS+1)
00594       ERREXIT(cinfo, JERR_BAD_DCT_COEF);
00595     
00596     /* Count/emit the Huffman-coded symbol for the number of bits */
00597     emit_dc_symbol(entropy, compptr->dc_tbl_no, nbits);
00598     
00599     /* Emit that number of bits of the value, if positive, */
00600     /* or the complement of its magnitude, if negative. */
00601     if (nbits)                  /* emit_bits rejects calls with size 0 */
00602       emit_bits_e(entropy, (unsigned int) temp2, nbits);
00603   }
00604 
00605   cinfo->dest->next_output_byte = entropy->next_output_byte;
00606   cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00607 
00608   /* Update restart-interval state too */
00609   if (cinfo->restart_interval) {
00610     if (entropy->restarts_to_go == 0) {
00611       entropy->restarts_to_go = cinfo->restart_interval;
00612       entropy->next_restart_num++;
00613       entropy->next_restart_num &= 7;
00614     }
00615     entropy->restarts_to_go--;
00616   }
00617 
00618   return TRUE;
00619 }
00620 
00621 
00622 /*
00623  * MCU encoding for AC initial scan (either spectral selection,
00624  * or first pass of successive approximation).
00625  */
00626 
00627 METHODDEF(boolean)
00628 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00629 {
00630   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
00631   register int temp, temp2;
00632   register int nbits;
00633   register int r, k;
00634   int Se, Al;
00635   const int * natural_order;
00636   JBLOCKROW block;
00637 
00638   entropy->next_output_byte = cinfo->dest->next_output_byte;
00639   entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00640 
00641   /* Emit restart marker if needed */
00642   if (cinfo->restart_interval)
00643     if (entropy->restarts_to_go == 0)
00644       emit_restart_e(entropy, entropy->next_restart_num);
00645 
00646   Se = cinfo->Se;
00647   Al = cinfo->Al;
00648   natural_order = cinfo->natural_order;
00649 
00650   /* Encode the MCU data block */
00651   block = MCU_data[0];
00652 
00653   /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
00654   
00655   r = 0;                        /* r = run length of zeros */
00656    
00657   for (k = cinfo->Ss; k <= Se; k++) {
00658     if ((temp = (*block)[natural_order[k]]) == 0) {
00659       r++;
00660       continue;
00661     }
00662     /* We must apply the point transform by Al.  For AC coefficients this
00663      * is an integer division with rounding towards 0.  To do this portably
00664      * in C, we shift after obtaining the absolute value; so the code is
00665      * interwoven with finding the abs value (temp) and output bits (temp2).
00666      */
00667     if (temp < 0) {
00668       temp = -temp;             /* temp is abs value of input */
00669       temp >>= Al;              /* apply the point transform */
00670       /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
00671       temp2 = ~temp;
00672     } else {
00673       temp >>= Al;              /* apply the point transform */
00674       temp2 = temp;
00675     }
00676     /* Watch out for case that nonzero coef is zero after point transform */
00677     if (temp == 0) {
00678       r++;
00679       continue;
00680     }
00681 
00682     /* Emit any pending EOBRUN */
00683     if (entropy->EOBRUN > 0)
00684       emit_eobrun(entropy);
00685     /* if run length > 15, must emit special run-length-16 codes (0xF0) */
00686     while (r > 15) {
00687       emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
00688       r -= 16;
00689     }
00690 
00691     /* Find the number of bits needed for the magnitude of the coefficient */
00692     nbits = 1;                  /* there must be at least one 1 bit */
00693     while ((temp >>= 1))
00694       nbits++;
00695     /* Check for out-of-range coefficient values */
00696     if (nbits > MAX_COEF_BITS)
00697       ERREXIT(cinfo, JERR_BAD_DCT_COEF);
00698 
00699     /* Count/emit Huffman symbol for run length / number of bits */
00700     emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
00701 
00702     /* Emit that number of bits of the value, if positive, */
00703     /* or the complement of its magnitude, if negative. */
00704     emit_bits_e(entropy, (unsigned int) temp2, nbits);
00705 
00706     r = 0;                      /* reset zero run length */
00707   }
00708 
00709   if (r > 0) {                  /* If there are trailing zeroes, */
00710     entropy->EOBRUN++;          /* count an EOB */
00711     if (entropy->EOBRUN == 0x7FFF)
00712       emit_eobrun(entropy);     /* force it out to avoid overflow */
00713   }
00714 
00715   cinfo->dest->next_output_byte = entropy->next_output_byte;
00716   cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00717 
00718   /* Update restart-interval state too */
00719   if (cinfo->restart_interval) {
00720     if (entropy->restarts_to_go == 0) {
00721       entropy->restarts_to_go = cinfo->restart_interval;
00722       entropy->next_restart_num++;
00723       entropy->next_restart_num &= 7;
00724     }
00725     entropy->restarts_to_go--;
00726   }
00727 
00728   return TRUE;
00729 }
00730 
00731 
00732 /*
00733  * MCU encoding for DC successive approximation refinement scan.
00734  * Note: we assume such scans can be multi-component, although the spec
00735  * is not very clear on the point.
00736  */
00737 
00738 METHODDEF(boolean)
00739 encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00740 {
00741   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
00742   register int temp;
00743   int blkn;
00744   int Al = cinfo->Al;
00745   JBLOCKROW block;
00746 
00747   entropy->next_output_byte = cinfo->dest->next_output_byte;
00748   entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00749 
00750   /* Emit restart marker if needed */
00751   if (cinfo->restart_interval)
00752     if (entropy->restarts_to_go == 0)
00753       emit_restart_e(entropy, entropy->next_restart_num);
00754 
00755   /* Encode the MCU data blocks */
00756   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
00757     block = MCU_data[blkn];
00758 
00759     /* We simply emit the Al'th bit of the DC coefficient value. */
00760     temp = (*block)[0];
00761     emit_bits_e(entropy, (unsigned int) (temp >> Al), 1);
00762   }
00763 
00764   cinfo->dest->next_output_byte = entropy->next_output_byte;
00765   cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00766 
00767   /* Update restart-interval state too */
00768   if (cinfo->restart_interval) {
00769     if (entropy->restarts_to_go == 0) {
00770       entropy->restarts_to_go = cinfo->restart_interval;
00771       entropy->next_restart_num++;
00772       entropy->next_restart_num &= 7;
00773     }
00774     entropy->restarts_to_go--;
00775   }
00776 
00777   return TRUE;
00778 }
00779 
00780 
00781 /*
00782  * MCU encoding for AC successive approximation refinement scan.
00783  */
00784 
00785 METHODDEF(boolean)
00786 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
00787 {
00788   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
00789   register int temp;
00790   register int r, k;
00791   int EOB;
00792   char *BR_buffer;
00793   unsigned int BR;
00794   int Se, Al;
00795   const int * natural_order;
00796   JBLOCKROW block;
00797   int absvalues[DCTSIZE2];
00798 
00799   entropy->next_output_byte = cinfo->dest->next_output_byte;
00800   entropy->free_in_buffer = cinfo->dest->free_in_buffer;
00801 
00802   /* Emit restart marker if needed */
00803   if (cinfo->restart_interval)
00804     if (entropy->restarts_to_go == 0)
00805       emit_restart_e(entropy, entropy->next_restart_num);
00806 
00807   Se = cinfo->Se;
00808   Al = cinfo->Al;
00809   natural_order = cinfo->natural_order;
00810 
00811   /* Encode the MCU data block */
00812   block = MCU_data[0];
00813 
00814   /* It is convenient to make a pre-pass to determine the transformed
00815    * coefficients' absolute values and the EOB position.
00816    */
00817   EOB = 0;
00818   for (k = cinfo->Ss; k <= Se; k++) {
00819     temp = (*block)[natural_order[k]];
00820     /* We must apply the point transform by Al.  For AC coefficients this
00821      * is an integer division with rounding towards 0.  To do this portably
00822      * in C, we shift after obtaining the absolute value.
00823      */
00824     if (temp < 0)
00825       temp = -temp;             /* temp is abs value of input */
00826     temp >>= Al;                /* apply the point transform */
00827     absvalues[k] = temp;        /* save abs value for main pass */
00828     if (temp == 1)
00829       EOB = k;                  /* EOB = index of last newly-nonzero coef */
00830   }
00831 
00832   /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
00833   
00834   r = 0;                        /* r = run length of zeros */
00835   BR = 0;                       /* BR = count of buffered bits added now */
00836   BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
00837 
00838   for (k = cinfo->Ss; k <= Se; k++) {
00839     if ((temp = absvalues[k]) == 0) {
00840       r++;
00841       continue;
00842     }
00843 
00844     /* Emit any required ZRLs, but not if they can be folded into EOB */
00845     while (r > 15 && k <= EOB) {
00846       /* emit any pending EOBRUN and the BE correction bits */
00847       emit_eobrun(entropy);
00848       /* Emit ZRL */
00849       emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
00850       r -= 16;
00851       /* Emit buffered correction bits that must be associated with ZRL */
00852       emit_buffered_bits(entropy, BR_buffer, BR);
00853       BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
00854       BR = 0;
00855     }
00856 
00857     /* If the coef was previously nonzero, it only needs a correction bit.
00858      * NOTE: a straight translation of the spec's figure G.7 would suggest
00859      * that we also need to test r > 15.  But if r > 15, we can only get here
00860      * if k > EOB, which implies that this coefficient is not 1.
00861      */
00862     if (temp > 1) {
00863       /* The correction bit is the next bit of the absolute value. */
00864       BR_buffer[BR++] = (char) (temp & 1);
00865       continue;
00866     }
00867 
00868     /* Emit any pending EOBRUN and the BE correction bits */
00869     emit_eobrun(entropy);
00870 
00871     /* Count/emit Huffman symbol for run length / number of bits */
00872     emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
00873 
00874     /* Emit output bit for newly-nonzero coef */
00875     temp = ((*block)[natural_order[k]] < 0) ? 0 : 1;
00876     emit_bits_e(entropy, (unsigned int) temp, 1);
00877 
00878     /* Emit buffered correction bits that must be associated with this code */
00879     emit_buffered_bits(entropy, BR_buffer, BR);
00880     BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
00881     BR = 0;
00882     r = 0;                      /* reset zero run length */
00883   }
00884 
00885   if (r > 0 || BR > 0) {        /* If there are trailing zeroes, */
00886     entropy->EOBRUN++;          /* count an EOB */
00887     entropy->BE += BR;          /* concat my correction bits to older ones */
00888     /* We force out the EOB if we risk either:
00889      * 1. overflow of the EOB counter;
00890      * 2. overflow of the correction bit buffer during the next MCU.
00891      */
00892     if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
00893       emit_eobrun(entropy);
00894   }
00895 
00896   cinfo->dest->next_output_byte = entropy->next_output_byte;
00897   cinfo->dest->free_in_buffer = entropy->free_in_buffer;
00898 
00899   /* Update restart-interval state too */
00900   if (cinfo->restart_interval) {
00901     if (entropy->restarts_to_go == 0) {
00902       entropy->restarts_to_go = cinfo->restart_interval;
00903       entropy->next_restart_num++;
00904       entropy->next_restart_num &= 7;
00905     }
00906     entropy->restarts_to_go--;
00907   }
00908 
00909   return TRUE;
00910 }
00911 
00912 
00913 /* Encode a single block's worth of coefficients */
00914 
00915 LOCAL(boolean)
00916 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
00917                   c_derived_tbl *dctbl, c_derived_tbl *actbl)
00918 {
00919   register int temp, temp2;
00920   register int nbits;
00921   register int k, r, i;
00922   int Se = state->cinfo->lim_Se;
00923   const int * natural_order = state->cinfo->natural_order;
00924 
00925   /* Encode the DC coefficient difference per section F.1.2.1 */
00926 
00927   temp = temp2 = block[0] - last_dc_val;
00928 
00929   if (temp < 0) {
00930     temp = -temp;               /* temp is abs value of input */
00931     /* For a negative input, want temp2 = bitwise complement of abs(input) */
00932     /* This code assumes we are on a two's complement machine */
00933     temp2--;
00934   }
00935 
00936   /* Find the number of bits needed for the magnitude of the coefficient */
00937   nbits = 0;
00938   while (temp) {
00939     nbits++;
00940     temp >>= 1;
00941   }
00942   /* Check for out-of-range coefficient values.
00943    * Since we're encoding a difference, the range limit is twice as much.
00944    */
00945   if (nbits > MAX_COEF_BITS+1)
00946     ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
00947 
00948   /* Emit the Huffman-coded symbol for the number of bits */
00949   if (! emit_bits_s(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
00950     return FALSE;
00951 
00952   /* Emit that number of bits of the value, if positive, */
00953   /* or the complement of its magnitude, if negative. */
00954   if (nbits)                    /* emit_bits rejects calls with size 0 */
00955     if (! emit_bits_s(state, (unsigned int) temp2, nbits))
00956       return FALSE;
00957 
00958   /* Encode the AC coefficients per section F.1.2.2 */
00959 
00960   r = 0;                        /* r = run length of zeros */
00961 
00962   for (k = 1; k <= Se; k++) {
00963     if ((temp = block[natural_order[k]]) == 0) {
00964       r++;
00965     } else {
00966       /* if run length > 15, must emit special run-length-16 codes (0xF0) */
00967       while (r > 15) {
00968         if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
00969           return FALSE;
00970         r -= 16;
00971       }
00972 
00973       temp2 = temp;
00974       if (temp < 0) {
00975         temp = -temp;           /* temp is abs value of input */
00976         /* This code assumes we are on a two's complement machine */
00977         temp2--;
00978       }
00979 
00980       /* Find the number of bits needed for the magnitude of the coefficient */
00981       nbits = 1;                /* there must be at least one 1 bit */
00982       while ((temp >>= 1))
00983         nbits++;
00984       /* Check for out-of-range coefficient values */
00985       if (nbits > MAX_COEF_BITS)
00986         ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
00987 
00988       /* Emit Huffman symbol for run length / number of bits */
00989       i = (r << 4) + nbits;
00990       if (! emit_bits_s(state, actbl->ehufco[i], actbl->ehufsi[i]))
00991         return FALSE;
00992 
00993       /* Emit that number of bits of the value, if positive, */
00994       /* or the complement of its magnitude, if negative. */
00995       if (! emit_bits_s(state, (unsigned int) temp2, nbits))
00996         return FALSE;
00997 
00998       r = 0;
00999     }
01000   }
01001 
01002   /* If the last coef(s) were zero, emit an end-of-block code */
01003   if (r > 0)
01004     if (! emit_bits_s(state, actbl->ehufco[0], actbl->ehufsi[0]))
01005       return FALSE;
01006 
01007   return TRUE;
01008 }
01009 
01010 
01011 /*
01012  * Encode and output one MCU's worth of Huffman-compressed coefficients.
01013  */
01014 
01015 METHODDEF(boolean)
01016 encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
01017 {
01018   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
01019   working_state state;
01020   int blkn, ci;
01021   jpeg_component_info * compptr;
01022 
01023   /* Load up working state */
01024   state.next_output_byte = cinfo->dest->next_output_byte;
01025   state.free_in_buffer = cinfo->dest->free_in_buffer;
01026   ASSIGN_STATE(state.cur, entropy->saved);
01027   state.cinfo = cinfo;
01028 
01029   /* Emit restart marker if needed */
01030   if (cinfo->restart_interval) {
01031     if (entropy->restarts_to_go == 0)
01032       if (! emit_restart_s(&state, entropy->next_restart_num))
01033         return FALSE;
01034   }
01035 
01036   /* Encode the MCU data blocks */
01037   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
01038     ci = cinfo->MCU_membership[blkn];
01039     compptr = cinfo->cur_comp_info[ci];
01040     if (! encode_one_block(&state,
01041                            MCU_data[blkn][0], state.cur.last_dc_val[ci],
01042                            entropy->dc_derived_tbls[compptr->dc_tbl_no],
01043                            entropy->ac_derived_tbls[compptr->ac_tbl_no]))
01044       return FALSE;
01045     /* Update last_dc_val */
01046     state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
01047   }
01048 
01049   /* Completed MCU, so update state */
01050   cinfo->dest->next_output_byte = state.next_output_byte;
01051   cinfo->dest->free_in_buffer = state.free_in_buffer;
01052   ASSIGN_STATE(entropy->saved, state.cur);
01053 
01054   /* Update restart-interval state too */
01055   if (cinfo->restart_interval) {
01056     if (entropy->restarts_to_go == 0) {
01057       entropy->restarts_to_go = cinfo->restart_interval;
01058       entropy->next_restart_num++;
01059       entropy->next_restart_num &= 7;
01060     }
01061     entropy->restarts_to_go--;
01062   }
01063 
01064   return TRUE;
01065 }
01066 
01067 
01068 /*
01069  * Finish up at the end of a Huffman-compressed scan.
01070  */
01071 
01072 METHODDEF(void)
01073 finish_pass_huff (j_compress_ptr cinfo)
01074 {
01075   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
01076   working_state state;
01077 
01078   if (cinfo->progressive_mode) {
01079     entropy->next_output_byte = cinfo->dest->next_output_byte;
01080     entropy->free_in_buffer = cinfo->dest->free_in_buffer;
01081 
01082     /* Flush out any buffered data */
01083     emit_eobrun(entropy);
01084     flush_bits_e(entropy);
01085 
01086     cinfo->dest->next_output_byte = entropy->next_output_byte;
01087     cinfo->dest->free_in_buffer = entropy->free_in_buffer;
01088   } else {
01089     /* Load up working state ... flush_bits needs it */
01090     state.next_output_byte = cinfo->dest->next_output_byte;
01091     state.free_in_buffer = cinfo->dest->free_in_buffer;
01092     ASSIGN_STATE(state.cur, entropy->saved);
01093     state.cinfo = cinfo;
01094 
01095     /* Flush out the last data */
01096     if (! flush_bits_s(&state))
01097       ERREXIT(cinfo, JERR_CANT_SUSPEND);
01098 
01099     /* Update state */
01100     cinfo->dest->next_output_byte = state.next_output_byte;
01101     cinfo->dest->free_in_buffer = state.free_in_buffer;
01102     ASSIGN_STATE(entropy->saved, state.cur);
01103   }
01104 }
01105 
01106 
01107 /*
01108  * Huffman coding optimization.
01109  *
01110  * We first scan the supplied data and count the number of uses of each symbol
01111  * that is to be Huffman-coded. (This process MUST agree with the code above.)
01112  * Then we build a Huffman coding tree for the observed counts.
01113  * Symbols which are not needed at all for the particular image are not
01114  * assigned any code, which saves space in the DHT marker as well as in
01115  * the compressed data.
01116  */
01117 
01118 
01119 /* Process a single block's worth of coefficients */
01120 
01121 LOCAL(void)
01122 htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
01123                  long dc_counts[], long ac_counts[])
01124 {
01125   register int temp;
01126   register int nbits;
01127   register int k, r;
01128   int Se = cinfo->lim_Se;
01129   const int * natural_order = cinfo->natural_order;
01130   
01131   /* Encode the DC coefficient difference per section F.1.2.1 */
01132   
01133   temp = block[0] - last_dc_val;
01134   if (temp < 0)
01135     temp = -temp;
01136   
01137   /* Find the number of bits needed for the magnitude of the coefficient */
01138   nbits = 0;
01139   while (temp) {
01140     nbits++;
01141     temp >>= 1;
01142   }
01143   /* Check for out-of-range coefficient values.
01144    * Since we're encoding a difference, the range limit is twice as much.
01145    */
01146   if (nbits > MAX_COEF_BITS+1)
01147     ERREXIT(cinfo, JERR_BAD_DCT_COEF);
01148 
01149   /* Count the Huffman symbol for the number of bits */
01150   dc_counts[nbits]++;
01151   
01152   /* Encode the AC coefficients per section F.1.2.2 */
01153   
01154   r = 0;                        /* r = run length of zeros */
01155   
01156   for (k = 1; k <= Se; k++) {
01157     if ((temp = block[natural_order[k]]) == 0) {
01158       r++;
01159     } else {
01160       /* if run length > 15, must emit special run-length-16 codes (0xF0) */
01161       while (r > 15) {
01162         ac_counts[0xF0]++;
01163         r -= 16;
01164       }
01165       
01166       /* Find the number of bits needed for the magnitude of the coefficient */
01167       if (temp < 0)
01168         temp = -temp;
01169       
01170       /* Find the number of bits needed for the magnitude of the coefficient */
01171       nbits = 1;                /* there must be at least one 1 bit */
01172       while ((temp >>= 1))
01173         nbits++;
01174       /* Check for out-of-range coefficient values */
01175       if (nbits > MAX_COEF_BITS)
01176         ERREXIT(cinfo, JERR_BAD_DCT_COEF);
01177       
01178       /* Count Huffman symbol for run length / number of bits */
01179       ac_counts[(r << 4) + nbits]++;
01180       
01181       r = 0;
01182     }
01183   }
01184 
01185   /* If the last coef(s) were zero, emit an end-of-block code */
01186   if (r > 0)
01187     ac_counts[0]++;
01188 }
01189 
01190 
01191 /*
01192  * Trial-encode one MCU's worth of Huffman-compressed coefficients.
01193  * No data is actually output, so no suspension return is possible.
01194  */
01195 
01196 METHODDEF(boolean)
01197 encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
01198 {
01199   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
01200   int blkn, ci;
01201   jpeg_component_info * compptr;
01202 
01203   /* Take care of restart intervals if needed */
01204   if (cinfo->restart_interval) {
01205     if (entropy->restarts_to_go == 0) {
01206       /* Re-initialize DC predictions to 0 */
01207       for (ci = 0; ci < cinfo->comps_in_scan; ci++)
01208         entropy->saved.last_dc_val[ci] = 0;
01209       /* Update restart state */
01210       entropy->restarts_to_go = cinfo->restart_interval;
01211     }
01212     entropy->restarts_to_go--;
01213   }
01214 
01215   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
01216     ci = cinfo->MCU_membership[blkn];
01217     compptr = cinfo->cur_comp_info[ci];
01218     htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
01219                     entropy->dc_count_ptrs[compptr->dc_tbl_no],
01220                     entropy->ac_count_ptrs[compptr->ac_tbl_no]);
01221     entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
01222   }
01223 
01224   return TRUE;
01225 }
01226 
01227 
01228 /*
01229  * Generate the best Huffman code table for the given counts, fill htbl.
01230  *
01231  * The JPEG standard requires that no symbol be assigned a codeword of all
01232  * one bits (so that padding bits added at the end of a compressed segment
01233  * can't look like a valid code).  Because of the canonical ordering of
01234  * codewords, this just means that there must be an unused slot in the
01235  * longest codeword length category.  Section K.2 of the JPEG spec suggests
01236  * reserving such a slot by pretending that symbol 256 is a valid symbol
01237  * with count 1.  In theory that's not optimal; giving it count zero but
01238  * including it in the symbol set anyway should give a better Huffman code.
01239  * But the theoretically better code actually seems to come out worse in
01240  * practice, because it produces more all-ones bytes (which incur stuffed
01241  * zero bytes in the final file).  In any case the difference is tiny.
01242  *
01243  * The JPEG standard requires Huffman codes to be no more than 16 bits long.
01244  * If some symbols have a very small but nonzero probability, the Huffman tree
01245  * must be adjusted to meet the code length restriction.  We currently use
01246  * the adjustment method suggested in JPEG section K.2.  This method is *not*
01247  * optimal; it may not choose the best possible limited-length code.  But
01248  * typically only very-low-frequency symbols will be given less-than-optimal
01249  * lengths, so the code is almost optimal.  Experimental comparisons against
01250  * an optimal limited-length-code algorithm indicate that the difference is
01251  * microscopic --- usually less than a hundredth of a percent of total size.
01252  * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
01253  */
01254 
01255 LOCAL(void)
01256 jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
01257 {
01258 #define MAX_CLEN 32             /* assumed maximum initial code length */
01259   UINT8 bits[MAX_CLEN+1];       /* bits[k] = # of symbols with code length k */
01260   int codesize[257];            /* codesize[k] = code length of symbol k */
01261   int others[257];              /* next symbol in current branch of tree */
01262   int c1, c2;
01263   int p, i, j;
01264   long v;
01265 
01266   /* This algorithm is explained in section K.2 of the JPEG standard */
01267 
01268   MEMZERO(bits, SIZEOF(bits));
01269   MEMZERO(codesize, SIZEOF(codesize));
01270   for (i = 0; i < 257; i++)
01271     others[i] = -1;             /* init links to empty */
01272   
01273   freq[256] = 1;                /* make sure 256 has a nonzero count */
01274   /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
01275    * that no real symbol is given code-value of all ones, because 256
01276    * will be placed last in the largest codeword category.
01277    */
01278 
01279   /* Huffman's basic algorithm to assign optimal code lengths to symbols */
01280 
01281   for (;;) {
01282     /* Find the smallest nonzero frequency, set c1 = its symbol */
01283     /* In case of ties, take the larger symbol number */
01284     c1 = -1;
01285     v = 1000000000L;
01286     for (i = 0; i <= 256; i++) {
01287       if (freq[i] && freq[i] <= v) {
01288         v = freq[i];
01289         c1 = i;
01290       }
01291     }
01292 
01293     /* Find the next smallest nonzero frequency, set c2 = its symbol */
01294     /* In case of ties, take the larger symbol number */
01295     c2 = -1;
01296     v = 1000000000L;
01297     for (i = 0; i <= 256; i++) {
01298       if (freq[i] && freq[i] <= v && i != c1) {
01299         v = freq[i];
01300         c2 = i;
01301       }
01302     }
01303 
01304     /* Done if we've merged everything into one frequency */
01305     if (c2 < 0)
01306       break;
01307     
01308     /* Else merge the two counts/trees */
01309     freq[c1] += freq[c2];
01310     freq[c2] = 0;
01311 
01312     /* Increment the codesize of everything in c1's tree branch */
01313     codesize[c1]++;
01314     while (others[c1] >= 0) {
01315       c1 = others[c1];
01316       codesize[c1]++;
01317     }
01318     
01319     others[c1] = c2;            /* chain c2 onto c1's tree branch */
01320     
01321     /* Increment the codesize of everything in c2's tree branch */
01322     codesize[c2]++;
01323     while (others[c2] >= 0) {
01324       c2 = others[c2];
01325       codesize[c2]++;
01326     }
01327   }
01328 
01329   /* Now count the number of symbols of each code length */
01330   for (i = 0; i <= 256; i++) {
01331     if (codesize[i]) {
01332       /* The JPEG standard seems to think that this can't happen, */
01333       /* but I'm paranoid... */
01334       if (codesize[i] > MAX_CLEN)
01335         ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
01336 
01337       bits[codesize[i]]++;
01338     }
01339   }
01340 
01341   /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
01342    * Huffman procedure assigned any such lengths, we must adjust the coding.
01343    * Here is what the JPEG spec says about how this next bit works:
01344    * Since symbols are paired for the longest Huffman code, the symbols are
01345    * removed from this length category two at a time.  The prefix for the pair
01346    * (which is one bit shorter) is allocated to one of the pair; then,
01347    * skipping the BITS entry for that prefix length, a code word from the next
01348    * shortest nonzero BITS entry is converted into a prefix for two code words
01349    * one bit longer.
01350    */
01351   
01352   for (i = MAX_CLEN; i > 16; i--) {
01353     while (bits[i] > 0) {
01354       j = i - 2;                /* find length of new prefix to be used */
01355       while (bits[j] == 0)
01356         j--;
01357       
01358       bits[i] -= 2;             /* remove two symbols */
01359       bits[i-1]++;              /* one goes in this length */
01360       bits[j+1] += 2;           /* two new symbols in this length */
01361       bits[j]--;                /* symbol of this length is now a prefix */
01362     }
01363   }
01364 
01365   /* Remove the count for the pseudo-symbol 256 from the largest codelength */
01366   while (bits[i] == 0)          /* find largest codelength still in use */
01367     i--;
01368   bits[i]--;
01369   
01370   /* Return final symbol counts (only for lengths 0..16) */
01371   MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
01372   
01373   /* Return a list of the symbols sorted by code length */
01374   /* It's not real clear to me why we don't need to consider the codelength
01375    * changes made above, but the JPEG spec seems to think this works.
01376    */
01377   p = 0;
01378   for (i = 1; i <= MAX_CLEN; i++) {
01379     for (j = 0; j <= 255; j++) {
01380       if (codesize[j] == i) {
01381         htbl->huffval[p] = (UINT8) j;
01382         p++;
01383       }
01384     }
01385   }
01386 
01387   /* Set sent_table FALSE so updated table will be written to JPEG file. */
01388   htbl->sent_table = FALSE;
01389 }
01390 
01391 
01392 /*
01393  * Finish up a statistics-gathering pass and create the new Huffman tables.
01394  */
01395 
01396 METHODDEF(void)
01397 finish_pass_gather (j_compress_ptr cinfo)
01398 {
01399   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
01400   int ci, tbl;
01401   jpeg_component_info * compptr;
01402   JHUFF_TBL **htblptr;
01403   boolean did_dc[NUM_HUFF_TBLS];
01404   boolean did_ac[NUM_HUFF_TBLS];
01405 
01406   /* It's important not to apply jpeg_gen_optimal_table more than once
01407    * per table, because it clobbers the input frequency counts!
01408    */
01409   if (cinfo->progressive_mode)
01410     /* Flush out buffered data (all we care about is counting the EOB symbol) */
01411     emit_eobrun(entropy);
01412 
01413   MEMZERO(did_dc, SIZEOF(did_dc));
01414   MEMZERO(did_ac, SIZEOF(did_ac));
01415 
01416   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
01417     compptr = cinfo->cur_comp_info[ci];
01418     /* DC needs no table for refinement scan */
01419     if (cinfo->Ss == 0 && cinfo->Ah == 0) {
01420       tbl = compptr->dc_tbl_no;
01421       if (! did_dc[tbl]) {
01422         htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
01423         if (*htblptr == NULL)
01424           *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
01425         jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[tbl]);
01426         did_dc[tbl] = TRUE;
01427       }
01428     }
01429     /* AC needs no table when not present */
01430     if (cinfo->Se) {
01431       tbl = compptr->ac_tbl_no;
01432       if (! did_ac[tbl]) {
01433         htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
01434         if (*htblptr == NULL)
01435           *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
01436         jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[tbl]);
01437         did_ac[tbl] = TRUE;
01438       }
01439     }
01440   }
01441 }
01442 
01443 
01444 /*
01445  * Initialize for a Huffman-compressed scan.
01446  * If gather_statistics is TRUE, we do not output anything during the scan,
01447  * just count the Huffman symbols used and generate Huffman code tables.
01448  */
01449 
01450 METHODDEF(void)
01451 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
01452 {
01453   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
01454   int ci, tbl;
01455   jpeg_component_info * compptr;
01456 
01457   if (gather_statistics)
01458     entropy->pub.finish_pass = finish_pass_gather;
01459   else
01460     entropy->pub.finish_pass = finish_pass_huff;
01461 
01462   if (cinfo->progressive_mode) {
01463     entropy->cinfo = cinfo;
01464     entropy->gather_statistics = gather_statistics;
01465 
01466     /* We assume jcmaster.c already validated the scan parameters. */
01467 
01468     /* Select execution routine */
01469     if (cinfo->Ah == 0) {
01470       if (cinfo->Ss == 0)
01471         entropy->pub.encode_mcu = encode_mcu_DC_first;
01472       else
01473         entropy->pub.encode_mcu = encode_mcu_AC_first;
01474     } else {
01475       if (cinfo->Ss == 0)
01476         entropy->pub.encode_mcu = encode_mcu_DC_refine;
01477       else {
01478         entropy->pub.encode_mcu = encode_mcu_AC_refine;
01479         /* AC refinement needs a correction bit buffer */
01480         if (entropy->bit_buffer == NULL)
01481           entropy->bit_buffer = (char *)
01482             (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
01483                                         MAX_CORR_BITS * SIZEOF(char));
01484       }
01485     }
01486 
01487     /* Initialize AC stuff */
01488     entropy->ac_tbl_no = cinfo->cur_comp_info[0]->ac_tbl_no;
01489     entropy->EOBRUN = 0;
01490     entropy->BE = 0;
01491   } else {
01492     if (gather_statistics)
01493       entropy->pub.encode_mcu = encode_mcu_gather;
01494     else
01495       entropy->pub.encode_mcu = encode_mcu_huff;
01496   }
01497 
01498   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
01499     compptr = cinfo->cur_comp_info[ci];
01500     /* DC needs no table for refinement scan */
01501     if (cinfo->Ss == 0 && cinfo->Ah == 0) {
01502       tbl = compptr->dc_tbl_no;
01503       if (gather_statistics) {
01504         /* Check for invalid table index */
01505         /* (make_c_derived_tbl does this in the other path) */
01506         if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
01507           ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
01508         /* Allocate and zero the statistics tables */
01509         /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
01510         if (entropy->dc_count_ptrs[tbl] == NULL)
01511           entropy->dc_count_ptrs[tbl] = (long *)
01512             (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
01513                                         257 * SIZEOF(long));
01514         MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long));
01515       } else {
01516         /* Compute derived values for Huffman tables */
01517         /* We may do this more than once for a table, but it's not expensive */
01518         jpeg_make_c_derived_tbl(cinfo, TRUE, tbl,
01519                                 & entropy->dc_derived_tbls[tbl]);
01520       }
01521       /* Initialize DC predictions to 0 */
01522       entropy->saved.last_dc_val[ci] = 0;
01523     }
01524     /* AC needs no table when not present */
01525     if (cinfo->Se) {
01526       tbl = compptr->ac_tbl_no;
01527       if (gather_statistics) {
01528         if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
01529           ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
01530         if (entropy->ac_count_ptrs[tbl] == NULL)
01531           entropy->ac_count_ptrs[tbl] = (long *)
01532             (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
01533                                         257 * SIZEOF(long));
01534         MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long));
01535       } else {
01536         jpeg_make_c_derived_tbl(cinfo, FALSE, tbl,
01537                                 & entropy->ac_derived_tbls[tbl]);
01538       }
01539     }
01540   }
01541 
01542   /* Initialize bit buffer to empty */
01543   entropy->saved.put_buffer = 0;
01544   entropy->saved.put_bits = 0;
01545 
01546   /* Initialize restart stuff */
01547   entropy->restarts_to_go = cinfo->restart_interval;
01548   entropy->next_restart_num = 0;
01549 }
01550 
01551 
01552 /*
01553  * Module initialization routine for Huffman entropy encoding.
01554  */
01555 
01556 GLOBAL(void)
01557 jinit_huff_encoder (j_compress_ptr cinfo)
01558 {
01559   huff_entropy_ptr entropy;
01560   int i;
01561 
01562   entropy = (huff_entropy_ptr)
01563     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
01564                                 SIZEOF(huff_entropy_encoder));
01565   cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
01566   entropy->pub.start_pass = start_pass_huff;
01567 
01568   /* Mark tables unallocated */
01569   for (i = 0; i < NUM_HUFF_TBLS; i++) {
01570     entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
01571     entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
01572   }
01573 
01574   if (cinfo->progressive_mode)
01575     entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
01576 }

Generated on Tue Jul 5 14:13:28 2011 for ROOT_528-00b_version by  doxygen 1.5.1