1 | /*
|
---|
2 | * jcphuff.c
|
---|
3 | *
|
---|
4 | * Copyright (C) 1995-1997, Thomas G. Lane.
|
---|
5 | * This file is part of the Independent JPEG Group's software.
|
---|
6 | * For conditions of distribution and use, see the accompanying README file.
|
---|
7 | *
|
---|
8 | * This file contains Huffman entropy encoding routines for progressive JPEG.
|
---|
9 | *
|
---|
10 | * We do not support output suspension in this module, since the library
|
---|
11 | * currently does not allow multiple-scan files to be written with output
|
---|
12 | * suspension.
|
---|
13 | */
|
---|
14 |
|
---|
15 | #define JPEG_INTERNALS
|
---|
16 | #include "jinclude.h"
|
---|
17 | #include "jpeglib.h"
|
---|
18 | #include "jchuff.h" /* Declarations shared with jchuff.c */
|
---|
19 |
|
---|
20 | #ifdef C_PROGRESSIVE_SUPPORTED
|
---|
21 |
|
---|
22 | /* Expanded entropy encoder object for progressive Huffman encoding. */
|
---|
23 |
|
---|
24 | typedef struct {
|
---|
25 | struct jpeg_entropy_encoder pub; /* public fields */
|
---|
26 |
|
---|
27 | /* Mode flag: TRUE for optimization, FALSE for actual data output */
|
---|
28 | boolean gather_statistics;
|
---|
29 |
|
---|
30 | /* Bit-level coding status.
|
---|
31 | * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
|
---|
32 | */
|
---|
33 | JOCTET * next_output_byte; /* => next byte to write in buffer */
|
---|
34 | size_t free_in_buffer; /* # of byte spaces remaining in buffer */
|
---|
35 | INT32 put_buffer; /* current bit-accumulation buffer */
|
---|
36 | int put_bits; /* # of bits now in it */
|
---|
37 | j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
|
---|
38 |
|
---|
39 | /* Coding status for DC components */
|
---|
40 | int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
|
---|
41 |
|
---|
42 | /* Coding status for AC components */
|
---|
43 | int ac_tbl_no; /* the table number of the single component */
|
---|
44 | unsigned int EOBRUN; /* run length of EOBs */
|
---|
45 | unsigned int BE; /* # of buffered correction bits before MCU */
|
---|
46 | char * bit_buffer; /* buffer for correction bits (1 per char) */
|
---|
47 | /* packing correction bits tightly would save some space but cost time... */
|
---|
48 |
|
---|
49 | unsigned int restarts_to_go; /* MCUs left in this restart interval */
|
---|
50 | int next_restart_num; /* next restart number to write (0-7) */
|
---|
51 |
|
---|
52 | /* Pointers to derived tables (these workspaces have image lifespan).
|
---|
53 | * Since any one scan codes only DC or only AC, we only need one set
|
---|
54 | * of tables, not one for DC and one for AC.
|
---|
55 | */
|
---|
56 | c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
|
---|
57 |
|
---|
58 | /* Statistics tables for optimization; again, one set is enough */
|
---|
59 | long * count_ptrs[NUM_HUFF_TBLS];
|
---|
60 | } phuff_entropy_encoder;
|
---|
61 |
|
---|
62 | typedef phuff_entropy_encoder * phuff_entropy_ptr;
|
---|
63 |
|
---|
64 | /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
|
---|
65 | * buffer can hold. Larger sizes may slightly improve compression, but
|
---|
66 | * 1000 is already well into the realm of overkill.
|
---|
67 | * The minimum safe size is 64 bits.
|
---|
68 | */
|
---|
69 |
|
---|
70 | #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
|
---|
71 |
|
---|
72 | /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
|
---|
73 | * We assume that int right shift is unsigned if INT32 right shift is,
|
---|
74 | * which should be safe.
|
---|
75 | */
|
---|
76 |
|
---|
77 | #ifdef RIGHT_SHIFT_IS_UNSIGNED
|
---|
78 | #define ISHIFT_TEMPS int ishift_temp;
|
---|
79 | #define IRIGHT_SHIFT(x,shft) \
|
---|
80 | ((ishift_temp = (x)) < 0 ? \
|
---|
|
---|