image_saver_tinyexr.cpp 8.9 KB

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