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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019