BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
huffman.h File Reference

Module for Huffman encoding and decoding. More...

#include <memory>
#include <b8/type.h>
#include <pipe.h>

Go to the source code of this file.

Classes

class  Huffman::CHuffmanEncoder
 Class for Huffman encoding. More...
 
class  Huffman::CHuffmanDecoder
 Class for Huffman decoding. More...
 

Variables

constexpr u8 Huffman::Signature = 0x77
 

Detailed Description

Module for Huffman encoding and decoding.

This module provides classes for Huffman encoding and decoding. It uses the Pipe module for data transfer between different stages of the encoding and decoding process.

Huffman Format

The Huffman format used in this module is defined as follows:

  • u8 _signature; // = 0x77 = Huffman::Signature
  • u1 _flat; // 0: HuffmanEncoded, 1: NotEncoded

if (_flat == 0) {

  • u9 _num_of_huffman_table; { u5 _num_of_bits_huffman_code; u$(_num_of_bits_huffman_code) _huffman_code; u8 _decode_data; }[_num_of_huffman_table];
  • u20 _byte_size_of_orgin; } else {
  • u20 _byte_size_of_orgin;
  • u3 _reserved;
  • u8 _flat_data[_byte_size_of_orgin]; }

Usage Example

Here is an example of how to use this module to perform Huffman encoding and decoding:

#include <iostream>
#include <memory>
#include <huffman.h>
#include <pipe.h>
using namespace std;
using namespace Pipe;
using namespace Huffman;
int main() {
// Prepare input data
const char* input_data = "example data to compress";
size_t input_size = strlen(input_data);
// Create memory reader and buffer pipes
auto pipe_mem_reader = make_shared<CMemReaderPipe>(reinterpret_cast<const u8*>(input_data), input_size);
auto pipe_mem_packed = make_shared<CMemBufferPipe>();
// Create Huffman encoder and set input and output pipes
CHuffmanEncoder encoder;
encoder.SetIn(pipe_mem_reader);
encoder.SetOut(pipe_mem_packed);
// Perform Huffman encoding
encoder.Encode();
// Dump encoded data
cout << "Encoded Data Dump:" << endl;
pipe_mem_packed->Dump();
// Create memory buffer and reader pipes for decoding
auto decode_pipe_in = make_shared<CMemBufferPipe>();
auto decode_pipe_out = make_shared<CMemBufferPipe>();
// Move encoded data to the decoder's input pipe
Pipe::Move(pipe_mem_packed, decode_pipe_in);
// Create Huffman decoder and set input and output pipes
CHuffmanDecoder decoder;
decoder.SetIn(decode_pipe_in);
decoder.SetOut(decode_pipe_out);
// Perform Huffman decoding
if (decoder.Decode() != CHuffmanDecoder::DECODE_OK) {
cerr << "Decoding failed!" << endl;
return 1;
}
// Dump decoded data
cout << "Decoded Data Dump:" << endl;
decode_pipe_out->Dump();
return 0;
}
Class for Huffman decoding.
Definition huffman.h:161
void SetIn(std::shared_ptr< Pipe::CPipe > pipe_in_)
Sets the input pipe for the decoder.
Definition huffman.cpp:609
CHuffmanDecoder::DecodeResult Decode()
Performs Huffman decoding on the input data.
Definition huffman.cpp:540
void SetOut(std::shared_ptr< Pipe::CPipe > pipe_out_)
Sets the output pipe for the decoder.
Definition huffman.cpp:613
Class for Huffman encoding.
Definition huffman.h:129
void SetIn(std::shared_ptr< Pipe::CPipe > pipe_in_)
Sets the input pipe for the encoder.
Definition huffman.cpp:342
void SetOut(std::shared_ptr< Pipe::CPipe > pipe_out_)
Sets the output pipe for the encoder.
Definition huffman.cpp:346
void Encode()
Performs Huffman encoding on the input data.
Definition huffman.cpp:357
Module for Huffman encoding and decoding.
Module for data pipeline processing using pipes.
Version
1.0
Date
2024