2
0

image_loader_tinyexr.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #include "core/io/file_access_memory.h"
  34. Error ImageLoaderTinyEXR::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
  35. Vector<uint8_t> src_image;
  36. uint64_t src_image_len = f->get_length();
  37. ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
  38. src_image.resize(src_image_len);
  39. uint8_t *w = src_image.ptrw();
  40. f->get_buffer(&w[0], src_image_len);
  41. // Re-implementation of tinyexr's LoadEXRFromMemory using Godot types to store the Image data
  42. // and Godot's error codes.
  43. // When debugging after updating the thirdparty library, check that we're still in sync with
  44. // their API usage in LoadEXRFromMemory.
  45. EXRVersion exr_version;
  46. EXRImage exr_image;
  47. EXRHeader exr_header;
  48. const char *err = nullptr;
  49. InitEXRHeader(&exr_header);
  50. int ret = ParseEXRVersionFromMemory(&exr_version, w, src_image_len);
  51. if (ret != TINYEXR_SUCCESS) {
  52. return ERR_FILE_CORRUPT;
  53. }
  54. ret = ParseEXRHeaderFromMemory(&exr_header, &exr_version, w, src_image_len, &err);
  55. if (ret != TINYEXR_SUCCESS) {
  56. if (err) {
  57. ERR_PRINT(String(err));
  58. FreeEXRErrorMessage(err);
  59. }
  60. return ERR_FILE_CORRUPT;
  61. }
  62. // Read HALF channel as FLOAT. (GH-13490)
  63. bool use_float16 = false;
  64. for (int i = 0; i < exr_header.num_channels; i++) {
  65. if (exr_header.pixel_types[i] == TINYEXR_PIXELTYPE_HALF) {
  66. use_float16 = true;
  67. exr_header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
  68. }
  69. }
  70. InitEXRImage(&exr_image);
  71. ret = LoadEXRImageFromMemory(&exr_image, &exr_header, w, src_image_len, &err);
  72. if (ret != TINYEXR_SUCCESS) {
  73. if (err) {
  74. ERR_PRINT(String(err));
  75. FreeEXRErrorMessage(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. Vector<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. uint8_t *wd = imgdata.ptrw();
  151. uint16_t *iw16 = (uint16_t *)wd;
  152. float *iw32 = (float *)wd;
  153. // Assume `out_rgba` have enough memory allocated.
  154. for (int tile_index = 0; tile_index < num_tiles; tile_index++) {
  155. const EXRTile &tile = exr_tiles[tile_index];
  156. int tw = tile.width;
  157. int th = tile.height;
  158. const float *r_channel_start = reinterpret_cast<const float *>(tile.images[idxR]);
  159. const float *g_channel_start = nullptr;
  160. const float *b_channel_start = nullptr;
  161. const float *a_channel_start = nullptr;
  162. if (idxG != -1) {
  163. g_channel_start = reinterpret_cast<const float *>(tile.images[idxG]);
  164. }
  165. if (idxB != -1) {
  166. b_channel_start = reinterpret_cast<const float *>(tile.images[idxB]);
  167. }
  168. if (idxA != -1) {
  169. a_channel_start = reinterpret_cast<const float *>(tile.images[idxA]);
  170. }
  171. uint16_t *first_row_w16 = iw16 + (tile.offset_y * tile_height * exr_image.width + tile.offset_x * tile_width) * output_channels;
  172. float *first_row_w32 = iw32 + (tile.offset_y * tile_height * exr_image.width + tile.offset_x * tile_width) * output_channels;
  173. for (int y = 0; y < th; y++) {
  174. const float *r_channel = r_channel_start + y * tile_width;
  175. const float *g_channel = nullptr;
  176. const float *b_channel = nullptr;
  177. const float *a_channel = nullptr;
  178. if (g_channel_start) {
  179. g_channel = g_channel_start + y * tile_width;
  180. }
  181. if (b_channel_start) {
  182. b_channel = b_channel_start + y * tile_width;
  183. }
  184. if (a_channel_start) {
  185. a_channel = a_channel_start + y * tile_width;
  186. }
  187. if (use_float16) {
  188. uint16_t *row_w = first_row_w16 + (y * exr_image.width * output_channels);
  189. for (int x = 0; x < tw; x++) {
  190. Color color;
  191. color.r = *r_channel++;
  192. if (g_channel) {
  193. color.g = *g_channel++;
  194. }
  195. if (b_channel) {
  196. color.b = *b_channel++;
  197. }
  198. if (a_channel) {
  199. color.a = *a_channel++;
  200. }
  201. if (p_flags & FLAG_FORCE_LINEAR) {
  202. color = color.srgb_to_linear();
  203. }
  204. *row_w++ = Math::make_half_float(color.r);
  205. if (g_channel) {
  206. *row_w++ = Math::make_half_float(color.g);
  207. }
  208. if (b_channel) {
  209. *row_w++ = Math::make_half_float(color.b);
  210. }
  211. if (a_channel) {
  212. *row_w++ = Math::make_half_float(color.a);
  213. }
  214. }
  215. } else {
  216. float *row_w = first_row_w32 + (y * exr_image.width * output_channels);
  217. for (int x = 0; x < tw; x++) {
  218. Color color;
  219. color.r = *r_channel++;
  220. if (g_channel) {
  221. color.g = *g_channel++;
  222. }
  223. if (b_channel) {
  224. color.b = *b_channel++;
  225. }
  226. if (a_channel) {
  227. color.a = *a_channel++;
  228. }
  229. if (p_flags & FLAG_FORCE_LINEAR) {
  230. color = color.srgb_to_linear();
  231. }
  232. *row_w++ = color.r;
  233. if (g_channel) {
  234. *row_w++ = color.g;
  235. }
  236. if (b_channel) {
  237. *row_w++ = color.b;
  238. }
  239. if (a_channel) {
  240. *row_w++ = color.a;
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. p_image->set_data(exr_image.width, exr_image.height, false, format, imgdata);
  248. FreeEXRHeader(&exr_header);
  249. FreeEXRImage(&exr_image);
  250. return OK;
  251. }
  252. void ImageLoaderTinyEXR::get_recognized_extensions(List<String> *p_extensions) const {
  253. p_extensions->push_back("exr");
  254. }
  255. static Ref<Image> _tinyexr_mem_loader_func(const uint8_t *p_exr, int p_size) {
  256. Ref<FileAccessMemory> memfile;
  257. memfile.instantiate();
  258. Error open_memfile_error = memfile->open_custom(p_exr, p_size);
  259. ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for EXR image buffer.");
  260. Ref<Image> img;
  261. img.instantiate();
  262. Error load_error = ImageLoaderTinyEXR().load_image(img, memfile, false, 1.0f);
  263. ERR_FAIL_COND_V_MSG(load_error, Ref<Image>(), "Failed to load EXR image.");
  264. return img;
  265. }
  266. ImageLoaderTinyEXR::ImageLoaderTinyEXR() {
  267. Image::_exr_mem_loader_func = _tinyexr_mem_loader_func;
  268. }