Debian Codegraph @

modules/webp/webp_common.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* webp_common.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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 "webp_common.h"
32
33 #include "core/config/project_settings.h"
34
35 #include <webp/decode.h>
36 #include <webp/encode.h>
37
38 namespace WebPCommon {
39 Vector<uint8_t> _webp_lossy_pack(const Ref<Image> &p_image, float p_quality) {
40 ERR_FAIL_COND_V(p_image.is_null() || p_image->is_empty(), Vector<uint8_t>());
41
42 return _webp_packer(p_image, CLAMP(p_quality * 100.0f, 0.0f, 100.0f), false);
43 }
44
45 Vector<uint8_t> _webp_lossless_pack(const Ref<Image> &p_image) {
46 ERR_FAIL_COND_V(p_image.is_null() || p_image->is_empty(), Vector<uint8_t>());
47
48 float compression_factor = GLOBAL_GET("rendering/textures/webp_compression/lossless_compression_factor");
49 compression_factor = CLAMP(compression_factor, 0.0f, 100.0f);
50
51 return _webp_packer(p_image, compression_factor, true);
52 }
53
54 Vector<uint8_t> _webp_packer(const Ref<Image> &p_image, float p_quality, bool p_lossless) {
55 int compression_method = GLOBAL_GET("rendering/textures/webp_compression/compression_method");
56 compression_method = CLAMP(compression_method, 0, 6);
57
58 Ref<Image> img = p_image->duplicate();
59 if (img->is_compressed()) {
60 Error error = img->decompress();
61 ERR_FAIL_COND_V_MSG(error != OK, Vector<uint8_t>(), "Couldn't decompress image.");
62 }
63 if (img->detect_alpha() != Image::ALPHA_NONE) {
64 img->convert(Image::FORMAT_RGBA8);
65 } else {
66 img->convert(Image::FORMAT_RGB8);
67 }
68
69 Size2 s(img->get_width(), img->get_height());
70 Vector<uint8_t> data = img->get_data();
71 const uint8_t *r = data.ptr();
72
73 // we need to use the more complex API in order to access specific flags...
74
75 WebPConfig config;
76 WebPPicture pic;
77 if (!WebPConfigInit(&config) || !WebPPictureInit(&pic)) {
78 ERR_FAIL_V(Vector<uint8_t>());
79 }
80
81 WebPMemoryWriter wrt;
82 if (p_lossless) {
83 config.lossless = 1;
84 config.exact = 1;
85 }
86 config.method = compression_method;
87 config.quality = p_quality;
88 config.use_sharp_yuv = 1;
89 pic.use_argb = 1;
90 pic.width = s.width;
91 pic.height = s.height;
92 pic.writer = WebPMemoryWrite;
93 pic.custom_ptr = &wrt;
94 WebPMemoryWriterInit(&wrt);
95
96 bool success_import = false;
97 if (img->get_format() == Image::FORMAT_RGB8) {
98 success_import = WebPPictureImportRGB(&pic, r, 3 * s.width);
99 } else {
100 success_import = WebPPictureImportRGBA(&pic, r, 4 * s.width);
101 }
102 bool success_encode = false;
103 if (success_import) {
104 success_encode = WebPEncode(&config, &pic);
105 }
106 WebPPictureFree(&pic);
107
108 if (!success_encode) {
109 WebPMemoryWriterClear(&wrt);
110 ERR_FAIL_V_MSG(Vector<uint8_t>(), "WebP packing failed.");
111 }
112
113 // copy from wrt
114 Vector<uint8_t> dst;
115 dst.resize(wrt.size);
116 uint8_t *w = dst.ptrw();
117 memcpy(w, wrt.mem, wrt.size);
118 WebPMemoryWriterClear(&wrt);
119 return dst;
120 }
121
122 Ref<Image> _webp_unpack(const Vector<uint8_t> &p_buffer) {
123 int size = p_buffer.size();
124 ERR_FAIL_COND_V(size <= 0, Ref<Image>());
125 const uint8_t *r = p_buffer.ptr();
126
127 // A WebP file uses a RIFF header, which starts with "RIFF____WEBP".
128 ERR_FAIL_COND_V(r[0] != 'R' || r[1] != 'I' || r[2] != 'F' || r[3] != 'F' || r[8] != 'W' || r[9] != 'E' || r[10] != 'B' || r[11] != 'P', Ref<Image>());
129 WebPBitstreamFeatures features;
130 if (WebPGetFeatures(r, size, &features) != VP8_STATUS_OK) {
131 ERR_FAIL_V_MSG(Ref<Image>(), "Error unpacking WebP image.");
132 }
133
134 Vector<uint8_t> dst_image;
135 int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
136 dst_image.resize(datasize);
137
138 uint8_t *dst_w = dst_image.ptrw();
139
140 bool errdec = false;
141 if (features.has_alpha) {
142 errdec = WebPDecodeRGBAInto(r, size, dst_w, datasize, 4 * features.width) == nullptr;
143 } else {
144 errdec = WebPDecodeRGBInto(r, size, dst_w, datasize, 3 * features.width) == nullptr;
145 }
146
147 ERR_FAIL_COND_V_MSG(errdec, Ref<Image>(), "Failed decoding WebP image.");
148
149 Ref<Image> img = memnew(Image(features.width, features.height, false, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image));
150 return img;
151 }
152
153 Error webp_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
154 ERR_FAIL_NULL_V(p_image, ERR_INVALID_PARAMETER);
155
156 WebPBitstreamFeatures features;
157 if (WebPGetFeatures(p_buffer, p_buffer_len, &features) != VP8_STATUS_OK) {
158 ERR_FAIL_V(ERR_FILE_CORRUPT);
159 }
160
161 Vector<uint8_t> dst_image;
162 int datasize = features.width * features.height * (features.has_alpha ? 4 : 3);
163 dst_image.resize(datasize);
164 uint8_t *dst_w = dst_image.ptrw();
165
166 bool errdec = false;
167 if (features.has_alpha) {
168 errdec = WebPDecodeRGBAInto(p_buffer, p_buffer_len, dst_w, datasize, 4 * features.width) == nullptr;
169 } else {
170 errdec = WebPDecodeRGBInto(p_buffer, p_buffer_len, dst_w, datasize, 3 * features.width) == nullptr;
171 }
172
173 ERR_FAIL_COND_V_MSG(errdec, ERR_FILE_CORRUPT, "Failed decoding WebP image.");
174
175 p_image->set_data(features.width, features.height, false, features.has_alpha ? Image::FORMAT_RGBA8 : Image::FORMAT_RGB8, dst_image);
176
177 return OK;
178 }
179 } // namespace WebPCommon