ReactOS 0.4.16-dev-2613-g9533ad7
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 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, JBLOCKARRAY MCU_data)
 
 encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKARRAY MCU_data)
 
 encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKARRAY MCU_data)
 
 encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKARRAY 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, JBLOCKARRAY 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, JBLOCKARRAY 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 61 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:203

Definition at line 255 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
action
Definition: namespace.c:707
dump_buffer_s(working_state *state)
Definition: jchuff.c:262

Definition at line 248 of file jchuff.c.

◆ IRIGHT_SHIFT

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

Definition at line 145 of file jchuff.c.

◆ ISHIFT_TEMPS

#define ISHIFT_TEMPS

Definition at line 144 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_CORR_BITS

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

Definition at line 130 of file jchuff.c.

Typedef Documentation

◆ huff_entropy_ptr

Definition at line 111 of file jchuff.c.

Function Documentation

◆ dump_buffer_e()

dump_buffer_e ( huff_entropy_ptr  entropy)

Definition at line 277 of file jchuff.c.

279{
280 struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
281
282 if (! (*dest->empty_output_buffer) (entropy->cinfo))
283 ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
284 /* After a successful buffer dump, must reset buffer pointers */
285 entropy->next_output_byte = dest->next_output_byte;
286 entropy->free_in_buffer = dest->free_in_buffer;
287}
static char * dest
Definition: rtl.c:135
#define ERREXIT(msg)
Definition: rdjpgcom.c:72
j_compress_ptr cinfo
Definition: jchuff.c:101
JOCTET * next_output_byte
Definition: jchuff.c:99
size_t free_in_buffer
Definition: jchuff.c:100
struct jpeg_destination_mgr * dest
Definition: jpeglib.h:295

◆ dump_buffer_s()

dump_buffer_s ( working_state state)

Definition at line 262 of file jchuff.c.

264{
265 struct jpeg_destination_mgr * dest = state->cinfo->dest;
266
267 if (! (*dest->empty_output_buffer) (state->cinfo))
268 return FALSE;
269 /* After a successful buffer dump, must reset buffer pointers */
270 state->next_output_byte = dest->next_output_byte;
271 state->free_in_buffer = dest->free_in_buffer;
272 return TRUE;
273}
#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 422 of file jchuff.c.

423{
424 if (entropy->gather_statistics)
425 entropy->ac_count_ptrs[tbl_no][symbol]++;
426 else {
427 c_derived_tbl * tbl = entropy->ac_derived_tbls[tbl_no];
428 emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
429 }
430}
INLINE emit_bits_e(huff_entropy_ptr entropy, unsigned int code, int size)
Definition: jchuff.c:342
unsigned int ehufco[256]
Definition: jchuff.c:37
char ehufsi[256]
Definition: jchuff.c:38
c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]
Definition: jchuff.c:86
boolean gather_statistics
Definition: jchuff.c:95
long * ac_count_ptrs[NUM_HUFF_TBLS]
Definition: jchuff.c:90

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 342 of file jchuff.c.

344{
345 /* This routine is heavily used, so it's worth coding tightly. */
346 register INT32 put_buffer;
347 register int put_bits;
348
349 /* if size is 0, caller used an invalid Huffman table entry */
350 if (size == 0)
351 ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
352
353 if (entropy->gather_statistics)
354 return; /* do nothing if we're only getting stats */
355
356 /* mask off any extra bits in code */
357 put_buffer = ((INT32) code) & ((((INT32) 1) << size) - 1);
358
359 /* new number of bits in buffer */
360 put_bits = size + entropy->saved.put_bits;
361
362 put_buffer <<= 24 - put_bits; /* align incoming bits */
363
364 /* and merge with old buffer contents */
365 put_buffer |= entropy->saved.put_buffer;
366
367 while (put_bits >= 8) {
368 int c = (int) ((put_buffer >> 16) & 0xFF);
369
370 emit_byte_e(entropy, c);
371 if (c == 0xFF) { /* need to stuff a zero byte? */
372 emit_byte_e(entropy, 0);
373 }
374 put_buffer <<= 8;
375 put_bits -= 8;
376 }
377
378 entropy->saved.put_buffer = put_buffer; /* update variables */
379 entropy->saved.put_bits = put_bits;
380}
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:255
Definition: inflate.c:139
savable_state saved
Definition: jchuff.c:78
INT32 put_buffer
Definition: jchuff.c:50
int put_bits
Definition: jchuff.c:51
int32_t INT32
Definition: typedefs.h:58

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 300 of file jchuff.c.

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

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 438 of file jchuff.c.

440{
441 if (entropy->gather_statistics)
442 return; /* no real work */
443
444 while (nbits > 0) {
445 emit_bits_e(entropy, (unsigned int) (*bufstart), 1);
446 bufstart++;
447 nbits--;
448 }
449}

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 409 of file jchuff.c.

410{
411 if (entropy->gather_statistics)
412 entropy->dc_count_ptrs[tbl_no][symbol]++;
413 else {
414 c_derived_tbl * tbl = entropy->dc_derived_tbls[tbl_no];
415 emit_bits_e(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
416 }
417}
c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]
Definition: jchuff.c:85
long * dc_count_ptrs[NUM_HUFF_TBLS]
Definition: jchuff.c:89

Referenced by encode_mcu_DC_first().

◆ emit_eobrun()

emit_eobrun ( huff_entropy_ptr  entropy)

Definition at line 457 of file jchuff.c.

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

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 509 of file jchuff.c.

510{
511 int ci;
512
513 emit_eobrun(entropy);
514
515 if (! entropy->gather_statistics) {
516 flush_bits_e(entropy);
517 emit_byte_e(entropy, 0xFF);
518 emit_byte_e(entropy, JPEG_RST0 + restart_num);
519 }
520
521 if (entropy->cinfo->Ss == 0) {
522 /* Re-initialize DC predictions to 0 */
523 for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
524 entropy->saved.last_dc_val[ci] = 0;
525 } else {
526 /* Re-initialize all AC-related fields to 0 */
527 entropy->EOBRUN = 0;
528 entropy->BE = 0;
529 }
530}
emit_eobrun(huff_entropy_ptr entropy)
Definition: jchuff.c:457
flush_bits_e(huff_entropy_ptr entropy)
Definition: jchuff.c:395
#define JPEG_RST0
Definition: jpeglib.h:1128
int last_dc_val[MAX_COMPS_IN_SCAN]
Definition: jchuff.c:52

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 488 of file jchuff.c.

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

Referenced by encode_mcu_huff().

◆ encode_mcu_AC_first()

encode_mcu_AC_first ( j_compress_ptr  cinfo,
JBLOCKARRAY  MCU_data 
)

Definition at line 626 of file jchuff.c.

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

Definition at line 786 of file jchuff.c.

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

Referenced by start_pass_huff().

◆ encode_mcu_DC_first()

encode_mcu_DC_first ( j_compress_ptr  cinfo,
JBLOCKARRAY  MCU_data 
)

Definition at line 539 of file jchuff.c.

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

Referenced by start_pass_huff().

◆ encode_mcu_DC_refine()

encode_mcu_DC_refine ( j_compress_ptr  cinfo,
JBLOCKARRAY  MCU_data 
)

Definition at line 743 of file jchuff.c.

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

Referenced by start_pass_huff().

◆ encode_mcu_gather()

encode_mcu_gather ( j_compress_ptr  cinfo,
JBLOCKARRAY  MCU_data 
)

Definition at line 1209 of file jchuff.c.

1210{
1211 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1212 int blkn, ci;
1214
1215 /* Take care of restart intervals if needed */
1216 if (cinfo->restart_interval) {
1217 if (entropy->restarts_to_go == 0) {
1218 /* Re-initialize DC predictions to 0 */
1219 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
1220 entropy->saved.last_dc_val[ci] = 0;
1221 /* Update restart state */
1222 entropy->restarts_to_go = cinfo->restart_interval;
1223 }
1224 entropy->restarts_to_go--;
1225 }
1226
1227 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
1228 ci = cinfo->MCU_membership[blkn];
1229 compptr = cinfo->cur_comp_info[ci];
1230 htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
1231 entropy->dc_count_ptrs[compptr->dc_tbl_no],
1232 entropy->ac_count_ptrs[compptr->ac_tbl_no]);
1233 entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
1234 }
1235
1236 return TRUE;
1237}
htest_one_block(j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val, long dc_counts[], long ac_counts[])
Definition: jchuff.c:1128
jpeg_component_info * compptr
Definition: jdct.h:252

Referenced by start_pass_huff().

◆ encode_mcu_huff()

encode_mcu_huff ( j_compress_ptr  cinfo,
JBLOCKARRAY  MCU_data 
)

Definition at line 1022 of file jchuff.c.

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

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 916 of file jchuff.c.

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

Referenced by encode_mcu_huff().

◆ finish_pass_gather()

finish_pass_gather ( j_compress_ptr  cinfo)

Definition at line 1481 of file jchuff.c.

1482{
1483 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1484 int ci, tbl;
1486 JHUFF_TBL **htblptr;
1487 boolean did_dc[NUM_HUFF_TBLS];
1488 boolean did_ac[NUM_HUFF_TBLS];
1489
1490 if (cinfo->progressive_mode)
1491 /* Flush out buffered data (all we care about is counting the EOB symbol) */
1492 emit_eobrun(entropy);
1493
1494 /* It's important not to apply jpeg_gen_optimal_table more than once
1495 * per table, because it clobbers the input frequency counts!
1496 */
1497 MEMZERO(did_dc, SIZEOF(did_dc));
1498 MEMZERO(did_ac, SIZEOF(did_ac));
1499
1500 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
1501 compptr = cinfo->cur_comp_info[ci];
1502 /* DC needs no table for refinement scan */
1503 if (cinfo->Ss == 0 && cinfo->Ah == 0) {
1504 tbl = compptr->dc_tbl_no;
1505 if (! did_dc[tbl]) {
1506 htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
1507 if (*htblptr == NULL)
1508 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
1509 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[tbl]);
1510 did_dc[tbl] = TRUE;
1511 }
1512 }
1513 /* AC needs no table when not present */
1514 if (cinfo->Se) {
1515 tbl = compptr->ac_tbl_no;
1516 if (! did_ac[tbl]) {
1517 htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
1518 if (*htblptr == NULL)
1519 *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
1520 jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[tbl]);
1521 did_ac[tbl] = TRUE;
1522 }
1523 }
1524 }
1525}
#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:1268
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:405
#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 1079 of file jchuff.c.

1080{
1081 huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
1083
1084 if (cinfo->progressive_mode) {
1085 entropy->next_output_byte = cinfo->dest->next_output_byte;
1086 entropy->free_in_buffer = cinfo->dest->free_in_buffer;
1087
1088 /* Flush out any buffered data */
1089 emit_eobrun(entropy);
1090 flush_bits_e(entropy);
1091
1092 cinfo->dest->next_output_byte = entropy->next_output_byte;
1093 cinfo->dest->free_in_buffer = entropy->free_in_buffer;
1094 } else {
1095 /* Load up working state ... flush_bits needs it */
1096 state.next_output_byte = cinfo->dest->next_output_byte;
1097 state.free_in_buffer = cinfo->dest->free_in_buffer;
1098 ASSIGN_STATE(state.cur, entropy->saved);
1099 state.cinfo = cinfo;
1100
1101 /* Flush out the last data */
1102 if (! flush_bits_s(&state))
1103 ERREXIT(cinfo, JERR_CANT_SUSPEND);
1104
1105 /* Update state */
1106 cinfo->dest->next_output_byte = state.next_output_byte;
1107 cinfo->dest->free_in_buffer = state.free_in_buffer;
1108 ASSIGN_STATE(entropy->saved, state.cur);
1109 }
1110}

Referenced by start_pass_huff().

◆ flush_bits_e()

flush_bits_e ( huff_entropy_ptr  entropy)

Definition at line 395 of file jchuff.c.

396{
397 emit_bits_e(entropy, 0x7F, 7); /* fill any partial byte with ones */
398 entropy->saved.put_buffer = 0; /* and reset bit-buffer to empty */
399 entropy->saved.put_bits = 0;
400}

Referenced by emit_restart_e(), and finish_pass_huff().

◆ flush_bits_s()

flush_bits_s ( working_state state)

Definition at line 384 of file jchuff.c.

385{
386 if (! emit_bits_s(state, 0x7F, 7)) /* fill any partial byte with ones */
387 return FALSE;
388 state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
389 state->cur.put_bits = 0;
390 return TRUE;
391}

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 1128 of file jchuff.c.

1130{
1131 register int temp;
1132 register int nbits;
1133 register int r, k;
1134 int Se = cinfo->lim_Se;
1135 int max_coef_bits = cinfo->data_precision + 3;
1136 const int * natural_order = cinfo->natural_order;
1137
1138 /* Encode the DC coefficient difference per section F.1.2.1 */
1139
1140 if ((temp = block[0] - last_dc_val) == 0) {
1141 /* Count the Huffman symbol for the number of bits */
1142 dc_counts[0]++;
1143 } else {
1144 if (temp < 0)
1145 temp = -temp; /* temp is abs value of input */
1146
1147 /* Find the number of bits needed for the magnitude of the coefficient */
1148 nbits = 0;
1149 do nbits++; /* there must be at least one 1 bit */
1150 while ((temp >>= 1));
1151 /* Check for out-of-range coefficient values.
1152 * Since we're encoding a difference, the range limit is twice as much.
1153 */
1154 if (nbits > max_coef_bits)
1155 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
1156
1157 /* Count the Huffman symbol for the number of bits */
1158 dc_counts[nbits]++;
1159 }
1160
1161 /* Encode the AC coefficients per section F.1.2.2 */
1162
1163 r = 0; /* r = run length of zeros */
1164
1165 for (k = 1; k <= Se; k++) {
1166 if ((temp = block[natural_order[k]]) == 0) {
1167 r++;
1168 continue;
1169 }
1170
1171 /* if run length > 15, must emit special run-length-16 codes (0xF0) */
1172 while (r > 15) {
1173 ac_counts[0xF0]++;
1174 r -= 16;
1175 }
1176
1177 if (temp < 0)
1178 temp = -temp; /* temp is abs value of input */
1179
1180 /* Find the number of bits needed for the magnitude of the coefficient */
1181 nbits = 0;
1182 do nbits++; /* there must be at least one 1 bit */
1183 while ((temp >>= 1));
1184 /* Check for out-of-range coefficient values.
1185 * Use ">=" instead of ">" so can use the
1186 * same one larger limit from DC check here.
1187 */
1188 if (nbits >= max_coef_bits)
1189 ERREXIT(cinfo, JERR_BAD_DCT_COEF);
1190
1191 /* Count Huffman symbol for run length / number of bits */
1192 ac_counts[(r << 4) + nbits]++;
1193
1194 r = 0; /* reset zero run length */
1195 }
1196
1197 /* If the last coef(s) were zero, emit an end-of-block code */
1198 if (r > 0)
1199 ac_counts[0]++;
1200}

Referenced by encode_mcu_gather().

◆ jinit_huff_encoder()

jinit_huff_encoder ( j_compress_ptr  cinfo)

Definition at line 1638 of file jchuff.c.

1639{
1640 huff_entropy_ptr entropy;
1641 int i;
1642
1643 entropy = (huff_entropy_ptr) (*cinfo->mem->alloc_small)
1645 cinfo->entropy = &entropy->pub;
1646 entropy->pub.start_pass = start_pass_huff;
1647
1648 /* Mark tables unallocated */
1649 for (i = 0; i < NUM_HUFF_TBLS; i++) {
1650 entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
1651 entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
1652 }
1653
1654 if (cinfo->progressive_mode)
1655 entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
1656}
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:1535
struct jpeg_common_struct * j_common_ptr
Definition: jpeglib.h:284
#define JPOOL_IMAGE
Definition: jpeglib.h:810
struct jpeg_entropy_encoder pub
Definition: jchuff.c:76

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 1268 of file jchuff.c.

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

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

Referenced by start_pass_huff().

◆ start_pass_huff()

start_pass_huff ( j_compress_ptr  cinfo,
boolean  gather_statistics 
)

Definition at line 1535 of file jchuff.c.

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

Referenced by jinit_huff_encoder().