Image file type detection fixes/optimisations

This commit is contained in:
jdgleaver 2020-05-22 16:55:41 +01:00
parent 00d7c40c7a
commit d24498bdf1
3 changed files with 65 additions and 28 deletions

View File

@ -28,26 +28,51 @@
#include <boolean.h> #include <boolean.h>
#include <formats/image.h> #include <formats/image.h>
#include <file/nbio.h> #include <file/nbio.h>
#include <string/stdstring.h>
enum image_type_enum image_texture_get_type(const char *path) enum image_type_enum image_texture_get_type(const char *path)
{ {
#ifdef HAVE_RTGA /* We are comparing against a fixed list of file
if (strstr(path, ".tga")) * extensions, the longest (jpeg) being 4 characters
return IMAGE_TYPE_TGA; * in length. We therefore only need to extract the first
#endif * 5 characters from the extension of the input path
* to correctly validate a match */
const char *ext = NULL;
char ext_lower[6];
ext_lower[0] = '\0';
if (string_is_empty(path))
return IMAGE_TYPE_NONE;
/* Get file extension */
ext = strrchr(path, '.');
if (!ext || (*(++ext) == '\0'))
return IMAGE_TYPE_NONE;
/* Copy and convert to lower case */
strlcpy(ext_lower, ext, sizeof(ext_lower));
string_to_lower(ext_lower);
#ifdef HAVE_RPNG #ifdef HAVE_RPNG
if (strstr(path, ".png")) if (string_is_equal(ext_lower, "png"))
return IMAGE_TYPE_PNG; return IMAGE_TYPE_PNG;
#endif #endif
#ifdef HAVE_RJPEG #ifdef HAVE_RJPEG
if (strstr(path, ".jpg") || strstr(path, ".jpeg")) if (string_is_equal(ext_lower, "jpg") ||
string_is_equal(ext_lower, "jpeg"))
return IMAGE_TYPE_JPEG; return IMAGE_TYPE_JPEG;
#endif #endif
#ifdef HAVE_RBMP #ifdef HAVE_RBMP
if (strstr(path, ".bmp")) if (string_is_equal(ext_lower, "bmp"))
return IMAGE_TYPE_BMP; return IMAGE_TYPE_BMP;
#endif #endif
#ifdef HAVE_RTGA
if (string_is_equal(ext_lower, "tga"))
return IMAGE_TYPE_TGA;
#endif
return IMAGE_TYPE_NONE; return IMAGE_TYPE_NONE;
} }

View File

@ -50,6 +50,21 @@ static INLINE bool string_starts_with(const char *str, const char *prefix)
return (str && prefix) ? !strncmp(prefix, str, strlen(prefix)) : false; return (str && prefix) ? !strncmp(prefix, str, strlen(prefix)) : false;
} }
static INLINE bool string_ends_with(const char *str, const char *suffix)
{
size_t str_len;
size_t suffix_len;
if (!str || !suffix)
return false;
str_len = strlen(str);
suffix_len = strlen(suffix);
return (str_len < suffix_len) ? false :
!memcmp(suffix, str + (str_len - suffix_len), suffix_len);
}
#define STRLEN_CONST(x) ((sizeof((x))-1)) #define STRLEN_CONST(x) ((sizeof((x))-1))
#define string_is_not_equal(a, b) !string_is_equal((a), (b)) #define string_is_not_equal(a, b) !string_is_equal((a), (b))

View File

@ -422,7 +422,7 @@ bool task_push_image_load(const char *fullpath,
nbio->path = strdup(fullpath); nbio->path = strdup(fullpath);
image->type = IMAGE_TYPE_NONE; image->type = image_texture_get_type(fullpath);
image->status = IMAGE_STATUS_WAIT; image->status = IMAGE_STATUS_WAIT;
image->is_blocking = false; image->is_blocking = false;
image->is_blocking_on_processing = false; image->is_blocking_on_processing = false;
@ -439,26 +439,23 @@ bool task_push_image_load(const char *fullpath,
/* TODO/FIXME - shouldn't we set this ? */ /* TODO/FIXME - shouldn't we set this ? */
image->ti.supports_rgba = false; image->ti.supports_rgba = false;
if (strstr(fullpath, ".png")) switch (image->type)
{ {
nbio->type = NBIO_TYPE_PNG; case IMAGE_TYPE_PNG:
image->type = IMAGE_TYPE_PNG; nbio->type = NBIO_TYPE_PNG;
} break;
else if (strstr(fullpath, ".jpeg") case IMAGE_TYPE_JPEG:
|| strstr(fullpath, ".jpg")) nbio->type = NBIO_TYPE_JPEG;
{ break;
nbio->type = NBIO_TYPE_JPEG; case IMAGE_TYPE_BMP:
image->type = IMAGE_TYPE_JPEG; nbio->type = NBIO_TYPE_BMP;
} break;
else if (strstr(fullpath, ".bmp")) case IMAGE_TYPE_TGA:
{ nbio->type = NBIO_TYPE_TGA;
nbio->type = NBIO_TYPE_BMP; break;
image->type = IMAGE_TYPE_BMP; default:
} nbio->type = NBIO_TYPE_NONE;
else if (strstr(fullpath, ".tga")) break;
{
nbio->type = NBIO_TYPE_TGA;
image->type = IMAGE_TYPE_TGA;
} }
nbio->data = (struct nbio_image_handle*)image; nbio->data = (struct nbio_image_handle*)image;