123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- #include "image_loader_hdr.h"
- #include "core/os/os.h"
- #include "core/print_string.h"
- #include "thirdparty/tinyexr/tinyexr.h"
- Error ImageLoaderHDR::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
- String header = f->get_token();
- ERR_FAIL_COND_V(header != "#?RADIANCE" && header != "#?RGBE", ERR_FILE_UNRECOGNIZED);
- while (true) {
- String line = f->get_line();
- ERR_FAIL_COND_V(f->eof_reached(), ERR_FILE_UNRECOGNIZED);
- if (line == "")
- break;
- if (line.begins_with("FORMAT=")) {
- if (line != "FORMAT=32-bit_rle_rgbe") {
- ERR_EXPLAIN("Only 32-bit_rle_rgbe is supported for HDR files.");
- return ERR_FILE_UNRECOGNIZED;
- }
- } else if (!line.begins_with("#")) {
- WARN_PRINTS("Ignoring unsupported header information in HDR : " + line);
- }
- }
- String token = f->get_token();
- ERR_FAIL_COND_V(token != "-Y", ERR_FILE_CORRUPT);
- int height = f->get_token().to_int();
- token = f->get_token();
- ERR_FAIL_COND_V(token != "+X", ERR_FILE_CORRUPT);
- int width = f->get_line().to_int();
- PoolVector<uint8_t> imgdata;
- imgdata.resize(height * width * sizeof(uint32_t));
- {
- PoolVector<uint8_t>::Write w = imgdata.write();
- uint8_t *ptr = (uint8_t *)w.ptr();
- if (width < 8 || width >= 32768) {
-
- f->get_buffer(ptr, width * height * 4);
- } else {
-
- for (int j = 0; j < height; ++j) {
- int c1 = f->get_8();
- int c2 = f->get_8();
- int len = f->get_8();
- if (c1 != 2 || c2 != 2 || (len & 0x80)) {
-
-
- ptr[(j * width) * 4 + 0] = uint8_t(c1);
- ptr[(j * width) * 4 + 1] = uint8_t(c2);
- ptr[(j * width) * 4 + 2] = uint8_t(len);
- ptr[(j * width) * 4 + 3] = f->get_8();
- f->get_buffer(&ptr[(j * width + 1) * 4], (width - 1) * 4);
- continue;
- }
- len <<= 8;
- len |= f->get_8();
- if (len != width) {
- ERR_EXPLAIN("invalid decoded scanline length, corrupt HDR");
- ERR_FAIL_V(ERR_FILE_CORRUPT);
- }
- for (int k = 0; k < 4; ++k) {
- int i = 0;
- while (i < width) {
- int count = f->get_8();
- if (count > 128) {
-
- int value = f->get_8();
- count -= 128;
- for (int z = 0; z < count; ++z)
- ptr[(j * width + i++) * 4 + k] = uint8_t(value);
- } else {
-
- for (int z = 0; z < count; ++z)
- ptr[(j * width + i++) * 4 + k] = f->get_8();
- }
- }
- }
- }
- }
-
- for (int i = 0; i < width * height; i++) {
- float exp = pow(2.0f, ptr[3] - 128.0f);
- Color c(
- ptr[0] * exp / 255.0,
- ptr[1] * exp / 255.0,
- ptr[2] * exp / 255.0);
- if (p_force_linear) {
- c = c.to_linear();
- }
- *(uint32_t *)ptr = c.to_rgbe9995();
- ptr += 4;
- }
- }
- p_image->create(width, height, false, Image::FORMAT_RGBE9995, imgdata);
- return OK;
- }
- void ImageLoaderHDR::get_recognized_extensions(List<String> *p_extensions) const {
- p_extensions->push_back("hdr");
- }
- ImageLoaderHDR::ImageLoaderHDR() {
- }
|