image_loader_tinyexr.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "os/os.h"
  32. #include "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. if (idxA > 0) {
  111. imgdata.resize(exr_image.width * exr_image.height * 8); //RGBA16
  112. format = Image::FORMAT_RGBAH;
  113. } else {
  114. imgdata.resize(exr_image.width * exr_image.height * 6); //RGB16
  115. format = Image::FORMAT_RGBH;
  116. }
  117. {
  118. PoolVector<uint8_t>::Write wd = imgdata.write();
  119. uint16_t *iw = (uint16_t *)wd.ptr();
  120. // Assume `out_rgba` have enough memory allocated.
  121. for (int i = 0; i < exr_image.width * exr_image.height; i++) {
  122. Color color(
  123. reinterpret_cast<float **>(exr_image.images)[idxR][i],
  124. reinterpret_cast<float **>(exr_image.images)[idxG][i],
  125. reinterpret_cast<float **>(exr_image.images)[idxB][i]);
  126. if (p_force_linear)
  127. color = color.to_linear();
  128. *iw++ = Math::make_half_float(color.r);
  129. *iw++ = Math::make_half_float(color.g);
  130. *iw++ = Math::make_half_float(color.b);
  131. if (idxA > 0) {
  132. *iw++ = Math::make_half_float(reinterpret_cast<float **>(exr_image.images)[idxA][i]);
  133. }
  134. }
  135. }
  136. p_image->create(exr_image.width, exr_image.height, false, format, imgdata);
  137. w = PoolVector<uint8_t>::Write();
  138. FreeEXRHeader(&exr_header);
  139. FreeEXRImage(&exr_image);
  140. return OK;
  141. }
  142. void ImageLoaderTinyEXR::get_recognized_extensions(List<String> *p_extensions) const {
  143. p_extensions->push_back("exr");
  144. }
  145. ImageLoaderTinyEXR::ImageLoaderTinyEXR() {
  146. }