-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
icbc_test.cpp
531 lines (435 loc) · 15.6 KB
/
icbc_test.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Compilation instructions:
// $ g++ icbc_test.cpp -O3 -mavx2 -lpthread -std=c+=11
// > cl icbc_test.cpp /O2 /arch:AVX2
// Enable one of these to override the default selection:
//#define ICBC_SIMD 0 // FLOAT
//#define ICBC_SIMD 1 // SSE2
//#define ICBC_SIMD 2 // SSE4.1
//#define ICBC_SIMD 3 // AVX
//#define ICBC_SIMD 4 // AVX2
//#define ICBC_SIMD 5 // AVX512
//#define ICBC_SIMD -1 // NEON
//#define ICBC_SIMD -2 // VMX
#define ICBC_IMPLEMENTATION
#include "icbc.h"
// stb_image from: https://github.com/nothings/stb/blob/master/stb_image.h
#define STB_IMAGE_IMPLEMENTATION
#include "extern/stb_image.h"
// stb_image_write from: https://github.com/nothings/stb/blob/master/stb_image_write.h
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "extern/stb_image_write.h"
#define IC_PFOR_IMPLEMENTATION
#include "ic_pfor.h"
#include <stdio.h>
#include <stdint.h>
////////////////////////////////
// Basic types
typedef unsigned char u8;
typedef unsigned int u32;
typedef uint64_t u64;
////////////////////////////////
// defer
#define CONCAT_INTERNAL(x,y) x##y
#define CONCAT(x,y) CONCAT_INTERNAL(x,y)
template<typename T>
struct ExitScope {
T lambda;
ExitScope(T lambda):lambda(lambda){}
~ExitScope(){lambda();}
private:
ExitScope& operator=(const ExitScope&);
};
struct ExitScopeHelp {
template<typename T>
ExitScope<T> operator+(T t){ return t;}
};
#if _MSC_VER
#define defer const auto& CONCAT(defer__, __LINE__) = ExitScopeHelp() + [&]()
#else // __GNUC__ or __clang__
#define defer const auto& __attribute__((unused)) CONCAT(defer__, __LINE__) = ExitScopeHelp() + [&]()
#endif
////////////////////////////////
// Timer
// Based of https://gist.github.com/pervognsen/496d659251ad2af200dde4c773bd565f
#if defined _WIN32 || __CYGWIN__
#include <windows.h>
inline double timer_frequency() {
LARGE_INTEGER qpf;
QueryPerformanceFrequency(&qpf);
return double(qpf.QuadPart);
}
inline u64 timer_time() {
LARGE_INTEGER qpc;
QueryPerformanceCounter(&qpc);
return qpc.QuadPart;
}
#elif defined(__APPLE__) || defined(__MACH__)
#include <mach/mach_time.h>
inline double timer_frequency() {
mach_timebase_info_data_t mach_timebase;
mach_timebase_info(&mach_timebase);
return double(mach_timebase.denom) / mach_timebase.numer * 1e9;
}
inline u64 timer_time() {
return mach_absolute_time();
}
#else
#include <time.h>
inline double timer_frequency() {
return 1e9;
}
inline u64 timer_time() {
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
return -1;
}
return u64(ts.tv_sec) * 1000000000 + ts.tv_nsec;
}
#endif
struct Timer {
double freq;
u64 time_start;
Timer() {
freq = timer_frequency();
time_start = 0;
}
void start() {
time_start = timer_time();
}
double secs() {
if (time_start == 0) return 0.0;
u64 time_now = timer_time();
return (time_now - time_start) / freq;
}
double stop() {
double t = secs();
time_start = 0;
return t;
}
};
struct TimeEstimate {
int num = 0; // number of samples
double unit = 1.0; // units per second
double min = 0.0; // min sample
double max = 0.0; // max sample
double avg = 0.0; // average sample
void add(double sample) {
num++;
min = (num == 1 || sample < min) ? sample : min;
max = (num == 1 || sample > max) ? sample : max;
avg += (sample - avg) / num;
}
};
////////////////////////////////
// File Output
#define IC_MAKEFOURCC(str) (u32(str[0]) | (u32(str[1]) << 8) | (u32(str[2]) << 16) | (u32(str[3]) << 24 ))
static bool output_dxt_dds(u32 w, u32 h, const u8* data, const char * filename) {
const u32 DDSD_CAPS = 0x00000001;
const u32 DDSD_PIXELFORMAT = 0x00001000;
const u32 DDSD_WIDTH = 0x00000004;
const u32 DDSD_HEIGHT = 0x00000002;
const u32 DDSD_LINEARSIZE = 0x00080000;
const u32 DDPF_FOURCC = 0x00000004;
const u32 DDSCAPS_TEXTURE = 0x00001000;
struct DDS {
u32 fourcc = IC_MAKEFOURCC("DDS ");
u32 size = 124;
u32 flags = DDSD_CAPS|DDSD_PIXELFORMAT|DDSD_WIDTH|DDSD_HEIGHT|DDSD_LINEARSIZE;
u32 height = 0;
u32 width = 0;
u32 pitch = 0;
u32 depth = 0;
u32 mipmapcount = 1;
u32 reserved [11];
struct {
u32 size = 32;
u32 flags = DDPF_FOURCC;
u32 fourcc = IC_MAKEFOURCC("DXT1");
u32 bitcount = 0;
u32 rmask = 0;
u32 gmask = 0;
u32 bmask = 0;
u32 amask = 0;
} pf;
struct {
u32 caps1 = DDSCAPS_TEXTURE;
u32 caps2 = 0;
u32 caps3 = 0;
u32 caps4 = 0;
} caps;
u32 notused = 0;
} dds;
static_assert(sizeof(DDS) == 128, "DDS size must be 128");
dds.width = w;
dds.height = h;
dds.pitch = 8 * (((w+3)/4) * ((h+3)/4)); // linear size
FILE * fp = fopen(filename, "wb");
if (fp == nullptr) return false;
// Write header:
fwrite(&dds, sizeof(dds), 1, fp);
// Write dxt data:
fwrite(data, dds.pitch, 1, fp);
fclose(fp);
return true;
}
static bool compare_dxt_dds(u32 w, u32 h, const u8* data, const char * filename) {
u32 size = 8 * (((w+3)/4) * ((h+3)/4)); // linear size
void * original = malloc(size);
defer { free(original); };
FILE * fp = fopen(filename, "rb");
if (fp == nullptr) return false;
// Skip header:
fseek(fp, 128, SEEK_SET);
// Read dxt data:
fread(original, size, 1, fp);
fclose(fp);
return memcmp(original, data, size) == 0;
}
static bool output_dxt_ktx(u32 w, u32 h, const u8* data, const char * filename) {
const u32 GL_COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0;
const u32 GL_RGBA = 0x1908;
struct KTX {
// '«', 'K', 'T', 'X', ' ', '2', '0', '»', '\r', '\n', '\x1A', '\n'
u8 identifier[12] = { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A};
u32 endianness = 0x04030201;
u32 glType = 0;
u32 glTypeSize = 0;
u32 glFormat = 0;
u32 glInternalFormat = 0;
u32 glBaseInternalFormat = 0;
u32 pixelWidth = 0;
u32 pixelHeight = 0;
u32 pixelDepth = 0;
u32 numberOfArrayElements = 0;
u32 numberOfFaces = 0;
u32 numberOfMipmapLevels = 0;
u32 bytesOfKeyValueData = 0;
} ktx;
ktx.glTypeSize = 1;
ktx.glInternalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
ktx.glBaseInternalFormat = GL_RGBA;
ktx.pixelWidth = w;
ktx.pixelHeight = h;
ktx.numberOfFaces = 1;
ktx.numberOfMipmapLevels = 1;
u32 image_size = 8 * ((w+3)/4 * (h+3)/4);
FILE * fp = fopen(filename, "wb");
if (fp == nullptr) return false;
// Write header:
fwrite(&ktx, sizeof(ktx), 1, fp);
fwrite(&image_size, sizeof(u32), 1, fp);
// Write dxt data:
fwrite(data, image_size, 1, fp);
fclose(fp);
return true;
}
static bool output_dxt_png(u32 w, u32 h, const u8* data, const char * filename, icbc::Decoder decoder) {
u8 * rgb_data = (u8 *)malloc(w * h * 4 * 4 * 3);
for (int y = 0; y < h; y += 4) {
for (int x = 0; x < w; x += 4) {
unsigned char rgba_block[16 * 4];
icbc::decode_bc1(data, rgba_block, decoder);
data += 8;
for (int yy = 0; yy < 4; yy++) {
for (int xx = 0; xx < 4; xx++) {
rgb_data[(y + yy) * w * 3 + (x + xx) * 3 + 0] = rgba_block[yy * 16 + xx * 4 + 0];
rgb_data[(y + yy) * w * 3 + (x + xx) * 3 + 1] = rgba_block[yy * 16 + xx * 4 + 1];
rgb_data[(y + yy) * w * 3 + (x + xx) * 3 + 2] = rgba_block[yy * 16 + xx * 4 + 2];
}
}
}
}
return stbi_write_png(filename, w, h, 3, rgb_data, /*stride_in_bytes=*/w*3) != 0;
}
////////////////////////////////
// DXT
// Returns mse.
static float evaluate_bc1_mse(u8 * rgba, u8 * block, int block_count, icbc::Decoder decoder = icbc::Decoder_D3D10) {
double total = 0.0f;
for (int b = 0; b < block_count; b++) {
total += icbc::evaluate_bc1_error(rgba, block, decoder);
rgba += 4 * 4 * 4;
block += 8;
}
return float(total / (16 * block_count));
}
static float mse_to_psnr(float mse) {
float rms = sqrtf(mse);
float psnr = rms ? (float)icbc::clamp(log10(255.0 / rms) * 20.0, 0.0, 300.0) : 1e+10f;
return psnr;
}
// Input options:
bool output_dds = false;
bool output_ktx = false;
bool output_png = false;
bool compare_dds = false;
int repeat_count = 1;
icbc::Decoder decoder = icbc::Decoder_D3D10;
icbc::Quality quality_level = icbc::Quality_Default;
// Output stats:
int total_block_count = 0;
double total_avg_time = 0;
double total_min_time = 0;
double total_mse = 0;
bool encode_image(const char * input_filename) {
int w, h, n;
unsigned char *input_data = stbi_load(input_filename, &w, &h, &n, 4);
defer { stbi_image_free(input_data); };
if (input_data == nullptr) {
printf("Failed to load input image '%s'.\n", input_filename);
return false;
}
int block_count = (w / 4) * (h / 4);
u8 * rgba_block_data = (u8 *)malloc(block_count * 4 * 4 * 4);
defer { free(rgba_block_data); };
int bw = 4 * (w / 4); // @@ Round down.
int bh = 4 * (h / 4);
// Convert to block layout.
for (int y = 0, b = 0; y < bh; y += 4) {
for (int x = 0; x < bw; x += 4, b++) {
for (int yy = 0; yy < 4; yy++) {
for (int xx = 0; xx < 4; xx++) {
if (x + xx < w && y + yy < h) {
rgba_block_data[b * 4 * 4 * 4 + (yy * 4 + xx) * 4 + 0] = input_data[((y + yy) * w + x + xx) * 4 + 0];
rgba_block_data[b * 4 * 4 * 4 + (yy * 4 + xx) * 4 + 1] = input_data[((y + yy) * w + x + xx) * 4 + 1];
rgba_block_data[b * 4 * 4 * 4 + (yy * 4 + xx) * 4 + 2] = input_data[((y + yy) * w + x + xx) * 4 + 2];
rgba_block_data[b * 4 * 4 * 4 + (yy * 4 + xx) * 4 + 3] = input_data[((y + yy) * w + x + xx) * 4 + 3];
}
else {
rgba_block_data[b * 4 * 4 * 4 + (yy * 4 + xx) * 4 + 0] = 0;
rgba_block_data[b * 4 * 4 * 4 + (yy * 4 + xx) * 4 + 1] = 0;
rgba_block_data[b * 4 * 4 * 4 + (yy * 4 + xx) * 4 + 2] = 0;
rgba_block_data[b * 4 * 4 * 4 + (yy * 4 + xx) * 4 + 3] = 0;
}
}
}
}
}
//const float color_weights[3] = {3, 4, 2}; // This is probably better for color images.
const float color_weights[3] = {1, 1, 1};
u8 * block_data = (u8 *)malloc(block_count * 8);
printf("Encoding '%s':", input_filename);
TimeEstimate estimate;
Timer timer;
for (int i = 0; i < repeat_count; i++) {
timer.start();
//for (int b = 0; b < block_count; b++) {
//ic::pfor(block_count, 32, [=](int b) {
ic_pfor(b, block_count, 32) {
ICBC_ALIGN float input_colors[16 * 4];
ICBC_ALIGN float input_weights[16];
for (int j = 0; j < 16; j++) {
input_colors[4 * j + 0] = rgba_block_data[b * 4 * 4 * 4 + j * 4 + 0] / 255.0f;
input_colors[4 * j + 1] = rgba_block_data[b * 4 * 4 * 4 + j * 4 + 1] / 255.0f;
input_colors[4 * j + 2] = rgba_block_data[b * 4 * 4 * 4 + j * 4 + 2] / 255.0f;
input_colors[4 * j + 3] = 1.0f;
input_weights[j] = 1.0f;
}
icbc::compress_bc1(quality_level, input_colors, input_weights, color_weights, /*three_color_mode=*/true, /*three_color_black=*/true, (block_data + b * 8));
};
//});
estimate.add(timer.stop());
}
float mse = evaluate_bc1_mse(rgba_block_data, block_data, block_count, decoder);
char output_filename[1024];
if (output_dds) {
snprintf(output_filename, 1024, "%.*s_bc1.dds", int(strchr(input_filename, '.')-input_filename), input_filename);
output_dxt_dds(bw, bh, block_data, output_filename);
}
if (output_ktx) {
snprintf(output_filename, 1024, "%.*s_bc1.ktx", int(strchr(input_filename, '.')-input_filename), input_filename);
output_dxt_ktx(bw, bh, block_data, output_filename);
}
if (output_png) {
snprintf(output_filename, 1024, "%.*s_bc1.png", int(strchr(input_filename, '.')-input_filename), input_filename);
output_dxt_png(bw, bh, block_data, output_filename, decoder);
}
if (compare_dds) {
snprintf(output_filename, 1024, "%.*s_bc1.dds", int(strchr(input_filename, '.')-input_filename), input_filename);
if (!compare_dxt_dds(bw, bh, block_data, output_filename)) {
printf("\nFiles differ!!!");
}
}
total_block_count += block_count;
total_avg_time += estimate.avg;
total_min_time += estimate.min;
total_mse += mse * block_count;
printf("\tRMSE = %.3f\tPSNR = %.3f\tTIME = %f (%f)\n", sqrtf(mse), mse_to_psnr(mse), estimate.avg, estimate.min);
return true;
}
// Kodak image set from: http://r0k.us/graphics/kodak/
static const char * images[] = {
"data/kodim01.png",
"data/kodim02.png",
"data/kodim03.png",
"data/kodim04.png",
"data/kodim05.png",
"data/kodim06.png",
"data/kodim07.png",
"data/kodim08.png",
"data/kodim09.png",
"data/kodim10.png",
"data/kodim11.png",
"data/kodim12.png",
"data/kodim13.png",
"data/kodim14.png",
"data/kodim15.png",
"data/kodim16.png",
"data/kodim17.png",
"data/kodim18.png",
"data/kodim19.png",
"data/kodim20.png",
"data/kodim21.png",
"data/kodim22.png",
"data/kodim23.png",
"data/kodim24.png",
};
static const int image_count = sizeof(images) / sizeof(images[0]);
int main(int argc, char * argv[]) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-dds") == 0) {
output_dds = true;
}
else if (strcmp(argv[i], "-ktx") == 0) {
output_ktx = true;
}
else if (strcmp(argv[i], "-png") == 0) {
output_png = true;
}
else if (strcmp(argv[i], "-cmp") == 0) {
compare_dds = true;
}
else if (strncmp(argv[i], "-q", 2) == 0) {
if (argv[i][2]) {
quality_level = (icbc::Quality)(argv[i][2] - '1');
if (quality_level < icbc::Quality_Fast) quality_level = icbc::Quality_Fast;
if (quality_level > icbc::Quality_Max) quality_level = icbc::Quality_Max;
}
}
else if (strcmp(argv[i], "-dec") == 0) {
if (i+1 < argc) {
if (strcmp(argv[i+1], "nv") == 0) decoder = icbc::Decoder_NVIDIA;
else if (strcmp(argv[i+1], "amd") == 0) decoder = icbc::Decoder_AMD;
else if (strcmp(argv[i+1], "intel") == 0) decoder = icbc::Decoder_Intel;
else if (strcmp(argv[i+1], "d3d10") == 0) decoder = icbc::Decoder_D3D10;
else {
printf("Unrecognized decoder argument: %s\n", argv[i+1]);
}
i += 1;
}
}
else if (atoi(argv[i])) {
repeat_count = atoi(argv[i]);
}
}
icbc::init(decoder);
int thread_count = ic::init_pfor();
printf("Using %d threads.\n", thread_count);
for (int i = 0; i < image_count; i++) {
encode_image(images[i]);
}
total_mse /= total_block_count;
printf("Average Results:\n");
printf("\tRMSE = %.3f\tPSNR = %.3f\tTIME = %f (%f)\n", sqrtf(total_mse), mse_to_psnr(total_mse), total_avg_time, total_min_time);
return 0;
}