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