image_saver_tinyexr.cpp 8.6 KB

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