1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

Add support for svg images in the asset lib.

Pixel based image formats are identified by magic numbers. This is not possible with svg therefore svg parsing is tried and if it succeeded the result is used.

WebP and bmp support is added as well. But I could not test it as I am not able to run a local instance of the asset lib and there is no asset using those formats.
This commit is contained in:
HolonProduction
2022-12-19 21:42:43 +01:00
parent c023d41036
commit 66fa776667
3 changed files with 37 additions and 9 deletions

View File

@@ -43,6 +43,11 @@
#include "editor/project_settings_editor.h"
#include "scene/gui/menu_button.h"
#include "modules/modules_enabled.gen.h" // For svg.
#ifdef MODULE_SVG_ENABLED
#include "modules/svg/image_loader_svg.h"
#endif
static inline void setup_http_request(HTTPRequest *request) {
request->set_use_threads(EDITOR_DEF("asset_library/use_threads", true));
@@ -751,13 +756,30 @@ void EditorAssetLibrary::_image_update(bool use_cache, bool final, const PackedB
uint8_t png_signature[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
uint8_t jpg_signature[3] = { 255, 216, 255 };
uint8_t webp_signature[4] = { 82, 73, 70, 70 };
uint8_t bmp_signature[2] = { 66, 77 };
if (r) {
if ((memcmp(&r[0], &png_signature[0], 8) == 0) && Image::_png_mem_loader_func) {
image->copy_internals_from(Image::_png_mem_loader_func(r, len));
} else if ((memcmp(&r[0], &jpg_signature[0], 3) == 0) && Image::_jpg_mem_loader_func) {
image->copy_internals_from(Image::_jpg_mem_loader_func(r, len));
} else if ((memcmp(&r[0], &webp_signature[0], 4) == 0) && Image::_webp_mem_loader_func) {
image->copy_internals_from(Image::_webp_mem_loader_func(r, len));
} else if ((memcmp(&r[0], &bmp_signature[0], 2) == 0) && Image::_bmp_mem_loader_func) {
image->copy_internals_from(Image::_bmp_mem_loader_func(r, len));
}
#ifdef MODULE_SVG_ENABLED
else {
ImageLoaderSVG svg_loader;
Ref<Image> img = Ref<Image>(memnew(Image));
Error err = svg_loader.create_image_from_utf8_buffer(img, image_data, 1.0, false);
if (err == OK) {
image->copy_internals_from(img);
}
}
#endif
}
if (!image->is_empty()) {