| 2000 | } stbi__jpeg; |
| 2001 | |
| 2002 | static int stbi__build_huffman(stbi__huffman *h, int *count) |
| 2003 | { |
| 2004 | int i,j,k=0; |
| 2005 | unsigned int code; |
| 2006 | // build size list for each symbol (from JPEG spec) |
| 2007 | for (i=0; i < 16; ++i) { |
| 2008 | for (j=0; j < count[i]; ++j) { |
| 2009 | h->size[k++] = (stbi_uc) (i+1); |
| 2010 | if(k >= 257) return stbi__err("bad size list","Corrupt JPEG"); |
| 2011 | } |
| 2012 | } |
| 2013 | h->size[k] = 0; |
| 2014 | |
| 2015 | // compute actual symbols (from jpeg spec) |
| 2016 | code = 0; |
| 2017 | k = 0; |
| 2018 | for(j=1; j <= 16; ++j) { |
| 2019 | // compute delta to add to code to compute symbol id |
| 2020 | h->delta[j] = k - code; |
| 2021 | if (h->size[k] == j) { |
| 2022 | while (h->size[k] == j) |
| 2023 | h->code[k++] = (stbi__uint16) (code++); |
| 2024 | if (code-1 >= (1u << j)) return stbi__err("bad code lengths","Corrupt JPEG"); |
| 2025 | } |
| 2026 | // compute largest code + 1 for this size, preshifted as needed later |
| 2027 | h->maxcode[j] = code << (16-j); |
| 2028 | code <<= 1; |
| 2029 | } |
| 2030 | h->maxcode[j] = 0xffffffff; |
| 2031 | |
| 2032 | // build non-spec acceleration table; 255 is flag for not-accelerated |
| 2033 | memset(h->fast, 255, 1 << FAST_BITS); |
| 2034 | for (i=0; i < k; ++i) { |
| 2035 | int s = h->size[i]; |
| 2036 | if (s <= FAST_BITS) { |
| 2037 | int c = h->code[i] << (FAST_BITS-s); |
| 2038 | int m = 1 << (FAST_BITS-s); |
| 2039 | for (j=0; j < m; ++j) { |
| 2040 | h->fast[c+j] = (stbi_uc) i; |
| 2041 | } |
| 2042 | } |
| 2043 | } |
| 2044 | return 1; |
| 2045 | } |
| 2046 | |
| 2047 | // build a table that decodes both magnitude and value of small ACs in |
| 2048 | // one go. |
no test coverage detected