Debian Codegraph @

modules/tinyexr/image_loader_tinyexr.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* image_loader_tinyexr.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 "image_loader_tinyexr.h"
32
33 #include <zlib.h> // Should come before including tinyexr.
34
35 #include "thirdparty/tinyexr/tinyexr.h"
36
37 #include "core/io/file_access_memory.h"
38
39 Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
40 Vector<uint8_t> src_image;
41 uint64_t src_image_len = f->get_length();
42 ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
43 src_image.resize(src_image_len);
44
45 uint8_t *w = src_image.ptrw();
46
47 f->get_buffer(&w[0], src_image_len);
48
49 // Re-implementation of tinyexr's LoadEXRFromMemory using Godot types to store the Image data
50 // and Godot's error codes.
51 // When debugging after updating the thirdparty library, check that we're still in sync with
52 // their API usage in LoadEXRFromMemory.
53
54 EXRVersion exr_version;
55 EXRImage exr_image;
56 EXRHeader exr_header;
57 const char *err = nullptr;
58
59 InitEXRHeader(&exr_header);
60
61 int ret = ParseEXRVersionFromMemory(&exr_version, w, src_image_len);
62 if (ret != TINYEXR_SUCCESS) {
63 return ERR_FILE_CORRUPT;
64 }
65
66 ret = ParseEXRHeaderFromMemory(&exr_header, &exr_version, w, src_image_len, &err);
67 if (ret != TINYEXR_SUCCESS) {
68 if (err) {
69 ERR_PRINT(String(err));
70 FreeEXRErrorMessage(err);
71 }
72 return ERR_FILE_CORRUPT;
73 }
74
75 // Read HALF channel as FLOAT. (GH-13490)
76 bool use_float16 = false;
77 for (int i = 0; i < exr_header.num_channels; i++) {
78 if (exr_header.pixel_types[i] == TINYEXR_PIXELTYPE_HALF) {
79 use_float16 = true;
80 exr_header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
81 }
82 }
83
84 InitEXRImage(&exr_image);
85 ret = LoadEXRImageFromMemory(&exr_image, &exr_header, w, src_image_len, &err);
86 if (ret != TINYEXR_SUCCESS) {
87 if (err) {
88 ERR_PRINT(String(err));
89 FreeEXRErrorMessage(err);
90 }
91 return ERR_FILE_CORRUPT;
92 }
93
94 // RGBA
95 int idxR = -1;
96 int idxG = -1;
97 int idxB = -1;
98 int idxA = -1;
99 for (int c = 0; c < exr_header.num_channels; c++) {
100 if (strcmp(exr_header.channels[c].name, "R") == 0) {
101 idxR = c;
102 } else if (strcmp(exr_header.channels[c].name, "G") == 0) {
103 idxG = c;
104 } else if (strcmp(exr_header.channels[c].name, "B") == 0) {
105 idxB = c;
106 } else if (strcmp(exr_header.channels[c].name, "A") == 0) {
107 idxA = c;
108 } else if (strcmp(exr_header.channels[c].name, "Y") == 0) {
109 idxR = c;
110 idxG = c;
111 idxB = c;
112 }
113 }
114
115 // EXR image data loaded, now parse it into Godot-friendly image data
116
117 Vector<uint8_t> imgdata;
118 Image::Format format;
119 int output_channels = 0;
120
121 int channel_size = use_float16 ? 2 : 4;
122 if (idxA != -1) {
123 imgdata.resize(exr_image.width * exr_image.height * 4 * channel_size); //RGBA
124 format = use_float16 ? Image::FORMAT_RGBAH : Image::FORMAT_RGBAF;
125 output_channels = 4;
126 } else if (idxB != -1) {
127 ERR_FAIL_COND_V(idxG == -1, ERR_FILE_CORRUPT);
128 ERR_FAIL_COND_V(idxR == -1, ERR_FILE_CORRUPT);
129 imgdata.resize(exr_image.width * exr_image.height * 3 * channel_size); //RGB
130 format = use_float16 ? Image::FORMAT_RGBH : Image::FORMAT_RGBF;
131 output_channels = 3;
132 } else if (idxG != -1) {
133 ERR_FAIL_COND_V(idxR == -1, ERR_FILE_CORRUPT);
134 imgdata.resize(exr_image.width * exr_image.height * 2 * channel_size); //RG
135 format = use_float16 ? Image::FORMAT_RGH : Image::FORMAT_RGF;
136 output_channels = 2;
137 } else {
138 ERR_FAIL_COND_V(idxR == -1, ERR_FILE_CORRUPT);
139 imgdata.resize(exr_image.width * exr_image.height * 1 * channel_size); //R
140 format = use_float16 ? Image::FORMAT_RH : Image::FORMAT_RF;
141 output_channels = 1;
142 }
143
144 EXRTile single_image_tile;
145 int num_tiles;
146 int tile_width = 0;
147 int tile_height = 0;
148
149 const EXRTile *exr_tiles;
150
151 if (!exr_header.tiled) {
152 single_image_tile.images = exr_image.images;
153 single_image_tile.width = exr_image.width;
154 single_image_tile.height = exr_image.height;
155 single_image_tile.level_x = exr_image.width;
156 single_image_tile.level_y = exr_image.height;
157 single_image_tile.offset_x = 0;
158 single_image_tile.offset_y = 0;
159
160 exr_tiles = &single_image_tile;
161 num_tiles = 1;
162 tile_width = exr_image.width;
163 tile_height = exr_image.height;
164 } else {
165 tile_width = exr_header.tile_size_x;
166 tile_height = exr_header.tile_size_y;
167 num_tiles = exr_image.num_tiles;
168 exr_tiles = exr_image.tiles;
169 }
170
171 //print_line("reading format: " + Image::get_format_name(format));
172 {
173 uint8_t *wd = imgdata.ptrw();
174 uint16_t *iw16 = (uint16_t *)wd;
175 float *iw32 = (float *)wd;
176
177 // Assume `out_rgba` have enough memory allocated.
178 for (int tile_index = 0; tile_index < num_tiles; tile_index++) {
179 const EXRTile &tile = exr_tiles[tile_index];
180
181 int tw = tile.width;
182 int th = tile.height;
183
184 const float *r_channel_start = reinterpret_cast<const float *>(tile.images[idxR]);
185 const float *g_channel_start = nullptr;
186 const float *b_channel_start = nullptr;
187 const float *a_channel_start = nullptr;
188
189 if (idxG != -1) {
190 g_channel_start = reinterpret_cast<const float *>(tile.images[idxG]);
191 }
192 if (idxB != -1) {
193 b_channel_start = reinterpret_cast<const float *>(tile.images[idxB]);
194 }
195 if (idxA != -1) {
196 a_channel_start = reinterpret_cast<const float *>(tile.images[idxA]);
197 }
198
199 uint16_t *first_row_w16 = iw16 + (tile.offset_y * tile_height * exr_image.width + tile.offset_x * tile_width) * output_channels;
200 float *first_row_w32 = iw32 + (tile.offset_y * tile_height * exr_image.width + tile.offset_x * tile_width) * output_channels;
201
202 for (int y = 0; y < th; y++) {
203 const float *r_channel = r_channel_start + y * tile_width;
204 const float *g_channel = nullptr;
205 const float *b_channel = nullptr;
206 const float *a_channel = nullptr;
207 if (g_channel_start) {
208 g_channel = g_channel_start + y * tile_width;
209 }
210 if (b_channel_start) {
211 b_channel = b_channel_start + y * tile_width;
212 }
213 if (a_channel_start) {
214 a_channel = a_channel_start + y * tile_width;
215 }
216
217 if (use_float16) {
218 uint16_t *row_w = first_row_w16 + (y * exr_image.width * output_channels);
219
220 for (int x = 0; x < tw; x++) {
221 Color color;
222 color.r = *r_channel++;
223 if (g_channel) {
224 color.g = *g_channel++;
225 }
226 if (b_channel) {
227 color.b = *b_channel++;
228 }
229 if (a_channel) {
230 color.a = *a_channel++;
231 }
232
233 if (p_flags & FLAG_FORCE_LINEAR) {
234 color = color.srgb_to_linear();
235 }
236
237 *row_w++ = Math::make_half_float(color.r);
238 if (g_channel) {
239 *row_w++ = Math::make_half_float(color.g);
240 }
241 if (b_channel) {
242 *row_w++ = Math::make_half_float(color.b);
243 }
244 if (a_channel) {
245 *row_w++ = Math::make_half_float(color.a);
246 }
247 }
248 } else {
249 float *row_w = first_row_w32 + (y * exr_image.width * output_channels);
250
251 for (int x = 0; x < tw; x++) {
252 Color color;
253 color.r = *r_channel++;
254 if (g_channel) {
255 color.g = *g_channel++;
256 }
257 if (b_channel) {
258 color.b = *b_channel++;
259 }
260 if (a_channel) {
261 color.a = *a_channel++;
262 }
263
264 if (p_flags & FLAG_FORCE_LINEAR) {
265 color = color.srgb_to_linear();
266 }
267
268 *row_w++ = color.r;
269 if (g_channel) {
270 *row_w++ = color.g;
271 }
272 if (b_channel) {
273 *row_w++ = color.b;
274 }
275 if (a_channel) {
276 *row_w++ = color.a;
277 }
278 }
279 }
280 }
281 }
282 }
283
284 p_image->set_data(exr_image.width, exr_image.height, false, format, imgdata);
285
286 FreeEXRHeader(&exr_header);
287 FreeEXRImage(&exr_image);
288
289 return OK;
290 }
291
292 void ImageLoaderTinyEXR::get_recognized_extensions(List<String> *p_extensions) const {
293 p_extensions->push_back("exr");
294 }
295
296 static Ref<Image> _tinyexr_mem_loader_func(const uint8_t *p_exr, int p_size) {
297 Ref<FileAccessMemory> memfile;
298 memfile.instantiate();
299 Error open_memfile_error = memfile->open_custom(p_exr, p_size);
300 ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for EXR image buffer.");
301
302 Ref<Image> img;
303 img.instantiate();
304 Error load_error = ImageLoaderTinyEXR().load_image(img, memfile, false, 1.0f);
305 ERR_FAIL_COND_V_MSG(load_error, Ref<Image>(), "Failed to load EXR image.");
306 return img;
307 }
308
309 ImageLoaderTinyEXR::ImageLoaderTinyEXR() {
310 Image::_exr_mem_loader_func = _tinyexr_mem_loader_func;
311 }