Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add animated WebP import #940

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ Comment: The FreeType Project
Copyright: 1996-2023, David Turner, Robert Wilhelm, and Werner Lemberg.
License: FTL

Files: thirdparty/giflib/*
Comment: giflib
Copyright: 1997-2024, Eric S. Raymond
License: Expat

Files: thirdparty/glad/*
Comment: glad
Copyright: 2013-2022, David Herberth
Expand Down
1 change: 1 addition & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ if env.msvc and not methods.using_clang(env): # MSVC
"/wd4514", # C4514 (unreferenced inline function has been removed)
"/wd4714", # C4714 (function marked as __forceinline not inlined)
"/wd4820", # C4820 (padding added after construct)
"/wd4611", # C4611 (interaction between '_setjmp' and C++ object destruction is non-portable): libpng uses setjmp use for error handling
]

if env["warnings"] == "extra":
Expand Down
10 changes: 10 additions & 0 deletions core/io/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ ImageMemLoadFunc Image::_tga_mem_loader_func = nullptr;
ImageMemLoadFunc Image::_bmp_mem_loader_func = nullptr;
ScalableImageMemLoadFunc Image::_svg_scalable_mem_loader_func = nullptr;
ImageMemLoadFunc Image::_ktx_mem_loader_func = nullptr;
ImageMemLoadFunc Image::_gif_mem_loader_func = nullptr;

// External VRAM compression function pointers.

Expand Down Expand Up @@ -3579,6 +3580,7 @@ void Image::_bind_methods() {
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);
ClassDB::bind_method(D_METHOD("load_ktx_from_buffer", "buffer"), &Image::load_ktx_from_buffer);
ClassDB::bind_method(D_METHOD("load_gif_from_buffer", "buffer"), &Image::load_gif_from_buffer);

ClassDB::bind_method(D_METHOD("load_svg_from_buffer", "buffer", "scale"), &Image::load_svg_from_buffer, DEFVAL(1.0));
ClassDB::bind_method(D_METHOD("load_svg_from_string", "svg_str", "scale"), &Image::load_svg_from_string, DEFVAL(1.0));
Expand Down Expand Up @@ -4104,6 +4106,14 @@ Error Image::load_ktx_from_buffer(const Vector<uint8_t> &p_array) {
return _load_from_buffer(p_array, _ktx_mem_loader_func);
}

Error Image::load_gif_from_buffer(const Vector<uint8_t> &p_array) {
ERR_FAIL_NULL_V_MSG(
_gif_mem_loader_func,
ERR_UNAVAILABLE,
"The GIF module isn't enabled. Recompile the Redot editor or export template binary with the `module_gif_enabled=yes` SCons option.");
return _load_from_buffer(p_array, _gif_mem_loader_func);
}

void Image::convert_rg_to_ra_rgba8() {
ERR_FAIL_COND(format != FORMAT_RGBA8);
ERR_FAIL_COND(data.is_empty());
Expand Down
2 changes: 2 additions & 0 deletions core/io/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class Image : public Resource {
static ImageMemLoadFunc _bmp_mem_loader_func;
static ScalableImageMemLoadFunc _svg_scalable_mem_loader_func;
static ImageMemLoadFunc _ktx_mem_loader_func;
static ImageMemLoadFunc _gif_mem_loader_func;

// External VRAM compression function pointers.

Expand Down Expand Up @@ -405,6 +406,7 @@ class Image : public Resource {
Error load_tga_from_buffer(const Vector<uint8_t> &p_array);
Error load_bmp_from_buffer(const Vector<uint8_t> &p_array);
Error load_ktx_from_buffer(const Vector<uint8_t> &p_array);
Error load_gif_from_buffer(const Vector<uint8_t> &p_array);

Error load_svg_from_buffer(const Vector<uint8_t> &p_array, float scale = 1.0);
Error load_svg_from_string(const String &p_svg_str, float scale = 1.0);
Expand Down
195 changes: 195 additions & 0 deletions core/io/image_frames.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/**************************************************************************/
/* image_frames.cpp */
/**************************************************************************/
/* This file is part of: */
/* REDOT ENGINE */
/* https://redotengine.org */
/**************************************************************************/
/* Copyright (c) 2024-present Redot Engine contributors */
/* (see REDOT_AUTHORS.md) */
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "image_frames.h"
#include "core/error/error_macros.h"
#include "core/io/image_frames_loader.h"
#include "core/io/resource_loader.h"
#include "core/object/class_db.h"

ImageFramesMemLoadFunc ImageFrames::_apng_mem_loader_func = nullptr;
ImageFramesMemLoadFunc ImageFrames::_webp_mem_loader_func = nullptr;
ImageFramesMemLoadFunc ImageFrames::_gif_mem_loader_func = nullptr;

void ImageFrames::set_frame_count(int p_frames) {
ERR_FAIL_COND(p_frames < 0);

frames.resize(p_frames);
}

int ImageFrames::get_frame_count() const {
return frames.size();
}

void ImageFrames::set_frame_image(int p_frame, Ref<Image> p_image) {
ERR_FAIL_INDEX(p_frame, frames.size());

frames.write[p_frame].image = p_image;
}

Ref<Image> ImageFrames::get_frame_image(int p_frame) const {
ERR_FAIL_INDEX_V(p_frame, frames.size(), Ref<Image>());

return frames[p_frame].image;
}

void ImageFrames::set_frame_delay(int p_frame, float p_delay) {
ERR_FAIL_INDEX(p_frame, frames.size());

frames.write[p_frame].delay = p_delay;
}

float ImageFrames::get_frame_delay(int p_frame) const {
ERR_FAIL_INDEX_V(p_frame, frames.size(), 0);

return frames[p_frame].delay;
}

void ImageFrames::set_loop_count(int p_loop) {
ERR_FAIL_COND(p_loop >= 0);

loop_count = p_loop;
}

int ImageFrames::get_loop_count() const {
return loop_count;
}

bool ImageFrames::is_empty() const {
return get_frame_count() == 0;
}

Error ImageFrames::_load_from_buffer(const Vector<uint8_t> &p_array, ImageFramesMemLoadFunc p_loader, int p_max_frames) {
int buffer_size = p_array.size();

ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER);
ERR_FAIL_NULL_V(p_loader, ERR_INVALID_PARAMETER);

const uint8_t *r = p_array.ptr();

Ref<ImageFrames> img_frames = p_loader(r, buffer_size, p_max_frames);
ERR_FAIL_COND_V(!img_frames.is_valid(), ERR_PARSE_ERROR);

copy_internals_from(img_frames);

return OK;
}

Error ImageFrames::load(const String &p_path) {
#ifdef DEBUG_ENABLED
if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) {
WARN_PRINT("Loaded resource as image frames file, this will not work on export: '" + p_path + "'. Instead, import the image frames file as an ImageFrames resource and load it normally as a resource.");
}
#endif
return ImageFramesLoader::load_image_frames(p_path, this);
}

Ref<ImageFrames> ImageFrames::load_from_file(const String &p_path) {
#ifdef DEBUG_ENABLED
if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) {
WARN_PRINT("Loaded resource as image frames file, this will not work on export: '" + p_path + "'. Instead, import the image frames file as an ImageFrames resource and load it normally as a resource.");
}
#endif
Ref<ImageFrames> img_frames;
img_frames.instantiate();
Error err = ImageFramesLoader::load_image_frames(p_path, img_frames);
if (err != OK) {
ERR_FAIL_V_MSG(Ref<ImageFrames>(), vformat("Failed to load image frames. Error %d", err));
}
return img_frames;
}

Error ImageFrames::load_apng_from_buffer(const PackedByteArray &p_array, int p_max_frames) {
return _load_from_buffer(p_array, _apng_mem_loader_func, p_max_frames);
}

Error ImageFrames::load_webp_from_buffer(const PackedByteArray &p_array, int p_max_frames) {
return _load_from_buffer(p_array, _webp_mem_loader_func, p_max_frames);
}

Error ImageFrames::load_gif_from_buffer(const PackedByteArray &p_array, int p_max_frames) {
ERR_FAIL_NULL_V_MSG(
_gif_mem_loader_func,
ERR_UNAVAILABLE,
"The GIF module isn't enabled. Recompile the Redot editor or export template binary with the `module_gif_enabled=yes` SCons option.");
return _load_from_buffer(p_array, _gif_mem_loader_func, p_max_frames);
}

ImageFrames::ImageFrames(const uint8_t *p_mem_apng, int p_len) {
if (_apng_mem_loader_func) {
copy_internals_from(_apng_mem_loader_func(p_mem_apng, p_len, 0));
}
}

ImageFrames::ImageFrames(const Vector<Ref<Image>> &p_images, float p_delay) {
set_frame_count(p_images.size());
for (uint32_t index = 0; index < p_images.size(); index++) {
ERR_CONTINUE(p_images[index].is_null());
frames.write[index] = Frame{ p_images[index], p_delay };
}
}

ImageFrames::ImageFrames(const Vector<Ref<Image>> &p_images, const Vector<float> &p_delays) {
ERR_FAIL_COND_MSG(p_images.size() != p_delays.size(), vformat("The Image count (%d) does not match the delays count (%d).", p_images.size(), p_delays.size()));

set_frame_count(p_images.size());
for (uint32_t index = 0; index < p_images.size(); index++) {
ERR_CONTINUE(p_images[index].is_null());
frames.write[index] = Frame{ p_images[index], p_delays[index] };
}
}

void ImageFrames::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_frame_count", "frames"), &ImageFrames::set_frame_count);
ClassDB::bind_method(D_METHOD("get_frame_count"), &ImageFrames::get_frame_count);

ClassDB::bind_method(D_METHOD("set_frame_image", "frame", "image"), &ImageFrames::set_frame_image);
ClassDB::bind_method(D_METHOD("get_frame_image", "frame"), &ImageFrames::get_frame_image);

ClassDB::bind_method(D_METHOD("set_frame_delay", "frame", "delay"), &ImageFrames::set_frame_delay);
ClassDB::bind_method(D_METHOD("get_frame_delay", "frame"), &ImageFrames::get_frame_delay);

ClassDB::bind_method(D_METHOD("set_loop_count", "loop"), &ImageFrames::set_loop_count);
ClassDB::bind_method(D_METHOD("get_loop_count"), &ImageFrames::get_loop_count);

ClassDB::bind_method(D_METHOD("is_empty"), &ImageFrames::is_empty);

ClassDB::bind_method(D_METHOD("load", "path"), &ImageFrames::load);
ClassDB::bind_static_method("ImageFrames", D_METHOD("load_from_file", "path"), &ImageFrames::load_from_file);

ClassDB::bind_method(D_METHOD("load_apng_from_buffer", "buffer", "max_frames"), &ImageFrames::load_apng_from_buffer);
ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer", "max_frames"), &ImageFrames::load_webp_from_buffer);
ClassDB::bind_method(D_METHOD("load_gif_from_buffer", "buffer", "max_frames"), &ImageFrames::load_gif_from_buffer);

ADD_PROPERTY(PropertyInfo(Variant::INT, "frame_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_frame_count", "get_frame_count");
ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_loop_count", "get_loop_count");
}
100 changes: 100 additions & 0 deletions core/io/image_frames.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**************************************************************************/
/* image_frames.h */
/**************************************************************************/
/* This file is part of: */
/* REDOT ENGINE */
/* https://redotengine.org */
/**************************************************************************/
/* Copyright (c) 2024-present Redot Engine contributors */
/* (see REDOT_AUTHORS.md) */
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef IMAGE_FRAMES_H
#define IMAGE_FRAMES_H

#include "core/io/image.h"
#include "core/io/resource.h"
#include "core/variant/variant.h"

class ImageFrames;
typedef Ref<ImageFrames> (*ImageFramesMemLoadFunc)(const uint8_t *p_png, int p_size, int p_max_frames);

class ImageFrames : public Resource {
GDCLASS(ImageFrames, Resource);

public:
static ImageFramesMemLoadFunc _apng_mem_loader_func;
static ImageFramesMemLoadFunc _webp_mem_loader_func;
static ImageFramesMemLoadFunc _gif_mem_loader_func;

private:
struct Frame {
Ref<Image> image;
float delay = 1.0;
};

Vector<Frame> frames;
int loop_count = 0;

Error _load_from_buffer(const Vector<uint8_t> &p_array, ImageFramesMemLoadFunc p_loader, int p_max_frames);

protected:
static void _bind_methods();

public:
void set_frame_count(int p_frames);
int get_frame_count() const;

void set_frame_image(int p_frame, Ref<Image> p_image);
Ref<Image> get_frame_image(int p_frame) const;

void set_frame_delay(int p_frame, float p_delay);
float get_frame_delay(int p_frame) const;

void set_loop_count(int p_loop);
int get_loop_count() const;

bool is_empty() const;

ImageFrames() = default; // Create empty image frames.
ImageFrames(const uint8_t *p_mem_apng, int p_len);
ImageFrames(const Vector<Ref<Image>> &p_images, float p_delay = 1.0); // Import images from an image vector and delay.
ImageFrames(const Vector<Ref<Image>> &p_images, const Vector<float> &p_delays); // Import images from an image vector and delay vector.

~ImageFrames() {}

Error load(const String &p_path);
static Ref<ImageFrames> load_from_file(const String &p_path);

Error load_apng_from_buffer(const PackedByteArray &p_array, int p_max_frames = 0);
Error load_webp_from_buffer(const PackedByteArray &p_array, int p_max_frames = 0);
Error load_gif_from_buffer(const PackedByteArray &p_array, int p_max_frames = 0);

void copy_internals_from(const Ref<ImageFrames> &p_frames) {
ERR_FAIL_COND_MSG(p_frames.is_null(), "Cannot copy image internals: invalid ImageFrames object.");
frames = p_frames->frames;
}
};

#endif // IMAGE_FRAMES_H
Loading