Does a RLE compression run of the values given the byte array 'val'.
| 2224 | |
| 2225 | // Does a RLE compression run of the values given the byte array 'val'. |
| 2226 | int WriteCompressionByte(CFILE *fp, uint8_t *val, int total, int just_count, int compress) { |
| 2227 | int done = 0; |
| 2228 | int written = 0; |
| 2229 | int curptr = 0; |
| 2230 | |
| 2231 | ASSERT(!(just_count == 1 && compress == 0)); |
| 2232 | |
| 2233 | if (compress == NO_COMPRESS) { |
| 2234 | int i; |
| 2235 | |
| 2236 | // Write 0 to indicate no compression |
| 2237 | cf_WriteByte(fp, NO_COMPRESS); |
| 2238 | for (i = 0; i < total; i++) |
| 2239 | cf_WriteByte(fp, val[i]); |
| 2240 | |
| 2241 | return total; |
| 2242 | } else { |
| 2243 | // Indicate compression |
| 2244 | if (!just_count) |
| 2245 | cf_WriteByte(fp, COMPRESS); |
| 2246 | } |
| 2247 | |
| 2248 | while (!done) { |
| 2249 | if (curptr == total) { |
| 2250 | done = 1; |
| 2251 | continue; |
| 2252 | } |
| 2253 | |
| 2254 | ASSERT(curptr < total); |
| 2255 | |
| 2256 | uint8_t curval = val[curptr]; |
| 2257 | uint8_t count = 1; |
| 2258 | |
| 2259 | while ((curptr + count) < total && val[curptr + count] == curval && count < 250) |
| 2260 | count++; |
| 2261 | |
| 2262 | written += 2; |
| 2263 | |
| 2264 | if (just_count) { |
| 2265 | curptr += count; |
| 2266 | continue; |
| 2267 | } |
| 2268 | |
| 2269 | if (count == 1) { |
| 2270 | cf_WriteByte(fp, 0); |
| 2271 | cf_WriteByte(fp, curval); |
| 2272 | } else { |
| 2273 | cf_WriteByte(fp, count); |
| 2274 | cf_WriteByte(fp, curval); |
| 2275 | } |
| 2276 | |
| 2277 | curptr += count; |
| 2278 | } |
| 2279 | |
| 2280 | return written; |
| 2281 | } |
| 2282 | |
| 2283 | // Does a RLE compression run of the values given the int16_t array 'val'. |
no test coverage detected