Skip to content

Commit 36bedea

Browse files
committed
support for TGA
1 parent 5416aba commit 36bedea

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

libgpujpeg/gpujpeg_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ enum gpujpeg_image_file_format {
325325
/// RGBA file format, simple data format without header [R G B A] [R G B A] ...
326326
GPUJPEG_IMAGE_FILE_RGBA,
327327
GPUJPEG_IMAGE_FILE_BMP,
328+
GPUJPEG_IMAGE_FILE_TGA,
328329
/// PNM file format
329330
GPUJPEG_IMAGE_FILE_PGM,
330331
GPUJPEG_IMAGE_FILE_PPM,

src/gpujpeg_common.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ gpujpeg_image_get_file_format(const char* filename)
393393
{ "jpeg", GPUJPEG_IMAGE_FILE_JPEG},
394394
{ "jfif", GPUJPEG_IMAGE_FILE_JPEG},
395395
{ "bmp", GPUJPEG_IMAGE_FILE_BMP},
396+
{ "tga", GPUJPEG_IMAGE_FILE_TGA},
396397
//{ "pbm", GPUJPEG_IMAGE_FILE_PNM},
397398
{ "pnm", GPUJPEG_IMAGE_FILE_PNM},
398399
{ "pgm", GPUJPEG_IMAGE_FILE_PGM},
@@ -439,6 +440,7 @@ static enum { FF_CS_NONE, FF_CS_RGB, FF_CS_YCBCR } get_file_type_cs(enum gpujpeg
439440
case GPUJPEG_IMAGE_FILE_RGB:
440441
case GPUJPEG_IMAGE_FILE_RGBA:
441442
case GPUJPEG_IMAGE_FILE_BMP:
443+
case GPUJPEG_IMAGE_FILE_TGA:
442444
case GPUJPEG_IMAGE_FILE_PGM:
443445
case GPUJPEG_IMAGE_FILE_PPM:
444446
case GPUJPEG_IMAGE_FILE_PNM:
@@ -1285,6 +1287,7 @@ gpujpeg_image_get_properties(const char *filename, struct gpujpeg_image_paramete
12851287
param_image->pixel_format = GPUJPEG_420_U8_P0P1P2;
12861288
break;
12871289
case GPUJPEG_IMAGE_FILE_BMP:
1290+
case GPUJPEG_IMAGE_FILE_TGA:
12881291
case GPUJPEG_IMAGE_FILE_PAM:
12891292
case GPUJPEG_IMAGE_FILE_PGM:
12901293
case GPUJPEG_IMAGE_FILE_PNM:

src/utils/image_delegate.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
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

8789
static 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

426428
static 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

437455
static 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
536560
image_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

Comments
 (0)