ReactOS 0.4.16-dev-88-ga65b6ae
jchuff.c File Reference
#include "jinclude.h"
#include "jpeglib.h"
Include dependency graph for jchuff.c:

Go to the source code of this file.

Classes

struct  c_derived_tbl
 
struct  savable_state
 
struct  huff_entropy_encoder
 
struct  working_state
 

Macros

#define JPEG_INTERNALS
 
#define MAX_COEF_BITS   14
 
#define ASSIGN_STATE(dest, src)   ((dest) = (src))
 
#define MAX_CORR_BITS   1000 /* Max # of correction bits I can buffer */
 
#define ISHIFT_TEMPS
 
#define IRIGHT_SHIFT(x, shft)   ((x) >> (shft))
 
#define emit_byte_s(state, val, action)
 
#define emit_byte_e(entropy, val)
 
#define MAX_CLEN   32 /* assumed maximum initial code length */
 

Typedefs

typedef huff_entropy_encoderhuff_entropy_ptr
 

Functions

 jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno, c_derived_tbl **pdtbl)
 
 dump_buffer_s (working_state *state)
 
 dump_buffer_e (huff_entropy_ptr entropy)
 
INLINE emit_bits_s (working_state *state, unsigned int code, int size)
 
INLINE emit_bits_e (huff_entropy_ptr entropy, unsigned int code, int size)
 
 flush_bits_s (working_state *state)
 
 flush_bits_e (huff_entropy_ptr entropy)
 
INLINE emit_dc_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
 
INLINE emit_ac_symbol (huff_entropy_ptr entropy, int tbl_no, int symbol)
 
 emit_buffered_bits (huff_entropy_ptr entropy, char *bufstart, unsigned int nbits)
 
 emit_eobrun (huff_entropy_ptr entropy)
 
 emit_restart_s (working_state *state, int restart_num)
 
 emit_restart_e (huff_entropy_ptr entropy, int restart_num)
 
 encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 
 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 
 encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 
 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 
 encode_one_block (working_state *state, JCOEFPTR block, int last_dc_val, c_derived_tbl *dctbl, c_derived_tbl *actbl)
 
 encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 
 finish_pass_huff (j_compress_ptr cinfo)
 
 htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val, long dc_counts[], long ac_counts[])
 
 encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
 
 jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[])
 
 finish_pass_gather (j_compress_ptr cinfo)
 
 start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
 
 jinit_huff_encoder (j_compress_ptr cinfo)
 

Macro Definition Documentation

◆ ASSIGN_STATE

#define ASSIGN_STATE (   dest,
  src 
)    ((dest) = (src))

Definition at line 67 of file jchuff.c.

◆ emit_byte_e

#define emit_byte_e (   entropy,
  val 
)
Value:
{ *(entropy)->next_output_byte++ = (JOCTET) (val); \
if (--(entropy)->free_in_buffer == 0) \
dump_buffer_e(entropy); }
GLuint GLfloat * val
Definition: glext.h:7180
char JOCTET
Definition: jmorecfg.h:167

Definition at line 261 of file jchuff.c.

◆ emit_byte_s

#define emit_byte_s (   state,
  val,
  action 
)
Value:
{ *(state)->next_output_byte++ = (JOCTET) (val); \
if (--(state)->free_in_buffer == 0) \
if (! dump_buffer_s(state)) \
{ action; } }
static int state
Definition: maze.c:121
const WCHAR * action
Definition: action.c:7509
dump_buffer_s(working_state *state)
Definition: jchuff.c:268

Definition at line 254 of file jchuff.c.

◆ IRIGHT_SHIFT

#define IRIGHT_SHIFT (   x,
  shft 
)    ((x) >> (shft))

Definition at line 151 of file jchuff.c.

◆ ISHIFT_TEMPS

#define ISHIFT_TEMPS

Definition at line 150 of file jchuff.c.

◆ JPEG_INTERNALS

#define JPEG_INTERNALS

Definition at line 23 of file jchuff.c.

◆ MAX_CLEN

#define MAX_CLEN   32 /* assumed maximum initial code length */

◆ MAX_COEF_BITS

#define MAX_COEF_BITS   14

Definition at line 37 of file jchuff.c.

◆ MAX_CORR_BITS

#define MAX_CORR_BITS   1000 /* Max # of correction bits I can buffer */

Definition at line 136 of file jchuff.c.

Typedef Documentation

◆ huff_entropy_ptr

Definition at line 117 of file jchuff.c.

Function Documentation

◆ dump_buffer_e()

dump_buffer_e ( huff_entropy_ptr  entropy)

Definition at line 283 of file jchuff.c.

285{
286 struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
287
288 if (! (*dest->empty_output_buffer) (entropy->cinfo))
289 ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
290 /* After a successful buffer dump, must reset buffer pointers */
291 entropy->next_output_byte = dest->next_output_byte;
292 entropy->free_in_buffer = dest->free_in_buffer;
293}
static char * dest
Definition: rtl.c:135
#define ERREXIT(msg)
Definition: rdjpgcom.c:72
j_compress_ptr cinfo
Definition: jchuff.c:107
JOCTET * next_output_byte
Definition: jchuff.c:105
size_t free_in_buffer
Definition: jchuff.c:106
struct jpeg_destination_mgr * dest
Definition: jpeglib.h:295

◆ dump_buffer_s()

dump_buffer_s ( working_state state)

Definition at line 268 of file jchuff.c.

270{
271 struct jpeg_destination_mgr * dest = state->cinfo->dest;
272
273 if (! (*dest->empty_output_buffer) (state->cinfo))
274 return FALSE;
275 /* After a successful buffer dump, must reset buffer pointers */
276 state->next_output_byte = dest->next_output_byte;
277 state->free_in_buffer = dest->free_in_buffer;
278 return TRUE;
279}
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117

◆ emit_ac_symbol()

INLINE emit_ac_symbol ( huff_entropy_ptr  entropy,
int  tbl_no,
int  symbol 
)

Definition at line 428 of file jchuff.c.

429{
430 if (entropy->gather_statistics)
431 entropy->ac_count_ptrs[tbl_no][symbol]++;
432 else {
433 c_derived_tbl * tbl = entropy->ac_derived_tbls[tbl_no];
434 emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
435 }
436}
INLINE emit_bits_e(huff_entropy_ptr entropy, unsigned int code, int size)
Definition: jchuff.c:348
unsigned int ehufco[256]
Definition: jchuff.c:43
char ehufsi[256]
Definition: jchuff.c:44
c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]
Definition: jchuff.c:92
boolean gather_statistics
Definition: jchuff.c:101
long * ac_count_ptrs[NUM_HUFF_TBLS]
Definition: jchuff.c:96

Referenced by emit_eobrun(), encode_mcu_AC_first(), and encode_mcu_AC_refine().

◆ emit_bits_e()

INLINE emit_bits_e ( huff_entropy_ptr  entropy,
unsigned int  code,
int  size 
)

Definition at line 348 of file jchuff.c.

350{
351 /* This routine is heavily used, so it's worth coding tightly. */
352 register INT32 put_buffer;
353 register int put_bits;
354
355 /* if size is 0, caller used an invalid Huffman table entry */
356 if (size == 0)
357 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
358
359 if (entropy->gather_statistics)
360 return; /* do nothing if we're only getting stats */
361
362 /* mask off any extra bits in code */
363 put_buffer = ((INT32) code) & ((((INT32) 1) << size) - 1);
364
365 /* new number of bits in buffer */
366 put_bits = size + entropy->saved.put_bits;
367
368 put_buffer <<= 24 - put_bits; /* align incoming bits */
369
370 /* and merge with old buffer contents */
371 put_buffer |= entropy->saved.put_buffer;
372
373 while (put_bits >= 8) {
374 int c = (int) ((put_buffer >> 16) & 0xFF);
375
376 emit_byte_e(entropy, c);
377 if (c == 0xFF) { /* need to stuff a zero byte? */
378 emit_byte_e(entropy, 0);
379 }
380 put_buffer <<= 8;
381 put_bits -= 8;
382 }
383
384 entropy->saved.put_buffer = put_buffer; /* update variables */
385 entropy->saved.put_bits = put_bits;
386}
signed int INT32
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
GLsizeiptr size
Definition: glext.h:5919
const GLubyte * c
Definition: glext.h:8905
#define emit_byte_e(entropy, val)
Definition: jchuff.c:261
Definition: inflate.c:139
savable_state saved
Definition: jchuff.c:84
INT32 put_buffer
Definition: jchuff.c:56
int put_bits
Definition: jchuff.c:57

Referenced by emit_ac_symbol(), emit_buffered_bits(), emit_dc_symbol(), emit_eobrun(), encode_mcu_AC_first(), encode_mcu_AC_refine(), encode_mcu_DC_first(), encode_mcu_DC_refine(), and flush_bits_e().

◆ emit_bits_s()

INLINE emit_bits_s ( working_state state,
unsigned int  code,
int  size 
)

Definition at line 306 of file jchuff.c.

308{
309 /* This routine is heavily used, so it's worth coding tightly. */
310 register INT32 put_buffer;
311 register int put_bits;
312
313 /* if size is 0, caller used an invalid Huffman table entry */
314 if (size == 0)
315 ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
316
317 /* mask off any extra bits in code */
318 put_buffer = ((INT32) code) & ((((INT32) 1) << size) - 1);
319
320 /* new number of bits in buffer */
321 put_bits = size + state->cur.put_bits;
322
323 put_buffer <<= 24 - put_bits; /* align incoming bits */
324
325 /* and merge with old buffer contents */
326 put_buffer |= state->cur.put_buffer;
327
328 while (put_bits >= 8) {
329 int c = (int) ((put_buffer >> 16) & 0xFF);
330
331 emit_byte_s(state, c, return FALSE);
332 if (c == 0xFF) { /* need to stuff a zero byte? */
333 emit_byte_s(state, 0, return FALSE);
334 }
335 put_buffer <<= 8;
336 put_bits -= 8;
337 }
338
339 state->cur.put_buffer = put_buffer; /* update state variables */
340 state->cur.put_bits = put_bits;
341
342 return TRUE;
343}
#define emit_byte_s(state, val, action)
Definition: jchuff.c:254

Referenced by encode_one_block(), and flush_bits_s().

◆ emit_buffered_bits()

emit_buffered_bits ( huff_entropy_ptr  entropy,
char bufstart,
unsigned int  nbits 
)

Definition at line 444 of file jchuff.c.

446{
447 if (entropy->gather_statistics)
448 return; /* no real work */
449
450 while (nbits > 0) {
451 emit_bits_e(entropy, (unsigned int) (*bufstart), 1);
452 bufstart++;
453 nbits--;
454 }
455}

Referenced by emit_eobrun(), and encode_mcu_AC_refine().

◆ emit_dc_symbol()

INLINE emit_dc_symbol ( huff_entropy_ptr  entropy,
int  tbl_no,
int  symbol 
)

Definition at line 415 of file jchuff.c.

416{
417 if (entropy->gather_statistics)
418 entropy->dc_count_ptrs[tbl_no][symbol]++;
419 else {
420 c_derived_tbl * tbl = entropy->dc_derived_tbls[tbl_no];
421 emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
422 }
423}
c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]
Definition: jchuff.c:91
long * dc_count_ptrs[NUM_HUFF_TBLS]
Definition: jchuff.c:95

Referenced by encode_mcu_DC_first().

◆ emit_eobrun()

emit_eobrun ( huff_entropy_ptr  entropy)

Definition at line 463 of file jchuff.c.

464{
465 register int temp, nbits;
466
467 if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
468 temp = entropy->EOBRUN;
469 nbits = 0;
470 while ((temp >>= 1))
471 nbits++;
472 /* safety check: shouldn't happen given limited correction-bit buffer */
473 if (nbits > 14)
474 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
475
476 emit_ac_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
477 if (nbits)
478 emit_bits_e(entropy, entropy->EOBRUN, nbits);
479
480 entropy->EOBRUN = 0;
481
482 /* Emit any buffered correction bits */
483 emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
484 entropy->BE = 0;
485 }
486}
INLINE emit_ac_symbol(huff_entropy_ptr entropy, int tbl_no, int symbol)
Definition: jchuff.c:428
emit_buffered_bits(huff_entropy_ptr entropy, char *bufstart, unsigned int nbits)
Definition: jchuff.c:444
static calc_node_t temp
Definition: rpn_ieee.c:38
unsigned int BE
Definition: jchuff.c:112
unsigned int EOBRUN
Definition: jchuff.c:111

Referenced by emit_restart_e(), encode_mcu_AC_first(), encode_mcu_AC_refine(), finish_pass_gather(), and finish_pass_huff().

◆ emit_restart_e()

emit_restart_e ( huff_entropy_ptr  entropy,
int  restart_num 
)

Definition at line 515 of file jchuff.c.

516{
517 int ci;
518
519 emit_eobrun(entropy);
520
521 if (! entropy->gather_statistics) {
522 flush_bits_e(entropy);
523 emit_byte_e(entropy, 0xFF);
524 emit_byte_e(entropy, JPEG_RST0 + restart_num);
525 }
526
527 if (entropy->cinfo->Ss == 0) {
528 /* Re-initialize DC predictions to 0 */
529 for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
530 entropy->saved.last_dc_val[ci] = 0;
531 } else {
532 /* Re-initialize all AC-related fields to 0 */
533 entropy->EOBRUN = 0;
534 entropy->BE = 0;
535 }
536}
emit_eobrun(huff_entropy_ptr entropy)
Definition: jchuff.c:463
flush_bits_e(huff_entropy_ptr entropy)
Definition: jchuff.c:401
#define JPEG_RST0
Definition: jpeglib.h:1126
int last_dc_val[MAX_COMPS_IN_SCAN]
Definition: jchuff.c:58

Referenced by encode_mcu_AC_first(), encode_mcu_AC_refine(), encode_mcu_DC_first(), and encode_mcu_DC_refine().

◆ emit_restart_s()

emit_restart_s ( working_state state,
int  restart_num 
)

Definition at line 494 of file jchuff.c.

495{
496 int ci;
497
498 if (! flush_bits_s(state))
499 return FALSE;
500
501 emit_byte_s(state, 0xFF, return FALSE);
502 emit_byte_s(state, JPEG_RST0 + restart_num, return FALSE);
503
504 /* Re-initialize DC predictions to 0 */
505 for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
506 state->cur.last_dc_val[ci] = 0;
507
508 /* The restart counter is not updated until we successfully write the MCU. */
509
510 return TRUE;
511}
flush_bits_s(working_state *state)
Definition: jchuff.c:390

Referenced by encode_mcu_huff().

◆ encode_mcu_AC_first()

encode_mcu_AC_first ( j_compress_ptr  cinfo,
JBLOCKROW MCU_data 
)

Definition at line 628 of file jchuff.c.

629{
630 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
631 const int * natural_order;
633 register int temp, temp2;
634 register int nbits;
635 register int r, k;
636 int Se, Al;
637
638 entropy->next_output_byte = cinfo->dest->next_output_byte;
639 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
640
641 /* Emit restart marker if needed */
642 if (cinfo->restart_interval)
643 if (entropy->restarts_to_go == 0)
644 emit_restart_e(entropy, entropy->next_restart_num);
645
646 Se = cinfo->Se;
647 Al = cinfo->Al;
648 natural_order = cinfo->natural_order;
649
650 /* Encode the MCU data block */
651 block = MCU_data[0];
652
653 /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
654
655 r = 0; /* r = run length of zeros */
656
657 for (k = cinfo->Ss; k <= Se; k++) {
658 if ((temp = (*block)[natural_order[k]]) == 0) {
659 r++;
660 continue;
661 }
662 /* We must apply the point transform by Al. For AC coefficients this
663 * is an integer division with rounding towards 0. To do this portably
664 * in C, we shift after obtaining the absolute value; so the code is
665 * interwoven with finding the abs value (temp) and output bits (temp2).
666 */
667 if (temp < 0) {
668 temp = -temp; /* temp is abs value of input */
669 temp >>= Al; /* apply the point transform */
670 /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
671 temp2 = ~temp;
672 } else {
673 temp >>= Al; /* apply the point transform */
674 temp2 = temp;
675 }
676 /* Watch out for case that nonzero coef is zero after point transform */
677 if (temp == 0) {
678 r++;
679 continue;
680 }
681
682 /* Emit any pending EOBRUN */
683 if (entropy->EOBRUN > 0)
684 emit_eobrun(entropy);
685 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
686 while (r > 15) {
687 emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
688 r -= 16;
689 }
690
691 /* Find the number of bits needed for the magnitude of the coefficient */
692 nbits = 1; /* there must be at least one 1 bit */
693 while ((temp >>= 1))
694 nbits++;
695 /* Check for out-of-range coefficient values */
696 if (nbits > MAX_COEF_BITS)
697 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
698
699 /* Count/emit Huffman symbol for run length / number of bits */
700 emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
701
702 /* Emit that number of bits of the value, if positive, */
703 /* or the complement of its magnitude, if negative. */
704 emit_bits_e(entropy, (unsigned int) temp2, nbits);
705
706 r = 0; /* reset zero run length */
707 }
708
709 if (r > 0) { /* If there are trailing zeroes, */
710 entropy->EOBRUN++; /* count an EOB */
711 if (entropy->EOBRUN == 0x7FFF)
712 emit_eobrun(entropy); /* force it out to avoid overflow */
713 }
714
715 cinfo->dest->next_output_byte = entropy->next_output_byte;
716 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
717
718 /* Update restart-interval state too */
719 if (cinfo->restart_interval) {
720 if (entropy->restarts_to_go == 0) {
721 entropy->restarts_to_go = cinfo->restart_interval;
722 entropy->next_restart_num++;
723 entropy->next_restart_num &= 7;
724 }
725 entropy->restarts_to_go--;
726 }
727
728 return TRUE;
729}
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
#define MAX_COEF_BITS
Definition: jchuff.c:37
emit_restart_e(huff_entropy_ptr entropy, int restart_num)
Definition: jchuff.c:515
huff_entropy_encoder * huff_entropy_ptr
Definition: jchuff.c:117
JBLOCK FAR * JBLOCKROW
Definition: jpeglib.h:80
if(dx< 0)
Definition: linetemp.h:194
int k
Definition: mpi.c:3369
unsigned int restarts_to_go
Definition: jchuff.c:87
struct jpeg_entropy_encoder * entropy
Definition: jpeglib.h:451
const int * natural_order
Definition: jpeglib.h:437
unsigned int restart_interval
Definition: jpeglib.h:370
static unsigned int block
Definition: xmlmemory.c:101

Referenced by start_pass_huff().

◆ encode_mcu_AC_refine()

encode_mcu_AC_refine ( j_compress_ptr  cinfo,
JBLOCKROW MCU_data 
)

Definition at line 782 of file jchuff.c.

783{
784 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
785 const int * natural_order;
787 register int temp;
788 register int r, k;
789 int Se, Al;
790 int EOB;
791 char *BR_buffer;
792 unsigned int BR;
793 int absvalues[DCTSIZE2];
794
795 entropy->next_output_byte = cinfo->dest->next_output_byte;
796 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
797
798 /* Emit restart marker if needed */
799 if (cinfo->restart_interval)
800 if (entropy->restarts_to_go == 0)
801 emit_restart_e(entropy, entropy->next_restart_num);
802
803 Se = cinfo->Se;
804 Al = cinfo->Al;
805 natural_order = cinfo->natural_order;
806
807 /* Encode the MCU data block */
808 block = MCU_data[0];
809
810 /* It is convenient to make a pre-pass to determine the transformed
811 * coefficients' absolute values and the EOB position.
812 */
813 EOB = 0;
814 for (k = cinfo->Ss; k <= Se; k++) {
815 temp = (*block)[natural_order[k]];
816 /* We must apply the point transform by Al. For AC coefficients this
817 * is an integer division with rounding towards 0. To do this portably
818 * in C, we shift after obtaining the absolute value.
819 */
820 if (temp < 0)
821 temp = -temp; /* temp is abs value of input */
822 temp >>= Al; /* apply the point transform */
823 absvalues[k] = temp; /* save abs value for main pass */
824 if (temp == 1)
825 EOB = k; /* EOB = index of last newly-nonzero coef */
826 }
827
828 /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
829
830 r = 0; /* r = run length of zeros */
831 BR = 0; /* BR = count of buffered bits added now */
832 BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
833
834 for (k = cinfo->Ss; k <= Se; k++) {
835 if ((temp = absvalues[k]) == 0) {
836 r++;
837 continue;
838 }
839
840 /* Emit any required ZRLs, but not if they can be folded into EOB */
841 while (r > 15 && k <= EOB) {
842 /* emit any pending EOBRUN and the BE correction bits */
843 emit_eobrun(entropy);
844 /* Emit ZRL */
845 emit_ac_symbol(entropy, entropy->ac_tbl_no, 0xF0);
846 r -= 16;
847 /* Emit buffered correction bits that must be associated with ZRL */
848 emit_buffered_bits(entropy, BR_buffer, BR);
849 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
850 BR = 0;
851 }
852
853 /* If the coef was previously nonzero, it only needs a correction bit.
854 * NOTE: a straight translation of the spec's figure G.7 would suggest
855 * that we also need to test r > 15. But if r > 15, we can only get here
856 * if k > EOB, which implies that this coefficient is not 1.
857 */
858 if (temp > 1) {
859 /* The correction bit is the next bit of the absolute value. */
860 BR_buffer[BR++] = (char) (temp & 1);
861 continue;
862 }
863
864 /* Emit any pending EOBRUN and the BE correction bits */
865 emit_eobrun(entropy);
866
867 /* Count/emit Huffman symbol for run length / number of bits */
868 emit_ac_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
869
870 /* Emit output bit for newly-nonzero coef */
871 temp = ((*block)[natural_order[k]] < 0) ? 0 : 1;
872 emit_bits_e(entropy, (unsigned int) temp, 1);
873
874 /* Emit buffered correction bits that must be associated with this code */
875 emit_buffered_bits(entropy, BR_buffer, BR);
876 BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
877 BR = 0;
878 r = 0; /* reset zero run length */
879 }
880
881 if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
882 entropy->EOBRUN++; /* count an EOB */
883 entropy->BE += BR; /* concat my correction bits to older ones */
884 /* We force out the EOB if we risk either:
885 * 1. overflow of the EOB counter;
886 * 2. overflow of the correction bit buffer during the next MCU.
887 */
888 if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
889 emit_eobrun(entropy);
890 }
891
892 cinfo->dest->next_output_byte = entropy->next_output_byte;
893 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
894
895 /* Update restart-interval state too */
896 if (cinfo->restart_interval) {
897 if (entropy->restarts_to_go == 0) {
898 entropy->restarts_to_go = cinfo->restart_interval;
899 entropy->next_restart_num++;
900 entropy->next_restart_num &= 7;
901 }
902 entropy->restarts_to_go--;
903 }
904
905 return TRUE;
906}
unsigned char
Definition: typeof.h:29
#define MAX_CORR_BITS
Definition: jchuff.c:136
#define DCTSIZE2
Definition: jpeglib.h:51

Referenced by start_pass_huff().

◆ encode_mcu_DC_first()

encode_mcu_DC_first ( j_compress_ptr  cinfo,
JBLOCKROW MCU_data 
)

Definition at line 545 of file jchuff.c.

546{
547 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
548 register int temp, temp2;
549 register int nbits;
550 int blkn, ci, tbl;
552
553 entropy->next_output_byte = cinfo->dest->next_output_byte;
554 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
555
556 /* Emit restart marker if needed */
557 if (cinfo->restart_interval)
558 if (entropy->restarts_to_go == 0)
559 emit_restart_e(entropy, entropy->next_restart_num);
560
561 /* Encode the MCU data blocks */
562 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
563 ci = cinfo->MCU_membership[blkn];
564 tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
565
566 /* Compute the DC value after the required point transform by Al.
567 * This is simply an arithmetic right shift.
568 */
569 temp = IRIGHT_SHIFT((int) (MCU_data[blkn][0][0]), cinfo->Al);
570
571 /* DC differences are figured on the point-transformed values. */
572 temp2 = temp - entropy->saved.last_dc_val[ci];
573 entropy->saved.last_dc_val[ci] = temp;
574
575 /* Encode the DC coefficient difference per section G.1.2.1 */
576 temp = temp2;
577 if (temp < 0) {
578 temp = -temp; /* temp is abs value of input */
579 /* For a negative input, want temp2 = bitwise complement of abs(input) */
580 /* This code assumes we are on a two's complement machine */
581 temp2--;
582 }
583
584 /* Find the number of bits needed for the magnitude of the coefficient */
585 nbits = 0;
586 while (temp) {
587 nbits++;
588 temp >>= 1;
589 }
590 /* Check for out-of-range coefficient values.
591 * Since we're encoding a difference, the range limit is twice as much.
592 */
593 if (nbits > MAX_COEF_BITS+1)
594 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
595
596 /* Count/emit the Huffman-coded symbol for the number of bits */
597 emit_dc_symbol(entropy, tbl, nbits);
598
599 /* Emit that number of bits of the value, if positive, */
600 /* or the complement of its magnitude, if negative. */
601 if (nbits) /* emit_bits rejects calls with size 0 */
602 emit_bits_e(entropy, (unsigned int) temp2, nbits);
603 }
604
605 cinfo->dest->next_output_byte = entropy->next_output_byte;
606 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
607
608 /* Update restart-interval state too */
609 if (cinfo->restart_interval) {
610 if (entropy->restarts_to_go == 0) {
611 entropy->restarts_to_go = cinfo->restart_interval;
612 entropy->next_restart_num++;
613 entropy->next_restart_num &= 7;
614 }
615 entropy->restarts_to_go--;
616 }
617
618 return TRUE;
619}
#define ISHIFT_TEMPS
Definition: jchuff.c:150
#define IRIGHT_SHIFT(x, shft)
Definition: jchuff.c:151
INLINE emit_dc_symbol(huff_entropy_ptr entropy, int tbl_no, int symbol)
Definition: jchuff.c:415
jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN]
Definition: jpeglib.h:423
int MCU_membership[C_MAX_BLOCKS_IN_MCU]
Definition: jpeglib.h:430

Referenced by start_pass_huff().

◆ encode_mcu_DC_refine()

encode_mcu_DC_refine ( j_compress_ptr  cinfo,
JBLOCKROW MCU_data 
)

Definition at line 739 of file jchuff.c.

740{
741 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
742 int Al, blkn;
743
744 entropy->next_output_byte = cinfo->dest->next_output_byte;
745 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
746
747 /* Emit restart marker if needed */
748 if (cinfo->restart_interval)
749 if (entropy->restarts_to_go == 0)
750 emit_restart_e(entropy, entropy->next_restart_num);
751
752 Al = cinfo->Al;
753
754 /* Encode the MCU data blocks */
755 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
756 /* We simply emit the Al'th bit of the DC coefficient value. */
757 emit_bits_e(entropy, (unsigned int) (MCU_data[blkn][0][0] >> Al), 1);
758 }
759
760 cinfo->dest->next_output_byte = entropy->next_output_byte;
761 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
762
763 /* Update restart-interval state too */
764 if (cinfo->restart_interval) {
765 if (entropy->restarts_to_go == 0) {
766 entropy->restarts_to_go = cinfo->restart_interval;
767 entropy->next_restart_num++;
768 entropy->next_restart_num &= 7;
769 }
770 entropy->restarts_to_go--;
771 }
772
773 return TRUE;
774}

Referenced by start_pass_huff().

◆ encode_mcu_gather()

encode_mcu_gather ( j_compress_ptr  cinfo,
JBLOCKROW MCU_data 
)

Definition at line 1193 of file jchuff.c.

1194{
1195 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1196 int blkn, ci;
1198
1199 /* Take care of restart intervals if needed */
1200 if (cinfo->restart_interval) {
1201 if (entropy->restarts_to_go == 0) {
1202 /* Re-initialize DC predictions to 0 */
1203 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
1204 entropy->saved.last_dc_val[ci] = 0;
1205 /* Update restart state */
1206 entropy->restarts_to_go = cinfo->restart_interval;
1207 }
1208 entropy->restarts_to_go--;
1209 }
1210
1211 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
1212 ci = cinfo->MCU_membership[blkn];
1213 compptr = cinfo->cur_comp_info[ci];
1214 htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
1215 entropy->dc_count_ptrs[compptr->dc_tbl_no],
1216 entropy->ac_count_ptrs[compptr->ac_tbl_no]);
1217 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
1218 }
1219
1220 return TRUE;
1221}
htest_one_block(j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val, long dc_counts[], long ac_counts[])
Definition: jchuff.c:1118
jpeg_component_info * compptr
Definition: jdct.h:238

Referenced by start_pass_huff().

◆ encode_mcu_huff()

encode_mcu_huff ( j_compress_ptr  cinfo,
JBLOCKROW MCU_data 
)

Definition at line 1012 of file jchuff.c.

1013{
1014 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1016 int blkn, ci;
1018
1019 /* Load up working state */
1020 state.next_output_byte = cinfo->dest->next_output_byte;
1021 state.free_in_buffer = cinfo->dest->free_in_buffer;
1022 ASSIGN_STATE(state.cur, entropy->saved);
1023 state.cinfo = cinfo;
1024
1025 /* Emit restart marker if needed */
1026 if (cinfo->restart_interval) {
1027 if (entropy->restarts_to_go == 0)
1028 if (! emit_restart_s(&state, entropy->next_restart_num))
1029 return FALSE;
1030 }
1031
1032 /* Encode the MCU data blocks */
1033 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
1034 ci = cinfo->MCU_membership[blkn];
1035 compptr = cinfo->cur_comp_info[ci];
1036 if (! encode_one_block(&state,
1037 MCU_data[blkn][0], state.cur.last_dc_val[ci],
1039 entropy->ac_derived_tbls[compptr->ac_tbl_no]))
1040 return FALSE;
1041 /* Update last_dc_val */
1042 state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
1043 }
1044
1045 /* Completed MCU, so update state */
1046 cinfo->dest->next_output_byte = state.next_output_byte;
1047 cinfo->dest->free_in_buffer = state.free_in_buffer;
1048 ASSIGN_STATE(entropy->saved, state.cur);
1049
1050 /* Update restart-interval state too */
1051 if (cinfo->restart_interval) {
1052 if (entropy->restarts_to_go == 0) {
1053 entropy->restarts_to_go = cinfo->restart_interval;
1054 entropy->next_restart_num++;
1055 entropy->next_restart_num &= 7;
1056 }
1057 entropy->restarts_to_go--;
1058 }
1059
1060 return TRUE;
1061}
#define ASSIGN_STATE(dest, src)
Definition: jchuff.c:67
encode_one_block(working_state *state, JCOEFPTR block, int last_dc_val, c_derived_tbl *dctbl, c_derived_tbl *actbl)
Definition: jchuff.c:912
emit_restart_s(working_state *state, int restart_num)
Definition: jchuff.c:494

Referenced by start_pass_huff().

◆ encode_one_block()

encode_one_block ( working_state state,
JCOEFPTR  block,
int  last_dc_val,
c_derived_tbl dctbl,
c_derived_tbl actbl 
)

Definition at line 912 of file jchuff.c.

914{
915 register int temp, temp2;
916 register int nbits;
917 register int r, k;
918 int Se = state->cinfo->lim_Se;
919 const int * natural_order = state->cinfo->natural_order;
920
921 /* Encode the DC coefficient difference per section F.1.2.1 */
922
923 temp = temp2 = block[0] - last_dc_val;
924
925 if (temp < 0) {
926 temp = -temp; /* temp is abs value of input */
927 /* For a negative input, want temp2 = bitwise complement of abs(input) */
928 /* This code assumes we are on a two's complement machine */
929 temp2--;
930 }
931
932 /* Find the number of bits needed for the magnitude of the coefficient */
933 nbits = 0;
934 while (temp) {
935 nbits++;
936 temp >>= 1;
937 }
938 /* Check for out-of-range coefficient values.
939 * Since we're encoding a difference, the range limit is twice as much.
940 */
941 if (nbits > MAX_COEF_BITS+1)
942 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
943
944 /* Emit the Huffman-coded symbol for the number of bits */
945 if (! emit_bits_s(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
946 return FALSE;
947
948 /* Emit that number of bits of the value, if positive, */
949 /* or the complement of its magnitude, if negative. */
950 if (nbits) /* emit_bits rejects calls with size 0 */
951 if (! emit_bits_s(state, (unsigned int) temp2, nbits))
952 return FALSE;
953
954 /* Encode the AC coefficients per section F.1.2.2 */
955
956 r = 0; /* r = run length of zeros */
957
958 for (k = 1; k <= Se; k++) {
959 if ((temp2 = block[natural_order[k]]) == 0) {
960 r++;
961 } else {
962 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
963 while (r > 15) {
964 if (! emit_bits_s(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
965 return FALSE;
966 r -= 16;
967 }
968
969 temp = temp2;
970 if (temp < 0) {
971 temp = -temp; /* temp is abs value of input */
972 /* This code assumes we are on a two's complement machine */
973 temp2--;
974 }
975
976 /* Find the number of bits needed for the magnitude of the coefficient */
977 nbits = 1; /* there must be at least one 1 bit */
978 while ((temp >>= 1))
979 nbits++;
980 /* Check for out-of-range coefficient values */
981 if (nbits > MAX_COEF_BITS)
982 ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
983
984 /* Emit Huffman symbol for run length / number of bits */
985 temp = (r << 4) + nbits;
986 if (! emit_bits_s(state, actbl->ehufco[temp], actbl->ehufsi[temp]))
987 return FALSE;
988
989 /* Emit that number of bits of the value, if positive, */
990 /* or the complement of its magnitude, if negative. */
991 if (! emit_bits_s(state, (unsigned int) temp2, nbits))
992 return FALSE;
993
994 r = 0;
995 }
996 }
997
998 /* If the last coef(s) were zero, emit an end-of-block code */
999 if (r > 0)
1000 if (! emit_bits_s(state, actbl->ehufco[0], actbl->ehufsi[0]))
1001 return FALSE;
1002
1003 return TRUE;
1004}
INLINE emit_bits_s(working_state *state, unsigned int code, int size)
Definition: jchuff.c:306

Referenced by encode_mcu_huff().

◆ finish_pass_gather()

finish_pass_gather ( j_compress_ptr  cinfo)

Definition at line 1465 of file jchuff.c.

1466{
1467 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1468 int ci, tbl;
1470 JHUFF_TBL **htblptr;
1471 boolean did_dc[NUM_HUFF_TBLS];
1472 boolean did_ac[NUM_HUFF_TBLS];
1473
1474 if (cinfo->progressive_mode)
1475 /* Flush out buffered data (all we care about is counting the EOB symbol) */
1476 emit_eobrun(entropy);
1477
1478 /* It's important not to apply jpeg_gen_optimal_table more than once
1479 * per table, because it clobbers the input frequency counts!
1480 */
1481 MEMZERO(did_dc, SIZEOF(did_dc));
1482 MEMZERO(did_ac, SIZEOF(did_ac));
1483
1484 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1485 compptr = cinfo->cur_comp_info[ci];
1486 /* DC needs no table for refinement scan */
1487 if (cinfo->Ss == 0 && cinfo->Ah == 0) {
1488 tbl = compptr->dc_tbl_no;
1489 if (! did_dc[tbl]) {
1490 htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
1491 if (*htblptr == NULL)
1492 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
1493 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[tbl]);
1494 did_dc[tbl] = TRUE;
1495 }
1496 }
1497 /* AC needs no table when not present */
1498 if (cinfo->Se) {
1499 tbl = compptr->ac_tbl_no;
1500 if (! did_ac[tbl]) {
1501 htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
1502 if (*htblptr == NULL)
1503 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
1504 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[tbl]);
1505 did_ac[tbl] = TRUE;
1506 }
1507 }
1508 }
1509}
#define SIZEOF(_ar)
Definition: calc.h:97
#define NULL
Definition: types.h:112
jpeg_gen_optimal_table(j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[])
Definition: jchuff.c:1252
jpeg_alloc_huff_table(j_common_ptr cinfo)
Definition: jcomapi.c:99
#define NUM_HUFF_TBLS
Definition: jpeglib.h:53
JHUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS]
Definition: jpeglib.h:342
JHUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS]
Definition: jpeglib.h:343
boolean progressive_mode
Definition: jpeglib.h:404
#define MEMZERO(addr, type, size)
Definition: svc_dg.c:324

Referenced by start_pass_huff().

◆ finish_pass_huff()

finish_pass_huff ( j_compress_ptr  cinfo)

Definition at line 1069 of file jchuff.c.

1070{
1071 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1073
1074 if (cinfo->progressive_mode) {
1075 entropy->next_output_byte = cinfo->dest->next_output_byte;
1076 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
1077
1078 /* Flush out any buffered data */
1079 emit_eobrun(entropy);
1080 flush_bits_e(entropy);
1081
1082 cinfo->dest->next_output_byte = entropy->next_output_byte;
1083 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
1084 } else {
1085 /* Load up working state ... flush_bits needs it */
1086 state.next_output_byte = cinfo->dest->next_output_byte;
1087 state.free_in_buffer = cinfo->dest->free_in_buffer;
1088 ASSIGN_STATE(state.cur, entropy->saved);
1089 state.cinfo = cinfo;
1090
1091 /* Flush out the last data */
1092 if (! flush_bits_s(&state))
1093 ERREXIT(cinfo, JERR_CANT_SUSPEND);
1094
1095 /* Update state */
1096 cinfo->dest->next_output_byte = state.next_output_byte;
1097 cinfo->dest->free_in_buffer = state.free_in_buffer;
1098 ASSIGN_STATE(entropy->saved, state.cur);
1099 }
1100}

Referenced by start_pass_huff().

◆ flush_bits_e()

flush_bits_e ( huff_entropy_ptr  entropy)

Definition at line 401 of file jchuff.c.

402{
403 emit_bits_e(entropy, 0x7F, 7); /* fill any partial byte with ones */
404 entropy->saved.put_buffer = 0; /* and reset bit-buffer to empty */
405 entropy->saved.put_bits = 0;
406}

Referenced by emit_restart_e(), and finish_pass_huff().

◆ flush_bits_s()

flush_bits_s ( working_state state)

Definition at line 390 of file jchuff.c.

391{
392 if (! emit_bits_s(state, 0x7F, 7)) /* fill any partial byte with ones */
393 return FALSE;
394 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
395 state->cur.put_bits = 0;
396 return TRUE;
397}

Referenced by emit_restart_s(), and finish_pass_huff().

◆ htest_one_block()

htest_one_block ( j_compress_ptr  cinfo,
JCOEFPTR  block,
int  last_dc_val,
long  dc_counts[],
long  ac_counts[] 
)

Definition at line 1118 of file jchuff.c.

1120{
1121 register int temp;
1122 register int nbits;
1123 register int r, k;
1124 int Se = cinfo->lim_Se;
1125 const int * natural_order = cinfo->natural_order;
1126
1127 /* Encode the DC coefficient difference per section F.1.2.1 */
1128
1129 temp = block[0] - last_dc_val;
1130 if (temp < 0)
1131 temp = -temp;
1132
1133 /* Find the number of bits needed for the magnitude of the coefficient */
1134 nbits = 0;
1135 while (temp) {
1136 nbits++;
1137 temp >>= 1;
1138 }
1139 /* Check for out-of-range coefficient values.
1140 * Since we're encoding a difference, the range limit is twice as much.
1141 */
1142 if (nbits > MAX_COEF_BITS+1)
1143 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
1144
1145 /* Count the Huffman symbol for the number of bits */
1146 dc_counts[nbits]++;
1147
1148 /* Encode the AC coefficients per section F.1.2.2 */
1149
1150 r = 0; /* r = run length of zeros */
1151
1152 for (k = 1; k <= Se; k++) {
1153 if ((temp = block[natural_order[k]]) == 0) {
1154 r++;
1155 } else {
1156 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
1157 while (r > 15) {
1158 ac_counts[0xF0]++;
1159 r -= 16;
1160 }
1161
1162 /* Find the number of bits needed for the magnitude of the coefficient */
1163 if (temp < 0)
1164 temp = -temp;
1165
1166 /* Find the number of bits needed for the magnitude of the coefficient */
1167 nbits = 1; /* there must be at least one 1 bit */
1168 while ((temp >>= 1))
1169 nbits++;
1170 /* Check for out-of-range coefficient values */
1171 if (nbits > MAX_COEF_BITS)
1172 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
1173
1174 /* Count Huffman symbol for run length / number of bits */
1175 ac_counts[(r << 4) + nbits]++;
1176
1177 r = 0;
1178 }
1179 }
1180
1181 /* If the last coef(s) were zero, emit an end-of-block code */
1182 if (r > 0)
1183 ac_counts[0]++;
1184}

Referenced by encode_mcu_gather().

◆ jinit_huff_encoder()

jinit_huff_encoder ( j_compress_ptr  cinfo)

Definition at line 1622 of file jchuff.c.

1623{
1624 huff_entropy_ptr entropy;
1625 int i;
1626
1627 entropy = (huff_entropy_ptr) (*cinfo->mem->alloc_small)
1629 cinfo->entropy = &entropy->pub;
1630 entropy->pub.start_pass = start_pass_huff;
1631
1632 /* Mark tables unallocated */
1633 for (i = 0; i < NUM_HUFF_TBLS; i++) {
1634 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
1635 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
1636 }
1637
1638 if (cinfo->progressive_mode)
1639 entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
1640}
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
start_pass_huff(j_compress_ptr cinfo, boolean gather_statistics)
Definition: jchuff.c:1519
struct jpeg_common_struct * j_common_ptr
Definition: jpeglib.h:284
#define JPOOL_IMAGE
Definition: jpeglib.h:808
struct jpeg_entropy_encoder pub
Definition: jchuff.c:82

Referenced by jinit_compress_master(), and transencode_master_selection().

◆ jpeg_gen_optimal_table()

jpeg_gen_optimal_table ( j_compress_ptr  cinfo,
JHUFF_TBL htbl,
long  freq[] 
)

Definition at line 1252 of file jchuff.c.

1253{
1254#define MAX_CLEN 32 /* assumed maximum initial code length */
1255 UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
1256 int codesize[257]; /* codesize[k] = code length of symbol k */
1257 int others[257]; /* next symbol in current branch of tree */
1258 int c1, c2, i, j;
1259 UINT8 *p;
1260 long v;
1261
1262 freq[256] = 1; /* make sure 256 has a nonzero count */
1263 /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
1264 * that no real symbol is given code-value of all ones, because 256
1265 * will be placed last in the largest codeword category.
1266 * In the symbol list build procedure this element serves as sentinel
1267 * for the zero run loop.
1268 */
1269
1270#ifndef DONT_USE_FANCY_HUFF_OPT
1271
1272 /* Build list of symbols sorted in order of descending frequency */
1273 /* This approach has several benefits (thank to John Korejwa for the idea):
1274 * 1.
1275 * If a codelength category is split during the length limiting procedure
1276 * below, the feature that more frequent symbols are assigned shorter
1277 * codewords remains valid for the adjusted code.
1278 * 2.
1279 * To reduce consecutive ones in a Huffman data stream (thus reducing the
1280 * number of stuff bytes in JPEG) it is preferable to follow 0 branches
1281 * (and avoid 1 branches) as much as possible. This is easily done by
1282 * assigning symbols to leaves of the Huffman tree in order of decreasing
1283 * frequency, with no secondary sort based on codelengths.
1284 * 3.
1285 * The symbol list can be built independently from the assignment of code
1286 * lengths by the Huffman procedure below.
1287 * Note: The symbol list build procedure must be performed first, because
1288 * the Huffman procedure assigning the codelengths clobbers the frequency
1289 * counts!
1290 */
1291
1292 /* Here we use the others array as a linked list of nonzero frequencies
1293 * to be sorted. Already sorted elements are removed from the list.
1294 */
1295
1296 /* Building list */
1297
1298 /* This item does not correspond to a valid symbol frequency and is used
1299 * as starting index.
1300 */
1301 j = 256;
1302
1303 for (i = 0;; i++) {
1304 if (freq[i] == 0) /* skip zero frequencies */
1305 continue;
1306 if (i > 255)
1307 break;
1308 others[j] = i; /* this symbol value */
1309 j = i; /* previous symbol value */
1310 }
1311 others[j] = -1; /* mark end of list */
1312
1313 /* Sorting list */
1314
1315 p = htbl->huffval;
1316 while ((c1 = others[256]) >= 0) {
1317 v = freq[c1];
1318 i = c1; /* first symbol value */
1319 j = 256; /* pseudo symbol value for starting index */
1320 while ((c2 = others[c1]) >= 0) {
1321 if (freq[c2] > v) {
1322 v = freq[c2];
1323 i = c2; /* this symbol value */
1324 j = c1; /* previous symbol value */
1325 }
1326 c1 = c2;
1327 }
1328 others[j] = others[i]; /* remove this symbol i from list */
1329 *p++ = (UINT8) i;
1330 }
1331
1332#endif /* DONT_USE_FANCY_HUFF_OPT */
1333
1334 /* This algorithm is explained in section K.2 of the JPEG standard */
1335
1337 MEMZERO(codesize, SIZEOF(codesize));
1338 for (i = 0; i < 257; i++)
1339 others[i] = -1; /* init links to empty */
1340
1341 /* Huffman's basic algorithm to assign optimal code lengths to symbols */
1342
1343 for (;;) {
1344 /* Find the smallest nonzero frequency, set c1 = its symbol */
1345 /* In case of ties, take the larger symbol number */
1346 c1 = -1;
1347 v = 1000000000L;
1348 for (i = 0; i <= 256; i++) {
1349 if (freq[i] && freq[i] <= v) {
1350 v = freq[i];
1351 c1 = i;
1352 }
1353 }
1354
1355 /* Find the next smallest nonzero frequency, set c2 = its symbol */
1356 /* In case of ties, take the larger symbol number */
1357 c2 = -1;
1358 v = 1000000000L;
1359 for (i = 0; i <= 256; i++) {
1360 if (freq[i] && freq[i] <= v && i != c1) {
1361 v = freq[i];
1362 c2 = i;
1363 }
1364 }
1365
1366 /* Done if we've merged everything into one frequency */
1367 if (c2 < 0)
1368 break;
1369
1370 /* Else merge the two counts/trees */
1371 freq[c1] += freq[c2];
1372 freq[c2] = 0;
1373
1374 /* Increment the codesize of everything in c1's tree branch */
1375 codesize[c1]++;
1376 while (others[c1] >= 0) {
1377 c1 = others[c1];
1378 codesize[c1]++;
1379 }
1380
1381 others[c1] = c2; /* chain c2 onto c1's tree branch */
1382
1383 /* Increment the codesize of everything in c2's tree branch */
1384 codesize[c2]++;
1385 while (others[c2] >= 0) {
1386 c2 = others[c2];
1387 codesize[c2]++;
1388 }
1389 }
1390
1391 /* Now count the number of symbols of each code length */
1392 for (i = 0; i <= 256; i++) {
1393 if (codesize[i]) {
1394 /* The JPEG standard seems to think that this can't happen, */
1395 /* but I'm paranoid... */
1396 if (codesize[i] > MAX_CLEN)
1397 ERREXIT(cinfo, JERR_HUFF_CLEN_OUTOFBOUNDS);
1398
1399 bits[codesize[i]]++;
1400 }
1401 }
1402
1403 /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
1404 * Huffman procedure assigned any such lengths, we must adjust the coding.
1405 * Here is what the JPEG spec says about how this next bit works:
1406 * Since symbols are paired for the longest Huffman code, the symbols are
1407 * removed from this length category two at a time. The prefix for the pair
1408 * (which is one bit shorter) is allocated to one of the pair; then,
1409 * skipping the BITS entry for that prefix length, a code word from the next
1410 * shortest nonzero BITS entry is converted into a prefix for two code words
1411 * one bit longer.
1412 */
1413
1414 for (i = MAX_CLEN; i > 16; i--) {
1415 while (bits[i] > 0) {
1416 j = i - 2; /* find length of new prefix to be used */
1417 while (bits[j] == 0) {
1418 if (j == 0)
1419 ERREXIT(cinfo, JERR_HUFF_CLEN_OUTOFBOUNDS);
1420 j--;
1421 }
1422
1423 bits[i] -= 2; /* remove two symbols */
1424 bits[i-1]++; /* one goes in this length */
1425 bits[j+1] += 2; /* two new symbols in this length */
1426 bits[j]--; /* symbol of this length is now a prefix */
1427 }
1428 }
1429
1430 /* Remove the count for the pseudo-symbol 256 from the largest codelength */
1431 while (bits[i] == 0) /* find largest codelength still in use */
1432 i--;
1433 bits[i]--;
1434
1435 /* Return final symbol counts (only for lengths 0..16) */
1436 MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
1437
1438#ifdef DONT_USE_FANCY_HUFF_OPT
1439
1440 /* Return a list of the symbols sorted by code length */
1441 /* Note: Due to the codelength changes made above, it can happen
1442 * that more frequent symbols are assigned longer codewords.
1443 */
1444 p = htbl->huffval;
1445 for (i = 1; i <= MAX_CLEN; i++) {
1446 for (j = 0; j <= 255; j++) {
1447 if (codesize[j] == i) {
1448 *p++ = (UINT8) j;
1449 }
1450 }
1451 }
1452
1453#endif /* DONT_USE_FANCY_HUFF_OPT */
1454
1455 /* Set sent_table FALSE so updated table will be written to JPEG file. */
1456 htbl->sent_table = FALSE;
1457}
unsigned char UINT8
const GLdouble * v
Definition: gl.h:2040
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * bits
Definition: glext.h:10929
GLfloat GLfloat p
Definition: glext.h:8902
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
#define MAX_CLEN
#define MEMCOPY(dest, src, size)
Definition: jinclude.h:69
boolean sent_table
Definition: jpeglib.h:119
UINT8 bits[17]
Definition: jpeglib.h:111
UINT8 huffval[256]
Definition: jpeglib.h:113

Referenced by finish_pass_gather().

◆ jpeg_make_c_derived_tbl()

jpeg_make_c_derived_tbl ( j_compress_ptr  cinfo,
boolean  isDC,
int  tblno,
c_derived_tbl **  pdtbl 
)

Definition at line 161 of file jchuff.c.

163{
164 JHUFF_TBL *htbl;
165 c_derived_tbl *dtbl;
166 int p, i, l, lastp, si, maxsymbol;
167 char huffsize[257];
168 unsigned int huffcode[257];
169 unsigned int code;
170
171 /* Note that huffsize[] and huffcode[] are filled in code-length order,
172 * paralleling the order of the symbols themselves in htbl->huffval[].
173 */
174
175 /* Find the input Huffman table */
176 if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
177 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
178 htbl =
180 if (htbl == NULL)
181 htbl = jpeg_std_huff_table((j_common_ptr) cinfo, isDC, tblno);
182
183 /* Allocate a workspace if we haven't already done so. */
184 if (*pdtbl == NULL)
185 *pdtbl = (c_derived_tbl *) (*cinfo->mem->alloc_small)
187 dtbl = *pdtbl;
188
189 /* Figure C.1: make table of Huffman code length for each symbol */
190
191 p = 0;
192 for (l = 1; l <= 16; l++) {
193 i = (int) htbl->bits[l];
194 if (i < 0 || p + i > 256) /* protect against table overrun */
195 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
196 while (i--)
197 huffsize[p++] = (char) l;
198 }
199 huffsize[p] = 0;
200 lastp = p;
201
202 /* Figure C.2: generate the codes themselves */
203 /* We also validate that the counts represent a legal Huffman code tree. */
204
205 code = 0;
206 si = huffsize[0];
207 p = 0;
208 while (huffsize[p]) {
209 while (((int) huffsize[p]) == si) {
210 huffcode[p++] = code;
211 code++;
212 }
213 /* code is now 1 more than the last code used for codelength si; but
214 * it must still fit in si bits, since no code is allowed to be all ones.
215 */
216 if (((INT32) code) >= (((INT32) 1) << si))
217 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
218 code <<= 1;
219 si++;
220 }
221
222 /* Figure C.3: generate encoding tables */
223 /* These are code and size indexed by symbol value */
224
225 /* Set all codeless symbols to have code length 0;
226 * this lets us detect duplicate VAL entries here, and later
227 * allows emit_bits to detect any attempt to emit such symbols.
228 */
229 MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
230
231 /* This is also a convenient place to check for out-of-range
232 * and duplicated VAL entries. We allow 0..255 for AC symbols
233 * but only 0..15 for DC. (We could constrain them further
234 * based on data depth and mode, but this seems enough.)
235 */
236 maxsymbol = isDC ? 15 : 255;
237
238 for (p = 0; p < lastp; p++) {
239 i = htbl->huffval[p];
240 if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
241 ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
242 dtbl->ehufco[i] = huffcode[p];
243 dtbl->ehufsi[i] = huffsize[p];
244 }
245}
r l[0]
Definition: byte_order.h:168
jpeg_std_huff_table(j_common_ptr cinfo, boolean isDC, int tblno)
Definition: jcomapi.c:117
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:212
boolean isDC
Definition: jpeglib.h:1010
boolean int tblno
Definition: jpeglib.h:1010

Referenced by start_pass_huff().

◆ start_pass_huff()

start_pass_huff ( j_compress_ptr  cinfo,
boolean  gather_statistics 
)

Definition at line 1519 of file jchuff.c.

1520{
1521 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1522 int ci, tbl;
1524
1525 if (gather_statistics)
1526 entropy->pub.finish_pass = finish_pass_gather;
1527 else
1528 entropy->pub.finish_pass = finish_pass_huff;
1529
1530 if (cinfo->progressive_mode) {
1531 entropy->cinfo = cinfo;
1532 entropy->gather_statistics = gather_statistics;
1533
1534 /* We assume jcmaster.c already validated the scan parameters. */
1535
1536 /* Select execution routine */
1537 if (cinfo->Ah == 0) {
1538 if (cinfo->Ss == 0)
1539 entropy->pub.encode_mcu = encode_mcu_DC_first;
1540 else
1541 entropy->pub.encode_mcu = encode_mcu_AC_first;
1542 } else {
1543 if (cinfo->Ss == 0)
1544 entropy->pub.encode_mcu = encode_mcu_DC_refine;
1545 else {
1546 entropy->pub.encode_mcu = encode_mcu_AC_refine;
1547 /* AC refinement needs a correction bit buffer */
1548 if (entropy->bit_buffer == NULL)
1549 entropy->bit_buffer = (char *) (*cinfo->mem->alloc_small)
1550 ((j_common_ptr) cinfo, JPOOL_IMAGE, MAX_CORR_BITS * SIZEOF(char));
1551 }
1552 }
1553
1554 /* Initialize AC stuff */
1555 entropy->ac_tbl_no = cinfo->cur_comp_info[0]->ac_tbl_no;
1556 entropy->EOBRUN = 0;
1557 entropy->BE = 0;
1558 } else {
1559 if (gather_statistics)
1560 entropy->pub.encode_mcu = encode_mcu_gather;
1561 else
1562 entropy->pub.encode_mcu = encode_mcu_huff;
1563 }
1564
1565 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1566 compptr = cinfo->cur_comp_info[ci];
1567 /* DC needs no table for refinement scan */
1568 if (cinfo->Ss == 0 && cinfo->Ah == 0) {
1569 tbl = compptr->dc_tbl_no;
1570 if (gather_statistics) {
1571 /* Check for invalid table index */
1572 /* (make_c_derived_tbl does this in the other path) */
1573 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
1574 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
1575 /* Allocate and zero the statistics tables */
1576 /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
1577 if (entropy->dc_count_ptrs[tbl] == NULL)
1578 entropy->dc_count_ptrs[tbl] = (long *) (*cinfo->mem->alloc_small)
1579 ((j_common_ptr) cinfo, JPOOL_IMAGE, 257 * SIZEOF(long));
1580 MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long));
1581 } else {
1582 /* Compute derived values for Huffman tables */
1583 /* We may do this more than once for a table, but it's not expensive */
1584 jpeg_make_c_derived_tbl(cinfo, TRUE, tbl,
1585 & entropy->dc_derived_tbls[tbl]);
1586 }
1587 /* Initialize DC predictions to 0 */
1588 entropy->saved.last_dc_val[ci] = 0;
1589 }
1590 /* AC needs no table when not present */
1591 if (cinfo->Se) {
1592 tbl = compptr->ac_tbl_no;
1593 if (gather_statistics) {
1594 if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
1595 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
1596 if (entropy->ac_count_ptrs[tbl] == NULL)
1597 entropy->ac_count_ptrs[tbl] = (long *) (*cinfo->mem->alloc_small)
1598 ((j_common_ptr) cinfo, JPOOL_IMAGE, 257 * SIZEOF(long));
1599 MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long));
1600 } else {
1601 jpeg_make_c_derived_tbl(cinfo, FALSE, tbl,
1602 & entropy->ac_derived_tbls[tbl]);
1603 }
1604 }
1605 }
1606
1607 /* Initialize bit buffer to empty */
1608 entropy->saved.put_buffer = 0;
1609 entropy->saved.put_bits = 0;
1610
1611 /* Initialize restart stuff */
1612 entropy->restarts_to_go = cinfo->restart_interval;
1613 entropy->next_restart_num = 0;
1614}
encode_mcu_AC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
Definition: jchuff.c:628
encode_mcu_DC_first(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
Definition: jchuff.c:545
encode_mcu_gather(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
Definition: jchuff.c:1193
encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
Definition: jchuff.c:1012
encode_mcu_DC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
Definition: jchuff.c:739
jpeg_make_c_derived_tbl(j_compress_ptr cinfo, boolean isDC, int tblno, c_derived_tbl **pdtbl)
Definition: jchuff.c:161
finish_pass_gather(j_compress_ptr cinfo)
Definition: jchuff.c:1465
finish_pass_huff(j_compress_ptr cinfo)
Definition: jchuff.c:1069
encode_mcu_AC_refine(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
Definition: jchuff.c:782

Referenced by jinit_huff_encoder().