Jon's code includes the pre-generated Huffman codes I don't like these "magic constants" and compute them on my own :-)
| 325 | // Jon's code includes the pre-generated Huffman codes |
| 326 | // I don't like these "magic constants" and compute them on my own :-) |
| 327 | void generateHuffmanTable(const uint8_t numCodes[16], const uint8_t* values, BitCode result[256]) |
| 328 | { |
| 329 | // process all bitsizes 1 thru 16, no JPEG Huffman code is allowed to exceed 16 bits |
| 330 | auto huffmanCode = 0; |
| 331 | for (auto numBits = 1; numBits <= 16; numBits++) |
| 332 | { |
| 333 | // ... and each code of these bitsizes |
| 334 | for (auto i = 0; i < numCodes[numBits - 1]; i++) // note: numCodes array starts at zero, but smallest bitsize is 1 |
| 335 | result[*values++] = BitCode(huffmanCode++, numBits); |
| 336 | |
| 337 | // next Huffman code needs to be one bit wider |
| 338 | huffmanCode <<= 1; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | } // end of anonymous namespace |
| 343 |