3434#include " ThirdParty/stb_image_write.h"
3535#include " ThirdParty/tinyexr.h"
3636
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+
3749/* *
3850 * @brief Determine the output file name to use for a sliced image write.
3951 *
@@ -656,18 +668,6 @@ static void switch_endianness4(
656668 }
657669}
658670
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-
671671/*
672672 Notes about KTX:
673673
@@ -904,7 +904,7 @@ static uint8_t ktx_magic[12] {
904904
905905static void ktx_header_switch_endianness (ktx_header * kt)
906906{
907- #define REV (x ) kt->x = u32_byterev (kt->x)
907+ #define REV (x ) kt->x = reverse_bytes_u32 (kt->x)
908908 REV (endianness);
909909 REV (gl_type);
910910 REV (gl_type_size);
@@ -961,11 +961,11 @@ static astcenc_image* load_ktx_uncompressed_image(
961961 return nullptr ;
962962 }
963963
964- int switch_endianness = 0 ;
964+ bool switch_endianness = false ;
965965 if (hdr.endianness == 0x01020304 )
966966 {
967967 ktx_header_switch_endianness (&hdr);
968- switch_endianness = 1 ;
968+ switch_endianness = true ;
969969 }
970970
971971 if (hdr.gl_type == 0 || hdr.gl_format == 0 )
@@ -1191,7 +1191,7 @@ static astcenc_image* load_ktx_uncompressed_image(
11911191
11921192 if (switch_endianness)
11931193 {
1194- specified_bytes_of_surface = u32_byterev (specified_bytes_of_surface);
1194+ specified_bytes_of_surface = reverse_bytes_u32 (specified_bytes_of_surface);
11951195 }
11961196
11971197 // read the surface
@@ -1346,7 +1346,7 @@ bool load_ktx_compressed_image(
13461346
13471347 if (switch_endianness)
13481348 {
1349- data_len = u32_byterev (data_len);
1349+ data_len = reverse_bytes_u32 (data_len);
13501350 }
13511351
13521352 // Read the data
@@ -1409,6 +1409,10 @@ bool store_ktx_compressed_image(
14091409 hdr.number_of_mipmap_levels = 1 ;
14101410 hdr.bytes_of_key_value_data = 0 ;
14111411
1412+ #if defined(ASTCENC_BIG_ENDIAN)
1413+ ktx_header_switch_endianness (&hdr);
1414+ #endif
1415+
14121416 size_t expected = sizeof (ktx_header) + 4 + img.data_len ;
14131417 size_t actual = 0 ;
14141418
@@ -1418,8 +1422,13 @@ bool store_ktx_compressed_image(
14181422 return true ;
14191423 }
14201424
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+
14211430 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);
14231432 actual += fwrite (img.data , 1 , img.data_len , wf);
14241433 fclose (wf);
14251434
@@ -1746,21 +1755,6 @@ struct dds_header_dx10
17461755#define DDS_MAGIC 0x20534444
17471756#define DX10_MAGIC 0x30315844
17481757
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-
17641758/* *
17651759 * @brief Load an uncompressed DDS image using the local custom loader.
17661760 *
0 commit comments