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