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-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 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 #include "image_loader_webp.h"
31
32 #include "io/marshalls.h"
33 #include "os/os.h"
34 #include "print_string.h"
35
36 #include <stdlib.h>
37 #include <webp/decode.h>
38 #include <webp/encode.h>
39
_webp_lossy_pack(const Image & p_image,float p_quality)40 static DVector<uint8_t> _webp_lossy_pack(const Image &p_image, float p_quality) {
41
42 ERR_FAIL_COND_V(p_image.empty(), DVector<uint8_t>());
43
44 Image img = p_image;
45 if (img.detect_alpha())
46 img.convert(Image::FORMAT_RGBA);
47 else
48 img.convert(Image::FORMAT_RGB);
49
50 Size2 s(img.get_width(), img.get_height());
51 DVector<uint8_t> data = img.get_data();
52 DVector<uint8_t>::Read r = data.read();
53
54 uint8_t *dst_buff = NULL;
55 size_t dst_size = 0;
56 if (img.get_format() == Image::FORMAT_RGB) {
57
58 dst_size = WebPEncodeRGB(r.ptr(), s.width, s.height, 3 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
59 } else {
60 dst_size = WebPEncodeRGBA(r.ptr(), s.width, s.height, 4 * s.width, CLAMP(p_quality * 100.0, 0, 100.0), &dst_buff);
61 }
62
63 ERR_FAIL_COND_V(dst_size == 0, DVector<uint8_t>());
64 DVector<uint8_t> dst;
65 dst.resize(4 + dst_size);
66 DVector<uint8_t>::Write w = dst.write();
67 w[0] = 'W';
68 w[1] = 'E';
69 w[2] = 'B';
70 w[3] = 'P';
71 copymem(&w[4], dst_buff, dst_size);
72 free(dst_buff);
73 w = DVector<uint8_t>::Write();
74 return dst;
75 }
76
_webp_lossy_unpack(const DVector<uint8_t> & p_buffer)77 static Image _webp_lossy_unpack(const DVector<uint8_t> &p_buffer) {
78
79 int size = p_buffer.size() - 4;
80 ERR_FAIL_COND_V(size <= 0, Image());
81 DVector<uint8_t>::Read r = p_buffer.read();
82
83 ERR_FAIL_COND_V(r[0] != 'W' || r[1] != 'E' || r[2] != 'B' || r[3] != 'P', Image());
84 WebPBitstreamFeatures features;
85 if (WebPGetFeatures(&r[4], size, &features) != VP8_STATUS_OK) {
86 ERR_EXPLAIN("Error unpacking WEBP image:");
87 ERR_FAIL_V(Image());
88 }
89
90 //print_line("width: "+itos(features.width));
91 //print_line("height: "+itos(features.height));
92 //print_line("alpha: "+itos(features.has_alpha));
93
94 DVector<uint8_t> dst_image;
95 int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
96 dst_image.resize(datasize);
97
98 DVector<uint8_t>::Write dst_w = dst_image.write();
99
100 bool errdec = false;
101 if (features.has_alpha) {
102 errdec = WebPDecodeRGBAInto(&r[4], size, dst_w.ptr(), datasize, 4 * features.width) == NULL;
103 } else {
104 errdec = WebPDecodeRGBInto(&r[4], size, dst_w.ptr(), datasize, 3 * features.width) == NULL;
105 }
106
107 //ERR_EXPLAIN("Error decoding webp! - "+p_file);
108 ERR_FAIL_COND_V(errdec, Image());
109
110 dst_w = DVector<uint8_t>::Write();
111
112 return Image(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA : Image::FORMAT_RGB, dst_image);
113 }
114
load_image(Image * p_image,FileAccess * f)115 Error ImageLoaderWEBP::load_image(Image *p_image, FileAccess *f) {
116
117 uint32_t size = f->get_len();
118 DVector<uint8_t> src_image;
119 src_image.resize(size);
120
121 WebPBitstreamFeatures features;
122
123 DVector<uint8_t>::Write src_w = src_image.write();
124 f->get_buffer(src_w.ptr(), size);
125 ERR_FAIL_COND_V(f->eof_reached(), ERR_FILE_EOF);
126
127 if (WebPGetFeatures(src_w.ptr(), size, &features) != VP8_STATUS_OK) {
128 f->close();
129 //ERR_EXPLAIN("Error decoding WEBP image: "+p_file);
130 ERR_FAIL_V(ERR_FILE_CORRUPT);
131 }
132
133 print_line("width: " + itos(features.width));
134 print_line("height: " + itos(features.height));
135 print_line("alpha: " + itos(features.has_alpha));
136
137 src_w = DVector<uint8_t>::Write();
138
139 DVector<uint8_t> dst_image;
140 int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
141 dst_image.resize(datasize);
142
143 DVector<uint8_t>::Read src_r = src_image.read();
144 DVector<uint8_t>::Write dst_w = dst_image.write();
145
146 bool errdec = false;
147 if (features.has_alpha) {
148 errdec = WebPDecodeRGBAInto(src_r.ptr(), size, dst_w.ptr(), datasize, 4 * features.width) == NULL;
149 } else {
150 errdec = WebPDecodeRGBInto(src_r.ptr(), size, dst_w.ptr(), datasize, 3 * features.width) == NULL;
151 }
152
153 //ERR_EXPLAIN("Error decoding webp! - "+p_file);
154 ERR_FAIL_COND_V(errdec, ERR_FILE_CORRUPT);
155
156 src_r = DVector<uint8_t>::Read();
157 dst_w = DVector<uint8_t>::Write();
158
159 *p_image = Image(features.width, features.height, 0, features.has_alpha ? Image::FORMAT_RGBA : Image::FORMAT_RGB, dst_image);
160
161 return OK;
162 }
163
get_recognized_extensions(List<String> * p_extensions) const164 void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const {
165
166 p_extensions->push_back("webp");
167 }
168
ImageLoaderWEBP()169 ImageLoaderWEBP::ImageLoaderWEBP() {
170
171 Image::lossy_packer = _webp_lossy_pack;
172 Image::lossy_unpacker = _webp_lossy_unpack;
173 }
174