forked from JayXon/Leanify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
325 lines (265 loc) · 8.9 KB
/
main.cpp
File metadata and controls
325 lines (265 loc) · 8.9 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
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
#include "main.h"
#include <climits>
#include <csignal>
#include <cstdlib>
#include <atomic>
#include <filesystem>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#ifdef _WIN32
#include <Windows.h>
#endif
#ifndef _WIN32
#include <unistd.h>
#endif
#include <CLI/CLI11.hpp>
#include <taskflow/taskflow.hpp>
#include "fileio.h"
#include "library.h"
#include "formats/jpeg.h"
#include "formats/png.h"
#include "formats/zip.h"
#include "leanify.h"
#include "version.h"
using std::cerr;
using std::cout;
using std::endl;
using std::string;
namespace {
void SignalHandler(int sig) {
const char* name = "Unknown signal";
switch (sig) {
case SIGSEGV: name = "SIGSEGV (Segmentation fault)"; break;
case SIGABRT: name = "SIGABRT (Abort)"; break;
case SIGFPE: name = "SIGFPE (Floating point exception)"; break;
case SIGILL: name = "SIGILL (Illegal instruction)"; break;
}
fprintf(stderr, "\nFatal signal caught: %s (signal %d)\n", name, sig);
fflush(stderr);
_exit(128 + sig);
}
void InstallSignalHandlers() {
std::signal(SIGSEGV, SignalHandler);
std::signal(SIGABRT, SignalHandler);
std::signal(SIGFPE, SignalHandler);
std::signal(SIGILL, SignalHandler);
}
#ifdef _WIN32
LONG WINAPI CrashHandler(EXCEPTION_POINTERS* ep) {
DWORD code = ep->ExceptionRecord->ExceptionCode;
void* addr = ep->ExceptionRecord->ExceptionAddress;
fprintf(stderr, "\nUnhandled exception 0x%08lX at address %p\n", code, addr);
switch (code) {
case EXCEPTION_ACCESS_VIOLATION:
fprintf(stderr, "ACCESS_VIOLATION: %s address %p\n",
ep->ExceptionRecord->ExceptionInformation[0] ? "writing" : "reading",
reinterpret_cast<void*>(ep->ExceptionRecord->ExceptionInformation[1]));
break;
case EXCEPTION_STACK_OVERFLOW:
fprintf(stderr, "STACK_OVERFLOW\n");
break;
}
fflush(stderr);
return EXCEPTION_EXECUTE_HANDLER;
}
#endif
} // namespace
static std::atomic<int> error_count{0};
template <typename T>
std::string ToString(const T a_value, const int n = 2) {
std::ostringstream out;
out.precision(n);
out << std::fixed << a_value;
return out.str();
}
std::string BuildSize(size_t size) {
if (size < 1024)
return std::to_string(size) + " B";
else if (size < 1024 * 1024)
return ToString(size / 1024.0) + " KB";
else
return ToString(size / 1024.0 / 1024.0) + " MB";
}
int ProcessFile(const std::filesystem::path& file_path) {
std::string filename = file_path.string();
try {
if (!parallel_processing)
cout << "Processing: " << filename << endl;
auto data_opt = ReadFile(file_path);
if (!data_opt || data_opt->empty())
return 0;
auto& data = *data_opt;
size_t original_size = data.size();
size_t new_size(0);
string libraryTag = ToString(iterations) + (zopflipng_lossy_transparent ? "lossy" : "lossless") +
(is_fast ? "no_zopflipng" : "zopflipng");
bool reusedFromLibrary = false;
auto libraryEntry = Library::GetEntry(data.data(), original_size, libraryTag.c_str());
if (!libraryEntry || !libraryEntry->isExists()) {
new_size = LeanifyFile(data.data(), original_size, 0, filename);
if (libraryEntry)
libraryEntry->Save(data.data(), new_size);
} else {
new_size = libraryEntry->Load(data.data(), original_size);
if (new_size == 0) {
// Library load failed (race condition or corrupt cache), process normally
new_size = LeanifyFile(data.data(), original_size, 0, filename);
} else {
reusedFromLibrary = true;
}
}
std::string log;
if (parallel_processing)
log = "Processed: " + filename + "\n";
if (original_size > 0 && new_size <= original_size) {
log +=
BuildSize(original_size) +
" -> " +
BuildSize(new_size) +
((!reusedFromLibrary) ? "\tLeanified: " : "\tReused: ") +
BuildSize(original_size - new_size) +
" (" +
ToString(100 - 100.0 * new_size / original_size) +
"%)";
} else {
log += BuildSize(original_size) + " -> " + BuildSize(new_size);
}
cout << log << endl;
if (new_size < original_size) {
if (!WriteFile(file_path, data.data(), new_size))
cerr << "Error writing file: " << filename << endl;
}
} catch (const std::exception& e) {
cerr << "Error processing " << filename << ": " << e.what() << endl;
error_count++;
} catch (...) {
cerr << "Unknown error processing " << filename << endl;
error_count++;
}
return 0;
}
void PauseIfNotTerminal() {
// pause if Leanify is not started in terminal
// so that user can see the output instead of just a flash of a black box
#ifdef _WIN32
if (is_pause)
system("pause");
#endif // _WIN32
}
int main(int argc, char** argv) {
InstallSignalHandlers();
#ifdef _WIN32
SetUnhandledExceptionFilter(CrashHandler);
#endif
is_fast = false;
is_verbose = false;
iterations = 15;
max_depth = INT_MAX;
zopflipng_lossy_transparent = true;
std::string library_path;
bool quiet = false;
bool keep_exif = false;
bool keep_icc = false;
bool jpeg_keep_all = false;
bool jpeg_arithmetic = false;
bool png_lossless_transparent = false;
bool zip_deflate = false;
std::vector<std::string> paths;
#ifdef _WIN32
is_pause = !getenv("PROMPT");
#endif // _WIN32
CLI::App app{"Leanify " VERSION_STR "\nFork: https://github.com/doterax/Leanify\nBuilt with " COMPILER_STR " (" ARCH_STR ")"};
app.get_formatter()->column_width(40);
app.add_option("-i,--iteration", iterations,
"More iterations may produce better result, but\n"
" use more time, default is 15.")
->check(CLI::PositiveNumber);
app.add_option("-d,--max_depth", max_depth,
"Maximum recursive depth, unlimited by default.\n"
" Set to 1 will disable recursive minifying.")
->check(CLI::PositiveNumber);
app.add_flag("-f,--fastmode", is_fast, "Fast mode, no recompression.");
app.add_flag("-q,--quiet", quiet, "No output to stdout.");
app.add_flag("-v,--verbose", is_verbose, "Verbose output.");
app.add_flag("-p,--parallel", parallel_processing, "Distribute all tasks to all CPUs.");
app.add_option("-l,--library", library_path,
"Use library to store and reuse already compressed files.\n"
" Set * will automatically use temporary folder.");
app.add_flag("--keep-exif", keep_exif, "Do not remove Exif.");
app.add_flag("--keep-icc", keep_icc, "Do not remove ICC profile.");
// JPEG options
app.add_flag("--jpeg-keep-all", jpeg_keep_all,
"Do not remove any metadata or comments in JPEG.");
app.add_flag("--jpeg-arithmetic", jpeg_arithmetic,
"Use arithmetic coding for JPEG.");
// PNG options
app.add_flag("--png-lossless-transparent", png_lossless_transparent,
"Prohibit altering hidden colors of fully transparent pixels.");
// ZIP options
app.add_flag("--zip-deflate", zip_deflate,
"Try deflate even if not compressed originally.");
app.add_option("paths", paths, "File or directory paths to process")
->required();
CLI11_PARSE(app, argc, argv);
#ifdef _WIN32
// Any options given -> do not pause
if (argc > 1)
is_pause = false;
#endif // _WIN32
// Apply parsed flags to statics
if (quiet) {
cout.setstate(std::ios::failbit);
is_verbose = false;
}
if (parallel_processing && is_verbose) {
cerr << "Verbose logs not supported in parallel mode." << endl;
return 1;
}
Jpeg::keep_exif_ = keep_exif;
Jpeg::keep_icc_profile_ = keep_icc;
Png::keep_icc_profile_ = keep_icc;
Jpeg::keep_all_metadata_ = jpeg_keep_all;
Jpeg::force_arithmetic_coding_ = jpeg_arithmetic;
zopflipng_lossy_transparent = !png_lossless_transparent;
Zip::force_deflate_ = zip_deflate;
cout << std::fixed;
cout.precision(2);
if (!library_path.empty()) {
try {
Library::Initialize(library_path);
cout << "Library storage: " << Library::GetStorageName() << endl << endl;
} catch (const std::runtime_error& e) {
cerr << "Error: " << e.what() << endl << endl;
return 1;
}
}
// Phase 1: Collect all file paths
std::vector<std::filesystem::path> work_items;
for (const auto& path : paths) {
auto files = CollectFiles(std::filesystem::path(path));
work_items.insert(work_items.end(),
std::make_move_iterator(files.begin()),
std::make_move_iterator(files.end()));
}
// Phase 2: Create tasks and execute
tf::Taskflow taskflow;
for (auto& file_path : work_items) {
taskflow.emplace([fp = std::move(file_path)]() {
ProcessFile(fp);
});
}
size_t parallel_tasks = 1;
if (parallel_processing)
parallel_tasks = std::thread::hardware_concurrency();
tf::Executor executor(parallel_tasks);
executor.run(taskflow).wait();
int errors = error_count.load();
if (errors > 0)
cout << "Finished with " << errors << " error(s)." << endl;
else
cout << "Finished." << endl;
PauseIfNotTerminal();
return errors > 0 ? 1 : 0;
}