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



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019