Cleanup: remove unnecessary ImFileType.ftype callback

This callback made some sense before moving the file-type information
from a bit-flag to an enum: e142ae77ca

Since then, we can compare the type value directly.

Also replace loops over file types with IMB_file_type_from_{ibuf/ftype}.
This commit is contained in:
Campbell Barton 2020-11-13 11:24:02 +11:00
parent ac299bb453
commit 454b7876ff
6 changed files with 45 additions and 61 deletions

View File

@ -33,7 +33,6 @@ typedef struct ImFileType {
void (*exit)(void);
bool (*is_a)(const unsigned char *buf, const size_t size);
int (*ftype)(const struct ImFileType *type, const struct ImBuf *ibuf);
struct ImBuf *(*load)(const unsigned char *mem,
size_t size,
int flags,
@ -55,6 +54,9 @@ typedef struct ImFileType {
extern const ImFileType IMB_FILE_TYPES[];
extern const ImFileType *IMB_FILE_TYPES_LAST;
const ImFileType *IMB_file_type_from_ftype(int ftype);
const ImFileType *IMB_file_type_from_ibuf(const struct ImBuf *ibuf);
void imb_filetypes_init(void);
void imb_filetypes_exit(void);

View File

@ -2616,7 +2616,6 @@ ImBuf *IMB_colormanagement_imbuf_for_write(ImBuf *ibuf,
if (do_colormanagement) {
bool make_byte = false;
const ImFileType *type;
/* for proper check whether byte buffer is required by a format or not
* should be pretty safe since this image buffer is supposed to be used for
@ -2629,13 +2628,10 @@ ImBuf *IMB_colormanagement_imbuf_for_write(ImBuf *ibuf,
* we need to allocate byte buffer and store color managed
* image there
*/
for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
if (type->save && type->ftype(type, colormanaged_ibuf)) {
if ((type->flag & IM_FTYPE_FLOAT) == 0) {
make_byte = true;
}
break;
const ImFileType *type = IMB_file_type_from_ibuf(colormanaged_ibuf);
if (type != NULL) {
if ((type->save != NULL) && (type->flag & IM_FTYPE_FLOAT) == 0) {
make_byte = true;
}
}
@ -3238,14 +3234,11 @@ void IMB_colormanagement_colorspace_from_ibuf_ftype(
}
/* Get color space from file type. */
const ImFileType *type;
for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
if (type->save && type->ftype(type, ibuf)) {
const char *role_colorspace;
role_colorspace = IMB_colormanagement_role_colorspace_name_get(type->default_save_role);
const ImFileType *type = IMB_file_type_from_ibuf(ibuf);
if (type != NULL) {
if (type->save != NULL) {
const char *role_colorspace = IMB_colormanagement_role_colorspace_name_get(
type->default_save_role);
BLI_strncpy(colorspace_settings->name, role_colorspace, sizeof(colorspace_settings->name));
}
}

View File

@ -40,22 +40,11 @@
# include "dds/dds_api.h"
#endif
static int imb_ftype_default(const ImFileType *type, const ImBuf *ibuf)
{
return (ibuf->ftype == type->filetype);
}
static int imb_ftype_iris(const ImFileType *type, const ImBuf *ibuf)
{
(void)type;
return (ibuf->ftype == IMB_FTYPE_IMAGIC);
}
const ImFileType IMB_FILE_TYPES[] = {
{
.init = NULL,
.exit = NULL,
.is_a = imb_is_a_jpeg,
.ftype = imb_ftype_default,
.load = imb_load_jpeg,
.load_filepath = NULL,
.save = imb_savejpeg,
@ -68,7 +57,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = NULL,
.exit = NULL,
.is_a = imb_is_a_png,
.ftype = imb_ftype_default,
.load = imb_loadpng,
.load_filepath = NULL,
.save = imb_savepng,
@ -81,7 +69,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = NULL,
.exit = NULL,
.is_a = imb_is_a_bmp,
.ftype = imb_ftype_default,
.load = imb_bmp_decode,
.load_filepath = NULL,
.save = imb_savebmp,
@ -94,7 +81,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = NULL,
.exit = NULL,
.is_a = imb_is_a_targa,
.ftype = imb_ftype_default,
.load = imb_loadtarga,
.load_filepath = NULL,
.save = imb_savetarga,
@ -107,7 +93,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = NULL,
.exit = NULL,
.is_a = imb_is_a_iris,
.ftype = imb_ftype_iris,
.load = imb_loadiris,
.load_filepath = NULL,
.save = imb_saveiris,
@ -121,7 +106,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = NULL,
.exit = NULL,
.is_a = imb_is_a_dpx,
.ftype = imb_ftype_default,
.load = imb_load_dpx,
.load_filepath = NULL,
.save = imb_save_dpx,
@ -134,7 +118,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = NULL,
.exit = NULL,
.is_a = imb_is_a_cineon,
.ftype = imb_ftype_default,
.load = imb_load_cineon,
.load_filepath = NULL,
.save = imb_save_cineon,
@ -149,7 +132,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = imb_inittiff,
.exit = NULL,
.is_a = imb_is_a_tiff,
.ftype = imb_ftype_default,
.load = imb_loadtiff,
.load_filepath = NULL,
.save = imb_savetiff,
@ -164,7 +146,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = NULL,
.exit = NULL,
.is_a = imb_is_a_hdr,
.ftype = imb_ftype_default,
.load = imb_loadhdr,
.load_filepath = NULL,
.save = imb_savehdr,
@ -179,7 +160,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = imb_initopenexr,
.exit = imb_exitopenexr,
.is_a = imb_is_a_openexr,
.ftype = imb_ftype_default,
.load = imb_load_openexr,
.load_filepath = NULL,
.save = imb_save_openexr,
@ -194,7 +174,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = NULL,
.exit = NULL,
.is_a = imb_is_a_jp2,
.ftype = imb_ftype_default,
.load = imb_load_jp2,
.load_filepath = NULL,
.save = imb_save_jp2,
@ -209,7 +188,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = NULL,
.exit = NULL,
.is_a = imb_is_a_dds,
.ftype = imb_ftype_default,
.load = imb_load_dds,
.load_filepath = NULL,
.save = NULL,
@ -224,7 +202,6 @@ const ImFileType IMB_FILE_TYPES[] = {
.init = NULL,
.exit = NULL,
.is_a = imb_is_a_photoshop,
.ftype = imb_ftype_default,
.load = NULL,
.load_filepath = imb_load_photoshop,
.save = NULL,
@ -234,11 +211,26 @@ const ImFileType IMB_FILE_TYPES[] = {
.default_save_role = COLOR_ROLE_DEFAULT_FLOAT,
},
#endif
{NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0},
{NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0},
};
const ImFileType *IMB_FILE_TYPES_LAST = &IMB_FILE_TYPES[ARRAY_SIZE(IMB_FILE_TYPES) - 1];
const ImFileType *IMB_file_type_from_ftype(int ftype)
{
for (const ImFileType *type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
if (ftype == type->filetype) {
return type;
}
}
return NULL;
}
const ImFileType *IMB_file_type_from_ibuf(const ImBuf *ibuf)
{
return IMB_file_type_from_ftype(ibuf->ftype);
}
void imb_filetypes_init(void)
{
const ImFileType *type;

View File

@ -282,7 +282,6 @@ ImBuf *IMB_testiffname(const char *filepath, int flags)
static void imb_loadtilefile(ImBuf *ibuf, int file, int tx, int ty, unsigned int *rect)
{
const ImFileType *type;
unsigned char *mem;
size_t size;
@ -301,8 +300,9 @@ static void imb_loadtilefile(ImBuf *ibuf, int file, int tx, int ty, unsigned int
return;
}
for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
if (type->load_tile && type->ftype(type, ibuf)) {
const ImFileType *type = IMB_file_type_from_ibuf(ibuf);
if (type != NULL) {
if (type->load_tile != NULL) {
type->load_tile(ibuf, mem, size, tx, ty, rect);
}
}

View File

@ -180,14 +180,13 @@ bool IMB_ispic_type_matches(const char *filepath, int filetype)
return false;
}
for (const ImFileType *type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
if (type->filetype == filetype) {
/* Requesting to load a type that can't check it's own header doesn't make sense.
* Keep the check for developers. */
BLI_assert(type->is_a != NULL);
if (type->is_a != NULL) {
return type->is_a(buf, (size_t)buf_size);
}
const ImFileType *type = IMB_file_type_from_ftype(filetype);
if (type != NULL) {
/* Requesting to load a type that can't check it's own header doesn't make sense.
* Keep the check for developers. */
BLI_assert(type->is_a != NULL);
if (type->is_a != NULL) {
return type->is_a(buf, (size_t)buf_size);
}
}
return false;
@ -416,11 +415,10 @@ bool IMB_isanim(const char *filepath)
bool IMB_isfloat(const ImBuf *ibuf)
{
const ImFileType *type;
for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
if (type->ftype(type, ibuf)) {
return (type->flag & IM_FTYPE_FLOAT) != 0;
const ImFileType *type = IMB_file_type_from_ibuf(ibuf);
if (type != NULL) {
if (type->flag & IM_FTYPE_FLOAT) {
return true;
}
}
return false;

View File

@ -43,8 +43,6 @@ static bool prepare_write_imbuf(const ImFileType *type, ImBuf *ibuf)
bool IMB_saveiff(struct ImBuf *ibuf, const char *filepath, int flags)
{
const ImFileType *type;
errno = 0;
BLI_assert(!BLI_path_is_rel(filepath));
@ -54,8 +52,9 @@ bool IMB_saveiff(struct ImBuf *ibuf, const char *filepath, int flags)
}
ibuf->flags = flags;
for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
if (type->save && type->ftype(type, ibuf)) {
const ImFileType *type = IMB_file_type_from_ibuf(ibuf);
if (type != NULL) {
if (type->save != NULL) {
prepare_write_imbuf(type, ibuf);
return type->save(ibuf, filepath, flags);
}