Skip to content

Commit c18d0f2

Browse files
authored
Merge pull request #61770 from aaronfranke/webp
2 parents f3bf033 + 8f05bd9 commit c18d0f2

File tree

10 files changed

+441
-177
lines changed

10 files changed

+441
-177
lines changed

core/io/image.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ SaveEXRFunc Image::save_exr_func = nullptr;
8787
SavePNGBufferFunc Image::save_png_buffer_func = nullptr;
8888
SaveJPGBufferFunc Image::save_jpg_buffer_func = nullptr;
8989

90+
SaveWebPFunc Image::save_webp_func = nullptr;
91+
SaveWebPBufferFunc Image::save_webp_buffer_func = nullptr;
92+
9093
void Image::_put_pixelb(int p_x, int p_y, uint32_t p_pixel_size, uint8_t *p_data, const uint8_t *p_pixel) {
9194
uint32_t ofs = (p_y * width + p_x) * p_pixel_size;
9295
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 {
23202323
return save_exr_func(p_path, Ref<Image>((Image *)this), p_grayscale);
23212324
}
23222325

2326+
Error Image::save_webp(const String &p_path, const bool p_lossy, const float p_quality) const {
2327+
if (save_webp_func == nullptr) {
2328+
return ERR_UNAVAILABLE;
2329+
}
2330+
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).");
2331+
2332+
return save_webp_func(p_path, Ref<Image>((Image *)this), p_lossy, p_quality);
2333+
}
2334+
2335+
Vector<uint8_t> Image::save_webp_to_buffer(const bool p_lossy, const float p_quality) const {
2336+
if (save_webp_buffer_func == nullptr) {
2337+
return Vector<uint8_t>();
2338+
}
2339+
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).");
2340+
2341+
return save_webp_buffer_func(Ref<Image>((Image *)this), p_lossy, p_quality);
2342+
}
2343+
23232344
int Image::get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps) {
23242345
int mm;
23252346
return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps ? -1 : 0);
@@ -3159,6 +3180,8 @@ void Image::_bind_methods() {
31593180
ClassDB::bind_method(D_METHOD("save_jpg", "path", "quality"), &Image::save_jpg, DEFVAL(0.75));
31603181
ClassDB::bind_method(D_METHOD("save_jpg_to_buffer", "quality"), &Image::save_jpg_to_buffer, DEFVAL(0.75));
31613182
ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale"), &Image::save_exr, DEFVAL(false));
3183+
ClassDB::bind_method(D_METHOD("save_webp", "path", "lossy", "quality"), &Image::save_webp, DEFVAL(false), DEFVAL(0.75f));
3184+
ClassDB::bind_method(D_METHOD("save_webp_to_buffer", "lossy", "quality"), &Image::save_webp_to_buffer, DEFVAL(false), DEFVAL(0.75f));
31623185

31633186
ClassDB::bind_method(D_METHOD("detect_alpha"), &Image::detect_alpha);
31643187
ClassDB::bind_method(D_METHOD("is_invisible"), &Image::is_invisible);

core/io/image.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ typedef Vector<uint8_t> (*SavePNGBufferFunc)(const Ref<Image> &p_img);
4848
typedef Error (*SaveJPGFunc)(const String &p_path, const Ref<Image> &p_img, float p_quality);
4949
typedef Vector<uint8_t> (*SaveJPGBufferFunc)(const Ref<Image> &p_img, float p_quality);
5050
typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size);
51+
typedef Error (*SaveWebPFunc)(const String &p_path, const Ref<Image> &p_img, const bool p_lossy, const float p_quality);
52+
typedef Vector<uint8_t> (*SaveWebPBufferFunc)(const Ref<Image> &p_img, const bool p_lossy, const float p_quality);
5153

5254
typedef Error (*SaveEXRFunc)(const String &p_path, const Ref<Image> &p_img, bool p_grayscale);
5355

@@ -60,6 +62,8 @@ class Image : public Resource {
6062
static SaveEXRFunc save_exr_func;
6163
static SavePNGBufferFunc save_png_buffer_func;
6264
static SaveJPGBufferFunc save_jpg_buffer_func;
65+
static SaveWebPFunc save_webp_func;
66+
static SaveWebPBufferFunc save_webp_buffer_func;
6367

6468
enum {
6569
MAX_WIDTH = (1 << 24), // force a limit somehow
@@ -289,6 +293,8 @@ class Image : public Resource {
289293
Vector<uint8_t> save_png_to_buffer() const;
290294
Vector<uint8_t> save_jpg_to_buffer(float p_quality = 0.75) const;
291295
Error save_exr(const String &p_path, bool p_grayscale) const;
296+
Error save_webp(const String &p_path, const bool p_lossy = false, const float p_quality = 0.75f) const;
297+
Vector<uint8_t> save_webp_to_buffer(const bool p_lossy = false, const float p_quality = 0.75f) const;
292298

293299
void create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
294300
create(p_width, p_height, p_use_mipmaps, p_format);

doc/classes/Image.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,30 @@
397397
<return type="int" enum="Error" />
398398
<argument index="0" name="path" type="String" />
399399
<description>
400-
Saves the image as a PNG file to [code]path[/code].
400+
Saves the image as a PNG file to the file at [code]path[/code].
401401
</description>
402402
</method>
403403
<method name="save_png_to_buffer" qualifiers="const">
404404
<return type="PackedByteArray" />
405405
<description>
406+
Saves the image as a PNG file to a byte array.
407+
</description>
408+
</method>
409+
<method name="save_webp" qualifiers="const">
410+
<return type="int" enum="Error" />
411+
<argument index="0" name="path" type="String" />
412+
<argument index="1" name="lossy" type="bool" default="false" />
413+
<argument index="2" name="quality" type="float" default="0.75" />
414+
<description>
415+
Saves the image as a WebP (Web Picture) file to the file at [code]path[/code]. By default it will save lossless. If [code]lossy[/code] is true, the image will be saved lossy, using the [code]quality[/code] setting between 0.0 and 1.0 (inclusive).
416+
</description>
417+
</method>
418+
<method name="save_webp_to_buffer" qualifiers="const">
419+
<return type="PackedByteArray" />
420+
<argument index="0" name="lossy" type="bool" default="false" />
421+
<argument index="1" name="quality" type="float" default="0.75" />
422+
<description>
423+
Saves the image as a WebP (Web Picture) file to a byte array. By default it will save lossless. If [code]lossy[/code] is true, the image will be saved lossy, using the [code]quality[/code] setting between 0.0 and 1.0 (inclusive).
406424
</description>
407425
</method>
408426
<method name="set_pixel">

modules/webp/image_loader_webp.cpp

Lines changed: 9 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -34,184 +34,21 @@
3434
#include "core/io/marshalls.h"
3535
#include "core/os/os.h"
3636
#include "core/string/print_string.h"
37+
#include "webp_common.h"
3738

3839
#include <stdlib.h>
3940
#include <webp/decode.h>
4041
#include <webp/encode.h>
4142

42-
static Vector<uint8_t> _webp_lossy_pack(const Ref<Image> &p_image, float p_quality) {
43-
ERR_FAIL_COND_V(p_image.is_null() || p_image->is_empty(), Vector<uint8_t>());
44-
45-
Ref<Image> img = p_image->duplicate();
46-
if (img->detect_alpha()) {
47-
img->convert(Image::FORMAT_RGBA8);
48-
} else {
49-
img->convert(Image::FORMAT_RGB8);
50-
}
51-
52-
Size2 s(img->get_width(), img->get_height());
53-
Vector<uint8_t> data = img->get_data();
54-
const uint8_t *r = data.ptr();
55-
56-
uint8_t *dst_buff = nullptr;
57-
size_t dst_size = 0;
58-
if (img->get_format() == Image::FORMAT_RGB8) {
59-
dst_size = WebPEncodeRGB(r, s.width, s.height, 3 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
60-
} else {
61-
dst_size = WebPEncodeRGBA(r, s.width, s.height, 4 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
62-
}
63-
64-
ERR_FAIL_COND_V(dst_size == 0, Vector<uint8_t>());
65-
Vector<uint8_t> dst;
66-
dst.resize(4 + dst_size);
67-
uint8_t *w = dst.ptrw();
68-
w[0] = 'W';
69-
w[1] = 'E';
70-
w[2] = 'B';
71-
w[3] = 'P';
72-
memcpy(&w[4], dst_buff, dst_size);
73-
WebPFree(dst_buff);
74-
75-
return dst;
76-
}
77-
78-
static Vector<uint8_t> _webp_lossless_pack(const Ref<Image> &p_image) {
79-
ERR_FAIL_COND_V(p_image.is_null() || p_image->is_empty(), Vector<uint8_t>());
80-
81-
int compression_level = ProjectSettings::get_singleton()->get("rendering/textures/lossless_compression/webp_compression_level");
82-
compression_level = CLAMP(compression_level, 0, 9);
83-
84-
Ref<Image> img = p_image->duplicate();
85-
if (img->detect_alpha()) {
86-
img->convert(Image::FORMAT_RGBA8);
87-
} else {
88-
img->convert(Image::FORMAT_RGB8);
89-
}
90-
91-
Size2 s(img->get_width(), img->get_height());
92-
Vector<uint8_t> data = img->get_data();
93-
const uint8_t *r = data.ptr();
94-
95-
// we need to use the more complex API in order to access the 'exact' flag...
96-
97-
WebPConfig config;
98-
WebPPicture pic;
99-
if (!WebPConfigInit(&config) || !WebPConfigLosslessPreset(&config, compression_level) || !WebPPictureInit(&pic)) {
100-
ERR_FAIL_V(Vector<uint8_t>());
101-
}
102-
103-
WebPMemoryWriter wrt;
104-
config.exact = 1;
105-
pic.use_argb = 1;
106-
pic.width = s.width;
107-
pic.height = s.height;
108-
pic.writer = WebPMemoryWrite;
109-
pic.custom_ptr = &wrt;
110-
WebPMemoryWriterInit(&wrt);
111-
112-
bool success_import = false;
113-
if (img->get_format() == Image::FORMAT_RGB8) {
114-
success_import = WebPPictureImportRGB(&pic, r, 3 * s.width);
115-
} else {
116-
success_import = WebPPictureImportRGBA(&pic, r, 4 * s.width);
117-
}
118-
bool success_encode = false;
119-
if (success_import) {
120-
success_encode = WebPEncode(&config, &pic);
121-
}
122-
WebPPictureFree(&pic);
123-
124-
if (!success_encode) {
125-
WebPMemoryWriterClear(&wrt);
126-
ERR_FAIL_V_MSG(Vector<uint8_t>(), "WebP packing failed.");
127-
}
128-
129-
// copy from wrt
130-
Vector<uint8_t> dst;
131-
dst.resize(4 + wrt.size);
132-
uint8_t *w = dst.ptrw();
133-
w[0] = 'W';
134-
w[1] = 'E';
135-
w[2] = 'B';
136-
w[3] = 'P';
137-
memcpy(&w[4], wrt.mem, wrt.size);
138-
WebPMemoryWriterClear(&wrt);
139-
140-
return dst;
141-
}
142-
143-
static Ref<Image> _webp_unpack(const Vector<uint8_t> &p_buffer) {
144-
int size = p_buffer.size() - 4;
145-
ERR_FAIL_COND_V(size <= 0, Ref<Image>());
146-
const uint8_t *r = p_buffer.ptr();
147-
148-
ERR_FAIL_COND_V(r[0] != 'W' || r[1] != 'E' || r[2] != 'B' || r[3] != 'P', Ref<Image>());
149-
WebPBitstreamFeatures features;
150-
if (WebPGetFeatures(&r[4], size, &features) != VP8_STATUS_OK) {
151-
ERR_FAIL_V_MSG(Ref<Image>(), "Error unpacking WEBP image.");
152-
}
153-
154-
/*
155-
print_line("width: "+itos(features.width));
156-
print_line("height: "+itos(features.height));
157-
print_line("alpha: "+itos(features.has_alpha));
158-
*/
159-
160-
Vector<uint8_t> dst_image;
161-
int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
162-
dst_image.resize(datasize);
163-
164-
uint8_t *dst_w = dst_image.ptrw();
165-
166-
bool errdec = false;
167-
if (features.has_alpha) {
168-
errdec = WebPDecodeRGBAInto(&r[4], size, dst_w, datasize, 4 * features.width) == nullptr;
169-
} else {
170-
errdec = WebPDecodeRGBInto(&r[4], size, dst_w, datasize, 3 * features.width) == nullptr;
171-
}
172-
173-
ERR_FAIL_COND_V_MSG(errdec, Ref<Image>(), "Failed decoding WebP image.");
174-
175-
Ref<Image> img = memnew(Image(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image));
176-
return img;
177-
}
178-
179-
Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
180-
ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER);
181-
182-
WebPBitstreamFeatures features;
183-
if (WebPGetFeatures(p_buffer, p_buffer_len, &features) != VP8_STATUS_OK) {
184-
ERR_FAIL_V(ERR_FILE_CORRUPT);
185-
}
186-
187-
Vector<uint8_t> dst_image;
188-
int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
189-
dst_image.resize(datasize);
190-
uint8_t *dst_w = dst_image.ptrw();
191-
192-
bool errdec = false;
193-
if (features.has_alpha) {
194-
errdec = WebPDecodeRGBAInto(p_buffer, p_buffer_len, dst_w, datasize, 4 * features.width) == nullptr;
195-
} else {
196-
errdec = WebPDecodeRGBInto(p_buffer, p_buffer_len, dst_w, datasize, 3 * features.width) == nullptr;
197-
}
198-
199-
ERR_FAIL_COND_V_MSG(errdec, ERR_FILE_CORRUPT, "Failed decoding WebP image.");
200-
201-
p_image->create(features.width, features.height, false, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image);
202-
203-
return OK;
204-
}
205-
20643
static Ref<Image> _webp_mem_loader_func(const uint8_t *p_png, int p_size) {
20744
Ref<Image> img;
20845
img.instantiate();
209-
Error err = webp_load_image_from_buffer(img.ptr(), p_png, p_size);
46+
Error err = WebPCommon::webp_load_image_from_buffer(img.ptr(), p_png, p_size);
21047
ERR_FAIL_COND_V(err, Ref<Image>());
21148
return img;
21249
}
21350

214-
Error ImageLoaderWEBP::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale) {
51+
Error ImageLoaderWebP::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale) {
21552
Vector<uint8_t> src_image;
21653
uint64_t src_image_len = f->get_length();
21754
ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
@@ -221,18 +58,18 @@ Error ImageLoaderWEBP::load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_
22158

22259
f->get_buffer(&w[0], src_image_len);
22360

224-
Error err = webp_load_image_from_buffer(p_image.ptr(), w, src_image_len);
61+
Error err = WebPCommon::webp_load_image_from_buffer(p_image.ptr(), w, src_image_len);
22562

22663
return err;
22764
}
22865

229-
void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const {
66+
void ImageLoaderWebP::get_recognized_extensions(List<String> *p_extensions) const {
23067
p_extensions->push_back("webp");
23168
}
23269

233-
ImageLoaderWEBP::ImageLoaderWEBP() {
70+
ImageLoaderWebP::ImageLoaderWebP() {
23471
Image::_webp_mem_loader_func = _webp_mem_loader_func;
235-
Image::webp_lossy_packer = _webp_lossy_pack;
236-
Image::webp_lossless_packer = _webp_lossless_pack;
237-
Image::webp_unpacker = _webp_unpack;
72+
Image::webp_lossy_packer = WebPCommon::_webp_lossy_pack;
73+
Image::webp_lossless_packer = WebPCommon::_webp_lossless_pack;
74+
Image::webp_unpacker = WebPCommon::_webp_unpack;
23875
}

modules/webp/image_loader_webp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333

3434
#include "core/io/image_loader.h"
3535

36-
class ImageLoaderWEBP : public ImageFormatLoader {
36+
class ImageLoaderWebP : public ImageFormatLoader {
3737
public:
3838
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, bool p_force_linear, float p_scale);
3939
virtual void get_recognized_extensions(List<String> *p_extensions) const;
40-
ImageLoaderWEBP();
40+
ImageLoaderWebP();
4141
};
4242

4343
#endif

modules/webp/register_types.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,20 @@
3131
#include "register_types.h"
3232

3333
#include "image_loader_webp.h"
34+
#include "resource_saver_webp.h"
3435

35-
static ImageLoaderWEBP *image_loader_webp = nullptr;
36+
static ImageLoaderWebP *image_loader_webp = nullptr;
37+
static Ref<ResourceSaverWebP> resource_saver_webp;
3638

3739
void initialize_webp_module(ModuleInitializationLevel p_level) {
3840
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
3941
return;
4042
}
4143

42-
image_loader_webp = memnew(ImageLoaderWEBP);
44+
image_loader_webp = memnew(ImageLoaderWebP);
45+
resource_saver_webp.instantiate();
4346
ImageLoader::add_image_format_loader(image_loader_webp);
47+
ResourceSaver::add_resource_format_saver(resource_saver_webp);
4448
}
4549

4650
void uninitialize_webp_module(ModuleInitializationLevel p_level) {
@@ -49,4 +53,6 @@ void uninitialize_webp_module(ModuleInitializationLevel p_level) {
4953
}
5054

5155
memdelete(image_loader_webp);
56+
ResourceSaver::remove_resource_format_saver(resource_saver_webp);
57+
resource_saver_webp.unref();
5258
}

0 commit comments

Comments
 (0)