3434#ifdef _WIN32
3535#include <windows.h>
3636#define PATH_MAX MAX_PATH
37+ #define strcasecmp _stricmp
3738#else
3839#include <limits.h>
40+ #include <strings.h> // for strcasecmp
3941#endif
4042
4143#ifdef __linux__
@@ -85,7 +87,7 @@ gpujpeg_cuda_realloc_sized_host(void* ptr, int oldsz, int newsz)
8587}
8688
8789static int
88- bmp_load_delegate (const char * filename , size_t * image_size , void * * image_data , allocator_t alloc )
90+ stbi_load_delegate (const char * filename , size_t * image_size , void * * image_data , allocator_t alloc )
8991{
9092 int x = 0 ;
9193 int y = 0 ;
@@ -424,18 +426,34 @@ tst_image_probe_delegate(const char* filename, enum gpujpeg_image_file_format fo
424426}
425427
426428static int
427- bmp_save_delegate (const char * filename , const struct gpujpeg_image_parameters * param_image , const char * data )
429+ stbi_save_delegate (const char * filename , const struct gpujpeg_image_parameters * param_image , const char * data )
428430{
429- if ( stbi_write_bmp (filename , param_image -> width , param_image -> height ,
430- gpujpeg_pixel_format_get_comp_count (param_image -> pixel_format ), data ) ) {
431+ int (* write_func )(char const * filename , int x , int y , int comp , const void * data ) = NULL ;
432+ const char * ext = strrchr (filename , '.' );
433+ if ( ext == NULL ) {
434+ ERROR_MSG ("[stbi] Output file %s doesn't contain extension!\n" , filename );
435+ }
436+ ext += 1 ;
437+ if ( strcasecmp (ext , "bmp" ) == 0 ) {
438+ write_func = stbi_write_bmp ;
439+ }
440+ else if ( strcasecmp (ext , "tga" ) == 0 ) {
441+ write_func = stbi_write_tga ;
442+ }
443+ else {
444+ ERROR_MSG ("[stbi] Unhandled file %s extension!\n" , filename );
445+ return -1 ;
446+ }
447+ if ( write_func (filename , param_image -> width , param_image -> height ,
448+ gpujpeg_pixel_format_get_comp_count (param_image -> pixel_format ), data ) ) {
431449 return 0 ;
432450 }
433451 ERROR_MSG ("[stbi] Cannot write output file %s: %s\n" , filename , stbi_failure_reason ());
434452 return -1 ;
435453}
436454
437455static int
438- bmp_image_probe_delegate (const char * filename , enum gpujpeg_image_file_format format ,
456+ stbi_image_probe_delegate (const char * filename , enum gpujpeg_image_file_format format ,
439457 struct gpujpeg_image_parameters * param_image , bool file_exists )
440458{
441459 (void )filename ;
@@ -450,6 +468,12 @@ bmp_image_probe_delegate(const char* filename, enum gpujpeg_image_file_format fo
450468 ERROR_MSG ("[stbi] Cannot obtain metadata from file %s: %s\n" , filename , stbi_failure_reason ());
451469 return -1 ;
452470 }
471+
472+ const char * fname_dot = strrchr (filename , '.' );
473+ if ( fname_dot != NULL && strcasecmp (fname_dot + 1 , "tga" ) == 0 && stbi_is_16_bit (filename ) ) {
474+ ERROR_MSG ("[stbi] 16-bit TGA %s is not supported!\n" , filename );
475+ }
476+
453477 if ( comp == 1 ) {
454478 // presumably sRGB, so not entirely correct (transfer, BT.709 primaries) but BT601_256LVLS chosen because is the
455479 // only one full-range
@@ -536,7 +560,8 @@ tst_image_load_delegate(const char* filename, size_t* image_size, void** image_d
536560image_load_delegate_t gpujpeg_get_image_load_delegate (enum gpujpeg_image_file_format format ) {
537561 switch (format ) {
538562 case GPUJPEG_IMAGE_FILE_BMP :
539- return bmp_load_delegate ;
563+ case GPUJPEG_IMAGE_FILE_TGA :
564+ return stbi_load_delegate ;
540565 case GPUJPEG_IMAGE_FILE_PGM :
541566 case GPUJPEG_IMAGE_FILE_PPM :
542567 case GPUJPEG_IMAGE_FILE_PNM :
@@ -564,7 +589,8 @@ image_probe_delegate_t gpujpeg_get_image_probe_delegate(enum gpujpeg_image_file_
564589{
565590 switch (format ) {
566591 case GPUJPEG_IMAGE_FILE_BMP :
567- return bmp_image_probe_delegate ;
592+ case GPUJPEG_IMAGE_FILE_TGA :
593+ return stbi_image_probe_delegate ;
568594 case GPUJPEG_IMAGE_FILE_PGM :
569595 case GPUJPEG_IMAGE_FILE_PPM :
570596 case GPUJPEG_IMAGE_FILE_PNM :
@@ -592,7 +618,8 @@ image_save_delegate_t gpujpeg_get_image_save_delegate(enum gpujpeg_image_file_fo
592618{
593619 switch (format ) {
594620 case GPUJPEG_IMAGE_FILE_BMP :
595- return bmp_save_delegate ;
621+ case GPUJPEG_IMAGE_FILE_TGA :
622+ return stbi_save_delegate ;
596623 case GPUJPEG_IMAGE_FILE_PAM :
597624 return pam_save_delegate ;
598625 case GPUJPEG_IMAGE_FILE_PGM :
0 commit comments