| 3070 | // |
| 3071 | |
| 3072 | static int hufCompress(const unsigned short raw[], int nRaw, |
| 3073 | char compressed[]) { |
| 3074 | if (nRaw == 0) return 0; |
| 3075 | |
| 3076 | std::vector<long long> freq(HUF_ENCSIZE); |
| 3077 | |
| 3078 | countFrequencies(freq, raw, nRaw); |
| 3079 | |
| 3080 | int im = 0; |
| 3081 | int iM = 0; |
| 3082 | hufBuildEncTable(freq.data(), &im, &iM); |
| 3083 | |
| 3084 | char *tableStart = compressed + 20; |
| 3085 | char *tableEnd = tableStart; |
| 3086 | hufPackEncTable(freq.data(), im, iM, &tableEnd); |
| 3087 | int tableLength = tableEnd - tableStart; |
| 3088 | |
| 3089 | char *dataStart = tableEnd; |
| 3090 | int nBits = hufEncode(freq.data(), raw, nRaw, iM, dataStart); |
| 3091 | int data_length = (nBits + 7) / 8; |
| 3092 | |
| 3093 | writeUInt(compressed, im); |
| 3094 | writeUInt(compressed + 4, iM); |
| 3095 | writeUInt(compressed + 8, tableLength); |
| 3096 | writeUInt(compressed + 12, nBits); |
| 3097 | writeUInt(compressed + 16, 0); // room for future extensions |
| 3098 | |
| 3099 | return dataStart + data_length - compressed; |
| 3100 | } |
| 3101 | |
| 3102 | static bool hufUncompress(const char compressed[], int nCompressed, |
| 3103 | std::vector<unsigned short> *raw) { |
no test coverage detected