34
34
#include " ThirdParty/stb_image_write.h"
35
35
#include " ThirdParty/tinyexr.h"
36
36
37
+ /* *
38
+ * @brief Reverse the bytes in a uint32_t value.
39
+ */
40
+ static uint32_t reverse_bytes_u32 (
41
+ uint32_t val
42
+ ) {
43
+ return ((val >> 24 ) & 0x000000FF ) |
44
+ ((val >> 8 ) & 0x0000FF00 ) |
45
+ ((val << 8 ) & 0x00FF0000 ) |
46
+ ((val << 24 ) & 0xFF000000 );
47
+ }
48
+
37
49
/* *
38
50
* @brief Determine the output file name to use for a sliced image write.
39
51
*
@@ -656,18 +668,6 @@ static void switch_endianness4(
656
668
}
657
669
}
658
670
659
- /* *
660
- * @brief Swap endianness of a u32 value.
661
- *
662
- * @param v The data to convert.
663
- *
664
- * @return The converted value.
665
- */
666
- static uint32_t u32_byterev (uint32_t v)
667
- {
668
- return (v >> 24 ) | ((v >> 8 ) & 0xFF00 ) | ((v << 8 ) & 0xFF0000 ) | (v << 24 );
669
- }
670
-
671
671
/*
672
672
Notes about KTX:
673
673
@@ -904,7 +904,7 @@ static uint8_t ktx_magic[12] {
904
904
905
905
static void ktx_header_switch_endianness (ktx_header * kt)
906
906
{
907
- #define REV (x ) kt->x = u32_byterev (kt->x)
907
+ #define REV (x ) kt->x = reverse_bytes_u32 (kt->x)
908
908
REV (endianness);
909
909
REV (gl_type);
910
910
REV (gl_type_size);
@@ -961,11 +961,11 @@ static astcenc_image* load_ktx_uncompressed_image(
961
961
return nullptr ;
962
962
}
963
963
964
- int switch_endianness = 0 ;
964
+ bool switch_endianness = false ;
965
965
if (hdr.endianness == 0x01020304 )
966
966
{
967
967
ktx_header_switch_endianness (&hdr);
968
- switch_endianness = 1 ;
968
+ switch_endianness = true ;
969
969
}
970
970
971
971
if (hdr.gl_type == 0 || hdr.gl_format == 0 )
@@ -1191,7 +1191,7 @@ static astcenc_image* load_ktx_uncompressed_image(
1191
1191
1192
1192
if (switch_endianness)
1193
1193
{
1194
- specified_bytes_of_surface = u32_byterev (specified_bytes_of_surface);
1194
+ specified_bytes_of_surface = reverse_bytes_u32 (specified_bytes_of_surface);
1195
1195
}
1196
1196
1197
1197
// read the surface
@@ -1346,7 +1346,7 @@ bool load_ktx_compressed_image(
1346
1346
1347
1347
if (switch_endianness)
1348
1348
{
1349
- data_len = u32_byterev (data_len);
1349
+ data_len = reverse_bytes_u32 (data_len);
1350
1350
}
1351
1351
1352
1352
// Read the data
@@ -1409,6 +1409,10 @@ bool store_ktx_compressed_image(
1409
1409
hdr.number_of_mipmap_levels = 1 ;
1410
1410
hdr.bytes_of_key_value_data = 0 ;
1411
1411
1412
+ #if defined(ASTCENC_BIG_ENDIAN)
1413
+ ktx_header_switch_endianness (&hdr);
1414
+ #endif
1415
+
1412
1416
size_t expected = sizeof (ktx_header) + 4 + img.data_len ;
1413
1417
size_t actual = 0 ;
1414
1418
@@ -1418,8 +1422,13 @@ bool store_ktx_compressed_image(
1418
1422
return true ;
1419
1423
}
1420
1424
1425
+ uint32_t data_len = static_cast <uint32_t >(img.data_len );
1426
+ #if defined(ASTCENC_BIG_ENDIAN)
1427
+ data_len = reverse_bytes_u32 (data_len);
1428
+ #endif
1429
+
1421
1430
actual += fwrite (&hdr, 1 , sizeof (ktx_header), wf);
1422
- actual += fwrite (&img. data_len , 1 , 4 , wf);
1431
+ actual += fwrite (&data_len, 1 , sizeof ( uint32_t ) , wf);
1423
1432
actual += fwrite (img.data , 1 , img.data_len , wf);
1424
1433
fclose (wf);
1425
1434
@@ -1746,21 +1755,6 @@ struct dds_header_dx10
1746
1755
#define DDS_MAGIC 0x20534444
1747
1756
#define DX10_MAGIC 0x30315844
1748
1757
1749
-
1750
- #if defined(ASTCENC_BIG_ENDIAN)
1751
- /* *
1752
- * @brief Reverse the bytes in a uint32_t value.
1753
- */
1754
- static uint32_t reverse_bytes_u32 (
1755
- uint32_t val
1756
- ) {
1757
- return ((val >> 24 ) & 0x000000FF ) |
1758
- ((val >> 8 ) & 0x0000FF00 ) |
1759
- ((val << 8 ) & 0x00FF0000 ) |
1760
- ((val << 24 ) & 0xFF000000 );
1761
- }
1762
- #endif
1763
-
1764
1758
/* *
1765
1759
* @brief Load an uncompressed DDS image using the local custom loader.
1766
1760
*
0 commit comments