diff options
| author | BossCode45 <boss@tehbox.org> | 2025-06-27 14:54:46 +1200 |
|---|---|---|
| committer | BossCode45 <boss@tehbox.org> | 2025-07-24 12:48:21 +1200 |
| commit | f953c730af2b90db99f709258c5d5e50fdcaadf7 (patch) | |
| tree | adbc79f0d48a74a42fd0eb22d85f03696a99dc42 /src/zlib.h | |
| parent | 06a2928d96b3d9aeaffb19013ba71a5cb6d96381 (diff) | |
| download | tehimage-f953c730af2b90db99f709258c5d5e50fdcaadf7.tar.gz tehimage-f953c730af2b90db99f709258c5d5e50fdcaadf7.zip | |
Initial commit
Bringing a lot of stuff over from my maze-reader project in an attempt
to split the image reading code off into a library.
Diffstat (limited to 'src/zlib.h')
| -rw-r--r-- | src/zlib.h | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/zlib.h b/src/zlib.h new file mode 100644 index 0000000..d9d6b44 --- /dev/null +++ b/src/zlib.h @@ -0,0 +1,52 @@ +#pragma once + +#include <cstdint> +#include <string> + +struct HuffmanTree +{ + uint16_t val = 0xFFFF; + HuffmanTree* left = nullptr; // 0 + HuffmanTree* right = nullptr; // 1 + HuffmanTree() = default; + ~HuffmanTree(); + void free(); +}; + +struct StreamData +{ + uint8_t* data; + unsigned long length; + unsigned long pos; +}; + +class ZLibInflator +{ +private: + HuffmanTree tree; + HuffmanTree distTree; + bool staticTree = false; + bool haveTree = false; +public: + ZLibInflator() = default; + ~ZLibInflator() = default; + + static bool nextBit(StreamData* stream); + static uint16_t nextBits(StreamData* stream, int bits); // Max 16 bits + + void calculateCodes(uint8_t* lengths, uint16_t* codesOut, int codeCount); + + void buildHuffmanTree(uint8_t* lengths, uint16_t* codes, int codeCount); + void buildHuffmanTree(uint8_t* lengths, uint16_t* codes, int codeCount, HuffmanTree* treeOut); + + void buildStaticHuffmanTree(); + void buildStaticHuffmanTree(HuffmanTree* treeOut, HuffmanTree* distTreeOut); + + void buildDynamicHuffmanTree(StreamData* stream); + void buildDynamicHuffmanTree(StreamData* stream, HuffmanTree* treeOut, HuffmanTree* distTreeOut); + + uint16_t getNextCode(StreamData* stream); + uint16_t getNextCode(StreamData* stream, HuffmanTree* tree); + + int decodeData(uint8_t* data, unsigned long length, uint8_t* out, unsigned long outLength); +}; |
