-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The symbols of available image loaders should be hidden. This commit introduces a generic image loader that can select the appropriate image loader implementation by parsing the name of the given image file. Currently, both PNG and JPEG image formats are supported, and we might support (animated) GIFs later. Close #14
- Loading branch information
Showing
7 changed files
with
106 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <string.h> | ||
#include <strings.h> | ||
|
||
#include "twin_private.h" | ||
|
||
#if !defined(CONFIG_LOADER_PNG) | ||
#define CONFIG_LOADER_PNG 0 | ||
#endif | ||
|
||
#if !defined(CONFIG_LOADER_JPEG) | ||
#define CONFIG_LOADER_JPEG 0 | ||
#endif | ||
|
||
/* Feature test macro */ | ||
#define LOADER_HAS(x) CONFIG_LOADER_##x | ||
|
||
/* clang-format off */ | ||
#define SUPPORTED_FORMATS \ | ||
IIF(LOADER_HAS(PNG))( \ | ||
_(png) \ | ||
) \ | ||
IIF(LOADER_HAS(JPEG))( \ | ||
_(jpeg) \ | ||
) | ||
/* clang-format on */ | ||
|
||
typedef enum { | ||
IMAGE_TYPE_unknown, | ||
#define _(x) IMAGE_TYPE_##x, | ||
SUPPORTED_FORMATS | ||
#undef _ | ||
} twin_image_format_t; | ||
|
||
/* FIXME: Check the header of the given images to determine the supported image | ||
* formats instead of parsing the filename without checking its content. | ||
*/ | ||
static twin_image_format_t image_type_from_name(const char *path) | ||
{ | ||
twin_image_format_t type = IMAGE_TYPE_unknown; | ||
const char *extname = strrchr(path, '.'); | ||
if (!extname) | ||
return IMAGE_TYPE_unknown; | ||
#if LOADER_HAS(PNG) | ||
else if (!strcasecmp(extname, ".png")) { | ||
type = IMAGE_TYPE_png; | ||
} | ||
#endif | ||
#if LOADER_HAS(JPEG) | ||
else if (!strcasecmp(extname, ".jpg") || !strcasecmp(extname, ".jpeg")) { | ||
type = IMAGE_TYPE_jpeg; | ||
} | ||
#endif | ||
/* otherwise, unsupported format */ | ||
|
||
return type; | ||
} | ||
|
||
#define _(x) \ | ||
twin_pixmap_t *_twin_##x##_to_pixmap(const char *filepath, \ | ||
twin_format_t fmt); | ||
SUPPORTED_FORMATS | ||
#undef _ | ||
|
||
typedef twin_pixmap_t *(*loader_func_t)(const char *, twin_format_t); | ||
|
||
/* clang-format off */ | ||
static loader_func_t image_loaders[] = { | ||
[IMAGE_TYPE_unknown] = NULL, | ||
#define _(x) [IMAGE_TYPE_##x] = _twin_##x##_to_pixmap, | ||
SUPPORTED_FORMATS | ||
#undef _ | ||
}; | ||
/* clang-format on */ | ||
|
||
twin_pixmap_t *twin_pixmap_from_file(const char *path, twin_format_t fmt) | ||
{ | ||
loader_func_t loader = image_loaders[image_type_from_name(path)]; | ||
if (!loader) | ||
return NULL; | ||
return loader(path, fmt); | ||
} |