image_loader_tinyexr.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 (exr_header.num_channels == 1) {
  93. // Grayscale channel only.
  94. idxR = 0;
  95. idxG = 0;
  96. idxB = 0;
  97. idxA = 0;
  98. } else {
  99. // Assume RGB(A)
  100. if (idxR == -1) {
  101. ERR_PRINT("TinyEXR: R channel not found.");
  102. // @todo { free exr_image }
  103. return ERR_FILE_CORRUPT;
  104. }
  105. if (idxG == -1) {
  106. ERR_PRINT("TinyEXR: G channel not found.")
  107. // @todo { free exr_image }
  108. return ERR_FILE_CORRUPT;
  109. }
  110. if (idxB == -1) {
  111. ERR_PRINT("TinyEXR: B channel not found.")
  112. // @todo { free exr_image }
  113. return ERR_FILE_CORRUPT;
  114. }
  115. }
  116. // EXR image data loaded, now parse it into Godot-friendly image data
  117. PoolVector<uint8_t> imgdata;
  118. Image::Format format;
  119. int output_channels = 0;
  120. if (idxA != -1) {
  121. imgdata.resize(exr_image.width * exr_image.height * 8); //RGBA16
  122. format = Image::FORMAT_RGBAH;
  123. output_channels = 4;
  124. } else {
  125. imgdata.resize(exr_image.width * exr_image.height * 6); //RGB16
  126. format = Image::FORMAT_RGBH;
  127. output_channels = 3;
  128. }
  129. EXRTile single_image_tile;
  130. int num_tiles;
  131. int tile_width = 0;
  132. int tile_height = 0;
  133. const EXRTile *exr_tiles;
  134. if (!exr_header.tiled) {
  135. single_image_tile.images = exr_image.images;
  136. single_image_tile.width = exr_image.width;
  137. single_image_tile.height = exr_image.height;
  138. single_image_tile.level_x = exr_image.width;
  139. single_image_tile.level_y = exr_image.height;
  140. single_image_tile.offset_x = 0;
  141. single_image_tile.offset_y = 0;
  142. exr_tiles = &single_image_tile;
  143. num_tiles = 1;
  144. tile_width = exr_image.width;
  145. tile_height = exr_image.height;
  146. } else {
  147. tile_width = exr_header.tile_size_x;
  148. tile_height = exr_header.tile_size_y;
  149. num_tiles = exr_image.num_tiles;
  150. exr_tiles = exr_image.tiles;
  151. }
  152. {
  153. PoolVector<uint8_t>::Write wd = imgdata.write();
  154. uint16_t *iw = (uint16_t *)wd.ptr();
  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 = reinterpret_cast<const float *>(tile.images[idxG]);
  162. const float *b_channel_start = reinterpret_cast<const float *>(tile.images[idxB]);
  163. const float *a_channel_start = NULL;
  164. if (idxA != -1) {
  165. a_channel_start = reinterpret_cast<const float *>(tile.images[idxA]);
  166. }
  167. uint16_t *first_row_w = iw + (tile.offset_y * tile_height * exr_image.width + tile.offset_x * tile_width) * output_channels;
  168. for (int y = 0; y < th; y++) {
  169. const float *r_channel = r_channel_start + y * tile_width;
  170. const float *g_channel = g_channel_start + y * tile_width;
  171. const float *b_channel = b_channel_start + y * tile_width;
  172. const float *a_channel = NULL;
  173. if (a_channel_start) {
  174. a_channel = a_channel_start + y * tile_width;
  175. }
  176. uint16_t *row_w = first_row_w + (y * exr_image.width * output_channels);
  177. for (int x = 0; x < tw; x++) {
  178. Color color(*r_channel++, *g_channel++, *b_channel++);
  179. if (p_force_linear)
  180. color = color.to_linear();
  181. *row_w++ = Math::make_half_float(color.r);
  182. *row_w++ = Math::make_half_float(color.g);
  183. *row_w++ = Math::make_half_float(color.b);
  184. if (idxA != -1) {
  185. *row_w++ = Math::make_half_float(*a_channel++);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. p_image->create(exr_image.width, exr_image.height, false, format, imgdata);
  192. w = PoolVector<uint8_t>::Write();
  193. FreeEXRHeader(&exr_header);
  194. FreeEXRImage(&exr_image);
  195. return OK;
  196. }
  197. void ImageLoaderTinyEXR::get_recognized_extensions(List<String> *p_extensions) const {
  198. p_extensions->push_back("exr");
  199. }
  200. ImageLoaderTinyEXR::ImageLoaderTinyEXR() {
  201. }