-
Notifications
You must be signed in to change notification settings - Fork 0
/
LZW.cpp
147 lines (134 loc) · 2.85 KB
/
LZW.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
#include <iostream>
#include <Windows.h>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <list>
using namespace std;
void readAllBytes(string fileName, vector<char> &result);
void writeToFile(string fileName, vector<int> &compressCode);
void compress(vector<char> &uncompressInfo, vector<int> &compressCode);
void decompress(vector<int> op, string fileName);
int main(char argc, char *argv[])
{
argc = 4;
argv[1] = (char*)"123.txt";
argv[2] = (char*)"234.lzw";
argv[3] = (char*)"decompress123.txt";
if (argc != 4)
{
printf("Wrong number of arguments!!!");
system("pause");
return 0;
}
string fileInputName = argv[1];
string fileOutputName = argv[2];
string fileDecompressName = argv[3];
vector<char> uncompressInformation;
vector<int> compressCode;
readAllBytes(fileInputName, uncompressInformation);
compress(uncompressInformation, compressCode);
writeToFile(fileOutputName, compressCode);
decompress(compressCode, fileDecompressName);
system("pause");
return 0;
}
void writeToFile(string fileName, vector<int> &compressCode)
{
ofstream file(fileName, ios::binary);
if (file.is_open())
{
file.write((char*)&compressCode[0], compressCode.size() * sizeof(int));
}
else
{
cout << "Unable to open file" << endl;
}
file.close();
}
void readAllBytes(string fileName, vector<char> &result)
{
ifstream file(fileName, ios::binary | ios::ate);
if (file.is_open())
{
ifstream::pos_type pos = file.tellg();
result.resize(pos);
file.seekg(0, ios::beg);
file.read(&result[0], pos);
}
else
{
cout << "Unable to open file" << endl;
}
file.close();
}
void compress(vector<char> &uncompressInfo, vector<int>& compressCode)
{
map<string, int> table;
for (int i = 0; i <= 255; i++) {
string ch = "";
ch += char(i);
table[ch] = i;
}
string p = "", c = "";
p += uncompressInfo[0];
int code = 256;
vector<int> output_code;
for (int i = 0; i < uncompressInfo.size(); i++)
{
if (i != uncompressInfo.size() - 1)
c += uncompressInfo[i + 1];
if (table.find(p + c) != table.end())
{
p = p + c;
}
else
{
compressCode.push_back(table[p]);
table[p + c] = code;
code++;
p = c;
}
c = "";
}
compressCode.push_back(table[p]);
for (auto it : compressCode)
cout << it << endl;
}
void decompress(vector<int> compressCode, string fileName)
{
ofstream file(fileName);
map<int, string> table;
for (int i = 0; i <= 255; i++) {
string ch = "";
ch += char(i);
table[i] = ch;
}
int old = compressCode[0], n;
string s = table[old];
string c = "";
c += s[0];
file << s;
int count = 256;
for (int i = 0; i < compressCode.size() - 1; i++)
{
n = compressCode[i + 1];
if (table.find(n) == table.end())
{
s = table[old];
s = s + c;
}
else
{
s = table[n];
}
file << s;
c = "";
c += s[0];
table[count] = table[old] + c;
count++;
old = n;
}
file.close();
}