Debian Codegraph @

modules/tinyexr/image_saver_tinyexr.cpp

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