2017-05-29 02:46:48 +02:00
|
|
|
/**************************************************************************/
|
2017-09-01 16:07:55 +02:00
|
|
|
/* image_loader_hdr.cpp */
|
2017-05-29 02:46:48 +02:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
#include "image_loader_hdr.h"
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/os.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/string/print_string.h"
|
2017-05-29 02:46:48 +02:00
|
|
|
|
2022-07-08 15:01:02 +02:00
|
|
|
Error ImageLoaderHDR::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
|
2017-05-29 02:46:48 +02:00
|
|
|
String header = f->get_token();
|
|
|
|
|
2019-08-17 13:04:33 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(header != "#?RADIANCE" && header != "#?RGBE", ERR_FILE_UNRECOGNIZED, "Unsupported header information in HDR: " + header + ".");
|
2017-05-30 03:11:33 +02:00
|
|
|
|
|
|
|
while (true) {
|
2018-03-11 19:21:09 +01:00
|
|
|
String line = f->get_line();
|
2017-05-30 03:11:33 +02:00
|
|
|
ERR_FAIL_COND_V(f->eof_reached(), ERR_FILE_UNRECOGNIZED);
|
2021-12-09 10:42:46 +01:00
|
|
|
if (line.is_empty()) { // empty line indicates end of header
|
2017-05-30 03:11:33 +02:00
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-03-11 19:21:09 +01:00
|
|
|
if (line.begins_with("FORMAT=")) { // leave option to implement other commands
|
2019-08-17 13:04:33 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(line != "FORMAT=32-bit_rle_rgbe", ERR_FILE_UNRECOGNIZED, "Only 32-bit_rle_rgbe is supported for HDR files.");
|
2018-03-11 19:21:09 +01:00
|
|
|
} else if (!line.begins_with("#")) { // not comment
|
2019-11-07 09:44:15 +01:00
|
|
|
WARN_PRINT("Ignoring unsupported header information in HDR: " + line + ".");
|
2018-03-11 19:21:09 +01:00
|
|
|
}
|
2017-05-30 03:11:33 +02:00
|
|
|
}
|
2017-05-29 02:46:48 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> imgdata;
|
2017-05-29 02:46:48 +02:00
|
|
|
|
2021-09-08 06:25:35 +02:00
|
|
|
imgdata.resize(height * width * (int)sizeof(uint32_t));
|
2017-05-29 02:46:48 +02:00
|
|
|
|
|
|
|
{
|
2024-08-08 19:13:00 +02:00
|
|
|
uint8_t *ptr = imgdata.ptrw();
|
2017-05-29 02:46:48 +02:00
|
|
|
|
2024-08-08 19:13:00 +02:00
|
|
|
Vector<uint8_t> temp_read_data;
|
|
|
|
temp_read_data.resize(128);
|
|
|
|
uint8_t *temp_read_ptr = temp_read_data.ptrw();
|
2017-05-29 02:46:48 +02:00
|
|
|
|
|
|
|
if (width < 8 || width >= 32768) {
|
|
|
|
// Read flat data
|
|
|
|
|
2021-09-08 06:25:35 +02:00
|
|
|
f->get_buffer(ptr, (uint64_t)width * height * 4);
|
2017-05-29 02:46:48 +02:00
|
|
|
} else {
|
|
|
|
// Read RLE-encoded data
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
// not run-length encoded, so we have to actually use THIS data as a decoded
|
|
|
|
// pixel (note this can't be a valid pixel--one of RGB must be >= 128)
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2019-08-11 10:49:53 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(len != width, ERR_FILE_CORRUPT, "Invalid decoded scanline length, corrupt HDR.");
|
2017-05-29 02:46:48 +02:00
|
|
|
|
|
|
|
for (int k = 0; k < 4; ++k) {
|
|
|
|
int i = 0;
|
|
|
|
while (i < width) {
|
|
|
|
int count = f->get_8();
|
|
|
|
if (count > 128) {
|
|
|
|
// Run
|
|
|
|
int value = f->get_8();
|
|
|
|
count -= 128;
|
2020-05-14 16:41:43 +02:00
|
|
|
for (int z = 0; z < count; ++z) {
|
2017-05-29 02:46:48 +02:00
|
|
|
ptr[(j * width + i++) * 4 + k] = uint8_t(value);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-05-29 02:46:48 +02:00
|
|
|
} else {
|
|
|
|
// Dump
|
2024-08-08 19:13:00 +02:00
|
|
|
f->get_buffer(temp_read_ptr, count);
|
2020-05-14 16:41:43 +02:00
|
|
|
for (int z = 0; z < count; ++z) {
|
2024-08-08 19:13:00 +02:00
|
|
|
ptr[(j * width + i++) * 4 + k] = temp_read_ptr[z];
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-05-29 02:46:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 19:13:00 +02:00
|
|
|
const bool force_linear = p_flags & FLAG_FORCE_LINEAR;
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
//convert
|
|
|
|
for (int i = 0; i < width * height; i++) {
|
2024-08-08 19:13:00 +02:00
|
|
|
int e = ptr[3] - 128;
|
|
|
|
|
|
|
|
if (force_linear || (e < -15 || e > 15)) {
|
|
|
|
float exp = pow(2.0f, e);
|
|
|
|
Color c(ptr[0] * exp / 255.0, ptr[1] * exp / 255.0, ptr[2] * exp / 255.0);
|
2017-05-29 02:46:48 +02:00
|
|
|
|
2024-08-08 19:13:00 +02:00
|
|
|
if (force_linear) {
|
|
|
|
c = c.srgb_to_linear();
|
|
|
|
}
|
2017-05-29 02:46:48 +02:00
|
|
|
|
2024-08-08 19:13:00 +02:00
|
|
|
*(uint32_t *)ptr = c.to_rgbe9995();
|
|
|
|
} else {
|
|
|
|
// https://github.com/george-steel/rgbe-rs/blob/e7cc33b7f42b4eb3272c166dac75385e48687c92/src/types.rs#L123-L129
|
|
|
|
uint32_t e5 = (uint32_t)(e + 15);
|
|
|
|
*(uint32_t *)ptr = ((e5 << 27) | ((uint32_t)ptr[2] << 19) | ((uint32_t)ptr[1] << 10) | ((uint32_t)ptr[0] << 1));
|
2017-05-30 03:11:33 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
ptr += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 20:06:19 +02:00
|
|
|
p_image->set_data(width, height, false, Image::FORMAT_RGBE9995, imgdata);
|
2017-05-29 02:46:48 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageLoaderHDR::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
p_extensions->push_back("hdr");
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageLoaderHDR::ImageLoaderHDR() {
|
|
|
|
}
|