-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlzw.cpp
More file actions
216 lines (187 loc) · 6.58 KB
/
Copy pathlzw.cpp
File metadata and controls
216 lines (187 loc) · 6.58 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//--------------------------------------------------------------------------------------------------------------------------------
// Hassan Shahzad & Azka Khurram
// 18i-0441 & 18i-0461
// Algo-Project
// FAST NUCES
// Task3.cpp
//--------------------------------------------------------------------------------------------------------------------------------
//================================================================================================================================
// 1: This file is basically the implementation of "lzw.h". It consists all the functionalities and definitions of fucntions.
//================================================================================================================================
#include "lzw.h"
namespace lzw
{
// ================================================================================================
//
// LZW Implementation
//
// ================================================================================================
// ========================================================
// Encoding implementation:
// ========================================================
void easyEncode(const uint8_t * uncompressed, int uncompressedSizeBytes,
uint8_t ** compressed, int * compressedSizeBytes, int * compressedSizeBits)
{
if (uncompressed == nullptr || compressed == nullptr)
{
cout<<"easyEncode(): Null data pointer(s)!"<<endl;
return;
}
if (uncompressedSizeBytes <= 0 || compressedSizeBytes == nullptr || compressedSizeBits == nullptr)
{
cout<<"easyEncode(): Bad in/out sizes!"<<endl;
return;
}
// LZW encoding context:
int code = Nil;
int codeBitsWidth = StartBits;
Dictionary dictionary;
// Output bit stream we write to. This will allocate
// memory as needed to accommodate the encoded data.
BitStreamWriter bitStream;
for (; uncompressedSizeBytes > 0; --uncompressedSizeBytes, ++uncompressed)
{
const int value = *uncompressed;
const int index = dictionary.findIndex(code, value);
if (index != Nil)
{
code = index;
continue;
}
// Write the dictionary code using the minimum bit-with:
bitStream.appendBitsU64(code, codeBitsWidth);
// Flush it when full so we can restart the sequences.
if (!dictionary.flush(codeBitsWidth))
{
// There's still space for this sequence.
dictionary.add(code, value);
}
code = value;
}
// Residual code at the end:
if (code != Nil)
{
bitStream.appendBitsU64(code, codeBitsWidth);
}
// Pass ownership of the compressed data buffer to the user pointer:
*compressedSizeBytes = bitStream.getByteCount();
*compressedSizeBits = bitStream.getBitCount();
*compressed = bitStream.release();
}
// ========================================================
// Decoding implementation:
// ========================================================
static bool outputByte(int code, uint8_t *& output, int outputSizeBytes, int & bytesDecodedSoFar)
{
if (bytesDecodedSoFar >= outputSizeBytes)
{
cout<<"Decoder output buffer too small!"<<endl;
return false;
}
assert(code >= 0 && code < 256);
*output++ = static_cast<uint8_t>(code);
++bytesDecodedSoFar;
return true;
}
static bool outputSequence(const Dictionary & dict, int code,
uint8_t *& output, int outputSizeBytes,
int & bytesDecodedSoFar, int & firstByte)
{
// A sequence is stored backwards, so we have to write
// it to a temp then output the buffer in reverse.
int i = 0;
uint8_t sequence[MaxDictEntries];
do
{
assert(i < MaxDictEntries - 1 && code >= 0);
sequence[i++] = dict.entries[code].value;
code = dict.entries[code].code;
}
while (code >= 0);
firstByte = sequence[--i];
for (; i >= 0; --i)
{
if (!outputByte(sequence[i], output, outputSizeBytes, bytesDecodedSoFar))
{
return false;
}
}
return true;
}
int easyDecode(const uint8_t * compressed, const int compressedSizeBytes, const int compressedSizeBits,
uint8_t * uncompressed, const int uncompressedSizeBytes)
{
if (compressed == nullptr || uncompressed == nullptr)
{
cout<<"easyDecode(): Null data pointer(s)!"<<endl;
return 0;
}
if (compressedSizeBytes <= 0 || compressedSizeBits <= 0 || uncompressedSizeBytes <= 0)
{
cout<<"easyDecode(): Bad in/out sizes!"<<endl;
return 0;
}
int code = Nil;
int prevCode = Nil;
int firstByte = 0;
int bytesDecoded = 0;
int codeBitsWidth = StartBits;
// We'll reconstruct the dictionary based on the
// bit stream codes. Unlike Huffman encoding, we
// don't store the dictionary as a prefix to the data.
Dictionary dictionary;
BitStreamReader bitStream(compressed, compressedSizeBytes, compressedSizeBits);
// We check to avoid an overflow of the user buffer.
// If the buffer is smaller than the decompressed size,
// LZW_ERROR() is called. If that doesn't throw or
// terminate we break the loop and return the current
// decompression count.
while (!bitStream.isEndOfStream())
{
assert(codeBitsWidth <= MaxDictBits);
code = static_cast<int>(bitStream.readBitsU64(codeBitsWidth));
if (prevCode == Nil)
{
if (!outputByte(code, uncompressed,
uncompressedSizeBytes, bytesDecoded))
{
break;
}
firstByte = code;
prevCode = code;
continue;
}
if (code >= dictionary.size)
{
if (!outputSequence(dictionary, prevCode, uncompressed,
uncompressedSizeBytes, bytesDecoded, firstByte))
{
break;
}
if (!outputByte(firstByte, uncompressed,
uncompressedSizeBytes, bytesDecoded))
{
break;
}
}
else
{
if (!outputSequence(dictionary, code, uncompressed,
uncompressedSizeBytes, bytesDecoded, firstByte))
{
break;
}
}
dictionary.add(prevCode, firstByte);
if (dictionary.flush(codeBitsWidth))
{
prevCode = Nil;
}
else
{
prevCode = code;
}
}
return bytesDecoded;
}
}