image_loader_tinyexr.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/string/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, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
  36. Vector<uint8_t> src_image;
  37. uint64_t src_image_len = f->get_length();
  38. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  39. src_image.resize(src_image_len);
  40. uint8_t *w = src_image.ptrw();
  41. f->get_buffer(&w[0], src_image_len);
  42. // Re-implementation of tinyexr's LoadEXRFromMemory using Godot types to store the Image data
  43. // and Godot's error codes.
  44. // When debugging after updating the thirdparty library, check that we're still in sync with
  45. // their API usage in LoadEXRFromMemory.
  46. EXRVersion exr_version;
  47. EXRImage exr_image;
  48. EXRHeader exr_header;
  49. const char *err = nullptr;
  50. InitEXRHeader(&exr_header);
  51. int ret = ParseEXRVersionFromMemory(&exr_version, w, src_image_len);
  52. if (ret != TINYEXR_SUCCESS) {
  53. return ERR_FILE_CORRUPT;
  54. }
  55. ret = ParseEXRHeaderFromMemory(&exr_header, &exr_version, w, src_image_len, &err);
  56. if (ret != TINYEXR_SUCCESS) {
  57. if (err) {
  58. ERR_PRINT(String(err));
  59. FreeEXRErrorMessage(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. FreeEXRErrorMessage(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. Vector<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. uint8_t *wd = imgdata.ptrw();
  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_flags & FLAG_FORCE_LINEAR) {
  203. color = color.srgb_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_flags & FLAG_FORCE_LINEAR) {
  231. color = color.srgb_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->set_data(exr_image.width, exr_image.height, false, format, imgdata);
  249. FreeEXRHeader(&exr_header);
  250. FreeEXRImage(&exr_image);
  251. return OK;
  252. }
  253. void ImageLoaderTinyEXR::get_recognized_extensions(List<String> *p_extensions) const {
  254. p_extensions->push_back("exr");
  255. }
  256. ImageLoaderTinyEXR::ImageLoaderTinyEXR() {
  257. }