From e9a32a966d49ab798a4c6f24471669fc03adb395 Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 2 Apr 2026 11:58:24 +1300 Subject: feat: Added basic zlib compression Currently not incredibly well implemented, but it can handle fixed codes --- src/zlib.h | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src/zlib.h') diff --git a/src/zlib.h b/src/zlib.h index fc6a1c3..efebedb 100644 --- a/src/zlib.h +++ b/src/zlib.h @@ -1,7 +1,9 @@ #pragma once #include +#include #include +#include namespace TehImage { @@ -23,7 +25,16 @@ namespace TehImage unsigned long pos; }; - class ZLibInflator + enum CompressionType : uint16_t + { + NONE = 0b00, + STATIC_CODES = 0b01, + DYNAMIC_CODES = 0b10, + RESERVED = 0b11 + }; + + // RFC 1950/1951 + class ZLib { private: HuffmanTree tree; @@ -31,17 +42,24 @@ namespace TehImage bool staticTree = false; bool haveTree = false; public: - ZLibInflator() = default; - ~ZLibInflator() = default; + ZLib() = default; + ~ZLib() = default; static bool nextBit(StreamData* stream); - static uint16_t nextBits(StreamData* stream, int bits); // Max 16 bits + static uint16_t nextBits(StreamData* stream, int count); // Max 16 bits + + static void writeBit(StreamData* stream, bool bit); + static void writeBits(StreamData* stream, int count, uint16_t bits); + + void writeCode(StreamData* stream, uint16_t code, uint8_t codeLen); 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 getStaticHuffmanCodes(uint16_t* codesOut, uint8_t* codeLensOut, uint16_t* distCodesOut, uint8_t* distCodeLensOut); + void buildStaticHuffmanTree(); void buildStaticHuffmanTree(HuffmanTree* treeOut, HuffmanTree* distTreeOut); @@ -52,6 +70,7 @@ namespace TehImage uint16_t getNextCode(StreamData* stream, HuffmanTree* tree); int decodeData(uint8_t* data, unsigned long length, uint8_t* out, unsigned long outLength); + int encodeData(uint8_t* data, unsigned long length, uint8_t* out, unsigned long outLength); }; } -- cgit v1.2.3