image_saver_tinyexr.cpp 9.5 KB

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