diff options
Diffstat (limited to 'modules/hdr')
-rw-r--r-- | modules/hdr/SCsub | 9 | ||||
-rw-r--r-- | modules/hdr/config.py | 7 | ||||
-rw-r--r-- | modules/hdr/image_loader_hdr.cpp | 151 | ||||
-rw-r--r-- | modules/hdr/image_loader_hdr.h | 46 | ||||
-rw-r--r-- | modules/hdr/register_types.cpp | 45 | ||||
-rw-r--r-- | modules/hdr/register_types.h | 31 |
6 files changed, 289 insertions, 0 deletions
diff --git a/modules/hdr/SCsub b/modules/hdr/SCsub new file mode 100644 index 0000000000..c960e8126b --- /dev/null +++ b/modules/hdr/SCsub @@ -0,0 +1,9 @@ +#!/usr/bin/env python + +Import('env') +Import('env_modules') + +env_hdr = env_modules.Clone() + +# Godot's own source files +env_hdr.add_source_files(env.modules_sources, "*.cpp") diff --git a/modules/hdr/config.py b/modules/hdr/config.py new file mode 100644 index 0000000000..fb920482f5 --- /dev/null +++ b/modules/hdr/config.py @@ -0,0 +1,7 @@ + +def can_build(platform): + return True + + +def configure(env): + pass diff --git a/modules/hdr/image_loader_hdr.cpp b/modules/hdr/image_loader_hdr.cpp new file mode 100644 index 0000000000..4c897e66af --- /dev/null +++ b/modules/hdr/image_loader_hdr.cpp @@ -0,0 +1,151 @@ +/*************************************************************************/ +/* image_loader_jpegd.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +#include "image_loader_hdr.h" + +#include "os/os.h" +#include "print_string.h" + +#include "thirdparty/tinyexr/tinyexr.h" + +Error ImageLoaderHDR::load_image(Ref<Image> p_image, FileAccess *f) { + + String header = f->get_token(); + + print_line("HEADER: " + header); + ERR_FAIL_COND_V(header != "#?RADIANCE", ERR_FILE_UNRECOGNIZED); + + String format = f->get_token(); + print_line("FORMAT: " + format); + + ERR_FAIL_COND_V(format != "FORMAT=32-bit_rle_rgbe", ERR_FILE_UNRECOGNIZED); + + 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(); + + print_line("HDR w: " + itos(width) + " h:" + itos(height)); + + 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) { + // Read flat data + + f->get_buffer(ptr, width * height * 4); + } 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(); + + print_line("line: " + itos(len)); + 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) { + // Run + int value = f->get_8(); + count -= 128; + for (int z = 0; z < count; ++z) + ptr[(j * width + i++) * 4 + k] = uint8_t(value); + } else { + // Dump + for (int z = 0; z < count; ++z) + ptr[(j * width + i++) * 4 + k] = f->get_8(); + } + } + } + } + } + + //convert + for (int i = 0; i < width * height; i++) { + + float exp = pow(2, ptr[3] - 128); + + Color c( + ptr[0] * exp / 255.0, + ptr[1] * exp / 255.0, + ptr[2] * exp / 255.0); + + *(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() { +} diff --git a/modules/hdr/image_loader_hdr.h b/modules/hdr/image_loader_hdr.h new file mode 100644 index 0000000000..93bb0fdc03 --- /dev/null +++ b/modules/hdr/image_loader_hdr.h @@ -0,0 +1,46 @@ +/*************************************************************************/ +/* image_loader_jpegd.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +#ifndef IMAGE_LOADER_TINYEXR_H +#define IMAGE_LOADER_TINYEXR_H + +#include "io/image_loader.h" + +/** + @author Juan Linietsky <reduzio@gmail.com> +*/ +class ImageLoaderHDR : public ImageFormatLoader { + +public: + virtual Error load_image(Ref<Image> p_image, FileAccess *f); + virtual void get_recognized_extensions(List<String> *p_extensions) const; + ImageLoaderHDR(); +}; + +#endif diff --git a/modules/hdr/register_types.cpp b/modules/hdr/register_types.cpp new file mode 100644 index 0000000000..e4f7c14aa7 --- /dev/null +++ b/modules/hdr/register_types.cpp @@ -0,0 +1,45 @@ +/*************************************************************************/ +/* register_types.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +#include "register_types.h" + +#include "image_loader_hdr.h" + +static ImageLoaderHDR *image_loader_hdr = NULL; + +void register_hdr_types() { + + image_loader_hdr = memnew(ImageLoaderHDR); + ImageLoader::add_image_format_loader(image_loader_hdr); +} + +void unregister_hdr_types() { + + memdelete(image_loader_hdr); +} diff --git a/modules/hdr/register_types.h b/modules/hdr/register_types.h new file mode 100644 index 0000000000..3d901ea003 --- /dev/null +++ b/modules/hdr/register_types.h @@ -0,0 +1,31 @@ +/*************************************************************************/ +/* register_types.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +void register_hdr_types(); +void unregister_hdr_types(); |