image_loader_tinyexr.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*************************************************************************/
  2. /* image_loader_tinyexr.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_loader_tinyexr.h"
  31. #include "core/os/os.h"
  32. #include "core/print_string.h"
  33. #include "thirdparty/tinyexr/tinyexr.h"
  34. Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
  35. PoolVector<uint8_t> src_image;
  36. uint64_t src_image_len = f->get_len();
  37. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  38. src_image.resize(src_image_len);
  39. PoolVector<uint8_t>::Write img_write = src_image.write();
  40. uint8_t *w = img_write.ptr();
  41. f->get_buffer(&w[0], src_image_len);
  42. f->close();
  43. // Re-implementation of tinyexr's LoadEXRFromMemory using Godot types to store the Image data
  44. // and Godot's error codes.
  45. // When debugging after updating the thirdparty library, check that we're still in sync with
  46. // their API usage in LoadEXRFromMemory.
  47. EXRVersion exr_version;
  48. EXRImage exr_image;
  49. EXRHeader exr_header;
  50. const char *err = nullptr;
  51. InitEXRHeader(&exr_header);
  52. int ret = ParseEXRVersionFromMemory(&exr_version, w, src_image_len);
  53. if (ret != TINYEXR_SUCCESS) {
  54. return ERR_FILE_CORRUPT;
  55. }
  56. ret = ParseEXRHeaderFromMemory(&exr_header, &exr_version, w, src_image_len, &err);
  57. if (ret != TINYEXR_SUCCESS) {
  58. if (err) {
  59. ERR_PRINT(String(err));
  60. }
  61. return ERR_FILE_CORRUPT;
  62. }
  63. // Read HALF channel as FLOAT. (GH-13490)
  64. bool use_float16 = false;
  65. for (int i = 0; i < exr_header.num_channels; i++) {
  66. if (exr_header.pixel_types[i] == TINYEXR_PIXELTYPE_HALF) {
  67. use_float16 = true;
  68. exr_header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
  69. }
  70. }
  71. InitEXRImage(&exr_image);
  72. ret = LoadEXRImageFromMemory(&exr_image, &exr_header, w, src_image_len, &err);
  73. if (ret != TINYEXR_SUCCESS) {
  74. if (err) {
  75. ERR_PRINT(String(err));
  76. }
  77. return ERR_FILE_CORRUPT;
  78. }
  79. // RGBA
  80. int idxR = -1;
  81. int idxG = -1;
  82. int idxB = -1;
  83. int idxA = -1;
  84. for (int c = 0; c < exr_header.num_channels; c++) {
  85. if (strcmp(exr_header.channels[c].name, "R") == 0) {
  86. idxR = c;
  87. } else if (strcmp(exr_header.channels[c].name, "G") == 0) {
  88. idxG = c;
  89. } else if (strcmp(exr_header.channels[c].name, "B") == 0) {
  90. idxB = c;
  91. } else if (strcmp(exr_header.channels[c].name, "A") == 0) {
  92. idxA = c;
  93. } else if (strcmp(exr_header.channels[c].name, "Y") == 0) {
  94. idxR = c;
  95. idxG = c;
  96. idxB = c;
  97. }
  98. }
  99. // EXR image data loaded, now parse it into Godot-friendly image data
  100. PoolVector<uint8_t> imgdata;
  101. Image::Format format;
  102. int output_channels = 0;
  103. int channel_size = use_float16 ? 2 : 4;
  104. if (idxA != -1) {
  105. imgdata.resize(exr_image.width * exr_image.height * 4 * channel_size); //RGBA
  106. format = use_float16 ? Image::FORMAT_RGBAH : Image::FORMAT_RGBAF;
  107. output_channels = 4;
  108. } else if (idxB != -1) {
  109. ERR_FAIL_COND_V(idxG == -1, ERR_FILE_CORRUPT);
  110. ERR_FAIL_COND_V(idxR == -1, ERR_FILE_CORRUPT);
  111. imgdata.resize(exr_image.width * exr_image.height * 3 * channel_size); //RGB
  112. format = use_float16 ? Image::FORMAT_RGBH : Image::FORMAT_RGBF;
  113. output_channels = 3;
  114. } else if (idxG != -1) {
  115. ERR_FAIL_COND_V(idxR == -1, ERR_FILE_CORRUPT);
  116. imgdata.resize(exr_image.width * exr_image.height * 2 * channel_size); //RG
  117. format = use_float16 ? Image::FORMAT_RGH : Image::FORMAT_RGF;
  118. output_channels = 2;
  119. } else {
  120. ERR_FAIL_COND_V(idxR == -1, ERR_FILE_CORRUPT);
  121. imgdata.resize(exr_image.width * exr_image.height * 1 * channel_size); //R
  122. format = use_float16 ? Image::FORMAT_RH : Image::FORMAT_RF;
  123. output_channels = 1;
  124. }
  125. EXRTile single_image_tile;
  126. int num_tiles;
  127. int tile_width = 0;
  128. int tile_height = 0;
  129. const EXRTile *exr_tiles;
  130. if (!exr_header.tiled) {
  131. single_image_tile.images = exr_image.images;
  132. single_image_tile.width = exr_image.width;
  133. single_image_tile.height = exr_image.height;
  134. single_image_tile.level_x = exr_image.width;
  135. single_image_tile.level_y = exr_image.height;
  136. single_image_tile.offset_x = 0;
  137. single_image_tile.offset_y = 0;
  138. exr_tiles = &single_image_tile;
  139. num_tiles = 1;
  140. tile_width = exr_image.width;
  141. tile_height = exr_image.height;
  142. } else {
  143. tile_width = exr_header.tile_size_x;
  144. tile_height = exr_header.tile_size_y;
  145. num_tiles = exr_image.num_tiles;
  146. exr_tiles = exr_image.tiles;
  147. }
  148. //print_line("reading format: " + Image::get_format_name(format));
  149. {
  150. PoolVector<uint8_t>::Write imgdata_write = imgdata.write();
  151. uint8_t *wd = imgdata_write.ptr();
  152. uint16_t *iw16 = (uint16_t *)wd;
  153. float *iw32 = (float *)wd;
  154. // Assume `out_rgba` have enough memory allocated.
  155. for (int tile_index = 0; tile_index < num_tiles; tile_index++) {
  156. const EXRTile &tile = exr_tiles[tile_index];
  157. int tw = tile.width;
  158. int th = tile.height;
  159. const float *r_channel_start = reinterpret_cast<const float *>(tile.images[idxR]);
  160. const float *g_channel_start = nullptr;
  161. const float *b_channel_start = nullptr;
  162. const float *a_channel_start = nullptr;
  163. if (idxG != -1) {
  164. g_channel_start = reinterpret_cast<const float *>(tile.images[idxG]);
  165. }
  166. if (idxB != -1) {
  167. b_channel_start = reinterpret_cast<const float *>(tile.images[idxB]);
  168. }
  169. if (idxA != -1) {
  170. a_channel_start = reinterpret_cast<const float *>(tile.images[idxA]);
  171. }
  172. uint16_t *first_row_w16 = iw16 + (tile.offset_y * tile_height * exr_image.width + tile.offset_x * tile_width) * output_channels;
  173. float *first_row_w32 = iw32 + (tile.offset_y * tile_height * exr_image.width + tile.offset_x * tile_width) * output_channels;
  174. for (int y = 0; y < th; y++) {
  175. const float *r_channel = r_channel_start + y * tile_width;
  176. const float *g_channel = nullptr;
  177. const float *b_channel = nullptr;
  178. const float *a_channel = nullptr;
  179. if (g_channel_start) {
  180. g_channel = g_channel_start + y * tile_width;
  181. }
  182. if (b_channel_start) {
  183. b_channel = b_channel_start + y * tile_width;
  184. }
  185. if (a_channel_start) {
  186. a_channel = a_channel_start + y * tile_width;
  187. }
  188. if (use_float16) {
  189. uint16_t *row_w = first_row_w16 + (y * exr_image.width * output_channels);
  190. for (int x = 0; x < tw; x++) {
  191. Color color;
  192. color.r = *r_channel++;
  193. if (g_channel) {
  194. color.g = *g_channel++;
  195. }
  196. if (b_channel) {
  197. color.b = *b_channel++;
  198. }
  199. if (a_channel) {
  200. color.a = *a_channel++;
  201. }
  202. if (p_force_linear) {
  203. color = color.to_linear();
  204. }
  205. *row_w++ = Math::make_half_float(color.r);
  206. if (g_channel) {
  207. *row_w++ = Math::make_half_float(color.g);
  208. }
  209. if (b_channel) {
  210. *row_w++ = Math::make_half_float(color.b);
  211. }
  212. if (a_channel) {
  213. *row_w++ = Math::make_half_float(color.a);
  214. }
  215. }
  216. } else {
  217. float *row_w = first_row_w32 + (y * exr_image.width * output_channels);
  218. for (int x = 0; x < tw; x++) {
  219. Color color;
  220. color.r = *r_channel++;
  221. if (g_channel) {
  222. color.g = *g_channel++;
  223. }
  224. if (b_channel) {
  225. color.b = *b_channel++;
  226. }
  227. if (a_channel) {
  228. color.a = *a_channel++;
  229. }
  230. if (p_force_linear) {
  231. color = color.to_linear();
  232. }
  233. *row_w++ = color.r;
  234. if (g_channel) {
  235. *row_w++ = color.g;
  236. }
  237. if (b_channel) {
  238. *row_w++ = color.b;
  239. }
  240. if (a_channel) {
  241. *row_w++ = color.a;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. p_image->create(exr_image.width, exr_image.height, false, format, imgdata);
  249. img_write.release();
  250. FreeEXRHeader(&exr_header);
  251. FreeEXRImage(&exr_image);
  252. return OK;
  253. }
  254. void ImageLoaderTinyEXR::get_recognized_extensions(List<String> *p_extensions) const {
  255. p_extensions->push_back("exr");
  256. }
  257. ImageLoaderTinyEXR::ImageLoaderTinyEXR() {
  258. }