forked from BinomialLLC/basis_universal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyperpix_tool.cpp
More file actions
314 lines (276 loc) · 9.55 KB
/
hyperpix_tool.cpp
File metadata and controls
314 lines (276 loc) · 9.55 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
#include <dirent.h>
#include <fcntl.h>
#include <sys/dirent.h>
#include <sys/fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <latch>
#include <mutex>
#include <thread>
#include <vector>
#include "encoder/basisu_comp.h"
#include "encoder/basisu_enc.h"
#include "encoder/basisu_frontend.h"
#include "zstd/zstd.h"
#include <libgen.h>
#include <atomic>
#define SIGNATURE "HYPERPIX"
#define SIGNATURE_LEN 8
#define FLAGS_LEN 8
#define VERSION 0x01
static std::atomic_int64_t fileSize0 = 0;
static std::atomic_int64_t fileSize1 = 0;
static std::atomic_int32_t skipFiles = 0;
static std::atomic_int32_t errorFiles = 0;
enum HyperPixQuality {
HPQ_BEST = 0,
HPQ_HIGH = 1,
HPQ_DEFAULT = HPQ_HIGH,
HPQ_NORMAL = 2,
HPQ_LOW = 3,
};
struct HyperPixFlags {
uint8_t version = VERSION;
uint8_t quality = HPQ_HIGH;
uint8_t hasAlpha = 0;
uint8_t reserved[5] = {0};
};
std::vector<uint8_t> compressFile(const void *data, size_t dataLen) {
const int compressLevel = ZSTD_maxCLevel();
#if 0
auto capacity = ZSTD_compressBound(dataLen);
std::vector<uint8_t> output(capacity);
size_t compressedLen = ZSTD_compress(output.data(), capacity, data, dataLen, compressLevel);
output.resize(compressedLen);
#else
auto *ctx = ZSTD_createCCtx();
auto capacity = ZSTD_compressBound(dataLen);
std::vector<uint8_t> output(capacity);
auto compressedLen = ZSTD_compressCCtx(ctx, output.data(), output.size(), data, dataLen, compressLevel);
output.resize(compressedLen);
ZSTD_freeCCtx(ctx);
#endif
return output;
}
void writeFile(const char *dst, const void *data, size_t dataLen, bool hasAlpha, HyperPixQuality quality = HPQ_DEFAULT) {
int fd = open(dst, O_RDWR | O_CREAT | O_TRUNC, 0664);
if (fd < 0) {
fprintf(stderr, "[error] failed to open <%s>!\n", dst);
return;
}
HyperPixFlags flags;
flags.hasAlpha = hasAlpha;
flags.quality = quality;
ftruncate(fd, dataLen + FLAGS_LEN + SIGNATURE_LEN);
void *ptr =
mmap(nullptr, dataLen + 8, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
fprintf(stderr, "[error] failed to mmap <%s>!\n", dst);
return;
}
auto *cptr = reinterpret_cast<char *>(ptr);
if constexpr (FLAGS_LEN + SIGNATURE_LEN > 0) {
memcpy(ptr, SIGNATURE, SIGNATURE_LEN);
memcpy(cptr + SIGNATURE_LEN, &flags, FLAGS_LEN);
}
memcpy(cptr + SIGNATURE_LEN + FLAGS_LEN, data, dataLen);
munmap(ptr, dataLen + SIGNATURE_LEN + FLAGS_LEN);
close(fd);
}
static void collectPngFiles(const std::string &dirname, std::vector<std::string> &output) {
DIR *dir = opendir(dirname.c_str());
struct dirent *ent = nullptr;
while ((ent = readdir(dir)) != nullptr) {
auto cdirname = std::string(ent->d_name, ent->d_namlen);
if (cdirname.length() == 1 && cdirname[0] == '.') continue;
if (cdirname.length() == 2 && cdirname[0] == '.' && cdirname[1] == '.') continue;
auto filepath = (dirname + "/").append(cdirname);
if (ent->d_type == DT_DIR) {
collectPngFiles(filepath, output);
} else if (ent->d_type == DT_REG) {
if (filepath.ends_with(".png") || filepath.ends_with(".jpg") || filepath.ends_with(".jpeg")) {
output.emplace_back(filepath);
}
}
}
closedir(dir);
}
static void printProgress(float precent, int a, int b) {
printf("\r\033[K 转码中 [");
int l = static_cast<int>(precent * 30);
int r = 30 - l;
for (int i = 0; i < 30; i++) {
if (i < l)
putc('#', stdout);
else
putc(' ', stdout);
}
printf("] %.2f%% >> %d/%d <<", precent * 100, a, b);
fflush(stdout);
}
bool convertFile(const std::string &file, const std::string &output, std::atomic_bool &);
#ifndef VERSION_STRING
#define VERSION_STRING "0.1.0"
#endif
#ifndef COMMIT_STRING
#define COMMIT_STRING "unknow commit id"
#endif
int main(int argc, char **argv) {
int opt = 0;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") || strcmp(argv[i], "--help")) {
opt = 'h';
break;
}
}
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-v") || strcmp(argv[i], "--version")) {
opt = 'v';
break;
}
}
if (argc == 1) {
opt = 'h';
}
switch (opt) {
case 'h':
printf("Usage: %s file_or_directory ... \n", argv[0]);
return 0;
case 'v':
printf("Version: %s\nCommit: %s\n", VERSION_STRING, COMMIT_STRING);
return 0;
}
basisu::basisu_encoder_init();
basisu::job_pool pool(std::thread::hardware_concurrency());
std::atomic_bool openCLFailed = false;
for (int arg = 1; arg < argc; arg++) {
struct stat st;
if (lstat(argv[arg], &st)) {
fprintf(stderr, "[error] file <%s> not found!\n", argv[1]);
continue;
}
if (S_ISREG(st.st_mode)) {
printf("[log] 处理单一文件<%s>...\n", argv[arg]);
convertFile(argv[arg], argv[arg], openCLFailed);
} else if (S_ISDIR(st.st_mode)) {
printf("[log] 查找目录<%s>中的图片...\n", argv[arg]);
std::vector<std::string> files;
collectPngFiles(argv[arg], files);
std::atomic_int32_t i = 0;
std::mutex mtx;
std::latch lat(files.size());
auto total = files.size();
for (auto &f : files) {
pool.add_job([ff = f, &i, &openCLFailed, &mtx, &lat, total]() {
i++;
convertFile(ff, ff, openCLFailed);
lat.count_down();
});
}
while (!lat.try_wait()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
printProgress(i * 1.0F / total, i.load(), total);
}
printf("\r\033[K 转码完成. 共 %d 个图片\n", static_cast<int>(files.size()));
if (skipFiles) {
printf(" 忽略 %d 个图片\n", skipFiles.load(std::memory_order_acquire));
}
if (errorFiles) {
printf(" 错误图片: %d\n", errorFiles.load(std::memory_order_acquire));
}
} else {
fprintf(stderr, "[error] unknown file type <%s>!\n", argv[arg]);
}
}
pool.wait_for_all();
if (fileSize1 > 0) {
printf(" 文件大小变化:%lld -> %lld\n", fileSize0.load(), fileSize1.load());
auto diff = 100.0 * (fileSize1.load() - fileSize0.load()) / fileSize0.load();
printf(" %s : %.2lf %%\n", diff > 0 ? "膨胀" : "减少", diff);
}
return EXIT_SUCCESS;
}
bool convertFile(const std::string &file, const std::string &output, std::atomic_bool &openCLFailed) {
int fd = open(file.c_str(), O_RDONLY);
if (fd < 0) {
fprintf(stderr, "[error] failed to open file image <%s>!\n", file.c_str());
errorFiles++;
return false;
}
struct stat st;
fstat(fd, &st);
if (st.st_size < 16) {
fprintf(stderr, "[error] file <%s> too small!\n", file.c_str());
close(fd);
errorFiles++;
return false;
}
char header[16];
auto readn = read(fd, header, 16);
if (readn < 16) {
fprintf(stderr, "[error] failed to read header <%s>\n", file.c_str());
close(fd);
errorFiles++;
return false;
}
if (memcmp(header, SIGNATURE, SIGNATURE_LEN) == 0) {
// printf("\n[log] skip file %s\n", file.c_str());
close(fd);
skipFiles++;
return false;
}
close(fd);
basisu::basis_compressor_params params;
basisu::basis_compressor compressor;
basisu::image srcImage;
if (!basisu::load_image(file, srcImage)) {
fprintf(stderr, "[error] failed to load image <%s>!\n", file.c_str());
errorFiles++;
return EXIT_FAILURE;
}
fileSize0 += st.st_size;
basisu::job_pool lpool(1);
params.m_compression_level = basisu::BASISU_MAX_COMPRESSION_LEVEL;
params.m_create_ktx2_file = false;
params.m_uastc = true;
// params.m_quality_level = 120;
params.m_pJob_pool = &lpool;
params.m_status_output = false;
// params.m_use_opencl = true;
// params.m_no_selector_rdo = true;
// params.m_mip_gen = false;
// params.m_read_source_images = true;
params.m_source_images.push_back(srcImage);
if (openCLFailed.load(std::memory_order_acquire)) {
params.m_use_opencl = false;
} else {
params.m_use_opencl = true;
}
compressor.init(params);
if (compressor.get_opencl_failed()) {
openCLFailed.store(true, std::memory_order_release);
}
auto code = compressor.process();
if (code != basisu::basis_compressor::error_code::cECSuccess) {
fprintf(stderr, "[error] failed to encode image <%s>!\n", file.c_str());
errorFiles++;
return false;
}
lpool.wait_for_all();
const auto &basisFile = compressor.get_output_basis_file();
#if 1 // enable compress
auto compressedData = compressFile(basisFile.data(), basisFile.size());
writeFile(output.c_str(), compressedData.data(), compressedData.size(), srcImage.has_alpha());
fileSize1.fetch_add(compressedData.size() + SIGNATURE_LEN + FLAGS_LEN);
#else
writeFile(output.c_str(), basisFile.data(), basisFile.size(), srcImage.has_alpha());
fileSize1.fetch_add(basisFile.size() + SIGNATURE_LEN + FLAGS_LEN);
#endif
return true;
}