Main MRPT website > C++ reference for MRPT 1.5.9
jchuff.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #define JPEG_INTERNALS
11 #include "jinclude.h"
12 #include "mrpt_jpeglib.h"
13 #include "jchuff.h" /* Declarations shared with jcphuff.c */
14 
15 
16 /* Expanded entropy encoder object for Huffman encoding.
17  *
18  * The savable_state subrecord contains fields that change within an MCU,
19  * but must not be updated permanently until we complete the MCU.
20  */
21 
22 typedef struct {
23  INT32 put_buffer; /* current bit-accumulation buffer */
24  int put_bits; /* # of bits now in it */
25  int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
27 
28 /* This macro is to work around compilers with missing or broken
29  * structure assignment. You'll need to fix this code if you have
30  * such a compiler and you change MAX_COMPS_IN_SCAN.
31  */
32 
33 #ifndef NO_STRUCT_ASSIGN
34 #define ASSIGN_STATE(dest,src) ((dest) = (src))
35 #else
36 #if MAX_COMPS_IN_SCAN == 4
37 #define ASSIGN_STATE(dest,src) \
38  ((dest).put_buffer = (src).put_buffer, \
39  (dest).put_bits = (src).put_bits, \
40  (dest).last_dc_val[0] = (src).last_dc_val[0], \
41  (dest).last_dc_val[1] = (src).last_dc_val[1], \
42  (dest).last_dc_val[2] = (src).last_dc_val[2], \
43  (dest).last_dc_val[3] = (src).last_dc_val[3])
44 #endif
45 #endif
46 
47 
48 typedef struct {
49  struct jpeg_entropy_encoder pub; /* public fields */
50 
51  savable_state saved; /* Bit buffer & DC state at start of MCU */
52 
53  /* These fields are NOT loaded into local working state. */
54  unsigned int restarts_to_go; /* MCUs left in this restart interval */
55  int next_restart_num; /* next restart number to write (0-7) */
56 
57  /* Pointers to derived tables (these workspaces have image lifespan) */
58  c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
59  c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
60 
61 #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
62  long * dc_count_ptrs[NUM_HUFF_TBLS];
63  long * ac_count_ptrs[NUM_HUFF_TBLS];
64 #endif
66 
68 
69 /* Working state while writing an MCU.
70  * This struct contains all the fields that are needed by subroutines.
71  */
72 
73 typedef struct {
74  JOCTET * next_output_byte; /* => next byte to write in buffer */
75  size_t free_in_buffer; /* # of byte spaces remaining in buffer */
76  savable_state cur; /* Current bit buffer & DC state */
77  j_compress_ptr cinfo; /* dump_buffer needs access to this */
79 
80 
81 /* Forward declarations */
85 #ifdef ENTROPY_OPT_SUPPORTED
89 #endif
90 
91 
92 /*
93  * Initialize for a Huffman-compressed scan.
94  * If gather_statistics is TRUE, we do not output anything during the scan,
95  * just count the Huffman symbols used and generate Huffman code tables.
96  */
97 
98 METHODDEF(void)
99 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
100 {
101  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
102  int ci, dctbl, actbl;
104 
105  if (gather_statistics) {
106 #ifdef ENTROPY_OPT_SUPPORTED
107  entropy->pub.encode_mcu = encode_mcu_gather;
108  entropy->pub.finish_pass = finish_pass_gather;
109 #else
110  ERREXIT(cinfo, JERR_NOT_COMPILED);
111 #endif
112  } else {
113  entropy->pub.encode_mcu = encode_mcu_huff;
114  entropy->pub.finish_pass = finish_pass_huff;
115  }
116 
117  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
118  compptr = cinfo->cur_comp_info[ci];
119  dctbl = compptr->dc_tbl_no;
120  actbl = compptr->ac_tbl_no;
121  if (gather_statistics) {
122 #ifdef ENTROPY_OPT_SUPPORTED
123  /* Check for invalid table indexes */
124  /* (make_c_derived_tbl does this in the other path) */
125  if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
126  ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
127  if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
128  ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
129  /* Allocate and zero the statistics tables */
130  /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
131  if (entropy->dc_count_ptrs[dctbl] == NULL)
132  entropy->dc_count_ptrs[dctbl] = (long *)
133  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
134  257 * SIZEOF(long));
135  MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
136  if (entropy->ac_count_ptrs[actbl] == NULL)
137  entropy->ac_count_ptrs[actbl] = (long *)
138  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
139  257 * SIZEOF(long));
140  MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
141 #endif
142  } else {
143  /* Compute derived values for Huffman tables */
144  /* We may do this more than once for a table, but it's not expensive */
145  jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
146  & entropy->dc_derived_tbls[dctbl]);
147  jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
148  & entropy->ac_derived_tbls[actbl]);
149  }
150  /* Initialize DC predictions to 0 */
151  entropy->saved.last_dc_val[ci] = 0;
152  }
153 
154  /* Initialize bit buffer to empty */
155  entropy->saved.put_buffer = 0;
156  entropy->saved.put_bits = 0;
157 
158  /* Initialize restart stuff */
159  entropy->restarts_to_go = cinfo->restart_interval;
160  entropy->next_restart_num = 0;
161 }
162 
163 
164 /*
165  * Compute the derived values for a Huffman table.
166  * This routine also performs some validation checks on the table.
167  *
168  * Note this is also used by jcphuff.c.
169  */
170 
171 GLOBAL(void)
173  c_derived_tbl ** pdtbl)
174 {
175  JHUFF_TBL *htbl;
176  c_derived_tbl *dtbl;
177  int p, i, l, lastp, si, maxsymbol;
178  char huffsize[257];
179  unsigned int huffcode[257];
180  unsigned int code;
181 
182  /* Note that huffsize[] and huffcode[] are filled in code-length order,
183  * paralleling the order of the symbols themselves in htbl->huffval[].
184  */
185 
186  /* Find the input Huffman table */
187  if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
188  ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
189  htbl =
190  isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
191  if (htbl == NULL)
192  ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
193 
194  /* Allocate a workspace if we haven't already done so. */
195  if (*pdtbl == NULL)
196  *pdtbl = (c_derived_tbl *)
197  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
199  dtbl = *pdtbl;
200 
201  /* Figure C.1: make table of Huffman code length for each symbol */
202 
203  p = 0;
204  for (l = 1; l <= 16; l++) {
205  i = (int) htbl->bits[l];
206  if (i < 0 || p + i > 256) /* protect against table overrun */
207  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
208  while (i--)
209  huffsize[p++] = (char) l;
210  }
211  huffsize[p] = 0;
212  lastp = p;
213 
214  /* Figure C.2: generate the codes themselves */
215  /* We also validate that the counts represent a legal Huffman code tree. */
216 
217  code = 0;
218  si = huffsize[0];
219  p = 0;
220  while (huffsize[p]) {
221  while (((int) huffsize[p]) == si) {
222  huffcode[p++] = code;
223  code++;
224  }
225  /* code is now 1 more than the last code used for codelength si; but
226  * it must still fit in si bits, since no code is allowed to be all ones.
227  */
228  if (((INT32) code) >= (((INT32) 1) << si))
229  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
230  code <<= 1;
231  si++;
232  }
233 
234  /* Figure C.3: generate encoding tables */
235  /* These are code and size indexed by symbol value */
236 
237  /* Set all codeless symbols to have code length 0;
238  * this lets us detect duplicate VAL entries here, and later
239  * allows emit_bits to detect any attempt to emit such symbols.
240  */
241  MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
242 
243  /* This is also a convenient place to check for out-of-range
244  * and duplicated VAL entries. We allow 0..255 for AC symbols
245  * but only 0..15 for DC. (We could constrain them further
246  * based on data depth and mode, but this seems enough.)
247  */
248  maxsymbol = isDC ? 15 : 255;
249 
250  for (p = 0; p < lastp; p++) {
251  i = htbl->huffval[p];
252  if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
253  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
254  dtbl->ehufco[i] = huffcode[p];
255  dtbl->ehufsi[i] = huffsize[p];
256  }
257 }
258 
259 
260 /* Outputting bytes to the file */
261 
262 /* Emit a byte, taking 'action' if must suspend. */
263 #define emit_byte(state,val,action) \
264  { *(state)->next_output_byte++ = (JOCTET) (val); \
265  if (--(state)->free_in_buffer == 0) \
266  if (! dump_buffer(state)) \
267  { action; } }
268 
269 
270 LOCAL(boolean)
272 /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
273 {
274  struct jpeg_destination_mgr * dest = state->cinfo->dest;
275 
276  if (! (*dest->empty_output_buffer) (state->cinfo))
277  return FALSE;
278  /* After a successful buffer dump, must reset buffer pointers */
279  state->next_output_byte = dest->next_output_byte;
280  state->free_in_buffer = dest->free_in_buffer;
281  return TRUE;
282 }
283 
284 
285 /* Outputting bits to the file */
286 
287 /* Only the right 24 bits of put_buffer are used; the valid bits are
288  * left-justified in this part. At most 16 bits can be passed to emit_bits
289  * in one call, and we never retain more than 7 bits in put_buffer
290  * between calls, so 24 bits are sufficient.
291  */
292 
293 INLINE
294 LOCAL(boolean)
295 emit_bits (working_state * state, unsigned int code, int size)
296 /* Emit some bits; return TRUE if successful, FALSE if must suspend */
297 {
298  /* This routine is heavily used, so it's worth coding tightly. */
299  INT32 put_buffer = (INT32) code;
300  int put_bits = state->cur.put_bits;
301 
302  /* if size is 0, caller used an invalid Huffman table entry */
303  if (size == 0)
304  ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
305 
306  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
307 
308  put_bits += size; /* new number of bits in buffer */
309 
310  put_buffer <<= 24 - put_bits; /* align incoming bits */
311 
312  put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
313 
314  while (put_bits >= 8) {
315  int c = (int) ((put_buffer >> 16) & 0xFF);
316 
317  emit_byte(state, c, return FALSE);
318  if (c == 0xFF) { /* need to stuff a zero byte? */
319  emit_byte(state, 0, return FALSE);
320  }
321  put_buffer <<= 8;
322  put_bits -= 8;
323  }
324 
325  state->cur.put_buffer = put_buffer; /* update state variables */
326  state->cur.put_bits = put_bits;
327 
328  return TRUE;
329 }
330 
331 
332 LOCAL(boolean)
334 {
335  if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
336  return FALSE;
337  state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
338  state->cur.put_bits = 0;
339  return TRUE;
340 }
341 
342 
343 /* Encode a single block's worth of coefficients */
344 
345 LOCAL(boolean)
346 encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
347  c_derived_tbl *dctbl, c_derived_tbl *actbl)
348 {
349  int temp, temp2;
350  int nbits;
351  int k, r, i;
352 
353  /* Encode the DC coefficient difference per section F.1.2.1 */
354 
355  temp = temp2 = block[0] - last_dc_val;
356 
357  if (temp < 0) {
358  temp = -temp; /* temp is abs value of input */
359  /* For a negative input, want temp2 = bitwise complement of abs(input) */
360  /* This code assumes we are on a two's complement machine */
361  temp2--;
362  }
363 
364  /* Find the number of bits needed for the magnitude of the coefficient */
365  nbits = 0;
366  while (temp) {
367  nbits++;
368  temp >>= 1;
369  }
370  /* Check for out-of-range coefficient values.
371  * Since we're encoding a difference, the range limit is twice as much.
372  */
373  if (nbits > MAX_COEF_BITS+1)
374  ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
375 
376  /* Emit the Huffman-coded symbol for the number of bits */
377  if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
378  return FALSE;
379 
380  /* Emit that number of bits of the value, if positive, */
381  /* or the complement of its magnitude, if negative. */
382  if (nbits) /* emit_bits rejects calls with size 0 */
383  if (! emit_bits(state, (unsigned int) temp2, nbits))
384  return FALSE;
385 
386  /* Encode the AC coefficients per section F.1.2.2 */
387 
388  r = 0; /* r = run length of zeros */
389 
390  for (k = 1; k < DCTSIZE2; k++) {
391  if ((temp = block[jpeg_natural_order[k]]) == 0) {
392  r++;
393  } else {
394  /* if run length > 15, must emit special run-length-16 codes (0xF0) */
395  while (r > 15) {
396  if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
397  return FALSE;
398  r -= 16;
399  }
400 
401  temp2 = temp;
402  if (temp < 0) {
403  temp = -temp; /* temp is abs value of input */
404  /* This code assumes we are on a two's complement machine */
405  temp2--;
406  }
407 
408  /* Find the number of bits needed for the magnitude of the coefficient */
409  nbits = 1; /* there must be at least one 1 bit */
410  while ((temp >>= 1))
411  nbits++;
412  /* Check for out-of-range coefficient values */
413  if (nbits > MAX_COEF_BITS)
414  ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
415 
416  /* Emit Huffman symbol for run length / number of bits */
417  i = (r << 4) + nbits;
418  if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
419  return FALSE;
420 
421  /* Emit that number of bits of the value, if positive, */
422  /* or the complement of its magnitude, if negative. */
423  if (! emit_bits(state, (unsigned int) temp2, nbits))
424  return FALSE;
425 
426  r = 0;
427  }
428  }
429 
430  /* If the last coef(s) were zero, emit an end-of-block code */
431  if (r > 0)
432  if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
433  return FALSE;
434 
435  return TRUE;
436 }
437 
438 
439 /*
440  * Emit a restart marker & resynchronize predictions.
441  */
442 
443 LOCAL(boolean)
444 emit_restart (working_state * state, int restart_num)
445 {
446  int ci;
447 
448  if (! flush_bits(state))
449  return FALSE;
450 
451  emit_byte(state, 0xFF, return FALSE);
452  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
453 
454  /* Re-initialize DC predictions to 0 */
455  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
456  state->cur.last_dc_val[ci] = 0;
457 
458  /* The restart counter is not updated until we successfully write the MCU. */
459 
460  return TRUE;
461 }
462 
463 
464 /*
465  * Encode and output one MCU's worth of Huffman-compressed coefficients.
466  */
467 
468 METHODDEF(boolean)
470 {
471  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
472  working_state state;
473  int blkn, ci;
475 
476  /* Load up working state */
477  state.next_output_byte = cinfo->dest->next_output_byte;
478  state.free_in_buffer = cinfo->dest->free_in_buffer;
479  ASSIGN_STATE(state.cur, entropy->saved);
480  state.cinfo = cinfo;
481 
482  /* Emit restart marker if needed */
483  if (cinfo->restart_interval) {
484  if (entropy->restarts_to_go == 0)
485  if (! emit_restart(&state, entropy->next_restart_num))
486  return FALSE;
487  }
488 
489  /* Encode the MCU data blocks */
490  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
491  ci = cinfo->MCU_membership[blkn];
492  compptr = cinfo->cur_comp_info[ci];
493  if (! encode_one_block(&state,
494  MCU_data[blkn][0], state.cur.last_dc_val[ci],
495  entropy->dc_derived_tbls[compptr->dc_tbl_no],
496  entropy->ac_derived_tbls[compptr->ac_tbl_no]))
497  return FALSE;
498  /* Update last_dc_val */
499  state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
500  }
501 
502  /* Completed MCU, so update state */
503  cinfo->dest->next_output_byte = state.next_output_byte;
504  cinfo->dest->free_in_buffer = state.free_in_buffer;
505  ASSIGN_STATE(entropy->saved, state.cur);
506 
507  /* Update restart-interval state too */
508  if (cinfo->restart_interval) {
509  if (entropy->restarts_to_go == 0) {
510  entropy->restarts_to_go = cinfo->restart_interval;
511  entropy->next_restart_num++;
512  entropy->next_restart_num &= 7;
513  }
514  entropy->restarts_to_go--;
515  }
516 
517  return TRUE;
518 }
519 
520 
521 /*
522  * Finish up at the end of a Huffman-compressed scan.
523  */
524 
525 METHODDEF(void)
527 {
528  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
529  working_state state;
530 
531  /* Load up working state ... flush_bits needs it */
532  state.next_output_byte = cinfo->dest->next_output_byte;
533  state.free_in_buffer = cinfo->dest->free_in_buffer;
534  ASSIGN_STATE(state.cur, entropy->saved);
535  state.cinfo = cinfo;
536 
537  /* Flush out the last data */
538  if (! flush_bits(&state))
539  ERREXIT(cinfo, JERR_CANT_SUSPEND);
540 
541  /* Update state */
542  cinfo->dest->next_output_byte = state.next_output_byte;
543  cinfo->dest->free_in_buffer = state.free_in_buffer;
544  ASSIGN_STATE(entropy->saved, state.cur);
545 }
546 
547 
548 /*
549  * Huffman coding optimization.
550  *
551  * We first scan the supplied data and count the number of uses of each symbol
552  * that is to be Huffman-coded. (This process MUST agree with the code above.)
553  * Then we build a Huffman coding tree for the observed counts.
554  * Symbols which are not needed at all for the particular image are not
555  * assigned any code, which saves space in the DHT marker as well as in
556  * the compressed data.
557  */
558 
559 #ifdef ENTROPY_OPT_SUPPORTED
560 
561 
562 /* Process a single block's worth of coefficients */
563 
564 LOCAL(void)
565 htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
566  long dc_counts[], long ac_counts[])
567 {
568  int temp;
569  int nbits;
570  int k, r;
571 
572  /* Encode the DC coefficient difference per section F.1.2.1 */
573 
574  temp = block[0] - last_dc_val;
575  if (temp < 0)
576  temp = -temp;
577 
578  /* Find the number of bits needed for the magnitude of the coefficient */
579  nbits = 0;
580  while (temp) {
581  nbits++;
582  temp >>= 1;
583  }
584  /* Check for out-of-range coefficient values.
585  * Since we're encoding a difference, the range limit is twice as much.
586  */
587  if (nbits > MAX_COEF_BITS+1)
588  ERREXIT(cinfo, JERR_BAD_DCT_COEF);
589 
590  /* Count the Huffman symbol for the number of bits */
591  dc_counts[nbits]++;
592 
593  /* Encode the AC coefficients per section F.1.2.2 */
594 
595  r = 0; /* r = run length of zeros */
596 
597  for (k = 1; k < DCTSIZE2; k++) {
598  if ((temp = block[jpeg_natural_order[k]]) == 0) {
599  r++;
600  } else {
601  /* if run length > 15, must emit special run-length-16 codes (0xF0) */
602  while (r > 15) {
603  ac_counts[0xF0]++;
604  r -= 16;
605  }
606 
607  /* Find the number of bits needed for the magnitude of the coefficient */
608  if (temp < 0)
609  temp = -temp;
610 
611  /* Find the number of bits needed for the magnitude of the coefficient */
612  nbits = 1; /* there must be at least one 1 bit */
613  while ((temp >>= 1))
614  nbits++;
615  /* Check for out-of-range coefficient values */
616  if (nbits > MAX_COEF_BITS)
617  ERREXIT(cinfo, JERR_BAD_DCT_COEF);
618 
619  /* Count Huffman symbol for run length / number of bits */
620  ac_counts[(r << 4) + nbits]++;
621 
622  r = 0;
623  }
624  }
625 
626  /* If the last coef(s) were zero, emit an end-of-block code */
627  if (r > 0)
628  ac_counts[0]++;
629 }
630 
631 
632 /*
633  * Trial-encode one MCU's worth of Huffman-compressed coefficients.
634  * No data is actually output, so no suspension return is possible.
635  */
636 
637 METHODDEF(boolean)
639 {
640  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
641  int blkn, ci;
643 
644  /* Take care of restart intervals if needed */
645  if (cinfo->restart_interval) {
646  if (entropy->restarts_to_go == 0) {
647  /* Re-initialize DC predictions to 0 */
648  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
649  entropy->saved.last_dc_val[ci] = 0;
650  /* Update restart state */
651  entropy->restarts_to_go = cinfo->restart_interval;
652  }
653  entropy->restarts_to_go--;
654  }
655 
656  for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
657  ci = cinfo->MCU_membership[blkn];
658  compptr = cinfo->cur_comp_info[ci];
659  htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
660  entropy->dc_count_ptrs[compptr->dc_tbl_no],
661  entropy->ac_count_ptrs[compptr->ac_tbl_no]);
662  entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
663  }
664 
665  return TRUE;
666 }
667 
668 
669 /*
670  * Generate the best Huffman code table for the given counts, fill htbl.
671  * Note this is also used by jcphuff.c.
672  *
673  * The JPEG standard requires that no symbol be assigned a codeword of all
674  * one bits (so that padding bits added at the end of a compressed segment
675  * can't look like a valid code). Because of the canonical ordering of
676  * codewords, this just means that there must be an unused slot in the
677  * longest codeword length category. Section K.2 of the JPEG spec suggests
678  * reserving such a slot by pretending that symbol 256 is a valid symbol
679  * with count 1. In theory that's not optimal; giving it count zero but
680  * including it in the symbol set anyway should give a better Huffman code.
681  * But the theoretically better code actually seems to come out worse in
682  * practice, because it produces more all-ones bytes (which incur stuffed
683  * zero bytes in the final file). In any case the difference is tiny.
684  *
685  * The JPEG standard requires Huffman codes to be no more than 16 bits long.
686  * If some symbols have a very small but nonzero probability, the Huffman tree
687  * must be adjusted to meet the code length restriction. We currently use
688  * the adjustment method suggested in JPEG section K.2. This method is *not*
689  * optimal; it may not choose the best possible limited-length code. But
690  * typically only very-low-frequency symbols will be given less-than-optimal
691  * lengths, so the code is almost optimal. Experimental comparisons against
692  * an optimal limited-length-code algorithm indicate that the difference is
693  * microscopic --- usually less than a hundredth of a percent of total size.
694  * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
695  */
696 
697 GLOBAL(void)
699 {
700 #define MAX_CLEN 32 /* assumed maximum initial code length */
701  UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
702  int codesize[257]; /* codesize[k] = code length of symbol k */
703  int others[257]; /* next symbol in current branch of tree */
704  int c1, c2;
705  int p, i, j;
706  long v;
707 
708  /* This algorithm is explained in section K.2 of the JPEG standard */
709 
710  MEMZERO(bits, SIZEOF(bits));
711  MEMZERO(codesize, SIZEOF(codesize));
712  for (i = 0; i < 257; i++)
713  others[i] = -1; /* init links to empty */
714 
715  freq[256] = 1; /* make sure 256 has a nonzero count */
716  /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
717  * that no real symbol is given code-value of all ones, because 256
718  * will be placed last in the largest codeword category.
719  */
720 
721  /* Huffman's basic algorithm to assign optimal code lengths to symbols */
722 
723  for (;;) {
724  /* Find the smallest nonzero frequency, set c1 = its symbol */
725  /* In case of ties, take the larger symbol number */
726  c1 = -1;
727  v = 1000000000L;
728  for (i = 0; i <= 256; i++) {
729  if (freq[i] && freq[i] <= v) {
730  v = freq[i];
731  c1 = i;
732  }
733  }
734 
735  /* Find the next smallest nonzero frequency, set c2 = its symbol */
736  /* In case of ties, take the larger symbol number */
737  c2 = -1;
738  v = 1000000000L;
739  for (i = 0; i <= 256; i++) {
740  if (freq[i] && freq[i] <= v && i != c1) {
741  v = freq[i];
742  c2 = i;
743  }
744  }
745 
746  /* Done if we've merged everything into one frequency */
747  if (c2 < 0)
748  break;
749 
750  /* Else merge the two counts/trees */
751  freq[c1] += freq[c2];
752  freq[c2] = 0;
753 
754  /* Increment the codesize of everything in c1's tree branch */
755  codesize[c1]++;
756  while (others[c1] >= 0) {
757  c1 = others[c1];
758  codesize[c1]++;
759  }
760 
761  others[c1] = c2; /* chain c2 onto c1's tree branch */
762 
763  /* Increment the codesize of everything in c2's tree branch */
764  codesize[c2]++;
765  while (others[c2] >= 0) {
766  c2 = others[c2];
767  codesize[c2]++;
768  }
769  }
770 
771  /* Now count the number of symbols of each code length */
772  for (i = 0; i <= 256; i++) {
773  if (codesize[i]) {
774  /* The JPEG standard seems to think that this can't happen, */
775  /* but I'm paranoid... */
776  if (codesize[i] > MAX_CLEN)
777  ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
778 
779  bits[codesize[i]]++;
780  }
781  }
782 
783  /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
784  * Huffman procedure assigned any such lengths, we must adjust the coding.
785  * Here is what the JPEG spec says about how this next bit works:
786  * Since symbols are paired for the longest Huffman code, the symbols are
787  * removed from this length category two at a time. The prefix for the pair
788  * (which is one bit shorter) is allocated to one of the pair; then,
789  * skipping the BITS entry for that prefix length, a code word from the next
790  * shortest nonzero BITS entry is converted into a prefix for two code words
791  * one bit longer.
792  */
793 
794  for (i = MAX_CLEN; i > 16; i--) {
795  while (bits[i] > 0) {
796  j = i - 2; /* find length of new prefix to be used */
797  while (bits[j] == 0)
798  j--;
799 
800  bits[i] -= 2; /* remove two symbols */
801  bits[i-1]++; /* one goes in this length */
802  bits[j+1] += 2; /* two new symbols in this length */
803  bits[j]--; /* symbol of this length is now a prefix */
804  }
805  }
806 
807  /* Remove the count for the pseudo-symbol 256 from the largest codelength */
808  while (bits[i] == 0) /* find largest codelength still in use */
809  i--;
810  bits[i]--;
811 
812  /* Return final symbol counts (only for lengths 0..16) */
813  MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
814 
815  /* Return a list of the symbols sorted by code length */
816  /* It's not real clear to me why we don't need to consider the codelength
817  * changes made above, but the JPEG spec seems to think this works.
818  */
819  p = 0;
820  for (i = 1; i <= MAX_CLEN; i++) {
821  for (j = 0; j <= 255; j++) {
822  if (codesize[j] == i) {
823  htbl->huffval[p] = (UINT8) j;
824  p++;
825  }
826  }
827  }
828 
829  /* Set sent_table FALSE so updated table will be written to JPEG file. */
830  htbl->sent_table = FALSE;
831 }
832 
833 
834 /*
835  * Finish up a statistics-gathering pass and create the new Huffman tables.
836  */
837 
838 METHODDEF(void)
840 {
841  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
842  int ci, dctbl, actbl;
844  JHUFF_TBL **htblptr;
845  boolean did_dc[NUM_HUFF_TBLS];
846  boolean did_ac[NUM_HUFF_TBLS];
847 
848  /* It's important not to apply jpeg_gen_optimal_table more than once
849  * per table, because it clobbers the input frequency counts!
850  */
851  MEMZERO(did_dc, SIZEOF(did_dc));
852  MEMZERO(did_ac, SIZEOF(did_ac));
853 
854  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
855  compptr = cinfo->cur_comp_info[ci];
856  dctbl = compptr->dc_tbl_no;
857  actbl = compptr->ac_tbl_no;
858  if (! did_dc[dctbl]) {
859  htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
860  if (*htblptr == NULL)
861  *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
862  jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
863  did_dc[dctbl] = TRUE;
864  }
865  if (! did_ac[actbl]) {
866  htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
867  if (*htblptr == NULL)
868  *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
869  jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
870  did_ac[actbl] = TRUE;
871  }
872  }
873 }
874 
875 
876 #endif /* ENTROPY_OPT_SUPPORTED */
877 
878 
879 /*
880  * Module initialization routine for Huffman entropy encoding.
881  */
882 
883 GLOBAL(void)
885 {
886  huff_entropy_ptr entropy;
887  int i;
888 
889  entropy = (huff_entropy_ptr)
890  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
892  cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
893  entropy->pub.start_pass = start_pass_huff;
894 
895  /* Mark tables unallocated */
896  for (i = 0; i < NUM_HUFF_TBLS; i++) {
897  entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
898 #ifdef ENTROPY_OPT_SUPPORTED
899  entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
900 #endif
901  }
902 }
j_compress_ptr cinfo
Definition: jchuff.cpp:77
boolean int c_derived_tbl ** pdtbl
Definition: jchuff.h:39
JBLOCKROW * MCU_data
Definition: jchuff.cpp:83
#define emit_byte(state, val, action)
Definition: jchuff.cpp:263
long * ac_count_ptrs[NUM_HUFF_TBLS]
Definition: jchuff.cpp:63
#define MAX_COMPS_IN_SCAN
Definition: mrpt_jpeglib.h:43
jpeg_make_c_derived_tbl(j_compress_ptr cinfo, boolean isDC, int tblno, c_derived_tbl **pdtbl)
Definition: jchuff.cpp:172
const int jpeg_natural_order[]
Definition: jutils.cpp:49
unsigned int ehufco[256]
Definition: jchuff.h:25
JOCTET * next_output_byte
Definition: jchuff.cpp:74
struct jpeg_common_struct * j_common_ptr
Definition: mrpt_jpeglib.h:258
savable_state saved
Definition: jchuff.cpp:51
encode_one_block(working_state *state, JCOEFPTR block, int last_dc_val, c_derived_tbl *dctbl, c_derived_tbl *actbl)
Definition: jchuff.cpp:346
#define ERREXIT(cinfo, code)
Definition: jerror.h:199
#define SIZEOF(object)
Definition: jinclude.h:73
htest_one_block(j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val, long dc_counts[], long ac_counts[])
Definition: jchuff.cpp:565
long INT32
Definition: jmorecfg.h:158
jpeg_component_info * compptr
Definition: jdct.h:97
encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
Definition: jchuff.cpp:469
finish_pass_gather(j_compress_ptr cinfo)
Definition: jchuff.cpp:839
c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]
Definition: jchuff.cpp:59
INLINE emit_bits(working_state *state, unsigned int code, int size)
Definition: jchuff.cpp:295
flush_bits(working_state *state)
Definition: jchuff.cpp:333
encode_mcu_gather(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
Definition: jchuff.cpp:638
jinit_huff_encoder(j_compress_ptr cinfo)
Definition: jchuff.cpp:884
int put_bits
Definition: jchuff.cpp:24
#define MEMCOPY(dest, src, size)
Definition: jinclude.h:61
jpeg_gen_optimal_table(j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[])
Definition: jchuff.cpp:698
const GLubyte * c
Definition: glext.h:5590
#define FALSE
Definition: jmorecfg.h:227
short UINT8
Definition: jmorecfg.h:137
bit_buf_type int int nbits
Definition: jdhuff.h:152
#define JPOOL_IMAGE
Definition: mrpt_jpeglib.h:746
#define LOCAL(type)
Definition: jmorecfg.h:183
JCOEF FAR * JCOEFPTR
Definition: mrpt_jpeglib.h:72
huff_entropy_encoder * huff_entropy_ptr
Definition: jchuff.cpp:67
JHUFF_TBL long freq[]
Definition: jchuff.h:44
jpeg_alloc_huff_table(j_common_ptr cinfo)
Definition: jcomapi.cpp:96
#define DCTSIZE2
Definition: mrpt_jpeglib.h:39
struct jpeg_entropy_encoder pub
Definition: jchuff.cpp:49
INT32 put_buffer
Definition: jchuff.cpp:23
#define NUM_HUFF_TBLS
Definition: mrpt_jpeglib.h:41
#define MAX_COEF_BITS
Definition: jchuff.h:19
#define ASSIGN_STATE(dest, src)
Definition: jchuff.cpp:34
#define JPP(arglist)
Definition: mrpt_jpeglib.h:815
#define JPEG_RST0
#define TRUE
Definition: jmorecfg.h:230
emit_restart(working_state *state, int restart_num)
Definition: jchuff.cpp:444
METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:202
UINT8 bits[17]
Definition: mrpt_jpeglib.h:99
const GLdouble * v
Definition: glext.h:3603
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
Definition: inftrees.h:28
boolean sent_table
Definition: mrpt_jpeglib.h:107
finish_pass_huff(j_compress_ptr cinfo)
Definition: jchuff.cpp:526
#define GLOBAL(type)
Definition: jmorecfg.h:185
size_t free_in_buffer
Definition: jchuff.cpp:75
UINT8 huffval[256]
Definition: mrpt_jpeglib.h:101
JBLOCK FAR * JBLOCKROW
Definition: mrpt_jpeglib.h:68
int last_dc_val[MAX_COMPS_IN_SCAN]
Definition: jchuff.cpp:25
GLsizeiptr size
Definition: glext.h:3779
#define MAX_CLEN
unsigned int restarts_to_go
Definition: jchuff.cpp:54
JHUFF_TBL * htbl
Definition: jchuff.h:44
char JOCTET
Definition: jmorecfg.h:112
dump_buffer(working_state *state)
Definition: jchuff.cpp:271
boolean int tblno
Definition: jchuff.h:39
boolean isDC
Definition: jchuff.h:39
c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]
Definition: jchuff.cpp:58
GLfloat GLfloat p
Definition: glext.h:5587
char ehufsi[256]
Definition: jchuff.h:26
savable_state cur
Definition: jchuff.cpp:76
start_pass_huff(j_compress_ptr cinfo, boolean gather_statistics)
Definition: jchuff.cpp:99
#define MEMZERO(target, size)
Definition: jinclude.h:60
long * dc_count_ptrs[NUM_HUFF_TBLS]
Definition: jchuff.cpp:62



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020