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