MCPcopy Create free account
hub / github.com/OpenFodder/openfodder / stbi__jpeg_huff_decode

Function stbi__jpeg_huff_decode

Source/Utils/stb_image.h:2096–2145  ·  view source on GitHub ↗

decode a jpeg huffman value from the bitstream

Source from the content-addressed store, hash-verified

2094
2095// decode a jpeg huffman value from the bitstream
2096stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)
2097{
2098 unsigned int temp;
2099 int c,k;
2100
2101 if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
2102
2103 // look at the top FAST_BITS and determine what symbol ID it is,
2104 // if the code is <= FAST_BITS
2105 c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
2106 k = h->fast[c];
2107 if (k < 255) {
2108 int s = h->size[k];
2109 if (s > j->code_bits)
2110 return -1;
2111 j->code_buffer <<= s;
2112 j->code_bits -= s;
2113 return h->values[k];
2114 }
2115
2116 // naive test is to shift the code_buffer down so k bits are
2117 // valid, then test against maxcode. To speed this up, we've
2118 // preshifted maxcode left so that it has (16-k) 0s at the
2119 // end; in other words, regardless of the number of bits, it
2120 // wants to be compared against something shifted to have 16;
2121 // that way we don't need to shift inside the loop.
2122 temp = j->code_buffer >> 16;
2123 for (k=FAST_BITS+1 ; ; ++k)
2124 if (temp < h->maxcode[k])
2125 break;
2126 if (k == 17) {
2127 // error! code not found
2128 j->code_bits -= 16;
2129 return -1;
2130 }
2131
2132 if (k > j->code_bits)
2133 return -1;
2134
2135 // convert the huffman code to the symbol id
2136 c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
2137 if(c < 0 || c >= 256) // symbol id out of bounds!
2138 return -1;
2139 STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]);
2140
2141 // convert the id to a symbol
2142 j->code_bits -= k;
2143 j->code_buffer <<= k;
2144 return h->values[c];
2145}
2146
2147// bias[n] = (-1<<n) + 1
2148static const int stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767};

Callers 3

stbi__jpeg_decode_blockFunction · 0.85

Calls 1

stbi__grow_buffer_unsafeFunction · 0.85

Tested by

no test coverage detected