diff options
author | Aaron Franke <arnfranke@yahoo.com> | 2022-06-04 16:05:55 -0500 |
---|---|---|
committer | Aaron Franke <arnfranke@yahoo.com> | 2022-06-21 08:27:51 -0500 |
commit | 8f05bd97b5c03510937270af23cc9f513b33be4b (patch) | |
tree | 6796ea2f98bf2f859aa1c1adbc629e051aea5a93 /core | |
parent | fdfbaf9f0f3fb8cbf640f0d409257039c14f314f (diff) |
Add support for saving WebP images
Diffstat (limited to 'core')
-rw-r--r-- | core/io/image.cpp | 23 | ||||
-rw-r--r-- | core/io/image.h | 6 |
2 files changed, 29 insertions, 0 deletions
diff --git a/core/io/image.cpp b/core/io/image.cpp index 6585fd9adf..a945d3e6cd 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -87,6 +87,9 @@ SaveEXRFunc Image::save_exr_func = nullptr; SavePNGBufferFunc Image::save_png_buffer_func = nullptr; SaveJPGBufferFunc Image::save_jpg_buffer_func = nullptr; +SaveWebPFunc Image::save_webp_func = nullptr; +SaveWebPBufferFunc Image::save_webp_buffer_func = nullptr; + void Image::_put_pixelb(int p_x, int p_y, uint32_t p_pixel_size, uint8_t *p_data, const uint8_t *p_pixel) { uint32_t ofs = (p_y * width + p_x) * p_pixel_size; memcpy(p_data + ofs, p_pixel, p_pixel_size); @@ -2320,6 +2323,24 @@ Error Image::save_exr(const String &p_path, bool p_grayscale) const { return save_exr_func(p_path, Ref<Image>((Image *)this), p_grayscale); } +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; + } + ERR_FAIL_COND_V_MSG(p_lossy && !(0.0f <= p_quality && p_quality <= 1.0f), ERR_INVALID_PARAMETER, "The WebP lossy quality was set to " + rtos(p_quality) + ", which is not valid. WebP lossy quality must be between 0.0 and 1.0 (inclusive)."); + + return save_webp_func(p_path, Ref<Image>((Image *)this), p_lossy, p_quality); +} + +Vector<uint8_t> Image::save_webp_to_buffer(const bool p_lossy, const float p_quality) const { + if (save_webp_buffer_func == nullptr) { + return Vector<uint8_t>(); + } + ERR_FAIL_COND_V_MSG(p_lossy && !(0.0f <= p_quality && p_quality <= 1.0f), Vector<uint8_t>(), "The WebP lossy quality was set to " + rtos(p_quality) + ", which is not valid. WebP lossy quality must be between 0.0 and 1.0 (inclusive)."); + + return save_webp_buffer_func(Ref<Image>((Image *)this), p_lossy, p_quality); +} + int Image::get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps) { int mm; return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps ? -1 : 0); @@ -3159,6 +3180,8 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("save_jpg", "path", "quality"), &Image::save_jpg, DEFVAL(0.75)); 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_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)); ClassDB::bind_method(D_METHOD("detect_alpha"), &Image::detect_alpha); ClassDB::bind_method(D_METHOD("is_invisible"), &Image::is_invisible); diff --git a/core/io/image.h b/core/io/image.h index 88b80dc984..229103f792 100644 --- a/core/io/image.h +++ b/core/io/image.h @@ -48,6 +48,8 @@ typedef Vector<uint8_t> (*SavePNGBufferFunc)(const Ref<Image> &p_img); typedef Error (*SaveJPGFunc)(const String &p_path, const Ref<Image> &p_img, float p_quality); typedef Vector<uint8_t> (*SaveJPGBufferFunc)(const Ref<Image> &p_img, float p_quality); typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size); +typedef Error (*SaveWebPFunc)(const String &p_path, const Ref<Image> &p_img, const bool p_lossy, const float p_quality); +typedef Vector<uint8_t> (*SaveWebPBufferFunc)(const Ref<Image> &p_img, const bool p_lossy, const float p_quality); typedef Error (*SaveEXRFunc)(const String &p_path, const Ref<Image> &p_img, bool p_grayscale); @@ -60,6 +62,8 @@ public: static SaveEXRFunc save_exr_func; static SavePNGBufferFunc save_png_buffer_func; static SaveJPGBufferFunc save_jpg_buffer_func; + static SaveWebPFunc save_webp_func; + static SaveWebPBufferFunc save_webp_buffer_func; enum { MAX_WIDTH = (1 << 24), // force a limit somehow @@ -289,6 +293,8 @@ public: Vector<uint8_t> save_png_to_buffer() const; Vector<uint8_t> save_jpg_to_buffer(float p_quality = 0.75) const; Error save_exr(const String &p_path, bool p_grayscale) 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; void create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { create(p_width, p_height, p_use_mipmaps, p_format); |