-
Notifications
You must be signed in to change notification settings - Fork 1
/
compressor.cpp
173 lines (140 loc) · 5.49 KB
/
compressor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// ================================================================================================
// -*- C++ -*-
// File: compressor.cpp
// Author: Guilherme R. Lampert
// Created on: 18/02/16
// Brief: Handles the font bitmap compression.
//
// This source code is released under the MIT license.
// See the accompanying LICENSE file for details.
// ================================================================================================
#include "compressor.hpp"
#define RLE_IMPLEMENTATION
#include "extern/compression/rle.hpp"
#define LZW_IMPLEMENTATION
#include "extern/compression/lzw.hpp"
#define HUFFMAN_IMPLEMENTATION
#include "extern/compression/huffman.hpp"
// ========================================================
// NoOpCompressor:
// ========================================================
class NoOpCompressor final
: public Compressor
{
public:
// Return the input unchanged.
ByteBuffer compress(const ByteBuffer & uncompressed) override { return uncompressed; }
};
// ========================================================
// RLECompressor:
// ========================================================
class RLECompressor final
: public Compressor
{
public:
ByteBuffer compress(const ByteBuffer & uncompressed) override
{
// RLE might make things bigger, so make the compress buffer the double.
ByteBuffer compressed(uncompressed.size() * 2);
// Compress it:
const int compressedSize = rle::easyEncode(uncompressed.data(), uncompressed.size(),
compressed.data(), compressed.size());
// Error / no compression?
if (compressedSize <= 0)
{
compressed.clear();
}
// If we compressed or got the same size, ignore the extra overhead in the output buffer:
else if (static_cast<std::size_t>(compressedSize) <= uncompressed.size())
{
compressed.resize(compressedSize);
}
return compressed;
}
};
// ========================================================
// LZWCompressor:
// ========================================================
class LZWCompressor final
: public Compressor
{
public:
ByteBuffer compress(const ByteBuffer & uncompressed) override
{
int compressedSizeBits = 0;
int compressedSizeBytes = 0;
lzw::UByte * compressedDataPtr = nullptr;
// Compress:
lzw::easyEncode(uncompressed.data(), uncompressed.size(),
&compressedDataPtr, &compressedSizeBytes,
&compressedSizeBits);
ByteBuffer compressedBuffer((sizeof(std::uint32_t) * 2) + compressedSizeBytes);
// Append the compressed size in bytes and bits to
// the output buffer. We will need those for decompression.
auto bitsPtr = reinterpret_cast<std::uint32_t *>(compressedBuffer.data());
*bitsPtr++ = compressedSizeBytes;
*bitsPtr++ = compressedSizeBits;
// Append the rest of the compressed bit stream:
std::memcpy(bitsPtr, compressedDataPtr, compressedSizeBytes);
// Done! We can now free the LZW buffer.
LZW_MFREE(compressedDataPtr);
return compressedBuffer;
}
};
// ========================================================
// HuffmanCompressor:
// ========================================================
class HuffmanCompressor final
: public Compressor
{
public:
ByteBuffer compress(const ByteBuffer & uncompressed) override
{
int compressedSizeBits = 0;
int compressedSizeBytes = 0;
huffman::UByte * compressedDataPtr = nullptr;
// Compress:
huffman::easyEncode(uncompressed.data(), uncompressed.size(),
&compressedDataPtr, &compressedSizeBytes,
&compressedSizeBits);
ByteBuffer compressedBuffer((sizeof(std::uint32_t) * 2) + compressedSizeBytes);
// Append the compressed size in bytes and bits to
// the output buffer. We will need those for decompression.
auto bitsPtr = reinterpret_cast<std::uint32_t *>(compressedBuffer.data());
*bitsPtr++ = compressedSizeBytes;
*bitsPtr++ = compressedSizeBits;
// Append the rest of the compressed bit stream:
std::memcpy(bitsPtr, compressedDataPtr, compressedSizeBytes);
// Done! We can now free the Huffman buffer.
HUFFMAN_MFREE(compressedDataPtr);
return compressedBuffer;
}
};
// ========================================================
// Compressor factory:
// ========================================================
std::unique_ptr<Compressor> Compressor::create(const Encoding encoding)
{
switch (encoding)
{
case Encoding::None :
return std::make_unique<NoOpCompressor>();
case Encoding::RLE :
return std::make_unique<RLECompressor>();
case Encoding::LZW :
return std::make_unique<LZWCompressor>();
case Encoding::Huffman :
return std::make_unique<HuffmanCompressor>();
default :
error("Invalid compressor encoding enum!");
} // switch (encoding)
}
std::string Compressor::getMemorySaved(const ByteBuffer & compressed, const ByteBuffer & uncompressed)
{
const long diff = uncompressed.size() - compressed.size();
return formatMemoryUnit(diff >= 0 ? diff : 0);
}
double Compressor::getCompressionRatio(const ByteBuffer & compressed, const ByteBuffer & uncompressed)
{
return static_cast<double>(uncompressed.size()) / static_cast<double>(compressed.size());
}