From 75c71b78ba9a14818d10dc38c91fc180ec52330f Mon Sep 17 00:00:00 2001 From: Jesse Yurkovich Date: Sat, 6 Jan 2024 05:50:04 +0100 Subject: [PATCH] Cleanup: Use new OIIO valid_file API Now that OIIO has proper `valid_file` APIs for the formats we care about, and which take MemReaders, we can remove the code added to TIFF, PSD, and PNG as part of 5cc8fea7e99. Additionally, this change eliminates the recent console spew on startup where the TIFF loader is asked to load non-TIFF files (it is based on the ordering of the filetype array)[1]. We now make a `valid_file` check during open to address this. [1] `: Not a TIFF or MDI file, bad magic number 12150 (0x2f76).` Pull Request: https://projects.blender.org/blender/blender/pulls/116826 --- source/blender/imbuf/intern/format_png.cc | 6 +----- source/blender/imbuf/intern/format_psd.cc | 6 +----- source/blender/imbuf/intern/format_tiff.cc | 10 +--------- .../blender/imbuf/intern/oiio/openimageio_support.cc | 8 ++++---- 4 files changed, 7 insertions(+), 23 deletions(-) diff --git a/source/blender/imbuf/intern/format_png.cc b/source/blender/imbuf/intern/format_png.cc index e1a62581e14..9db42aacea9 100644 --- a/source/blender/imbuf/intern/format_png.cc +++ b/source/blender/imbuf/intern/format_png.cc @@ -19,11 +19,7 @@ extern "C" { bool imb_is_a_png(const uchar *mem, size_t size) { - const char signature[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}; - if (size < sizeof(signature)) { - return false; - } - return memcmp(signature, mem, sizeof(signature)) == 0; + return imb_oiio_check(mem, size, "png"); } ImBuf *imb_load_png(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]) diff --git a/source/blender/imbuf/intern/format_psd.cc b/source/blender/imbuf/intern/format_psd.cc index 655336d5e57..fe53dfdd025 100644 --- a/source/blender/imbuf/intern/format_psd.cc +++ b/source/blender/imbuf/intern/format_psd.cc @@ -19,11 +19,7 @@ extern "C" { bool imb_is_a_psd(const uchar *mem, size_t size) { - const uchar magic[4] = {'8', 'B', 'P', 'S'}; - if (size < sizeof(magic)) { - return false; - } - return memcmp(magic, mem, sizeof(magic)) == 0; + return imb_oiio_check(mem, size, "psd"); } ImBuf *imb_load_psd(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]) diff --git a/source/blender/imbuf/intern/format_tiff.cc b/source/blender/imbuf/intern/format_tiff.cc index 0d777ff6db1..3652b4e38e3 100644 --- a/source/blender/imbuf/intern/format_tiff.cc +++ b/source/blender/imbuf/intern/format_tiff.cc @@ -19,15 +19,7 @@ extern "C" { bool imb_is_a_tiff(const uchar *mem, size_t size) { - constexpr int MAGIC_SIZE = 4; - if (size < MAGIC_SIZE) { - return false; - } - - const char big_endian[MAGIC_SIZE] = {0x4d, 0x4d, 0x00, 0x2a}; - const char lil_endian[MAGIC_SIZE] = {0x49, 0x49, 0x2a, 0x00}; - return ((memcmp(big_endian, mem, MAGIC_SIZE) == 0) || - (memcmp(lil_endian, mem, MAGIC_SIZE) == 0)); + return imb_oiio_check(mem, size, "tif"); } ImBuf *imb_load_tiff(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE]) diff --git a/source/blender/imbuf/intern/oiio/openimageio_support.cc b/source/blender/imbuf/intern/oiio/openimageio_support.cc index 19feded724b..b6c329c468a 100644 --- a/source/blender/imbuf/intern/oiio/openimageio_support.cc +++ b/source/blender/imbuf/intern/oiio/openimageio_support.cc @@ -250,7 +250,7 @@ static unique_ptr get_oiio_reader(const char *format, { /* Attempt to create a reader based on the passed in format. */ unique_ptr in = ImageInput::create(format); - if (!in) { + if (!(in && in->valid_file(&mem_reader))) { return nullptr; } @@ -258,7 +258,7 @@ static unique_ptr get_oiio_reader(const char *format, in->set_ioproxy(&mem_reader); bool ok = in->open("", r_newspec, config); if (!ok) { - in.reset(); + return nullptr; } return in; @@ -270,8 +270,8 @@ bool imb_oiio_check(const uchar *mem, size_t mem_size, const char *file_format) /* This memory proxy must remain alive for the full duration of the read. */ Filesystem::IOMemReader mem_reader(cspan(mem, mem_size)); - unique_ptr in = get_oiio_reader(file_format, config, mem_reader, spec); - return in ? true : false; + unique_ptr in = ImageInput::create(file_format); + return in && in->valid_file(&mem_reader); } ImBuf *imb_oiio_read(const ReadContext &ctx,