-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlzw_serial_encode.cpp
More file actions
128 lines (113 loc) · 3.48 KB
/
lzw_serial_encode.cpp
File metadata and controls
128 lines (113 loc) · 3.48 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
#include <unordered_map>
#include <vector>
#include <map>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <sys/stat.h>
#define PRINT 0
using namespace std;
int compressed_file_size;
long long get_file_size(string file_name) {
struct stat stat_buf;
int rc = stat(file_name.c_str(), &stat_buf);
return rc == 0 ? stat_buf.st_size : -1;
}
std::vector<int> encoding(ifstream& ifile)
{
#if PRINT
std::cout << "Encoding\n";
#endif
std::unordered_map<std::string, int> table;
for (int i = 0; i <= 255; i++) {
std::string ch = "";
ch += char(i);
table[ch] = i;
}
std::string p = "", c = "";
// p += s1[0];
p += (char)ifile.get();
int code = 256;
std::vector<int> output_code;
#if PRINT
std::cout << "String\tOutput_Code\tAddition\n";
#endif
char ch;
unsigned long long cnt = 0;
while (!ifile.fail()) {
// if (i != s1.length() - 1)
// c += s1[i + 1];
ch = ifile.get();
if (!ifile.eof()) {
c += ch;
}
if (table.find(p + c) != table.end()) {
p = p + c;
}
else {
#if PRINT
std::cout << p << "\t" << table[p] << "\t\t"
<< p + c << "\t" << code << std::endl;
#endif
output_code.push_back(table[p]);
// ofile << table[p] << endl;
cnt++;
table[p + c] = code;
code++;
p = c;
}
c = "";
}
#if PRINT
std::cout << p << "\t" << table[p] << std::endl;
#endif
output_code.push_back(table[p]);
// ofile << table[p] << endl;
// Analysis
int num_bits = (int)ceil(log2(code));
#if PRINT
std::cout << "Largest code assigned: " << code << endl;
std::cout << "Number of bits to store each code: " << num_bits << endl;
std::cout << "Number of output codes: " << cnt << endl;
std::cout << "Estimated best-case compressed size: " << cnt * num_bits / 8 << " bytes" << endl;
#endif
compressed_file_size = cnt * num_bits / 8;
return output_code;
}
int main(int argc, char** argv)
{
if (argc < 2 || argc > 3) {
cerr << "Usage: ./lzw_serial_encode filename [output]" << endl;
return 1;
}
string output_file = argc == 3 ? argv[2] : "output_code.txt";
ifstream ifile(argv[1]);
if (ifile.fail()) {
cerr << "Cannot open file" << endl;
return 1;
}
ofstream ofile(argv[2]);
struct timespec start, stop;
double time = 0;
if( clock_gettime(CLOCK_REALTIME, &start) == -1) { perror("clock gettime");}
std::vector<int> output_code = encoding(ifile);
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) { perror("clock gettime");}
time = (stop.tv_sec - start.tv_sec)+ (double)(stop.tv_nsec - start.tv_nsec)/1e9;
std::cout << "Encoding time = " << time << " sec " <<std::endl;
int file_size = get_file_size(argv[1]);
#if PRINT
std::cout << "Input file size = " << file_size << endl;
#endif
std::cout << "Estimated Compression Rate = " << (double)file_size / compressed_file_size << endl;
for (unsigned long long i = 0; i < output_code.size(); i++) {
ofile << output_code[i] << endl;
}
#if PRINT
std::cout << "Output Codes are: ";
for (size_t i = 0; i < output_code.size(); i++) {
std::cout << output_code[i] << " ";
}
std::cout << std::endl;
#endif
}