diff options
Diffstat (limited to 'source/blender/imbuf')
24 files changed, 307 insertions, 244 deletions
diff --git a/source/blender/imbuf/IMB_imbuf.h b/source/blender/imbuf/IMB_imbuf.h index 2f848b5be08..d947f4229a0 100644 --- a/source/blender/imbuf/IMB_imbuf.h +++ b/source/blender/imbuf/IMB_imbuf.h @@ -466,27 +466,29 @@ void IMB_scaleImBuf_threaded(struct ImBuf *ibuf, unsigned int newx, unsigned int * * \attention Defined in writeimage.c */ -short IMB_saveiff(struct ImBuf *ibuf, const char *filepath, int flags); +bool IMB_saveiff(struct ImBuf *ibuf, const char *filepath, int flags); bool IMB_prepare_write_ImBuf(const bool isfloat, struct ImBuf *ibuf); /** * * \attention Defined in util.c */ -bool IMB_ispic(const char *name); -int IMB_ispic_type(const char *name); +bool IMB_ispic(const char *filepath); +bool IMB_ispic_type_matches(const char *filepath, int filetype); +int IMB_ispic_type_from_memory(const unsigned char *mem, const size_t mem_size); +int IMB_ispic_type(const char *filepath); /** * * \attention Defined in util.c */ -bool IMB_isanim(const char *name); +bool IMB_isanim(const char *filepath); /** * * \attention Defined in util.c */ -int imb_get_anim_type(const char *name); +int imb_get_anim_type(const char *filepath); /** * diff --git a/source/blender/imbuf/intern/IMB_filetype.h b/source/blender/imbuf/intern/IMB_filetype.h index 53d9a1a2919..7bdbbb03227 100644 --- a/source/blender/imbuf/intern/IMB_filetype.h +++ b/source/blender/imbuf/intern/IMB_filetype.h @@ -32,15 +32,14 @@ typedef struct ImFileType { void (*init)(void); void (*exit)(void); - int (*is_a)(const unsigned char *buf); - int (*is_a_filepath)(const char *filepath); + 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, char colorspace[IM_MAX_SPACE]); struct ImBuf *(*load_filepath)(const char *filepath, int flags, char colorspace[IM_MAX_SPACE]); - int (*save)(struct ImBuf *ibuf, const char *filepath, int flags); + bool (*save)(struct ImBuf *ibuf, const char *filepath, int flags); void (*load_tile)(struct ImBuf *ibuf, const unsigned char *mem, size_t size, @@ -68,31 +67,31 @@ void imb_tile_cache_tile_free(struct ImBuf *ibuf, int tx, int ty); /* Type Specific Functions */ /* png */ -int imb_is_a_png(const unsigned char *mem); +bool imb_is_a_png(const unsigned char *mem, const size_t size); struct ImBuf *imb_loadpng(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]); -int imb_savepng(struct ImBuf *ibuf, const char *filepath, int flags); +bool imb_savepng(struct ImBuf *ibuf, const char *filepath, int flags); /* targa */ -int imb_is_a_targa(const unsigned char *buf); +bool imb_is_a_targa(const unsigned char *buf, const size_t size); struct ImBuf *imb_loadtarga(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]); -int imb_savetarga(struct ImBuf *ibuf, const char *filepath, int flags); +bool imb_savetarga(struct ImBuf *ibuf, const char *filepath, int flags); /* iris */ -int imb_is_a_iris(const unsigned char *mem); +bool imb_is_a_iris(const unsigned char *mem, const size_t size); struct ImBuf *imb_loadiris(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]); -int imb_saveiris(struct ImBuf *ibuf, const char *filepath, int flags); +bool imb_saveiris(struct ImBuf *ibuf, const char *filepath, int flags); /* jp2 */ -int imb_is_a_jp2(const unsigned char *buf); +bool imb_is_a_jp2(const unsigned char *buf, const size_t size); struct ImBuf *imb_load_jp2(const unsigned char *mem, size_t size, int flags, @@ -100,55 +99,55 @@ struct ImBuf *imb_load_jp2(const unsigned char *mem, struct ImBuf *imb_load_jp2_filepath(const char *filepath, int flags, char colorspace[IM_MAX_SPACE]); -int imb_save_jp2(struct ImBuf *ibuf, const char *filepath, int flags); +bool imb_save_jp2(struct ImBuf *ibuf, const char *filepath, int flags); /* jpeg */ -int imb_is_a_jpeg(const unsigned char *mem); -int imb_savejpeg(struct ImBuf *ibuf, const char *filepath, int flags); +bool imb_is_a_jpeg(const unsigned char *mem, const size_t size); +bool imb_savejpeg(struct ImBuf *ibuf, const char *filepath, int flags); struct ImBuf *imb_load_jpeg(const unsigned char *buffer, size_t size, int flags, char colorspace[IM_MAX_SPACE]); /* bmp */ -int imb_is_a_bmp(const unsigned char *buf); +bool imb_is_a_bmp(const unsigned char *buf, const size_t size); struct ImBuf *imb_bmp_decode(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]); -int imb_savebmp(struct ImBuf *ibuf, const char *filepath, int flags); +bool imb_savebmp(struct ImBuf *ibuf, const char *filepath, int flags); /* cineon */ -int imb_is_a_cineon(const unsigned char *buf); -int imb_save_cineon(struct ImBuf *buf, const char *filepath, int flags); +bool imb_is_a_cineon(const unsigned char *buf, const size_t size); +bool imb_save_cineon(struct ImBuf *buf, const char *filepath, int flags); struct ImBuf *imb_load_cineon(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]); /* dpx */ -int imb_is_a_dpx(const unsigned char *buf); -int imb_save_dpx(struct ImBuf *buf, const char *filepath, int flags); +bool imb_is_a_dpx(const unsigned char *buf, const size_t size); +bool imb_save_dpx(struct ImBuf *buf, const char *filepath, int flags); struct ImBuf *imb_load_dpx(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]); /* hdr */ -int imb_is_a_hdr(const unsigned char *buf); +bool imb_is_a_hdr(const unsigned char *buf, const size_t size); struct ImBuf *imb_loadhdr(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]); -int imb_savehdr(struct ImBuf *ibuf, const char *filepath, int flags); +bool imb_savehdr(struct ImBuf *ibuf, const char *filepath, int flags); /* tiff */ void imb_inittiff(void); -int imb_is_a_tiff(const unsigned char *buf); +bool imb_is_a_tiff(const unsigned char *buf, const size_t size); struct ImBuf *imb_loadtiff(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]); void imb_loadtiletiff( struct ImBuf *ibuf, const unsigned char *mem, size_t size, int tx, int ty, unsigned int *rect); -int imb_savetiff(struct ImBuf *ibuf, const char *filepath, int flags); +bool imb_savetiff(struct ImBuf *ibuf, const char *filepath, int flags); diff --git a/source/blender/imbuf/intern/bmp.c b/source/blender/imbuf/intern/bmp.c index f897b1c6df3..58ce02f28ae 100644 --- a/source/blender/imbuf/intern/bmp.c +++ b/source/blender/imbuf/intern/bmp.c @@ -72,43 +72,43 @@ typedef struct BMPHEADER { CHECK_HEADER_FIELD(_mem, "CI") || CHECK_HEADER_FIELD(_mem, "CP") || \ CHECK_HEADER_FIELD(_mem, "IC") || CHECK_HEADER_FIELD(_mem, "PT")) -static int checkbmp(const uchar *mem) +static bool checkbmp(const uchar *mem, const size_t size) { + if (size < BMP_FILEHEADER_SIZE) { + return false; + } + + if (!CHECK_HEADER_FIELD_BMP(mem)) { + return false; + } - int ret_val = 0; + bool ok = false; BMPINFOHEADER bmi; uint u; - if (mem) { - if (CHECK_HEADER_FIELD_BMP(mem)) { - /* skip fileheader */ - mem += BMP_FILEHEADER_SIZE; - } - else { - return 0; - } + /* skip fileheader */ + mem += BMP_FILEHEADER_SIZE; - /* for systems where an int needs to be 4 bytes aligned */ - memcpy(&bmi, mem, sizeof(bmi)); + /* for systems where an int needs to be 4 bytes aligned */ + memcpy(&bmi, mem, sizeof(bmi)); - u = LITTLE_LONG(bmi.biSize); - /* we only support uncompressed images for now. */ - if (u >= sizeof(BMPINFOHEADER)) { - if (bmi.biCompression == 0) { - u = LITTLE_SHORT(bmi.biBitCount); - if (u > 0 && u <= 32) { - ret_val = 1; - } + u = LITTLE_LONG(bmi.biSize); + /* we only support uncompressed images for now. */ + if (u >= sizeof(BMPINFOHEADER)) { + if (bmi.biCompression == 0) { + u = LITTLE_SHORT(bmi.biBitCount); + if (u > 0 && u <= 32) { + ok = true; } } } - return ret_val; + return ok; } -int imb_is_a_bmp(const uchar *buf) +bool imb_is_a_bmp(const uchar *buf, size_t size) { - return checkbmp(buf); + return checkbmp(buf, size); } ImBuf *imb_bmp_decode(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]) @@ -124,7 +124,7 @@ ImBuf *imb_bmp_decode(const uchar *mem, size_t size, int flags, char colorspace[ (void)size; /* unused */ - if (checkbmp(mem) == 0) { + if (checkbmp(mem, size) == 0) { return NULL; } @@ -296,7 +296,7 @@ static int putShortLSB(ushort us, FILE *ofile) } /* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */ -int imb_savebmp(ImBuf *ibuf, const char *filepath, int UNUSED(flags)) +bool imb_savebmp(ImBuf *ibuf, const char *filepath, int UNUSED(flags)) { BMPINFOHEADER infoheader; diff --git a/source/blender/imbuf/intern/cineon/cineon_dpx.c b/source/blender/imbuf/intern/cineon/cineon_dpx.c index 98082a97e0f..de54e6dab9d 100644 --- a/source/blender/imbuf/intern/cineon/cineon_dpx.c +++ b/source/blender/imbuf/intern/cineon/cineon_dpx.c @@ -183,14 +183,14 @@ static int imb_save_dpx_cineon(ImBuf *ibuf, const char *filename, int use_cineon return rvalue; } -int imb_save_cineon(struct ImBuf *buf, const char *filepath, int flags) +bool imb_save_cineon(struct ImBuf *buf, const char *filepath, int flags) { return imb_save_dpx_cineon(buf, filepath, 1, flags); } -int imb_is_a_cineon(const unsigned char *buf) +bool imb_is_a_cineon(const unsigned char *buf, size_t size) { - return logImageIsCineon(buf); + return logImageIsCineon(buf, size); } ImBuf *imb_load_cineon(const unsigned char *mem, @@ -198,20 +198,20 @@ ImBuf *imb_load_cineon(const unsigned char *mem, int flags, char colorspace[IM_MAX_SPACE]) { - if (imb_is_a_cineon(mem)) { + if (imb_is_a_cineon(mem, size)) { return imb_load_dpx_cineon(mem, size, 1, flags, colorspace); } return NULL; } -int imb_save_dpx(struct ImBuf *buf, const char *filepath, int flags) +bool imb_save_dpx(struct ImBuf *buf, const char *filepath, int flags) { return imb_save_dpx_cineon(buf, filepath, 0, flags); } -int imb_is_a_dpx(const unsigned char *buf) +bool imb_is_a_dpx(const unsigned char *buf, size_t size) { - return logImageIsDpx(buf); + return logImageIsDpx(buf, size); } ImBuf *imb_load_dpx(const unsigned char *mem, @@ -219,7 +219,7 @@ ImBuf *imb_load_dpx(const unsigned char *mem, int flags, char colorspace[IM_MAX_SPACE]) { - if (imb_is_a_dpx(mem)) { + if (imb_is_a_dpx(mem, size)) { return imb_load_dpx_cineon(mem, size, 0, flags, colorspace); } return NULL; diff --git a/source/blender/imbuf/intern/cineon/logImageCore.c b/source/blender/imbuf/intern/cineon/logImageCore.c index d5f5691c5f0..2d42609fdf5 100644 --- a/source/blender/imbuf/intern/cineon/logImageCore.c +++ b/source/blender/imbuf/intern/cineon/logImageCore.c @@ -96,15 +96,23 @@ void logImageSetVerbose(int verbosity) * IO stuff */ -int logImageIsDpx(const void *buffer) +int logImageIsDpx(const void *buffer, const unsigned int size) { - unsigned int magicNum = *(unsigned int *)buffer; + unsigned int magicNum; + if (size < sizeof(magicNum)) { + return 0; + } + magicNum = *(unsigned int *)buffer; return (magicNum == DPX_FILE_MAGIC || magicNum == swap_uint(DPX_FILE_MAGIC, 1)); } -int logImageIsCineon(const void *buffer) +int logImageIsCineon(const void *buffer, const unsigned int size) { - unsigned int magicNum = *(unsigned int *)buffer; + unsigned int magicNum; + if (size < sizeof(magicNum)) { + return 0; + } + magicNum = *(unsigned int *)buffer; return (magicNum == CINEON_FILE_MAGIC || magicNum == swap_uint(CINEON_FILE_MAGIC, 1)); } @@ -119,17 +127,17 @@ LogImageFile *logImageOpenFromFile(const char *filename, int cineon) return NULL; } - if (fread(&magicNum, sizeof(unsigned int), 1, f) != 1) { + if (fread(&magicNum, sizeof(magicNum), 1, f) != 1) { fclose(f); return NULL; } fclose(f); - if (logImageIsDpx(&magicNum)) { + if (logImageIsDpx(&magicNum, sizeof(magicNum))) { return dpxOpen((const unsigned char *)filename, 0, 0); } - if (logImageIsCineon(&magicNum)) { + if (logImageIsCineon(&magicNum, sizeof(magicNum))) { return cineonOpen((const unsigned char *)filename, 0, 0); } @@ -138,10 +146,10 @@ LogImageFile *logImageOpenFromFile(const char *filename, int cineon) LogImageFile *logImageOpenFromMemory(const unsigned char *buffer, unsigned int size) { - if (logImageIsDpx(buffer)) { + if (logImageIsDpx(buffer, size)) { return dpxOpen(buffer, 1, size); } - if (logImageIsCineon(buffer)) { + if (logImageIsCineon(buffer, size)) { return cineonOpen(buffer, 1, size); } diff --git a/source/blender/imbuf/intern/cineon/logImageCore.h b/source/blender/imbuf/intern/cineon/logImageCore.h index a2d50f21a98..c2704a086b6 100644 --- a/source/blender/imbuf/intern/cineon/logImageCore.h +++ b/source/blender/imbuf/intern/cineon/logImageCore.h @@ -181,8 +181,8 @@ enum descriptor { /* int functions return 0 for OK */ void logImageSetVerbose(int verbosity); -int logImageIsDpx(const void *buffer); -int logImageIsCineon(const void *buffer); +int logImageIsDpx(const void *buffer, unsigned int size); +int logImageIsCineon(const void *buffer, unsigned int size); LogImageFile *logImageOpenFromMemory(const unsigned char *buffer, unsigned int size); LogImageFile *logImageOpenFromFile(const char *filename, int cineon); void logImageGetSize(LogImageFile *logImage, int *width, int *height, int *depth); diff --git a/source/blender/imbuf/intern/dds/dds_api.cpp b/source/blender/imbuf/intern/dds/dds_api.cpp index e620c968f1c..5687824f9fd 100644 --- a/source/blender/imbuf/intern/dds/dds_api.cpp +++ b/source/blender/imbuf/intern/dds/dds_api.cpp @@ -42,16 +42,16 @@ extern "C" { -int imb_save_dds(struct ImBuf *ibuf, const char *name, int /*flags*/) +bool imb_save_dds(struct ImBuf *ibuf, const char *name, int /*flags*/) { - return 0; /* todo: finish this function */ + return false; /* todo: finish this function */ /* check image buffer */ if (ibuf == nullptr) { - return 0; + return false; } if (ibuf->rect == nullptr) { - return 0; + return false; } /* open file for writing */ @@ -69,21 +69,25 @@ int imb_save_dds(struct ImBuf *ibuf, const char *name, int /*flags*/) fildes << "DDS "; fildes.close(); - return 1; + return true; } -int imb_is_a_dds(const unsigned char *mem) /* note: use at most first 32 bytes */ +/* note: use at most first 32 bytes */ +bool imb_is_a_dds(const unsigned char *mem, const size_t size) { + if (size < 8) { + return false; + } /* heuristic check to see if mem contains a DDS file */ /* header.fourcc == FOURCC_DDS */ if ((mem[0] != 'D') || (mem[1] != 'D') || (mem[2] != 'S') || (mem[3] != ' ')) { - return 0; + return false; } /* header.size == 124 */ if ((mem[4] != 124) || mem[5] || mem[6] || mem[7]) { - return 0; + return false; } - return 1; + return true; } struct ImBuf *imb_load_dds(const unsigned char *mem, @@ -108,7 +112,7 @@ struct ImBuf *imb_load_dds(const unsigned char *mem, */ colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE); - if (!imb_is_a_dds(mem)) { + if (!imb_is_a_dds(mem, size)) { return nullptr; } diff --git a/source/blender/imbuf/intern/dds/dds_api.h b/source/blender/imbuf/intern/dds/dds_api.h index 930205c9efb..9020529f210 100644 --- a/source/blender/imbuf/intern/dds/dds_api.h +++ b/source/blender/imbuf/intern/dds/dds_api.h @@ -26,8 +26,9 @@ extern "C" { #endif -int imb_is_a_dds(const unsigned char *mem); /* use only first 32 bytes of mem */ -int imb_save_dds(struct ImBuf *ibuf, const char *name, int flags); +/* use only first 32 bytes of mem */ +bool imb_is_a_dds(const unsigned char *mem, const size_t size); +bool imb_save_dds(struct ImBuf *ibuf, const char *name, int flags); struct ImBuf *imb_load_dds(const unsigned char *mem, size_t size, int flags, diff --git a/source/blender/imbuf/intern/filetype.c b/source/blender/imbuf/intern/filetype.c index 667027ebfeb..1746be85131 100644 --- a/source/blender/imbuf/intern/filetype.c +++ b/source/blender/imbuf/intern/filetype.c @@ -54,7 +54,6 @@ const ImFileType IMB_FILE_TYPES[] = { {NULL, NULL, imb_is_a_jpeg, - NULL, imb_ftype_default, imb_load_jpeg, NULL, @@ -66,7 +65,6 @@ const ImFileType IMB_FILE_TYPES[] = { {NULL, NULL, imb_is_a_png, - NULL, imb_ftype_default, imb_loadpng, NULL, @@ -78,7 +76,6 @@ const ImFileType IMB_FILE_TYPES[] = { {NULL, NULL, imb_is_a_bmp, - NULL, imb_ftype_default, imb_bmp_decode, NULL, @@ -90,7 +87,6 @@ const ImFileType IMB_FILE_TYPES[] = { {NULL, NULL, imb_is_a_targa, - NULL, imb_ftype_default, imb_loadtarga, NULL, @@ -102,7 +98,6 @@ const ImFileType IMB_FILE_TYPES[] = { {NULL, NULL, imb_is_a_iris, - NULL, imb_ftype_iris, imb_loadiris, NULL, @@ -115,7 +110,6 @@ const ImFileType IMB_FILE_TYPES[] = { {NULL, NULL, imb_is_a_dpx, - NULL, imb_ftype_default, imb_load_dpx, NULL, @@ -127,7 +121,6 @@ const ImFileType IMB_FILE_TYPES[] = { {NULL, NULL, imb_is_a_cineon, - NULL, imb_ftype_default, imb_load_cineon, NULL, @@ -141,7 +134,6 @@ const ImFileType IMB_FILE_TYPES[] = { {imb_inittiff, NULL, imb_is_a_tiff, - NULL, imb_ftype_default, imb_loadtiff, NULL, @@ -155,7 +147,6 @@ const ImFileType IMB_FILE_TYPES[] = { {NULL, NULL, imb_is_a_hdr, - NULL, imb_ftype_default, imb_loadhdr, NULL, @@ -169,7 +160,6 @@ const ImFileType IMB_FILE_TYPES[] = { {imb_initopenexr, imb_exitopenexr, imb_is_a_openexr, - NULL, imb_ftype_default, imb_load_openexr, NULL, @@ -183,7 +173,6 @@ const ImFileType IMB_FILE_TYPES[] = { {NULL, NULL, imb_is_a_jp2, - NULL, imb_ftype_default, imb_load_jp2, NULL, @@ -197,7 +186,6 @@ const ImFileType IMB_FILE_TYPES[] = { {NULL, NULL, imb_is_a_dds, - NULL, imb_ftype_default, imb_load_dds, NULL, @@ -210,8 +198,7 @@ const ImFileType IMB_FILE_TYPES[] = { #ifdef WITH_OPENIMAGEIO {NULL, NULL, - NULL, - imb_is_a_filepath_photoshop, + imb_is_a_photoshop, imb_ftype_default, NULL, imb_load_photoshop, @@ -221,7 +208,7 @@ const ImFileType IMB_FILE_TYPES[] = { IMB_FTYPE_PSD, COLOR_ROLE_DEFAULT_FLOAT}, #endif - {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0}, + {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0}, }; const ImFileType *IMB_FILE_TYPES_LAST = &IMB_FILE_TYPES[ARRAY_SIZE(IMB_FILE_TYPES) - 1]; diff --git a/source/blender/imbuf/intern/indexer.c b/source/blender/imbuf/intern/indexer.c index 76717bef537..b5b8cd4a580 100644 --- a/source/blender/imbuf/intern/indexer.c +++ b/source/blender/imbuf/intern/indexer.c @@ -1218,8 +1218,11 @@ IndexBuildContext *IMB_anim_index_rebuild_context(struct anim *anim, } void IMB_anim_index_rebuild(struct IndexBuildContext *context, + /* NOLINTNEXTLINE: readability-non-const-parameter. */ short *stop, + /* NOLINTNEXTLINE: readability-non-const-parameter. */ short *do_update, + /* NOLINTNEXTLINE: readability-non-const-parameter. */ float *progress) { switch (context->anim_type) { diff --git a/source/blender/imbuf/intern/iris.c b/source/blender/imbuf/intern/iris.c index 214cdf1b63b..112b95bf1a1 100644 --- a/source/blender/imbuf/intern/iris.c +++ b/source/blender/imbuf/intern/iris.c @@ -243,8 +243,11 @@ static void test_endian_zbuf(struct ImBuf *ibuf) /* this one is only def-ed once, strangely... */ #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0]) -int imb_is_a_iris(const uchar *mem) +bool imb_is_a_iris(const uchar *mem, size_t size) { + if (size < 2) { + return false; + } return ((GS(mem) == IMAGIC) || (GSS(mem) == IMAGIC)); } @@ -271,7 +274,7 @@ struct ImBuf *imb_loadiris(const uchar *mem, size_t size, int flags, char colors return NULL; } - if (!imb_is_a_iris(mem)) { + if (!imb_is_a_iris(mem, size)) { return NULL; } @@ -807,7 +810,7 @@ fail: * Added: zbuf write */ -static int output_iris(uint *lptr, int xsize, int ysize, int zsize, const char *name, int *zptr) +static bool output_iris(uint *lptr, int xsize, int ysize, int zsize, const char *name, int *zptr) { FILE *outf; IMAGE *image; @@ -969,10 +972,9 @@ static int compressrow(uchar *lbuf, uchar *rlebuf, int z, int cnt) return optr - (uchar *)rlebuf; } -int imb_saveiris(struct ImBuf *ibuf, const char *filepath, int flags) +bool imb_saveiris(struct ImBuf *ibuf, const char *filepath, int flags) { short zsize; - int ret; zsize = (ibuf->planes + 7) >> 3; if (flags & IB_zbuf && ibuf->zbuf != NULL) { @@ -982,11 +984,11 @@ int imb_saveiris(struct ImBuf *ibuf, const char *filepath, int flags) IMB_convert_rgba_to_abgr(ibuf); test_endian_zbuf(ibuf); - ret = output_iris(ibuf->rect, ibuf->x, ibuf->y, zsize, filepath, ibuf->zbuf); + const bool ok = output_iris(ibuf->rect, ibuf->x, ibuf->y, zsize, filepath, ibuf->zbuf); /* restore! Quite clumsy, 2 times a switch... maybe better a malloc ? */ IMB_convert_rgba_to_abgr(ibuf); test_endian_zbuf(ibuf); - return ret; + return ok; } diff --git a/source/blender/imbuf/intern/jp2.c b/source/blender/imbuf/intern/jp2.c index 97af0eb8feb..e19589317d7 100644 --- a/source/blender/imbuf/intern/jp2.c +++ b/source/blender/imbuf/intern/jp2.c @@ -58,31 +58,38 @@ enum { DCP_CINEMA4K = 4, }; -static bool check_jp2(const unsigned char *mem) /* J2K_CFMT */ +static bool check_jp2(const unsigned char *mem, const size_t size) /* J2K_CFMT */ { + if (size < sizeof(JP2_HEAD)) { + return false; + } return memcmp(JP2_HEAD, mem, sizeof(JP2_HEAD)) ? 0 : 1; } -static bool check_j2k(const unsigned char *mem) /* J2K_CFMT */ +static bool check_j2k(const unsigned char *mem, const size_t size) /* J2K_CFMT */ { + if (size < sizeof(J2K_HEAD)) { + return false; + } return memcmp(J2K_HEAD, mem, sizeof(J2K_HEAD)) ? 0 : 1; } -static OPJ_CODEC_FORMAT format_from_header(const unsigned char mem[JP2_FILEHEADER_SIZE]) +static OPJ_CODEC_FORMAT format_from_header(const unsigned char mem[JP2_FILEHEADER_SIZE], + const size_t size) { - if (check_jp2(mem)) { + if (check_jp2(mem, size)) { return OPJ_CODEC_JP2; } - if (check_j2k(mem)) { + if (check_j2k(mem, size)) { return OPJ_CODEC_J2K; } return OPJ_CODEC_UNKNOWN; } -int imb_is_a_jp2(const unsigned char *buf) +bool imb_is_a_jp2(const unsigned char *buf, size_t size) { - return (check_jp2(buf) || check_j2k(buf)); + return (check_jp2(buf, size) || check_j2k(buf, size)); } /** @@ -317,7 +324,7 @@ ImBuf *imb_load_jp2(const unsigned char *mem, int flags, char colorspace[IM_MAX_SPACE]) { - const OPJ_CODEC_FORMAT format = (size > JP2_FILEHEADER_SIZE) ? format_from_header(mem) : + const OPJ_CODEC_FORMAT format = (size > JP2_FILEHEADER_SIZE) ? format_from_header(mem, size) : OPJ_CODEC_UNKNOWN; struct BufInfo buf_wrapper = { .buf = mem, @@ -348,7 +355,7 @@ ImBuf *imb_load_jp2_filepath(const char *filepath, int flags, char colorspace[IM fseek(p_file, 0, SEEK_SET); - const OPJ_CODEC_FORMAT format = format_from_header(mem); + const OPJ_CODEC_FORMAT format = format_from_header(mem, sizeof(mem)); ImBuf *ibuf = imb_load_jp2_stream(stream, format, flags, colorspace); opj_stream_destroy(stream); return ibuf; @@ -1194,22 +1201,22 @@ static opj_image_t *ibuftoimage(ImBuf *ibuf, opj_cparameters_t *parameters) return image; } -int imb_save_jp2_stream(struct ImBuf *ibuf, opj_stream_t *stream, int flags); +bool imb_save_jp2_stream(struct ImBuf *ibuf, opj_stream_t *stream, int flags); -int imb_save_jp2(struct ImBuf *ibuf, const char *filepath, int flags) +bool imb_save_jp2(struct ImBuf *ibuf, const char *filepath, int flags) { opj_stream_t *stream = opj_stream_create_from_file( filepath, OPJ_J2K_STREAM_CHUNK_SIZE, false, NULL); if (stream == NULL) { return 0; } - int ret = imb_save_jp2_stream(ibuf, stream, flags); + const bool ok = imb_save_jp2_stream(ibuf, stream, flags); opj_stream_destroy(stream); - return ret; + return ok; } /* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */ -int imb_save_jp2_stream(struct ImBuf *ibuf, opj_stream_t *stream, int UNUSED(flags)) +bool imb_save_jp2_stream(struct ImBuf *ibuf, opj_stream_t *stream, int UNUSED(flags)) { int quality = ibuf->foptions.quality; @@ -1230,7 +1237,7 @@ int imb_save_jp2_stream(struct ImBuf *ibuf, opj_stream_t *stream, int UNUSED(fla image = ibuftoimage(ibuf, ¶meters); opj_codec_t *codec = NULL; - int ok = false; + bool ok = false; /* JP2 format output */ { /* get a JP2 compressor handle */ diff --git a/source/blender/imbuf/intern/jpeg.c b/source/blender/imbuf/intern/jpeg.c index 38f8806d910..93cdbbb1407 100644 --- a/source/blender/imbuf/intern/jpeg.c +++ b/source/blender/imbuf/intern/jpeg.c @@ -57,12 +57,13 @@ static ImBuf *ibJpegImageFromCinfo(struct jpeg_decompress_struct *cinfo, int fla static const uchar jpeg_default_quality = 75; static uchar ibuf_quality; -int imb_is_a_jpeg(const unsigned char *mem) +bool imb_is_a_jpeg(const unsigned char *mem, const size_t size) { - if ((mem[0] == 0xFF) && (mem[1] == 0xD8)) { - return 1; + const char magic[2] = {0xFF, 0xD8}; + if (size < sizeof(magic)) { + return false; } - return 0; + return memcmp(mem, magic, sizeof(magic)) == 0; } /*---------------------------------------------------------- @@ -429,7 +430,7 @@ ImBuf *imb_load_jpeg(const unsigned char *buffer, struct my_error_mgr jerr; ImBuf *ibuf; - if (!imb_is_a_jpeg(buffer)) { + if (!imb_is_a_jpeg(buffer, size)) { return NULL; } @@ -608,7 +609,7 @@ static int init_jpeg(FILE *outfile, struct jpeg_compress_struct *cinfo, struct I return 0; } -static int save_stdjpeg(const char *name, struct ImBuf *ibuf) +static bool save_stdjpeg(const char *name, struct ImBuf *ibuf) { FILE *outfile; struct jpeg_compress_struct _cinfo, *cinfo = &_cinfo; @@ -642,7 +643,7 @@ static int save_stdjpeg(const char *name, struct ImBuf *ibuf) return 1; } -int imb_savejpeg(struct ImBuf *ibuf, const char *filepath, int flags) +bool imb_savejpeg(struct ImBuf *ibuf, const char *filepath, int flags) { ibuf->flags = flags; diff --git a/source/blender/imbuf/intern/oiio/openimageio_api.cpp b/source/blender/imbuf/intern/oiio/openimageio_api.cpp index 9d8c5d50a89..1e8c3c25778 100644 --- a/source/blender/imbuf/intern/oiio/openimageio_api.cpp +++ b/source/blender/imbuf/intern/oiio/openimageio_api.cpp @@ -163,16 +163,13 @@ static ImBuf *imb_oiio_load_image_float( extern "C" { -int imb_is_a_filepath_photoshop(const char *filename) +bool imb_is_a_photoshop(const unsigned char *mem, size_t size) { - const char *photoshop_extension[] = { - ".psd", - ".pdd", - ".psb", - nullptr, - }; - - return BLI_path_extension_check_array(filename, photoshop_extension); + const unsigned char magic[4] = {'8', 'B', 'P', 'S'}; + if (size < sizeof(magic)) { + return false; + } + return memcmp(magic, mem, sizeof(magic)) == 0; } int imb_save_photoshop(struct ImBuf *ibuf, const char * /*name*/, int flags) @@ -198,7 +195,7 @@ struct ImBuf *imb_load_photoshop(const char *filename, int flags, char colorspac const bool is_colorspace_manually_set = (colorspace[0] != '\0'); /* load image from file through OIIO */ - if (imb_is_a_filepath_photoshop(filename) == 0) { + if (IMB_ispic_type_matches(filename, IMB_FTYPE_PSD) == 0) { return nullptr; } diff --git a/source/blender/imbuf/intern/oiio/openimageio_api.h b/source/blender/imbuf/intern/oiio/openimageio_api.h index 135e3521f71..659050cdb00 100644 --- a/source/blender/imbuf/intern/oiio/openimageio_api.h +++ b/source/blender/imbuf/intern/oiio/openimageio_api.h @@ -31,7 +31,7 @@ extern "C" { struct ImBuf; -int imb_is_a_filepath_photoshop(const char *name); +bool imb_is_a_photoshop(const unsigned char *mem, const size_t size); int imb_save_photoshop(struct ImBuf *ibuf, const char *name, int flags); diff --git a/source/blender/imbuf/intern/openexr/openexr_api.cpp b/source/blender/imbuf/intern/openexr/openexr_api.cpp index 40a502a14db..56188fbe98a 100644 --- a/source/blender/imbuf/intern/openexr/openexr_api.cpp +++ b/source/blender/imbuf/intern/openexr/openexr_api.cpp @@ -330,8 +330,12 @@ extern "C" { * Test presence of OpenEXR file. * \param mem: pointer to loaded OpenEXR bitstream */ -int imb_is_a_openexr(const unsigned char *mem) +bool imb_is_a_openexr(const unsigned char *mem, const size_t size) { + /* No define is exposed for this size. */ + if (size < 4) { + return false; + } return Imf::isImfMagic((const char *)mem); } @@ -586,7 +590,7 @@ static bool imb_save_openexr_float(ImBuf *ibuf, const char *name, const int flag return true; } -int imb_save_openexr(struct ImBuf *ibuf, const char *name, int flags) +bool imb_save_openexr(struct ImBuf *ibuf, const char *name, int flags) { if (flags & IB_mem) { imb_addencodedbufferImBuf(ibuf); @@ -594,15 +598,15 @@ int imb_save_openexr(struct ImBuf *ibuf, const char *name, int flags) } if (ibuf->foptions.flag & OPENEXR_HALF) { - return (int)imb_save_openexr_half(ibuf, name, flags); + return imb_save_openexr_half(ibuf, name, flags); } /* when no float rect, we save as half (16 bits is sufficient) */ if (ibuf->rect_float == nullptr) { - return (int)imb_save_openexr_half(ibuf, name, flags); + return imb_save_openexr_half(ibuf, name, flags); } - return (int)imb_save_openexr_float(ibuf, name, flags); + return imb_save_openexr_float(ibuf, name, flags); } /* ******* Nicer API, MultiLayer and with Tile file support ************************************ */ @@ -1905,7 +1909,7 @@ struct ImBuf *imb_load_openexr(const unsigned char *mem, IMemStream *membuf = nullptr; MultiPartInputFile *file = nullptr; - if (imb_is_a_openexr(mem) == 0) { + if (imb_is_a_openexr(mem, size) == 0) { return nullptr; } diff --git a/source/blender/imbuf/intern/openexr/openexr_api.h b/source/blender/imbuf/intern/openexr/openexr_api.h index 73db146849b..940715690a7 100644 --- a/source/blender/imbuf/intern/openexr/openexr_api.h +++ b/source/blender/imbuf/intern/openexr/openexr_api.h @@ -32,9 +32,9 @@ extern "C" { void imb_initopenexr(void); void imb_exitopenexr(void); -int imb_is_a_openexr(const unsigned char *mem); +bool imb_is_a_openexr(const unsigned char *mem, const size_t size); -int imb_save_openexr(struct ImBuf *ibuf, const char *name, int flags); +bool imb_save_openexr(struct ImBuf *ibuf, const char *name, int flags); struct ImBuf *imb_load_openexr(const unsigned char *mem, size_t size, int flags, char *colorspace); diff --git a/source/blender/imbuf/intern/openexr/openexr_stub.cpp b/source/blender/imbuf/intern/openexr/openexr_stub.cpp index bfc4291c7b1..51bc2094053 100644 --- a/source/blender/imbuf/intern/openexr/openexr_stub.cpp +++ b/source/blender/imbuf/intern/openexr/openexr_stub.cpp @@ -26,11 +26,11 @@ void *IMB_exr_get_handle(void) { - return NULL; + return nullptr; } void *IMB_exr_get_handle_name(const char * /*name*/) { - return NULL; + return nullptr; } void IMB_exr_add_channel(void * /*handle*/, const char * /*layname*/, @@ -82,7 +82,7 @@ float *IMB_exr_channel_rect(void * /*handle*/, const char * /*passname*/, const char * /*view*/) { - return NULL; + return nullptr; } void IMB_exr_read_channels(void * /*handle*/) diff --git a/source/blender/imbuf/intern/png.c b/source/blender/imbuf/intern/png.c index c4fbd3f7563..25fc6a1cddf 100644 --- a/source/blender/imbuf/intern/png.c +++ b/source/blender/imbuf/intern/png.c @@ -59,19 +59,21 @@ BLI_INLINE unsigned short UPSAMPLE_8_TO_16(const unsigned char _val) return (_val << 8) + _val; } -int imb_is_a_png(const unsigned char *mem) +bool imb_is_a_png(const unsigned char *mem, size_t size) { - int ret_val = 0; + const int num_to_check = 8; + if (size < num_to_check) { + return false; + } + bool ok = false; - if (mem) { #if (PNG_LIBPNG_VER_MAJOR == 1) && (PNG_LIBPNG_VER_MINOR == 2) - /* Older version of libpng doesn't use const pointer to memory. */ - ret_val = !png_sig_cmp((png_bytep)mem, 0, 8); + /* Older version of libpng doesn't use const pointer to memory. */ + ok = !png_sig_cmp((png_bytep)mem, 0, num_to_check); #else - ret_val = !png_sig_cmp(mem, 0, 8); + ok = !png_sig_cmp(mem, 0, num_to_check); #endif - } - return ret_val; + return ok; } static void Flush(png_structp png_ptr) @@ -119,7 +121,7 @@ BLI_INLINE unsigned short ftoshort(float val) return unit_float_to_ushort_clamp(val); } -int imb_savepng(struct ImBuf *ibuf, const char *filepath, int flags) +bool imb_savepng(struct ImBuf *ibuf, const char *filepath, int flags) { png_structp png_ptr; png_infop info_ptr; @@ -549,7 +551,7 @@ ImBuf *imb_loadpng(const unsigned char *mem, size_t size, int flags, char colors float *to_float; unsigned int channels; - if (imb_is_a_png(mem) == 0) { + if (imb_is_a_png(mem, size) == 0) { return NULL; } diff --git a/source/blender/imbuf/intern/radiance_hdr.c b/source/blender/imbuf/intern/radiance_hdr.c index 4cd44a25cb8..285b18595f7 100644 --- a/source/blender/imbuf/intern/radiance_hdr.c +++ b/source/blender/imbuf/intern/radiance_hdr.c @@ -197,17 +197,22 @@ static void FLOAT2RGBE(const fCOLOR fcol, RGBE rgbe) /* ImBuf read */ -int imb_is_a_hdr(const unsigned char *buf) +bool imb_is_a_hdr(const unsigned char *buf, const size_t size) { - /* For recognition, Blender only loads first 32 bytes, so use #?RADIANCE id instead */ - /* update: actually, the 'RADIANCE' part is just an optional program name, - * the magic word is really only the '#?' part */ - // if (strstr((char *)buf, "#?RADIANCE")) return 1; - if (strstr((char *)buf, "#?")) { - return 1; + /* NOTE: `#?RADIANCE` is used by other programs such as `ImageMagik`, + * Although there are some files in the wild that only use `#?` (from looking online). + * If this is ever a problem we could check for the longer header since this is part of the spec. + * + * We could check `32-bit_rle_rgbe` or `32-bit_rle_xyze` too since this is part of the format. + * Currently this isn't needed. + * + * See: http://paulbourke.net/dataformats/pic/ + */ + const unsigned char magic[2] = {'#', '?'}; + if (size < sizeof(magic)) { + return false; } - // if (strstr((char *)buf, "32-bit_rle_rgbe")) return 1; - return 0; + return memcmp(buf, magic, sizeof(magic)) == 0; } struct ImBuf *imb_loadhdr(const unsigned char *mem, @@ -224,7 +229,7 @@ struct ImBuf *imb_loadhdr(const unsigned char *mem, const unsigned char *ptr, *mem_eof = mem + size; char oriY[80], oriX[80]; - if (imb_is_a_hdr((void *)mem)) { + if (imb_is_a_hdr(mem, size)) { colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_FLOAT); /* find empty line, next line is resolution info */ @@ -409,7 +414,7 @@ static void writeHeader(FILE *file, int width, int height) fputc(10, file); } -int imb_savehdr(struct ImBuf *ibuf, const char *filepath, int flags) +bool imb_savehdr(struct ImBuf *ibuf, const char *filepath, int flags) { FILE *file = BLI_fopen(filepath, "wb"); float *fp = NULL; diff --git a/source/blender/imbuf/intern/targa.c b/source/blender/imbuf/intern/targa.c index 5a3cbd375b6..a9833623250 100644 --- a/source/blender/imbuf/intern/targa.c +++ b/source/blender/imbuf/intern/targa.c @@ -60,6 +60,14 @@ typedef struct TARGA { unsigned char imgdes; } TARGA; +/** + * On-disk header size. + * + * \note In theory it's possible padding would make the struct and on-disk size differ, + * so use a constant instead of `sizeof(TARGA)`. + */ +#define TARGA_HEADER_SIZE 18 + /***/ static int tga_out1(unsigned int data, FILE *file) @@ -286,14 +294,12 @@ static bool dumptarga(struct ImBuf *ibuf, FILE *file) return 1; } -int imb_savetarga(struct ImBuf *ibuf, const char *filepath, int flags) +bool imb_savetarga(struct ImBuf *ibuf, const char *filepath, int UNUSED(flags)) { - char buf[20] = {0}; + char buf[TARGA_HEADER_SIZE] = {0}; FILE *fildes; bool ok = false; - (void)flags; /* unused */ - buf[16] = (ibuf->planes + 0x7) & ~0x7; if (ibuf->planes > 8) { buf[2] = 10; @@ -326,7 +332,7 @@ int imb_savetarga(struct ImBuf *ibuf, const char *filepath, int flags) return 0; } - if (fwrite(buf, 1, 18, fildes) != 18) { + if (fwrite(buf, 1, TARGA_HEADER_SIZE, fildes) != TARGA_HEADER_SIZE) { fclose(fildes); return 0; } @@ -355,8 +361,12 @@ int imb_savetarga(struct ImBuf *ibuf, const char *filepath, int flags) return ok; } -static int checktarga(TARGA *tga, const unsigned char *mem) +static bool checktarga(TARGA *tga, const unsigned char *mem, const size_t size) { + if (size < TARGA_HEADER_SIZE) { + return false; + } + tga->numid = mem[0]; tga->maptyp = mem[1]; tga->imgtyp = mem[2]; @@ -372,7 +382,7 @@ static int checktarga(TARGA *tga, const unsigned char *mem) tga->imgdes = mem[17]; if (tga->maptyp > 1) { - return 0; + return false; } switch (tga->imgtyp) { case 1: /* raw cmap */ @@ -383,31 +393,31 @@ static int checktarga(TARGA *tga, const unsigned char *mem) case 11: /* b&w */ break; default: - return 0; + return false; } if (tga->mapsize && tga->mapbits > 32) { - return 0; + return false; } if (tga->xsize <= 0) { - return 0; + return false; } if (tga->ysize <= 0) { - return 0; + return false; } if (tga->pixsize > 32) { - return 0; + return false; } if (tga->pixsize == 0) { - return 0; + return false; } - return 1; + return true; } -int imb_is_a_targa(const unsigned char *buf) +bool imb_is_a_targa(const unsigned char *buf, size_t size) { TARGA tga; - return checktarga(&tga, buf); + return checktarga(&tga, buf, size); } static void complete_partial_load(struct ImBuf *ibuf, unsigned int *rect) @@ -627,7 +637,7 @@ ImBuf *imb_loadtarga(const unsigned char *mem, int32_t cp_data; uchar *cp = (uchar *)&cp_data; - if (checktarga(&tga, mem) == 0) { + if (checktarga(&tga, mem, mem_size) == 0) { return NULL; } @@ -647,7 +657,7 @@ ImBuf *imb_loadtarga(const unsigned char *mem, if (tga.imgtyp < 4) { ibuf->foptions.flag |= RAWTGA; } - mem = mem + 18 + tga.numid; + mem = mem + TARGA_HEADER_SIZE + tga.numid; cp[0] = 0xff; cp[1] = cp[2] = 0; diff --git a/source/blender/imbuf/intern/tiff.c b/source/blender/imbuf/intern/tiff.c index bc69a14fa47..587d6ad9e7e 100644 --- a/source/blender/imbuf/intern/tiff.c +++ b/source/blender/imbuf/intern/tiff.c @@ -316,10 +316,13 @@ static TIFF *imb_tiff_client_open(ImbTIFFMemFile *memFile, const unsigned char * * hence my manual comparison. - Jonathan Merritt (lancelet) 4th Sept 2005. */ #define IMB_TIFF_NCB 4 /* number of comparison bytes used */ -int imb_is_a_tiff(const unsigned char *buf) +bool imb_is_a_tiff(const unsigned char *buf, size_t size) { const char big_endian[IMB_TIFF_NCB] = {0x4d, 0x4d, 0x00, 0x2a}; const char lil_endian[IMB_TIFF_NCB] = {0x49, 0x49, 0x2a, 0x00}; + if (size < IMB_TIFF_NCB) { + return false; + } return ((memcmp(big_endian, buf, IMB_TIFF_NCB) == 0) || (memcmp(lil_endian, buf, IMB_TIFF_NCB) == 0)); @@ -578,7 +581,7 @@ ImBuf *imb_loadtiff(const unsigned char *mem, fprintf(stderr, "imb_loadtiff: size < IMB_TIFF_NCB\n"); return NULL; } - if (imb_is_a_tiff(mem) == 0) { + if (imb_is_a_tiff(mem, size) == 0) { return NULL; } @@ -759,8 +762,7 @@ void imb_loadtiletiff( * * \return 1 if the function is successful, 0 on failure. */ - -int imb_savetiff(ImBuf *ibuf, const char *filepath, int flags) +bool imb_savetiff(ImBuf *ibuf, const char *filepath, int flags) { TIFF *image = NULL; uint16 samplesperpixel, bitspersample; diff --git a/source/blender/imbuf/intern/util.c b/source/blender/imbuf/intern/util.c index 64b03f332a8..1741d3f20b7 100644 --- a/source/blender/imbuf/intern/util.c +++ b/source/blender/imbuf/intern/util.c @@ -117,75 +117,109 @@ const char *imb_ext_audio[] = { NULL, }; -int IMB_ispic_type(const char *name) -{ - /* increased from 32 to 64 because of the bitmaps header size */ +/* Increased from 32 to 64 because of the bitmaps header size. */ #define HEADER_SIZE 64 - unsigned char buf[HEADER_SIZE]; - const ImFileType *type; +static bool imb_ispic_read_header_from_filepath(const char *filepath, + unsigned char buf[HEADER_SIZE]) +{ BLI_stat_t st; int fp; - BLI_assert(!BLI_path_is_rel(name)); + BLI_assert(!BLI_path_is_rel(filepath)); if (UTIL_DEBUG) { - printf("%s: loading %s\n", __func__, name); + printf("%s: loading %s\n", __func__, filepath); } - if (BLI_stat(name, &st) == -1) { + if (BLI_stat(filepath, &st) == -1) { return false; } if (((st.st_mode) & S_IFMT) != S_IFREG) { return false; } - if ((fp = BLI_open(name, O_BINARY | O_RDONLY, 0)) == -1) { + if ((fp = BLI_open(filepath, O_BINARY | O_RDONLY, 0)) == -1) { return false; } - memset(buf, 0, sizeof(buf)); + memset(buf, 0, HEADER_SIZE); if (read(fp, buf, HEADER_SIZE) <= 0) { close(fp); return false; } close(fp); + return true; +} + +int IMB_ispic_type_from_memory(const unsigned char *mem, const size_t mem_size) +{ + unsigned char buf_static[HEADER_SIZE]; + const unsigned char *buf; - /* XXX move this exception */ - if ((BIG_LONG(((int *)buf)[0]) & 0xfffffff0) == 0xffd8ffe0) { - return IMB_FTYPE_JPG; + if (mem_size >= HEADER_SIZE) { + buf = buf_static; + } + else { + memset(buf_static, 0, HEADER_SIZE); + memcpy(buf_static, mem, mem_size); + buf = buf_static; } - for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) { - if (type->is_a) { - if (type->is_a(buf)) { + for (const ImFileType *type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) { + if (type->is_a != NULL) { + if (type->is_a(buf, HEADER_SIZE)) { return type->filetype; } } - else if (type->is_a_filepath) { - if (type->is_a_filepath(name)) { - return type->filetype; + } + + return 0; +} + +int IMB_ispic_type(const char *filepath) +{ + unsigned char buf[HEADER_SIZE]; + if (!imb_ispic_read_header_from_filepath(filepath, buf)) { + return 0; + } + return IMB_ispic_type_from_memory(buf, HEADER_SIZE); +} + +bool IMB_ispic_type_matches(const char *filepath, int filetype) +{ + unsigned char buf[HEADER_SIZE]; + if (!imb_ispic_read_header_from_filepath(filepath, buf)) { + return 0; + } + + 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, HEADER_SIZE); } } } - return 0; +} #undef HEADER_SIZE -} -bool IMB_ispic(const char *name) +bool IMB_ispic(const char *filepath) { - return (IMB_ispic_type(name) != 0); + return (IMB_ispic_type(filepath) != 0); } -static int isavi(const char *name) +static bool isavi(const char *filepath) { #ifdef WITH_AVI - return AVI_is_avi(name); + return AVI_is_avi(filepath); #else - (void)name; + (void)filepath; return false; #endif } @@ -244,7 +278,7 @@ const char *IMB_ffmpeg_last_error(void) return ffmpeg_last_error; } -static int isffmpeg(const char *filename) +static int isffmpeg(const char *filepath) { AVFormatContext *pFormatCtx = NULL; unsigned int i; @@ -252,7 +286,7 @@ static int isffmpeg(const char *filename) AVCodec *pCodec; AVCodecContext *pCodecCtx; - if (BLI_path_extension_check_n(filename, + if (BLI_path_extension_check_n(filepath, ".swf", ".jpg", ".jp2", @@ -269,7 +303,7 @@ static int isffmpeg(const char *filename) return 0; } - if (avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) { + if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0) { if (UTIL_DEBUG) { fprintf(stderr, "isffmpeg: av_open_input_file failed\n"); } @@ -285,7 +319,7 @@ static int isffmpeg(const char *filename) } if (UTIL_DEBUG) { - av_dump_format(pFormatCtx, 0, filename, 0); + av_dump_format(pFormatCtx, 0, filepath, 0); } /* Find the first video stream */ @@ -324,60 +358,60 @@ static int isffmpeg(const char *filename) } #endif -int imb_get_anim_type(const char *name) +int imb_get_anim_type(const char *filepath) { int type; BLI_stat_t st; - BLI_assert(!BLI_path_is_rel(name)); + BLI_assert(!BLI_path_is_rel(filepath)); if (UTIL_DEBUG) { - printf("%s: %s\n", __func__, name); + printf("%s: %s\n", __func__, filepath); } #ifndef _WIN32 # ifdef WITH_FFMPEG /* stat test below fails on large files > 4GB */ - if (isffmpeg(name)) { + if (isffmpeg(filepath)) { return ANIM_FFMPEG; } # endif - if (BLI_stat(name, &st) == -1) { + if (BLI_stat(filepath, &st) == -1) { return 0; } if (((st.st_mode) & S_IFMT) != S_IFREG) { return 0; } - if (isavi(name)) { + if (isavi(filepath)) { return ANIM_AVI; } - if (ismovie(name)) { + if (ismovie(filepath)) { return ANIM_MOVIE; } #else - if (BLI_stat(name, &st) == -1) { + if (BLI_stat(filepath, &st) == -1) { return 0; } if (((st.st_mode) & S_IFMT) != S_IFREG) { return 0; } - if (ismovie(name)) { + if (ismovie(filepath)) { return ANIM_MOVIE; } # ifdef WITH_FFMPEG - if (isffmpeg(name)) { + if (isffmpeg(filepath)) { return ANIM_FFMPEG; } # endif - if (isavi(name)) { + if (isavi(filepath)) { return ANIM_AVI; } #endif - type = IMB_ispic(name); + type = IMB_ispic(filepath); if (type) { return ANIM_SEQUENCE; } @@ -385,11 +419,11 @@ int imb_get_anim_type(const char *name) return ANIM_NONE; } -bool IMB_isanim(const char *filename) +bool IMB_isanim(const char *filepath) { int type; - type = imb_get_anim_type(filename); + type = imb_get_anim_type(filepath); return (type && type != ANIM_SEQUENCE); } diff --git a/source/blender/imbuf/intern/writeimage.c b/source/blender/imbuf/intern/writeimage.c index e321470559a..71ecc8b14ae 100644 --- a/source/blender/imbuf/intern/writeimage.c +++ b/source/blender/imbuf/intern/writeimage.c @@ -41,7 +41,7 @@ static bool prepare_write_imbuf(const ImFileType *type, ImBuf *ibuf) return IMB_prepare_write_ImBuf((type->flag & IM_FTYPE_FLOAT), ibuf); } -short IMB_saveiff(struct ImBuf *ibuf, const char *filepath, int flags) +bool IMB_saveiff(struct ImBuf *ibuf, const char *filepath, int flags) { const ImFileType *type; @@ -56,13 +56,8 @@ short IMB_saveiff(struct ImBuf *ibuf, const char *filepath, int flags) for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) { if (type->save && type->ftype(type, ibuf)) { - short result = false; - prepare_write_imbuf(type, ibuf); - - result = type->save(ibuf, filepath, flags); - - return result; + return type->save(ibuf, filepath, flags); } } |