image_loader_hdr.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_hdr.h"
  31. #include "os/os.h"
  32. #include "print_string.h"
  33. #include "thirdparty/tinyexr/tinyexr.h"
  34. Error ImageLoaderHDR::load_image(Ref<Image> p_image, FileAccess *f) {
  35. String header = f->get_token();
  36. print_line("HEADER: " + header);
  37. ERR_FAIL_COND_V(header != "#?RADIANCE", ERR_FILE_UNRECOGNIZED);
  38. String format = f->get_token();
  39. print_line("FORMAT: " + format);
  40. ERR_FAIL_COND_V(format != "FORMAT=32-bit_rle_rgbe", ERR_FILE_UNRECOGNIZED);
  41. String token = f->get_token();
  42. ERR_FAIL_COND_V(token != "-Y", ERR_FILE_CORRUPT);
  43. int height = f->get_token().to_int();
  44. token = f->get_token();
  45. ERR_FAIL_COND_V(token != "+X", ERR_FILE_CORRUPT);
  46. int width = f->get_line().to_int();
  47. print_line("HDR w: " + itos(width) + " h:" + itos(height));
  48. PoolVector<uint8_t> imgdata;
  49. imgdata.resize(height * width * sizeof(uint32_t));
  50. {
  51. PoolVector<uint8_t>::Write w = imgdata.write();
  52. uint8_t *ptr = (uint8_t *)w.ptr();
  53. if (width < 8 || width >= 32768) {
  54. // Read flat data
  55. f->get_buffer(ptr, width * height * 4);
  56. } else {
  57. // Read RLE-encoded data
  58. for (int j = 0; j < height; ++j) {
  59. int c1 = f->get_8();
  60. int c2 = f->get_8();
  61. int len = f->get_8();
  62. if (c1 != 2 || c2 != 2 || (len & 0x80)) {
  63. // not run-length encoded, so we have to actually use THIS data as a decoded
  64. // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
  65. ptr[(j * width) * 4 + 0] = uint8_t(c1);
  66. ptr[(j * width) * 4 + 1] = uint8_t(c2);
  67. ptr[(j * width) * 4 + 2] = uint8_t(len);
  68. ptr[(j * width) * 4 + 3] = f->get_8();
  69. f->get_buffer(&ptr[(j * width + 1) * 4], (width - 1) * 4);
  70. continue;
  71. }
  72. len <<= 8;
  73. len |= f->get_8();
  74. print_line("line: " + itos(len));
  75. if (len != width) {
  76. ERR_EXPLAIN("invalid decoded scanline length, corrupt HDR");
  77. ERR_FAIL_V(ERR_FILE_CORRUPT);
  78. }
  79. for (int k = 0; k < 4; ++k) {
  80. int i = 0;
  81. while (i < width) {
  82. int count = f->get_8();
  83. if (count > 128) {
  84. // Run
  85. int value = f->get_8();
  86. count -= 128;
  87. for (int z = 0; z < count; ++z)
  88. ptr[(j * width + i++) * 4 + k] = uint8_t(value);
  89. } else {
  90. // Dump
  91. for (int z = 0; z < count; ++z)
  92. ptr[(j * width + i++) * 4 + k] = f->get_8();
  93. }
  94. }
  95. }
  96. }
  97. }
  98. //convert
  99. for (int i = 0; i < width * height; i++) {
  100. float exp = pow(2.0f, ptr[3] - 128.0f);
  101. Color c(
  102. ptr[0] * exp / 255.0,
  103. ptr[1] * exp / 255.0,
  104. ptr[2] * exp / 255.0);
  105. *(uint32_t *)ptr = c.to_rgbe9995();
  106. ptr += 4;
  107. }
  108. }
  109. p_image->create(width, height, false, Image::FORMAT_RGBE9995, imgdata);
  110. return OK;
  111. }
  112. void ImageLoaderHDR::get_recognized_extensions(List<String> *p_extensions) const {
  113. p_extensions->push_back("hdr");
  114. }
  115. ImageLoaderHDR::ImageLoaderHDR() {
  116. }