-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathastcenccli_image_load_store.cpp
More file actions
2750 lines (2473 loc) · 78.6 KB
/
Copy pathastcenccli_image_load_store.cpp
File metadata and controls
2750 lines (2473 loc) · 78.6 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
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
// ----------------------------------------------------------------------------
// Copyright 2011-2026 Arm Limited
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
// ----------------------------------------------------------------------------
/**
* @brief Functions for loading/storing uncompressed and compressed images.
*/
#include <array>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <memory>
#include <new>
#include <sstream>
#include <utility>
#include <vector>
#include "astcenccli_internal.h"
#include "ThirdParty/stb_image.h"
#include "ThirdParty/stb_image_write.h"
#include "ThirdParty/tinyexr.h"
/**
* @brief Reverse the bytes in a uint32_t value.
*/
static uint32_t reverse_bytes_u32(
uint32_t val
) {
return ((val >> 24) & 0x000000FF) |
((val >> 8) & 0x0000FF00) |
((val << 8) & 0x00FF0000) |
((val << 24) & 0xFF000000);
}
/**
* @brief Determine the output file name to use for a sliced image write.
*
* @param img The source data for the image.
* @param filename The base name of the file to save.
* @param index The slice index to write.
*
* @return The file name to use when saving the file.
*/
static std::string get_output_filename(
const astcenc_image* img,
const char* filename,
unsigned int index
) {
if (img->dim_z <= 1)
{
return filename;
}
std::string fnmod(filename);
std::string fnext = fnmod.substr(fnmod.find_last_of("."));
// Remove the extension
fnmod = fnmod.erase(fnmod.length() - fnext.size());
// Insert the file index into the base name, then append the extension
std::stringstream ss;
ss << fnmod << "_" << std::setw(3) << std::setfill('0') << index << fnext;
return ss.str();
}
/* ============================================================================
Image load and store through the stb_image and tinyexr libraries
============================================================================ */
struct tinyexr_image_deleter
{
void operator()(void* ptr) const
{
free(ptr);
}
};
struct tinyexr_error_deleter
{
void operator()(const void* ptr) const
{
free(const_cast<void*>(ptr));
}
};
struct stbi_image_deleter
{
void operator()(void* ptr) const
{
stbi_image_free(ptr);
}
};
/**
* @brief Load a .exr image using TinyExr to provide the loader.
*
* @param filename The name of the file to load.
* @param y_flip Should the image be vertically flipped?
* @param[out] is_hdr Is this an HDR image load? Always @c true for this function.
* @param[out] component_count The number of components in the data.
*
* @return The loaded image data in a canonical 4 channel format.
*/
static astcenc_image_ptr load_image_with_tinyexr(
const char* filename,
bool y_flip,
bool& is_hdr,
unsigned int& component_count
) {
int dim_x, dim_y;
float* image_raw;
const char* err { nullptr };
int load_res = LoadEXR(&image_raw, &dim_x, &dim_y, filename, &err);
if (load_res != TINYEXR_SUCCESS)
{
std::unique_ptr<const char, tinyexr_error_deleter> err_ptr { err };
print_error("ERROR: Image load failed '%s' (%s)\n",
filename, err_ptr.get() ? err_ptr.get() : "unknown error");
return nullptr;
}
std::unique_ptr<float, tinyexr_image_deleter> image { image_raw };
auto res_img = astc_img_from_floatx4_array(image.get(), dim_x, dim_y, y_flip);
is_hdr = true;
component_count = 4;
return res_img;
}
/**
* @brief Load an image using STBImage to provide the loader.
*
* @param filename The name of the file to load.
* @param y_flip Should the image be vertically flipped?
* @param[out] is_hdr Is this an HDR image load?
* @param[out] component_count The number of components in the data.
*
* @return The loaded image data in a canonical 4 channel format, or @c nullptr on error.
*/
static astcenc_image_ptr load_image_with_stb(
const char* filename,
bool y_flip,
bool& is_hdr,
unsigned int& component_count
) {
int dim_x, dim_y;
if (stbi_is_hdr(filename))
{
std::unique_ptr<float, stbi_image_deleter> data {
stbi_loadf(filename, &dim_x, &dim_y, nullptr, STBI_rgb_alpha)
};
if (data)
{
auto img = astc_img_from_floatx4_array(data.get(), dim_x, dim_y, y_flip);
is_hdr = true;
component_count = 4;
return img;
}
}
else
{
std::unique_ptr<uint8_t, stbi_image_deleter> data {
stbi_load(filename, &dim_x, &dim_y, nullptr, STBI_rgb_alpha)
};
if (data)
{
auto img = astc_img_from_unorm8x4_array(data.get(), dim_x, dim_y, y_flip);
is_hdr = false;
component_count = 4;
return img;
}
}
print_error("ERROR: Image load failed '%s' (%s)\n", filename, stbi_failure_reason());
return nullptr;
}
/**
* @brief Save an EXR image using TinyExr to provide the store routine.
*
* @param img The source data for the image.
* @param filename The name of the file to save.
* @param y_flip Should the image be vertically flipped?
*
* @return @c true if the image saved OK, @c false on error.
*/
static bool store_exr_image_with_tinyexr(
const astcenc_image* img,
const char* filename,
int y_flip
) {
int res { 0 };
for (unsigned int i = 0; i < img->dim_z; i++)
{
std::string fnmod = get_output_filename(img, filename, i);
std::vector<float> buf = floatx4_array_from_astc_img(img, y_flip, i);
res = SaveEXR(buf.data(), img->dim_x, img->dim_y, 4, 1, fnmod.c_str(), nullptr);
if (res < 0)
{
break;
}
}
return res >= 0;
}
/**
* @brief Save a PNG image using STBImageWrite to provide the store routine.
*
* @param img The source data for the image.
* @param filename The name of the file to save.
* @param y_flip Should the image be vertically flipped?
*
* @return @c true if the image saved OK, @c false on error.
*/
static bool store_png_image_with_stb(
const astcenc_image* img,
const char* filename,
int y_flip
) {
int res { 0 };
assert(img->data_type == ASTCENC_TYPE_U8);
for (unsigned int i = 0; i < img->dim_z; i++)
{
std::string fnmod = get_output_filename(img, filename, i);
uint8_t* buf = reinterpret_cast<uint8_t*>(img->data[i]);
stbi_flip_vertically_on_write(y_flip);
res = stbi_write_png(fnmod.c_str(), img->dim_x, img->dim_y, 4, buf, img->dim_x * 4);
if (res == 0)
{
break;
}
}
return res != 0;
}
/**
* @brief Save a TGA image using STBImageWrite to provide the store routine.
*
* @param img The source data for the image.
* @param filename The name of the file to save.
* @param y_flip Should the image be vertically flipped?
*
* @return @c true if the image saved OK, @c false on error.
*/
static bool store_tga_image_with_stb(
const astcenc_image* img,
const char* filename,
int y_flip
) {
int res { 0 };
assert(img->data_type == ASTCENC_TYPE_U8);
for (unsigned int i = 0; i < img->dim_z; i++)
{
std::string fnmod = get_output_filename(img, filename, i);
uint8_t* buf = reinterpret_cast<uint8_t*>(img->data[i]);
stbi_flip_vertically_on_write(y_flip);
res = stbi_write_tga(fnmod.c_str(), img->dim_x, img->dim_y, 4, buf);
if (res == 0)
{
break;
}
}
return res != 0;
}
/**
* @brief Save a BMP image using STBImageWrite to provide the store routine.
*
* @param img The source data for the image.
* @param filename The name of the file to save.
* @param y_flip Should the image be vertically flipped?
*
* @return @c true if the image saved OK, @c false on error.
*/
static bool store_bmp_image_with_stb(
const astcenc_image* img,
const char* filename,
int y_flip
) {
int res { 0 };
assert(img->data_type == ASTCENC_TYPE_U8);
for (unsigned int i = 0; i < img->dim_z; i++)
{
std::string fnmod = get_output_filename(img, filename, i);
uint8_t* buf = reinterpret_cast<uint8_t*>(img->data[i]);
stbi_flip_vertically_on_write(y_flip);
res = stbi_write_bmp(fnmod.c_str(), img->dim_x, img->dim_y, 4, buf);
if (res == 0)
{
break;
}
}
return res != 0;
}
/**
* @brief Save a HDR image using STBImageWrite to provide the store routine.
*
* @param img The source data for the image.
* @param filename The name of the file to save.
* @param y_flip Should the image be vertically flipped?
*
* @return @c true if the image saved OK, @c false on error.
*/
static bool store_hdr_image_with_stb(
const astcenc_image* img,
const char* filename,
int y_flip
) {
int res { 0 };
for (unsigned int i = 0; i < img->dim_z; i++)
{
std::string fnmod = get_output_filename(img, filename, i);
std::vector<float> buf = floatx4_array_from_astc_img(img, y_flip, i);
res = stbi_write_hdr(fnmod.c_str(), img->dim_x, img->dim_y, 4, buf.data());
if (res == 0)
{
break;
}
}
return res != 0;
}
/* ============================================================================
Native Load and store of KTX and DDS file formats.
Unlike "regular" 2D image formats, which are mostly supported through stb_image
and tinyexr, these formats are supported directly; this involves a relatively
large number of pixel formats.
The following restrictions apply to loading of these file formats:
* Only uncompressed data supported
* Only first mipmap in mipmap pyramid supported
* KTX: Cube-map arrays are not supported
============================================================================ */
enum scanline_transfer
{
R8_TO_RGBA8,
RG8_TO_RGBA8,
RGB8_TO_RGBA8,
RGBA8_TO_RGBA8,
BGR8_TO_RGBA8,
BGRA8_TO_RGBA8,
L8_TO_RGBA8,
LA8_TO_RGBA8,
RGBX8_TO_RGBA8,
BGRX8_TO_RGBA8,
R16_TO_RGBA16F,
RG16_TO_RGBA16F,
RGB16_TO_RGBA16F,
RGBA16_TO_RGBA16F,
BGR16_TO_RGBA16F,
BGRA16_TO_RGBA16F,
L16_TO_RGBA16F,
LA16_TO_RGBA16F,
R16F_TO_RGBA16F,
RG16F_TO_RGBA16F,
RGB16F_TO_RGBA16F,
RGBA16F_TO_RGBA16F,
BGR16F_TO_RGBA16F,
BGRA16F_TO_RGBA16F,
L16F_TO_RGBA16F,
LA16F_TO_RGBA16F,
R32F_TO_RGBA16F,
RG32F_TO_RGBA16F,
RGB32F_TO_RGBA16F,
RGBA32F_TO_RGBA16F,
BGR32F_TO_RGBA16F,
BGRA32F_TO_RGBA16F,
L32F_TO_RGBA16F,
LA32F_TO_RGBA16F
};
/**
* @brief Copy a scanline from a source file and expand to a canonical format.
*
* Outputs are always 4 component RGBA, stored as U8 (LDR) or FP16 (HDR).
*
* @param[out] dst The start of the line to store to.
* @param src The start of the line to load.
* @param pixel_count The number of pixels in the scanline.
* @param method The conversion function.
*/
static void copy_scanline(
void* dst,
const void* src,
size_t pixel_count,
scanline_transfer method
) {
#define id(x) (x)
#define u16_sf16(x) float_to_float16(x * (1.0f/65535.0f))
#define f32_sf16(x) float_to_float16(x)
#define COPY_R(dsttype, srctype, convfunc, oneval) \
do { \
const srctype* s = reinterpret_cast<const srctype*>(src); \
dsttype* d = reinterpret_cast<dsttype*>(dst); \
for (size_t i = 0; i < pixel_count; i++) \
{ \
d[4 * i ] = convfunc(s[i]); \
d[4 * i + 1] = 0; \
d[4 * i + 2] = 0; \
d[4 * i + 3] = oneval; \
} \
} while (0); \
break
#define COPY_RG(dsttype, srctype, convfunc, oneval) \
do { \
const srctype* s = reinterpret_cast<const srctype*>(src); \
dsttype* d = reinterpret_cast<dsttype*>(dst); \
for (size_t i = 0; i < pixel_count; i++) \
{ \
d[4 * i ] = convfunc(s[2 * i ]); \
d[4 * i + 1] = convfunc(s[2 * i + 1]); \
d[4 * i + 2] = 0; \
d[4 * i + 3] = oneval; \
} \
} while (0); \
break
#define COPY_RGB(dsttype, srctype, convfunc, oneval) \
do { \
const srctype* s = reinterpret_cast<const srctype*>(src); \
dsttype* d = reinterpret_cast<dsttype*>(dst); \
for (size_t i = 0; i < pixel_count; i++) \
{ \
d[4 * i ] = convfunc(s[3 * i ]); \
d[4 * i + 1] = convfunc(s[3 * i + 1]); \
d[4 * i + 2] = convfunc(s[3 * i + 2]); \
d[4 * i + 3] = oneval; \
} \
} while (0); \
break
#define COPY_BGR(dsttype, srctype, convfunc, oneval) \
do { \
const srctype* s = reinterpret_cast<const srctype*>(src); \
dsttype* d = reinterpret_cast<dsttype*>(dst); \
for (size_t i = 0; i < pixel_count; i++)\
{ \
d[4 * i ] = convfunc(s[3 * i + 2]); \
d[4 * i + 1] = convfunc(s[3 * i + 1]); \
d[4 * i + 2] = convfunc(s[3 * i ]); \
d[4 * i + 3] = oneval; \
} \
} while (0); \
break
#define COPY_RGBX(dsttype, srctype, convfunc, oneval) \
do { \
const srctype* s = reinterpret_cast<const srctype*>(src); \
dsttype* d = reinterpret_cast<dsttype*>(dst); \
for (size_t i = 0; i < pixel_count; i++)\
{ \
d[4 * i ] = convfunc(s[4 * i ]); \
d[4 * i + 1] = convfunc(s[4 * i + 1]); \
d[4 * i + 2] = convfunc(s[4 * i + 2]); \
d[4 * i + 3] = oneval; \
} \
} while (0); \
break
#define COPY_BGRX(dsttype, srctype, convfunc, oneval) \
do { \
const srctype* s = reinterpret_cast<const srctype*>(src); \
dsttype* d = reinterpret_cast<dsttype*>(dst); \
for (size_t i = 0; i < pixel_count; i++)\
{ \
d[4 * i ] = convfunc(s[4 * i + 2]); \
d[4 * i + 1] = convfunc(s[4 * i + 1]); \
d[4 * i + 2] = convfunc(s[4 * i ]); \
d[4 * i + 3] = oneval; \
} \
} while (0); \
break
#define COPY_RGBA(dsttype, srctype, convfunc, oneval) \
do { \
const srctype* s = reinterpret_cast<const srctype*>(src); \
dsttype* d = reinterpret_cast<dsttype*>(dst); \
for (size_t i = 0; i < pixel_count; i++) \
{ \
d[4 * i ] = convfunc(s[4 * i ]); \
d[4 * i + 1] = convfunc(s[4 * i + 1]); \
d[4 * i + 2] = convfunc(s[4 * i + 2]); \
d[4 * i + 3] = convfunc(s[4 * i + 3]); \
} \
} while (0); \
break
#define COPY_BGRA(dsttype, srctype, convfunc, oneval) \
do { \
const srctype* s = reinterpret_cast<const srctype*>(src); \
dsttype* d = reinterpret_cast<dsttype*>(dst); \
for (size_t i = 0; i < pixel_count; i++) \
{ \
d[4 * i ] = convfunc(s[4 * i + 2]); \
d[4 * i + 1] = convfunc(s[4 * i + 1]); \
d[4 * i + 2] = convfunc(s[4 * i ]); \
d[4 * i + 3] = convfunc(s[4 * i + 3]); \
} \
} while (0); \
break
#define COPY_L(dsttype, srctype, convfunc, oneval) \
do { \
const srctype* s = reinterpret_cast<const srctype*>(src); \
dsttype* d = reinterpret_cast<dsttype*>(dst); \
for (size_t i = 0; i < pixel_count; i++) \
{ \
d[4 * i ] = convfunc(s[i]); \
d[4 * i + 1] = convfunc(s[i]); \
d[4 * i + 2] = convfunc(s[i]); \
d[4 * i + 3] = oneval; \
} \
} while (0); \
break
#define COPY_LA(dsttype, srctype, convfunc, oneval) \
do { \
const srctype* s = reinterpret_cast<const srctype*>(src); \
dsttype* d = reinterpret_cast<dsttype*>(dst); \
for (size_t i = 0; i < pixel_count; i++) \
{ \
d[4 * i ] = convfunc(s[2 * i ]); \
d[4 * i + 1] = convfunc(s[2 * i ]); \
d[4 * i + 2] = convfunc(s[2 * i ]); \
d[4 * i + 3] = convfunc(s[2 * i + 1]); \
} \
} while (0); \
break
switch (method)
{
case R8_TO_RGBA8:
COPY_R(uint8_t, uint8_t, id, 0xFF);
case RG8_TO_RGBA8:
COPY_RG(uint8_t, uint8_t, id, 0xFF);
case RGB8_TO_RGBA8:
COPY_RGB(uint8_t, uint8_t, id, 0xFF);
case RGBA8_TO_RGBA8:
COPY_RGBA(uint8_t, uint8_t, id, 0xFF);
case BGR8_TO_RGBA8:
COPY_BGR(uint8_t, uint8_t, id, 0xFF);
case BGRA8_TO_RGBA8:
COPY_BGRA(uint8_t, uint8_t, id, 0xFF);
case RGBX8_TO_RGBA8:
COPY_RGBX(uint8_t, uint8_t, id, 0xFF);
case BGRX8_TO_RGBA8:
COPY_BGRX(uint8_t, uint8_t, id, 0xFF);
case L8_TO_RGBA8:
COPY_L(uint8_t, uint8_t, id, 0xFF);
case LA8_TO_RGBA8:
COPY_LA(uint8_t, uint8_t, id, 0xFF);
case R16F_TO_RGBA16F:
COPY_R(uint16_t, uint16_t, id, 0x3C00);
case RG16F_TO_RGBA16F:
COPY_RG(uint16_t, uint16_t, id, 0x3C00);
case RGB16F_TO_RGBA16F:
COPY_RGB(uint16_t, uint16_t, id, 0x3C00);
case RGBA16F_TO_RGBA16F:
COPY_RGBA(uint16_t, uint16_t, id, 0x3C00);
case BGR16F_TO_RGBA16F:
COPY_BGR(uint16_t, uint16_t, id, 0x3C00);
case BGRA16F_TO_RGBA16F:
COPY_BGRA(uint16_t, uint16_t, id, 0x3C00);
case L16F_TO_RGBA16F:
COPY_L(uint16_t, uint16_t, id, 0x3C00);
case LA16F_TO_RGBA16F:
COPY_LA(uint16_t, uint16_t, id, 0x3C00);
case R16_TO_RGBA16F:
COPY_R(uint16_t, uint16_t, u16_sf16, 0x3C00);
case RG16_TO_RGBA16F:
COPY_RG(uint16_t, uint16_t, u16_sf16, 0x3C00);
case RGB16_TO_RGBA16F:
COPY_RGB(uint16_t, uint16_t, u16_sf16, 0x3C00);
case RGBA16_TO_RGBA16F:
COPY_RGBA(uint16_t, uint16_t, u16_sf16, 0x3C00);
case BGR16_TO_RGBA16F:
COPY_BGR(uint16_t, uint16_t, u16_sf16, 0x3C00);
case BGRA16_TO_RGBA16F:
COPY_BGRA(uint16_t, uint16_t, u16_sf16, 0x3C00);
case L16_TO_RGBA16F:
COPY_L(uint16_t, uint16_t, u16_sf16, 0x3C00);
case LA16_TO_RGBA16F:
COPY_LA(uint16_t, uint16_t, u16_sf16, 0x3C00);
case R32F_TO_RGBA16F:
COPY_R(uint16_t, float, f32_sf16, 0x3C00);
case RG32F_TO_RGBA16F:
COPY_RG(uint16_t, float, f32_sf16, 0x3C00);
case RGB32F_TO_RGBA16F:
COPY_RGB(uint16_t, float, f32_sf16, 0x3C00);
case RGBA32F_TO_RGBA16F:
COPY_RGBA(uint16_t, float, f32_sf16, 0x3C00);
case BGR32F_TO_RGBA16F:
COPY_BGR(uint16_t, float, f32_sf16, 0x3C00);
case BGRA32F_TO_RGBA16F:
COPY_BGRA(uint16_t, float, f32_sf16, 0x3C00);
case L32F_TO_RGBA16F:
COPY_L(uint16_t, float, f32_sf16, 0x3C00);
case LA32F_TO_RGBA16F:
COPY_LA(uint16_t, float, f32_sf16, 0x3C00);
}
}
/**
* @brief Swap endianness of N two byte values.
*
* @param[in,out] dataptr The data to convert.
* @param byte_count The number of bytes to convert.
*/
static void switch_endianness2(
void* dataptr,
size_t byte_count
) {
uint8_t* data = reinterpret_cast<uint8_t*>(dataptr);
for (size_t i = 0; i < byte_count / 2; i++)
{
uint8_t d0 = data[0];
uint8_t d1 = data[1];
data[0] = d1;
data[1] = d0;
data += 2;
}
}
/**
* @brief Swap endianness of N four byte values.
*
* @param[in,out] dataptr The data to convert.
* @param byte_count The number of bytes to convert.
*/
static void switch_endianness4(
void* dataptr,
size_t byte_count
) {
uint8_t* data = reinterpret_cast<uint8_t*>(dataptr);
for (size_t i = 0; i < byte_count / 4; i++)
{
uint8_t d0 = data[0];
uint8_t d1 = data[1];
uint8_t d2 = data[2];
uint8_t d3 = data[3];
data[0] = d3;
data[1] = d2;
data[2] = d1;
data[3] = d0;
data += 4;
}
}
// Khronos enums
#define GL_RED 0x1903
#define GL_RG 0x8227
#define GL_RGB 0x1907
#define GL_RGBA 0x1908
#define GL_BGR 0x80E0
#define GL_BGRA 0x80E1
#define GL_LUMINANCE 0x1909
#define GL_LUMINANCE_ALPHA 0x190A
#define GL_R8 0x8229
#define GL_RG8 0x822B
#define GL_RGB8 0x8051
#define GL_RGBA8 0x8058
#define GL_R16F 0x822D
#define GL_RG16F 0x822F
#define GL_RGB16F 0x881B
#define GL_RGBA16F 0x881A
#define GL_UNSIGNED_BYTE 0x1401
#define GL_UNSIGNED_SHORT 0x1403
#define GL_HALF_FLOAT 0x140B
#define GL_FLOAT 0x1406
#define GL_COMPRESSED_RGBA_ASTC_4x4 0x93B0
#define GL_COMPRESSED_RGBA_ASTC_5x4 0x93B1
#define GL_COMPRESSED_RGBA_ASTC_5x5 0x93B2
#define GL_COMPRESSED_RGBA_ASTC_6x5 0x93B3
#define GL_COMPRESSED_RGBA_ASTC_6x6 0x93B4
#define GL_COMPRESSED_RGBA_ASTC_8x5 0x93B5
#define GL_COMPRESSED_RGBA_ASTC_8x6 0x93B6
#define GL_COMPRESSED_RGBA_ASTC_8x8 0x93B7
#define GL_COMPRESSED_RGBA_ASTC_10x5 0x93B8
#define GL_COMPRESSED_RGBA_ASTC_10x6 0x93B9
#define GL_COMPRESSED_RGBA_ASTC_10x8 0x93BA
#define GL_COMPRESSED_RGBA_ASTC_10x10 0x93BB
#define GL_COMPRESSED_RGBA_ASTC_12x10 0x93BC
#define GL_COMPRESSED_RGBA_ASTC_12x12 0x93BD
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4 0x93D0
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4 0x93D1
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5 0x93D2
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5 0x93D3
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6 0x93D4
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5 0x93D5
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6 0x93D6
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8 0x93D7
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5 0x93D8
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6 0x93D9
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8 0x93DA
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10 0x93DB
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10 0x93DC
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12 0x93DD
#define GL_COMPRESSED_RGBA_ASTC_3x3x3_OES 0x93C0
#define GL_COMPRESSED_RGBA_ASTC_4x3x3_OES 0x93C1
#define GL_COMPRESSED_RGBA_ASTC_4x4x3_OES 0x93C2
#define GL_COMPRESSED_RGBA_ASTC_4x4x4_OES 0x93C3
#define GL_COMPRESSED_RGBA_ASTC_5x4x4_OES 0x93C4
#define GL_COMPRESSED_RGBA_ASTC_5x5x4_OES 0x93C5
#define GL_COMPRESSED_RGBA_ASTC_5x5x5_OES 0x93C6
#define GL_COMPRESSED_RGBA_ASTC_6x5x5_OES 0x93C7
#define GL_COMPRESSED_RGBA_ASTC_6x6x5_OES 0x93C8
#define GL_COMPRESSED_RGBA_ASTC_6x6x6_OES 0x93C9
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES 0x93E0
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES 0x93E1
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES 0x93E2
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES 0x93E3
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES 0x93E4
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES 0x93E5
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES 0x93E6
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES 0x93E7
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES 0x93E8
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES 0x93E9
struct format_entry
{
unsigned int x;
unsigned int y;
unsigned int z;
bool is_srgb;
unsigned int format;
};
static const std::array<format_entry, 48> ASTC_FORMATS =
{{
// 2D Linear RGB
{ 4, 4, 1, false, GL_COMPRESSED_RGBA_ASTC_4x4},
{ 5, 4, 1, false, GL_COMPRESSED_RGBA_ASTC_5x4},
{ 5, 5, 1, false, GL_COMPRESSED_RGBA_ASTC_5x5},
{ 6, 5, 1, false, GL_COMPRESSED_RGBA_ASTC_6x5},
{ 6, 6, 1, false, GL_COMPRESSED_RGBA_ASTC_6x6},
{ 8, 5, 1, false, GL_COMPRESSED_RGBA_ASTC_8x5},
{ 8, 6, 1, false, GL_COMPRESSED_RGBA_ASTC_8x6},
{ 8, 8, 1, false, GL_COMPRESSED_RGBA_ASTC_8x8},
{10, 5, 1, false, GL_COMPRESSED_RGBA_ASTC_10x5},
{10, 6, 1, false, GL_COMPRESSED_RGBA_ASTC_10x6},
{10, 8, 1, false, GL_COMPRESSED_RGBA_ASTC_10x8},
{10, 10, 1, false, GL_COMPRESSED_RGBA_ASTC_10x10},
{12, 10, 1, false, GL_COMPRESSED_RGBA_ASTC_12x10},
{12, 12, 1, false, GL_COMPRESSED_RGBA_ASTC_12x12},
// 2D SRGB
{ 4, 4, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4},
{ 5, 4, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4},
{ 5, 5, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5},
{ 6, 5, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5},
{ 6, 6, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6},
{ 8, 5, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5},
{ 8, 6, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6},
{ 8, 8, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8},
{10, 5, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5},
{10, 6, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6},
{10, 8, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8},
{10, 10, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10},
{12, 10, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10},
{12, 12, 1, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12},
// 3D Linear RGB
{ 3, 3, 3, false, GL_COMPRESSED_RGBA_ASTC_3x3x3_OES},
{ 4, 3, 3, false, GL_COMPRESSED_RGBA_ASTC_4x3x3_OES},
{ 4, 4, 3, false, GL_COMPRESSED_RGBA_ASTC_4x4x3_OES},
{ 4, 4, 4, false, GL_COMPRESSED_RGBA_ASTC_4x4x4_OES},
{ 5, 4, 4, false, GL_COMPRESSED_RGBA_ASTC_5x4x4_OES},
{ 5, 5, 4, false, GL_COMPRESSED_RGBA_ASTC_5x5x4_OES},
{ 5, 5, 5, false, GL_COMPRESSED_RGBA_ASTC_5x5x5_OES},
{ 6, 5, 5, false, GL_COMPRESSED_RGBA_ASTC_6x5x5_OES},
{ 6, 6, 5, false, GL_COMPRESSED_RGBA_ASTC_6x6x5_OES},
{ 6, 6, 6, false, GL_COMPRESSED_RGBA_ASTC_6x6x6_OES},
// 3D SRGB
{ 3, 3, 3, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES},
{ 4, 3, 3, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES},
{ 4, 4, 3, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES},
{ 4, 4, 4, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES},
{ 5, 4, 4, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES},
{ 5, 5, 4, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES},
{ 5, 5, 5, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES},
{ 6, 5, 5, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES},
{ 6, 6, 5, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES},
{ 6, 6, 6, true, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES}
}};
static const format_entry* get_format(
unsigned int format
) {
for (auto& it : ASTC_FORMATS)
{
if (it.format == format)
{
return ⁢
}
}
return nullptr;
}
static unsigned int get_format(
unsigned int x,
unsigned int y,
unsigned int z,
bool is_srgb
) {
for (auto& it : ASTC_FORMATS)
{
if ((it.x == x) && (it.y == y) && (it.z == z) && (it.is_srgb == is_srgb))
{
return it.format;
}
}
return 0;
}
struct ktx_header
{
uint8_t magic[12];
uint32_t endianness; // should be 0x04030201; if it is instead 0x01020304, then the endianness of everything must be switched.
uint32_t gl_type; // 0 for compressed textures, otherwise value from table 3.2 (page 162) of OpenGL 4.0 spec
uint32_t gl_type_size; // size of data elements to do endianness swap on (1=endian-neutral data)
uint32_t gl_format; // 0 for compressed textures, otherwise value from table 3.3 (page 163) of OpenGL spec
uint32_t gl_internal_format; // sized-internal-format, corresponding to table 3.12 to 3.14 (pages 182-185) of OpenGL spec
uint32_t gl_base_internal_format; // unsized-internal-format: corresponding to table 3.11 (page 179) of OpenGL spec
uint32_t pixel_width; // texture dimensions; not rounded up to block size for compressed.
uint32_t pixel_height; // must be 0 for 1D textures.
uint32_t pixel_depth; // must be 0 for 1D, 2D and cubemap textures.
uint32_t number_of_array_elements; // 0 if not a texture array
uint32_t number_of_faces; // 6 for cubemaps, 1 for non-cubemaps
uint32_t number_of_mipmap_levels; // 0 or 1 for non-mipmapped textures; 0 indicates that auto-mipmap-gen should be done at load time.
uint32_t bytes_of_key_value_data; // size in bytes of the key-and-value area immediately following the header.
};
// Magic 12-byte sequence that must appear at the beginning of every KTX file.
static uint8_t ktx_magic[12] {
0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
};
static void ktx_header_switch_endianness(ktx_header * kt)
{
#define REV(x) kt->x = reverse_bytes_u32(kt->x)
REV(endianness);
REV(gl_type);
REV(gl_type_size);
REV(gl_format);
REV(gl_internal_format);
REV(gl_base_internal_format);
REV(pixel_width);
REV(pixel_height);
REV(pixel_depth);
REV(number_of_array_elements);
REV(number_of_faces);
REV(number_of_mipmap_levels);
REV(bytes_of_key_value_data);
#undef REV
}
/**
* @brief Load an uncompressed KTX image using the local custom loader.
*
* @param filename The name of the file to load.
* @param y_flip Should the image be vertically flipped?
* @param[out] is_hdr Is this an HDR image load?
* @param[out] component_count The number of components in the data.
*
* @return The loaded image data in a canonical 4 channel format, or @c nullptr on error.
*/
static astcenc_image_ptr load_ktx_uncompressed_image(
const char* filename,
bool y_flip,
bool& is_hdr,
unsigned int& component_count
) {
std::ifstream file(filename, std::ios::in | std::ios::binary);
if (!file)
{
print_error("ERROR: File open failed '%s'\n", filename);
return nullptr;
}
ktx_header hdr;
file.read(reinterpret_cast<char*>(&hdr), sizeof(hdr));
if (file.fail())
{
print_error("ERROR: File read failed '%s'\n", filename);
return nullptr;
}
if (memcmp(hdr.magic, ktx_magic, 12) != 0 || (hdr.endianness != 0x04030201 && hdr.endianness != 0x01020304))
{
print_error("ERROR: Image header corrupt '%s'\n", filename);
return nullptr;
}
bool switch_endianness = false;
if (hdr.endianness == 0x01020304)
{
ktx_header_switch_endianness(&hdr);
switch_endianness = true;
}
if (hdr.gl_type == 0 || hdr.gl_format == 0)
{
print_error("ERROR: Image uses unsupported KTX format '%s'\n", filename);
return nullptr;
}
// Supported formats are all pairings of:
// type=(UNSIGNED_BYTE, UNSIGNED_SHORT, HALF_FLOAT, FLOAT)
// gl_format=(RED, RG, RGB, RGBA, BGR, BGRA)
unsigned int components;
switch (hdr.gl_format)
{
case GL_RED:
components = 1;
break;
case GL_RG:
components = 2;
break;
case GL_RGB:
components = 3;
break;
case GL_RGBA:
components = 4;
break;
case GL_BGR:
components = 3;
break;
case GL_BGRA:
components = 4;
break;
case GL_LUMINANCE:
components = 1;
break;
case GL_LUMINANCE_ALPHA:
components = 2;
break;
default:
print_error("ERROR: Image uses unsupported KTX format '%s'\n", filename);
return nullptr;
}
// Although these are set up later, use default initializer to remove warnings
unsigned int bitness = 8; // Internal precision after conversion