summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2020-06-30 16:40:28 +0200
committerGitHub <noreply@github.com>2020-06-30 16:40:28 +0200
commitf3c5f12e3965b0a3147a9264f5ac42599abca672 (patch)
tree7ee6fdf50369ebd5aeb2bdbb808970a21092cf7d
parent27605769c483ed135808443be70e4ac7f7afa541 (diff)
parent7d4b3e65876af61452a13c23bec1829a0a2de77f (diff)
Merge pull request #38920 from paulherman/tga
Expose loading TGA images in Image.
-rw-r--r--core/image.cpp8
-rw-r--r--core/image.h2
-rw-r--r--modules/tga/image_loader_tga.cpp14
3 files changed, 24 insertions, 0 deletions
diff --git a/core/image.cpp b/core/image.cpp
index 45f10625a9..e2b184fcde 100644
--- a/core/image.cpp
+++ b/core/image.cpp
@@ -30,6 +30,7 @@
#include "image.h"
+#include "core/error_macros.h"
#include "core/hash_map.h"
#include "core/io/image_loader.h"
#include "core/io/resource_loader.h"
@@ -2629,6 +2630,7 @@ void Image::fill(const Color &c) {
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;
void (*Image::_image_compress_bc_func)(Image *, float, Image::UsedChannels) = nullptr;
void (*Image::_image_compress_bptc_func)(Image *, float, Image::UsedChannels) = nullptr;
@@ -3058,6 +3060,7 @@ void Image::_bind_methods() {
ClassDB::bind_method(D_METHOD("load_png_from_buffer", "buffer"), &Image::load_png_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_tga_from_buffer", "buffer"), &Image::load_tga_from_buffer);
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data");
@@ -3389,6 +3392,11 @@ Error Image::load_webp_from_buffer(const Vector<uint8_t> &p_array) {
return _load_from_buffer(p_array, _webp_mem_loader_func);
}
+Error Image::load_tga_from_buffer(const Vector<uint8_t> &p_array) {
+ ERR_FAIL_NULL_V_MSG(_tga_mem_loader_func, ERR_UNAVAILABLE, "TGA module was not installed.");
+ return _load_from_buffer(p_array, _tga_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 53c203998e..711bf5721c 100644
--- a/core/image.h
+++ b/core/image.h
@@ -135,6 +135,7 @@ public:
static ImageMemLoadFunc _png_mem_loader_func;
static ImageMemLoadFunc _jpg_mem_loader_func;
static ImageMemLoadFunc _webp_mem_loader_func;
+ static ImageMemLoadFunc _tga_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);
@@ -360,6 +361,7 @@ public:
Error load_png_from_buffer(const Vector<uint8_t> &p_array);
Error load_jpg_from_buffer(const Vector<uint8_t> &p_array);
Error load_webp_from_buffer(const Vector<uint8_t> &p_array);
+ Error load_tga_from_buffer(const Vector<uint8_t> &p_array);
void convert_rg_to_ra_rgba8();
void convert_ra_rgba8_to_rg();
diff --git a/modules/tga/image_loader_tga.cpp b/modules/tga/image_loader_tga.cpp
index ce889a4928..1475d24792 100644
--- a/modules/tga/image_loader_tga.cpp
+++ b/modules/tga/image_loader_tga.cpp
@@ -30,6 +30,8 @@
#include "image_loader_tga.h"
+#include "core/error_macros.h"
+#include "core/io/file_access_memory.h"
#include "core/os/os.h"
#include "core/print_string.h"
@@ -311,5 +313,17 @@ void ImageLoaderTGA::get_recognized_extensions(List<String> *p_extensions) const
p_extensions->push_back("tga");
}
+static Ref<Image> _tga_mem_loader_func(const uint8_t *p_png, int p_size) {
+ FileAccessMemory memfile;
+ Error open_memfile_error = memfile.open_custom(p_png, p_size);
+ ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for TGA image buffer.");
+ Ref<Image> img;
+ img.instance();
+ Error load_error = ImageLoaderTGA().load_image(img, &memfile, false, 1.0f);
+ ERR_FAIL_COND_V_MSG(load_error, Ref<Image>(), "Failed to load TGA image.");
+ return img;
+}
+
ImageLoaderTGA::ImageLoaderTGA() {
+ Image::_tga_mem_loader_func = _tga_mem_loader_func;
}