From 0209e3790e4ab984b811f2994947ae71eef69b82 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Tue, 20 Oct 2020 23:25:58 +0200 Subject: Add `Image.load_bmp_from_buffer()` for run-time BMP image loading This partially addresses https://github.com/godotengine/godot-proposals/issues/676. --- modules/bmp/image_loader_bmp.cpp | 20 +++++++++++++++++--- modules/tga/image_loader_tga.cpp | 4 ++-- 2 files changed, 19 insertions(+), 5 deletions(-) (limited to 'modules') diff --git a/modules/bmp/image_loader_bmp.cpp b/modules/bmp/image_loader_bmp.cpp index 757afeb9e3..b08970d110 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 1475d24792..a4a4561fc6 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(); -- cgit v1.2.3