| OLD | NEW | 
|---|
| 1 /* | 1 /* | 
| 2  * jcphuff.c | 2  * jcphuff.c | 
| 3  * | 3  * | 
|  | 4  * This file was part of the Independent JPEG Group's software: | 
| 4  * Copyright (C) 1995-1997, Thomas G. Lane. | 5  * Copyright (C) 1995-1997, Thomas G. Lane. | 
| 5  * This file is part of the Independent JPEG Group's software. | 6  * libjpeg-turbo Modifications: | 
| 6  * For conditions of distribution and use, see the accompanying README file. | 7  * Copyright (C) 2015, D. R. Commander. | 
|  | 8  * For conditions of distribution and use, see the accompanying README.ijg | 
|  | 9  * file. | 
| 7  * | 10  * | 
| 8  * This file contains Huffman entropy encoding routines for progressive JPEG. | 11  * This file contains Huffman entropy encoding routines for progressive JPEG. | 
| 9  * | 12  * | 
| 10  * We do not support output suspension in this module, since the library | 13  * We do not support output suspension in this module, since the library | 
| 11  * currently does not allow multiple-scan files to be written with output | 14  * currently does not allow multiple-scan files to be written with output | 
| 12  * suspension. | 15  * suspension. | 
| 13  */ | 16  */ | 
| 14 | 17 | 
| 15 #define JPEG_INTERNALS | 18 #define JPEG_INTERNALS | 
| 16 #include "jinclude.h" | 19 #include "jinclude.h" | 
| 17 #include "jpeglib.h" | 20 #include "jpeglib.h" | 
| 18 #include "jchuff.h"»    »       /* Declarations shared with jchuff.c */ | 21 #include "jchuff.h"             /* Declarations shared with jchuff.c */ | 
| 19 | 22 | 
| 20 #ifdef C_PROGRESSIVE_SUPPORTED | 23 #ifdef C_PROGRESSIVE_SUPPORTED | 
| 21 | 24 | 
| 22 /* Expanded entropy encoder object for progressive Huffman encoding. */ | 25 /* Expanded entropy encoder object for progressive Huffman encoding. */ | 
| 23 | 26 | 
| 24 typedef struct { | 27 typedef struct { | 
| 25   struct jpeg_entropy_encoder pub; /* public fields */ | 28   struct jpeg_entropy_encoder pub; /* public fields */ | 
| 26 | 29 | 
| 27   /* Mode flag: TRUE for optimization, FALSE for actual data output */ | 30   /* Mode flag: TRUE for optimization, FALSE for actual data output */ | 
| 28   boolean gather_statistics; | 31   boolean gather_statistics; | 
| 29 | 32 | 
| 30   /* Bit-level coding status. | 33   /* Bit-level coding status. | 
| 31    * next_output_byte/free_in_buffer are local copies of cinfo->dest fields. | 34    * next_output_byte/free_in_buffer are local copies of cinfo->dest fields. | 
| 32    */ | 35    */ | 
| 33   JOCTET * next_output_byte;»   /* => next byte to write in buffer */ | 36   JOCTET *next_output_byte;     /* => next byte to write in buffer */ | 
| 34   size_t free_in_buffer;»       /* # of byte spaces remaining in buffer */ | 37   size_t free_in_buffer;        /* # of byte spaces remaining in buffer */ | 
| 35   INT32 put_buffer;»    »       /* current bit-accumulation buffer */ | 38   size_t put_buffer;            /* current bit-accumulation buffer */ | 
| 36   int put_bits;»»       »       /* # of bits now in it */ | 39   int put_bits;                 /* # of bits now in it */ | 
| 37   j_compress_ptr cinfo;»»       /* link to cinfo (needed for dump_buffer) */ | 40   j_compress_ptr cinfo;         /* link to cinfo (needed for dump_buffer) */ | 
| 38 | 41 | 
| 39   /* Coding status for DC components */ | 42   /* Coding status for DC components */ | 
| 40   int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ | 43   int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ | 
| 41 | 44 | 
| 42   /* Coding status for AC components */ | 45   /* Coding status for AC components */ | 
| 43   int ac_tbl_no;»       »       /* the table number of the single component */ | 46   int ac_tbl_no;                /* the table number of the single component */ | 
| 44   unsigned int EOBRUN;» »       /* run length of EOBs */ | 47   unsigned int EOBRUN;          /* run length of EOBs */ | 
| 45   unsigned int BE;»     »       /* # of buffered correction bits before MCU */ | 48   unsigned int BE;              /* # of buffered correction bits before MCU */ | 
| 46   char * bit_buffer;»   »       /* buffer for correction bits (1 per char) */ | 49   char *bit_buffer;             /* buffer for correction bits (1 per char) */ | 
| 47   /* packing correction bits tightly would save some space but cost time... */ | 50   /* packing correction bits tightly would save some space but cost time... */ | 
| 48 | 51 | 
| 49   unsigned int restarts_to_go;» /* MCUs left in this restart interval */ | 52   unsigned int restarts_to_go;  /* MCUs left in this restart interval */ | 
| 50   int next_restart_num;»»       /* next restart number to write (0-7) */ | 53   int next_restart_num;         /* next restart number to write (0-7) */ | 
| 51 | 54 | 
| 52   /* Pointers to derived tables (these workspaces have image lifespan). | 55   /* Pointers to derived tables (these workspaces have image lifespan). | 
| 53    * Since any one scan codes only DC or only AC, we only need one set | 56    * Since any one scan codes only DC or only AC, we only need one set | 
| 54    * of tables, not one for DC and one for AC. | 57    * of tables, not one for DC and one for AC. | 
| 55    */ | 58    */ | 
| 56   c_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; | 59   c_derived_tbl *derived_tbls[NUM_HUFF_TBLS]; | 
| 57 | 60 | 
| 58   /* Statistics tables for optimization; again, one set is enough */ | 61   /* Statistics tables for optimization; again, one set is enough */ | 
| 59   long * count_ptrs[NUM_HUFF_TBLS]; | 62   long *count_ptrs[NUM_HUFF_TBLS]; | 
| 60 } phuff_entropy_encoder; | 63 } phuff_entropy_encoder; | 
| 61 | 64 | 
| 62 typedef phuff_entropy_encoder * phuff_entropy_ptr; | 65 typedef phuff_entropy_encoder *phuff_entropy_ptr; | 
| 63 | 66 | 
| 64 /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit | 67 /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit | 
| 65  * buffer can hold.  Larger sizes may slightly improve compression, but | 68  * buffer can hold.  Larger sizes may slightly improve compression, but | 
| 66  * 1000 is already well into the realm of overkill. | 69  * 1000 is already well into the realm of overkill. | 
| 67  * The minimum safe size is 64 bits. | 70  * The minimum safe size is 64 bits. | 
| 68  */ | 71  */ | 
| 69 | 72 | 
| 70 #define MAX_CORR_BITS  1000»    /* Max # of correction bits I can buffer */ | 73 #define MAX_CORR_BITS  1000     /* Max # of correction bits I can buffer */ | 
| 71 | 74 | 
| 72 /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32. | 75 /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG. | 
| 73  * We assume that int right shift is unsigned if INT32 right shift is, | 76  * We assume that int right shift is unsigned if JLONG right shift is, | 
| 74  * which should be safe. | 77  * which should be safe. | 
| 75  */ | 78  */ | 
| 76 | 79 | 
| 77 #ifdef RIGHT_SHIFT_IS_UNSIGNED | 80 #ifdef RIGHT_SHIFT_IS_UNSIGNED | 
| 78 #define ISHIFT_TEMPS»   int ishift_temp; | 81 #define ISHIFT_TEMPS    int ishift_temp; | 
| 79 #define IRIGHT_SHIFT(x,shft)  \ | 82 #define IRIGHT_SHIFT(x,shft)  \ | 
| 80 »       ((ishift_temp = (x)) < 0 ? \ | 83         ((ishift_temp = (x)) < 0 ? \ | 
| 81 »        (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ | 84          (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \ | 
| 82 »        (ishift_temp >> (shft))) | 85          (ishift_temp >> (shft))) | 
| 83 #else | 86 #else | 
| 84 #define ISHIFT_TEMPS | 87 #define ISHIFT_TEMPS | 
| 85 #define IRIGHT_SHIFT(x,shft)»   ((x) >> (shft)) | 88 #define IRIGHT_SHIFT(x,shft)    ((x) >> (shft)) | 
| 86 #endif | 89 #endif | 
| 87 | 90 | 
| 88 /* Forward declarations */ | 91 /* Forward declarations */ | 
| 89 METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo, | 92 METHODDEF(boolean) encode_mcu_DC_first (j_compress_ptr cinfo, | 
| 90 »       »       »       »       »           JBLOCKROW *MCU_data)); | 93                                         JBLOCKROW *MCU_data); | 
| 91 METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo, | 94 METHODDEF(boolean) encode_mcu_AC_first (j_compress_ptr cinfo, | 
| 92 »       »       »       »       »           JBLOCKROW *MCU_data)); | 95                                         JBLOCKROW *MCU_data); | 
| 93 METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo, | 96 METHODDEF(boolean) encode_mcu_DC_refine (j_compress_ptr cinfo, | 
| 94 »       »       »       »       »            JBLOCKROW *MCU_data)); | 97                                          JBLOCKROW *MCU_data); | 
| 95 METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo, | 98 METHODDEF(boolean) encode_mcu_AC_refine (j_compress_ptr cinfo, | 
| 96 »       »       »       »       »            JBLOCKROW *MCU_data)); | 99                                          JBLOCKROW *MCU_data); | 
| 97 METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo)); | 100 METHODDEF(void) finish_pass_phuff (j_compress_ptr cinfo); | 
| 98 METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo)); | 101 METHODDEF(void) finish_pass_gather_phuff (j_compress_ptr cinfo); | 
| 99 | 102 | 
| 100 | 103 | 
| 101 /* | 104 /* | 
| 102  * Initialize for a Huffman-compressed scan using progressive JPEG. | 105  * Initialize for a Huffman-compressed scan using progressive JPEG. | 
| 103  */ | 106  */ | 
| 104 | 107 | 
| 105 METHODDEF(void) | 108 METHODDEF(void) | 
| 106 start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics) | 109 start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics) | 
| 107 { | 110 { | 
| 108   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; | 111   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; | 
| 109   boolean is_DC_band; | 112   boolean is_DC_band; | 
| 110   int ci, tbl; | 113   int ci, tbl; | 
| 111   jpeg_component_info * compptr; | 114   jpeg_component_info *compptr; | 
| 112 | 115 | 
| 113   entropy->cinfo = cinfo; | 116   entropy->cinfo = cinfo; | 
| 114   entropy->gather_statistics = gather_statistics; | 117   entropy->gather_statistics = gather_statistics; | 
| 115 | 118 | 
| 116   is_DC_band = (cinfo->Ss == 0); | 119   is_DC_band = (cinfo->Ss == 0); | 
| 117 | 120 | 
| 118   /* We assume jcmaster.c already validated the scan parameters. */ | 121   /* We assume jcmaster.c already validated the scan parameters. */ | 
| 119 | 122 | 
| 120   /* Select execution routines */ | 123   /* Select execution routines */ | 
| 121   if (cinfo->Ah == 0) { | 124   if (cinfo->Ah == 0) { | 
| 122     if (is_DC_band) | 125     if (is_DC_band) | 
| 123       entropy->pub.encode_mcu = encode_mcu_DC_first; | 126       entropy->pub.encode_mcu = encode_mcu_DC_first; | 
| 124     else | 127     else | 
| 125       entropy->pub.encode_mcu = encode_mcu_AC_first; | 128       entropy->pub.encode_mcu = encode_mcu_AC_first; | 
| 126   } else { | 129   } else { | 
| 127     if (is_DC_band) | 130     if (is_DC_band) | 
| 128       entropy->pub.encode_mcu = encode_mcu_DC_refine; | 131       entropy->pub.encode_mcu = encode_mcu_DC_refine; | 
| 129     else { | 132     else { | 
| 130       entropy->pub.encode_mcu = encode_mcu_AC_refine; | 133       entropy->pub.encode_mcu = encode_mcu_AC_refine; | 
| 131       /* AC refinement needs a correction bit buffer */ | 134       /* AC refinement needs a correction bit buffer */ | 
| 132       if (entropy->bit_buffer == NULL) | 135       if (entropy->bit_buffer == NULL) | 
| 133 »       entropy->bit_buffer = (char *) | 136         entropy->bit_buffer = (char *) | 
| 134 »         (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 137           (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 
| 135 »       »       »       »             MAX_CORR_BITS * SIZEOF(char)); | 138                                       MAX_CORR_BITS * sizeof(char)); | 
| 136     } | 139     } | 
| 137   } | 140   } | 
| 138   if (gather_statistics) | 141   if (gather_statistics) | 
| 139     entropy->pub.finish_pass = finish_pass_gather_phuff; | 142     entropy->pub.finish_pass = finish_pass_gather_phuff; | 
| 140   else | 143   else | 
| 141     entropy->pub.finish_pass = finish_pass_phuff; | 144     entropy->pub.finish_pass = finish_pass_phuff; | 
| 142 | 145 | 
| 143   /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1 | 146   /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1 | 
| 144    * for AC coefficients. | 147    * for AC coefficients. | 
| 145    */ | 148    */ | 
| 146   for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 149   for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 
| 147     compptr = cinfo->cur_comp_info[ci]; | 150     compptr = cinfo->cur_comp_info[ci]; | 
| 148     /* Initialize DC predictions to 0 */ | 151     /* Initialize DC predictions to 0 */ | 
| 149     entropy->last_dc_val[ci] = 0; | 152     entropy->last_dc_val[ci] = 0; | 
| 150     /* Get table index */ | 153     /* Get table index */ | 
| 151     if (is_DC_band) { | 154     if (is_DC_band) { | 
| 152       if (cinfo->Ah != 0)»      /* DC refinement needs no table */ | 155       if (cinfo->Ah != 0)       /* DC refinement needs no table */ | 
| 153 »       continue; | 156         continue; | 
| 154       tbl = compptr->dc_tbl_no; | 157       tbl = compptr->dc_tbl_no; | 
| 155     } else { | 158     } else { | 
| 156       entropy->ac_tbl_no = tbl = compptr->ac_tbl_no; | 159       entropy->ac_tbl_no = tbl = compptr->ac_tbl_no; | 
| 157     } | 160     } | 
| 158     if (gather_statistics) { | 161     if (gather_statistics) { | 
| 159       /* Check for invalid table index */ | 162       /* Check for invalid table index */ | 
| 160       /* (make_c_derived_tbl does this in the other path) */ | 163       /* (make_c_derived_tbl does this in the other path) */ | 
| 161       if (tbl < 0 || tbl >= NUM_HUFF_TBLS) | 164       if (tbl < 0 || tbl >= NUM_HUFF_TBLS) | 
| 162         ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); | 165         ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); | 
| 163       /* Allocate and zero the statistics tables */ | 166       /* Allocate and zero the statistics tables */ | 
| 164       /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ | 167       /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ | 
| 165       if (entropy->count_ptrs[tbl] == NULL) | 168       if (entropy->count_ptrs[tbl] == NULL) | 
| 166 »       entropy->count_ptrs[tbl] = (long *) | 169         entropy->count_ptrs[tbl] = (long *) | 
| 167 »         (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 170           (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 
| 168 »       »       »       »             257 * SIZEOF(long)); | 171                                       257 * sizeof(long)); | 
| 169       MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long)); | 172       MEMZERO(entropy->count_ptrs[tbl], 257 * sizeof(long)); | 
| 170     } else { | 173     } else { | 
| 171       /* Compute derived values for Huffman table */ | 174       /* Compute derived values for Huffman table */ | 
| 172       /* We may do this more than once for a table, but it's not expensive */ | 175       /* We may do this more than once for a table, but it's not expensive */ | 
| 173       jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl, | 176       jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl, | 
| 174 »       »       »             & entropy->derived_tbls[tbl]); | 177                               & entropy->derived_tbls[tbl]); | 
| 175     } | 178     } | 
| 176   } | 179   } | 
| 177 | 180 | 
| 178   /* Initialize AC stuff */ | 181   /* Initialize AC stuff */ | 
| 179   entropy->EOBRUN = 0; | 182   entropy->EOBRUN = 0; | 
| 180   entropy->BE = 0; | 183   entropy->BE = 0; | 
| 181 | 184 | 
| 182   /* Initialize bit buffer to empty */ | 185   /* Initialize bit buffer to empty */ | 
| 183   entropy->put_buffer = 0; | 186   entropy->put_buffer = 0; | 
| 184   entropy->put_bits = 0; | 187   entropy->put_bits = 0; | 
| 185 | 188 | 
| 186   /* Initialize restart stuff */ | 189   /* Initialize restart stuff */ | 
| 187   entropy->restarts_to_go = cinfo->restart_interval; | 190   entropy->restarts_to_go = cinfo->restart_interval; | 
| 188   entropy->next_restart_num = 0; | 191   entropy->next_restart_num = 0; | 
| 189 } | 192 } | 
| 190 | 193 | 
| 191 | 194 | 
| 192 /* Outputting bytes to the file. | 195 /* Outputting bytes to the file. | 
| 193  * NB: these must be called only when actually outputting, | 196  * NB: these must be called only when actually outputting, | 
| 194  * that is, entropy->gather_statistics == FALSE. | 197  * that is, entropy->gather_statistics == FALSE. | 
| 195  */ | 198  */ | 
| 196 | 199 | 
| 197 /* Emit a byte */ | 200 /* Emit a byte */ | 
| 198 #define emit_byte(entropy,val)  \ | 201 #define emit_byte(entropy,val)  \ | 
| 199 »       { *(entropy)->next_output_byte++ = (JOCTET) (val);  \ | 202         { *(entropy)->next_output_byte++ = (JOCTET) (val);  \ | 
| 200 »         if (--(entropy)->free_in_buffer == 0)  \ | 203           if (--(entropy)->free_in_buffer == 0)  \ | 
| 201 »           dump_buffer(entropy); } | 204             dump_buffer(entropy); } | 
| 202 | 205 | 
| 203 | 206 | 
| 204 LOCAL(void) | 207 LOCAL(void) | 
| 205 dump_buffer (phuff_entropy_ptr entropy) | 208 dump_buffer (phuff_entropy_ptr entropy) | 
| 206 /* Empty the output buffer; we do not support suspension in this module. */ | 209 /* Empty the output buffer; we do not support suspension in this module. */ | 
| 207 { | 210 { | 
| 208   struct jpeg_destination_mgr * dest = entropy->cinfo->dest; | 211   struct jpeg_destination_mgr *dest = entropy->cinfo->dest; | 
| 209 | 212 | 
| 210   if (! (*dest->empty_output_buffer) (entropy->cinfo)) | 213   if (! (*dest->empty_output_buffer) (entropy->cinfo)) | 
| 211     ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND); | 214     ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND); | 
| 212   /* After a successful buffer dump, must reset buffer pointers */ | 215   /* After a successful buffer dump, must reset buffer pointers */ | 
| 213   entropy->next_output_byte = dest->next_output_byte; | 216   entropy->next_output_byte = dest->next_output_byte; | 
| 214   entropy->free_in_buffer = dest->free_in_buffer; | 217   entropy->free_in_buffer = dest->free_in_buffer; | 
| 215 } | 218 } | 
| 216 | 219 | 
| 217 | 220 | 
| 218 /* Outputting bits to the file */ | 221 /* Outputting bits to the file */ | 
| 219 | 222 | 
| 220 /* Only the right 24 bits of put_buffer are used; the valid bits are | 223 /* Only the right 24 bits of put_buffer are used; the valid bits are | 
| 221  * left-justified in this part.  At most 16 bits can be passed to emit_bits | 224  * left-justified in this part.  At most 16 bits can be passed to emit_bits | 
| 222  * in one call, and we never retain more than 7 bits in put_buffer | 225  * in one call, and we never retain more than 7 bits in put_buffer | 
| 223  * between calls, so 24 bits are sufficient. | 226  * between calls, so 24 bits are sufficient. | 
| 224  */ | 227  */ | 
| 225 | 228 | 
| 226 LOCAL(void) | 229 LOCAL(void) | 
| 227 emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size) | 230 emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size) | 
| 228 /* Emit some bits, unless we are in gather mode */ | 231 /* Emit some bits, unless we are in gather mode */ | 
| 229 { | 232 { | 
| 230   /* This routine is heavily used, so it's worth coding tightly. */ | 233   /* This routine is heavily used, so it's worth coding tightly. */ | 
| 231   register INT32 put_buffer = (INT32) code; | 234   register size_t put_buffer = (size_t) code; | 
| 232   register int put_bits = entropy->put_bits; | 235   register int put_bits = entropy->put_bits; | 
| 233 | 236 | 
| 234   /* if size is 0, caller used an invalid Huffman table entry */ | 237   /* if size is 0, caller used an invalid Huffman table entry */ | 
| 235   if (size == 0) | 238   if (size == 0) | 
| 236     ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); | 239     ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); | 
| 237 | 240 | 
| 238   if (entropy->gather_statistics) | 241   if (entropy->gather_statistics) | 
| 239     return;»    »       »       /* do nothing if we're only getting stats */ | 242     return;                     /* do nothing if we're only getting stats */ | 
| 240 | 243 | 
| 241   put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */ | 244   put_buffer &= (((size_t) 1)<<size) - 1; /* mask off any extra bits in code */ | 
| 242 | 245 | 
| 243   put_bits += size;»    »       /* new number of bits in buffer */ | 246   put_bits += size;             /* new number of bits in buffer */ | 
| 244 | 247 | 
| 245   put_buffer <<= 24 - put_bits; /* align incoming bits */ | 248   put_buffer <<= 24 - put_bits; /* align incoming bits */ | 
| 246 | 249 | 
| 247   put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */ | 250   put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */ | 
| 248 | 251 | 
| 249   while (put_bits >= 8) { | 252   while (put_bits >= 8) { | 
| 250     int c = (int) ((put_buffer >> 16) & 0xFF); | 253     int c = (int) ((put_buffer >> 16) & 0xFF); | 
| 251 | 254 | 
| 252     emit_byte(entropy, c); | 255     emit_byte(entropy, c); | 
| 253     if (c == 0xFF) {»   »       /* need to stuff a zero byte? */ | 256     if (c == 0xFF) {            /* need to stuff a zero byte? */ | 
| 254       emit_byte(entropy, 0); | 257       emit_byte(entropy, 0); | 
| 255     } | 258     } | 
| 256     put_buffer <<= 8; | 259     put_buffer <<= 8; | 
| 257     put_bits -= 8; | 260     put_bits -= 8; | 
| 258   } | 261   } | 
| 259 | 262 | 
| 260   entropy->put_buffer = put_buffer; /* update variables */ | 263   entropy->put_buffer = put_buffer; /* update variables */ | 
| 261   entropy->put_bits = put_bits; | 264   entropy->put_bits = put_bits; | 
| 262 } | 265 } | 
| 263 | 266 | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 274 /* | 277 /* | 
| 275  * Emit (or just count) a Huffman symbol. | 278  * Emit (or just count) a Huffman symbol. | 
| 276  */ | 279  */ | 
| 277 | 280 | 
| 278 LOCAL(void) | 281 LOCAL(void) | 
| 279 emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol) | 282 emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol) | 
| 280 { | 283 { | 
| 281   if (entropy->gather_statistics) | 284   if (entropy->gather_statistics) | 
| 282     entropy->count_ptrs[tbl_no][symbol]++; | 285     entropy->count_ptrs[tbl_no][symbol]++; | 
| 283   else { | 286   else { | 
| 284     c_derived_tbl * tbl = entropy->derived_tbls[tbl_no]; | 287     c_derived_tbl *tbl = entropy->derived_tbls[tbl_no]; | 
| 285     emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); | 288     emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]); | 
| 286   } | 289   } | 
| 287 } | 290 } | 
| 288 | 291 | 
| 289 | 292 | 
| 290 /* | 293 /* | 
| 291  * Emit bits from a correction bit buffer. | 294  * Emit bits from a correction bit buffer. | 
| 292  */ | 295  */ | 
| 293 | 296 | 
| 294 LOCAL(void) | 297 LOCAL(void) | 
| 295 emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart, | 298 emit_buffered_bits (phuff_entropy_ptr entropy, char *bufstart, | 
| 296 »       »           unsigned int nbits) | 299                     unsigned int nbits) | 
| 297 { | 300 { | 
| 298   if (entropy->gather_statistics) | 301   if (entropy->gather_statistics) | 
| 299     return;»    »       »       /* no real work */ | 302     return;                     /* no real work */ | 
| 300 | 303 | 
| 301   while (nbits > 0) { | 304   while (nbits > 0) { | 
| 302     emit_bits(entropy, (unsigned int) (*bufstart), 1); | 305     emit_bits(entropy, (unsigned int) (*bufstart), 1); | 
| 303     bufstart++; | 306     bufstart++; | 
| 304     nbits--; | 307     nbits--; | 
| 305   } | 308   } | 
| 306 } | 309 } | 
| 307 | 310 | 
| 308 | 311 | 
| 309 /* | 312 /* | 
| 310  * Emit any pending EOBRUN symbol. | 313  * Emit any pending EOBRUN symbol. | 
| 311  */ | 314  */ | 
| 312 | 315 | 
| 313 LOCAL(void) | 316 LOCAL(void) | 
| 314 emit_eobrun (phuff_entropy_ptr entropy) | 317 emit_eobrun (phuff_entropy_ptr entropy) | 
| 315 { | 318 { | 
| 316   register int temp, nbits; | 319   register int temp, nbits; | 
| 317 | 320 | 
| 318   if (entropy->EOBRUN > 0) {»   /* if there is any pending EOBRUN */ | 321   if (entropy->EOBRUN > 0) {    /* if there is any pending EOBRUN */ | 
| 319     temp = entropy->EOBRUN; | 322     temp = entropy->EOBRUN; | 
| 320     nbits = 0; | 323     nbits = 0; | 
| 321     while ((temp >>= 1)) | 324     while ((temp >>= 1)) | 
| 322       nbits++; | 325       nbits++; | 
| 323     /* safety check: shouldn't happen given limited correction-bit buffer */ | 326     /* safety check: shouldn't happen given limited correction-bit buffer */ | 
| 324     if (nbits > 14) | 327     if (nbits > 14) | 
| 325       ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); | 328       ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE); | 
| 326 | 329 | 
| 327     emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4); | 330     emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4); | 
| 328     if (nbits) | 331     if (nbits) | 
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 373 | 376 | 
| 374 METHODDEF(boolean) | 377 METHODDEF(boolean) | 
| 375 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) | 378 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data) | 
| 376 { | 379 { | 
| 377   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; | 380   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; | 
| 378   register int temp, temp2; | 381   register int temp, temp2; | 
| 379   register int nbits; | 382   register int nbits; | 
| 380   int blkn, ci; | 383   int blkn, ci; | 
| 381   int Al = cinfo->Al; | 384   int Al = cinfo->Al; | 
| 382   JBLOCKROW block; | 385   JBLOCKROW block; | 
| 383   jpeg_component_info * compptr; | 386   jpeg_component_info *compptr; | 
| 384   ISHIFT_TEMPS | 387   ISHIFT_TEMPS | 
| 385 | 388 | 
| 386   entropy->next_output_byte = cinfo->dest->next_output_byte; | 389   entropy->next_output_byte = cinfo->dest->next_output_byte; | 
| 387   entropy->free_in_buffer = cinfo->dest->free_in_buffer; | 390   entropy->free_in_buffer = cinfo->dest->free_in_buffer; | 
| 388 | 391 | 
| 389   /* Emit restart marker if needed */ | 392   /* Emit restart marker if needed */ | 
| 390   if (cinfo->restart_interval) | 393   if (cinfo->restart_interval) | 
| 391     if (entropy->restarts_to_go == 0) | 394     if (entropy->restarts_to_go == 0) | 
| 392       emit_restart(entropy, entropy->next_restart_num); | 395       emit_restart(entropy, entropy->next_restart_num); | 
| 393 | 396 | 
| 394   /* Encode the MCU data blocks */ | 397   /* Encode the MCU data blocks */ | 
| 395   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | 398   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | 
| 396     block = MCU_data[blkn]; | 399     block = MCU_data[blkn]; | 
| 397     ci = cinfo->MCU_membership[blkn]; | 400     ci = cinfo->MCU_membership[blkn]; | 
| 398     compptr = cinfo->cur_comp_info[ci]; | 401     compptr = cinfo->cur_comp_info[ci]; | 
| 399 | 402 | 
| 400     /* Compute the DC value after the required point transform by Al. | 403     /* Compute the DC value after the required point transform by Al. | 
| 401      * This is simply an arithmetic right shift. | 404      * This is simply an arithmetic right shift. | 
| 402      */ | 405      */ | 
| 403     temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al); | 406     temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al); | 
| 404 | 407 | 
| 405     /* DC differences are figured on the point-transformed values. */ | 408     /* DC differences are figured on the point-transformed values. */ | 
| 406     temp = temp2 - entropy->last_dc_val[ci]; | 409     temp = temp2 - entropy->last_dc_val[ci]; | 
| 407     entropy->last_dc_val[ci] = temp2; | 410     entropy->last_dc_val[ci] = temp2; | 
| 408 | 411 | 
| 409     /* Encode the DC coefficient difference per section G.1.2.1 */ | 412     /* Encode the DC coefficient difference per section G.1.2.1 */ | 
| 410     temp2 = temp; | 413     temp2 = temp; | 
| 411     if (temp < 0) { | 414     if (temp < 0) { | 
| 412       temp = -temp;»    »       /* temp is abs value of input */ | 415       temp = -temp;             /* temp is abs value of input */ | 
| 413       /* For a negative input, want temp2 = bitwise complement of abs(input) */ | 416       /* For a negative input, want temp2 = bitwise complement of abs(input) */ | 
| 414       /* This code assumes we are on a two's complement machine */ | 417       /* This code assumes we are on a two's complement machine */ | 
| 415       temp2--; | 418       temp2--; | 
| 416     } | 419     } | 
| 417 | 420 | 
| 418     /* Find the number of bits needed for the magnitude of the coefficient */ | 421     /* Find the number of bits needed for the magnitude of the coefficient */ | 
| 419     nbits = 0; | 422     nbits = 0; | 
| 420     while (temp) { | 423     while (temp) { | 
| 421       nbits++; | 424       nbits++; | 
| 422       temp >>= 1; | 425       temp >>= 1; | 
| 423     } | 426     } | 
| 424     /* Check for out-of-range coefficient values. | 427     /* Check for out-of-range coefficient values. | 
| 425      * Since we're encoding a difference, the range limit is twice as much. | 428      * Since we're encoding a difference, the range limit is twice as much. | 
| 426      */ | 429      */ | 
| 427     if (nbits > MAX_COEF_BITS+1) | 430     if (nbits > MAX_COEF_BITS+1) | 
| 428       ERREXIT(cinfo, JERR_BAD_DCT_COEF); | 431       ERREXIT(cinfo, JERR_BAD_DCT_COEF); | 
| 429 | 432 | 
| 430     /* Count/emit the Huffman-coded symbol for the number of bits */ | 433     /* Count/emit the Huffman-coded symbol for the number of bits */ | 
| 431     emit_symbol(entropy, compptr->dc_tbl_no, nbits); | 434     emit_symbol(entropy, compptr->dc_tbl_no, nbits); | 
| 432 | 435 | 
| 433     /* Emit that number of bits of the value, if positive, */ | 436     /* Emit that number of bits of the value, if positive, */ | 
| 434     /* or the complement of its magnitude, if negative. */ | 437     /* or the complement of its magnitude, if negative. */ | 
| 435     if (nbits)» »       »       /* emit_bits rejects calls with size 0 */ | 438     if (nbits)                  /* emit_bits rejects calls with size 0 */ | 
| 436       emit_bits(entropy, (unsigned int) temp2, nbits); | 439       emit_bits(entropy, (unsigned int) temp2, nbits); | 
| 437   } | 440   } | 
| 438 | 441 | 
| 439   cinfo->dest->next_output_byte = entropy->next_output_byte; | 442   cinfo->dest->next_output_byte = entropy->next_output_byte; | 
| 440   cinfo->dest->free_in_buffer = entropy->free_in_buffer; | 443   cinfo->dest->free_in_buffer = entropy->free_in_buffer; | 
| 441 | 444 | 
| 442   /* Update restart-interval state too */ | 445   /* Update restart-interval state too */ | 
| 443   if (cinfo->restart_interval) { | 446   if (cinfo->restart_interval) { | 
| 444     if (entropy->restarts_to_go == 0) { | 447     if (entropy->restarts_to_go == 0) { | 
| 445       entropy->restarts_to_go = cinfo->restart_interval; | 448       entropy->restarts_to_go = cinfo->restart_interval; | 
| (...skipping 28 matching lines...) Expand all  Loading... | 
| 474 | 477 | 
| 475   /* Emit restart marker if needed */ | 478   /* Emit restart marker if needed */ | 
| 476   if (cinfo->restart_interval) | 479   if (cinfo->restart_interval) | 
| 477     if (entropy->restarts_to_go == 0) | 480     if (entropy->restarts_to_go == 0) | 
| 478       emit_restart(entropy, entropy->next_restart_num); | 481       emit_restart(entropy, entropy->next_restart_num); | 
| 479 | 482 | 
| 480   /* Encode the MCU data block */ | 483   /* Encode the MCU data block */ | 
| 481   block = MCU_data[0]; | 484   block = MCU_data[0]; | 
| 482 | 485 | 
| 483   /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ | 486   /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */ | 
| 484 | 487 | 
| 485   r = 0;»       »       »       /* r = run length of zeros */ | 488   r = 0;                        /* r = run length of zeros */ | 
| 486 | 489 | 
| 487   for (k = cinfo->Ss; k <= Se; k++) { | 490   for (k = cinfo->Ss; k <= Se; k++) { | 
| 488     if ((temp = (*block)[jpeg_natural_order[k]]) == 0) { | 491     if ((temp = (*block)[jpeg_natural_order[k]]) == 0) { | 
| 489       r++; | 492       r++; | 
| 490       continue; | 493       continue; | 
| 491     } | 494     } | 
| 492     /* We must apply the point transform by Al.  For AC coefficients this | 495     /* We must apply the point transform by Al.  For AC coefficients this | 
| 493      * is an integer division with rounding towards 0.  To do this portably | 496      * is an integer division with rounding towards 0.  To do this portably | 
| 494      * in C, we shift after obtaining the absolute value; so the code is | 497      * in C, we shift after obtaining the absolute value; so the code is | 
| 495      * interwoven with finding the abs value (temp) and output bits (temp2). | 498      * interwoven with finding the abs value (temp) and output bits (temp2). | 
| 496      */ | 499      */ | 
| 497     if (temp < 0) { | 500     if (temp < 0) { | 
| 498       temp = -temp;»    »       /* temp is abs value of input */ | 501       temp = -temp;             /* temp is abs value of input */ | 
| 499       temp >>= Al;»     »       /* apply the point transform */ | 502       temp >>= Al;              /* apply the point transform */ | 
| 500       /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ | 503       /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ | 
| 501       temp2 = ~temp; | 504       temp2 = ~temp; | 
| 502     } else { | 505     } else { | 
| 503       temp >>= Al;»     »       /* apply the point transform */ | 506       temp >>= Al;              /* apply the point transform */ | 
| 504       temp2 = temp; | 507       temp2 = temp; | 
| 505     } | 508     } | 
| 506     /* Watch out for case that nonzero coef is zero after point transform */ | 509     /* Watch out for case that nonzero coef is zero after point transform */ | 
| 507     if (temp == 0) { | 510     if (temp == 0) { | 
| 508       r++; | 511       r++; | 
| 509       continue; | 512       continue; | 
| 510     } | 513     } | 
| 511 | 514 | 
| 512     /* Emit any pending EOBRUN */ | 515     /* Emit any pending EOBRUN */ | 
| 513     if (entropy->EOBRUN > 0) | 516     if (entropy->EOBRUN > 0) | 
| 514       emit_eobrun(entropy); | 517       emit_eobrun(entropy); | 
| 515     /* if run length > 15, must emit special run-length-16 codes (0xF0) */ | 518     /* if run length > 15, must emit special run-length-16 codes (0xF0) */ | 
| 516     while (r > 15) { | 519     while (r > 15) { | 
| 517       emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); | 520       emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); | 
| 518       r -= 16; | 521       r -= 16; | 
| 519     } | 522     } | 
| 520 | 523 | 
| 521     /* Find the number of bits needed for the magnitude of the coefficient */ | 524     /* Find the number of bits needed for the magnitude of the coefficient */ | 
| 522     nbits = 1;» »       »       /* there must be at least one 1 bit */ | 525     nbits = 1;                  /* there must be at least one 1 bit */ | 
| 523     while ((temp >>= 1)) | 526     while ((temp >>= 1)) | 
| 524       nbits++; | 527       nbits++; | 
| 525     /* Check for out-of-range coefficient values */ | 528     /* Check for out-of-range coefficient values */ | 
| 526     if (nbits > MAX_COEF_BITS) | 529     if (nbits > MAX_COEF_BITS) | 
| 527       ERREXIT(cinfo, JERR_BAD_DCT_COEF); | 530       ERREXIT(cinfo, JERR_BAD_DCT_COEF); | 
| 528 | 531 | 
| 529     /* Count/emit Huffman symbol for run length / number of bits */ | 532     /* Count/emit Huffman symbol for run length / number of bits */ | 
| 530     emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); | 533     emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); | 
| 531 | 534 | 
| 532     /* Emit that number of bits of the value, if positive, */ | 535     /* Emit that number of bits of the value, if positive, */ | 
| 533     /* or the complement of its magnitude, if negative. */ | 536     /* or the complement of its magnitude, if negative. */ | 
| 534     emit_bits(entropy, (unsigned int) temp2, nbits); | 537     emit_bits(entropy, (unsigned int) temp2, nbits); | 
| 535 | 538 | 
| 536     r = 0;»     »       »       /* reset zero run length */ | 539     r = 0;                      /* reset zero run length */ | 
| 537   } | 540   } | 
| 538 | 541 | 
| 539   if (r > 0) {» »       »       /* If there are trailing zeroes, */ | 542   if (r > 0) {                  /* If there are trailing zeroes, */ | 
| 540     entropy->EOBRUN++;» »       /* count an EOB */ | 543     entropy->EOBRUN++;          /* count an EOB */ | 
| 541     if (entropy->EOBRUN == 0x7FFF) | 544     if (entropy->EOBRUN == 0x7FFF) | 
| 542       emit_eobrun(entropy);»    /* force it out to avoid overflow */ | 545       emit_eobrun(entropy);     /* force it out to avoid overflow */ | 
| 543   } | 546   } | 
| 544 | 547 | 
| 545   cinfo->dest->next_output_byte = entropy->next_output_byte; | 548   cinfo->dest->next_output_byte = entropy->next_output_byte; | 
| 546   cinfo->dest->free_in_buffer = entropy->free_in_buffer; | 549   cinfo->dest->free_in_buffer = entropy->free_in_buffer; | 
| 547 | 550 | 
| 548   /* Update restart-interval state too */ | 551   /* Update restart-interval state too */ | 
| 549   if (cinfo->restart_interval) { | 552   if (cinfo->restart_interval) { | 
| 550     if (entropy->restarts_to_go == 0) { | 553     if (entropy->restarts_to_go == 0) { | 
| 551       entropy->restarts_to_go = cinfo->restart_interval; | 554       entropy->restarts_to_go = cinfo->restart_interval; | 
| 552       entropy->next_restart_num++; | 555       entropy->next_restart_num++; | 
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 641    * coefficients' absolute values and the EOB position. | 644    * coefficients' absolute values and the EOB position. | 
| 642    */ | 645    */ | 
| 643   EOB = 0; | 646   EOB = 0; | 
| 644   for (k = cinfo->Ss; k <= Se; k++) { | 647   for (k = cinfo->Ss; k <= Se; k++) { | 
| 645     temp = (*block)[jpeg_natural_order[k]]; | 648     temp = (*block)[jpeg_natural_order[k]]; | 
| 646     /* We must apply the point transform by Al.  For AC coefficients this | 649     /* We must apply the point transform by Al.  For AC coefficients this | 
| 647      * is an integer division with rounding towards 0.  To do this portably | 650      * is an integer division with rounding towards 0.  To do this portably | 
| 648      * in C, we shift after obtaining the absolute value. | 651      * in C, we shift after obtaining the absolute value. | 
| 649      */ | 652      */ | 
| 650     if (temp < 0) | 653     if (temp < 0) | 
| 651       temp = -temp;»    »       /* temp is abs value of input */ | 654       temp = -temp;             /* temp is abs value of input */ | 
| 652     temp >>= Al;»       »       /* apply the point transform */ | 655     temp >>= Al;                /* apply the point transform */ | 
| 653     absvalues[k] = temp;»       /* save abs value for main pass */ | 656     absvalues[k] = temp;        /* save abs value for main pass */ | 
| 654     if (temp == 1) | 657     if (temp == 1) | 
| 655       EOB = k;» »       »       /* EOB = index of last newly-nonzero coef */ | 658       EOB = k;                  /* EOB = index of last newly-nonzero coef */ | 
| 656   } | 659   } | 
| 657 | 660 | 
| 658   /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ | 661   /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */ | 
| 659 | 662 | 
| 660   r = 0;»       »       »       /* r = run length of zeros */ | 663   r = 0;                        /* r = run length of zeros */ | 
| 661   BR = 0;»      »       »       /* BR = count of buffered bits added now */ | 664   BR = 0;                       /* BR = count of buffered bits added now */ | 
| 662   BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */ | 665   BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */ | 
| 663 | 666 | 
| 664   for (k = cinfo->Ss; k <= Se; k++) { | 667   for (k = cinfo->Ss; k <= Se; k++) { | 
| 665     if ((temp = absvalues[k]) == 0) { | 668     if ((temp = absvalues[k]) == 0) { | 
| 666       r++; | 669       r++; | 
| 667       continue; | 670       continue; | 
| 668     } | 671     } | 
| 669 | 672 | 
| 670     /* Emit any required ZRLs, but not if they can be folded into EOB */ | 673     /* Emit any required ZRLs, but not if they can be folded into EOB */ | 
| 671     while (r > 15 && k <= EOB) { | 674     while (r > 15 && k <= EOB) { | 
| (...skipping 26 matching lines...) Expand all  Loading... | 
| 698     emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); | 701     emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); | 
| 699 | 702 | 
| 700     /* Emit output bit for newly-nonzero coef */ | 703     /* Emit output bit for newly-nonzero coef */ | 
| 701     temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1; | 704     temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1; | 
| 702     emit_bits(entropy, (unsigned int) temp, 1); | 705     emit_bits(entropy, (unsigned int) temp, 1); | 
| 703 | 706 | 
| 704     /* Emit buffered correction bits that must be associated with this code */ | 707     /* Emit buffered correction bits that must be associated with this code */ | 
| 705     emit_buffered_bits(entropy, BR_buffer, BR); | 708     emit_buffered_bits(entropy, BR_buffer, BR); | 
| 706     BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ | 709     BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ | 
| 707     BR = 0; | 710     BR = 0; | 
| 708     r = 0;»     »       »       /* reset zero run length */ | 711     r = 0;                      /* reset zero run length */ | 
| 709   } | 712   } | 
| 710 | 713 | 
| 711   if (r > 0 || BR > 0) {»       /* If there are trailing zeroes, */ | 714   if (r > 0 || BR > 0) {        /* If there are trailing zeroes, */ | 
| 712     entropy->EOBRUN++;» »       /* count an EOB */ | 715     entropy->EOBRUN++;          /* count an EOB */ | 
| 713     entropy->BE += BR;» »       /* concat my correction bits to older ones */ | 716     entropy->BE += BR;          /* concat my correction bits to older ones */ | 
| 714     /* We force out the EOB if we risk either: | 717     /* We force out the EOB if we risk either: | 
| 715      * 1. overflow of the EOB counter; | 718      * 1. overflow of the EOB counter; | 
| 716      * 2. overflow of the correction bit buffer during the next MCU. | 719      * 2. overflow of the correction bit buffer during the next MCU. | 
| 717      */ | 720      */ | 
| 718     if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1)) | 721     if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1)) | 
| 719       emit_eobrun(entropy); | 722       emit_eobrun(entropy); | 
| 720   } | 723   } | 
| 721 | 724 | 
| 722   cinfo->dest->next_output_byte = entropy->next_output_byte; | 725   cinfo->dest->next_output_byte = entropy->next_output_byte; | 
| 723   cinfo->dest->free_in_buffer = entropy->free_in_buffer; | 726   cinfo->dest->free_in_buffer = entropy->free_in_buffer; | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 735   return TRUE; | 738   return TRUE; | 
| 736 } | 739 } | 
| 737 | 740 | 
| 738 | 741 | 
| 739 /* | 742 /* | 
| 740  * Finish up at the end of a Huffman-compressed progressive scan. | 743  * Finish up at the end of a Huffman-compressed progressive scan. | 
| 741  */ | 744  */ | 
| 742 | 745 | 
| 743 METHODDEF(void) | 746 METHODDEF(void) | 
| 744 finish_pass_phuff (j_compress_ptr cinfo) | 747 finish_pass_phuff (j_compress_ptr cinfo) | 
| 745 { | 748 { | 
| 746   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; | 749   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; | 
| 747 | 750 | 
| 748   entropy->next_output_byte = cinfo->dest->next_output_byte; | 751   entropy->next_output_byte = cinfo->dest->next_output_byte; | 
| 749   entropy->free_in_buffer = cinfo->dest->free_in_buffer; | 752   entropy->free_in_buffer = cinfo->dest->free_in_buffer; | 
| 750 | 753 | 
| 751   /* Flush out any buffered data */ | 754   /* Flush out any buffered data */ | 
| 752   emit_eobrun(entropy); | 755   emit_eobrun(entropy); | 
| 753   flush_bits(entropy); | 756   flush_bits(entropy); | 
| 754 | 757 | 
| 755   cinfo->dest->next_output_byte = entropy->next_output_byte; | 758   cinfo->dest->next_output_byte = entropy->next_output_byte; | 
| 756   cinfo->dest->free_in_buffer = entropy->free_in_buffer; | 759   cinfo->dest->free_in_buffer = entropy->free_in_buffer; | 
| 757 } | 760 } | 
| 758 | 761 | 
| 759 | 762 | 
| 760 /* | 763 /* | 
| 761  * Finish up a statistics-gathering pass and create the new Huffman tables. | 764  * Finish up a statistics-gathering pass and create the new Huffman tables. | 
| 762  */ | 765  */ | 
| 763 | 766 | 
| 764 METHODDEF(void) | 767 METHODDEF(void) | 
| 765 finish_pass_gather_phuff (j_compress_ptr cinfo) | 768 finish_pass_gather_phuff (j_compress_ptr cinfo) | 
| 766 { | 769 { | 
| 767   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; | 770   phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; | 
| 768   boolean is_DC_band; | 771   boolean is_DC_band; | 
| 769   int ci, tbl; | 772   int ci, tbl; | 
| 770   jpeg_component_info * compptr; | 773   jpeg_component_info *compptr; | 
| 771   JHUFF_TBL **htblptr; | 774   JHUFF_TBL **htblptr; | 
| 772   boolean did[NUM_HUFF_TBLS]; | 775   boolean did[NUM_HUFF_TBLS]; | 
| 773 | 776 | 
| 774   /* Flush out buffered data (all we care about is counting the EOB symbol) */ | 777   /* Flush out buffered data (all we care about is counting the EOB symbol) */ | 
| 775   emit_eobrun(entropy); | 778   emit_eobrun(entropy); | 
| 776 | 779 | 
| 777   is_DC_band = (cinfo->Ss == 0); | 780   is_DC_band = (cinfo->Ss == 0); | 
| 778 | 781 | 
| 779   /* It's important not to apply jpeg_gen_optimal_table more than once | 782   /* It's important not to apply jpeg_gen_optimal_table more than once | 
| 780    * per table, because it clobbers the input frequency counts! | 783    * per table, because it clobbers the input frequency counts! | 
| 781    */ | 784    */ | 
| 782   MEMZERO(did, SIZEOF(did)); | 785   MEMZERO(did, sizeof(did)); | 
| 783 | 786 | 
| 784   for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 787   for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 
| 785     compptr = cinfo->cur_comp_info[ci]; | 788     compptr = cinfo->cur_comp_info[ci]; | 
| 786     if (is_DC_band) { | 789     if (is_DC_band) { | 
| 787       if (cinfo->Ah != 0)»      /* DC refinement needs no table */ | 790       if (cinfo->Ah != 0)       /* DC refinement needs no table */ | 
| 788 »       continue; | 791         continue; | 
| 789       tbl = compptr->dc_tbl_no; | 792       tbl = compptr->dc_tbl_no; | 
| 790     } else { | 793     } else { | 
| 791       tbl = compptr->ac_tbl_no; | 794       tbl = compptr->ac_tbl_no; | 
| 792     } | 795     } | 
| 793     if (! did[tbl]) { | 796     if (! did[tbl]) { | 
| 794       if (is_DC_band) | 797       if (is_DC_band) | 
| 795         htblptr = & cinfo->dc_huff_tbl_ptrs[tbl]; | 798         htblptr = & cinfo->dc_huff_tbl_ptrs[tbl]; | 
| 796       else | 799       else | 
| 797         htblptr = & cinfo->ac_huff_tbl_ptrs[tbl]; | 800         htblptr = & cinfo->ac_huff_tbl_ptrs[tbl]; | 
| 798       if (*htblptr == NULL) | 801       if (*htblptr == NULL) | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 809  */ | 812  */ | 
| 810 | 813 | 
| 811 GLOBAL(void) | 814 GLOBAL(void) | 
| 812 jinit_phuff_encoder (j_compress_ptr cinfo) | 815 jinit_phuff_encoder (j_compress_ptr cinfo) | 
| 813 { | 816 { | 
| 814   phuff_entropy_ptr entropy; | 817   phuff_entropy_ptr entropy; | 
| 815   int i; | 818   int i; | 
| 816 | 819 | 
| 817   entropy = (phuff_entropy_ptr) | 820   entropy = (phuff_entropy_ptr) | 
| 818     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 821     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 
| 819 »       »       »       »       SIZEOF(phuff_entropy_encoder)); | 822                                 sizeof(phuff_entropy_encoder)); | 
| 820   cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; | 823   cinfo->entropy = (struct jpeg_entropy_encoder *) entropy; | 
| 821   entropy->pub.start_pass = start_pass_phuff; | 824   entropy->pub.start_pass = start_pass_phuff; | 
| 822 | 825 | 
| 823   /* Mark tables unallocated */ | 826   /* Mark tables unallocated */ | 
| 824   for (i = 0; i < NUM_HUFF_TBLS; i++) { | 827   for (i = 0; i < NUM_HUFF_TBLS; i++) { | 
| 825     entropy->derived_tbls[i] = NULL; | 828     entropy->derived_tbls[i] = NULL; | 
| 826     entropy->count_ptrs[i] = NULL; | 829     entropy->count_ptrs[i] = NULL; | 
| 827   } | 830   } | 
| 828   entropy->bit_buffer = NULL;»  /* needed only in AC refinement scan */ | 831   entropy->bit_buffer = NULL;   /* needed only in AC refinement scan */ | 
| 829 } | 832 } | 
| 830 | 833 | 
| 831 #endif /* C_PROGRESSIVE_SUPPORTED */ | 834 #endif /* C_PROGRESSIVE_SUPPORTED */ | 
| OLD | NEW | 
|---|