/
lzw435.cpp
371 lines (318 loc) · 9.98 KB
/
lzw435.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/***********************************************/
// Alyssa Myers
// 3460:435 Algorithms
// Project 2 — LZW
// March 6, 2017
//
// LZW compression & decompression of files.
/***********************************************/
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <sys/stat.h>
#include <cmath>
#define DICT_BITS 12
// Compress a string to a list of output symbols.
// The result will be written to the output iterator
// starting at "result"; the final iterator is returned.
template <typename Iterator>
Iterator compress(std::vector<char> &uncompressed, Iterator result)
{
// 2^8
int dictionary_base = std::pow(2,8);
// 2^12
int dictionary_limit = std::pow(2,DICT_BITS);
// current size
int dictionary_size = dictionary_base;
// build dictionary
std::map<std::string,int> dictionary;
for (int i = 0; i < dictionary_base; i++)
dictionary[std::string(1, i)] = i;
std::string word;
for(std::string::const_iterator it = uncompressed.begin();
it != uncompressed.end(); it++)
{
// retrieve next character
char value = *it;
// build prefix
std::string prefix = word + std::string(1,value);
// if prefix exists, build on
if(dictionary.count(prefix))
{
word = prefix;
}
// add to the dictionary
else
{
// add to the returned hash
*result++ = dictionary[word];
if(dictionary_size < dictionary_limit)
{
// add prefix to the dictionary
dictionary[prefix] = dictionary_size++;
}
// reset word to base
word = std::string(1, value);
}
}
// Output the code for word
if (!word.empty())
{
*result++ = dictionary[word];
}
return result;
}
// Decompress a list of output ks to a string.
// "begin" and "end" must form a valid range of ints
template <typename Iterator>
std::string decompress(Iterator begin, Iterator end)
{
// 2^8
int dictionary_base = std::pow(2,8);
// 2^12
int dictionary_limit = std::pow(2,DICT_BITS);
// current size
int dictionary_size = dictionary_base;
// build dictionary
std::map<int,std::string> dictionary;
for (int i = 0; i < dictionary_base; i++)
dictionary[i] = std::string(1, i);
std::string word(1, *begin++);
std::string result = word;
std::string entry;
for (; begin != end; begin++)
{
try
{
// retrieve
int value = *begin;
// if it exists, retrieve it
if (dictionary.count(value))
{
entry = dictionary[value];
}
// build entry to create
else if (value == dictionary_size)
{
entry = word + word[0];
}
else throw "Bad compression.";
result += entry;
// Add w+entry[0] to the dictionary.
dictionary[dictionary_size++] = word + entry[0];
word = entry;
}
catch (const char* e)
{
std::cout << e << std::endl;
break;
}
}
return result;
}
std::string int2BinaryString(int c, int cl)
{
//a binary code string with code length = cl
std::string p = "";
// converting number into binary
while (c > 0)
{
if(c % 2 == 0) p = "0" + p;
else p = "1" + p;
c = c >> 1;
}
int zeros = cl - p.size();
if (zeros < 0)
{
std::cout << "\nWarning: Overflow. code is too big to be coded by " << cl <<" bits!\n";
p = p.substr(p.size() - cl);
}
else
{
//pad 0s to left of the binary code if needed
for (int i = 0; i < zeros; i++) p = "0" + p;
}
return p;
}
int binaryString2Int(std::string p)
{
int code = 0;
if (p.size()>0)
{
if (p.at(0)=='1') code = 1;
p = p.substr(1);
while (p.size()>0)
{
code = code << 1;
if (p.at(0)=='1') code++;
p = p.substr(1);
}
}
return code;
}
int main(int argc, char* argv[])
{
if(argc != 3)
{
std::cout << "You must provide two arguments, (c/e) (filename)." << std::endl;
return 1;
}
else if(!strcmp(argv[1], "c"))
{
std::string file(argv[2]);
std::vector<int> compressed;
std::vector<char> content;
std::string zeros = "00000000";
long fsize, osize;
struct stat filestatus, outputstatus;
std::cout << "Compressing " << file << std::endl;
try
{
// read the file
std::ifstream in(file.c_str(), std::ios::binary);
// get the size of the file in bytes
stat(file.c_str(), &filestatus);
fsize = filestatus.st_size;
auto const start_pos = in.tellg();
in.ignore(std::numeric_limits<std::streamsize>::max());
auto char_count = in.gcount();
in.seekg(start_pos);
content = std::vector<char>(char_count);
in.read(&content[0], content.size());
in.close();
}
catch(std::exception &e)
{
std::cout << "Error: " << e.what() << std::endl;
}
// compress
compress(content, std::back_inserter(compressed));
// 8 bit binary string to 12 bit binary string
std::string binary_code, p;
for (std::vector<int>::iterator it = compressed.begin() ; it != compressed.end(); ++it)
{
p = int2BinaryString(*it, DICT_BITS);
binary_code += p;
}
// store to file in 8 bytes
try
{
// make sure the length of the binary string is a multiple of 8
std::ofstream out((file + ".lzw").c_str(), std::ios::binary);
if(binary_code.size() % 8 != 0)
binary_code += zeros.substr(0, 8 - binary_code.size() % 8);
int b;
for(int i = 0; i < binary_code.size(); i += 8)
{
b = 1;
for(int j = 0; j < 8; j++)
{
b = b << 1;
if(binary_code.at(i+j) == '1')
b += 1;
}
// save the string byte by byte (8 bits)
char c = (char) (b & 255);
out.write(&c, 1);
}
out.close();
}
catch(std::exception &e)
{
std::cout << "Error: " << e.what() << std::endl;
}
// get the size of the file in bytes;
stat((file+".lzw").c_str(), &outputstatus);
osize = outputstatus.st_size;
std::cout << "File bytes : " << fsize << " -> " << osize << std::endl;
}
else if(!strcmp(argv[1], "e"))
{
std::string file(argv[2]);
std::vector<int> compressed;
std::vector<char> content;
std::string buffer;
long fsize, osize;
struct stat filestatus, outputstatus;
std::cout << "Expanding " << file << std::endl;
try
{
// read the file
std::ifstream in(file.c_str(), std::ios::binary);
// get the size of the file in bytes
stat(file.c_str(), &filestatus);
fsize = filestatus.st_size;
auto const start_pos = in.tellg();
in.ignore(std::numeric_limits<std::streamsize>::max());
auto char_count = in.gcount();
in.seekg(start_pos);
content = std::vector<char>(char_count);
in.read(&content[0], content.size());
std::string zeros = "000000000000";
long count = 0;
while(count < fsize)
{
unsigned char uc = (unsigned char) content[count];
std::string p = "";
for(int j = 0; j < 8 && uc > 0; j++)
{
if(uc % 2 == 0) p = "0" + p;
else p = "1" + p;
uc = uc >> 1;
}
p = zeros.substr(0, 8-p.size()) + p;
buffer += p;
count++;
}
in.close();
}
catch(std::exception &e)
{
std::cout << "Error: " << e.what() << std::endl;
}
// 12 to 8 bits, string to int
std::string p;
int word;
for (int i = 0; i < buffer.length(); i += DICT_BITS)
{
p = "";
p = buffer.substr(i, DICT_BITS);
word = binaryString2Int(p);
compressed.push_back(word);
}
// decompress
std::string decompressed = decompress(compressed.begin(), compressed.end());
// to char
long dsize = decompressed.length() + 1;
char d[dsize];
strcpy(d, decompressed.c_str());
// creating file string
std::string name = file.substr(0, file.find('.')) + "2";
std::string ext = file.substr(file.find('.'), file.length());
ext.erase(ext.length()-4);
std::string output = name + ext;
try
{
std::ofstream out(output.c_str(), std::ios::binary);
out << d;
out.close();
}
catch(std::exception &e)
{
std::cout << "Error: " << e.what() << std::endl;
}
// get the size of the file in bytes
stat((output).c_str(), &outputstatus);
osize = outputstatus.st_size;
std::cout << "File bytes : " << fsize << " -> " << osize << std::endl;
}
else
{
std::cout << "Error: invalid arguments (" << argv[1] << ") (" << argv[2] << ")." << std::endl;
return 1;
}
return 0;
}