Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/tinyexr/image_saver_tinyexr.cpp
10277 views
1
/**************************************************************************/
2
/* image_saver_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_saver_tinyexr.h"
32
33
#include "core/io/file_access.h"
34
#include "core/math/math_funcs.h"
35
36
#include <zlib.h> // Should come before including tinyexr.
37
38
#include "thirdparty/tinyexr/tinyexr.h"
39
40
static bool is_supported_format(Image::Format p_format) {
41
// This is checked before anything else.
42
// Mostly uncompressed formats are considered.
43
switch (p_format) {
44
case Image::FORMAT_RF:
45
case Image::FORMAT_RGF:
46
case Image::FORMAT_RGBF:
47
case Image::FORMAT_RGBAF:
48
case Image::FORMAT_RH:
49
case Image::FORMAT_RGH:
50
case Image::FORMAT_RGBH:
51
case Image::FORMAT_RGBAH:
52
case Image::FORMAT_R8:
53
case Image::FORMAT_RG8:
54
case Image::FORMAT_RGB8:
55
case Image::FORMAT_RGBA8:
56
return true;
57
default:
58
return false;
59
}
60
}
61
62
enum SrcPixelType {
63
SRC_FLOAT,
64
SRC_HALF,
65
SRC_BYTE,
66
SRC_UNSUPPORTED
67
};
68
69
static SrcPixelType get_source_pixel_type(Image::Format p_format) {
70
switch (p_format) {
71
case Image::FORMAT_RF:
72
case Image::FORMAT_RGF:
73
case Image::FORMAT_RGBF:
74
case Image::FORMAT_RGBAF:
75
return SRC_FLOAT;
76
case Image::FORMAT_RH:
77
case Image::FORMAT_RGH:
78
case Image::FORMAT_RGBH:
79
case Image::FORMAT_RGBAH:
80
return SRC_HALF;
81
case Image::FORMAT_R8:
82
case Image::FORMAT_RG8:
83
case Image::FORMAT_RGB8:
84
case Image::FORMAT_RGBA8:
85
return SRC_BYTE;
86
default:
87
return SRC_UNSUPPORTED;
88
}
89
}
90
91
static int get_target_pixel_type(Image::Format p_format) {
92
switch (p_format) {
93
case Image::FORMAT_RF:
94
case Image::FORMAT_RGF:
95
case Image::FORMAT_RGBF:
96
case Image::FORMAT_RGBAF:
97
return TINYEXR_PIXELTYPE_FLOAT;
98
case Image::FORMAT_RH:
99
case Image::FORMAT_RGH:
100
case Image::FORMAT_RGBH:
101
case Image::FORMAT_RGBAH:
102
// EXR doesn't support 8-bit channels so in that case we'll convert
103
case Image::FORMAT_R8:
104
case Image::FORMAT_RG8:
105
case Image::FORMAT_RGB8:
106
case Image::FORMAT_RGBA8:
107
return TINYEXR_PIXELTYPE_HALF;
108
default:
109
return -1;
110
}
111
}
112
113
static int get_pixel_type_size(int p_pixel_type) {
114
switch (p_pixel_type) {
115
case TINYEXR_PIXELTYPE_HALF:
116
return 2;
117
case TINYEXR_PIXELTYPE_FLOAT:
118
return 4;
119
}
120
return -1;
121
}
122
123
static int get_channel_count(Image::Format p_format) {
124
switch (p_format) {
125
case Image::FORMAT_RF:
126
case Image::FORMAT_RH:
127
case Image::FORMAT_R8:
128
return 1;
129
case Image::FORMAT_RGF:
130
case Image::FORMAT_RGH:
131
case Image::FORMAT_RG8:
132
return 2;
133
case Image::FORMAT_RGBF:
134
case Image::FORMAT_RGBH:
135
case Image::FORMAT_RGB8:
136
return 3;
137
case Image::FORMAT_RGBAF:
138
case Image::FORMAT_RGBAH:
139
case Image::FORMAT_RGBA8:
140
return 4;
141
default:
142
return -1;
143
}
144
}
145
146
Vector<uint8_t> save_exr_buffer(const Ref<Image> &p_img, bool p_grayscale) {
147
Image::Format format = p_img->get_format();
148
149
if (!is_supported_format(format)) {
150
// Format not supported
151
print_error("Image format not supported for saving as EXR. Consider saving as PNG.");
152
153
return Vector<uint8_t>();
154
}
155
156
EXRHeader header;
157
InitEXRHeader(&header);
158
159
EXRImage image;
160
InitEXRImage(&image);
161
162
const int max_channels = 4;
163
164
// Godot does not support more than 4 channels,
165
// so we can preallocate header infos on the stack and use only the subset we need
166
PackedByteArray channels[max_channels];
167
unsigned char *channels_ptrs[max_channels];
168
EXRChannelInfo channel_infos[max_channels];
169
int pixel_types[max_channels];
170
int requested_pixel_types[max_channels] = { -1 };
171
172
// Gimp and Blender are a bit annoying so order of channels isn't straightforward.
173
const int channel_mappings[4][4] = {
174
{ 0 }, // R
175
{ 1, 0 }, // GR
176
{ 2, 1, 0 }, // BGR
177
{ 3, 2, 1, 0 } // ABGR
178
};
179
180
int channel_count = get_channel_count(format);
181
ERR_FAIL_COND_V(channel_count < 0, Vector<uint8_t>());
182
ERR_FAIL_COND_V(p_grayscale && channel_count != 1, Vector<uint8_t>());
183
184
int target_pixel_type = get_target_pixel_type(format);
185
ERR_FAIL_COND_V(target_pixel_type < 0, Vector<uint8_t>());
186
int target_pixel_type_size = get_pixel_type_size(target_pixel_type);
187
ERR_FAIL_COND_V(target_pixel_type_size < 0, Vector<uint8_t>());
188
SrcPixelType src_pixel_type = get_source_pixel_type(format);
189
ERR_FAIL_COND_V(src_pixel_type == SRC_UNSUPPORTED, Vector<uint8_t>());
190
const int pixel_count = p_img->get_width() * p_img->get_height();
191
192
const int *channel_mapping = channel_mappings[channel_count - 1];
193
194
{
195
PackedByteArray src_data = p_img->get_data();
196
const uint8_t *src_r = src_data.ptr();
197
198
for (int channel_index = 0; channel_index < channel_count; ++channel_index) {
199
// De-interleave channels
200
201
PackedByteArray &dst = channels[channel_index];
202
dst.resize(pixel_count * target_pixel_type_size);
203
204
uint8_t *dst_w = dst.ptrw();
205
206
if (src_pixel_type == SRC_FLOAT && target_pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
207
// Note: we don't save mipmaps
208
CRASH_COND(src_data.size() < pixel_count * channel_count * target_pixel_type_size);
209
210
const float *src_rp = (float *)src_r;
211
float *dst_wp = (float *)dst_w;
212
213
for (int i = 0; i < pixel_count; ++i) {
214
dst_wp[i] = src_rp[channel_index + i * channel_count];
215
}
216
217
} else if (src_pixel_type == SRC_HALF && target_pixel_type == TINYEXR_PIXELTYPE_HALF) {
218
CRASH_COND(src_data.size() < pixel_count * channel_count * target_pixel_type_size);
219
220
const uint16_t *src_rp = (uint16_t *)src_r;
221
uint16_t *dst_wp = (uint16_t *)dst_w;
222
223
for (int i = 0; i < pixel_count; ++i) {
224
dst_wp[i] = src_rp[channel_index + i * channel_count];
225
}
226
227
} else if (src_pixel_type == SRC_BYTE && target_pixel_type == TINYEXR_PIXELTYPE_HALF) {
228
CRASH_COND(src_data.size() < pixel_count * channel_count);
229
230
const uint8_t *src_rp = (uint8_t *)src_r;
231
uint16_t *dst_wp = (uint16_t *)dst_w;
232
233
for (int i = 0; i < pixel_count; ++i) {
234
dst_wp[i] = Math::make_half_float(src_rp[channel_index + i * channel_count] / 255.f);
235
}
236
237
} else {
238
CRASH_NOW();
239
}
240
241
int remapped_index = channel_mapping[channel_index];
242
243
channels_ptrs[remapped_index] = dst_w;
244
245
// No conversion
246
pixel_types[remapped_index] = target_pixel_type;
247
requested_pixel_types[remapped_index] = target_pixel_type;
248
249
// Write channel name
250
if (p_grayscale) {
251
channel_infos[remapped_index].name[0] = 'Y';
252
channel_infos[remapped_index].name[1] = '\0';
253
} else {
254
const char *rgba = "RGBA";
255
channel_infos[remapped_index].name[0] = rgba[channel_index];
256
channel_infos[remapped_index].name[1] = '\0';
257
}
258
}
259
}
260
261
image.images = channels_ptrs;
262
image.num_channels = channel_count;
263
image.width = p_img->get_width();
264
image.height = p_img->get_height();
265
266
header.num_channels = image.num_channels;
267
header.channels = channel_infos;
268
header.pixel_types = pixel_types;
269
header.requested_pixel_types = requested_pixel_types;
270
header.compression_type = TINYEXR_COMPRESSIONTYPE_PIZ;
271
272
unsigned char *mem = nullptr;
273
const char *err = nullptr;
274
275
size_t bytes = SaveEXRImageToMemory(&image, &header, &mem, &err);
276
if (err && *err != OK) {
277
return Vector<uint8_t>();
278
}
279
Vector<uint8_t> buffer;
280
buffer.resize(bytes);
281
memcpy(buffer.ptrw(), mem, bytes);
282
free(mem);
283
return buffer;
284
}
285
286
Error save_exr(const String &p_path, const Ref<Image> &p_img, bool p_grayscale) {
287
const Vector<uint8_t> buffer = save_exr_buffer(p_img, p_grayscale);
288
if (buffer.is_empty()) {
289
print_error(String("Saving EXR failed."));
290
return ERR_FILE_CANT_WRITE;
291
} else {
292
Ref<FileAccess> ref = FileAccess::open(p_path, FileAccess::WRITE);
293
ERR_FAIL_COND_V(ref.is_null(), ERR_FILE_CANT_WRITE);
294
ref->store_buffer(buffer.ptr(), buffer.size());
295
}
296
297
return OK;
298
}
299
300