| 1 |
/* This is a set of functions that compact a bitmask. |
| 2 |
* In the intended use case, a bitmask is produced by masking all |
| 3 |
* "missing values" or "fill values" in a model output with zero's, |
| 4 |
* whereas the locations with valid data points are marked with one's. |
| 5 |
* However, this compactor is likely to be effective with any bit patterns |
| 6 |
* that have lots of consecutive 0's or 1's. |
| 7 |
* |
| 8 |
* The bitmask compactor works in the following way: |
| 9 |
* 1. Assume that we use 32-bit ints; the compactor encodes 32 bits at a time. |
| 10 |
* 2. Every incoming int is encoded in one of three ways: |
| 11 |
* 2.1. For an int with all 0's, use a single 0 bit. |
| 12 |
* 2.2. For an int with all 1's, use two bits: 10. |
| 13 |
* 2.3. For all other ints, use 34 bits: two bits 11 then followed by the |
| 14 |
* verbose presentation of the 32-bit int. |
| 15 |
* 3. Obviously, it's the most economical to use a single 0 bit to present |
| 16 |
* the most frequent pattern (all 0's or all 1's). The encoder thus does |
| 17 |
* a test at the beginning and records the test result. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef COMPACTOR_H |
| 21 |
#define COMPACTOR_H |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <stdint.h> |
| 25 |
#include <assert.h> |
| 26 |
|
| 27 |
#ifdef __cplusplus |
| 28 |
extern "C" { |
| 29 |
#endif |
| 30 |
|
| 31 |
/* Return the compaction strategy to use: |
| 32 |
* 0: compact with all 0's being the most frequent |
| 33 |
* 1: compact with all 1's being the most frequent |
| 34 |
* Note: `bytes` has to be a multiple of 8. |
| 35 |
*/ |
| 36 |
int compactor_strategy(const void* buf, size_t bytes); |
| 37 |
|
| 38 |
/* Return the size in bytes of the resulting compacted bitstream, given an input buf. |
| 39 |
* Note: `buf_bytes` has to be a multiple of 8. |
| 40 |
*/ |
| 41 |
size_t compactor_comp_size(const void* buf, size_t buf_bytes); |
| 42 |
|
| 43 |
/* Return the number of useful bytes in a compacted bitstream. |
| 44 |
* This value is the same as the output of `compactor_comp_size()` during encoding. |
| 45 |
*/ |
| 46 |
size_t compactor_useful_bytes(const void* comp_buf); |
| 47 |
|
| 48 |
/* Return the useful size of the output bitstream, |
| 49 |
* which is the same as the output of `compactor_comp_size()`. |
| 50 |
* Note 1: the input bitmask length (in bytes) has to be a multiple of 8. |
| 51 |
* This requirement is inheritated from the bitstream implementation. |
| 52 |
* Note 2: the output buffer length should be 1) a multiple of 8, and |
| 53 |
* 2) no less than the size returned by `compactor_comp_size()`. |
| 54 |
*/ |
| 55 |
size_t compactor_encode(const void* bitmask, |
| 56 |
size_t bitmask_bytes, |
| 57 |
void* compact_bitstream, |
| 58 |
size_t compact_bitstream_bytes); |
| 59 |
|
| 60 |
/* Return the number of useful bytes in the decoded bitmask. |
| 61 |
* Note: The number of useful bytes might be bigger than the number of bytes being |
| 62 |
* encoded, because of the word size that the compactor operates on. |
| 63 |
* Note: `compact_bitstream_bytes` should be a multiple of 8 that is no less than |
| 64 |
* the size returned by `compactor_encode()`. |
| 65 |
*/ |
| 66 |
size_t compactor_decode(const void* compact_bitstream, |
| 67 |
size_t compact_bitstream_bytes, |
| 68 |
void* decoded_bitmask); |
| 69 |
|
| 70 |
#ifdef __cplusplus |
| 71 |
} |
| 72 |
#endif |
| 73 |
|
| 74 |
#endif |