Main MRPT website > C++ reference for MRPT 1.5.6
jdhuff.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 /* Short forms of external names for systems with brain-damaged linkers. */
11 
12 #ifdef NEED_SHORT_EXTERNAL_NAMES
13 #define jpeg_make_d_derived_tbl jMkDDerived
14 #define jpeg_fill_bit_buffer jFilBitBuf
15 #define jpeg_huff_decode jHufDecode
16 #endif /* NEED_SHORT_EXTERNAL_NAMES */
17 
18 
19 /* Derived data constructed for each Huffman table */
20 
21 #define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */
22 
23 typedef struct {
24  /* Basic tables: (element [0] of each array is unused) */
25  INT32 maxcode[18]; /* largest code of length k (-1 if none) */
26  /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
27  INT32 valoffset[17]; /* huffval[] offset for codes of length k */
28  /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
29  * the smallest code of length k; so given a code of length k, the
30  * corresponding symbol is huffval[code + valoffset[k]]
31  */
32 
33  /* Link to public Huffman table (needed only in jpeg_huff_decode) */
35 
36  /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
37  * the input data stream. If the next Huffman code is no more
38  * than HUFF_LOOKAHEAD bits long, we can obtain its length and
39  * the corresponding symbol directly from these tables.
40  */
41  int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
42  UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
44 
45 /* Expand a Huffman table definition into the derived format */
47  JPP((j_decompress_ptr cinfo, boolean isDC, int tblno,
48  d_derived_tbl ** pdtbl));
49 
50 
51 /*
52  * Fetching the next N bits from the input stream is a time-critical operation
53  * for the Huffman decoders. We implement it with a combination of inline
54  * macros and out-of-line subroutines. Note that N (the number of bits
55  * demanded at one time) never exceeds 15 for JPEG use.
56  *
57  * We read source bytes into get_buffer and dole out bits as needed.
58  * If get_buffer already contains enough bits, they are fetched in-line
59  * by the macros CHECK_BIT_BUFFER and GET_BITS. When there aren't enough
60  * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
61  * as full as possible (not just to the number of bits needed; this
62  * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
63  * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
64  * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
65  * at least the requested number of bits --- dummy zeroes are inserted if
66  * necessary.
67  */
68 
69 typedef INT32 bit_buf_type; /* type of bit-extraction buffer */
70 #define BIT_BUF_SIZE 32 /* size of buffer in bits */
71 
72 /* If long is > 32 bits on your machine, and shifting/masking longs is
73  * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
74  * appropriately should be a win. Unfortunately we can't define the size
75  * with something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
76  * because not all machines measure sizeof in 8-bit bytes.
77  */
78 
79 typedef struct { /* Bitreading state saved across MCUs */
80  bit_buf_type get_buffer; /* current bit-extraction buffer */
81  int bits_left; /* # of unused bits in it */
83 
84 typedef struct { /* Bitreading working state within an MCU */
85  /* Current data source location */
86  /* We need a copy, rather than munging the original, in case of suspension */
87  const JOCTET * next_input_byte; /* => next byte to read from source */
88  size_t bytes_in_buffer; /* # of bytes remaining in source buffer */
89  /* Bit input buffer --- note these values are kept in variables,
90  * not in this struct, inside the inner loops.
91  */
92  bit_buf_type get_buffer; /* current bit-extraction buffer */
93  int bits_left; /* # of unused bits in it */
94  /* Pointer needed by jpeg_fill_bit_buffer. */
95  j_decompress_ptr cinfo; /* back link to decompress master record */
97 
98 /* Macros to declare and load/save bitread local variables. */
99 #define BITREAD_STATE_VARS \
100  bit_buf_type get_buffer; \
101  int bits_left; \
102  bitread_working_state br_state
103 
104 #define BITREAD_LOAD_STATE(cinfop,permstate) \
105  br_state.cinfo = cinfop; \
106  br_state.next_input_byte = cinfop->src->next_input_byte; \
107  br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
108  get_buffer = permstate.get_buffer; \
109  bits_left = permstate.bits_left;
110 
111 #define BITREAD_SAVE_STATE(cinfop,permstate) \
112  cinfop->src->next_input_byte = br_state.next_input_byte; \
113  cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
114  permstate.get_buffer = get_buffer; \
115  permstate.bits_left = bits_left
116 
117 /*
118  * These macros provide the in-line portion of bit fetching.
119  * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
120  * before using GET_BITS, PEEK_BITS, or DROP_BITS.
121  * The variables get_buffer and bits_left are assumed to be locals,
122  * but the state struct might not be (jpeg_huff_decode needs this).
123  * CHECK_BIT_BUFFER(state,n,action);
124  * Ensure there are N bits in get_buffer; if suspend, take action.
125  * val = GET_BITS(n);
126  * Fetch next N bits.
127  * val = PEEK_BITS(n);
128  * Fetch next N bits without removing them from the buffer.
129  * DROP_BITS(n);
130  * Discard next N bits.
131  * The value N should be a simple variable, not an expression, because it
132  * is evaluated multiple times.
133  */
134 
135 #define CHECK_BIT_BUFFER(state,nbits,action) \
136  { if (bits_left < (nbits)) { \
137  if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits)) \
138  { action; } \
139  get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }
140 
141 #define GET_BITS(nbits) \
142  (((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1))
143 
144 #define PEEK_BITS(nbits) \
145  (((int) (get_buffer >> (bits_left - (nbits)))) & ((1<<(nbits))-1))
146 
147 #define DROP_BITS(nbits) \
148  (bits_left -= (nbits))
149 
150 /* Load up the bit buffer to a depth of at least nbits */
153  int bits_left, int nbits));
154 
155 
156 /*
157  * Code for extracting next Huffman-coded symbol from input bit stream.
158  * Again, this is time-critical and we make the main paths be macros.
159  *
160  * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
161  * without looping. Usually, more than 95% of the Huffman codes will be 8
162  * or fewer bits long. The few overlength codes are handled with a loop,
163  * which need not be inline code.
164  *
165  * Notes about the HUFF_DECODE macro:
166  * 1. Near the end of the data segment, we may fail to get enough bits
167  * for a lookahead. In that case, we do it the hard way.
168  * 2. If the lookahead table contains no entry, the next code must be
169  * more than HUFF_LOOKAHEAD bits long.
170  * 3. jpeg_huff_decode returns -1 if forced to suspend.
171  */
172 
173 #define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \
174 { int nb, look; \
175  if (bits_left < HUFF_LOOKAHEAD) { \
176  if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \
177  get_buffer = state.get_buffer; bits_left = state.bits_left; \
178  if (bits_left < HUFF_LOOKAHEAD) { \
179  nb = 1; goto slowlabel; \
180  } \
181  } \
182  look = PEEK_BITS(HUFF_LOOKAHEAD); \
183  if ((nb = htbl->look_nbits[look]) != 0) { \
184  DROP_BITS(nb); \
185  result = htbl->look_sym[look]; \
186  } else { \
187  nb = HUFF_LOOKAHEAD+1; \
188 slowlabel: \
189  if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
190  { failaction; } \
191  get_buffer = state.get_buffer; bits_left = state.bits_left; \
192  } \
193 }
194 
195 /* Out-of-line case for Huffman code fetching */
198  int bits_left, d_derived_tbl * htbl, int min_bits));
bit_buf_type get_buffer
Definition: jdhuff.h:92
jpeg_huff_decode(bitread_working_state *state, bit_buf_type get_buffer, int bits_left, d_derived_tbl *htbl, int min_bits)
Definition: jdhuff.cpp:391
boolean int d_derived_tbl ** pdtbl
Definition: jdhuff.h:47
jpeg_fill_bit_buffer(bitread_working_state *state, bit_buf_type get_buffer, int bits_left, int nbits)
Definition: jdhuff.cpp:285
bit_buf_type int d_derived_tbl int min_bits
Definition: jdhuff.h:197
const JOCTET * next_input_byte
Definition: jdhuff.h:87
bit_buf_type int bits_left
Definition: jdhuff.h:152
size_t bytes_in_buffer
Definition: jdhuff.h:88
long INT32
Definition: jmorecfg.h:158
j_decompress_ptr cinfo
Definition: jdhuff.h:95
EXTERN(void) jpeg_make_d_derived_tbl JPP((j_decompress_ptr cinfo
boolean isDC
Definition: jdhuff.h:47
bit_buf_type int d_derived_tbl * htbl
Definition: jdhuff.h:197
bit_buf_type get_buffer
Definition: jdhuff.h:80
short UINT8
Definition: jmorecfg.h:137
boolean int tblno
Definition: jdhuff.h:47
bit_buf_type int int nbits
Definition: jdhuff.h:152
bit_buf_type get_buffer
Definition: jdhuff.h:152
#define JPP(arglist)
Definition: mrpt_jpeglib.h:815
INT32 bit_buf_type
Definition: jdhuff.h:69
#define HUFF_LOOKAHEAD
Definition: jdhuff.h:21
jpeg_make_d_derived_tbl(j_decompress_ptr cinfo, boolean isDC, int tblno, d_derived_tbl **pdtbl)
Definition: jdhuff.cpp:142
char JOCTET
Definition: jmorecfg.h:112
JHUFF_TBL * pub
Definition: jdhuff.h:34



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019