image_loader_hdr.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**************************************************************************/
  2. /* image_loader_hdr.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_hdr.h"
  31. Error ImageLoaderHDR::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
  32. String header = f->get_token();
  33. ERR_FAIL_COND_V_MSG(header != "#?RADIANCE" && header != "#?RGBE", ERR_FILE_UNRECOGNIZED, "Unsupported header information in HDR: " + header + ".");
  34. while (true) {
  35. String line = f->get_line();
  36. ERR_FAIL_COND_V(f->eof_reached(), ERR_FILE_UNRECOGNIZED);
  37. if (line.is_empty()) { // empty line indicates end of header
  38. break;
  39. }
  40. if (line.begins_with("FORMAT=")) { // leave option to implement other commands
  41. ERR_FAIL_COND_V_MSG(line != "FORMAT=32-bit_rle_rgbe", ERR_FILE_UNRECOGNIZED, "Only 32-bit_rle_rgbe is supported for HDR files.");
  42. } else if (!line.begins_with("#")) { // not comment
  43. WARN_PRINT("Ignoring unsupported header information in HDR: " + line + ".");
  44. }
  45. }
  46. String token = f->get_token();
  47. ERR_FAIL_COND_V(token != "-Y", ERR_FILE_CORRUPT);
  48. int height = f->get_token().to_int();
  49. token = f->get_token();
  50. ERR_FAIL_COND_V(token != "+X", ERR_FILE_CORRUPT);
  51. int width = f->get_line().to_int();
  52. Vector<uint8_t> imgdata;
  53. imgdata.resize(height * width * (int)sizeof(uint32_t));
  54. {
  55. uint8_t *ptr = imgdata.ptrw();
  56. Vector<uint8_t> temp_read_data;
  57. temp_read_data.resize(128);
  58. uint8_t *temp_read_ptr = temp_read_data.ptrw();
  59. if (width < 8 || width >= 32768) {
  60. // Read flat data
  61. f->get_buffer(ptr, (uint64_t)width * height * 4);
  62. } else {
  63. // Read RLE-encoded data
  64. for (int j = 0; j < height; ++j) {
  65. int c1 = f->get_8();
  66. int c2 = f->get_8();
  67. int len = f->get_8();
  68. if (c1 != 2 || c2 != 2 || (len & 0x80)) {
  69. // not run-length encoded, so we have to actually use THIS data as a decoded
  70. // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
  71. ptr[(j * width) * 4 + 0] = uint8_t(c1);
  72. ptr[(j * width) * 4 + 1] = uint8_t(c2);
  73. ptr[(j * width) * 4 + 2] = uint8_t(len);
  74. ptr[(j * width) * 4 + 3] = f->get_8();
  75. f->get_buffer(&ptr[(j * width + 1) * 4], (width - 1) * 4);
  76. continue;
  77. }
  78. len <<= 8;
  79. len |= f->get_8();
  80. ERR_FAIL_COND_V_MSG(len != width, ERR_FILE_CORRUPT, "Invalid decoded scanline length, corrupt HDR.");
  81. for (int k = 0; k < 4; ++k) {
  82. int i = 0;
  83. while (i < width) {
  84. int count = f->get_8();
  85. if (count > 128) {
  86. // Run
  87. int value = f->get_8();
  88. count -= 128;
  89. for (int z = 0; z < count; ++z) {
  90. ptr[(j * width + i++) * 4 + k] = uint8_t(value);
  91. }
  92. } else {
  93. // Dump
  94. f->get_buffer(temp_read_ptr, count);
  95. for (int z = 0; z < count; ++z) {
  96. ptr[(j * width + i++) * 4 + k] = temp_read_ptr[z];
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. const bool force_linear = p_flags & FLAG_FORCE_LINEAR;
  104. //convert
  105. for (int i = 0; i < width * height; i++) {
  106. int e = ptr[3] - 128;
  107. if (force_linear || (e < -15 || e > 15)) {
  108. float exp = pow(2.0f, e);
  109. Color c(ptr[0] * exp / 255.0, ptr[1] * exp / 255.0, ptr[2] * exp / 255.0);
  110. if (force_linear) {
  111. c = c.srgb_to_linear();
  112. }
  113. *(uint32_t *)ptr = c.to_rgbe9995();
  114. } else {
  115. // https://github.com/george-steel/rgbe-rs/blob/e7cc33b7f42b4eb3272c166dac75385e48687c92/src/types.rs#L123-L129
  116. uint32_t e5 = (uint32_t)(e + 15);
  117. *(uint32_t *)ptr = ((e5 << 27) | ((uint32_t)ptr[2] << 19) | ((uint32_t)ptr[1] << 10) | ((uint32_t)ptr[0] << 1));
  118. }
  119. ptr += 4;
  120. }
  121. }
  122. p_image->set_data(width, height, false, Image::FORMAT_RGBE9995, imgdata);
  123. return OK;
  124. }
  125. void ImageLoaderHDR::get_recognized_extensions(List<String> *p_extensions) const {
  126. p_extensions->push_back("hdr");
  127. }
  128. ImageLoaderHDR::ImageLoaderHDR() {
  129. }