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-
20643static 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}
0 commit comments