image_loader_tinyexr.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. int 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 w = src_image.write();
  40. f->get_buffer(&w[0], src_image_len);
  41. f->close();
  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 = NULL;
  50. InitEXRHeader(&exr_header);
  51. int ret = ParseEXRVersionFromMemory(&exr_version, w.ptr(), src_image_len);
  52. if (ret != TINYEXR_SUCCESS) {
  53. return ERR_FILE_CORRUPT;
  54. }
  55. ret = ParseEXRHeaderFromMemory(&exr_header, &exr_version, w.ptr(), src_image_len, &err);
  56. if (ret != TINYEXR_SUCCESS) {
  57. if (err) {
  58. ERR_PRINTS(String(err));
  59. }
  60. return ERR_FILE_CORRUPT;
  61. }
  62. // Read HALF channel as FLOAT. (GH-13490)
  63. for (int i = 0; i < exr_header.num_channels; i++) {
  64. if (exr_header.pixel_types[i] == TINYEXR_PIXELTYPE_HALF) {
  65. exr_header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
  66. }
  67. }
  68. InitEXRImage(&exr_image);
  69. ret = LoadEXRImageFromMemory(&exr_image, &exr_header, w.ptr(), src_image_len, &err);
  70. if (ret != TINYEXR_SUCCESS) {
  71. if (err) {
  72. ERR_PRINTS(String(err));
  73. }
  74. return ERR_FILE_CORRUPT;
  75. }
  76. // RGBA
  77. int idxR = -1;
  78. int idxG = -1;
  79. int idxB = -1;
  80. int idxA = -1;
  81. for (int c = 0; c < exr_header.num_channels; c++) {
  82. if (strcmp(exr_header.channels[c].name, "R") == 0) {
  83. idxR = c;
  84. } else if (strcmp(exr_header.channels[c].name, "G") == 0) {
  85. idxG = c;
  86. } else if (strcmp(exr_header.channels[c].name, "B") == 0) {
  87. idxB = c;
  88. } else if (strcmp(exr_header.channels[c].name, "A") == 0) {
  89. idxA = c;
  90. }
  91. }
  92. if (idxR == -1) {
  93. ERR_PRINT("TinyEXR: R channel not found.");
  94. // @todo { free exr_image }
  95. return ERR_FILE_CORRUPT;
  96. }
  97. if (idxG == -1) {
  98. ERR_PRINT("TinyEXR: G channel not found.")
  99. // @todo { free exr_image }
  100. return ERR_FILE_CORRUPT;
  101. }
  102. if (idxB == -1) {
  103. ERR_PRINT("TinyEXR: B channel not found.")
  104. // @todo { free exr_image }
  105. return ERR_FILE_CORRUPT;
  106. }
  107. // EXR image data loaded, now parse it into Godot-friendly image data
  108. PoolVector<uint8_t> imgdata;
  109. Image::Format format;
  110. int output_channels = 0;
  111. if (idxA != -1) {
  112. imgdata.resize(exr_image.width * exr_image.height * 8); //RGBA16
  113. format = Image::FORMAT_RGBAH;
  114. output_channels = 4;
  115. } else {
  116. imgdata.resize(exr_image.width * exr_image.height * 6); //RGB16
  117. format = Image::FORMAT_RGBH;
  118. output_channels = 3;
  119. }
  120. EXRTile single_image_tile;
  121. int num_tiles;
  122. int tile_width = 0;
  123. int tile_height = 0;
  124. const EXRTile *exr_tiles;
  125. if (!exr_header.tiled) {
  126. single_image_tile.images = exr_image.images;
  127. single_image_tile.width = exr_image.width;
  128. single_image_tile.height = exr_image.height;
  129. single_image_tile.level_x = exr_image.width;
  130. single_image_tile.level_y = exr_image.height;
  131. single_image_tile.offset_x = 0;
  132. single_image_tile.offset_y = 0;
  133. exr_tiles = &single_image_tile;
  134. num_tiles = 1;
  135. tile_width = exr_image.width;
  136. tile_height = exr_image.height;
  137. } else {
  138. tile_width = exr_header.tile_size_x;
  139. tile_height = exr_header.tile_size_y;
  140. num_tiles = exr_image.num_tiles;
  141. exr_tiles = exr_image.tiles;
  142. }
  143. {
  144. PoolVector<uint8_t>::Write wd = imgdata.write();
  145. uint16_t *iw = (uint16_t *)wd.ptr();
  146. // Assume `out_rgba` have enough memory allocated.
  147. for (int tile_index = 0; tile_index < num_tiles; tile_index++) {
  148. const EXRTile &tile = exr_tiles[tile_index];
  149. int tw = tile.width;
  150. int th = tile.height;
  151. const float *r_channel_start = reinterpret_cast<const float *>(tile.images[idxR]);
  152. const float *g_channel_start = reinterpret_cast<const float *>(tile.images[idxG]);
  153. const float *b_channel_start = reinterpret_cast<const float *>(tile.images[idxB]);
  154. const float *a_channel_start = NULL;
  155. if (idxA != -1) {
  156. a_channel_start = reinterpret_cast<const float *>(tile.images[idxA]);
  157. }
  158. uint16_t *first_row_w = iw + (tile.offset_y * tile_height * exr_image.width + tile.offset_x * tile_width) * output_channels;
  159. for (int y = 0; y < th; y++) {
  160. const float *r_channel = r_channel_start + y * tile_width;
  161. const float *g_channel = g_channel_start + y * tile_width;
  162. const float *b_channel = b_channel_start + y * tile_width;
  163. const float *a_channel = NULL;
  164. if (a_channel_start) {
  165. a_channel = a_channel_start + y * tile_width;
  166. }
  167. uint16_t *row_w = first_row_w + (y * exr_image.width * output_channels);
  168. for (int x = 0; x < tw; x++) {
  169. Color color(*r_channel++, *g_channel++, *b_channel++);
  170. if (p_force_linear)
  171. color = color.to_linear();
  172. *row_w++ = Math::make_half_float(color.r);
  173. *row_w++ = Math::make_half_float(color.g);
  174. *row_w++ = Math::make_half_float(color.b);
  175. if (idxA != -1) {
  176. *row_w++ = Math::make_half_float(*a_channel++);
  177. }
  178. }
  179. }
  180. }
  181. }
  182. p_image->create(exr_image.width, exr_image.height, false, format, imgdata);
  183. w = PoolVector<uint8_t>::Write();
  184. FreeEXRHeader(&exr_header);
  185. FreeEXRImage(&exr_image);
  186. return OK;
  187. }
  188. void ImageLoaderTinyEXR::get_recognized_extensions(List<String> *p_extensions) const {
  189. p_extensions->push_back("exr");
  190. }
  191. ImageLoaderTinyEXR::ImageLoaderTinyEXR() {
  192. }