Add Image.load_bmp_from_buffer() for run-time BMP image loading

This partially addresses
https://github.com/godotengine/godot-proposals/issues/676.

(cherry picked from commit 0209e3790e)
This commit is contained in:
Hugo Locurcio 2020-10-20 23:25:58 +02:00 committed by Rémi Verschelde
parent 2c6b3074fc
commit 7db872b55a
No known key found for this signature in database
GPG key ID: C3336907360768E1
5 changed files with 41 additions and 6 deletions

View file

@ -2335,6 +2335,7 @@ ImageMemLoadFunc Image::_png_mem_loader_func = NULL;
ImageMemLoadFunc Image::_jpg_mem_loader_func = NULL; ImageMemLoadFunc Image::_jpg_mem_loader_func = NULL;
ImageMemLoadFunc Image::_webp_mem_loader_func = NULL; ImageMemLoadFunc Image::_webp_mem_loader_func = NULL;
ImageMemLoadFunc Image::_tga_mem_loader_func = NULL; ImageMemLoadFunc Image::_tga_mem_loader_func = NULL;
ImageMemLoadFunc Image::_bmp_mem_loader_func = NULL;
void (*Image::_image_compress_bc_func)(Image *, float, Image::CompressSource) = NULL; void (*Image::_image_compress_bc_func)(Image *, float, Image::CompressSource) = NULL;
void (*Image::_image_compress_bptc_func)(Image *, float, Image::CompressSource) = NULL; void (*Image::_image_compress_bptc_func)(Image *, float, Image::CompressSource) = NULL;
@ -2784,6 +2785,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_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_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_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"); ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data");
@ -3104,6 +3106,14 @@ Error Image::load_tga_from_buffer(const PoolVector<uint8_t> &p_array) {
return _load_from_buffer(p_array, _tga_mem_loader_func); return _load_from_buffer(p_array, _tga_mem_loader_func);
} }
Error Image::load_bmp_from_buffer(const PoolVector<uint8_t> &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);
}
Error Image::_load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader) { Error Image::_load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader) {
int buffer_size = p_array.size(); int buffer_size = p_array.size();

View file

@ -132,6 +132,7 @@ public:
static ImageMemLoadFunc _jpg_mem_loader_func; static ImageMemLoadFunc _jpg_mem_loader_func;
static ImageMemLoadFunc _webp_mem_loader_func; static ImageMemLoadFunc _webp_mem_loader_func;
static ImageMemLoadFunc _tga_mem_loader_func; static ImageMemLoadFunc _tga_mem_loader_func;
static ImageMemLoadFunc _bmp_mem_loader_func;
static void (*_image_compress_bc_func)(Image *, float, CompressSource p_source); static void (*_image_compress_bc_func)(Image *, float, CompressSource p_source);
static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, CompressSource p_source); static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, CompressSource p_source);
@ -333,6 +334,7 @@ public:
Error load_jpg_from_buffer(const PoolVector<uint8_t> &p_array); Error load_jpg_from_buffer(const PoolVector<uint8_t> &p_array);
Error load_webp_from_buffer(const PoolVector<uint8_t> &p_array); Error load_webp_from_buffer(const PoolVector<uint8_t> &p_array);
Error load_tga_from_buffer(const PoolVector<uint8_t> &p_array); Error load_tga_from_buffer(const PoolVector<uint8_t> &p_array);
Error load_bmp_from_buffer(const PoolVector<uint8_t> &p_array);
Image(const uint8_t *p_mem_png_jpg, int p_len = -1); Image(const uint8_t *p_mem_png_jpg, int p_len = -1);
Image(const char **p_xpm); Image(const char **p_xpm);

View file

@ -333,6 +333,16 @@
Loads an image from file [code]path[/code]. See [url=https://docs.godotengine.org/en/3.2/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 file [code]path[/code]. See [url=https://docs.godotengine.org/en/3.2/getting_started/workflow/assets/importing_images.html#supported-image-formats]Supported image formats[/url] for a list of supported image formats and limitations.
</description> </description>
</method> </method>
<method name="load_bmp_from_buffer">
<return type="int" enum="Error">
</return>
<argument index="0" name="buffer" type="PoolByteArray">
</argument>
<description>
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.
</description>
</method>
<method name="load_jpg_from_buffer"> <method name="load_jpg_from_buffer">
<return type="int" enum="Error"> <return type="int" enum="Error">
</return> </return>

View file

@ -30,6 +30,8 @@
#include "image_loader_bmp.h" #include "image_loader_bmp.h"
#include "core/io/file_access_memory.h"
Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image, Error ImageLoaderBMP::convert_to_image(Ref<Image> p_image,
const uint8_t *p_buffer, const uint8_t *p_buffer,
const uint8_t *p_color_buffer, const uint8_t *p_color_buffer,
@ -294,10 +296,21 @@ Error ImageLoaderBMP::load_image(Ref<Image> p_image, FileAccess *f,
return err; return err;
} }
void ImageLoaderBMP::get_recognized_extensions( void ImageLoaderBMP::get_recognized_extensions(List<String> *p_extensions) const {
List<String> *p_extensions) const {
p_extensions->push_back("bmp"); p_extensions->push_back("bmp");
} }
ImageLoaderBMP::ImageLoaderBMP() {} static Ref<Image> _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<Image>(), "Could not create memfile for BMP image buffer.");
Ref<Image> img;
img.instance();
Error load_error = ImageLoaderBMP().load_image(img, &memfile, false, 1.0f);
ERR_FAIL_COND_V_MSG(load_error, Ref<Image>(), "Failed to load BMP image.");
return img;
}
ImageLoaderBMP::ImageLoaderBMP() {
Image::_bmp_mem_loader_func = _bmp_mem_loader_func;
}

View file

@ -316,9 +316,9 @@ void ImageLoaderTGA::get_recognized_extensions(List<String> *p_extensions) const
p_extensions->push_back("tga"); p_extensions->push_back("tga");
} }
static Ref<Image> _tga_mem_loader_func(const uint8_t *p_png, int p_size) { static Ref<Image> _tga_mem_loader_func(const uint8_t *p_tga, int p_size) {
FileAccessMemory memfile; 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<Image>(), "Could not create memfile for TGA image buffer."); ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for TGA image buffer.");
Ref<Image> img; Ref<Image> img;
img.instance(); img.instance();