3232#include " tgautils/tgautils.h"
3333#include " tracing/Monitor.h"
3434#include " tracing/tracing.h"
35+ #include " ktxutils/ktxutils.h"
3536
3637#include < cctype>
3738#include < climits>
@@ -59,8 +60,8 @@ size_t bm_texture_ram = 0;
5960int Bm_paging = 0 ;
6061
6162// Extension type lists
62- const BM_TYPE bm_type_list[] = { BM_TYPE_DDS , BM_TYPE_TGA , BM_TYPE_PNG , BM_TYPE_JPG , BM_TYPE_PCX };
63- const char * bm_ext_list[] = { " .dds" , " .tga" , " .png" , " .jpg" , " .pcx" };
63+ const BM_TYPE bm_type_list[] = { BM_TYPE_DDS , BM_TYPE_TGA , BM_TYPE_PNG , BM_TYPE_JPG , BM_TYPE_PCX , BM_TYPE_KTX };
64+ const char * bm_ext_list[] = { " .dds" , " .tga" , " .png" , " .jpg" , " .pcx" , " .ktx " };
6465const int BM_NUM_TYPES = sizeof (bm_type_list) / sizeof (bm_type_list[0 ]);
6566
6667const BM_TYPE bm_ani_type_list[] = { BM_TYPE_EFF , BM_TYPE_ANI , BM_TYPE_PNG };
@@ -205,7 +206,7 @@ void clear_bm_lookup_cache() {
205206/* *
206207 * Converts the bitmap referenced by bmp to the type specified by flags
207208 */
208- static void bm_convert_format (bitmap *bmp, ushort flags);
209+ static void bm_convert_format (bitmap *bmp, uint flags);
209210
210211/* *
211212 * Frees a bitmap's data if it can
@@ -639,7 +640,7 @@ int bm_create_3d(int bpp, int w, int h, int d, void* data) {
639640 return n;
640641}
641642
642- void bm_convert_format (bitmap *bmp, ushort flags) {
643+ void bm_convert_format (bitmap *bmp, uint flags) {
643644 int idx;
644645
645646 // no transparency for 24 bpp images
@@ -901,7 +902,7 @@ void bm_get_frame_usage(int *ntotal, int *nnew) {
901902#endif
902903}
903904
904- int bm_get_info (int handle, int *w, int * h, ushort * flags, int *nframes, int *fps) {
905+ int bm_get_info (int handle, int *w, int * h, uint * flags, int *nframes, int *fps) {
905906 bitmap * bmp;
906907
907908 if (!bm_inited) return -1 ;
@@ -1055,6 +1056,24 @@ int bm_is_compressed(int num) {
10551056 case BM_TYPE_CUBEMAP_DXT5 :
10561057 return DDS_CUBEMAP_DXT5 ;
10571058
1059+ case BM_TYPE_ETC2_RGB :
1060+ return KTX_ETC2_RGB ;
1061+
1062+ case BM_TYPE_ETC2_SRGB :
1063+ return KTX_ETC2_SRGB ;
1064+
1065+ case BM_TYPE_ETC2_RGBA_EAC :
1066+ return KTX_ETC2_RGBA_EAC ;
1067+
1068+ case BM_TYPE_ETC2_SRGBA_EAC :
1069+ return KTX_ETC2_SRGBA_EAC ;
1070+
1071+ case BM_TYPE_ETC2_RGBA1 :
1072+ return KTX_ETC2_RGB_A1 ;
1073+
1074+ case BM_TYPE_ETC2_SRGBA1 :
1075+ return KTX_ETC2_SRGB_A1 ;
1076+
10581077 default :
10591078 return 0 ;
10601079 }
@@ -1148,6 +1167,16 @@ static int bm_load_info(BM_TYPE type, const char *filename, CFILE *img_cfp, int
11481167 return -1 ;
11491168 }
11501169 }
1170+ // its a KTX file
1171+ else if (type == BM_TYPE_KTX ) {
1172+ int ktx_ct;
1173+ int ktx_error = ktx1_read_header (filename, img_cfp, w, h, bpp, &ktx_ct, mm_lvl, size);
1174+ if (ktx_error != KTX1_ERROR_NONE ) {
1175+ mprintf ((" ktx: could not open '%s'\n " , filename));
1176+ return -1 ;
1177+ }
1178+ *c_type = (BM_TYPE )ktx_ct;
1179+ }
11511180 // if its a tga file
11521181 else if (type == BM_TYPE_TGA ) {
11531182 int tga_error = targa_read_header (filename, img_cfp, w, h, bpp, NULL );
@@ -1361,6 +1390,8 @@ bool bm_load_and_parse_eff(const char *filename, int dir_type, int *nframes, int
13611390 c_type = BM_TYPE_JPG ;
13621391 } else if (!stricmp (NOX (" pcx" ), ext)) {
13631392 c_type = BM_TYPE_PCX ;
1393+ } else if (!stricmp (NOX (" ktx" ), ext)) {
1394+ c_type = BM_TYPE_KTX ;
13641395 } else {
13651396 mprintf ((" BMPMAN: Unknown file type in EFF parse!\n " ));
13661397 return false ;
@@ -1390,7 +1421,7 @@ bool bm_load_and_parse_eff(const char *filename, int dir_type, int *nframes, int
13901421/* *
13911422* Lock an image files data into memory
13921423*/
1393- static int bm_load_image_data (int handle, int bpp, ushort flags, bool nodebug)
1424+ static int bm_load_image_data (int handle, int bpp, uint flags, bool nodebug)
13941425{
13951426 BM_TYPE c_type = BM_TYPE_NONE ;
13961427 int true_bpp;
@@ -1482,6 +1513,10 @@ static int bm_load_image_data(int handle, int bpp, ushort flags, bool nodebug)
14821513 bm_lock_user (handle, bs, bmp, true_bpp, flags, false );
14831514 break ;
14841515
1516+ case BM_TYPE_KTX :
1517+ bm_lock_ktx1 (handle, bs, bmp, true_bpp, flags);
1518+ break ;
1519+
14851520 default :
14861521 Warning (LOCATION , " Unsupported type in bm_lock -- %d\n " , c_type);
14871522 return -1 ;
@@ -1941,7 +1976,7 @@ int bm_load_sub_slow(const char *real_filename, const int num_ext, const char **
19411976 return -1 ;
19421977}
19431978
1944- bitmap * bm_lock (int handle, int bpp, ushort flags, bool nodebug) {
1979+ bitmap * bm_lock (int handle, int bpp, uint flags, bool nodebug) {
19451980 bitmap *bmp;
19461981
19471982 Assertion (bm_inited, " bmpman must be initialized before this function can be called!" );
@@ -2038,7 +2073,7 @@ bitmap * bm_lock(int handle, int bpp, ushort flags, bool nodebug) {
20382073 return bmp;
20392074}
20402075
2041- void bm_lock_ani (int /* handle*/ , bitmap_slot *bs, bitmap* /* bmp*/ , int bpp, ushort flags) {
2076+ void bm_lock_ani (int /* handle*/ , bitmap_slot *bs, bitmap* /* bmp*/ , int bpp, uint flags) {
20422077 anim *the_anim;
20432078 anim_instance *the_anim_instance;
20442079 bitmap *bm;
@@ -2164,7 +2199,7 @@ void bm_lock_ani(int /*handle*/, bitmap_slot *bs, bitmap* /*bmp*/, int bpp, usho
21642199}
21652200
21662201
2167- void bm_lock_apng (int /* handle*/ , bitmap_slot *bs, bitmap *bmp, int bpp, ushort /* flags*/ ) {
2202+ void bm_lock_apng (int /* handle*/ , bitmap_slot *bs, bitmap *bmp, int bpp, uint /* flags*/ ) {
21682203 auto be = &bs->entry ;
21692204 int first_frame = be->info .ani .first_frame ;
21702205 auto first_entry = bm_get_entry (first_frame);
@@ -2214,7 +2249,7 @@ void bm_lock_apng(int /*handle*/, bitmap_slot *bs, bitmap *bmp, int bpp, ushort
22142249}
22152250
22162251
2217- void bm_lock_dds (int handle, bitmap_slot *bs, bitmap *bmp, int /* bpp*/ , ushort /* flags*/ ) {
2252+ void bm_lock_dds (int handle, bitmap_slot *bs, bitmap *bmp, int /* bpp*/ , uint /* flags*/ ) {
22182253 ubyte *data = NULL ;
22192254 int error;
22202255 ubyte dds_bpp = 0 ;
@@ -2278,7 +2313,7 @@ void bm_lock_dds(int handle, bitmap_slot *bs, bitmap *bmp, int /*bpp*/, ushort /
22782313#endif
22792314}
22802315
2281- void bm_lock_jpg (int handle, bitmap_slot *bs, bitmap *bmp, int bpp, ushort /* flags*/ ) {
2316+ void bm_lock_jpg (int handle, bitmap_slot *bs, bitmap *bmp, int bpp, uint /* flags*/ ) {
22822317 ubyte *data = NULL ;
22832318 int d_size = 0 ;
22842319 int jpg_error = JPEG_ERROR_INVALID ;
@@ -2325,7 +2360,7 @@ void bm_lock_jpg(int handle, bitmap_slot *bs, bitmap *bmp, int bpp, ushort /*fla
23252360#endif
23262361}
23272362
2328- void bm_lock_pcx (int handle, bitmap_slot *bs, bitmap *bmp, int bpp, ushort flags) {
2363+ void bm_lock_pcx (int handle, bitmap_slot *bs, bitmap *bmp, int bpp, uint flags) {
23292364 ubyte *data;
23302365 int pcx_error;
23312366 char filename[MAX_FILENAME_LEN ];
@@ -2370,7 +2405,7 @@ void bm_lock_pcx(int handle, bitmap_slot *bs, bitmap *bmp, int bpp, ushort flags
23702405 bm_convert_format (bmp, flags);
23712406}
23722407
2373- void bm_lock_png (int handle, bitmap_slot *bs, bitmap *bmp, int /* bpp*/ , ushort /* flags*/ ) {
2408+ void bm_lock_png (int handle, bitmap_slot *bs, bitmap *bmp, int /* bpp*/ , uint /* flags*/ ) {
23742409 ubyte *data = NULL ;
23752410 // assume 32 bit - libpng should expand everything
23762411 int d_size;
@@ -2415,7 +2450,7 @@ void bm_lock_png(int handle, bitmap_slot *bs, bitmap *bmp, int /*bpp*/, ushort /
24152450#endif
24162451}
24172452
2418- void bm_lock_tga (int handle, bitmap_slot *bs, bitmap *bmp, int bpp, ushort flags) {
2453+ void bm_lock_tga (int handle, bitmap_slot *bs, bitmap *bmp, int bpp, uint flags) {
24192454 ubyte *data = NULL ;
24202455 int byte_size;
24212456 char filename[MAX_FILENAME_LEN ];
@@ -2474,7 +2509,7 @@ void bm_lock_tga(int handle, bitmap_slot *bs, bitmap *bmp, int bpp, ushort flags
24742509 bm_convert_format (bmp, flags);
24752510}
24762511
2477- void bm_lock_user (int /* handle*/ , bitmap_slot *bs, bitmap *bmp, int bpp, ushort flags, bool convert) {
2512+ void bm_lock_user (int /* handle*/ , bitmap_slot *bs, bitmap *bmp, int bpp, uint flags, bool convert) {
24782513 auto be = &bs->entry ;
24792514
24802515 // Unload any existing data
@@ -2767,6 +2802,21 @@ void bm_page_in_texture(int bitmapnum, int nframes) {
27672802 frame_entry->used_flags = BMP_TEX_CUBEMAP ;
27682803 continue ;
27692804
2805+ case BM_TYPE_ETC2_RGB :
2806+ case BM_TYPE_ETC2_SRGB :
2807+ frame_entry->used_flags = BMP_TEX_ETC2_RGB8 ;
2808+ continue ;
2809+
2810+ case BM_TYPE_ETC2_RGBA1 :
2811+ case BM_TYPE_ETC2_SRGBA1 :
2812+ frame_entry->used_flags = BMP_TEX_ETC2_RGBA1 ;
2813+ continue ;
2814+
2815+ case BM_TYPE_ETC2_RGBA_EAC :
2816+ case BM_TYPE_ETC2_SRGBA_EAC :
2817+ frame_entry->used_flags = BMP_TEX_ETC2_RGBA8 ;
2818+ continue ;
2819+
27702820 default :
27712821 continue ;
27722822 }
@@ -2812,6 +2862,21 @@ void bm_page_in_xparent_texture(int bitmapnum, int nframes) {
28122862 entry->used_flags = BMP_TEX_CUBEMAP ;
28132863 continue ;
28142864
2865+ case BM_TYPE_ETC2_RGB :
2866+ case BM_TYPE_ETC2_SRGB :
2867+ entry->used_flags = BMP_TEX_ETC2_RGB8 ;
2868+ continue ;
2869+
2870+ case BM_TYPE_ETC2_RGBA1 :
2871+ case BM_TYPE_ETC2_SRGBA1 :
2872+ entry->used_flags = BMP_TEX_ETC2_RGBA1 ;
2873+ continue ;
2874+
2875+ case BM_TYPE_ETC2_RGBA_EAC :
2876+ case BM_TYPE_ETC2_SRGBA_EAC :
2877+ entry->used_flags = BMP_TEX_ETC2_RGBA8 ;
2878+ continue ;
2879+
28152880 default :
28162881 continue ;
28172882 }
@@ -3432,3 +3497,47 @@ SDL_Surface* bm_to_sdl_surface(int handle) {
34323497 return bitmapSurface;
34333498
34343499}
3500+
3501+ // copied bm_lock_dds and adjusted for ktx. missing BIG_ENDIAN
3502+ void bm_lock_ktx1 (int handle, bitmap_slot* bs, bitmap* bmp, int bpp, uint flags)
3503+ {
3504+ (void )bpp;
3505+ (void )flags;
3506+ ubyte* data = nullptr ;
3507+ ubyte ktx_bpp = 0 ;
3508+ int ktx_error = KTX1_ERROR_NONE ;
3509+ char filename[MAX_FILENAME_LEN ];
3510+
3511+ auto be = &bs->entry ;
3512+
3513+ bm_free_data (bs);
3514+
3515+ Assert (be->mem_taken > 0 );
3516+ Assert (&be->bm == bmp);
3517+
3518+ data = (ubyte*)bm_malloc (handle, be->mem_taken );
3519+ if (!data)
3520+ return ;
3521+ memset (data, 0 , be->mem_taken );
3522+
3523+ // make sure we are using the correct filename in the case of an EFF.
3524+ // this will populate filename[] whether it's EFF or not
3525+ EFF_FILENAME_CHECK ;
3526+
3527+ ktx_error = ktx1_read_bitmap (filename, data, &ktx_bpp);
3528+
3529+ if (ktx_error != KTX1_ERROR_NONE )
3530+ {
3531+ mprintf ((" KTX: read_bitmap error=%d file='%s'\n " , ktx_error, filename));
3532+ bm_free_data (bs);
3533+ return ;
3534+ }
3535+
3536+ bmp->bpp = ktx_bpp;
3537+ bmp->data = (ptr_u)data;
3538+ bmp->flags = 0 ;
3539+
3540+ #ifdef BMPMAN_NDEBUG
3541+ Assert (be->data_size > 0 );
3542+ #endif
3543+ }
0 commit comments