1 /*************************************************************************/
2 /* image_loader_webp.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30
31 #include "image_loader_webp.h"
32
33 #include "core/io/marshalls.h"
34 #include "core/os/os.h"
35 #include "core/print_string.h"
36
37 #include <stdlib.h>
38 #include <webp/decode.h>
39 #include <webp/encode.h>
40
_webp_lossy_pack(const Ref<Image> & p_image,float p_quality)41 static PoolVector<uint8_t> _webp_lossy_pack(const Ref<Image> &p_image, float p_quality) {
42
43 ERR_FAIL_COND_V(p_image.is_null() || p_image->empty(), PoolVector<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 Size2 s(img->get_width(), img->get_height());
52 PoolVector<uint8_t> data = img->get_data();
53 PoolVector<uint8_t>::Read r = data.read();
54
55 uint8_t *dst_buff = NULL;
56 size_t dst_size = 0;
57 if (img->get_format() == Image::FORMAT_RGB8) {
58
59 dst_size = WebPEncodeRGB(r.ptr(), s.width, s.height, 3 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
60 } else {
61 dst_size = WebPEncodeRGBA(r.ptr(), 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, PoolVector<uint8_t>());
65 PoolVector<uint8_t> dst;
66 dst.resize(4 + dst_size);
67 PoolVector<uint8_t>::Write w = dst.write();
68 w[0] = 'W';
69 w[1] = 'E';
70 w[2] = 'B';
71 w[3] = 'P';
72 copymem(&w[4], dst_buff, dst_size);
73 free(dst_buff);
74 w.release();
75 return dst;
76 }
77
_webp_lossy_unpack(const PoolVector<uint8_t> & p_buffer)78 static Ref<Image> _webp_lossy_unpack(const PoolVector<uint8_t> &p_buffer) {
79
80 int size = p_buffer.size() - 4;
81 ERR_FAIL_COND_V(size <= 0, Ref<Image>());
82 PoolVector<uint8_t>::Read r = p_buffer.read();
83
84 ERR_FAIL_COND_V(r[0] != 'W' || r[1] != 'E' || r[2] != 'B' || r[3] != 'P', Ref<Image>());
85 WebPBitstreamFeatures features;
86 if (WebPGetFeatures(&r[4], size, &features) != VP8_STATUS_OK) {
87 ERR_FAIL_V_MSG(Ref<Image>(), "Error unpacking WEBP image.");
88 }
89
90 /*
91 print_line("width: "+itos(features.width));
92 print_line("height: "+itos(features.height));
93 print_line("alpha: "+itos(features.has_alpha));
94 */
95
96 PoolVector<uint8_t> dst_image;
97 int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
98 dst_image.resize(datasize);
99
100 PoolVector<uint8_t>::Write dst_w = dst_image.write();
101
102 bool errdec = false;
103 if (features.has_alpha) {
104 errdec = WebPDecodeRGBAInto(&r[4], size, dst_w.ptr(), datasize, 4 * features.width) == NULL;
105 } else {
106 errdec = WebPDecodeRGBInto(&r[4], size, dst_w.ptr(), datasize, 3 * features.width) == NULL;
107 }
108
109 ERR_FAIL_COND_V_MSG(errdec, Ref<Image>(), "Failed decoding WebP image.");
110
111 dst_w.release();
112
113 Ref<Image> img = memnew(Image(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image));
114 return img;
115 }
116
webp_load_image_from_buffer(Image * p_image,const uint8_t * p_buffer,int p_buffer_len)117 Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
118
119 ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER);
120
121 WebPBitstreamFeatures features;
122 if (WebPGetFeatures(p_buffer, p_buffer_len, &features) != VP8_STATUS_OK) {
123 ERR_FAIL_V(ERR_FILE_CORRUPT);
124 }
125
126 PoolVector<uint8_t> dst_image;
127 int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
128 dst_image.resize(datasize);
129 PoolVector<uint8_t>::Write dst_w = dst_image.write();
130
131 bool errdec = false;
132 if (features.has_alpha) {
133 errdec = WebPDecodeRGBAInto(p_buffer, p_buffer_len, dst_w.ptr(), datasize, 4 * features.width) == NULL;
134 } else {
135 errdec = WebPDecodeRGBInto(p_buffer, p_buffer_len, dst_w.ptr(), datasize, 3 * features.width) == NULL;
136 }
137 dst_w.release();
138
139 ERR_FAIL_COND_V_MSG(errdec, ERR_FILE_CORRUPT, "Failed decoding WebP image.");
140
141 p_image->create(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image);
142
143 return OK;
144 }
145
_webp_mem_loader_func(const uint8_t * p_png,int p_size)146 static Ref<Image> _webp_mem_loader_func(const uint8_t *p_png, int p_size) {
147
148 Ref<Image> img;
149 img.instance();
150 Error err = webp_load_image_from_buffer(img.ptr(), p_png, p_size);
151 ERR_FAIL_COND_V(err, Ref<Image>());
152 return img;
153 }
154
load_image(Ref<Image> p_image,FileAccess * f,bool p_force_linear,float p_scale)155 Error ImageLoaderWEBP::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
156
157 PoolVector<uint8_t> src_image;
158 int src_image_len = f->get_len();
159 ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
160 src_image.resize(src_image_len);
161
162 PoolVector<uint8_t>::Write w = src_image.write();
163
164 f->get_buffer(&w[0], src_image_len);
165
166 f->close();
167
168 Error err = webp_load_image_from_buffer(p_image.ptr(), w.ptr(), src_image_len);
169
170 return err;
171 }
172
get_recognized_extensions(List<String> * p_extensions) const173 void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const {
174
175 p_extensions->push_back("webp");
176 }
177
ImageLoaderWEBP()178 ImageLoaderWEBP::ImageLoaderWEBP() {
179 Image::_webp_mem_loader_func = _webp_mem_loader_func;
180 Image::lossy_packer = _webp_lossy_pack;
181 Image::lossy_unpacker = _webp_lossy_unpack;
182 }
183