-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbc7enc.cpp
744 lines (605 loc) · 20 KB
/
bc7enc.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
// bc7enc.cpp - bc7enc17.c command line example/test app
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <assert.h>
#include <time.h>
#include "bc7enc16.h"
#include "lodepng.h"
#include "dds_defs.h"
#include "ktx_defs.h"
#include "bc7decomp.h"
template <typename T> inline T clamp(T v, T l, T h) { if (v < l) v = l; else if (v > h) v = h; return v; }
inline int iabs(int i) { if (i < 0) i = -i; return i; }
static int print_usage()
{
fprintf(stderr, "bc7enc\n");
fprintf(stderr, "Reads PNG files (with or without alpha channels) and packs them to BC7/BPTC using modes 1 and 6.\n");
fprintf(stderr, "This tool works best with opaque images, or on images with relatively simple alpha channels.\n");
fprintf(stderr, "By default, a DX10 DDS file and a unpacked PNG file will be written to the source file's directory with the .dds/_unpacked.png/_unpacked_alpha.png suffixes.\n\n");
fprintf(stderr, "Usage: bc7enc [-apng_filename] [-l] [-uX] [-aX] [-g] [-y] input_filename.png [compressed_output.dds] [unpacked_output.png]\n");
fprintf(stderr, "-apng_filename Load G channel of PNG file into alpha channel of source image\n");
fprintf(stderr, "-l Use linear colorspace metrics instead of perceptual\n");
fprintf(stderr, "-uX Higher quality levels, X ranges from [0,4], higher=slower\n");
fprintf(stderr, "-pX Scan X partitions in mode 1, X ranges from [0,64], use 0 to disable mode 1 entirely (faster)\n");
fprintf(stderr, "-g Don't write an unpacked output PNG file\n");
fprintf(stderr, "-y Flip source image along Y axis before packing\n");
fprintf(stderr, "-k Generate .ktx file instead of .dds file\n");
return EXIT_FAILURE;
}
struct color_quad_u8
{
uint8_t m_c[4];
inline color_quad_u8(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
set(r, g, b, a);
}
inline color_quad_u8(uint8_t y = 0, uint8_t a = 255)
{
set(y, a);
}
inline color_quad_u8 &set(uint8_t y, uint8_t a = 255)
{
m_c[0] = y;
m_c[1] = y;
m_c[2] = y;
m_c[3] = a;
return *this;
}
inline color_quad_u8 &set(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
m_c[0] = r;
m_c[1] = g;
m_c[2] = b;
m_c[3] = a;
return *this;
}
inline uint8_t &operator[] (uint32_t i) { assert(i < 4); return m_c[i]; }
inline uint8_t operator[] (uint32_t i) const { assert(i < 4); return m_c[i]; }
inline int get_luma() const { return (13938U * m_c[0] + 46869U * m_c[1] + 4729U * m_c[2] + 32768U) >> 16U; } // REC709 weightings
};
typedef std::vector<color_quad_u8> color_quad_u8_vec;
class image_u8
{
public:
image_u8() :
m_width(0), m_height(0)
{
}
image_u8(uint32_t width, uint32_t height) :
m_width(width), m_height(height)
{
m_pixels.resize(width * height);
}
inline const color_quad_u8_vec &get_pixels() const { return m_pixels; }
inline color_quad_u8_vec &get_pixels() { return m_pixels; }
inline uint32_t width() const { return m_width; }
inline uint32_t height() const { return m_height; }
inline uint32_t total_pixels() const { return m_width * m_height; }
inline color_quad_u8 &operator()(uint32_t x, uint32_t y) { assert(x < m_width && y < m_height); return m_pixels[x + m_width * y]; }
inline const color_quad_u8 &operator()(uint32_t x, uint32_t y) const { assert(x < m_width && y < m_height); return m_pixels[x + m_width * y]; }
image_u8& clear()
{
m_width = m_height = 0;
m_pixels.clear();
return *this;
}
image_u8& init(uint32_t width, uint32_t height)
{
clear();
m_width = width;
m_height = height;
m_pixels.resize(width * height);
return *this;
}
image_u8& set_all(const color_quad_u8 &p)
{
for (uint32_t i = 0; i < m_pixels.size(); i++)
m_pixels[i] = p;
return *this;
}
image_u8& crop(uint32_t new_width, uint32_t new_height)
{
if ((m_width == new_width) && (m_height == new_height))
return *this;
image_u8 new_image(new_width, new_height);
const uint32_t w = std::min(m_width, new_width);
const uint32_t h = std::min(m_height, new_height);
for (uint32_t y = 0; y < h; y++)
for (uint32_t x = 0; x < w; x++)
new_image(x, y) = (*this)(x, y);
return swap(new_image);
}
image_u8 &swap(image_u8 &other)
{
std::swap(m_width, other.m_width);
std::swap(m_height, other.m_height);
std::swap(m_pixels, other.m_pixels);
return *this;
}
inline void get_block(uint32_t bx, uint32_t by, uint32_t width, uint32_t height, color_quad_u8 *pPixels)
{
assert((bx * width + width) <= m_width);
assert((by * height + height) <= m_height);
for (uint32_t y = 0; y < height; y++)
memcpy(pPixels + y * width, &(*this)(bx * width, by * height + y), width * sizeof(color_quad_u8));
}
inline void set_block(uint32_t bx, uint32_t by, uint32_t width, uint32_t height, const color_quad_u8 *pPixels)
{
assert((bx * width + width) <= m_width);
assert((by * height + height) <= m_height);
for (uint32_t y = 0; y < height; y++)
memcpy(&(*this)(bx * width, by * height + y), pPixels + y * width, width * sizeof(color_quad_u8));
}
image_u8 &swizzle(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
{
assert((r | g | b | a) <= 3);
for (uint32_t y = 0; y < m_height; y++)
{
for (uint32_t x = 0; x < m_width; x++)
{
color_quad_u8 tmp((*this)(x, y));
(*this)(x, y).set(tmp[r], tmp[g], tmp[b], tmp[a]);
}
}
return *this;
}
private:
color_quad_u8_vec m_pixels;
uint32_t m_width, m_height;
};
static bool load_png(const char *pFilename, image_u8 &img)
{
img.clear();
std::vector<unsigned char> pixels;
unsigned int w = 0, h = 0;
unsigned int e = lodepng::decode(pixels, w, h, pFilename);
if (e != 0)
{
fprintf(stderr, "Failed loading PNG file %s\n", pFilename);
return false;
}
img.init(w, h);
memcpy(&img.get_pixels()[0], &pixels[0], w * h * sizeof(uint32_t));
return true;
}
static bool save_png(const char *pFilename, const image_u8 &img, bool save_alpha)
{
const uint32_t w = img.width();
const uint32_t h = img.height();
std::vector<unsigned char> pixels;
if (save_alpha)
{
pixels.resize(w * h * sizeof(color_quad_u8));
memcpy(&pixels[0], &img.get_pixels()[0], w * h * sizeof(color_quad_u8));
}
else
{
pixels.resize(w * h * 3);
unsigned char *pDst = &pixels[0];
for (uint32_t y = 0; y < h; y++)
for (uint32_t x = 0; x < w; x++, pDst += 3)
pDst[0] = img(x, y)[0], pDst[1] = img(x, y)[1], pDst[2] = img(x, y)[2];
}
return lodepng::encode(pFilename, pixels, w, h, save_alpha ? LCT_RGBA : LCT_RGB) == 0;
}
class image_metrics
{
public:
double m_max, m_mean, m_mean_squared, m_root_mean_squared, m_peak_snr;
image_metrics()
{
clear();
}
void clear()
{
memset(this, 0, sizeof(*this));
}
void compute(const image_u8 &a, const image_u8 &b, uint32_t first_channel, uint32_t num_channels)
{
const bool average_component_error = true;
const uint32_t width = std::min(a.width(), b.width());
const uint32_t height = std::min(a.height(), b.height());
assert((first_channel < 4U) && (first_channel + num_channels <= 4U));
// Histogram approach originally due to Charles Bloom.
double hist[256];
memset(hist, 0, sizeof(hist));
for (uint32_t y = 0; y < height; y++)
{
for (uint32_t x = 0; x < width; x++)
{
const color_quad_u8 &ca = a(x, y);
const color_quad_u8 &cb = b(x, y);
if (!num_channels)
hist[iabs(ca.get_luma() - cb.get_luma())]++;
else
{
for (uint32_t c = 0; c < num_channels; c++)
hist[iabs(ca[first_channel + c] - cb[first_channel + c])]++;
}
}
}
m_max = 0;
double sum = 0.0f, sum2 = 0.0f;
for (uint32_t i = 0; i < 256; i++)
{
if (!hist[i])
continue;
m_max = std::max<double>(m_max, i);
double x = i * hist[i];
sum += x;
sum2 += i * x;
}
// See http://richg42.blogspot.com/2016/09/how-to-compute-psnr-from-old-berkeley.html
double total_values = width * height;
if (average_component_error)
total_values *= clamp<uint32_t>(num_channels, 1, 4);
m_mean = clamp<double>(sum / total_values, 0.0f, 255.0f);
m_mean_squared = clamp<double>(sum2 / total_values, 0.0f, 255.0f * 255.0f);
m_root_mean_squared = sqrt(m_mean_squared);
if (!m_root_mean_squared)
m_peak_snr = 1e+10f;
else
m_peak_snr = clamp<double>(log10(255.0f / m_root_mean_squared) * 20.0f, 0.0f, 500.0f);
}
};
struct bc7_block
{
uint64_t m_vals[2];
};
typedef std::vector<bc7_block> bc7_block_vec;
static bool save_bc7_dds(const char *pFilename, uint32_t width, uint32_t height, const bc7_block *pBlocks, bool srgb)
{
(void)srgb;
FILE *pFile = NULL;
pFile = fopen(pFilename, "wb");
if (!pFile)
{
fprintf(stderr, "Failed creating file %s!\n", pFilename);
return false;
}
fwrite("DDS ", 4, 1, pFile);
DDSURFACEDESC2 desc;
memset(&desc, 0, sizeof(desc));
desc.dwSize = sizeof(desc);
desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
desc.dwWidth = width;
desc.dwHeight = height;
desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
desc.ddpfPixelFormat.dwSize = sizeof(desc.ddpfPixelFormat);
desc.ddpfPixelFormat.dwFlags |= DDPF_FOURCC;
desc.ddpfPixelFormat.dwFourCC = (uint32_t)PIXEL_FMT_FOURCC('D', 'X', '1', '0');
desc.ddpfPixelFormat.dwRGBBitCount = 0;
const uint32_t pixel_format_bpp = 8;
desc.lPitch = (((desc.dwWidth + 3) & ~3) * ((desc.dwHeight + 3) & ~3) * pixel_format_bpp) >> 3;
desc.dwFlags |= DDSD_LINEARSIZE;
fwrite(&desc, sizeof(desc), 1, pFile);
DDS_HEADER_DXT10 hdr10;
memset(&hdr10, 0, sizeof(hdr10));
// Not all tools support DXGI_FORMAT_BC7_UNORM_SRGB (like NVTT), but ddsview in DirectXTex pays attention to it. So not sure what to do here.
// For best compatibility just write DXGI_FORMAT_BC7_UNORM.
//hdr10.dxgiFormat = srgb ? DXGI_FORMAT_BC7_UNORM_SRGB : DXGI_FORMAT_BC7_UNORM;
hdr10.dxgiFormat = DXGI_FORMAT_BC7_UNORM;
hdr10.resourceDimension = D3D10_RESOURCE_DIMENSION_TEXTURE2D;
hdr10.arraySize = 1;
fwrite(&hdr10, sizeof(hdr10), 1, pFile);
fwrite(pBlocks, desc.lPitch, 1, pFile);
if (fclose(pFile) == EOF)
{
fprintf(stderr, "Failed writing to DDS file %s!\n", pFilename);
return false;
}
return true;
}
static bool save_bc7_ktx(const char *pFilename,
uint32_t width, uint32_t height,
const bc7_block *pBlocks, bool srgb, bool has_alpha)
{
(void) has_alpha; // RGB without A is currently unsupported
FILE *pFile = NULL;
pFile = fopen(pFilename, "wb");
if (!pFile)
{
fprintf(stderr, "Failed creating file %s!\n", pFilename);
return false;
}
uint32_t keyValueSizeBrutto = 0;
//
// key/value pair length computation
//
// first key/value pair
uint32_t keyValueKtxOrientationSizeNetto = sizeof(ktxOrientation);
uint32_t keyValueKtxOrientationSizeBrutto = (keyValueKtxOrientationSizeNetto + 3) & ~3;
keyValueSizeBrutto += keyValueKtxOrientationSizeBrutto + 4 /* 4 is the size of the size field */;
// additional pairs
// ...
//
// header
//
struct KTX_HEADER header;
memcpy(header.identifier, ktxFileIdentifier, sizeof(header.identifier));
header.endianness = ktxEndianess;
header.glType = 0; // 0: compressed texture
header.glTypeSize = 1; // 1: endianess independent, especially for compressed textures
header.glFormat = 0; // 0: compressed texture
header.glInternalFormat = (srgb) ? GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM : GL_COMPRESSED_RGBA_BPTC_UNORM; // see table 8.14 and chapter C.2 of OpenGL 4.4. specification
// header.glBaseInternalFormat = (has_alpha) ? GL_RGBA : GL_RGB; // see table 8.11
header.glBaseInternalFormat = GL_RGBA; // see table 8.11
header.pixelWidth = width;
header.pixelHeight = height;
header.pixelDepth = 0;
header.numberOfArrayElements = 0; // numberOfArrayElements denotes the number of array elements which is the number of elements in the array or the size of the array measured in elements.
header.numberOfFaces = 1; // cubemap/s/arrays: 6, otherwise: 1
header.numberOfMipmapLevels = 1; // non-mipmapped: 1
header.bytesOfKeyValueData = keyValueSizeBrutto;
fwrite(&header, sizeof(header), 1, pFile);
//
// key/value pairs
//
const uint8_t pad[3] = {0 , 0 , 0};
// first key/value pair
fwrite(&keyValueKtxOrientationSizeNetto, sizeof(keyValueKtxOrientationSizeNetto), 1, pFile);
fwrite(ktxOrientation, sizeof(ktxOrientation), 1, pFile);
uint32_t numOfPad = keyValueKtxOrientationSizeBrutto - keyValueKtxOrientationSizeNetto;
fwrite(pad, 1, numOfPad, pFile);
// additional pairs
// ...
//
// image size
//
const uint32_t pixel_format_bpp = 8; // 8 bits per pixel (compressed to a quarter of that needed by RGBA)
const uint32_t imageSize = (((width + 3) & ~3) * ((height + 3) & ~3) * pixel_format_bpp) >> 3;
fwrite(&imageSize, sizeof(imageSize), 1, pFile);
//
// image data
//
fwrite(pBlocks, imageSize, 1, pFile);
if (fclose(pFile) == EOF)
{
fprintf(stderr, "Failed writing to KTX file %s!\n", pFilename);
return false;
}
return true;
}
static void strip_extension(std::string &s)
{
for (int32_t i = (int32_t)s.size() - 1; i >= 0; i--)
{
if (s[i] == '.')
{
s.resize(i);
break;
}
}
}
int main(int argc, char *argv[])
{
if (argc < 2)
return print_usage();
std::string src_filename;
std::string src_alpha_filename;
std::string dds_output_filename;
std::string png_output_filename;
std::string png_alpha_output_filename;
int uber_level = 0;
int max_partitions_to_scan = BC7ENC16_MAX_PARTITIONS1;
bool perceptual = true;
bool no_output_png = false;
bool y_flip = false;
bool ktx = false;
for (int i = 1; i < argc; i++)
{
const char *pArg = argv[i];
if (pArg[0] == '-')
{
switch (pArg[1])
{
case 'y':
{
y_flip = true;
break;
}
case 'a':
{
src_alpha_filename = pArg + 2;
break;
}
case 'u':
{
uber_level = atoi(pArg + 2);
if ((uber_level < 0) || (uber_level > BC7ENC16_MAX_UBER_LEVEL))
{
fprintf(stderr, "Invalid argument: %s\n", pArg);
return EXIT_FAILURE;
}
break;
}
case 'g':
{
no_output_png = true;
break;
}
case 'l':
{
perceptual = false;
break;
}
case 'p':
{
max_partitions_to_scan = atoi(pArg + 2);
if ((max_partitions_to_scan < 0) || (max_partitions_to_scan > BC7ENC16_MAX_PARTITIONS1))
{
fprintf(stderr, "Invalid argument: %s\n", pArg);
return EXIT_FAILURE;
}
break;
}
case 'k':
{
ktx = true;
break;
}
default:
{
fprintf(stderr, "Invalid argument: %s\n", pArg);
return EXIT_FAILURE;
}
}
}
else
{
if (!src_filename.size())
src_filename = pArg;
else if (!dds_output_filename.size())
dds_output_filename = pArg;
else if (!png_output_filename.size())
png_output_filename = pArg;
else
{
fprintf(stderr, "Invalid argument: %s\n", pArg);
return EXIT_FAILURE;
}
}
}
if (!src_filename.size())
{
fprintf(stderr, "No source filename specified!\n");
return EXIT_FAILURE;
}
if (!dds_output_filename.size())
{
dds_output_filename = src_filename;
strip_extension(dds_output_filename);
dds_output_filename += (ktx) ? ktxFileNameExt : ".dds";
}
if (!png_output_filename.size())
{
png_output_filename = src_filename;
strip_extension(png_output_filename);
png_output_filename += "_unpacked.png";
}
png_alpha_output_filename = png_output_filename;
strip_extension(png_alpha_output_filename);
png_alpha_output_filename += "_unpacked_alpha.png";
image_u8 source_image;
if (!load_png(src_filename.c_str(), source_image))
return EXIT_FAILURE;
printf("Source image: %s %ux%u\n", src_filename.c_str(), source_image.width(), source_image.height());
if (src_alpha_filename.size())
{
image_u8 source_alpha_image;
if (!load_png(src_alpha_filename.c_str(), source_alpha_image))
return EXIT_FAILURE;
printf("Source alpha image: %s %ux%u\n", src_alpha_filename.c_str(), source_alpha_image.width(), source_alpha_image.height());
const uint32_t w = std::min(source_alpha_image.width(), source_image.width());
const uint32_t h = std::min(source_alpha_image.height(), source_image.height());
for (uint32_t y = 0; y < h; y++)
for (uint32_t x = 0; x < w; x++)
source_image(x, y)[3] = source_alpha_image(x, y)[1];
}
const uint32_t orig_width = source_image.width();
const uint32_t orig_height = source_image.height();
if (y_flip)
{
image_u8 temp;
temp.init(orig_width, orig_height);
for (uint32_t y = 0; y < orig_height; y++)
for (uint32_t x = 0; x < orig_width; x++)
temp(x, (orig_height - 1) - y) = source_image(x, y);
temp.swap(source_image);
}
source_image.crop((source_image.width() + 3) & ~3, (source_image.height() + 3) & ~3);
const uint32_t blocks_x = source_image.width() / 4;
const uint32_t blocks_y = source_image.height() / 4;
bc7_block_vec packed_image(blocks_x * blocks_y);
bc7enc16_compress_block_params pack_params;
bc7enc16_compress_block_params_init(&pack_params);
if (!perceptual)
bc7enc16_compress_block_params_init_linear_weights(&pack_params);
pack_params.m_max_partitions_mode1 = max_partitions_to_scan;
pack_params.m_uber_level = uber_level;
printf("Max mode 1 partitions: %u, uber level: %u, perceptual: %u\n", pack_params.m_max_partitions_mode1, pack_params.m_uber_level, perceptual);
bc7enc16_compress_block_init();
bool has_alpha = false;
clock_t start_t = clock();
for (uint32_t by = 0; by < blocks_y; by++)
{
for (uint32_t bx = 0; bx < blocks_x; bx++)
{
color_quad_u8 pixels[16];
source_image.get_block(bx, by, 4, 4, pixels);
bc7_block *pBlock = &packed_image[bx + by * blocks_x];
if (bc7enc16_compress_block(pBlock, pixels, &pack_params))
has_alpha = true;
}
if ((by & 63) == 0)
{
printf(".");
fflush(stdout);
}
}
clock_t end_t = clock();
printf("\nTotal time: %f secs\n", (double)(end_t - start_t) / CLOCKS_PER_SEC);
if (has_alpha)
printf("Source image had an alpha channel.\n");
bool failed = false;
if (ktx)
{
if (!save_bc7_ktx(dds_output_filename.c_str(), orig_width, orig_height, &packed_image[0], perceptual, has_alpha))
failed = true;
else
printf("Wrote KTX file %s\n", dds_output_filename.c_str());
}
else
{
if (!save_bc7_dds(dds_output_filename.c_str(), orig_width, orig_height, &packed_image[0], perceptual))
failed = true;
else
printf("Wrote DDS file %s\n", dds_output_filename.c_str());
}
if ((!no_output_png) && (png_output_filename.size()))
{
image_u8 unpacked_image(source_image.width(), source_image.height());
for (uint32_t by = 0; by < blocks_y; by++)
{
for (uint32_t bx = 0; bx < blocks_x; bx++)
{
bc7_block *pBlock = &packed_image[bx + by * blocks_x];
color_quad_u8 unpacked_pixels[16];
detexDecompressBlockBPTC((const uint8_t *)pBlock, UINT32_MAX, 0, (uint8_t *)unpacked_pixels);
unpacked_image.set_block(bx, by, 4, 4, unpacked_pixels);
}
}
image_metrics y_metrics;
y_metrics.compute(source_image, unpacked_image, 0, 0);
printf("Luma Max error: %3.0f RMSE: %f PSNR %03.02f dB\n", y_metrics.m_max, y_metrics.m_root_mean_squared, y_metrics.m_peak_snr);
image_metrics rgb_metrics;
rgb_metrics.compute(source_image, unpacked_image, 0, 3);
printf("RGB Max error: %3.0f RMSE: %f PSNR %03.02f dB\n", rgb_metrics.m_max, rgb_metrics.m_root_mean_squared, rgb_metrics.m_peak_snr);
image_metrics rgba_metrics;
rgba_metrics.compute(source_image, unpacked_image, 0, 4);
printf("RGBA Max error: %3.0f RMSE: %f PSNR %03.02f dB\n", rgba_metrics.m_max, rgba_metrics.m_root_mean_squared, rgba_metrics.m_peak_snr);
image_metrics a_metrics;
a_metrics.compute(source_image, unpacked_image, 3, 1);
printf("Alpha Max error: %3.0f RMSE: %f PSNR %03.02f dB\n", a_metrics.m_max, a_metrics.m_root_mean_squared, a_metrics.m_peak_snr);
if (!save_png(png_output_filename.c_str(), unpacked_image, false))
failed = true;
else
printf("Wrote PNG file %s\n", png_output_filename.c_str());
//if ((png_alpha_output_filename.size()) && (has_alpha))
if (png_alpha_output_filename.size())
{
image_u8 unpacked_image_alpha(unpacked_image);
for (uint32_t y = 0; y < unpacked_image_alpha.height(); y++)
for (uint32_t x = 0; x < unpacked_image_alpha.width(); x++)
unpacked_image_alpha(x, y).set(unpacked_image_alpha(x, y)[3], 255);
if (!save_png(png_alpha_output_filename.c_str(), unpacked_image_alpha, false))
failed = true;
else
printf("Wrote PNG file %s\n", png_alpha_output_filename.c_str());
}
}
return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}