Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/v-sekai-godot/vsk-save-dds-4.4'…
Browse files Browse the repository at this point in the history
… into groups-staging-4.4
  • Loading branch information
fire committed Jan 25, 2025
2 parents 4e71255 + 5387e1a commit 157b5aa
Show file tree
Hide file tree
Showing 10 changed files with 816 additions and 80 deletions.
30 changes: 29 additions & 1 deletion core/io/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ SavePNGFunc Image::save_png_func = nullptr;
SaveJPGFunc Image::save_jpg_func = nullptr;
SaveEXRFunc Image::save_exr_func = nullptr;
SaveWebPFunc Image::save_webp_func = nullptr;
SaveDDSFunc Image::save_dds_func = nullptr;

SavePNGBufferFunc Image::save_png_buffer_func = nullptr;
SaveJPGBufferFunc Image::save_jpg_buffer_func = nullptr;
SaveEXRBufferFunc Image::save_exr_buffer_func = nullptr;
SaveWebPBufferFunc Image::save_webp_buffer_func = nullptr;
SaveDDSBufferFunc Image::save_dds_buffer_func = nullptr;

// External loader function pointers.

Expand All @@ -105,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::_dds_mem_loader_func = nullptr;

// External VRAM compression function pointers.

Expand Down Expand Up @@ -2587,7 +2590,6 @@ Vector<uint8_t> Image::save_jpg_to_buffer(float p_quality) const {

return save_jpg_buffer_func(Ref<Image>((Image *)this), p_quality);
}

Error Image::save_exr(const String &p_path, bool p_grayscale) const {
if (save_exr_func == nullptr) {
return ERR_UNAVAILABLE;
Expand All @@ -2603,6 +2605,21 @@ Vector<uint8_t> Image::save_exr_to_buffer(bool p_grayscale) const {
return save_exr_buffer_func(Ref<Image>((Image *)this), p_grayscale);
}

Error Image::save_dds(const String &p_path) const {
if (save_dds_func == nullptr) {
return ERR_UNAVAILABLE;
}

return save_dds_func(p_path, Ref<Image>((Image *)this));
}

Vector<uint8_t> Image::save_dds_to_buffer() const {
if (save_dds_buffer_func == nullptr) {
return Vector<uint8_t>();
}
return save_dds_buffer_func(Ref<Image>((Image *)this));
}

Error Image::save_webp(const String &p_path, const bool p_lossy, const float p_quality) const {
if (save_webp_func == nullptr) {
return ERR_UNAVAILABLE;
Expand Down Expand Up @@ -3524,6 +3541,9 @@ void Image::_bind_methods() {
ClassDB::bind_method(D_METHOD("save_jpg_to_buffer", "quality"), &Image::save_jpg_to_buffer, DEFVAL(0.75));
ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale"), &Image::save_exr, DEFVAL(false));
ClassDB::bind_method(D_METHOD("save_exr_to_buffer", "grayscale"), &Image::save_exr_to_buffer, DEFVAL(false));
ClassDB::bind_method(D_METHOD("save_dds", "path"), &Image::save_dds);
ClassDB::bind_method(D_METHOD("save_dds_to_buffer"), &Image::save_dds_to_buffer);

ClassDB::bind_method(D_METHOD("save_webp", "path", "lossy", "quality"), &Image::save_webp, DEFVAL(false), DEFVAL(0.75f));
ClassDB::bind_method(D_METHOD("save_webp_to_buffer", "lossy", "quality"), &Image::save_webp_to_buffer, DEFVAL(false), DEFVAL(0.75f));

Expand Down Expand Up @@ -4072,6 +4092,14 @@ Error Image::load_bmp_from_buffer(const Vector<uint8_t> &p_array) {
return _load_from_buffer(p_array, _bmp_mem_loader_func);
}

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

Error Image::load_svg_from_buffer(const Vector<uint8_t> &p_array, float scale) {
ERR_FAIL_NULL_V_MSG(
_svg_scalable_mem_loader_func,
Expand Down
9 changes: 9 additions & 0 deletions core/io/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ typedef Vector<uint8_t> (*SaveWebPBufferFunc)(const Ref<Image> &p_img, const boo
typedef Error (*SaveEXRFunc)(const String &p_path, const Ref<Image> &p_img, bool p_grayscale);
typedef Vector<uint8_t> (*SaveEXRBufferFunc)(const Ref<Image> &p_img, bool p_grayscale);

typedef Error (*SaveDDSFunc)(const String &p_path, const Ref<Image> &p_img);
typedef Vector<uint8_t> (*SaveDDSBufferFunc)(const Ref<Image> &p_img);

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

Expand Down Expand Up @@ -186,10 +189,12 @@ class Image : public Resource {
static SaveJPGFunc save_jpg_func;
static SaveEXRFunc save_exr_func;
static SaveWebPFunc save_webp_func;
static SaveDDSFunc save_dds_func;
static SavePNGBufferFunc save_png_buffer_func;
static SaveEXRBufferFunc save_exr_buffer_func;
static SaveJPGBufferFunc save_jpg_buffer_func;
static SaveWebPBufferFunc save_webp_buffer_func;
static SaveDDSBufferFunc save_dds_buffer_func;

// External loader function pointers.

Expand All @@ -201,6 +206,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 _dds_mem_loader_func;

// External VRAM compression function pointers.

Expand Down Expand Up @@ -334,9 +340,11 @@ class Image : public Resource {
static Ref<Image> load_from_file(const String &p_path);
Error save_png(const String &p_path) const;
Error save_jpg(const String &p_path, float p_quality = 0.75) const;
Error save_dds(const String &p_path) const;
Vector<uint8_t> save_png_to_buffer() const;
Vector<uint8_t> save_jpg_to_buffer(float p_quality = 0.75) const;
Vector<uint8_t> save_exr_to_buffer(bool p_grayscale = false) const;
Vector<uint8_t> save_dds_to_buffer() const;
Error save_exr(const String &p_path, bool p_grayscale = false) const;
Error save_webp(const String &p_path, const bool p_lossy = false, const float p_quality = 0.75f) const;
Vector<uint8_t> save_webp_to_buffer(const bool p_lossy = false, const float p_quality = 0.75f) const;
Expand Down Expand Up @@ -403,6 +411,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_dds_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
15 changes: 15 additions & 0 deletions doc/classes/Image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,21 @@
Rotates the image by [code]180[/code] degrees. The width and height of the image must be greater than [code]1[/code].
</description>
</method>
<method name="save_dds" qualifiers="const">
<return type="int" enum="Error" />
<param index="0" name="path" type="String" />
<description>
Saves the image as a DDS (DirectDraw Surface) file to [param path]. DDS is a container format that can store textures in various compression formats, such as DXT1, DXT5, or BC7. This function will return [constant ERR_UNAVAILABLE] if Godot was compiled without the DDS module.
[b]Note:[/b] The DDS module may be disabled in certain builds, which means [method save_dds] will return [constant ERR_UNAVAILABLE] when it is called from an exported project.
</description>
</method>
<method name="save_dds_to_buffer" qualifiers="const">
<return type="PackedByteArray" />
<description>
Saves the image as a DDS (DirectDraw Surface) file to a byte array. DDS is a container format that can store textures in various compression formats, such as DXT1, DXT5, or BC7. This function will return an empty byte array if Godot was compiled without the DDS module.
[b]Note:[/b] The DDS module may be disabled in certain builds, which means [method save_dds_to_buffer] will return an empty byte array when it is called from an exported project.
</description>
</method>
<method name="save_exr" qualifiers="const">
<return type="int" enum="Error" />
<param index="0" name="path" type="String" />
Expand Down
Loading

0 comments on commit 157b5aa

Please sign in to comment.