image_loader_tinyexr.cpp 9.6 KB

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