image_saver_tinyexr.cpp 9.2 KB

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