-
Encode/compress data or decode, from or to files
-
Coder class encodes to a file. You can then call decode() on file thru that Coder to get original text
- std::string stem("./png")
- std::string ext(".png")
- Coder coder(readFile(stem + name))
+ // 1: Core Huffman: decode through class
+ coder.decode(stem + "_decoded" + ext);
! // 2: Write tree, encoded to separate files, decode from files
! writeTree(coder.freqs, stem + ".ser");
! writeEncoded(coder.encoded, stem + ".dat");
! decode(readTree(stem + ".ser"), readEncoded(stem + ".dat"), stem + "_decoded" + ext);
+ // 3: Write tree, append encoded to that same file
+ writeTree(coder.freqs, stem + ".bin");
+ writeEncoded(coder.encoded, stem + ".bin", true); // toAppend : true
+ decode(stem + ".bin", stem + "_decoded" + ext);| Core (# overloads) | Helper |
|---|---|
| encode | fillCodebook |
| decode (2) | bitsToBytes |
| getTree (2) | readFile |
| (write/read)(Tree/Encoded) |
| Function | Description |
|---|---|
| encode | Fill components (freqs->tree->codebook) and encode thru codebook |
| decode | Thru tree, decode string<1 |
| getTree | From map<char -> freq>, build tree and use tree to fill codebook |
| writeTree | Write data in char-freq map to path |
| readTree | Read data in path into char-freq map. Call getTree(such map) |
| writeEncoded | Write bytes, from string<0 |
| readEncoded | Read into string<0 |
| fillCodebook | Traverse node/tree: for char, put its encoding into codebook |
| bitsToBytes | Pad final block of < 8 bits to 8 bits |
| readFile | Tokenize data in file into discrete bytes |
+ Huffman: -h to print this help message
- Commands:
! -en[code] readPath encodePath {3 args}
! -de[code] readPath decodePath {3 args}
+ Output : Compression ratio
// Example run
---> Encoded lipsum.txt (2958 bytes) to 1656 bytes: 1.786 compression ratio