-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
186 lines (173 loc) · 6.15 KB
/
main.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
#include <bits/stdc++.h>
#include "BinaryStdIn.h"
#include "BinaryStdOut.h"
#include "HuffmanEncoding.h"
#include "DCT.h"
#include "LZW.h"
#include "RunLengthEncoding.h"
#include <benchmark/benchmark.h>
#include "CLI11.hpp"
//using namespace std;
//namespace fs = std::filesystem;
//
//size_t getFileSize(const string& filename) {
// ifstream file(filename, ios::binary | ios::ate);
// return file.tellg();
//}
//
//vector<fs::path> findFiles(const std::string& path) {
// auto res = vector<filesystem::path>();
//
// for(const auto& entry : fs::recursive_directory_iterator(path)) {
// if(!fs::is_directory(entry) ) { // && fs::file_size(entry) < meta.size
// res.push_back(entry.path().filename());
// }
// }
//
// return res;
//}
//
//static void BM_HuffmanEncoding(benchmark::State& state) {
// size_t originalSize = getFileSize("text.txt");
//
// for (auto _ : state) {
// Huffman h = Huffman();
// h.compress("text.txt","huff_text.bin");
// h.expand("huff_text.bin", "expanded_huff.txt");
// }
//
// size_t compressedSize = getFileSize("huff_text.bin");
//
// double compressionRatio = (1-((double)(compressedSize) / (double)originalSize));
//
// state.counters["Improvement Ratio"] = compressionRatio;
//
//
//}
//
//
//static void BM_LZWCompression(benchmark::State& state) {
// size_t originalSize = getFileSize("text.txt");
//
// for (auto _ : state) {
// LZW c = LZW("text.txt", "lzw_text.bin");
// c.compress();
// LZW e = LZW("lzw_text.bin", "expanded_lzw.txt");
// e.expand();
// }
//
// size_t compressedSize = getFileSize("lzw_text.bin");
//
// double compressionRatio = (1-((double)(compressedSize) / (double)originalSize));
//
// state.counters["ImprovementRatio"] = compressionRatio;
//}
//
//static void BM_RLECompression(benchmark::State& state) {
// size_t originalSize = getFileSize("text.txt");
//
// for (auto _ : state) {
// RunLengthEncoding c = RunLengthEncoding("text.txt", "rle_text.bin");
// c.compress();
// RunLengthEncoding e = RunLengthEncoding("rle_text.bin", "expanded_rle.txt");
// e.expand();
// }
//
// size_t compressedSize = getFileSize("rle_text.bin");
//
// double compressionRatio = ((double)(compressedSize) / (double)originalSize);
//
// state.counters["Improvement Ratio"] = compressionRatio;
//}
//turn off the main function and uncomment these to run the Google benchmarks
//BENCHMARK(BM_HuffmanEncoding)->Unit(benchmark::kMillisecond);
//BENCHMARK(BM_LZWCompression)->Unit(benchmark::kMillisecond);
//BENCHMARK(BM_RLECompression)->Unit(benchmark::kMillisecond);
//BENCHMARK_MAIN();
//int main() {
// size_t originalSize = getFileSize("text.txt");
// std::cout << "Original file size: " << originalSize << " bytes" << endl;
//
// Huffman h = Huffman();
// h.compress("text.txt","huff_text.bin");
// h.expand("huff_text.bin", "expanded_huff.txt");
//
// std::cout << "Compressed with Huffman size: " << getFileSize("huff_text.bin") << " bytes" << endl;
//
//
// LZW c = LZW("text.txt", "lzw_text.bin");
// c.compress();
// LZW e = LZW("lzw_text.bin", "expanded_lzw.txt");
// e.expand();
//
// std::cout << "Compressed with LZW size: " << getFileSize("lzw_text.bin") << " bytes" << endl;
//
// RunLengthEncoding cr = RunLengthEncoding("text.txt", "rle_text.bin");
// cr.compress();
// RunLengthEncoding er = RunLengthEncoding("rle_text.bin", "expanded_rle.txt");
// er.expand();
//
// std::cout << "Compressed with RLE size: " << getFileSize("rle_text.bin") << " bytes" << endl;
//
// return 0;
//}
int main(int argc, char* argv[]) {
CLI::App app{"Compression CLI"};
std::string inputFilePath;
std::string outputFilePath;
std::string algorithm;
bool compress = false;
bool decompress = false;
// Command-Line Arguments
app.add_option("-i,--input", inputFilePath, "Input file path")->required()->default_val("text.txt");
app.add_option("-o,--output", outputFilePath, "Output file path")->required()->default_val("output.bin");
app.add_flag("-c,--compress", compress, "Compress the input file");
app.add_flag("-d,--decompress", decompress, "Decompress the input file");
app.add_option("-a,--algorithm", algorithm, "Compression algorithm (rle, lzw, huffman, dct)")->required();
// Parsing Arguments
try {
app.parse(argc, argv);
} catch (const CLI::ParseError& e) {
return app.exit(e);
}
// Ensuring only one of compress or decompress is specified
if (compress == decompress) {
std::cerr << "Error: You must specify either -c/--compress or -d/--decompress, but not both." << std::endl;
return 1;
}
// Creating compression/decompression objects
if (algorithm == "rle") {
RunLengthEncoding rle(std::move(inputFilePath), std::move(outputFilePath));
if (compress) {
rle.compress();
std::cout << "File compressed using Run-Length Encoding successfully!" << std::endl;
} else {
rle.expand();
std::cout << "File decompressed using Run-Length Encoding successfully!" << std::endl;
}
} else if (algorithm == "lzw") {
LZW lzw(std::move(inputFilePath), std::move(outputFilePath));
if (compress) {
lzw.compress();
std::cout << "File compressed using LZW successfully!" << std::endl;
} else {
lzw.expand();
std::cout << "File decompressed using LZW successfully!" << std::endl;
}
} else if (algorithm == "huffman") {
Huffman huffman;
if (compress) {
huffman.compress(std::move(inputFilePath), std::move(outputFilePath));
std::cout << "File compressed using Huffman Encoding successfully!" << std::endl;
} else {
huffman.expand(std::move(inputFilePath), std::move(outputFilePath));
std::cout << "File decompressed using Huffman Encoding successfully!" << std::endl;
}
} else if (algorithm == "dct") {
dctCompression(inputFilePath);
} else {
std::cerr << "Algorithm not supported! (yet)" << std::endl;
return 1;
}
return 0;
}