image_loader_tinyexr.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*************************************************************************/
  2. /* image_loader_jpegd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. EXRVersion exr_version;
  43. EXRImage exr_image;
  44. EXRHeader exr_header;
  45. const char *err = NULL;
  46. InitEXRHeader(&exr_header);
  47. int ret = ParseEXRVersionFromMemory(&exr_version, w.ptr(), src_image_len);
  48. if (ret != TINYEXR_SUCCESS) {
  49. return ERR_FILE_CORRUPT;
  50. }
  51. ret = ParseEXRHeaderFromMemory(&exr_header, &exr_version, w.ptr(), src_image_len, &err);
  52. if (ret != TINYEXR_SUCCESS) {
  53. if (err) {
  54. ERR_PRINTS(String(err));
  55. }
  56. return ERR_FILE_CORRUPT;
  57. }
  58. InitEXRImage(&exr_image);
  59. ret = LoadEXRImageFromMemory(&exr_image, &exr_header, w.ptr(), src_image_len, &err);
  60. if (ret != TINYEXR_SUCCESS) {
  61. if (err) {
  62. ERR_PRINTS(String(err));
  63. }
  64. return ERR_FILE_CORRUPT;
  65. }
  66. // RGBA
  67. int idxR = -1;
  68. int idxG = -1;
  69. int idxB = -1;
  70. int idxA = -1;
  71. for (int c = 0; c < exr_header.num_channels; c++) {
  72. if (strcmp(exr_header.channels[c].name, "R") == 0) {
  73. idxR = c;
  74. } else if (strcmp(exr_header.channels[c].name, "G") == 0) {
  75. idxG = c;
  76. } else if (strcmp(exr_header.channels[c].name, "B") == 0) {
  77. idxB = c;
  78. } else if (strcmp(exr_header.channels[c].name, "A") == 0) {
  79. idxA = c;
  80. }
  81. }
  82. if (idxR == -1) {
  83. ERR_PRINT("R channel not found");
  84. // @todo { free exr_image }
  85. return ERR_FILE_CORRUPT;
  86. }
  87. if (idxG == -1) {
  88. ERR_PRINT("G channel not found\n")
  89. // @todo { free exr_image }
  90. return ERR_FILE_CORRUPT;
  91. }
  92. if (idxB == -1) {
  93. ERR_PRINT("B channel not found\n")
  94. // @todo { free exr_image }
  95. return ERR_FILE_CORRUPT;
  96. }
  97. PoolVector<uint8_t> imgdata;
  98. Image::Format format;
  99. if (idxA > 0) {
  100. imgdata.resize(exr_image.width * exr_image.height * 8); //RGBA16
  101. format = Image::FORMAT_RGBAH;
  102. } else {
  103. imgdata.resize(exr_image.width * exr_image.height * 6); //RGB16
  104. format = Image::FORMAT_RGBH;
  105. }
  106. {
  107. PoolVector<uint8_t>::Write wd = imgdata.write();
  108. uint16_t *iw = (uint16_t *)wd.ptr();
  109. // Assume `out_rgba` have enough memory allocated.
  110. for (int i = 0; i < exr_image.width * exr_image.height; i++) {
  111. Color color(
  112. reinterpret_cast<float **>(exr_image.images)[idxR][i],
  113. reinterpret_cast<float **>(exr_image.images)[idxG][i],
  114. reinterpret_cast<float **>(exr_image.images)[idxB][i]);
  115. if (p_force_linear)
  116. color = color.to_linear();
  117. *iw++ = Math::make_half_float(color.r);
  118. *iw++ = Math::make_half_float(color.g);
  119. *iw++ = Math::make_half_float(color.b);
  120. if (idxA > 0) {
  121. *iw++ = Math::make_half_float(reinterpret_cast<float **>(exr_image.images)[idxA][i]);
  122. }
  123. }
  124. }
  125. print_line("EXR w: " + itos(exr_image.width) + " h:" + itos(exr_image.height) + " format " + Image::get_format_name(format));
  126. p_image->create(exr_image.width, exr_image.height, false, format, imgdata);
  127. w = PoolVector<uint8_t>::Write();
  128. return OK;
  129. }
  130. void ImageLoaderTinyEXR::get_recognized_extensions(List<String> *p_extensions) const {
  131. p_extensions->push_back("exr");
  132. }
  133. ImageLoaderTinyEXR::ImageLoaderTinyEXR() {
  134. }