MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / WriteCompressionShort

Function WriteCompressionShort

Descent3/LoadLevel.cpp:2285–2341  ·  view source on GitHub ↗

Does a RLE compression run of the values given the int16_t array 'val'. If just_count is 1, doesn't write anything

Source from the content-addressed store, hash-verified

2283// Does a RLE compression run of the values given the int16_t array 'val'.
2284// If just_count is 1, doesn't write anything
2285int WriteCompressionShort(CFILE *fp, uint16_t *val, int total, int just_count, int compress) {
2286 int done = 0, written = 0;
2287
2288 int curptr = 0;
2289
2290 ASSERT(!(just_count == 1 && compress == 0));
2291
2292 if (compress == NO_COMPRESS) {
2293 int i;
2294
2295 // Write 0 to indicate no compression
2296 cf_WriteByte(fp, NO_COMPRESS);
2297
2298 for (i = 0; i < total; i++)
2299 cf_WriteShort(fp, val[i]);
2300
2301 return total;
2302 } else {
2303 // Indicate compression
2304 if (!just_count)
2305 cf_WriteByte(fp, COMPRESS);
2306 }
2307
2308 while (!done) {
2309 if (curptr == total) {
2310 done = 1;
2311 continue;
2312 }
2313
2314 ASSERT(curptr < total);
2315
2316 uint16_t curval = val[curptr];
2317 uint8_t count = 1;
2318
2319 while ((curptr + count) < total && val[curptr + count] == curval && count < 250)
2320 count++;
2321
2322 written += 3;
2323
2324 if (just_count) {
2325 curptr += count;
2326 continue;
2327 }
2328
2329 if (count == 1) {
2330 cf_WriteByte(fp, 0);
2331 cf_WriteShort(fp, curval);
2332 } else {
2333 cf_WriteByte(fp, count);
2334 cf_WriteShort(fp, curval);
2335 }
2336
2337 curptr += count;
2338 }
2339
2340 return written;
2341}
2342

Callers 1

Calls 2

cf_WriteByteFunction · 0.85
cf_WriteShortFunction · 0.85

Tested by

no test coverage detected