Cleanup: use IMB_FTYPE_NONE instead of 0 for imbuf format comparison

Image format code checked the file type against an enum except for
zero which is used when the format can't be detected.

Also add doc-strings to some of the image file type callbacks.
This commit is contained in:
Campbell Barton 2020-11-13 20:25:21 +11:00
parent 4a3b26dd5e
commit a8f9a24939
7 changed files with 28 additions and 10 deletions

View File

@ -1341,7 +1341,7 @@ int BKE_image_imtype_to_ftype(const char imtype, ImbFormatOptions *r_options)
char BKE_image_ftype_to_imtype(const int ftype, const ImbFormatOptions *options)
{
if (ftype == 0) {
if (ftype == IMB_FTYPE_NONE) {
return R_IMF_IMTYPE_TARGA;
}
if (ftype == IMB_FTYPE_IMAGIC) {

View File

@ -536,8 +536,8 @@ static void unpack_generate_paths(const char *name,
ImagePackedFile *imapf = ((Image *)id)->packedfiles.last;
if (imapf != NULL && imapf->packedfile != NULL) {
const PackedFile *pf = imapf->packedfile;
const int ftype = IMB_ispic_type_from_memory((const uchar *)pf->data, pf->size);
if (ftype != 0) {
enum eImbFileType ftype = IMB_ispic_type_from_memory((const uchar *)pf->data, pf->size);
if (ftype != IMB_FTYPE_NONE) {
const int imtype = BKE_image_ftype_to_imtype(ftype, NULL);
BKE_image_path_ensure_ext_from_imtype(tempname, imtype);
}

View File

@ -69,7 +69,7 @@ typedef struct DDSData {
* See T46524. */
/** #ImBuf.ftype flag, main image types. */
enum eImbTypes {
enum eImbFileType {
IMB_FTYPE_PNG = 1,
IMB_FTYPE_TGA = 2,
IMB_FTYPE_JPG = 3,
@ -98,6 +98,9 @@ enum eImbTypes {
#endif
};
/* Only for readability. */
#define IMB_FTYPE_NONE 0
/* ibuf->foptions flag, type specific options.
* Some formats include compression rations on some bits */
@ -243,7 +246,7 @@ typedef struct ImBuf {
/* file information */
/** file type we are going to save as */
enum eImbTypes ftype;
enum eImbFileType ftype;
/** file format specific flags */
ImbFormatOptions foptions;
/** filename associated with this image */

View File

@ -29,15 +29,26 @@ struct ImBuf;
#define IM_FTYPE_FLOAT 1
typedef struct ImFileType {
/** Optional, called once when initializing. */
void (*init)(void);
/** Optional, called once when exiting. */
void (*exit)(void);
/**
* Check if the data matches this file types 'magic',
* \note that this may only read in a small part of the files header,
* see: #IMB_ispic_type for details.
*/
bool (*is_a)(const unsigned char *buf, const size_t size);
/** Load an image from memory. */
struct ImBuf *(*load)(const unsigned char *mem,
size_t size,
int flags,
char colorspace[IM_MAX_SPACE]);
/** Load an image from a file. */
struct ImBuf *(*load_filepath)(const char *filepath, int flags, char colorspace[IM_MAX_SPACE]);
/** Save to a file (or memory if #IB_mem is set in `flags` and the format supports it). */
bool (*save)(struct ImBuf *ibuf, const char *filepath, int flags);
void (*load_tile)(struct ImBuf *ibuf,
const unsigned char *mem,
@ -47,7 +58,10 @@ typedef struct ImFileType {
unsigned int *rect);
int flag;
/** #eImbFileType */
int filetype;
int default_save_role;
} ImFileType;

View File

@ -159,7 +159,7 @@ int IMB_ispic_type_from_memory(const unsigned char *buf, const size_t buf_size)
}
}
return 0;
return IMB_FTYPE_NONE;
}
int IMB_ispic_type(const char *filepath)
@ -167,7 +167,7 @@ int IMB_ispic_type(const char *filepath)
unsigned char buf[HEADER_SIZE];
const ssize_t buf_size = imb_ispic_read_header_from_filepath(filepath, buf);
if (buf_size <= 0) {
return 0;
return IMB_FTYPE_NONE;
}
return IMB_ispic_type_from_memory(buf, (size_t)buf_size);
}
@ -196,7 +196,7 @@ bool IMB_ispic_type_matches(const char *filepath, int filetype)
bool IMB_ispic(const char *filepath)
{
return (IMB_ispic_type(filepath) != 0);
return (IMB_ispic_type(filepath) != IMB_FTYPE_NONE);
}
static bool isavi(const char *filepath)

View File

@ -235,7 +235,8 @@ static int rna_Image_file_format_get(PointerRNA *ptr)
{
Image *image = (Image *)ptr->data;
ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
int imtype = BKE_image_ftype_to_imtype(ibuf ? ibuf->ftype : 0, ibuf ? &ibuf->foptions : NULL);
int imtype = BKE_image_ftype_to_imtype(ibuf ? ibuf->ftype : IMB_FTYPE_NONE,
ibuf ? &ibuf->foptions : NULL);
BKE_image_release_ibuf(image, ibuf, NULL);

View File

@ -1391,7 +1391,7 @@ static char *wm_main_playanim_intern(int argc, const char **argv)
while (ps.picture) {
int hasevent;
#ifndef USE_IMB_CACHE
if (ibuf != NULL && ibuf->ftype == 0) {
if (ibuf != NULL && ibuf->ftype == IMB_FTYPE_NONE) {
IMB_freeImBuf(ibuf);
}
#endif