tudocomp
– The TU Dortmund Compression Framework
RunLengthEncoder.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/util.hpp>
5 #include <tudocomp/Env.hpp>
7 
8 namespace tdc {
9 
15 template<class char_type>
16 void rle_encode(std::basic_istream<char_type>& is, std::basic_ostream<char_type>& os, size_t offset = 0) {
17  char_type prev;
18  if(tdc_unlikely(!is.get(prev))) return;
19  os << prev;
20  char_type c;
21  while(is.get(c)) {
22  if(prev == c) {
23  size_t run = 0;
24  while(is.peek() == c) { ++run; is.get(); }
25  os << c;
26  write_vbyte(os, run+offset);
27  } else {
28  os << c;
29  }
30  prev = c;
31  }
32 }
36 template<class char_type>
37 void rle_decode(std::basic_istream<char_type>& is, std::basic_ostream<char_type>& os, size_t offset = 0) {
38  char_type prev;
39  if(tdc_unlikely(!is.get(prev))) return;
40  os << prev;
41  char_type c;
42  while(is.get(c)) {
43  if(prev == c) {
44  size_t run = read_vbyte<size_t>(is)-offset;
45  while(run-- > 0) { os << c; }
46  }
47  os << c;
48  prev = c;
49  }
50 }
51 
52 class RunLengthEncoder : public Compressor {
53 public:
54  inline static Meta meta() {
55  Meta m("compressor", "rle", "Run Length Encoding Compressor");
56  m.option("offset").dynamic(0);
57  return m;
58  }
59  const size_t m_offset;
61  : Compressor(std::move(env)), m_offset(this->env().option("offset").as_integer()) {
62  }
63 
64  inline virtual void compress(Input& input, Output& output) override {
65  auto is = input.as_stream();
66  auto os = output.as_stream();
67  rle_encode(is,os,m_offset);
68  }
69  inline virtual void decompress(Input& input, Output& output) override {
70  auto is = input.as_stream();
71  auto os = output.as_stream();
72  rle_decode(is,os,m_offset);
73  }
74 };
75 
76 
77 }//ns
78 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
void write_vbyte(std::ostream &os, int_t v)
Store an integer as a bunch of bytes.
Definition: vbyte.hpp:29
#define tdc_unlikely(x)
Provides a hint to the compiler that x is expected to resolve to false.
Definition: def.hpp:23
Base for data compressors.
Definition: Compressor.hpp:19
InputStream as_stream() const
Creates a stream that allows for character-wise reading of the input.
Definition: Input.hpp:264
virtual void decompress(Input &input, Output &output) override
Decompress the given input to the given output.
void rle_decode(std::basic_istream< char_type > &is, std::basic_ostream< char_type > &os, size_t offset=0)
Decodes a run length encoded stream.
Env & env()
Provides access to the environment that the algorithm works in.
Definition: Algorithm.hpp:51
OutputStream as_stream() const
Creates a stream that allows for character-wise output.
An abstraction layer for algorithm output.
Definition: Output.hpp:23
virtual void compress(Input &input, Output &output) override
Compress the given input to the given output.
void rle_encode(std::basic_istream< char_type > &is, std::basic_ostream< char_type > &os, size_t offset=0)
Encode a byte-stream with run length encoding each run of the same character is substituted with two ...
OptionBuilder option(const std::string &name)
Declares an accepted option for this algorithm.
Definition: Meta.hpp:216
Local environment for a compression/encoding/decompression call.
void dynamic()
Declares that this option accepts values of a simple type that can be parsed from a string (e...
Definition: Meta.hpp:150
An abstraction layer for algorithm input.
Definition: Input.hpp:37