From 0209e3790e4ab984b811f2994947ae71eef69b82 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Tue, 20 Oct 2020 23:25:58 +0200 Subject: [PATCH] Add `Image.load_bmp_from_buffer()` for run-time BMP image loading This partially addresses https://github.com/godotengine/godot-proposals/issues/676. --- core/image.cpp | 15 ++++++++++++++- core/image.h | 2 ++ doc/classes/Image.xml | 10 ++++++++++ modules/bmp/image_loader_bmp.cpp | 20 +++++++++++++++++--- modules/tga/image_loader_tga.cpp | 4 ++-- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/core/image.cpp b/core/image.cpp index b8a443eed2d..950ff9b48d5 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -2711,6 +2711,7 @@ ImageMemLoadFunc Image::_png_mem_loader_func = nullptr; ImageMemLoadFunc Image::_jpg_mem_loader_func = nullptr; ImageMemLoadFunc Image::_webp_mem_loader_func = nullptr; ImageMemLoadFunc Image::_tga_mem_loader_func = nullptr; +ImageMemLoadFunc Image::_bmp_mem_loader_func = nullptr; void (*Image::_image_compress_bc_func)(Image *, float, Image::UsedChannels) = nullptr; void (*Image::_image_compress_bptc_func)(Image *, float, Image::UsedChannels) = nullptr; @@ -3141,6 +3142,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("load_jpg_from_buffer", "buffer"), &Image::load_jpg_from_buffer); ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer"), &Image::load_webp_from_buffer); ClassDB::bind_method(D_METHOD("load_tga_from_buffer", "buffer"), &Image::load_tga_from_buffer); + ClassDB::bind_method(D_METHOD("load_bmp_from_buffer", "buffer"), &Image::load_bmp_from_buffer); ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data"); @@ -3473,10 +3475,21 @@ Error Image::load_webp_from_buffer(const Vector &p_array) { } Error Image::load_tga_from_buffer(const Vector &p_array) { - ERR_FAIL_NULL_V_MSG(_tga_mem_loader_func, ERR_UNAVAILABLE, "TGA module was not installed."); + ERR_FAIL_NULL_V_MSG( + _tga_mem_loader_func, + ERR_UNAVAILABLE, + "The TGA module isn't enabled. Recompile the Godot editor or export template binary with the `module_tga_enabled=yes` SCons option."); return _load_from_buffer(p_array, _tga_mem_loader_func); } +Error Image::load_bmp_from_buffer(const Vector &p_array) { + ERR_FAIL_NULL_V_MSG( + _bmp_mem_loader_func, + ERR_UNAVAILABLE, + "The BMP module isn't enabled. Recompile the Godot editor or export template binary with the `module_bmp_enabled=yes` SCons option."); + return _load_from_buffer(p_array, _bmp_mem_loader_func); +} + void Image::convert_rg_to_ra_rgba8() { ERR_FAIL_COND(format != FORMAT_RGBA8); ERR_FAIL_COND(!data.size()); diff --git a/core/image.h b/core/image.h index 06794c7fed5..d5d687b3ee9 100644 --- a/core/image.h +++ b/core/image.h @@ -136,6 +136,7 @@ public: static ImageMemLoadFunc _jpg_mem_loader_func; static ImageMemLoadFunc _webp_mem_loader_func; static ImageMemLoadFunc _tga_mem_loader_func; + static ImageMemLoadFunc _bmp_mem_loader_func; static void (*_image_compress_bc_func)(Image *, float, UsedChannels p_channels); static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, UsedChannels p_channels); @@ -375,6 +376,7 @@ public: Error load_jpg_from_buffer(const Vector &p_array); Error load_webp_from_buffer(const Vector &p_array); Error load_tga_from_buffer(const Vector &p_array); + Error load_bmp_from_buffer(const Vector &p_array); void convert_rg_to_ra_rgba8(); void convert_ra_rgba8_to_rg(); diff --git a/doc/classes/Image.xml b/doc/classes/Image.xml index 20be20db348..3b108468ded 100644 --- a/doc/classes/Image.xml +++ b/doc/classes/Image.xml @@ -346,6 +346,16 @@ Loads an image from file [code]path[/code]. See [url=https://docs.godotengine.org/en/latest/getting_started/workflow/assets/importing_images.html#supported-image-formats]Supported image formats[/url] for a list of supported image formats and limitations. + + + + + + + Loads an image from the binary contents of a BMP file. + [b]Note:[/b] Godot's BMP module doesn't support 16-bit per pixel images. Only 1-bit, 4-bit, 8-bit, 24-bit, and 32-bit per pixel images are supported. + + diff --git a/modules/bmp/image_loader_bmp.cpp b/modules/bmp/image_loader_bmp.cpp index 757afeb9e31..b08970d1107 100644 --- a/modules/bmp/image_loader_bmp.cpp +++ b/modules/bmp/image_loader_bmp.cpp @@ -30,6 +30,8 @@ #include "image_loader_bmp.h" +#include "core/io/file_access_memory.h" + Error ImageLoaderBMP::convert_to_image(Ref p_image, const uint8_t *p_buffer, const uint8_t *p_color_buffer, @@ -293,9 +295,21 @@ Error ImageLoaderBMP::load_image(Ref p_image, FileAccess *f, return err; } -void ImageLoaderBMP::get_recognized_extensions( - List *p_extensions) const { +void ImageLoaderBMP::get_recognized_extensions(List *p_extensions) const { p_extensions->push_back("bmp"); } -ImageLoaderBMP::ImageLoaderBMP() {} +static Ref _bmp_mem_loader_func(const uint8_t *p_bmp, int p_size) { + FileAccessMemory memfile; + Error open_memfile_error = memfile.open_custom(p_bmp, p_size); + ERR_FAIL_COND_V_MSG(open_memfile_error, Ref(), "Could not create memfile for BMP image buffer."); + Ref img; + img.instance(); + Error load_error = ImageLoaderBMP().load_image(img, &memfile, false, 1.0f); + ERR_FAIL_COND_V_MSG(load_error, Ref(), "Failed to load BMP image."); + return img; +} + +ImageLoaderBMP::ImageLoaderBMP() { + Image::_bmp_mem_loader_func = _bmp_mem_loader_func; +} diff --git a/modules/tga/image_loader_tga.cpp b/modules/tga/image_loader_tga.cpp index 1475d24792b..a4a4561fc67 100644 --- a/modules/tga/image_loader_tga.cpp +++ b/modules/tga/image_loader_tga.cpp @@ -313,9 +313,9 @@ void ImageLoaderTGA::get_recognized_extensions(List *p_extensions) const p_extensions->push_back("tga"); } -static Ref _tga_mem_loader_func(const uint8_t *p_png, int p_size) { +static Ref _tga_mem_loader_func(const uint8_t *p_tga, int p_size) { FileAccessMemory memfile; - Error open_memfile_error = memfile.open_custom(p_png, p_size); + Error open_memfile_error = memfile.open_custom(p_tga, p_size); ERR_FAIL_COND_V_MSG(open_memfile_error, Ref(), "Could not create memfile for TGA image buffer."); Ref img; img.instance();