decode a jpeg huffman value from the bitstream
| 2240 | |
| 2241 | // decode a jpeg huffman value from the bitstream |
| 2242 | stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg* j, stbi__huffman* h) { |
| 2243 | unsigned int temp; |
| 2244 | int c, k; |
| 2245 | |
| 2246 | if (j->code_bits < 16) |
| 2247 | stbi__grow_buffer_unsafe(j); |
| 2248 | |
| 2249 | // look at the top FAST_BITS and determine what symbol ID it is, |
| 2250 | // if the code is <= FAST_BITS |
| 2251 | c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS) - 1); |
| 2252 | k = h->fast[c]; |
| 2253 | if (k < 255) { |
| 2254 | int s = h->size[k]; |
| 2255 | if (s > j->code_bits) |
| 2256 | return -1; |
| 2257 | j->code_buffer <<= s; |
| 2258 | j->code_bits -= s; |
| 2259 | return h->values[k]; |
| 2260 | } |
| 2261 | |
| 2262 | // naive test is to shift the code_buffer down so k bits are |
| 2263 | // valid, then test against maxcode. To speed this up, we've |
| 2264 | // preshifted maxcode left so that it has (16-k) 0s at the |
| 2265 | // end; in other words, regardless of the number of bits, it |
| 2266 | // wants to be compared against something shifted to have 16; |
| 2267 | // that way we don't need to shift inside the loop. |
| 2268 | temp = j->code_buffer >> 16; |
| 2269 | for (k = FAST_BITS + 1;; ++k) |
| 2270 | if (temp < h->maxcode[k]) |
| 2271 | break; |
| 2272 | if (k == 17) { |
| 2273 | // error! code not found |
| 2274 | j->code_bits -= 16; |
| 2275 | return -1; |
| 2276 | } |
| 2277 | |
| 2278 | if (k > j->code_bits) |
| 2279 | return -1; |
| 2280 | |
| 2281 | // convert the huffman code to the symbol id |
| 2282 | c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k]; |
| 2283 | STBI_ASSERT( |
| 2284 | (((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == |
| 2285 | h->code[c]); |
| 2286 | |
| 2287 | // convert the id to a symbol |
| 2288 | j->code_bits -= k; |
| 2289 | j->code_buffer <<= k; |
| 2290 | return h->values[c]; |
| 2291 | } |
| 2292 | |
| 2293 | // bias[n] = (-1<<n) + 1 |
| 2294 | static const int stbi__jbias[16] = {0, -1, -3, -7, -15, -31, |
no test coverage detected