image_loader_tinyexr.cpp 9.6 KB

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