Skip to content

Commit

Permalink
Add APNG import
Browse files Browse the repository at this point in the history
Add APNG file loading
Add APNG image load test assertions
Add APNG patch to libpng
  • Loading branch information
Spartan322 committed Jan 21, 2025
1 parent 82cbf2e commit b9c55f7
Show file tree
Hide file tree
Showing 25 changed files with 5,019 additions and 4 deletions.
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
12 changes: 12 additions & 0 deletions core/io/image_frames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "core/io/resource_loader.h"
#include "core/object/class_db.h"

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

void ImageFrames::set_frame_count(int p_frames) {
Expand Down Expand Up @@ -115,6 +116,10 @@ Ref<ImageFrames> ImageFrames::load_from_file(const String &p_path) {
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_gif_from_buffer(const PackedByteArray &p_array, int p_max_frames) {
ERR_FAIL_NULL_V_MSG(
_gif_mem_loader_func,
Expand All @@ -123,6 +128,12 @@ Error ImageFrames::load_gif_from_buffer(const PackedByteArray &p_array, int p_ma
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++) {
Expand Down Expand Up @@ -156,6 +167,7 @@ void ImageFrames::_bind_methods() {
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_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");
Expand Down
4 changes: 4 additions & 0 deletions core/io/image_frames.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ImageFrames : public Resource {
GDCLASS(ImageFrames, Resource);

public:
static ImageFramesMemLoadFunc _apng_mem_loader_func;
static ImageFramesMemLoadFunc _gif_mem_loader_func;

private:
Expand Down Expand Up @@ -72,13 +73,16 @@ class ImageFrames : public Resource {
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_gif_from_buffer(const PackedByteArray &p_array, int p_max_frames = 0);

void copy_internals_from(const Ref<ImageFrames> &p_frames) {
Expand Down
11 changes: 10 additions & 1 deletion doc/classes/ImageFrames.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<description>
A container of [Image]s used to load and arrange a sequence of frames. Each frame can specify a delay for animated images.
Can be used to load animated image formats externally.
Supported animated image formats are [url=https://www.w3.org/Graphics/GIF/spec-gif89a.txt]GIF[/url] ([code].gif[/code]) and any format exposed via a GDExtension plugin.
Supported animated image formats are [url=https://www.w3.org/Graphics/GIF/spec-gif89a.txt]GIF[/url] ([code].gif[/code]), [url=https://wiki.mozilla.org/APNG_Specification]APNG[/url] ([code].png[/code] and [code].apng[/code]), and any format exposed via a GDExtension plugin.
An [ImageTexture] is not meant to be operated from within the editor interface directly, and is mostly useful for rendering images on screen dynamically via code. If you need to generate images procedurally from within the editor, consider saving and importing images as custom texture resources implementing a new [EditorImportPlugin].
</description>
<tutorials>
Expand Down Expand Up @@ -57,6 +57,15 @@
[/codeblock]
</description>
</method>
<method name="load_apng_from_buffer">
<return type="int" enum="Error" />
<param index="0" name="buffer" type="PackedByteArray" />
<param index="1" name="max_frames" type="int" />
<description>
Loads an image from the binary contents of an APNG file.
[b]Note:[/b] This function will read standard PNG files just like [method Image.load_png_from_buffer]. If libpng is not compiled with support for reading APNG files, APNG files are treated as PNG files.
</description>
</method>
<method name="load_from_file" qualifiers="static">
<return type="ImageFrames" />
<param index="0" name="path" type="String" />
Expand Down
70 changes: 70 additions & 0 deletions drivers/png/image_frames_loader_png.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**************************************************************************/
/* image_frames_loader_png.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_loader_png.h"

#include "drivers/png/png_driver_common.h"

Error ImageFramesLoaderPNG::load_image_frames(Ref<ImageFrames> p_image, Ref<FileAccess> f, BitField<ImageFramesFormatLoader::LoaderFlags> p_flags, float p_scale, int p_max_frames) {
const uint64_t buffer_size = f->get_length();
Vector<uint8_t> file_buffer;
Error err = file_buffer.resize(buffer_size);
if (err) {
return err;
}
{
uint8_t *writer = file_buffer.ptrw();
f->get_buffer(writer, buffer_size);
}
const uint8_t *reader = file_buffer.ptr();
return PNGDriverCommon::apng_to_image_frames(reader, buffer_size, p_flags & FLAG_FORCE_LINEAR, p_max_frames, p_image);
}

void ImageFramesLoaderPNG::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("png");
p_extensions->push_back("apng");
}

Ref<ImageFrames> ImageFramesLoaderPNG::load_mem_apng(const uint8_t *p_png, int p_size, int p_max_frames) {
Ref<ImageFrames> img_frames;
img_frames.instantiate();

// the value of p_force_linear does not matter since it only applies to 16 bit
Error err = PNGDriverCommon::apng_to_image_frames(p_png, p_size, false, p_max_frames, img_frames);
ERR_FAIL_COND_V(err, Ref<ImageFrames>());

return img_frames;
}

ImageFramesLoaderPNG::ImageFramesLoaderPNG() {
ImageFrames::_apng_mem_loader_func = load_mem_apng;
}
48 changes: 48 additions & 0 deletions drivers/png/image_frames_loader_png.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**************************************************************************/
/* image_frames_loader_png.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_LOADER_PNG_H
#define IMAGE_FRAMES_LOADER_PNG_H

#include "core/io/image_frames_loader.h"

class ImageFramesLoaderPNG : public ImageFramesFormatLoader {
private:
static Ref<ImageFrames> load_mem_apng(const uint8_t *p_png, int p_size, int p_max_frames);

public:
virtual Error load_image_frames(Ref<ImageFrames> p_image, Ref<FileAccess> f, BitField<ImageFramesFormatLoader::LoaderFlags> p_flags, float p_scale = 1.0, int p_max_frames = 0);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageFramesLoaderPNG();
};

#endif // IMAGE_FRAMES_LOADER_PNG_H
Loading

0 comments on commit b9c55f7

Please sign in to comment.