image_saver_tinyexr.cpp 9.4 KB

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