diff options
Diffstat (limited to 'core/io')
61 files changed, 5948 insertions, 605 deletions
diff --git a/core/io/compression.cpp b/core/io/compression.cpp index 99ca8107e4..8e613cb3ce 100644 --- a/core/io/compression.cpp +++ b/core/io/compression.cpp @@ -30,9 +30,9 @@ #include "compression.h" +#include "core/config/project_settings.h" #include "core/io/zip_io.h" #include "core/os/copymem.h" -#include "core/project_settings.h" #include "thirdparty/misc/fastlz.h" @@ -180,8 +180,95 @@ int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p ERR_FAIL_V(-1); } +/** + This will handle both Gzip and Deflat streams. It will automatically allocate the output buffer into the provided p_dst_vect Vector. + This is required for compressed data who's final uncompressed size is unknown, as is the case for HTTP response bodies. + This is much slower however than using Compression::decompress because it may result in multiple full copies of the output buffer. +*/ +int Compression::decompress_dynamic(Vector<uint8_t> *p_dst_vect, int p_max_dst_size, const uint8_t *p_src, int p_src_size, Mode p_mode) { + int ret; + uint8_t *dst = nullptr; + int out_mark = 0; + z_stream strm; + + ERR_FAIL_COND_V(p_src_size <= 0, Z_DATA_ERROR); + + // This function only supports GZip and Deflate + int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16; + ERR_FAIL_COND_V(p_mode != MODE_DEFLATE && p_mode != MODE_GZIP, Z_ERRNO); + + // Initialize the stream + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = 0; + strm.next_in = Z_NULL; + + int err = inflateInit2(&strm, window_bits); + ERR_FAIL_COND_V(err != Z_OK, -1); + + // Setup the stream inputs + strm.next_in = (Bytef *)p_src; + strm.avail_in = p_src_size; + + // Ensure the destination buffer is empty + p_dst_vect->resize(0); + + // decompress until deflate stream ends or end of file + do { + // Add another chunk size to the output buffer + // This forces a copy of the whole buffer + p_dst_vect->resize(p_dst_vect->size() + gzip_chunk); + // Get pointer to the actual output buffer + dst = p_dst_vect->ptrw(); + + // Set the stream to the new output stream + // Since it was copied, we need to reset the stream to the new buffer + strm.next_out = &(dst[out_mark]); + strm.avail_out = gzip_chunk; + + // run inflate() on input until output buffer is full and needs to be resized + // or input runs out + do { + ret = inflate(&strm, Z_SYNC_FLUSH); + + switch (ret) { + case Z_NEED_DICT: + ret = Z_DATA_ERROR; + [[fallthrough]]; + case Z_DATA_ERROR: + case Z_MEM_ERROR: + case Z_STREAM_ERROR: + WARN_PRINT(strm.msg); + (void)inflateEnd(&strm); + p_dst_vect->resize(0); + return ret; + } + } while (strm.avail_out > 0 && strm.avail_in > 0); + + out_mark += gzip_chunk; + + // Encorce max output size + if (p_max_dst_size > -1 && strm.total_out > (uint64_t)p_max_dst_size) { + (void)inflateEnd(&strm); + p_dst_vect->resize(0); + return Z_BUF_ERROR; + } + } while (ret != Z_STREAM_END); + + // If all done successfully, resize the output if it's larger than the actual output + if ((unsigned long)p_dst_vect->size() > strm.total_out) { + p_dst_vect->resize(strm.total_out); + } + + // clean up and return + (void)inflateEnd(&strm); + return Z_OK; +} + int Compression::zlib_level = Z_DEFAULT_COMPRESSION; int Compression::gzip_level = Z_DEFAULT_COMPRESSION; int Compression::zstd_level = 3; bool Compression::zstd_long_distance_matching = false; int Compression::zstd_window_log_size = 27; // ZSTD_WINDOWLOG_LIMIT_DEFAULT +int Compression::gzip_chunk = 16384; diff --git a/core/io/compression.h b/core/io/compression.h index f195f96ba5..864869788a 100644 --- a/core/io/compression.h +++ b/core/io/compression.h @@ -31,6 +31,7 @@ #ifndef COMPRESSION_H #define COMPRESSION_H +#include "core/templates/vector.h" #include "core/typedefs.h" class Compression { @@ -40,6 +41,7 @@ public: static int zstd_level; static bool zstd_long_distance_matching; static int zstd_window_log_size; + static int gzip_chunk; enum Mode { MODE_FASTLZ, @@ -51,6 +53,7 @@ public: static int compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_ZSTD); static int get_max_compressed_buffer_size(int p_src_size, Mode p_mode = MODE_ZSTD); static int decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_ZSTD); + static int decompress_dynamic(Vector<uint8_t> *p_dst_vect, int p_max_dst_size, const uint8_t *p_src, int p_src_size, Mode p_mode); Compression() {} }; diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index 1af9142317..8be39178db 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -32,7 +32,7 @@ #include "core/io/file_access_encrypted.h" #include "core/os/keyboard.h" -#include "core/variant_parser.h" +#include "core/variant/variant_parser.h" PackedStringArray ConfigFile::_get_sections() const { List<String> s; diff --git a/core/io/config_file.h b/core/io/config_file.h index ae06960f02..1dc4492ca8 100644 --- a/core/io/config_file.h +++ b/core/io/config_file.h @@ -31,10 +31,10 @@ #ifndef CONFIG_FILE_H #define CONFIG_FILE_H -#include "core/ordered_hash_map.h" +#include "core/object/reference.h" #include "core/os/file_access.h" -#include "core/reference.h" -#include "core/variant_parser.h" +#include "core/templates/ordered_hash_map.h" +#include "core/variant/variant_parser.h" class ConfigFile : public Reference { GDCLASS(ConfigFile, Reference); diff --git a/core/io/dtls_server.cpp b/core/io/dtls_server.cpp index e43b1f5385..1930f40c47 100644 --- a/core/io/dtls_server.cpp +++ b/core/io/dtls_server.cpp @@ -30,8 +30,8 @@ #include "dtls_server.h" +#include "core/config/project_settings.h" #include "core/os/file_access.h" -#include "core/project_settings.h" DTLSServer *(*DTLSServer::_create)() = nullptr; bool DTLSServer::available = false; diff --git a/core/io/file_access_buffered.cpp b/core/io/file_access_buffered.cpp deleted file mode 100644 index 6208f3a4d1..0000000000 --- a/core/io/file_access_buffered.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/*************************************************************************/ -/* file_access_buffered.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 "file_access_buffered.h" - -#include "core/error_macros.h" - -Error FileAccessBuffered::set_error(Error p_error) const { - return (last_error = p_error); -} - -void FileAccessBuffered::set_cache_size(int p_size) { - cache_size = p_size; -} - -int FileAccessBuffered::get_cache_size() { - return cache_size; -} - -int FileAccessBuffered::cache_data_left() const { - if (file.offset >= file.size) { - return 0; - } - - if (cache.offset == -1 || file.offset < cache.offset || file.offset >= cache.offset + cache.buffer.size()) { - return read_data_block(file.offset, cache_size); - } - - return cache.buffer.size() - (file.offset - cache.offset); -} - -void FileAccessBuffered::seek(size_t p_position) { - file.offset = p_position; -} - -void FileAccessBuffered::seek_end(int64_t p_position) { - file.offset = file.size + p_position; -} - -size_t FileAccessBuffered::get_position() const { - return file.offset; -} - -size_t FileAccessBuffered::get_len() const { - return file.size; -} - -bool FileAccessBuffered::eof_reached() const { - return file.offset > file.size; -} - -uint8_t FileAccessBuffered::get_8() const { - ERR_FAIL_COND_V_MSG(!file.open, 0, "Can't get data, when file is not opened."); - - uint8_t byte = 0; - if (cache_data_left() >= 1) { - byte = cache.buffer[file.offset - cache.offset]; - } - - ++file.offset; - - return byte; -} - -int FileAccessBuffered::get_buffer(uint8_t *p_dest, int p_length) const { - ERR_FAIL_COND_V_MSG(!file.open, -1, "Can't get buffer, when file is not opened."); - - if (p_length > cache_size) { - int total_read = 0; - - if (!(cache.offset == -1 || file.offset < cache.offset || file.offset >= cache.offset + cache.buffer.size())) { - int size = (cache.buffer.size() - (file.offset - cache.offset)); - size = size - (size % 4); - //const uint8_t* read = cache.buffer.ptr(); - //memcpy(p_dest, read.ptr() + (file.offset - cache.offset), size); - memcpy(p_dest, cache.buffer.ptr() + (file.offset - cache.offset), size); - p_dest += size; - p_length -= size; - file.offset += size; - total_read += size; - } - - int err = read_data_block(file.offset, p_length, p_dest); - if (err >= 0) { - total_read += err; - file.offset += err; - } - - return total_read; - } - - int to_read = p_length; - int total_read = 0; - while (to_read > 0) { - int left = cache_data_left(); - if (left == 0) { - file.offset += to_read; - return total_read; - } - if (left < 0) { - return left; - } - - int r = MIN(left, to_read); - //const uint8_t* read = cache.buffer.ptr(); - //memcpy(p_dest+total_read, &read.ptr()[file.offset - cache.offset], r); - memcpy(p_dest + total_read, cache.buffer.ptr() + (file.offset - cache.offset), r); - - file.offset += r; - total_read += r; - to_read -= r; - } - - return p_length; -} - -bool FileAccessBuffered::is_open() const { - return file.open; -} - -Error FileAccessBuffered::get_error() const { - return last_error; -} diff --git a/core/io/file_access_buffered_fa.h b/core/io/file_access_buffered_fa.h deleted file mode 100644 index f22e54e154..0000000000 --- a/core/io/file_access_buffered_fa.h +++ /dev/null @@ -1,139 +0,0 @@ -/*************************************************************************/ -/* file_access_buffered_fa.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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 FILE_ACCESS_BUFFERED_FA_H -#define FILE_ACCESS_BUFFERED_FA_H - -#include "core/io/file_access_buffered.h" - -template <class T> -class FileAccessBufferedFA : public FileAccessBuffered { - T f; - - int read_data_block(int p_offset, int p_size, uint8_t *p_dest = 0) const { - ERR_FAIL_COND_V_MSG(!f.is_open(), -1, "Can't read data block when file is not opened."); - - ((T *)&f)->seek(p_offset); - - if (p_dest) { - f.get_buffer(p_dest, p_size); - return p_size; - - } else { - cache.offset = p_offset; - cache.buffer.resize(p_size); - - // on Vector - //uint8_t* write = cache.buffer.ptrw(); - //f.get_buffer(write.ptrw(), p_size); - - // on vector - f.get_buffer(cache.buffer.ptrw(), p_size); - - return p_size; - } - } - - static FileAccess *create() { - return memnew(FileAccessBufferedFA<T>()); - } - -protected: - virtual void _set_access_type(AccessType p_access) { - f._set_access_type(p_access); - FileAccessBuffered::_set_access_type(p_access); - } - -public: - void flush() { - f.flush(); - } - - void store_8(uint8_t p_dest) { - f.store_8(p_dest); - } - - void store_buffer(const uint8_t *p_src, int p_length) { - f.store_buffer(p_src, p_length); - } - - bool file_exists(const String &p_name) { - return f.file_exists(p_name); - } - - Error _open(const String &p_path, int p_mode_flags) { - close(); - - Error ret = f._open(p_path, p_mode_flags); - if (ret != OK) - return ret; - //ERR_FAIL_COND_V( ret != OK, ret ); - - file.size = f.get_len(); - file.offset = 0; - file.open = true; - file.name = p_path; - file.access_flags = p_mode_flags; - - cache.buffer.resize(0); - cache.offset = 0; - - return set_error(OK); - } - - void close() { - f.close(); - - file.offset = 0; - file.size = 0; - file.open = false; - file.name = ""; - - cache.buffer.resize(0); - cache.offset = 0; - set_error(OK); - } - - virtual uint64_t _get_modified_time(const String &p_file) { - return f._get_modified_time(p_file); - } - - virtual uint32_t _get_unix_permissions(const String &p_file) { - return f._get_unix_permissions(p_file); - } - - virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) { - return f._set_unix_permissions(p_file, p_permissions); - } - - FileAccessBufferedFA() {} -}; - -#endif // FILE_ACCESS_BUFFERED_FA_H diff --git a/core/io/file_access_compressed.cpp b/core/io/file_access_compressed.cpp index 7817ccb773..b06b3c078f 100644 --- a/core/io/file_access_compressed.cpp +++ b/core/io/file_access_compressed.cpp @@ -30,7 +30,7 @@ #include "file_access_compressed.h" -#include "core/print_string.h" +#include "core/string/print_string.h" void FileAccessCompressed::configure(const String &p_magic, Compression::Mode p_mode, int p_block_size) { magic = p_magic.ascii().get_data(); @@ -327,14 +327,14 @@ Error FileAccessCompressed::get_error() const { void FileAccessCompressed::flush() { ERR_FAIL_COND_MSG(!f, "File must be opened before use."); - ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); // compressed files keep data in memory till close() } void FileAccessCompressed::store_8(uint8_t p_dest) { ERR_FAIL_COND_MSG(!f, "File must be opened before use."); - ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); WRITE_FIT(1); write_ptr[write_pos++] = p_dest; diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp index 5938914cb0..cf5800b472 100644 --- a/core/io/file_access_encrypted.cpp +++ b/core/io/file_access_encrypted.cpp @@ -32,57 +32,59 @@ #include "core/crypto/crypto_core.h" #include "core/os/copymem.h" -#include "core/print_string.h" -#include "core/variant.h" +#include "core/string/print_string.h" +#include "core/variant/variant.h" #include <stdio.h> -#define COMP_MAGIC 0x43454447 - -Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode) { +Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode, bool p_with_magic) { ERR_FAIL_COND_V_MSG(file != nullptr, ERR_ALREADY_IN_USE, "Can't open file while another file from path '" + file->get_path_absolute() + "' is open."); ERR_FAIL_COND_V(p_key.size() != 32, ERR_INVALID_PARAMETER); pos = 0; eofed = false; + use_magic = p_with_magic; if (p_mode == MODE_WRITE_AES256) { data.clear(); writing = true; file = p_base; - mode = p_mode; key = p_key; } else if (p_mode == MODE_READ) { writing = false; key = p_key; - uint32_t magic = p_base->get_32(); - ERR_FAIL_COND_V(magic != COMP_MAGIC, ERR_FILE_UNRECOGNIZED); - mode = Mode(p_base->get_32()); - ERR_FAIL_INDEX_V(mode, MODE_MAX, ERR_FILE_CORRUPT); - ERR_FAIL_COND_V(mode == 0, ERR_FILE_CORRUPT); + if (use_magic) { + uint32_t magic = p_base->get_32(); + ERR_FAIL_COND_V(magic != ENCRYPTED_HEADER_MAGIC, ERR_FILE_UNRECOGNIZED); + } unsigned char md5d[16]; p_base->get_buffer(md5d, 16); length = p_base->get_64(); + + unsigned char iv[16]; + for (int i = 0; i < 16; i++) { + iv[i] = p_base->get_8(); + } + base = p_base->get_position(); ERR_FAIL_COND_V(p_base->get_len() < base + length, ERR_FILE_CORRUPT); uint32_t ds = length; if (ds % 16) { ds += 16 - (ds % 16); } - data.resize(ds); uint32_t blen = p_base->get_buffer(data.ptrw(), ds); ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT); - CryptoCore::AESContext ctx; - ctx.set_decode_key(key.ptrw(), 256); + { + CryptoCore::AESContext ctx; - for (size_t i = 0; i < ds; i += 16) { - ctx.decrypt_ecb(&data.write[i], &data.write[i]); + ctx.set_encode_key(key.ptrw(), 256); // Due to the nature of CFB, same key schedule is used for both encryption and decryption! + ctx.decrypt_cfb(ds, iv, data.ptrw(), data.ptrw()); } data.resize(length); @@ -119,6 +121,25 @@ void FileAccessEncrypted::close() { return; } + _release(); + + file->close(); + memdelete(file); + + file = nullptr; +} + +void FileAccessEncrypted::release() { + if (!file) { + return; + } + + _release(); + + file = nullptr; +} + +void FileAccessEncrypted::_release() { if (writing) { Vector<uint8_t> compressed; size_t len = data.size(); @@ -138,27 +159,23 @@ void FileAccessEncrypted::close() { CryptoCore::AESContext ctx; ctx.set_encode_key(key.ptrw(), 256); - for (size_t i = 0; i < len; i += 16) { - ctx.encrypt_ecb(&compressed.write[i], &compressed.write[i]); + if (use_magic) { + file->store_32(ENCRYPTED_HEADER_MAGIC); } - file->store_32(COMP_MAGIC); - file->store_32(mode); - file->store_buffer(hash, 16); file->store_64(data.size()); - file->store_buffer(compressed.ptr(), compressed.size()); - file->close(); - memdelete(file); - file = nullptr; - data.clear(); + unsigned char iv[16]; + for (int i = 0; i < 16; i++) { + iv[i] = Math::rand() % 256; + file->store_8(iv[i]); + } - } else { - file->close(); - memdelete(file); + ctx.encrypt_cfb(len, iv, compressed.ptrw(), compressed.ptrw()); + + file->store_buffer(compressed.ptr(), compressed.size()); data.clear(); - file = nullptr; } } @@ -239,7 +256,7 @@ Error FileAccessEncrypted::get_error() const { } void FileAccessEncrypted::store_buffer(const uint8_t *p_src, int p_length) { - ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); if (pos < data.size()) { for (int i = 0; i < p_length; i++) { @@ -255,13 +272,13 @@ void FileAccessEncrypted::store_buffer(const uint8_t *p_src, int p_length) { } void FileAccessEncrypted::flush() { - ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); // encrypted files keep data in memory till close() } void FileAccessEncrypted::store_8(uint8_t p_dest) { - ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); if (pos < data.size()) { data.write[pos] = p_dest; diff --git a/core/io/file_access_encrypted.h b/core/io/file_access_encrypted.h index e269c1e30c..c760933038 100644 --- a/core/io/file_access_encrypted.h +++ b/core/io/file_access_encrypted.h @@ -33,6 +33,8 @@ #include "core/os/file_access.h" +#define ENCRYPTED_HEADER_MAGIC 0x43454447 + class FileAccessEncrypted : public FileAccess { public: enum Mode { @@ -42,22 +44,25 @@ public: }; private: - Mode mode = MODE_MAX; Vector<uint8_t> key; bool writing = false; FileAccess *file = nullptr; - size_t base; - size_t length; + size_t base = 0; + size_t length = 0; Vector<uint8_t> data; mutable int pos = 0; mutable bool eofed = false; + bool use_magic = true; + + void _release(); public: - Error open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode); + Error open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode, bool p_with_magic = true); Error open_and_parse_password(FileAccess *p_base, const String &p_key, Mode p_mode); virtual Error _open(const String &p_path, int p_mode_flags); ///< open a file virtual void close(); ///< close a file + virtual void release(); ///< finish and keep base file open virtual bool is_open() const; ///< true when file is open virtual String get_path() const; /// returns the path for the current open file diff --git a/core/io/file_access_memory.cpp b/core/io/file_access_memory.cpp index a65ff92a89..79cba63765 100644 --- a/core/io/file_access_memory.cpp +++ b/core/io/file_access_memory.cpp @@ -30,10 +30,10 @@ #include "file_access_memory.h" -#include "core/map.h" +#include "core/config/project_settings.h" #include "core/os/copymem.h" #include "core/os/dir_access.h" -#include "core/project_settings.h" +#include "core/templates/map.h" static Map<String, Vector<uint8_t>> *files = nullptr; diff --git a/core/io/file_access_memory.h b/core/io/file_access_memory.h index 1a9bd3fbbb..47012b4e83 100644 --- a/core/io/file_access_memory.h +++ b/core/io/file_access_memory.h @@ -35,8 +35,8 @@ class FileAccessMemory : public FileAccess { uint8_t *data = nullptr; - int length; - mutable int pos; + int length = 0; + mutable int pos = 0; static FileAccess *create(); diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index 6890740d90..1e9266f118 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -30,10 +30,10 @@ #include "file_access_network.h" +#include "core/config/project_settings.h" #include "core/io/ip.h" #include "core/io/marshalls.h" #include "core/os/os.h" -#include "core/project_settings.h" //#define DEBUG_PRINT(m_p) print_line(m_p) //#define DEBUG_TIME(m_what) printf("MS: %s - %lli\n",m_what,OS::get_singleton()->get_ticks_usec()); diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp index 37240f234a..a025ca5730 100644 --- a/core/io/file_access_pack.cpp +++ b/core/io/file_access_pack.cpp @@ -30,13 +30,15 @@ #include "file_access_pack.h" +#include "core/io/file_access_encrypted.h" +#include "core/object/script_language.h" #include "core/version.h" #include <stdio.h> -Error PackedData::add_pack(const String &p_path, bool p_replace_files) { +Error PackedData::add_pack(const String &p_path, bool p_replace_files, size_t p_offset) { for (int i = 0; i < sources.size(); i++) { - if (sources[i]->try_open_pack(p_path, p_replace_files)) { + if (sources[i]->try_open_pack(p_path, p_replace_files, p_offset)) { return OK; } } @@ -44,13 +46,14 @@ Error PackedData::add_pack(const String &p_path, bool p_replace_files) { return ERR_FILE_UNRECOGNIZED; } -void PackedData::add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files) { +void PackedData::add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files, bool p_encrypted) { PathMD5 pmd5(path.md5_buffer()); - //printf("adding path %ls, %lli, %lli\n", path.c_str(), pmd5.a, pmd5.b); + //printf("adding path %s, %lli, %lli\n", path.utf8().get_data(), pmd5.a, pmd5.b); bool exists = files.has(pmd5); PackedFile pf; + pf.encrypted = p_encrypted; pf.pack = pkg_path; pf.offset = ofs; pf.size = size; @@ -123,15 +126,24 @@ PackedData::~PackedData() { ////////////////////////////////////////////////////////////////// -bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) { +bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset) { FileAccess *f = FileAccess::open(p_path, FileAccess::READ); if (!f) { return false; } + f->seek(p_offset); + uint32_t magic = f->get_32(); if (magic != PACK_HEADER_MAGIC) { + // loading with offset feature not supported for self contained exe files + if (p_offset != 0) { + f->close(); + memdelete(f); + ERR_FAIL_V_MSG(false, "Loading self-contained executable with offset not supported."); + } + //maybe at the end.... self contained exe f->seek_end(); f->seek(f->get_position() - 4); @@ -170,6 +182,11 @@ bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) ERR_FAIL_V_MSG(false, "Pack created with a newer version of the engine: " + itos(ver_major) + "." + itos(ver_minor) + "."); } + uint32_t pack_flags = f->get_32(); + uint64_t file_base = f->get_64(); + + bool enc_directory = (pack_flags & PACK_DIR_ENCRYPTED); + for (int i = 0; i < 16; i++) { //reserved f->get_32(); @@ -177,6 +194,30 @@ bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) int file_count = f->get_32(); + if (enc_directory) { + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + if (!fae) { + f->close(); + memdelete(f); + ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory."); + } + + Vector<uint8_t> key; + key.resize(32); + for (int i = 0; i < key.size(); i++) { + key.write[i] = script_encryption_key[i]; + } + + Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false); + if (err) { + f->close(); + memdelete(f); + memdelete(fae); + ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory."); + } + f = fae; + } + for (int i = 0; i < file_count; i++) { uint32_t sl = f->get_32(); CharString cs; @@ -187,11 +228,13 @@ bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) String path; path.parse_utf8(cs.ptr()); - uint64_t ofs = f->get_64(); + uint64_t ofs = file_base + f->get_64(); uint64_t size = f->get_64(); uint8_t md5[16]; f->get_buffer(md5, 16); - PackedData::get_singleton()->add_path(p_path, path, ofs, size, md5, this, p_replace_files); + uint32_t flags = f->get_32(); + + PackedData::get_singleton()->add_path(p_path, path, ofs + p_offset, size, md5, this, p_replace_files, (flags & PACK_FILE_ENCRYPTED)); } f->close(); @@ -225,7 +268,7 @@ void FileAccessPack::seek(size_t p_position) { eof = false; } - f->seek(pf.offset + p_position); + f->seek(off + p_position); pos = p_position; } @@ -310,12 +353,35 @@ FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFil ERR_FAIL_COND_MSG(!f, "Can't open pack-referenced file '" + String(pf.pack) + "'."); f->seek(pf.offset); + off = pf.offset; + + if (pf.encrypted) { + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + if (!fae) { + ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'."); + } + + Vector<uint8_t> key; + key.resize(32); + for (int i = 0; i < key.size(); i++) { + key.write[i] = script_encryption_key[i]; + } + + Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false); + if (err) { + memdelete(fae); + ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'."); + } + f = fae; + off = 0; + } pos = 0; eof = false; } FileAccessPack::~FileAccessPack() { if (f) { + f->close(); memdelete(f); } } @@ -376,8 +442,14 @@ String DirAccessPack::get_drive(int p_drive) { return ""; } -Error DirAccessPack::change_dir(String p_dir) { +PackedData::PackedDir *DirAccessPack::_find_dir(String p_dir) { String nd = p_dir.replace("\\", "/"); + + // Special handling since simplify_path() will forbid it + if (p_dir == "..") { + return current->parent; + } + bool absolute = false; if (nd.begins_with("res://")) { nd = nd.replace_first("res://", ""); @@ -417,13 +489,21 @@ Error DirAccessPack::change_dir(String p_dir) { pd = pd->subdirs[p]; } else { - return ERR_INVALID_PARAMETER; + return nullptr; } } - current = pd; + return pd; +} - return OK; +Error DirAccessPack::change_dir(String p_dir) { + PackedData::PackedDir *pd = _find_dir(p_dir); + if (pd) { + current = pd; + return OK; + } else { + return ERR_INVALID_PARAMETER; + } } String DirAccessPack::get_current_dir(bool p_include_drive) { @@ -441,13 +521,17 @@ String DirAccessPack::get_current_dir(bool p_include_drive) { bool DirAccessPack::file_exists(String p_file) { p_file = fix_path(p_file); - return current->files.has(p_file); + PackedData::PackedDir *pd = _find_dir(p_file.get_base_dir()); + if (!pd) { + return false; + } + return pd->files.has(p_file.get_file()); } bool DirAccessPack::dir_exists(String p_dir) { p_dir = fix_path(p_dir); - return current->subdirs.has(p_dir); + return _find_dir(p_dir) != nullptr; } Error DirAccessPack::make_dir(String p_dir) { diff --git a/core/io/file_access_pack.h b/core/io/file_access_pack.h index 348bc0c450..c13626a5aa 100644 --- a/core/io/file_access_pack.h +++ b/core/io/file_access_pack.h @@ -31,16 +31,24 @@ #ifndef FILE_ACCESS_PACK_H #define FILE_ACCESS_PACK_H -#include "core/list.h" -#include "core/map.h" #include "core/os/dir_access.h" #include "core/os/file_access.h" -#include "core/print_string.h" +#include "core/string/print_string.h" +#include "core/templates/list.h" +#include "core/templates/map.h" // Godot's packed file magic header ("GDPC" in ASCII). #define PACK_HEADER_MAGIC 0x43504447 // The current packed file format version number. -#define PACK_FORMAT_VERSION 1 +#define PACK_FORMAT_VERSION 2 + +enum PackFlags { + PACK_DIR_ENCRYPTED = 1 << 0 +}; + +enum PackFileFlags { + PACK_FILE_ENCRYPTED = 1 << 0 +}; class PackSource; @@ -56,6 +64,7 @@ public: uint64_t size; uint8_t md5[16]; PackSource *src; + bool encrypted; }; private: @@ -102,31 +111,34 @@ private: public: void add_pack_source(PackSource *p_source); - void add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files); // for PackSource + void add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files, bool p_encrypted = false); // for PackSource void set_disabled(bool p_disabled) { disabled = p_disabled; } _FORCE_INLINE_ bool is_disabled() const { return disabled; } static PackedData *get_singleton() { return singleton; } - Error add_pack(const String &p_path, bool p_replace_files); + Error add_pack(const String &p_path, bool p_replace_files, size_t p_offset); _FORCE_INLINE_ FileAccess *try_open_path(const String &p_path); _FORCE_INLINE_ bool has_path(const String &p_path); + _FORCE_INLINE_ DirAccess *try_open_directory(const String &p_path); + _FORCE_INLINE_ bool has_directory(const String &p_path); + PackedData(); ~PackedData(); }; class PackSource { public: - virtual bool try_open_pack(const String &p_path, bool p_replace_files) = 0; + virtual bool try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset) = 0; virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file) = 0; virtual ~PackSource() {} }; class PackedSourcePCK : public PackSource { public: - virtual bool try_open_pack(const String &p_path, bool p_replace_files); + virtual bool try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset); virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file); }; @@ -135,6 +147,7 @@ class FileAccessPack : public FileAccess { mutable size_t pos; mutable bool eof; + uint64_t off; FileAccess *f; virtual Error _open(const String &p_path, int p_mode_flags); @@ -189,6 +202,16 @@ bool PackedData::has_path(const String &p_path) { return files.has(PathMD5(p_path.md5_buffer())); } +bool PackedData::has_directory(const String &p_path) { + DirAccess *da = try_open_directory(p_path); + if (da) { + memdelete(da); + return true; + } else { + return false; + } +} + class DirAccessPack : public DirAccess { PackedData::PackedDir *current; @@ -196,6 +219,8 @@ class DirAccessPack : public DirAccess { List<String> list_files; bool cdir = false; + PackedData::PackedDir *_find_dir(String p_dir); + public: virtual Error list_dir_begin(); virtual String get_next(); @@ -225,4 +250,13 @@ public: ~DirAccessPack() {} }; +DirAccess *PackedData::try_open_directory(const String &p_path) { + DirAccess *da = memnew(DirAccessPack()); + if (da->change_dir(p_path) != OK) { + memdelete(da); + da = nullptr; + } + return da; +} + #endif // FILE_ACCESS_PACK_H diff --git a/core/io/file_access_zip.cpp b/core/io/file_access_zip.cpp index c3a62706c7..1163c409bc 100644 --- a/core/io/file_access_zip.cpp +++ b/core/io/file_access_zip.cpp @@ -102,7 +102,6 @@ static voidpf godot_alloc(voidpf opaque, uInt items, uInt size) { static void godot_free(voidpf opaque, voidpf address) { memfree(address); } - } // extern "C" void ZipArchive::close_handle(unzFile p_file) const { @@ -147,8 +146,11 @@ unzFile ZipArchive::get_file_handle(String p_file) const { return pkg; } -bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files) { - //printf("opening zip pack %ls, %i, %i\n", p_name.c_str(), p_name.extension().nocasecmp_to("zip"), p_name.extension().nocasecmp_to("pcz")); +bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset = 0) { + //printf("opening zip pack %s, %i, %i\n", p_name.utf8().get_data(), p_name.extension().nocasecmp_to("zip"), p_name.extension().nocasecmp_to("pcz")); + // load with offset feature only supported for PCK files + ERR_FAIL_COND_V_MSG(p_offset != 0, false, "Invalid PCK data. Note that loading files with a non-zero offset isn't supported with ZIP archives."); + if (p_path.get_extension().nocasecmp_to("zip") != 0 && p_path.get_extension().nocasecmp_to("pcz") != 0) { return false; } @@ -197,8 +199,8 @@ bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files) { files[fname] = f; uint8_t md5[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this, p_replace_files); - //printf("packed data add path %ls, %ls\n", p_name.c_str(), fname.c_str()); + PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this, p_replace_files, false); + //printf("packed data add path %s, %s\n", p_name.utf8().get_data(), fname.utf8().get_data()); if ((i + 1) < gi.number_entry) { unzGoToNextFile(zfile); diff --git a/core/io/file_access_zip.h b/core/io/file_access_zip.h index 776e830f36..f8e7c1e587 100644 --- a/core/io/file_access_zip.h +++ b/core/io/file_access_zip.h @@ -28,13 +28,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef MINIZIP_ENABLED - #ifndef FILE_ACCESS_ZIP_H #define FILE_ACCESS_ZIP_H +#ifdef MINIZIP_ENABLED + #include "core/io/file_access_pack.h" -#include "core/map.h" +#include "core/templates/map.h" #include "thirdparty/minizip/unzip.h" @@ -59,8 +59,6 @@ private: static ZipArchive *instance; - FileAccess::CreateFunc fa_create_func; - public: void close_handle(unzFile p_file) const; unzFile get_file_handle(String p_file) const; @@ -69,7 +67,7 @@ public: bool file_exists(String p_name) const; - virtual bool try_open_pack(const String &p_path, bool p_replace_files); + virtual bool try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset); FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file); static ZipArchive *get_singleton(); @@ -79,7 +77,7 @@ public: }; class FileAccessZip : public FileAccess { - unzFile zfile; + unzFile zfile = nullptr; unz_file_info64 file_info; mutable bool at_eof; @@ -113,6 +111,6 @@ public: ~FileAccessZip(); }; -#endif // FILE_ACCESS_ZIP_H - #endif // MINIZIP_ENABLED + +#endif // FILE_ACCESS_ZIP_H diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 46e45500bf..768fcdbb14 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -96,6 +96,10 @@ Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl, void HTTPClient::set_connection(const Ref<StreamPeer> &p_connection) { ERR_FAIL_COND_MSG(p_connection.is_null(), "Connection is not a reference to a valid StreamPeer object."); + if (connection == p_connection) { + return; + } + close(); connection = p_connection; status = STATUS_CONNECTED; @@ -105,24 +109,41 @@ Ref<StreamPeer> HTTPClient::get_connection() const { return connection; } +static bool _check_request_url(HTTPClient::Method p_method, const String &p_url) { + switch (p_method) { + case HTTPClient::METHOD_CONNECT: { + // Authority in host:port format, as in RFC7231 + int pos = p_url.find_char(':'); + return 0 < pos && pos < p_url.length() - 1; + } + case HTTPClient::METHOD_OPTIONS: { + if (p_url == "*") { + return true; + } + [[fallthrough]]; + } + default: + // Absolute path or absolute URL + return p_url.begins_with("/") || p_url.begins_with("http://") || p_url.begins_with("https://"); + } +} + Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const Vector<uint8_t> &p_body) { ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER); - ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(!_check_request_url(p_method, p_url), ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(connection.is_null(), ERR_INVALID_DATA); String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n"; - if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) { - // Don't append the standard ports - request += "Host: " + conn_host + "\r\n"; - } else { - request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; - } + bool add_host = true; bool add_clen = p_body.size() > 0; bool add_uagent = true; bool add_accept = true; for (int i = 0; i < p_headers.size(); i++) { request += p_headers[i] + "\r\n"; + if (add_host && p_headers[i].findn("Host:") == 0) { + add_host = false; + } if (add_clen && p_headers[i].findn("Content-Length:") == 0) { add_clen = false; } @@ -133,6 +154,14 @@ Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector add_accept = false; } } + if (add_host) { + if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) { + // Don't append the standard ports + request += "Host: " + conn_host + "\r\n"; + } else { + request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; + } + } if (add_clen) { request += "Content-Length: " + itos(p_body.size()) + "\r\n"; // Should it add utf8 encoding? @@ -174,22 +203,20 @@ Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector Error HTTPClient::request(Method p_method, const String &p_url, const Vector<String> &p_headers, const String &p_body) { ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER); - ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(!_check_request_url(p_method, p_url), ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(connection.is_null(), ERR_INVALID_DATA); String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n"; - if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) { - // Don't append the standard ports - request += "Host: " + conn_host + "\r\n"; - } else { - request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; - } + bool add_host = true; bool add_uagent = true; bool add_accept = true; bool add_clen = p_body.length() > 0; for (int i = 0; i < p_headers.size(); i++) { request += p_headers[i] + "\r\n"; + if (add_host && p_headers[i].findn("Host:") == 0) { + add_host = false; + } if (add_clen && p_headers[i].findn("Content-Length:") == 0) { add_clen = false; } @@ -200,6 +227,14 @@ Error HTTPClient::request(Method p_method, const String &p_url, const Vector<Str add_accept = false; } } + if (add_host) { + if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) { + // Don't append the standard ports + request += "Host: " + conn_host + "\r\n"; + } else { + request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; + } + } if (add_clen) { request += "Content-Length: " + itos(p_body.utf8().length()) + "\r\n"; // Should it add utf8 encoding? diff --git a/core/io/http_client.h b/core/io/http_client.h index 1dc1f3d76a..3d9fe321ba 100644 --- a/core/io/http_client.h +++ b/core/io/http_client.h @@ -34,14 +34,13 @@ #include "core/io/ip.h" #include "core/io/stream_peer.h" #include "core/io/stream_peer_tcp.h" -#include "core/reference.h" +#include "core/object/reference.h" class HTTPClient : public Reference { GDCLASS(HTTPClient, Reference); public: enum ResponseCode { - // 1xx informational RESPONSE_CONTINUE = 100, RESPONSE_SWITCHING_PROTOCOLS = 101, @@ -116,7 +115,6 @@ public: }; enum Method { - METHOD_GET, METHOD_HEAD, METHOD_POST, @@ -131,7 +129,6 @@ public: }; enum Status { - STATUS_DISCONNECTED, STATUS_RESOLVING, // Resolving hostname (if passed a hostname) STATUS_CANT_RESOLVE, @@ -150,7 +147,6 @@ private: static const int HOST_MIN_LEN = 4; enum Port { - PORT_HTTP = 80, PORT_HTTPS = 443, @@ -182,7 +178,8 @@ private: int response_num = 0; Vector<String> response_headers; - int read_chunk_size = 4096; + // 64 KiB by default (favors fast download speeds at the cost of memory usage). + int read_chunk_size = 65536; Error _get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received); diff --git a/core/io/image.cpp b/core/io/image.cpp new file mode 100644 index 0000000000..56d84325b5 --- /dev/null +++ b/core/io/image.cpp @@ -0,0 +1,3602 @@ +/*************************************************************************/ +/* image.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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.h" + +#include "core/error/error_macros.h" +#include "core/io/image_loader.h" +#include "core/io/resource_loader.h" +#include "core/math/math_funcs.h" +#include "core/os/copymem.h" +#include "core/string/print_string.h" +#include "core/templates/hash_map.h" + +#include <stdio.h> + +const char *Image::format_names[Image::FORMAT_MAX] = { + "Lum8", //luminance + "LumAlpha8", //luminance-alpha + "Red8", + "RedGreen", + "RGB8", + "RGBA8", + "RGBA4444", + "RGBA5551", + "RFloat", //float + "RGFloat", + "RGBFloat", + "RGBAFloat", + "RHalf", //half float + "RGHalf", + "RGBHalf", + "RGBAHalf", + "RGBE9995", + "DXT1 RGB8", //s3tc + "DXT3 RGBA8", + "DXT5 RGBA8", + "RGTC Red8", + "RGTC RedGreen8", + "BPTC_RGBA", + "BPTC_RGBF", + "BPTC_RGBFU", + "PVRTC1_2", //pvrtc + "PVRTC1_2A", + "PVRTC1_4", + "PVRTC1_4A", + "ETC", //etc1 + "ETC2_R11", //etc2 + "ETC2_R11S", //signed", NOT srgb. + "ETC2_RG11", + "ETC2_RG11S", + "ETC2_RGB8", + "ETC2_RGBA8", + "ETC2_RGB8A1", + "ETC2_RA_AS_RG", + "FORMAT_DXT5_RA_AS_RG", +}; + +SavePNGFunc Image::save_png_func = nullptr; +SaveEXRFunc Image::save_exr_func = nullptr; + +SavePNGBufferFunc Image::save_png_buffer_func = nullptr; + +void Image::_put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel) { + uint32_t ofs = (p_y * width + p_x) * p_pixelsize; + + for (uint32_t i = 0; i < p_pixelsize; i++) { + p_data[ofs + i] = p_pixel[i]; + } +} + +void Image::_get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_data, uint8_t *p_pixel) { + uint32_t ofs = (p_y * width + p_x) * p_pixelsize; + + for (uint32_t i = 0; i < p_pixelsize; i++) { + p_pixel[i] = p_data[ofs + i]; + } +} + +int Image::get_format_pixel_size(Format p_format) { + switch (p_format) { + case FORMAT_L8: + return 1; //luminance + case FORMAT_LA8: + return 2; //luminance-alpha + case FORMAT_R8: + return 1; + case FORMAT_RG8: + return 2; + case FORMAT_RGB8: + return 3; + case FORMAT_RGBA8: + return 4; + case FORMAT_RGBA4444: + return 2; + case FORMAT_RGB565: + return 2; + case FORMAT_RF: + return 4; //float + case FORMAT_RGF: + return 8; + case FORMAT_RGBF: + return 12; + case FORMAT_RGBAF: + return 16; + case FORMAT_RH: + return 2; //half float + case FORMAT_RGH: + return 4; + case FORMAT_RGBH: + return 6; + case FORMAT_RGBAH: + return 8; + case FORMAT_RGBE9995: + return 4; + case FORMAT_DXT1: + return 1; //s3tc bc1 + case FORMAT_DXT3: + return 1; //bc2 + case FORMAT_DXT5: + return 1; //bc3 + case FORMAT_RGTC_R: + return 1; //bc4 + case FORMAT_RGTC_RG: + return 1; //bc5 + case FORMAT_BPTC_RGBA: + return 1; //btpc bc6h + case FORMAT_BPTC_RGBF: + return 1; //float / + case FORMAT_BPTC_RGBFU: + return 1; //unsigned float + case FORMAT_PVRTC1_2: + return 1; //pvrtc + case FORMAT_PVRTC1_2A: + return 1; + case FORMAT_PVRTC1_4: + return 1; + case FORMAT_PVRTC1_4A: + return 1; + case FORMAT_ETC: + return 1; //etc1 + case FORMAT_ETC2_R11: + return 1; //etc2 + case FORMAT_ETC2_R11S: + return 1; //signed: return 1; NOT srgb. + case FORMAT_ETC2_RG11: + return 1; + case FORMAT_ETC2_RG11S: + return 1; + case FORMAT_ETC2_RGB8: + return 1; + case FORMAT_ETC2_RGBA8: + return 1; + case FORMAT_ETC2_RGB8A1: + return 1; + case FORMAT_ETC2_RA_AS_RG: + return 1; + case FORMAT_DXT5_RA_AS_RG: + return 1; + case FORMAT_MAX: { + } + } + return 0; +} + +void Image::get_format_min_pixel_size(Format p_format, int &r_w, int &r_h) { + switch (p_format) { + case FORMAT_DXT1: //s3tc bc1 + case FORMAT_DXT3: //bc2 + case FORMAT_DXT5: //bc3 + case FORMAT_RGTC_R: //bc4 + case FORMAT_RGTC_RG: { //bc5 case case FORMAT_DXT1: + + r_w = 4; + r_h = 4; + } break; + case FORMAT_PVRTC1_2: + case FORMAT_PVRTC1_2A: { + r_w = 16; + r_h = 8; + } break; + case FORMAT_PVRTC1_4A: + case FORMAT_PVRTC1_4: { + r_w = 8; + r_h = 8; + } break; + case FORMAT_ETC: { + r_w = 4; + r_h = 4; + } break; + case FORMAT_BPTC_RGBA: + case FORMAT_BPTC_RGBF: + case FORMAT_BPTC_RGBFU: { + r_w = 4; + r_h = 4; + } break; + case FORMAT_ETC2_R11: //etc2 + case FORMAT_ETC2_R11S: //signed: NOT srgb. + case FORMAT_ETC2_RG11: + case FORMAT_ETC2_RG11S: + case FORMAT_ETC2_RGB8: + case FORMAT_ETC2_RGBA8: + case FORMAT_ETC2_RGB8A1: + case FORMAT_ETC2_RA_AS_RG: + case FORMAT_DXT5_RA_AS_RG: { + r_w = 4; + r_h = 4; + + } break; + + default: { + r_w = 1; + r_h = 1; + } break; + } +} + +int Image::get_format_pixel_rshift(Format p_format) { + if (p_format == FORMAT_DXT1 || p_format == FORMAT_RGTC_R || p_format == FORMAT_PVRTC1_4 || p_format == FORMAT_PVRTC1_4A || p_format == FORMAT_ETC || p_format == FORMAT_ETC2_R11 || p_format == FORMAT_ETC2_R11S || p_format == FORMAT_ETC2_RGB8 || p_format == FORMAT_ETC2_RGB8A1) { + return 1; + } else if (p_format == FORMAT_PVRTC1_2 || p_format == FORMAT_PVRTC1_2A) { + return 2; + } else { + return 0; + } +} + +int Image::get_format_block_size(Format p_format) { + switch (p_format) { + case FORMAT_DXT1: //s3tc bc1 + case FORMAT_DXT3: //bc2 + case FORMAT_DXT5: //bc3 + case FORMAT_RGTC_R: //bc4 + case FORMAT_RGTC_RG: { //bc5 case case FORMAT_DXT1: + + return 4; + } + case FORMAT_PVRTC1_2: + case FORMAT_PVRTC1_2A: { + return 4; + } + case FORMAT_PVRTC1_4A: + case FORMAT_PVRTC1_4: { + return 4; + } + case FORMAT_ETC: { + return 4; + } + case FORMAT_BPTC_RGBA: + case FORMAT_BPTC_RGBF: + case FORMAT_BPTC_RGBFU: { + return 4; + } + case FORMAT_ETC2_R11: //etc2 + case FORMAT_ETC2_R11S: //signed: NOT srgb. + case FORMAT_ETC2_RG11: + case FORMAT_ETC2_RG11S: + case FORMAT_ETC2_RGB8: + case FORMAT_ETC2_RGBA8: + case FORMAT_ETC2_RGB8A1: + case FORMAT_ETC2_RA_AS_RG: //used to make basis universal happy + case FORMAT_DXT5_RA_AS_RG: //used to make basis universal happy + + { + return 4; + } + default: { + } + } + + return 1; +} + +void Image::_get_mipmap_offset_and_size(int p_mipmap, int &r_offset, int &r_width, int &r_height) const { + int w = width; + int h = height; + int ofs = 0; + + int pixel_size = get_format_pixel_size(format); + int pixel_rshift = get_format_pixel_rshift(format); + int block = get_format_block_size(format); + int minw, minh; + get_format_min_pixel_size(format, minw, minh); + + for (int i = 0; i < p_mipmap; i++) { + int bw = w % block != 0 ? w + (block - w % block) : w; + int bh = h % block != 0 ? h + (block - h % block) : h; + + int s = bw * bh; + + s *= pixel_size; + s >>= pixel_rshift; + ofs += s; + w = MAX(minw, w >> 1); + h = MAX(minh, h >> 1); + } + + r_offset = ofs; + r_width = w; + r_height = h; +} + +int Image::get_mipmap_offset(int p_mipmap) const { + ERR_FAIL_INDEX_V(p_mipmap, get_mipmap_count() + 1, -1); + + int ofs, w, h; + _get_mipmap_offset_and_size(p_mipmap, ofs, w, h); + return ofs; +} + +int Image::get_mipmap_byte_size(int p_mipmap) const { + ERR_FAIL_INDEX_V(p_mipmap, get_mipmap_count() + 1, -1); + + int ofs, w, h; + _get_mipmap_offset_and_size(p_mipmap, ofs, w, h); + int ofs2; + _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w, h); + return ofs2 - ofs; +} + +void Image::get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const { + int ofs, w, h; + _get_mipmap_offset_and_size(p_mipmap, ofs, w, h); + int ofs2; + _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w, h); + r_ofs = ofs; + r_size = ofs2 - ofs; +} + +void Image::get_mipmap_offset_size_and_dimensions(int p_mipmap, int &r_ofs, int &r_size, int &w, int &h) const { + int ofs; + _get_mipmap_offset_and_size(p_mipmap, ofs, w, h); + int ofs2, w2, h2; + _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w2, h2); + r_ofs = ofs; + r_size = ofs2 - ofs; +} + +Image::Image3DValidateError Image::validate_3d_image(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_images) { + int w = p_width; + int h = p_height; + int d = p_depth; + + int arr_ofs = 0; + + while (true) { + for (int i = 0; i < d; i++) { + int idx = i + arr_ofs; + if (idx >= p_images.size()) { + return VALIDATE_3D_ERR_MISSING_IMAGES; + } + if (p_images[idx].is_null() || p_images[idx]->empty()) { + return VALIDATE_3D_ERR_IMAGE_EMPTY; + } + if (p_images[idx]->get_format() != p_format) { + return VALIDATE_3D_ERR_IMAGE_FORMAT_MISMATCH; + } + if (p_images[idx]->get_width() != w || p_images[idx]->get_height() != h) { + return VALIDATE_3D_ERR_IMAGE_SIZE_MISMATCH; + } + if (p_images[idx]->has_mipmaps()) { + return VALIDATE_3D_ERR_IMAGE_HAS_MIPMAPS; + } + } + + arr_ofs += d; + + if (!p_mipmaps) { + break; + } + + if (w == 1 && h == 1 && d == 1) { + break; + } + + w = MAX(1, w >> 1); + h = MAX(1, h >> 1); + d = MAX(1, d >> 1); + } + + if (arr_ofs != p_images.size()) { + return VALIDATE_3D_ERR_EXTRA_IMAGES; + } + + return VALIDATE_3D_OK; +} + +String Image::get_3d_image_validation_error_text(Image3DValidateError p_error) { + switch (p_error) { + case VALIDATE_3D_OK: { + return TTR("Ok"); + } break; + case VALIDATE_3D_ERR_IMAGE_EMPTY: { + return TTR("Empty Image found"); + } break; + case VALIDATE_3D_ERR_MISSING_IMAGES: { + return TTR("Missing Images"); + } break; + case VALIDATE_3D_ERR_EXTRA_IMAGES: { + return TTR("Too many Images"); + } break; + case VALIDATE_3D_ERR_IMAGE_SIZE_MISMATCH: { + return TTR("Image size mismatch"); + } break; + case VALIDATE_3D_ERR_IMAGE_FORMAT_MISMATCH: { + return TTR("Image format mismatch"); + } break; + case VALIDATE_3D_ERR_IMAGE_HAS_MIPMAPS: { + return TTR("Image has included mipmaps"); + } break; + } + return String(); +} + +int Image::get_width() const { + return width; +} + +int Image::get_height() const { + return height; +} + +Vector2 Image::get_size() const { + return Vector2(width, height); +} + +bool Image::has_mipmaps() const { + return mipmaps; +} + +int Image::get_mipmap_count() const { + if (mipmaps) { + return get_image_required_mipmaps(width, height, format); + } else { + return 0; + } +} + +//using template generates perfectly optimized code due to constant expression reduction and unused variable removal present in all compilers +template <uint32_t read_bytes, bool read_alpha, uint32_t write_bytes, bool write_alpha, bool read_gray, bool write_gray> +static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p_dst) { + uint32_t max_bytes = MAX(read_bytes, write_bytes); + + for (int y = 0; y < p_height; y++) { + for (int x = 0; x < p_width; x++) { + const uint8_t *rofs = &p_src[((y * p_width) + x) * (read_bytes + (read_alpha ? 1 : 0))]; + uint8_t *wofs = &p_dst[((y * p_width) + x) * (write_bytes + (write_alpha ? 1 : 0))]; + + uint8_t rgba[4]; + + if (read_gray) { + rgba[0] = rofs[0]; + rgba[1] = rofs[0]; + rgba[2] = rofs[0]; + } else { + for (uint32_t i = 0; i < max_bytes; i++) { + rgba[i] = (i < read_bytes) ? rofs[i] : 0; + } + } + + if (read_alpha || write_alpha) { + rgba[3] = read_alpha ? rofs[read_bytes] : 255; + } + + if (write_gray) { + //TODO: not correct grayscale, should use fixed point version of actual weights + wofs[0] = uint8_t((uint16_t(rofs[0]) + uint16_t(rofs[1]) + uint16_t(rofs[2])) / 3); + } else { + for (uint32_t i = 0; i < write_bytes; i++) { + wofs[i] = rgba[i]; + } + } + + if (write_alpha) { + wofs[write_bytes] = rgba[3]; + } + } + } +} + +void Image::convert(Format p_new_format) { + if (data.size() == 0) { + return; + } + + if (p_new_format == format) { + return; + } + + if (format > FORMAT_RGBE9995 || p_new_format > FORMAT_RGBE9995) { + ERR_FAIL_MSG("Cannot convert to <-> from compressed formats. Use compress() and decompress() instead."); + + } else if (format > FORMAT_RGBA8 || p_new_format > FORMAT_RGBA8) { + //use put/set pixel which is slower but works with non byte formats + Image new_img(width, height, false, p_new_format); + + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + new_img.set_pixel(i, j, get_pixel(i, j)); + } + } + + if (has_mipmaps()) { + new_img.generate_mipmaps(); + } + + _copy_internals_from(new_img); + + return; + } + + Image new_img(width, height, false, p_new_format); + + const uint8_t *rptr = data.ptr(); + uint8_t *wptr = new_img.data.ptrw(); + + int conversion_type = format | p_new_format << 8; + + switch (conversion_type) { + case FORMAT_L8 | (FORMAT_LA8 << 8): + _convert<1, false, 1, true, true, true>(width, height, rptr, wptr); + break; + case FORMAT_L8 | (FORMAT_R8 << 8): + _convert<1, false, 1, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_L8 | (FORMAT_RG8 << 8): + _convert<1, false, 2, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_L8 | (FORMAT_RGB8 << 8): + _convert<1, false, 3, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_L8 | (FORMAT_RGBA8 << 8): + _convert<1, false, 3, true, true, false>(width, height, rptr, wptr); + break; + case FORMAT_LA8 | (FORMAT_L8 << 8): + _convert<1, true, 1, false, true, true>(width, height, rptr, wptr); + break; + case FORMAT_LA8 | (FORMAT_R8 << 8): + _convert<1, true, 1, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_LA8 | (FORMAT_RG8 << 8): + _convert<1, true, 2, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_LA8 | (FORMAT_RGB8 << 8): + _convert<1, true, 3, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_LA8 | (FORMAT_RGBA8 << 8): + _convert<1, true, 3, true, true, false>(width, height, rptr, wptr); + break; + case FORMAT_R8 | (FORMAT_L8 << 8): + _convert<1, false, 1, false, false, true>(width, height, rptr, wptr); + break; + case FORMAT_R8 | (FORMAT_LA8 << 8): + _convert<1, false, 1, true, false, true>(width, height, rptr, wptr); + break; + case FORMAT_R8 | (FORMAT_RG8 << 8): + _convert<1, false, 2, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_R8 | (FORMAT_RGB8 << 8): + _convert<1, false, 3, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_R8 | (FORMAT_RGBA8 << 8): + _convert<1, false, 3, true, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RG8 | (FORMAT_L8 << 8): + _convert<2, false, 1, false, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RG8 | (FORMAT_LA8 << 8): + _convert<2, false, 1, true, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RG8 | (FORMAT_R8 << 8): + _convert<2, false, 1, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RG8 | (FORMAT_RGB8 << 8): + _convert<2, false, 3, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RG8 | (FORMAT_RGBA8 << 8): + _convert<2, false, 3, true, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGB8 | (FORMAT_L8 << 8): + _convert<3, false, 1, false, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RGB8 | (FORMAT_LA8 << 8): + _convert<3, false, 1, true, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RGB8 | (FORMAT_R8 << 8): + _convert<3, false, 1, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGB8 | (FORMAT_RG8 << 8): + _convert<3, false, 2, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGB8 | (FORMAT_RGBA8 << 8): + _convert<3, false, 3, true, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGBA8 | (FORMAT_L8 << 8): + _convert<3, true, 1, false, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RGBA8 | (FORMAT_LA8 << 8): + _convert<3, true, 1, true, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RGBA8 | (FORMAT_R8 << 8): + _convert<3, true, 1, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGBA8 | (FORMAT_RG8 << 8): + _convert<3, true, 2, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGBA8 | (FORMAT_RGB8 << 8): + _convert<3, true, 3, false, false, false>(width, height, rptr, wptr); + break; + } + + bool gen_mipmaps = mipmaps; + + _copy_internals_from(new_img); + + if (gen_mipmaps) { + generate_mipmaps(); + } +} + +Image::Format Image::get_format() const { + return format; +} + +static double _bicubic_interp_kernel(double x) { + x = ABS(x); + + double bc = 0; + + if (x <= 1) { + bc = (1.5 * x - 2.5) * x * x + 1; + } else if (x < 2) { + bc = ((-0.5 * x + 2.5) * x - 4) * x + 2; + } + + return bc; +} + +template <int CC, class T> +static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) { + // get source image size + int width = p_src_width; + int height = p_src_height; + double xfac = (double)width / p_dst_width; + double yfac = (double)height / p_dst_height; + // coordinates of source points and coefficients + double ox, oy, dx, dy, k1, k2; + int ox1, oy1, ox2, oy2; + // destination pixel values + // width and height decreased by 1 + int ymax = height - 1; + int xmax = width - 1; + // temporary pointer + + for (uint32_t y = 0; y < p_dst_height; y++) { + // Y coordinates + oy = (double)y * yfac - 0.5f; + oy1 = (int)oy; + dy = oy - (double)oy1; + + for (uint32_t x = 0; x < p_dst_width; x++) { + // X coordinates + ox = (double)x * xfac - 0.5f; + ox1 = (int)ox; + dx = ox - (double)ox1; + + // initial pixel value + + T *__restrict dst = ((T *)p_dst) + (y * p_dst_width + x) * CC; + + double color[CC]; + for (int i = 0; i < CC; i++) { + color[i] = 0; + } + + for (int n = -1; n < 3; n++) { + // get Y coefficient + k1 = _bicubic_interp_kernel(dy - (double)n); + + oy2 = oy1 + n; + if (oy2 < 0) { + oy2 = 0; + } + if (oy2 > ymax) { + oy2 = ymax; + } + + for (int m = -1; m < 3; m++) { + // get X coefficient + k2 = k1 * _bicubic_interp_kernel((double)m - dx); + + ox2 = ox1 + m; + if (ox2 < 0) { + ox2 = 0; + } + if (ox2 > xmax) { + ox2 = xmax; + } + + // get pixel of original image + const T *__restrict p = ((T *)p_src) + (oy2 * p_src_width + ox2) * CC; + + for (int i = 0; i < CC; i++) { + if (sizeof(T) == 2) { //half float + color[i] = Math::half_to_float(p[i]); + } else { + color[i] += p[i] * k2; + } + } + } + } + + for (int i = 0; i < CC; i++) { + if (sizeof(T) == 1) { //byte + dst[i] = CLAMP(Math::fast_ftoi(color[i]), 0, 255); + } else if (sizeof(T) == 2) { //half float + dst[i] = Math::make_half_float(color[i]); + } else { + dst[i] = color[i]; + } + } + } + } +} + +template <int CC, class T> +static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) { + enum { + FRAC_BITS = 8, + FRAC_LEN = (1 << FRAC_BITS), + FRAC_HALF = (FRAC_LEN >> 1), + FRAC_MASK = FRAC_LEN - 1 + }; + + for (uint32_t i = 0; i < p_dst_height; i++) { + // Add 0.5 in order to interpolate based on pixel center + uint32_t src_yofs_up_fp = (i + 0.5) * p_src_height * FRAC_LEN / p_dst_height; + // Calculate nearest src pixel center above current, and truncate to get y index + uint32_t src_yofs_up = src_yofs_up_fp >= FRAC_HALF ? (src_yofs_up_fp - FRAC_HALF) >> FRAC_BITS : 0; + uint32_t src_yofs_down = (src_yofs_up_fp + FRAC_HALF) >> FRAC_BITS; + if (src_yofs_down >= p_src_height) { + src_yofs_down = p_src_height - 1; + } + // Calculate distance to pixel center of src_yofs_up + uint32_t src_yofs_frac = src_yofs_up_fp & FRAC_MASK; + src_yofs_frac = src_yofs_frac >= FRAC_HALF ? src_yofs_frac - FRAC_HALF : src_yofs_frac + FRAC_HALF; + + uint32_t y_ofs_up = src_yofs_up * p_src_width * CC; + uint32_t y_ofs_down = src_yofs_down * p_src_width * CC; + + for (uint32_t j = 0; j < p_dst_width; j++) { + uint32_t src_xofs_left_fp = (j + 0.5) * p_src_width * FRAC_LEN / p_dst_width; + uint32_t src_xofs_left = src_xofs_left_fp >= FRAC_HALF ? (src_xofs_left_fp - FRAC_HALF) >> FRAC_BITS : 0; + uint32_t src_xofs_right = (src_xofs_left_fp + FRAC_HALF) >> FRAC_BITS; + if (src_xofs_right >= p_src_width) { + src_xofs_right = p_src_width - 1; + } + uint32_t src_xofs_frac = src_xofs_left_fp & FRAC_MASK; + src_xofs_frac = src_xofs_frac >= FRAC_HALF ? src_xofs_frac - FRAC_HALF : src_xofs_frac + FRAC_HALF; + + src_xofs_left *= CC; + src_xofs_right *= CC; + + for (uint32_t l = 0; l < CC; l++) { + if (sizeof(T) == 1) { //uint8 + uint32_t p00 = p_src[y_ofs_up + src_xofs_left + l] << FRAC_BITS; + uint32_t p10 = p_src[y_ofs_up + src_xofs_right + l] << FRAC_BITS; + uint32_t p01 = p_src[y_ofs_down + src_xofs_left + l] << FRAC_BITS; + uint32_t p11 = p_src[y_ofs_down + src_xofs_right + l] << FRAC_BITS; + + uint32_t interp_up = p00 + (((p10 - p00) * src_xofs_frac) >> FRAC_BITS); + uint32_t interp_down = p01 + (((p11 - p01) * src_xofs_frac) >> FRAC_BITS); + uint32_t interp = interp_up + (((interp_down - interp_up) * src_yofs_frac) >> FRAC_BITS); + interp >>= FRAC_BITS; + p_dst[i * p_dst_width * CC + j * CC + l] = interp; + } else if (sizeof(T) == 2) { //half float + + float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS); + float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS); + const T *src = ((const T *)p_src); + T *dst = ((T *)p_dst); + + float p00 = Math::half_to_float(src[y_ofs_up + src_xofs_left + l]); + float p10 = Math::half_to_float(src[y_ofs_up + src_xofs_right + l]); + float p01 = Math::half_to_float(src[y_ofs_down + src_xofs_left + l]); + float p11 = Math::half_to_float(src[y_ofs_down + src_xofs_right + l]); + + float interp_up = p00 + (p10 - p00) * xofs_frac; + float interp_down = p01 + (p11 - p01) * xofs_frac; + float interp = interp_up + ((interp_down - interp_up) * yofs_frac); + + dst[i * p_dst_width * CC + j * CC + l] = Math::make_half_float(interp); + } else if (sizeof(T) == 4) { //float + + float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS); + float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS); + const T *src = ((const T *)p_src); + T *dst = ((T *)p_dst); + + float p00 = src[y_ofs_up + src_xofs_left + l]; + float p10 = src[y_ofs_up + src_xofs_right + l]; + float p01 = src[y_ofs_down + src_xofs_left + l]; + float p11 = src[y_ofs_down + src_xofs_right + l]; + + float interp_up = p00 + (p10 - p00) * xofs_frac; + float interp_down = p01 + (p11 - p01) * xofs_frac; + float interp = interp_up + ((interp_down - interp_up) * yofs_frac); + + dst[i * p_dst_width * CC + j * CC + l] = interp; + } + } + } + } +} + +template <int CC, class T> +static void _scale_nearest(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) { + for (uint32_t i = 0; i < p_dst_height; i++) { + uint32_t src_yofs = i * p_src_height / p_dst_height; + uint32_t y_ofs = src_yofs * p_src_width * CC; + + for (uint32_t j = 0; j < p_dst_width; j++) { + uint32_t src_xofs = j * p_src_width / p_dst_width; + src_xofs *= CC; + + for (uint32_t l = 0; l < CC; l++) { + const T *src = ((const T *)p_src); + T *dst = ((T *)p_dst); + + T p = src[y_ofs + src_xofs + l]; + dst[i * p_dst_width * CC + j * CC + l] = p; + } + } + } +} + +#define LANCZOS_TYPE 3 + +static float _lanczos(float p_x) { + return Math::abs(p_x) >= LANCZOS_TYPE ? 0 : Math::sincn(p_x) * Math::sincn(p_x / LANCZOS_TYPE); +} + +template <int CC, class T> +static void _scale_lanczos(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) { + int32_t src_width = p_src_width; + int32_t src_height = p_src_height; + int32_t dst_height = p_dst_height; + int32_t dst_width = p_dst_width; + + uint32_t buffer_size = src_height * dst_width * CC; + float *buffer = memnew_arr(float, buffer_size); // Store the first pass in a buffer + + { // FIRST PASS (horizontal) + + float x_scale = float(src_width) / float(dst_width); + + float scale_factor = MAX(x_scale, 1); // A larger kernel is required only when downscaling + int32_t half_kernel = LANCZOS_TYPE * scale_factor; + + float *kernel = memnew_arr(float, half_kernel * 2); + + for (int32_t buffer_x = 0; buffer_x < dst_width; buffer_x++) { + // The corresponding point on the source image + float src_x = (buffer_x + 0.5f) * x_scale; // Offset by 0.5 so it uses the pixel's center + int32_t start_x = MAX(0, int32_t(src_x) - half_kernel + 1); + int32_t end_x = MIN(src_width - 1, int32_t(src_x) + half_kernel); + + // Create the kernel used by all the pixels of the column + for (int32_t target_x = start_x; target_x <= end_x; target_x++) { + kernel[target_x - start_x] = _lanczos((target_x + 0.5f - src_x) / scale_factor); + } + + for (int32_t buffer_y = 0; buffer_y < src_height; buffer_y++) { + float pixel[CC] = { 0 }; + float weight = 0; + + for (int32_t target_x = start_x; target_x <= end_x; target_x++) { + float lanczos_val = kernel[target_x - start_x]; + weight += lanczos_val; + + const T *__restrict src_data = ((const T *)p_src) + (buffer_y * src_width + target_x) * CC; + + for (uint32_t i = 0; i < CC; i++) { + if (sizeof(T) == 2) { //half float + pixel[i] += Math::half_to_float(src_data[i]) * lanczos_val; + } else { + pixel[i] += src_data[i] * lanczos_val; + } + } + } + + float *dst_data = ((float *)buffer) + (buffer_y * dst_width + buffer_x) * CC; + + for (uint32_t i = 0; i < CC; i++) { + dst_data[i] = pixel[i] / weight; // Normalize the sum of all the samples + } + } + } + + memdelete_arr(kernel); + } // End of first pass + + { // SECOND PASS (vertical + result) + + float y_scale = float(src_height) / float(dst_height); + + float scale_factor = MAX(y_scale, 1); + int32_t half_kernel = LANCZOS_TYPE * scale_factor; + + float *kernel = memnew_arr(float, half_kernel * 2); + + for (int32_t dst_y = 0; dst_y < dst_height; dst_y++) { + float buffer_y = (dst_y + 0.5f) * y_scale; + int32_t start_y = MAX(0, int32_t(buffer_y) - half_kernel + 1); + int32_t end_y = MIN(src_height - 1, int32_t(buffer_y) + half_kernel); + + for (int32_t target_y = start_y; target_y <= end_y; target_y++) { + kernel[target_y - start_y] = _lanczos((target_y + 0.5f - buffer_y) / scale_factor); + } + + for (int32_t dst_x = 0; dst_x < dst_width; dst_x++) { + float pixel[CC] = { 0 }; + float weight = 0; + + for (int32_t target_y = start_y; target_y <= end_y; target_y++) { + float lanczos_val = kernel[target_y - start_y]; + weight += lanczos_val; + + float *buffer_data = ((float *)buffer) + (target_y * dst_width + dst_x) * CC; + + for (uint32_t i = 0; i < CC; i++) { + pixel[i] += buffer_data[i] * lanczos_val; + } + } + + T *dst_data = ((T *)p_dst) + (dst_y * dst_width + dst_x) * CC; + + for (uint32_t i = 0; i < CC; i++) { + pixel[i] /= weight; + + if (sizeof(T) == 1) { //byte + dst_data[i] = CLAMP(Math::fast_ftoi(pixel[i]), 0, 255); + } else if (sizeof(T) == 2) { //half float + dst_data[i] = Math::make_half_float(pixel[i]); + } else { // float + dst_data[i] = pixel[i]; + } + } + } + } + + memdelete_arr(kernel); + } // End of second pass + + memdelete_arr(buffer); +} + +static void _overlay(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, float p_alpha, uint32_t p_width, uint32_t p_height, uint32_t p_pixel_size) { + uint16_t alpha = MIN((uint16_t)(p_alpha * 256.0f), 256); + + for (uint32_t i = 0; i < p_width * p_height * p_pixel_size; i++) { + p_dst[i] = (p_dst[i] * (256 - alpha) + p_src[i] * alpha) >> 8; + } +} + +bool Image::is_size_po2() const { + return uint32_t(width) == next_power_of_2(width) && uint32_t(height) == next_power_of_2(height); +} + +void Image::resize_to_po2(bool p_square, Interpolation p_interpolation) { + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats."); + + int w = next_power_of_2(width); + int h = next_power_of_2(height); + if (p_square) { + w = h = MAX(w, h); + } + + if (w == width && h == height) { + if (!p_square || w == h) { + return; //nothing to do + } + } + + resize(w, h, p_interpolation); +} + +void Image::resize(int p_width, int p_height, Interpolation p_interpolation) { + ERR_FAIL_COND_MSG(data.size() == 0, "Cannot resize image before creating it, use create() or create_from_data() first."); + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats."); + + bool mipmap_aware = p_interpolation == INTERPOLATE_TRILINEAR /* || p_interpolation == INTERPOLATE_TRICUBIC */; + + ERR_FAIL_COND_MSG(p_width <= 0, "Image width must be greater than 0."); + ERR_FAIL_COND_MSG(p_height <= 0, "Image height must be greater than 0."); + ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, "Image width cannot be greater than " + itos(MAX_WIDTH) + "."); + ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT, "Image height cannot be greater than " + itos(MAX_HEIGHT) + "."); + ERR_FAIL_COND_MSG(p_width * p_height > MAX_PIXELS, "Too many pixels for image, maximum is " + itos(MAX_PIXELS)); + + if (p_width == width && p_height == height) { + return; + } + + Image dst(p_width, p_height, false, format); + + // Setup mipmap-aware scaling + Image dst2; + int mip1 = 0; + int mip2 = 0; + float mip1_weight = 0; + if (mipmap_aware) { + float avg_scale = ((float)p_width / width + (float)p_height / height) * 0.5f; + if (avg_scale >= 1.0f) { + mipmap_aware = false; + } else { + float level = Math::log(1.0f / avg_scale) / Math::log(2.0f); + mip1 = CLAMP((int)Math::floor(level), 0, get_mipmap_count()); + mip2 = CLAMP((int)Math::ceil(level), 0, get_mipmap_count()); + mip1_weight = 1.0f - (level - mip1); + } + } + bool interpolate_mipmaps = mipmap_aware && mip1 != mip2; + if (interpolate_mipmaps) { + dst2.create(p_width, p_height, false, format); + } + + bool had_mipmaps = mipmaps; + if (interpolate_mipmaps && !had_mipmaps) { + generate_mipmaps(); + } + // -- + + const uint8_t *r = data.ptr(); + const unsigned char *r_ptr = r; + + uint8_t *w = dst.data.ptrw(); + unsigned char *w_ptr = w; + + switch (p_interpolation) { + case INTERPOLATE_NEAREST: { + if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) { + switch (get_format_pixel_size(format)) { + case 1: + _scale_nearest<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 2: + _scale_nearest<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 3: + _scale_nearest<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_nearest<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) { + switch (get_format_pixel_size(format)) { + case 4: + _scale_nearest<1, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_nearest<2, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 12: + _scale_nearest<3, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 16: + _scale_nearest<4, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + + } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) { + switch (get_format_pixel_size(format)) { + case 2: + _scale_nearest<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_nearest<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 6: + _scale_nearest<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_nearest<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } + + } break; + case INTERPOLATE_BILINEAR: + case INTERPOLATE_TRILINEAR: { + for (int i = 0; i < 2; ++i) { + int src_width; + int src_height; + const unsigned char *src_ptr; + + if (!mipmap_aware) { + if (i == 0) { + // Standard behavior + src_width = width; + src_height = height; + src_ptr = r_ptr; + } else { + // No need for a second iteration + break; + } + } else { + if (i == 0) { + // Read from the first mipmap that will be interpolated + // (if both levels are the same, we will not interpolate, but at least we'll sample from the right level) + int offs; + _get_mipmap_offset_and_size(mip1, offs, src_width, src_height); + src_ptr = r_ptr + offs; + } else if (!interpolate_mipmaps) { + // No need generate a second image + break; + } else { + // Switch to read from the second mipmap that will be interpolated + int offs; + _get_mipmap_offset_and_size(mip2, offs, src_width, src_height); + src_ptr = r_ptr + offs; + // Switch to write to the second destination image + w = dst2.data.ptrw(); + w_ptr = w; + } + } + + if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) { + switch (get_format_pixel_size(format)) { + case 1: + _scale_bilinear<1, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 2: + _scale_bilinear<2, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 3: + _scale_bilinear<3, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 4: + _scale_bilinear<4, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) { + switch (get_format_pixel_size(format)) { + case 4: + _scale_bilinear<1, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 8: + _scale_bilinear<2, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 12: + _scale_bilinear<3, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 16: + _scale_bilinear<4, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) { + switch (get_format_pixel_size(format)) { + case 2: + _scale_bilinear<1, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 4: + _scale_bilinear<2, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 6: + _scale_bilinear<3, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 8: + _scale_bilinear<4, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + } + } + } + + if (interpolate_mipmaps) { + // Switch to read again from the first scaled mipmap to overlay it over the second + r = dst.data.ptr(); + _overlay(r, w, mip1_weight, p_width, p_height, get_format_pixel_size(format)); + } + + } break; + case INTERPOLATE_CUBIC: { + if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) { + switch (get_format_pixel_size(format)) { + case 1: + _scale_cubic<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 2: + _scale_cubic<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 3: + _scale_cubic<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_cubic<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) { + switch (get_format_pixel_size(format)) { + case 4: + _scale_cubic<1, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_cubic<2, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 12: + _scale_cubic<3, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 16: + _scale_cubic<4, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) { + switch (get_format_pixel_size(format)) { + case 2: + _scale_cubic<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_cubic<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 6: + _scale_cubic<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_cubic<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } + } break; + case INTERPOLATE_LANCZOS: { + if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) { + switch (get_format_pixel_size(format)) { + case 1: + _scale_lanczos<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 2: + _scale_lanczos<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 3: + _scale_lanczos<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_lanczos<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) { + switch (get_format_pixel_size(format)) { + case 4: + _scale_lanczos<1, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_lanczos<2, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 12: + _scale_lanczos<3, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 16: + _scale_lanczos<4, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) { + switch (get_format_pixel_size(format)) { + case 2: + _scale_lanczos<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_lanczos<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 6: + _scale_lanczos<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_lanczos<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } + } break; + } + + if (interpolate_mipmaps) { + dst._copy_internals_from(dst2); + } + + if (had_mipmaps) { + dst.generate_mipmaps(); + } + + _copy_internals_from(dst); +} + +void Image::crop_from_point(int p_x, int p_y, int p_width, int p_height) { + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot crop in compressed or custom image formats."); + + ERR_FAIL_COND_MSG(p_x < 0, "Start x position cannot be smaller than 0."); + ERR_FAIL_COND_MSG(p_y < 0, "Start y position cannot be smaller than 0."); + ERR_FAIL_COND_MSG(p_width <= 0, "Width of image must be greater than 0."); + ERR_FAIL_COND_MSG(p_height <= 0, "Height of image must be greater than 0."); + ERR_FAIL_COND_MSG(p_x + p_width > MAX_WIDTH, "End x position cannot be greater than " + itos(MAX_WIDTH) + "."); + ERR_FAIL_COND_MSG(p_y + p_height > MAX_HEIGHT, "End y position cannot be greater than " + itos(MAX_HEIGHT) + "."); + + /* to save memory, cropping should be done in-place, however, since this function + will most likely either not be used much, or in critical areas, for now it won't, because + it's a waste of time. */ + + if (p_width == width && p_height == height && p_x == 0 && p_y == 0) { + return; + } + + uint8_t pdata[16]; //largest is 16 + uint32_t pixel_size = get_format_pixel_size(format); + + Image dst(p_width, p_height, false, format); + + { + const uint8_t *r = data.ptr(); + uint8_t *w = dst.data.ptrw(); + + int m_h = p_y + p_height; + int m_w = p_x + p_width; + for (int y = p_y; y < m_h; y++) { + for (int x = p_x; x < m_w; x++) { + if ((x >= width || y >= height)) { + for (uint32_t i = 0; i < pixel_size; i++) { + pdata[i] = 0; + } + } else { + _get_pixelb(x, y, pixel_size, r, pdata); + } + + dst._put_pixelb(x - p_x, y - p_y, pixel_size, w, pdata); + } + } + } + + if (has_mipmaps()) { + dst.generate_mipmaps(); + } + _copy_internals_from(dst); +} + +void Image::crop(int p_width, int p_height) { + crop_from_point(0, 0, p_width, p_height); +} + +void Image::flip_y() { + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot flip_y in compressed or custom image formats."); + + bool used_mipmaps = has_mipmaps(); + if (used_mipmaps) { + clear_mipmaps(); + } + + { + uint8_t *w = data.ptrw(); + uint8_t up[16]; + uint8_t down[16]; + uint32_t pixel_size = get_format_pixel_size(format); + + for (int y = 0; y < height / 2; y++) { + for (int x = 0; x < width; x++) { + _get_pixelb(x, y, pixel_size, w, up); + _get_pixelb(x, height - y - 1, pixel_size, w, down); + + _put_pixelb(x, height - y - 1, pixel_size, w, up); + _put_pixelb(x, y, pixel_size, w, down); + } + } + } + + if (used_mipmaps) { + generate_mipmaps(); + } +} + +void Image::flip_x() { + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot flip_x in compressed or custom image formats."); + + bool used_mipmaps = has_mipmaps(); + if (used_mipmaps) { + clear_mipmaps(); + } + + { + uint8_t *w = data.ptrw(); + uint8_t up[16]; + uint8_t down[16]; + uint32_t pixel_size = get_format_pixel_size(format); + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width / 2; x++) { + _get_pixelb(x, y, pixel_size, w, up); + _get_pixelb(width - x - 1, y, pixel_size, w, down); + + _put_pixelb(width - x - 1, y, pixel_size, w, up); + _put_pixelb(x, y, pixel_size, w, down); + } + } + } + + if (used_mipmaps) { + generate_mipmaps(); + } +} + +int Image::_get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps, int *r_mm_width, int *r_mm_height) { + int size = 0; + int w = p_width; + int h = p_height; + int mm = 0; + + int pixsize = get_format_pixel_size(p_format); + int pixshift = get_format_pixel_rshift(p_format); + int block = get_format_block_size(p_format); + //technically, you can still compress up to 1 px no matter the format, so commenting this + //int minw, minh; + //get_format_min_pixel_size(p_format, minw, minh); + int minw = 1, minh = 1; + + while (true) { + int bw = w % block != 0 ? w + (block - w % block) : w; + int bh = h % block != 0 ? h + (block - h % block) : h; + + int s = bw * bh; + + s *= pixsize; + s >>= pixshift; + + size += s; + + if (r_mm_width) { + *r_mm_width = bw; + } + if (r_mm_height) { + *r_mm_height = bh; + } + + if (p_mipmaps >= 0 && mm == p_mipmaps) { + break; + } + + if (p_mipmaps >= 0) { + w = MAX(minw, w >> 1); + h = MAX(minh, h >> 1); + } else { + if (w == minw && h == minh) { + break; + } + w = MAX(minw, w >> 1); + h = MAX(minh, h >> 1); + } + mm++; + } + + r_mipmaps = mm; + return size; +} + +bool Image::_can_modify(Format p_format) const { + return p_format <= FORMAT_RGBE9995; +} + +template <class Component, int CC, bool renormalize, + void (*average_func)(Component &, const Component &, const Component &, const Component &, const Component &), + void (*renormalize_func)(Component *)> +static void _generate_po2_mipmap(const Component *p_src, Component *p_dst, uint32_t p_width, uint32_t p_height) { + //fast power of 2 mipmap generation + uint32_t dst_w = MAX(p_width >> 1, 1); + uint32_t dst_h = MAX(p_height >> 1, 1); + + int right_step = (p_width == 1) ? 0 : CC; + int down_step = (p_height == 1) ? 0 : (p_width * CC); + + for (uint32_t i = 0; i < dst_h; i++) { + const Component *rup_ptr = &p_src[i * 2 * down_step]; + const Component *rdown_ptr = rup_ptr + down_step; + Component *dst_ptr = &p_dst[i * dst_w * CC]; + uint32_t count = dst_w; + + while (count) { + count--; + for (int j = 0; j < CC; j++) { + average_func(dst_ptr[j], rup_ptr[j], rup_ptr[j + right_step], rdown_ptr[j], rdown_ptr[j + right_step]); + } + + if (renormalize) { + renormalize_func(dst_ptr); + } + + dst_ptr += CC; + rup_ptr += right_step * 2; + rdown_ptr += right_step * 2; + } + } +} + +void Image::shrink_x2() { + ERR_FAIL_COND(data.size() == 0); + + if (mipmaps) { + //just use the lower mipmap as base and copy all + Vector<uint8_t> new_img; + + int ofs = get_mipmap_offset(1); + + int new_size = data.size() - ofs; + new_img.resize(new_size); + ERR_FAIL_COND(new_img.size() == 0); + + { + uint8_t *w = new_img.ptrw(); + const uint8_t *r = data.ptr(); + + copymem(w, &r[ofs], new_size); + } + + width = MAX(width / 2, 1); + height = MAX(height / 2, 1); + data = new_img; + + } else { + Vector<uint8_t> new_img; + + ERR_FAIL_COND(!_can_modify(format)); + int ps = get_format_pixel_size(format); + new_img.resize((width / 2) * (height / 2) * ps); + ERR_FAIL_COND(new_img.size() == 0); + ERR_FAIL_COND(data.size() == 0); + + { + uint8_t *w = new_img.ptrw(); + const uint8_t *r = data.ptr(); + + switch (format) { + case FORMAT_L8: + case FORMAT_R8: + _generate_po2_mipmap<uint8_t, 1, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height); + break; + case FORMAT_LA8: + _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height); + break; + case FORMAT_RG8: + _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height); + break; + case FORMAT_RGB8: + _generate_po2_mipmap<uint8_t, 3, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height); + break; + case FORMAT_RGBA8: + _generate_po2_mipmap<uint8_t, 4, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height); + break; + + case FORMAT_RF: + _generate_po2_mipmap<float, 1, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height); + break; + case FORMAT_RGF: + _generate_po2_mipmap<float, 2, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height); + break; + case FORMAT_RGBF: + _generate_po2_mipmap<float, 3, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height); + break; + case FORMAT_RGBAF: + _generate_po2_mipmap<float, 4, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height); + break; + + case FORMAT_RH: + _generate_po2_mipmap<uint16_t, 1, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r), reinterpret_cast<uint16_t *>(w), width, height); + break; + case FORMAT_RGH: + _generate_po2_mipmap<uint16_t, 2, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r), reinterpret_cast<uint16_t *>(w), width, height); + break; + case FORMAT_RGBH: + _generate_po2_mipmap<uint16_t, 3, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r), reinterpret_cast<uint16_t *>(w), width, height); + break; + case FORMAT_RGBAH: + _generate_po2_mipmap<uint16_t, 4, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r), reinterpret_cast<uint16_t *>(w), width, height); + break; + + case FORMAT_RGBE9995: + _generate_po2_mipmap<uint32_t, 1, false, Image::average_4_rgbe9995, Image::renormalize_rgbe9995>(reinterpret_cast<const uint32_t *>(r), reinterpret_cast<uint32_t *>(w), width, height); + break; + default: { + } + } + } + + width /= 2; + height /= 2; + data = new_img; + } +} + +void Image::normalize() { + bool used_mipmaps = has_mipmaps(); + if (used_mipmaps) { + clear_mipmaps(); + } + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + Color c = get_pixel(x, y); + Vector3 v(c.r * 2.0 - 1.0, c.g * 2.0 - 1.0, c.b * 2.0 - 1.0); + v.normalize(); + c.r = v.x * 0.5 + 0.5; + c.g = v.y * 0.5 + 0.5; + c.b = v.z * 0.5 + 0.5; + set_pixel(x, y, c); + } + } + + if (used_mipmaps) { + generate_mipmaps(true); + } +} + +Error Image::generate_mipmaps(bool p_renormalize) { + ERR_FAIL_COND_V_MSG(!_can_modify(format), ERR_UNAVAILABLE, "Cannot generate mipmaps in compressed or custom image formats."); + + ERR_FAIL_COND_V_MSG(format == FORMAT_RGBA4444, ERR_UNAVAILABLE, "Cannot generate mipmaps from RGBA4444 format."); + + ERR_FAIL_COND_V_MSG(width == 0 || height == 0, ERR_UNCONFIGURED, "Cannot generate mipmaps with width or height equal to 0."); + + int mmcount; + + int size = _get_dst_image_size(width, height, format, mmcount); + + data.resize(size); + + uint8_t *wp = data.ptrw(); + + int prev_ofs = 0; + int prev_h = height; + int prev_w = width; + + for (int i = 1; i <= mmcount; i++) { + int ofs, w, h; + _get_mipmap_offset_and_size(i, ofs, w, h); + + switch (format) { + case FORMAT_L8: + case FORMAT_R8: + _generate_po2_mipmap<uint8_t, 1, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + break; + case FORMAT_LA8: + case FORMAT_RG8: + _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + break; + case FORMAT_RGB8: + if (p_renormalize) { + _generate_po2_mipmap<uint8_t, 3, true, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + } else { + _generate_po2_mipmap<uint8_t, 3, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + } + + break; + case FORMAT_RGBA8: + if (p_renormalize) { + _generate_po2_mipmap<uint8_t, 4, true, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + } else { + _generate_po2_mipmap<uint8_t, 4, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + } + break; + case FORMAT_RF: + _generate_po2_mipmap<float, 1, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + break; + case FORMAT_RGF: + _generate_po2_mipmap<float, 2, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + break; + case FORMAT_RGBF: + if (p_renormalize) { + _generate_po2_mipmap<float, 3, true, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + } else { + _generate_po2_mipmap<float, 3, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + } + + break; + case FORMAT_RGBAF: + if (p_renormalize) { + _generate_po2_mipmap<float, 4, true, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + } else { + _generate_po2_mipmap<float, 4, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + } + + break; + case FORMAT_RH: + _generate_po2_mipmap<uint16_t, 1, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + break; + case FORMAT_RGH: + _generate_po2_mipmap<uint16_t, 2, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + break; + case FORMAT_RGBH: + if (p_renormalize) { + _generate_po2_mipmap<uint16_t, 3, true, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + } else { + _generate_po2_mipmap<uint16_t, 3, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + } + + break; + case FORMAT_RGBAH: + if (p_renormalize) { + _generate_po2_mipmap<uint16_t, 4, true, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + } else { + _generate_po2_mipmap<uint16_t, 4, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + } + + break; + case FORMAT_RGBE9995: + if (p_renormalize) { + _generate_po2_mipmap<uint32_t, 1, true, Image::average_4_rgbe9995, Image::renormalize_rgbe9995>(reinterpret_cast<const uint32_t *>(&wp[prev_ofs]), reinterpret_cast<uint32_t *>(&wp[ofs]), prev_w, prev_h); + } else { + _generate_po2_mipmap<uint32_t, 1, false, Image::average_4_rgbe9995, Image::renormalize_rgbe9995>(reinterpret_cast<const uint32_t *>(&wp[prev_ofs]), reinterpret_cast<uint32_t *>(&wp[ofs]), prev_w, prev_h); + } + + break; + default: { + } + } + + prev_ofs = ofs; + prev_w = w; + prev_h = h; + } + + mipmaps = true; + + return OK; +} + +Error Image::generate_mipmap_roughness(RoughnessChannel p_roughness_channel, const Ref<Image> &p_normal_map) { + Vector<double> normal_sat_vec; //summed area table + double *normal_sat = nullptr; //summed area table for normalmap + int normal_w = 0, normal_h = 0; + + ERR_FAIL_COND_V_MSG(p_normal_map.is_null() || p_normal_map->empty(), ERR_INVALID_PARAMETER, "Must provide a valid normalmap for roughness mipmaps"); + + Ref<Image> nm = p_normal_map->duplicate(); + if (nm->is_compressed()) { + nm->decompress(); + } + + normal_w = nm->get_width(); + normal_h = nm->get_height(); + + normal_sat_vec.resize(normal_w * normal_h * 3); + + normal_sat = normal_sat_vec.ptrw(); + + //create summed area table + + for (int y = 0; y < normal_h; y++) { + double line_sum[3] = { 0, 0, 0 }; + for (int x = 0; x < normal_w; x++) { + double normal[3]; + Color color = nm->get_pixel(x, y); + normal[0] = color.r * 2.0 - 1.0; + normal[1] = color.g * 2.0 - 1.0; + normal[2] = Math::sqrt(MAX(0.0, 1.0 - (normal[0] * normal[0] + normal[1] * normal[1]))); //reconstruct if missing + + line_sum[0] += normal[0]; + line_sum[1] += normal[1]; + line_sum[2] += normal[2]; + + uint32_t ofs = (y * normal_w + x) * 3; + + normal_sat[ofs + 0] = line_sum[0]; + normal_sat[ofs + 1] = line_sum[1]; + normal_sat[ofs + 2] = line_sum[2]; + + if (y > 0) { + uint32_t prev_ofs = ((y - 1) * normal_w + x) * 3; + normal_sat[ofs + 0] += normal_sat[prev_ofs + 0]; + normal_sat[ofs + 1] += normal_sat[prev_ofs + 1]; + normal_sat[ofs + 2] += normal_sat[prev_ofs + 2]; + } + } + } + +#if 0 + { + Vector3 beg(normal_sat_vec[0], normal_sat_vec[1], normal_sat_vec[2]); + Vector3 end(normal_sat_vec[normal_sat_vec.size() - 3], normal_sat_vec[normal_sat_vec.size() - 2], normal_sat_vec[normal_sat_vec.size() - 1]); + Vector3 avg = (end - beg) / (normal_w * normal_h); + print_line("average: " + avg); + } +#endif + + int mmcount; + + _get_dst_image_size(width, height, format, mmcount); + + uint8_t *base_ptr = data.ptrw(); + + for (int i = 1; i <= mmcount; i++) { + int ofs, w, h; + _get_mipmap_offset_and_size(i, ofs, w, h); + uint8_t *ptr = &base_ptr[ofs]; + + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + int from_x = x * normal_w / w; + int from_y = y * normal_h / h; + int to_x = (x + 1) * normal_w / w; + int to_y = (y + 1) * normal_h / h; + to_x = MIN(to_x - 1, normal_w); + to_y = MIN(to_y - 1, normal_h); + + int size_x = (to_x - from_x) + 1; + int size_y = (to_y - from_y) + 1; + + //summed area table version (much faster) + + double avg[3] = { 0, 0, 0 }; + + if (from_x > 0 && from_y > 0) { + uint32_t tofs = ((from_y - 1) * normal_w + (from_x - 1)) * 3; + avg[0] += normal_sat[tofs + 0]; + avg[1] += normal_sat[tofs + 1]; + avg[2] += normal_sat[tofs + 2]; + } + + if (from_y > 0) { + uint32_t tofs = ((from_y - 1) * normal_w + to_x) * 3; + avg[0] -= normal_sat[tofs + 0]; + avg[1] -= normal_sat[tofs + 1]; + avg[2] -= normal_sat[tofs + 2]; + } + + if (from_x > 0) { + uint32_t tofs = (to_y * normal_w + (from_x - 1)) * 3; + avg[0] -= normal_sat[tofs + 0]; + avg[1] -= normal_sat[tofs + 1]; + avg[2] -= normal_sat[tofs + 2]; + } + + uint32_t tofs = (to_y * normal_w + to_x) * 3; + avg[0] += normal_sat[tofs + 0]; + avg[1] += normal_sat[tofs + 1]; + avg[2] += normal_sat[tofs + 2]; + + double div = double(size_x * size_y); + Vector3 vec(avg[0] / div, avg[1] / div, avg[2] / div); + + float r = vec.length(); + + int pixel_ofs = y * w + x; + Color c = _get_color_at_ofs(ptr, pixel_ofs); + + float roughness = 0; + + switch (p_roughness_channel) { + case ROUGHNESS_CHANNEL_R: { + roughness = c.r; + } break; + case ROUGHNESS_CHANNEL_G: { + roughness = c.g; + } break; + case ROUGHNESS_CHANNEL_B: { + roughness = c.b; + } break; + case ROUGHNESS_CHANNEL_L: { + roughness = c.get_v(); + } break; + case ROUGHNESS_CHANNEL_A: { + roughness = c.a; + } break; + } + + float variance = 0; + if (r < 1.0f) { + float r2 = r * r; + float kappa = (3.0f * r - r * r2) / (1.0f - r2); + variance = 0.25f / kappa; + } + + float threshold = 0.4; + roughness = Math::sqrt(roughness * roughness + MIN(3.0f * variance, threshold * threshold)); + + switch (p_roughness_channel) { + case ROUGHNESS_CHANNEL_R: { + c.r = roughness; + } break; + case ROUGHNESS_CHANNEL_G: { + c.g = roughness; + } break; + case ROUGHNESS_CHANNEL_B: { + c.b = roughness; + } break; + case ROUGHNESS_CHANNEL_L: { + c.r = roughness; + c.g = roughness; + c.b = roughness; + } break; + case ROUGHNESS_CHANNEL_A: { + c.a = roughness; + } break; + } + + _set_color_at_ofs(ptr, pixel_ofs, c); + } + } +#if 0 + { + int size = get_mipmap_byte_size(i); + print_line("size for mimpap " + itos(i) + ": " + itos(size)); + Vector<uint8_t> imgdata; + imgdata.resize(size); + + + uint8_t* wr = imgdata.ptrw(); + copymem(wr.ptr(), ptr, size); + wr = uint8_t*(); + Ref<Image> im; + im.instance(); + im->create(w, h, false, format, imgdata); + im->save_png("res://mipmap_" + itos(i) + ".png"); + } +#endif + } + + return OK; +} + +void Image::clear_mipmaps() { + if (!mipmaps) { + return; + } + + if (empty()) { + return; + } + + int ofs, w, h; + _get_mipmap_offset_and_size(1, ofs, w, h); + data.resize(ofs); + + mipmaps = false; +} + +bool Image::empty() const { + return (data.size() == 0); +} + +Vector<uint8_t> Image::get_data() const { + return data; +} + +void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { + ERR_FAIL_COND_MSG(p_width <= 0, "Image width must be greater than 0."); + ERR_FAIL_COND_MSG(p_height <= 0, "Image height must be greater than 0."); + ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, "Image width cannot be greater than " + itos(MAX_WIDTH) + "."); + ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT, "Image height cannot be greater than " + itos(MAX_HEIGHT) + "."); + ERR_FAIL_COND_MSG(p_width * p_height > MAX_PIXELS, "Too many pixels for image, maximum is " + itos(MAX_PIXELS)); + + int mm = 0; + int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0); + data.resize(size); + + { + uint8_t *w = data.ptrw(); + zeromem(w, size); + } + + width = p_width; + height = p_height; + mipmaps = p_use_mipmaps; + format = p_format; +} + +void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + ERR_FAIL_COND_MSG(p_width <= 0, "Image width must be greater than 0."); + ERR_FAIL_COND_MSG(p_height <= 0, "Image height must be greater than 0."); + ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, "Image width cannot be greater than " + itos(MAX_WIDTH) + "."); + ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT, "Image height cannot be greater than " + itos(MAX_HEIGHT) + "."); + ERR_FAIL_COND_MSG(p_width * p_height > MAX_PIXELS, "Too many pixels for image, maximum is " + itos(MAX_PIXELS)); + + int mm; + int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0); + + ERR_FAIL_COND_MSG(p_data.size() != size, "Expected data size of " + itos(size) + " bytes in Image::create(), got instead " + itos(p_data.size()) + " bytes."); + + height = p_height; + width = p_width; + format = p_format; + data = p_data; + + mipmaps = p_use_mipmaps; +} + +void Image::create(const char **p_xpm) { + int size_width = 0; + int size_height = 0; + int pixelchars = 0; + mipmaps = false; + bool has_alpha = false; + + enum Status { + READING_HEADER, + READING_COLORS, + READING_PIXELS, + DONE + }; + + Status status = READING_HEADER; + int line = 0; + + HashMap<String, Color> colormap; + int colormap_size = 0; + uint32_t pixel_size = 0; + uint8_t *data_write = nullptr; + + while (status != DONE) { + const char *line_ptr = p_xpm[line]; + + switch (status) { + case READING_HEADER: { + String line_str = line_ptr; + line_str.replace("\t", " "); + + size_width = line_str.get_slicec(' ', 0).to_int(); + size_height = line_str.get_slicec(' ', 1).to_int(); + colormap_size = line_str.get_slicec(' ', 2).to_int(); + pixelchars = line_str.get_slicec(' ', 3).to_int(); + ERR_FAIL_COND(colormap_size > 32766); + ERR_FAIL_COND(pixelchars > 5); + ERR_FAIL_COND(size_width > 32767); + ERR_FAIL_COND(size_height > 32767); + status = READING_COLORS; + } break; + case READING_COLORS: { + String colorstring; + for (int i = 0; i < pixelchars; i++) { + colorstring += *line_ptr; + line_ptr++; + } + //skip spaces + while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) { + if (*line_ptr == 0) { + break; + } + line_ptr++; + } + if (*line_ptr == 'c') { + line_ptr++; + while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) { + if (*line_ptr == 0) { + break; + } + line_ptr++; + } + + if (*line_ptr == '#') { + line_ptr++; + uint8_t col_r = 0; + uint8_t col_g = 0; + uint8_t col_b = 0; + //uint8_t col_a=255; + + for (int i = 0; i < 6; i++) { + char v = line_ptr[i]; + + if (v >= '0' && v <= '9') { + v -= '0'; + } else if (v >= 'A' && v <= 'F') { + v = (v - 'A') + 10; + } else if (v >= 'a' && v <= 'f') { + v = (v - 'a') + 10; + } else { + break; + } + + switch (i) { + case 0: + col_r = v << 4; + break; + case 1: + col_r |= v; + break; + case 2: + col_g = v << 4; + break; + case 3: + col_g |= v; + break; + case 4: + col_b = v << 4; + break; + case 5: + col_b |= v; + break; + } + } + + // magenta mask + if (col_r == 255 && col_g == 0 && col_b == 255) { + colormap[colorstring] = Color(0, 0, 0, 0); + has_alpha = true; + } else { + colormap[colorstring] = Color(col_r / 255.0, col_g / 255.0, col_b / 255.0, 1.0); + } + } + } + if (line == colormap_size) { + status = READING_PIXELS; + create(size_width, size_height, false, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8); + data_write = data.ptrw(); + pixel_size = has_alpha ? 4 : 3; + } + } break; + case READING_PIXELS: { + int y = line - colormap_size - 1; + for (int x = 0; x < size_width; x++) { + char pixelstr[6] = { 0, 0, 0, 0, 0, 0 }; + for (int i = 0; i < pixelchars; i++) { + pixelstr[i] = line_ptr[x * pixelchars + i]; + } + + Color *colorptr = colormap.getptr(pixelstr); + ERR_FAIL_COND(!colorptr); + uint8_t pixel[4]; + for (uint32_t i = 0; i < pixel_size; i++) { + pixel[i] = CLAMP((*colorptr)[i] * 255, 0, 255); + } + _put_pixelb(x, y, pixel_size, data_write, pixel); + } + + if (y == (size_height - 1)) { + status = DONE; + } + } break; + default: { + } + } + + line++; + } +} +#define DETECT_ALPHA_MAX_THRESHOLD 254 +#define DETECT_ALPHA_MIN_THRESHOLD 2 + +#define DETECT_ALPHA(m_value) \ + { \ + uint8_t value = m_value; \ + if (value < DETECT_ALPHA_MIN_THRESHOLD) \ + bit = true; \ + else if (value < DETECT_ALPHA_MAX_THRESHOLD) { \ + detected = true; \ + break; \ + } \ + } + +#define DETECT_NON_ALPHA(m_value) \ + { \ + uint8_t value = m_value; \ + if (value > 0) { \ + detected = true; \ + break; \ + } \ + } + +bool Image::is_invisible() const { + if (format == FORMAT_L8 || + format == FORMAT_RGB8 || format == FORMAT_RG8) { + return false; + } + + int len = data.size(); + + if (len == 0) { + return true; + } + + int w, h; + _get_mipmap_offset_and_size(1, len, w, h); + + const uint8_t *r = data.ptr(); + const unsigned char *data_ptr = r; + + bool detected = false; + + switch (format) { + case FORMAT_LA8: { + for (int i = 0; i < (len >> 1); i++) { + DETECT_NON_ALPHA(data_ptr[(i << 1) + 1]); + } + + } break; + case FORMAT_RGBA8: { + for (int i = 0; i < (len >> 2); i++) { + DETECT_NON_ALPHA(data_ptr[(i << 2) + 3]) + } + + } break; + + case FORMAT_PVRTC1_2A: + case FORMAT_PVRTC1_4A: + case FORMAT_DXT3: + case FORMAT_DXT5: { + detected = true; + } break; + default: { + } + } + + return !detected; +} + +Image::AlphaMode Image::detect_alpha() const { + int len = data.size(); + + if (len == 0) { + return ALPHA_NONE; + } + + int w, h; + _get_mipmap_offset_and_size(1, len, w, h); + + const uint8_t *r = data.ptr(); + const unsigned char *data_ptr = r; + + bool bit = false; + bool detected = false; + + switch (format) { + case FORMAT_LA8: { + for (int i = 0; i < (len >> 1); i++) { + DETECT_ALPHA(data_ptr[(i << 1) + 1]); + } + + } break; + case FORMAT_RGBA8: { + for (int i = 0; i < (len >> 2); i++) { + DETECT_ALPHA(data_ptr[(i << 2) + 3]) + } + + } break; + case FORMAT_PVRTC1_2A: + case FORMAT_PVRTC1_4A: + case FORMAT_DXT3: + case FORMAT_DXT5: { + detected = true; + } break; + default: { + } + } + + if (detected) { + return ALPHA_BLEND; + } else if (bit) { + return ALPHA_BIT; + } else { + return ALPHA_NONE; + } +} + +Error Image::load(const String &p_path) { +#ifdef DEBUG_ENABLED + if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) { + WARN_PRINT("Loaded resource as image file, this will not work on export: '" + p_path + "'. Instead, import the image file as an Image resource and load it normally as a resource."); + } +#endif + return ImageLoader::load_image(p_path, this); +} + +Error Image::save_png(const String &p_path) const { + if (save_png_func == nullptr) { + return ERR_UNAVAILABLE; + } + + return save_png_func(p_path, Ref<Image>((Image *)this)); +} + +Vector<uint8_t> Image::save_png_to_buffer() const { + if (save_png_buffer_func == nullptr) { + return Vector<uint8_t>(); + } + + return save_png_buffer_func(Ref<Image>((Image *)this)); +} + +Error Image::save_exr(const String &p_path, bool p_grayscale) const { + if (save_exr_func == nullptr) { + return ERR_UNAVAILABLE; + } + + return save_exr_func(p_path, Ref<Image>((Image *)this), p_grayscale); +} + +int Image::get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps) { + int mm; + return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps ? -1 : 0); +} + +int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) { + int mm; + _get_dst_image_size(p_width, p_height, p_format, mm, -1); + return mm; +} + +Size2i Image::get_image_mipmap_size(int p_width, int p_height, Format p_format, int p_mipmap) { + int mm; + Size2i ret; + _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap, &ret.x, &ret.y); + return ret; +} + +int Image::get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap) { + if (p_mipmap <= 0) { + return 0; + } + int mm; + return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap - 1); +} + +int Image::get_image_mipmap_offset_and_dimensions(int p_width, int p_height, Format p_format, int p_mipmap, int &r_w, int &r_h) { + if (p_mipmap <= 0) { + r_w = p_width; + r_h = p_height; + return 0; + } + int mm; + return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap - 1, &r_w, &r_h); +} + +bool Image::is_compressed() const { + return format > FORMAT_RGBE9995; +} + +Error Image::decompress() { + if (((format >= FORMAT_DXT1 && format <= FORMAT_RGTC_RG) || (format == FORMAT_DXT5_RA_AS_RG)) && _image_decompress_bc) { + _image_decompress_bc(this); + } else if (format >= FORMAT_BPTC_RGBA && format <= FORMAT_BPTC_RGBFU && _image_decompress_bptc) { + _image_decompress_bptc(this); + } else if (format >= FORMAT_PVRTC1_2 && format <= FORMAT_PVRTC1_4A && _image_decompress_pvrtc) { + _image_decompress_pvrtc(this); + } else if (format == FORMAT_ETC && _image_decompress_etc1) { + _image_decompress_etc1(this); + } else if (format >= FORMAT_ETC2_R11 && format <= FORMAT_ETC2_RA_AS_RG && _image_decompress_etc2) { + _image_decompress_etc2(this); + } else { + return ERR_UNAVAILABLE; + } + return OK; +} + +Error Image::compress(CompressMode p_mode, CompressSource p_source, float p_lossy_quality) { + return compress_from_channels(p_mode, detect_used_channels(p_source), p_lossy_quality); +} + +Error Image::compress_from_channels(CompressMode p_mode, UsedChannels p_channels, float p_lossy_quality) { + switch (p_mode) { + case COMPRESS_S3TC: { + ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE); + _image_compress_bc_func(this, p_lossy_quality, p_channels); + } break; + case COMPRESS_PVRTC1_4: { + ERR_FAIL_COND_V(!_image_compress_pvrtc1_4bpp_func, ERR_UNAVAILABLE); + _image_compress_pvrtc1_4bpp_func(this); + } break; + case COMPRESS_ETC: { + ERR_FAIL_COND_V(!_image_compress_etc1_func, ERR_UNAVAILABLE); + _image_compress_etc1_func(this, p_lossy_quality); + } break; + case COMPRESS_ETC2: { + ERR_FAIL_COND_V(!_image_compress_etc2_func, ERR_UNAVAILABLE); + _image_compress_etc2_func(this, p_lossy_quality, p_channels); + } break; + case COMPRESS_BPTC: { + ERR_FAIL_COND_V(!_image_compress_bptc_func, ERR_UNAVAILABLE); + _image_compress_bptc_func(this, p_lossy_quality, p_channels); + } break; + } + + return OK; +} + +Image::Image(const char **p_xpm) { + width = 0; + height = 0; + mipmaps = false; + format = FORMAT_L8; + + create(p_xpm); +} + +Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { + width = 0; + height = 0; + mipmaps = p_use_mipmaps; + format = FORMAT_L8; + + create(p_width, p_height, p_use_mipmaps, p_format); +} + +Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + width = 0; + height = 0; + mipmaps = p_mipmaps; + format = FORMAT_L8; + + create(p_width, p_height, p_mipmaps, p_format, p_data); +} + +Rect2 Image::get_used_rect() const { + if (format != FORMAT_LA8 && format != FORMAT_RGBA8 && format != FORMAT_RGBAF && format != FORMAT_RGBAH && format != FORMAT_RGBA4444 && format != FORMAT_RGB565) { + return Rect2(Point2(), Size2(width, height)); + } + + int len = data.size(); + + if (len == 0) { + return Rect2(); + } + + int minx = 0xFFFFFF, miny = 0xFFFFFFF; + int maxx = -1, maxy = -1; + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + if (!(get_pixel(i, j).a > 0)) { + continue; + } + if (i > maxx) { + maxx = i; + } + if (j > maxy) { + maxy = j; + } + if (i < minx) { + minx = i; + } + if (j < miny) { + miny = j; + } + } + } + + if (maxx == -1) { + return Rect2(); + } else { + return Rect2(minx, miny, maxx - minx + 1, maxy - miny + 1); + } +} + +Ref<Image> Image::get_rect(const Rect2 &p_area) const { + Ref<Image> img = memnew(Image(p_area.size.x, p_area.size.y, mipmaps, format)); + img->blit_rect(Ref<Image>((Image *)this), p_area, Point2(0, 0)); + return img; +} + +void Image::blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) { + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); + int dsize = data.size(); + int srcdsize = p_src->data.size(); + ERR_FAIL_COND(dsize == 0); + ERR_FAIL_COND(srcdsize == 0); + ERR_FAIL_COND(format != p_src->format); + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot blit_rect in compressed or custom image formats."); + + Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect); + + if (p_dest.x < 0) { + clipped_src_rect.position.x = ABS(p_dest.x); + } + if (p_dest.y < 0) { + clipped_src_rect.position.y = ABS(p_dest.y); + } + + if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0) { + return; + } + + Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y)); + Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size)); + + uint8_t *wp = data.ptrw(); + uint8_t *dst_data_ptr = wp; + + const uint8_t *rp = p_src->data.ptr(); + const uint8_t *src_data_ptr = rp; + + int pixel_size = get_format_pixel_size(format); + + for (int i = 0; i < dest_rect.size.y; i++) { + for (int j = 0; j < dest_rect.size.x; j++) { + int src_x = clipped_src_rect.position.x + j; + int src_y = clipped_src_rect.position.y + i; + + int dst_x = dest_rect.position.x + j; + int dst_y = dest_rect.position.y + i; + + const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size]; + uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size]; + + for (int k = 0; k < pixel_size; k++) { + dst[k] = src[k]; + } + } + } +} + +void Image::blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) { + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); + ERR_FAIL_COND_MSG(p_mask.is_null(), "It's not a reference to a valid Image object."); + int dsize = data.size(); + int srcdsize = p_src->data.size(); + int maskdsize = p_mask->data.size(); + ERR_FAIL_COND(dsize == 0); + ERR_FAIL_COND(srcdsize == 0); + ERR_FAIL_COND(maskdsize == 0); + ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width."); + ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height."); + ERR_FAIL_COND(format != p_src->format); + + Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect); + + if (p_dest.x < 0) { + clipped_src_rect.position.x = ABS(p_dest.x); + } + if (p_dest.y < 0) { + clipped_src_rect.position.y = ABS(p_dest.y); + } + + if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0) { + return; + } + + Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y)); + Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size)); + + uint8_t *wp = data.ptrw(); + uint8_t *dst_data_ptr = wp; + + const uint8_t *rp = p_src->data.ptr(); + const uint8_t *src_data_ptr = rp; + + int pixel_size = get_format_pixel_size(format); + + Ref<Image> msk = p_mask; + + for (int i = 0; i < dest_rect.size.y; i++) { + for (int j = 0; j < dest_rect.size.x; j++) { + int src_x = clipped_src_rect.position.x + j; + int src_y = clipped_src_rect.position.y + i; + + if (msk->get_pixel(src_x, src_y).a != 0) { + int dst_x = dest_rect.position.x + j; + int dst_y = dest_rect.position.y + i; + + const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size]; + uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size]; + + for (int k = 0; k < pixel_size; k++) { + dst[k] = src[k]; + } + } + } + } +} + +void Image::blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) { + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); + int dsize = data.size(); + int srcdsize = p_src->data.size(); + ERR_FAIL_COND(dsize == 0); + ERR_FAIL_COND(srcdsize == 0); + ERR_FAIL_COND(format != p_src->format); + + Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect); + + if (p_dest.x < 0) { + clipped_src_rect.position.x = ABS(p_dest.x); + } + if (p_dest.y < 0) { + clipped_src_rect.position.y = ABS(p_dest.y); + } + + if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0) { + return; + } + + Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y)); + Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size)); + + Ref<Image> img = p_src; + + for (int i = 0; i < dest_rect.size.y; i++) { + for (int j = 0; j < dest_rect.size.x; j++) { + int src_x = clipped_src_rect.position.x + j; + int src_y = clipped_src_rect.position.y + i; + + int dst_x = dest_rect.position.x + j; + int dst_y = dest_rect.position.y + i; + + Color sc = img->get_pixel(src_x, src_y); + if (sc.a != 0) { + Color dc = get_pixel(dst_x, dst_y); + dc = dc.blend(sc); + set_pixel(dst_x, dst_y, dc); + } + } + } +} + +void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) { + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); + ERR_FAIL_COND_MSG(p_mask.is_null(), "It's not a reference to a valid Image object."); + int dsize = data.size(); + int srcdsize = p_src->data.size(); + int maskdsize = p_mask->data.size(); + ERR_FAIL_COND(dsize == 0); + ERR_FAIL_COND(srcdsize == 0); + ERR_FAIL_COND(maskdsize == 0); + ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width."); + ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height."); + ERR_FAIL_COND(format != p_src->format); + + Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect); + + if (p_dest.x < 0) { + clipped_src_rect.position.x = ABS(p_dest.x); + } + if (p_dest.y < 0) { + clipped_src_rect.position.y = ABS(p_dest.y); + } + + if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0) { + return; + } + + Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y)); + Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size)); + + Ref<Image> img = p_src; + Ref<Image> msk = p_mask; + + for (int i = 0; i < dest_rect.size.y; i++) { + for (int j = 0; j < dest_rect.size.x; j++) { + int src_x = clipped_src_rect.position.x + j; + int src_y = clipped_src_rect.position.y + i; + + // If the mask's pixel is transparent then we skip it + //Color c = msk->get_pixel(src_x, src_y); + //if (c.a == 0) continue; + if (msk->get_pixel(src_x, src_y).a != 0) { + int dst_x = dest_rect.position.x + j; + int dst_y = dest_rect.position.y + i; + + Color sc = img->get_pixel(src_x, src_y); + if (sc.a != 0) { + Color dc = get_pixel(dst_x, dst_y); + dc = dc.blend(sc); + set_pixel(dst_x, dst_y, dc); + } + } + } + } +} + +void Image::fill(const Color &c) { + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill in compressed or custom image formats."); + + uint8_t *wp = data.ptrw(); + uint8_t *dst_data_ptr = wp; + + int pixel_size = get_format_pixel_size(format); + + // put first pixel with the format-aware API + set_pixel(0, 0, c); + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + uint8_t *dst = &dst_data_ptr[(y * width + x) * pixel_size]; + + for (int k = 0; k < pixel_size; k++) { + dst[k] = dst_data_ptr[k]; + } + } + } +} + +ImageMemLoadFunc Image::_png_mem_loader_func = nullptr; +ImageMemLoadFunc Image::_jpg_mem_loader_func = nullptr; +ImageMemLoadFunc Image::_webp_mem_loader_func = nullptr; +ImageMemLoadFunc Image::_tga_mem_loader_func = nullptr; +ImageMemLoadFunc Image::_bmp_mem_loader_func = nullptr; + +void (*Image::_image_compress_bc_func)(Image *, float, Image::UsedChannels) = nullptr; +void (*Image::_image_compress_bptc_func)(Image *, float, Image::UsedChannels) = nullptr; +void (*Image::_image_compress_pvrtc1_4bpp_func)(Image *) = nullptr; +void (*Image::_image_compress_etc1_func)(Image *, float) = nullptr; +void (*Image::_image_compress_etc2_func)(Image *, float, Image::UsedChannels) = nullptr; +void (*Image::_image_decompress_pvrtc)(Image *) = nullptr; +void (*Image::_image_decompress_bc)(Image *) = nullptr; +void (*Image::_image_decompress_bptc)(Image *) = nullptr; +void (*Image::_image_decompress_etc1)(Image *) = nullptr; +void (*Image::_image_decompress_etc2)(Image *) = nullptr; + +Vector<uint8_t> (*Image::lossy_packer)(const Ref<Image> &, float) = nullptr; +Ref<Image> (*Image::lossy_unpacker)(const Vector<uint8_t> &) = nullptr; +Vector<uint8_t> (*Image::lossless_packer)(const Ref<Image> &) = nullptr; +Ref<Image> (*Image::lossless_unpacker)(const Vector<uint8_t> &) = nullptr; +Vector<uint8_t> (*Image::basis_universal_packer)(const Ref<Image> &, Image::UsedChannels) = nullptr; +Ref<Image> (*Image::basis_universal_unpacker)(const Vector<uint8_t> &) = nullptr; + +void Image::_set_data(const Dictionary &p_data) { + ERR_FAIL_COND(!p_data.has("width")); + ERR_FAIL_COND(!p_data.has("height")); + ERR_FAIL_COND(!p_data.has("format")); + ERR_FAIL_COND(!p_data.has("mipmaps")); + ERR_FAIL_COND(!p_data.has("data")); + + int dwidth = p_data["width"]; + int dheight = p_data["height"]; + String dformat = p_data["format"]; + bool dmipmaps = p_data["mipmaps"]; + Vector<uint8_t> ddata = p_data["data"]; + Format ddformat = FORMAT_MAX; + for (int i = 0; i < FORMAT_MAX; i++) { + if (dformat == get_format_name(Format(i))) { + ddformat = Format(i); + break; + } + } + + ERR_FAIL_COND(ddformat == FORMAT_MAX); + + create(dwidth, dheight, dmipmaps, ddformat, ddata); +} + +Dictionary Image::_get_data() const { + Dictionary d; + d["width"] = width; + d["height"] = height; + d["format"] = get_format_name(format); + d["mipmaps"] = mipmaps; + d["data"] = data; + return d; +} + +Color Image::get_pixelv(const Point2i &p_point) const { + return get_pixel(p_point.x, p_point.y); +} + +Color Image::_get_color_at_ofs(const uint8_t *ptr, uint32_t ofs) const { + switch (format) { + case FORMAT_L8: { + float l = ptr[ofs] / 255.0; + return Color(l, l, l, 1); + } + case FORMAT_LA8: { + float l = ptr[ofs * 2 + 0] / 255.0; + float a = ptr[ofs * 2 + 1] / 255.0; + return Color(l, l, l, a); + } + case FORMAT_R8: { + float r = ptr[ofs] / 255.0; + return Color(r, 0, 0, 1); + } + case FORMAT_RG8: { + float r = ptr[ofs * 2 + 0] / 255.0; + float g = ptr[ofs * 2 + 1] / 255.0; + return Color(r, g, 0, 1); + } + case FORMAT_RGB8: { + float r = ptr[ofs * 3 + 0] / 255.0; + float g = ptr[ofs * 3 + 1] / 255.0; + float b = ptr[ofs * 3 + 2] / 255.0; + return Color(r, g, b, 1); + } + case FORMAT_RGBA8: { + float r = ptr[ofs * 4 + 0] / 255.0; + float g = ptr[ofs * 4 + 1] / 255.0; + float b = ptr[ofs * 4 + 2] / 255.0; + float a = ptr[ofs * 4 + 3] / 255.0; + return Color(r, g, b, a); + } + case FORMAT_RGBA4444: { + uint16_t u = ((uint16_t *)ptr)[ofs]; + float r = ((u >> 12) & 0xF) / 15.0; + float g = ((u >> 8) & 0xF) / 15.0; + float b = ((u >> 4) & 0xF) / 15.0; + float a = (u & 0xF) / 15.0; + return Color(r, g, b, a); + } + case FORMAT_RGB565: { + uint16_t u = ((uint16_t *)ptr)[ofs]; + float r = (u & 0x1F) / 31.0; + float g = ((u >> 5) & 0x3F) / 63.0; + float b = ((u >> 11) & 0x1F) / 31.0; + return Color(r, g, b, 1.0); + } + case FORMAT_RF: { + float r = ((float *)ptr)[ofs]; + return Color(r, 0, 0, 1); + } + case FORMAT_RGF: { + float r = ((float *)ptr)[ofs * 2 + 0]; + float g = ((float *)ptr)[ofs * 2 + 1]; + return Color(r, g, 0, 1); + } + case FORMAT_RGBF: { + float r = ((float *)ptr)[ofs * 3 + 0]; + float g = ((float *)ptr)[ofs * 3 + 1]; + float b = ((float *)ptr)[ofs * 3 + 2]; + return Color(r, g, b, 1); + } + case FORMAT_RGBAF: { + float r = ((float *)ptr)[ofs * 4 + 0]; + float g = ((float *)ptr)[ofs * 4 + 1]; + float b = ((float *)ptr)[ofs * 4 + 2]; + float a = ((float *)ptr)[ofs * 4 + 3]; + return Color(r, g, b, a); + } + case FORMAT_RH: { + uint16_t r = ((uint16_t *)ptr)[ofs]; + return Color(Math::half_to_float(r), 0, 0, 1); + } + case FORMAT_RGH: { + uint16_t r = ((uint16_t *)ptr)[ofs * 2 + 0]; + uint16_t g = ((uint16_t *)ptr)[ofs * 2 + 1]; + return Color(Math::half_to_float(r), Math::half_to_float(g), 0, 1); + } + case FORMAT_RGBH: { + uint16_t r = ((uint16_t *)ptr)[ofs * 3 + 0]; + uint16_t g = ((uint16_t *)ptr)[ofs * 3 + 1]; + uint16_t b = ((uint16_t *)ptr)[ofs * 3 + 2]; + return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), 1); + } + case FORMAT_RGBAH: { + uint16_t r = ((uint16_t *)ptr)[ofs * 4 + 0]; + uint16_t g = ((uint16_t *)ptr)[ofs * 4 + 1]; + uint16_t b = ((uint16_t *)ptr)[ofs * 4 + 2]; + uint16_t a = ((uint16_t *)ptr)[ofs * 4 + 3]; + return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), Math::half_to_float(a)); + } + case FORMAT_RGBE9995: { + return Color::from_rgbe9995(((uint32_t *)ptr)[ofs]); + } + default: { + ERR_FAIL_V_MSG(Color(), "Can't get_pixel() on compressed image, sorry."); + } + } +} + +void Image::_set_color_at_ofs(uint8_t *ptr, uint32_t ofs, const Color &p_color) { + switch (format) { + case FORMAT_L8: { + ptr[ofs] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255)); + } break; + case FORMAT_LA8: { + ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255)); + ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255)); + } break; + case FORMAT_R8: { + ptr[ofs] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255)); + } break; + case FORMAT_RG8: { + ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255)); + ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255)); + } break; + case FORMAT_RGB8: { + ptr[ofs * 3 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255)); + ptr[ofs * 3 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255)); + ptr[ofs * 3 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255)); + } break; + case FORMAT_RGBA8: { + ptr[ofs * 4 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255)); + ptr[ofs * 4 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255)); + ptr[ofs * 4 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255)); + ptr[ofs * 4 + 3] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255)); + + } break; + case FORMAT_RGBA4444: { + uint16_t rgba = 0; + + rgba = uint16_t(CLAMP(p_color.r * 15.0, 0, 15)) << 12; + rgba |= uint16_t(CLAMP(p_color.g * 15.0, 0, 15)) << 8; + rgba |= uint16_t(CLAMP(p_color.b * 15.0, 0, 15)) << 4; + rgba |= uint16_t(CLAMP(p_color.a * 15.0, 0, 15)); + + ((uint16_t *)ptr)[ofs] = rgba; + + } break; + case FORMAT_RGB565: { + uint16_t rgba = 0; + + rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31)); + rgba |= uint16_t(CLAMP(p_color.g * 63.0, 0, 33)) << 5; + rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31)) << 11; + + ((uint16_t *)ptr)[ofs] = rgba; + + } break; + case FORMAT_RF: { + ((float *)ptr)[ofs] = p_color.r; + } break; + case FORMAT_RGF: { + ((float *)ptr)[ofs * 2 + 0] = p_color.r; + ((float *)ptr)[ofs * 2 + 1] = p_color.g; + } break; + case FORMAT_RGBF: { + ((float *)ptr)[ofs * 3 + 0] = p_color.r; + ((float *)ptr)[ofs * 3 + 1] = p_color.g; + ((float *)ptr)[ofs * 3 + 2] = p_color.b; + } break; + case FORMAT_RGBAF: { + ((float *)ptr)[ofs * 4 + 0] = p_color.r; + ((float *)ptr)[ofs * 4 + 1] = p_color.g; + ((float *)ptr)[ofs * 4 + 2] = p_color.b; + ((float *)ptr)[ofs * 4 + 3] = p_color.a; + } break; + case FORMAT_RH: { + ((uint16_t *)ptr)[ofs] = Math::make_half_float(p_color.r); + } break; + case FORMAT_RGH: { + ((uint16_t *)ptr)[ofs * 2 + 0] = Math::make_half_float(p_color.r); + ((uint16_t *)ptr)[ofs * 2 + 1] = Math::make_half_float(p_color.g); + } break; + case FORMAT_RGBH: { + ((uint16_t *)ptr)[ofs * 3 + 0] = Math::make_half_float(p_color.r); + ((uint16_t *)ptr)[ofs * 3 + 1] = Math::make_half_float(p_color.g); + ((uint16_t *)ptr)[ofs * 3 + 2] = Math::make_half_float(p_color.b); + } break; + case FORMAT_RGBAH: { + ((uint16_t *)ptr)[ofs * 4 + 0] = Math::make_half_float(p_color.r); + ((uint16_t *)ptr)[ofs * 4 + 1] = Math::make_half_float(p_color.g); + ((uint16_t *)ptr)[ofs * 4 + 2] = Math::make_half_float(p_color.b); + ((uint16_t *)ptr)[ofs * 4 + 3] = Math::make_half_float(p_color.a); + } break; + case FORMAT_RGBE9995: { + ((uint32_t *)ptr)[ofs] = p_color.to_rgbe9995(); + + } break; + default: { + ERR_FAIL_MSG("Can't set_pixel() on compressed image, sorry."); + } + } +} + +Color Image::get_pixel(int p_x, int p_y) const { +#ifdef DEBUG_ENABLED + ERR_FAIL_INDEX_V(p_x, width, Color()); + ERR_FAIL_INDEX_V(p_y, height, Color()); +#endif + + uint32_t ofs = p_y * width + p_x; + return _get_color_at_ofs(data.ptr(), ofs); +} + +void Image::set_pixelv(const Point2i &p_point, const Color &p_color) { + set_pixel(p_point.x, p_point.y, p_color); +} + +void Image::set_pixel(int p_x, int p_y, const Color &p_color) { +#ifdef DEBUG_ENABLED + ERR_FAIL_INDEX(p_x, width); + ERR_FAIL_INDEX(p_y, height); +#endif + + uint32_t ofs = p_y * width + p_x; + _set_color_at_ofs(data.ptrw(), ofs, p_color); +} + +Image::UsedChannels Image::detect_used_channels(CompressSource p_source) { + ERR_FAIL_COND_V(data.size() == 0, USED_CHANNELS_RGBA); + ERR_FAIL_COND_V(is_compressed(), USED_CHANNELS_RGBA); + bool r = false, g = false, b = false, a = false, c = false; + + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + Color col = get_pixel(i, j); + + if (col.r > 0.001) { + r = true; + } + if (col.g > 0.001) { + g = true; + } + if (col.b > 0.001) { + b = true; + } + if (col.a < 0.999) { + a = true; + } + + if (col.r != col.b || col.r != col.g || col.b != col.g) { + c = true; + } + } + } + + UsedChannels used_channels; + + if (!c && !a) { + used_channels = USED_CHANNELS_L; + } else if (!c && a) { + used_channels = USED_CHANNELS_LA; + } else if (r && !g && !b && !a) { + used_channels = USED_CHANNELS_R; + } else if (r && g && !b && !a) { + used_channels = USED_CHANNELS_RG; + } else if (r && g && b && !a) { + used_channels = USED_CHANNELS_RGB; + } else { + used_channels = USED_CHANNELS_RGBA; + } + + if (p_source == COMPRESS_SOURCE_SRGB && (used_channels == USED_CHANNELS_R || used_channels == USED_CHANNELS_RG)) { + //R and RG do not support SRGB + used_channels = USED_CHANNELS_RGB; + } + + if (p_source == COMPRESS_SOURCE_NORMAL) { + //use RG channels only for normal + used_channels = USED_CHANNELS_RG; + } + + return used_channels; +} + +void Image::optimize_channels() { + switch (detect_used_channels()) { + case USED_CHANNELS_L: + convert(FORMAT_L8); + break; + case USED_CHANNELS_LA: + convert(FORMAT_LA8); + break; + case USED_CHANNELS_R: + convert(FORMAT_R8); + break; + case USED_CHANNELS_RG: + convert(FORMAT_RG8); + break; + case USED_CHANNELS_RGB: + convert(FORMAT_RGB8); + break; + case USED_CHANNELS_RGBA: + convert(FORMAT_RGBA8); + break; + } +} + +void Image::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_width"), &Image::get_width); + ClassDB::bind_method(D_METHOD("get_height"), &Image::get_height); + ClassDB::bind_method(D_METHOD("get_size"), &Image::get_size); + ClassDB::bind_method(D_METHOD("has_mipmaps"), &Image::has_mipmaps); + ClassDB::bind_method(D_METHOD("get_format"), &Image::get_format); + ClassDB::bind_method(D_METHOD("get_data"), &Image::get_data); + + ClassDB::bind_method(D_METHOD("convert", "format"), &Image::convert); + + ClassDB::bind_method(D_METHOD("get_mipmap_offset", "mipmap"), &Image::get_mipmap_offset); + + ClassDB::bind_method(D_METHOD("resize_to_po2", "square", "interpolation"), &Image::resize_to_po2, DEFVAL(false), DEFVAL(INTERPOLATE_BILINEAR)); + ClassDB::bind_method(D_METHOD("resize", "width", "height", "interpolation"), &Image::resize, DEFVAL(INTERPOLATE_BILINEAR)); + ClassDB::bind_method(D_METHOD("shrink_x2"), &Image::shrink_x2); + + ClassDB::bind_method(D_METHOD("crop", "width", "height"), &Image::crop); + ClassDB::bind_method(D_METHOD("flip_x"), &Image::flip_x); + ClassDB::bind_method(D_METHOD("flip_y"), &Image::flip_y); + ClassDB::bind_method(D_METHOD("generate_mipmaps", "renormalize"), &Image::generate_mipmaps, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("clear_mipmaps"), &Image::clear_mipmaps); + + ClassDB::bind_method(D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::_create_empty); + ClassDB::bind_method(D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::_create_from_data); + + ClassDB::bind_method(D_METHOD("is_empty"), &Image::empty); + + ClassDB::bind_method(D_METHOD("load", "path"), &Image::load); + ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png); + ClassDB::bind_method(D_METHOD("save_png_to_buffer"), &Image::save_png_to_buffer); + ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale"), &Image::save_exr, DEFVAL(false)); + + ClassDB::bind_method(D_METHOD("detect_alpha"), &Image::detect_alpha); + ClassDB::bind_method(D_METHOD("is_invisible"), &Image::is_invisible); + + ClassDB::bind_method(D_METHOD("detect_used_channels", "source"), &Image::detect_used_channels, DEFVAL(COMPRESS_SOURCE_GENERIC)); + ClassDB::bind_method(D_METHOD("compress", "mode", "source", "lossy_quality"), &Image::compress, DEFVAL(COMPRESS_SOURCE_GENERIC), DEFVAL(0.7)); + ClassDB::bind_method(D_METHOD("compress_from_channels", "mode", "channels", "lossy_quality"), &Image::compress_from_channels, DEFVAL(0.7)); + ClassDB::bind_method(D_METHOD("decompress"), &Image::decompress); + ClassDB::bind_method(D_METHOD("is_compressed"), &Image::is_compressed); + + ClassDB::bind_method(D_METHOD("fix_alpha_edges"), &Image::fix_alpha_edges); + ClassDB::bind_method(D_METHOD("premultiply_alpha"), &Image::premultiply_alpha); + ClassDB::bind_method(D_METHOD("srgb_to_linear"), &Image::srgb_to_linear); + ClassDB::bind_method(D_METHOD("normalmap_to_xy"), &Image::normalmap_to_xy); + ClassDB::bind_method(D_METHOD("rgbe_to_srgb"), &Image::rgbe_to_srgb); + ClassDB::bind_method(D_METHOD("bumpmap_to_normalmap", "bump_scale"), &Image::bumpmap_to_normalmap, DEFVAL(1.0)); + + ClassDB::bind_method(D_METHOD("blit_rect", "src", "src_rect", "dst"), &Image::blit_rect); + ClassDB::bind_method(D_METHOD("blit_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blit_rect_mask); + ClassDB::bind_method(D_METHOD("blend_rect", "src", "src_rect", "dst"), &Image::blend_rect); + ClassDB::bind_method(D_METHOD("blend_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blend_rect_mask); + ClassDB::bind_method(D_METHOD("fill", "color"), &Image::fill); + + ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect); + ClassDB::bind_method(D_METHOD("get_rect", "rect"), &Image::get_rect); + + ClassDB::bind_method(D_METHOD("copy_from", "src"), &Image::copy_internals_from); + + ClassDB::bind_method(D_METHOD("_set_data", "data"), &Image::_set_data); + ClassDB::bind_method(D_METHOD("_get_data"), &Image::_get_data); + + ClassDB::bind_method(D_METHOD("get_pixelv", "point"), &Image::get_pixelv); + ClassDB::bind_method(D_METHOD("get_pixel", "x", "y"), &Image::get_pixel); + ClassDB::bind_method(D_METHOD("set_pixelv", "point", "color"), &Image::set_pixelv); + ClassDB::bind_method(D_METHOD("set_pixel", "x", "y", "color"), &Image::set_pixel); + + ClassDB::bind_method(D_METHOD("load_png_from_buffer", "buffer"), &Image::load_png_from_buffer); + ClassDB::bind_method(D_METHOD("load_jpg_from_buffer", "buffer"), &Image::load_jpg_from_buffer); + ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer"), &Image::load_webp_from_buffer); + ClassDB::bind_method(D_METHOD("load_tga_from_buffer", "buffer"), &Image::load_tga_from_buffer); + ClassDB::bind_method(D_METHOD("load_bmp_from_buffer", "buffer"), &Image::load_bmp_from_buffer); + + ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data"); + + BIND_CONSTANT(MAX_WIDTH); + BIND_CONSTANT(MAX_HEIGHT); + + BIND_ENUM_CONSTANT(FORMAT_L8); //luminance + BIND_ENUM_CONSTANT(FORMAT_LA8); //luminance-alpha + BIND_ENUM_CONSTANT(FORMAT_R8); + BIND_ENUM_CONSTANT(FORMAT_RG8); + BIND_ENUM_CONSTANT(FORMAT_RGB8); + BIND_ENUM_CONSTANT(FORMAT_RGBA8); + BIND_ENUM_CONSTANT(FORMAT_RGBA4444); + BIND_ENUM_CONSTANT(FORMAT_RGB565); + BIND_ENUM_CONSTANT(FORMAT_RF); //float + BIND_ENUM_CONSTANT(FORMAT_RGF); + BIND_ENUM_CONSTANT(FORMAT_RGBF); + BIND_ENUM_CONSTANT(FORMAT_RGBAF); + BIND_ENUM_CONSTANT(FORMAT_RH); //half float + BIND_ENUM_CONSTANT(FORMAT_RGH); + BIND_ENUM_CONSTANT(FORMAT_RGBH); + BIND_ENUM_CONSTANT(FORMAT_RGBAH); + BIND_ENUM_CONSTANT(FORMAT_RGBE9995); + BIND_ENUM_CONSTANT(FORMAT_DXT1); //s3tc bc1 + BIND_ENUM_CONSTANT(FORMAT_DXT3); //bc2 + BIND_ENUM_CONSTANT(FORMAT_DXT5); //bc3 + BIND_ENUM_CONSTANT(FORMAT_RGTC_R); + BIND_ENUM_CONSTANT(FORMAT_RGTC_RG); + BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBA); //btpc bc6h + BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBF); //float / + BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBFU); //unsigned float + BIND_ENUM_CONSTANT(FORMAT_PVRTC1_2); //pvrtc + BIND_ENUM_CONSTANT(FORMAT_PVRTC1_2A); + BIND_ENUM_CONSTANT(FORMAT_PVRTC1_4); + BIND_ENUM_CONSTANT(FORMAT_PVRTC1_4A); + BIND_ENUM_CONSTANT(FORMAT_ETC); //etc1 + BIND_ENUM_CONSTANT(FORMAT_ETC2_R11); //etc2 + BIND_ENUM_CONSTANT(FORMAT_ETC2_R11S); //signed ); NOT srgb. + BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11); + BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11S); + BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8); + BIND_ENUM_CONSTANT(FORMAT_ETC2_RGBA8); + BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8A1); + BIND_ENUM_CONSTANT(FORMAT_ETC2_RA_AS_RG); + BIND_ENUM_CONSTANT(FORMAT_DXT5_RA_AS_RG); + BIND_ENUM_CONSTANT(FORMAT_MAX); + + BIND_ENUM_CONSTANT(INTERPOLATE_NEAREST); + BIND_ENUM_CONSTANT(INTERPOLATE_BILINEAR); + BIND_ENUM_CONSTANT(INTERPOLATE_CUBIC); + BIND_ENUM_CONSTANT(INTERPOLATE_TRILINEAR); + BIND_ENUM_CONSTANT(INTERPOLATE_LANCZOS); + + BIND_ENUM_CONSTANT(ALPHA_NONE); + BIND_ENUM_CONSTANT(ALPHA_BIT); + BIND_ENUM_CONSTANT(ALPHA_BLEND); + + BIND_ENUM_CONSTANT(COMPRESS_S3TC); + BIND_ENUM_CONSTANT(COMPRESS_PVRTC1_4); + BIND_ENUM_CONSTANT(COMPRESS_ETC); + BIND_ENUM_CONSTANT(COMPRESS_ETC2); + BIND_ENUM_CONSTANT(COMPRESS_BPTC); + + BIND_ENUM_CONSTANT(USED_CHANNELS_L); + BIND_ENUM_CONSTANT(USED_CHANNELS_LA); + BIND_ENUM_CONSTANT(USED_CHANNELS_R); + BIND_ENUM_CONSTANT(USED_CHANNELS_RG); + BIND_ENUM_CONSTANT(USED_CHANNELS_RGB); + BIND_ENUM_CONSTANT(USED_CHANNELS_RGBA); + + BIND_ENUM_CONSTANT(COMPRESS_SOURCE_GENERIC); + BIND_ENUM_CONSTANT(COMPRESS_SOURCE_SRGB); + BIND_ENUM_CONSTANT(COMPRESS_SOURCE_NORMAL); +} + +void Image::set_compress_bc_func(void (*p_compress_func)(Image *, float, UsedChannels)) { + _image_compress_bc_func = p_compress_func; +} + +void Image::set_compress_bptc_func(void (*p_compress_func)(Image *, float, UsedChannels)) { + _image_compress_bptc_func = p_compress_func; +} + +void Image::normalmap_to_xy() { + convert(Image::FORMAT_RGBA8); + + { + int len = data.size() / 4; + uint8_t *data_ptr = data.ptrw(); + + for (int i = 0; i < len; i++) { + data_ptr[(i << 2) + 3] = data_ptr[(i << 2) + 0]; //x to w + data_ptr[(i << 2) + 0] = data_ptr[(i << 2) + 1]; //y to xz + data_ptr[(i << 2) + 2] = data_ptr[(i << 2) + 1]; + } + } + + convert(Image::FORMAT_LA8); +} + +Ref<Image> Image::rgbe_to_srgb() { + if (data.size() == 0) { + return Ref<Image>(); + } + + ERR_FAIL_COND_V(format != FORMAT_RGBE9995, Ref<Image>()); + + Ref<Image> new_image; + new_image.instance(); + new_image->create(width, height, false, Image::FORMAT_RGB8); + + for (int row = 0; row < height; row++) { + for (int col = 0; col < width; col++) { + new_image->set_pixel(col, row, get_pixel(col, row).to_srgb()); + } + } + + if (has_mipmaps()) { + new_image->generate_mipmaps(); + } + + return new_image; +} + +Ref<Image> Image::get_image_from_mipmap(int p_mipamp) const { + int ofs, size, w, h; + get_mipmap_offset_size_and_dimensions(p_mipamp, ofs, size, w, h); + + Vector<uint8_t> new_data; + new_data.resize(size); + + { + uint8_t *wr = new_data.ptrw(); + const uint8_t *rd = data.ptr(); + copymem(wr, rd + ofs, size); + } + + Ref<Image> image; + image.instance(); + image->width = w; + image->height = h; + image->format = format; + image->data = new_data; + + image->mipmaps = false; + return image; +} + +void Image::bumpmap_to_normalmap(float bump_scale) { + ERR_FAIL_COND(!_can_modify(format)); + convert(Image::FORMAT_RF); + + Vector<uint8_t> result_image; //rgba output + result_image.resize(width * height * 4); + + { + const uint8_t *rp = data.ptr(); + uint8_t *wp = result_image.ptrw(); + + ERR_FAIL_COND(!rp); + + unsigned char *write_ptr = wp; + float *read_ptr = (float *)rp; + + for (int ty = 0; ty < height; ty++) { + int py = ty + 1; + if (py >= height) { + py -= height; + } + + for (int tx = 0; tx < width; tx++) { + int px = tx + 1; + if (px >= width) { + px -= width; + } + float here = read_ptr[ty * width + tx]; + float to_right = read_ptr[ty * width + px]; + float above = read_ptr[py * width + tx]; + Vector3 up = Vector3(0, 1, (here - above) * bump_scale); + Vector3 across = Vector3(1, 0, (to_right - here) * bump_scale); + + Vector3 normal = across.cross(up); + normal.normalize(); + + write_ptr[((ty * width + tx) << 2) + 0] = (127.5 + normal.x * 127.5); + write_ptr[((ty * width + tx) << 2) + 1] = (127.5 + normal.y * 127.5); + write_ptr[((ty * width + tx) << 2) + 2] = (127.5 + normal.z * 127.5); + write_ptr[((ty * width + tx) << 2) + 3] = 255; + } + } + } + format = FORMAT_RGBA8; + data = result_image; +} + +void Image::srgb_to_linear() { + if (data.size() == 0) { + return; + } + + static const uint8_t srgb2lin[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 22, 22, 23, 23, 24, 24, 25, 26, 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 36, 37, 38, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 47, 48, 49, 50, 51, 52, 53, 54, 55, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 70, 71, 72, 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 85, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99, 101, 102, 103, 105, 106, 107, 109, 110, 112, 113, 114, 116, 117, 119, 120, 122, 123, 125, 126, 128, 129, 131, 132, 134, 135, 137, 139, 140, 142, 144, 145, 147, 148, 150, 152, 153, 155, 157, 159, 160, 162, 164, 166, 167, 169, 171, 173, 175, 176, 178, 180, 182, 184, 186, 188, 190, 192, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 218, 220, 222, 224, 226, 228, 230, 232, 235, 237, 239, 241, 243, 245, 248, 250, 252, 255 }; + + ERR_FAIL_COND(format != FORMAT_RGB8 && format != FORMAT_RGBA8); + + if (format == FORMAT_RGBA8) { + int len = data.size() / 4; + uint8_t *data_ptr = data.ptrw(); + + for (int i = 0; i < len; i++) { + data_ptr[(i << 2) + 0] = srgb2lin[data_ptr[(i << 2) + 0]]; + data_ptr[(i << 2) + 1] = srgb2lin[data_ptr[(i << 2) + 1]]; + data_ptr[(i << 2) + 2] = srgb2lin[data_ptr[(i << 2) + 2]]; + } + + } else if (format == FORMAT_RGB8) { + int len = data.size() / 3; + uint8_t *data_ptr = data.ptrw(); + + for (int i = 0; i < len; i++) { + data_ptr[(i * 3) + 0] = srgb2lin[data_ptr[(i * 3) + 0]]; + data_ptr[(i * 3) + 1] = srgb2lin[data_ptr[(i * 3) + 1]]; + data_ptr[(i * 3) + 2] = srgb2lin[data_ptr[(i * 3) + 2]]; + } + } +} + +void Image::premultiply_alpha() { + if (data.size() == 0) { + return; + } + + if (format != FORMAT_RGBA8) { + return; //not needed + } + + uint8_t *data_ptr = data.ptrw(); + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + uint8_t *ptr = &data_ptr[(i * width + j) * 4]; + + ptr[0] = (uint16_t(ptr[0]) * uint16_t(ptr[3])) >> 8; + ptr[1] = (uint16_t(ptr[1]) * uint16_t(ptr[3])) >> 8; + ptr[2] = (uint16_t(ptr[2]) * uint16_t(ptr[3])) >> 8; + } + } +} + +void Image::fix_alpha_edges() { + if (data.size() == 0) { + return; + } + + if (format != FORMAT_RGBA8) { + return; //not needed + } + + Vector<uint8_t> dcopy = data; + const uint8_t *srcptr = dcopy.ptr(); + + uint8_t *data_ptr = data.ptrw(); + + const int max_radius = 4; + const int alpha_threshold = 20; + const int max_dist = 0x7FFFFFFF; + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + const uint8_t *rptr = &srcptr[(i * width + j) * 4]; + uint8_t *wptr = &data_ptr[(i * width + j) * 4]; + + if (rptr[3] >= alpha_threshold) { + continue; + } + + int closest_dist = max_dist; + uint8_t closest_color[3]; + + int from_x = MAX(0, j - max_radius); + int to_x = MIN(width - 1, j + max_radius); + int from_y = MAX(0, i - max_radius); + int to_y = MIN(height - 1, i + max_radius); + + for (int k = from_y; k <= to_y; k++) { + for (int l = from_x; l <= to_x; l++) { + int dy = i - k; + int dx = j - l; + int dist = dy * dy + dx * dx; + if (dist >= closest_dist) { + continue; + } + + const uint8_t *rp2 = &srcptr[(k * width + l) << 2]; + + if (rp2[3] < alpha_threshold) { + continue; + } + + closest_dist = dist; + closest_color[0] = rp2[0]; + closest_color[1] = rp2[1]; + closest_color[2] = rp2[2]; + } + } + + if (closest_dist != max_dist) { + wptr[0] = closest_color[0]; + wptr[1] = closest_color[1]; + wptr[2] = closest_color[2]; + } + } + } +} + +String Image::get_format_name(Format p_format) { + ERR_FAIL_INDEX_V(p_format, FORMAT_MAX, String()); + return format_names[p_format]; +} + +Error Image::load_png_from_buffer(const Vector<uint8_t> &p_array) { + return _load_from_buffer(p_array, _png_mem_loader_func); +} + +Error Image::load_jpg_from_buffer(const Vector<uint8_t> &p_array) { + return _load_from_buffer(p_array, _jpg_mem_loader_func); +} + +Error Image::load_webp_from_buffer(const Vector<uint8_t> &p_array) { + return _load_from_buffer(p_array, _webp_mem_loader_func); +} + +Error Image::load_tga_from_buffer(const Vector<uint8_t> &p_array) { + ERR_FAIL_NULL_V_MSG( + _tga_mem_loader_func, + ERR_UNAVAILABLE, + "The TGA module isn't enabled. Recompile the Godot editor or export template binary with the `module_tga_enabled=yes` SCons option."); + return _load_from_buffer(p_array, _tga_mem_loader_func); +} + +Error Image::load_bmp_from_buffer(const Vector<uint8_t> &p_array) { + ERR_FAIL_NULL_V_MSG( + _bmp_mem_loader_func, + ERR_UNAVAILABLE, + "The BMP module isn't enabled. Recompile the Godot editor or export template binary with the `module_bmp_enabled=yes` SCons option."); + return _load_from_buffer(p_array, _bmp_mem_loader_func); +} + +void Image::convert_rg_to_ra_rgba8() { + ERR_FAIL_COND(format != FORMAT_RGBA8); + ERR_FAIL_COND(!data.size()); + + int s = data.size(); + uint8_t *w = data.ptrw(); + for (int i = 0; i < s; i += 4) { + w[i + 3] = w[i + 1]; + w[i + 1] = 0; + w[i + 2] = 0; + } +} + +void Image::convert_ra_rgba8_to_rg() { + ERR_FAIL_COND(format != FORMAT_RGBA8); + ERR_FAIL_COND(!data.size()); + + int s = data.size(); + uint8_t *w = data.ptrw(); + for (int i = 0; i < s; i += 4) { + w[i + 1] = w[i + 3]; + w[i + 2] = 0; + w[i + 3] = 255; + } +} + +Error Image::_load_from_buffer(const Vector<uint8_t> &p_array, ImageMemLoadFunc p_loader) { + int buffer_size = p_array.size(); + + ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(!p_loader, ERR_INVALID_PARAMETER); + + const uint8_t *r = p_array.ptr(); + + Ref<Image> image = p_loader(r, buffer_size); + ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR); + + copy_internals_from(image); + + return OK; +} + +void Image::average_4_uint8(uint8_t &p_out, const uint8_t &p_a, const uint8_t &p_b, const uint8_t &p_c, const uint8_t &p_d) { + p_out = static_cast<uint8_t>((p_a + p_b + p_c + p_d + 2) >> 2); +} + +void Image::average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d) { + p_out = (p_a + p_b + p_c + p_d) * 0.25f; +} + +void Image::average_4_half(uint16_t &p_out, const uint16_t &p_a, const uint16_t &p_b, const uint16_t &p_c, const uint16_t &p_d) { + p_out = Math::make_half_float((Math::half_to_float(p_a) + Math::half_to_float(p_b) + Math::half_to_float(p_c) + Math::half_to_float(p_d)) * 0.25f); +} + +void Image::average_4_rgbe9995(uint32_t &p_out, const uint32_t &p_a, const uint32_t &p_b, const uint32_t &p_c, const uint32_t &p_d) { + p_out = ((Color::from_rgbe9995(p_a) + Color::from_rgbe9995(p_b) + Color::from_rgbe9995(p_c) + Color::from_rgbe9995(p_d)) * 0.25f).to_rgbe9995(); +} + +void Image::renormalize_uint8(uint8_t *p_rgb) { + Vector3 n(p_rgb[0] / 255.0, p_rgb[1] / 255.0, p_rgb[2] / 255.0); + n *= 2.0; + n -= Vector3(1, 1, 1); + n.normalize(); + n += Vector3(1, 1, 1); + n *= 0.5; + n *= 255; + p_rgb[0] = CLAMP(int(n.x), 0, 255); + p_rgb[1] = CLAMP(int(n.y), 0, 255); + p_rgb[2] = CLAMP(int(n.z), 0, 255); +} + +void Image::renormalize_float(float *p_rgb) { + Vector3 n(p_rgb[0], p_rgb[1], p_rgb[2]); + n.normalize(); + p_rgb[0] = n.x; + p_rgb[1] = n.y; + p_rgb[2] = n.z; +} + +void Image::renormalize_half(uint16_t *p_rgb) { + Vector3 n(Math::half_to_float(p_rgb[0]), Math::half_to_float(p_rgb[1]), Math::half_to_float(p_rgb[2])); + n.normalize(); + p_rgb[0] = Math::make_half_float(n.x); + p_rgb[1] = Math::make_half_float(n.y); + p_rgb[2] = Math::make_half_float(n.z); +} + +void Image::renormalize_rgbe9995(uint32_t *p_rgb) { + // Never used +} + +Image::Image(const uint8_t *p_mem_png_jpg, int p_len) { + width = 0; + height = 0; + mipmaps = false; + format = FORMAT_L8; + + if (_png_mem_loader_func) { + copy_internals_from(_png_mem_loader_func(p_mem_png_jpg, p_len)); + } + + if (empty() && _jpg_mem_loader_func) { + copy_internals_from(_jpg_mem_loader_func(p_mem_png_jpg, p_len)); + } +} + +Ref<Resource> Image::duplicate(bool p_subresources) const { + Ref<Image> copy; + copy.instance(); + copy->_copy_internals_from(*this); + return copy; +} + +void Image::set_as_black() { + zeromem(data.ptrw(), data.size()); +} diff --git a/core/io/image.h b/core/io/image.h new file mode 100644 index 0000000000..6b4488bd66 --- /dev/null +++ b/core/io/image.h @@ -0,0 +1,413 @@ +/*************************************************************************/ +/* image.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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_H +#define IMAGE_H + +#include "core/io/resource.h" +#include "core/math/color.h" +#include "core/math/rect2.h" + +/** + * @author Juan Linietsky <reduzio@gmail.com> + * + * Image storage class. This is used to store an image in user memory, as well as + * providing some basic methods for image manipulation. + * Images can be loaded from a file, or registered into the Render object as textures. +*/ + +class Image; + +typedef Error (*SavePNGFunc)(const String &p_path, const Ref<Image> &p_img); +typedef Vector<uint8_t> (*SavePNGBufferFunc)(const Ref<Image> &p_img); +typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size); + +typedef Error (*SaveEXRFunc)(const String &p_path, const Ref<Image> &p_img, bool p_grayscale); + +class Image : public Resource { + GDCLASS(Image, Resource); + +public: + static SavePNGFunc save_png_func; + static SaveEXRFunc save_exr_func; + static SavePNGBufferFunc save_png_buffer_func; + + enum { + MAX_WIDTH = (1 << 24), // force a limit somehow + MAX_HEIGHT = (1 << 24), // force a limit somehow + MAX_PIXELS = 268435456 + }; + + enum Format { + FORMAT_L8, //luminance + FORMAT_LA8, //luminance-alpha + FORMAT_R8, + FORMAT_RG8, + FORMAT_RGB8, + FORMAT_RGBA8, + FORMAT_RGBA4444, + FORMAT_RGB565, + FORMAT_RF, //float + FORMAT_RGF, + FORMAT_RGBF, + FORMAT_RGBAF, + FORMAT_RH, //half float + FORMAT_RGH, + FORMAT_RGBH, + FORMAT_RGBAH, + FORMAT_RGBE9995, + FORMAT_DXT1, //s3tc bc1 + FORMAT_DXT3, //bc2 + FORMAT_DXT5, //bc3 + FORMAT_RGTC_R, + FORMAT_RGTC_RG, + FORMAT_BPTC_RGBA, //btpc bc7 + FORMAT_BPTC_RGBF, //float bc6h + FORMAT_BPTC_RGBFU, //unsigned float bc6hu + FORMAT_PVRTC1_2, //pvrtc1 + FORMAT_PVRTC1_2A, + FORMAT_PVRTC1_4, + FORMAT_PVRTC1_4A, + FORMAT_ETC, //etc1 + FORMAT_ETC2_R11, //etc2 + FORMAT_ETC2_R11S, //signed, NOT srgb. + FORMAT_ETC2_RG11, + FORMAT_ETC2_RG11S, + FORMAT_ETC2_RGB8, + FORMAT_ETC2_RGBA8, + FORMAT_ETC2_RGB8A1, + FORMAT_ETC2_RA_AS_RG, //used to make basis universal happy + FORMAT_DXT5_RA_AS_RG, //used to make basis universal happy + FORMAT_MAX + }; + + static const char *format_names[FORMAT_MAX]; + enum Interpolation { + INTERPOLATE_NEAREST, + INTERPOLATE_BILINEAR, + INTERPOLATE_CUBIC, + INTERPOLATE_TRILINEAR, + INTERPOLATE_LANCZOS, + /* INTERPOLATE_TRICUBIC, */ + /* INTERPOLATE GAUSS */ + }; + + //this is used for compression + enum UsedChannels { + USED_CHANNELS_L, + USED_CHANNELS_LA, + USED_CHANNELS_R, + USED_CHANNELS_RG, + USED_CHANNELS_RGB, + USED_CHANNELS_RGBA, + }; + //some functions provided by something else + + static ImageMemLoadFunc _png_mem_loader_func; + static ImageMemLoadFunc _jpg_mem_loader_func; + static ImageMemLoadFunc _webp_mem_loader_func; + static ImageMemLoadFunc _tga_mem_loader_func; + static ImageMemLoadFunc _bmp_mem_loader_func; + + static void (*_image_compress_bc_func)(Image *, float, UsedChannels p_channels); + static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, UsedChannels p_channels); + static void (*_image_compress_pvrtc1_4bpp_func)(Image *); + static void (*_image_compress_etc1_func)(Image *, float); + static void (*_image_compress_etc2_func)(Image *, float, UsedChannels p_channels); + + static void (*_image_decompress_pvrtc)(Image *); + static void (*_image_decompress_bc)(Image *); + static void (*_image_decompress_bptc)(Image *); + static void (*_image_decompress_etc1)(Image *); + static void (*_image_decompress_etc2)(Image *); + + static Vector<uint8_t> (*lossy_packer)(const Ref<Image> &p_image, float p_quality); + static Ref<Image> (*lossy_unpacker)(const Vector<uint8_t> &p_buffer); + static Vector<uint8_t> (*lossless_packer)(const Ref<Image> &p_image); + static Ref<Image> (*lossless_unpacker)(const Vector<uint8_t> &p_buffer); + static Vector<uint8_t> (*basis_universal_packer)(const Ref<Image> &p_image, UsedChannels p_channels); + static Ref<Image> (*basis_universal_unpacker)(const Vector<uint8_t> &p_buffer); + + _FORCE_INLINE_ Color _get_color_at_ofs(const uint8_t *ptr, uint32_t ofs) const; + _FORCE_INLINE_ void _set_color_at_ofs(uint8_t *ptr, uint32_t ofs, const Color &p_color); + +protected: + static void _bind_methods(); + +private: + void _create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { + create(p_width, p_height, p_use_mipmaps, p_format); + } + + void _create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + create(p_width, p_height, p_use_mipmaps, p_format, p_data); + } + + Format format = FORMAT_L8; + Vector<uint8_t> data; + int width = 0; + int height = 0; + bool mipmaps = false; + + void _copy_internals_from(const Image &p_image) { + format = p_image.format; + width = p_image.width; + height = p_image.height; + mipmaps = p_image.mipmaps; + data = p_image.data; + } + + _FORCE_INLINE_ void _get_mipmap_offset_and_size(int p_mipmap, int &r_offset, int &r_width, int &r_height) const; //get where the mipmap begins in data + + static int _get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps = -1, int *r_mm_width = nullptr, int *r_mm_height = nullptr); + bool _can_modify(Format p_format) const; + + _FORCE_INLINE_ void _put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel); + _FORCE_INLINE_ void _get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_data, uint8_t *p_pixel); + + void _set_data(const Dictionary &p_data); + Dictionary _get_data() const; + + Error _load_from_buffer(const Vector<uint8_t> &p_array, ImageMemLoadFunc p_loader); + + static void average_4_uint8(uint8_t &p_out, const uint8_t &p_a, const uint8_t &p_b, const uint8_t &p_c, const uint8_t &p_d); + static void average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d); + static void average_4_half(uint16_t &p_out, const uint16_t &p_a, const uint16_t &p_b, const uint16_t &p_c, const uint16_t &p_d); + static void average_4_rgbe9995(uint32_t &p_out, const uint32_t &p_a, const uint32_t &p_b, const uint32_t &p_c, const uint32_t &p_d); + static void renormalize_uint8(uint8_t *p_rgb); + static void renormalize_float(float *p_rgb); + static void renormalize_half(uint16_t *p_rgb); + static void renormalize_rgbe9995(uint32_t *p_rgb); + +public: + int get_width() const; ///< Get image width + int get_height() const; ///< Get image height + Vector2 get_size() const; + bool has_mipmaps() const; + int get_mipmap_count() const; + + /** + * Convert the image to another format, conversion only to raw byte format + */ + void convert(Format p_new_format); + + /** + * Get the current image format. + */ + Format get_format() const; + + int get_mipmap_byte_size(int p_mipmap) const; //get where the mipmap begins in data + int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data + void get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const; //get where the mipmap begins in data + void get_mipmap_offset_size_and_dimensions(int p_mipmap, int &r_ofs, int &r_size, int &w, int &h) const; //get where the mipmap begins in data + + enum Image3DValidateError { + VALIDATE_3D_OK, + VALIDATE_3D_ERR_IMAGE_EMPTY, + VALIDATE_3D_ERR_MISSING_IMAGES, + VALIDATE_3D_ERR_EXTRA_IMAGES, + VALIDATE_3D_ERR_IMAGE_SIZE_MISMATCH, + VALIDATE_3D_ERR_IMAGE_FORMAT_MISMATCH, + VALIDATE_3D_ERR_IMAGE_HAS_MIPMAPS, + }; + + static Image3DValidateError validate_3d_image(Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_images); + static String get_3d_image_validation_error_text(Image3DValidateError p_error); + + /** + * Resize the image, using the preferred interpolation method. + */ + void resize_to_po2(bool p_square = false, Interpolation p_interpolation = INTERPOLATE_BILINEAR); + void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR); + void shrink_x2(); + bool is_size_po2() const; + /** + * Crop the image to a specific size, if larger, then the image is filled by black + */ + void crop_from_point(int p_x, int p_y, int p_width, int p_height); + void crop(int p_width, int p_height); + + void flip_x(); + void flip_y(); + + /** + * Generate a mipmap to an image (creates an image 1/4 the size, with averaging of 4->1) + */ + Error generate_mipmaps(bool p_renormalize = false); + + enum RoughnessChannel { + ROUGHNESS_CHANNEL_R, + ROUGHNESS_CHANNEL_G, + ROUGHNESS_CHANNEL_B, + ROUGHNESS_CHANNEL_A, + ROUGHNESS_CHANNEL_L, + }; + + Error generate_mipmap_roughness(RoughnessChannel p_roughness_channel, const Ref<Image> &p_normal_map); + + void clear_mipmaps(); + void normalize(); //for normal maps + + /** + * Create a new image of a given size and format. Current image will be lost + */ + void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format); + void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data); + + void create(const char **p_xpm); + /** + * returns true when the image is empty (0,0) in size + */ + bool empty() const; + + Vector<uint8_t> get_data() const; + + Error load(const String &p_path); + Error save_png(const String &p_path) const; + Vector<uint8_t> save_png_to_buffer() const; + Error save_exr(const String &p_path, bool p_grayscale) const; + + /** + * create an empty image + */ + Image() {} + /** + * create an empty image of a specific size and format + */ + Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format); + /** + * import an image of a specific size and format from a pointer + */ + Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const Vector<uint8_t> &p_data); + + ~Image() {} + + enum AlphaMode { + ALPHA_NONE, + ALPHA_BIT, + ALPHA_BLEND + }; + + AlphaMode detect_alpha() const; + bool is_invisible() const; + + static int get_format_pixel_size(Format p_format); + static int get_format_pixel_rshift(Format p_format); + static int get_format_block_size(Format p_format); + static void get_format_min_pixel_size(Format p_format, int &r_w, int &r_h); + + static int get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps = false); + static int get_image_required_mipmaps(int p_width, int p_height, Format p_format); + static Size2i get_image_mipmap_size(int p_width, int p_height, Format p_format, int p_mipmap); + static int get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap); + static int get_image_mipmap_offset_and_dimensions(int p_width, int p_height, Format p_format, int p_mipmap, int &r_w, int &r_h); + + enum CompressMode { + COMPRESS_S3TC, + COMPRESS_PVRTC1_4, + COMPRESS_ETC, + COMPRESS_ETC2, + COMPRESS_BPTC, + }; + enum CompressSource { + COMPRESS_SOURCE_GENERIC, + COMPRESS_SOURCE_SRGB, + COMPRESS_SOURCE_NORMAL + }; + + Error compress(CompressMode p_mode, CompressSource p_source = COMPRESS_SOURCE_GENERIC, float p_lossy_quality = 0.7); + Error compress_from_channels(CompressMode p_mode, UsedChannels p_channels, float p_lossy_quality = 0.7); + Error decompress(); + bool is_compressed() const; + + void fix_alpha_edges(); + void premultiply_alpha(); + void srgb_to_linear(); + void normalmap_to_xy(); + Ref<Image> rgbe_to_srgb(); + Ref<Image> get_image_from_mipmap(int p_mipamp) const; + void bumpmap_to_normalmap(float bump_scale = 1.0); + + void blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest); + void blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest); + void blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest); + void blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest); + void fill(const Color &c); + + Rect2 get_used_rect() const; + Ref<Image> get_rect(const Rect2 &p_area) const; + + static void set_compress_bc_func(void (*p_compress_func)(Image *, float, UsedChannels)); + static void set_compress_bptc_func(void (*p_compress_func)(Image *, float, UsedChannels)); + static String get_format_name(Format p_format); + + Error load_png_from_buffer(const Vector<uint8_t> &p_array); + Error load_jpg_from_buffer(const Vector<uint8_t> &p_array); + Error load_webp_from_buffer(const Vector<uint8_t> &p_array); + Error load_tga_from_buffer(const Vector<uint8_t> &p_array); + Error load_bmp_from_buffer(const Vector<uint8_t> &p_array); + + void convert_rg_to_ra_rgba8(); + void convert_ra_rgba8_to_rg(); + + Image(const uint8_t *p_mem_png_jpg, int p_len = -1); + Image(const char **p_xpm); + + virtual Ref<Resource> duplicate(bool p_subresources = false) const override; + + UsedChannels detect_used_channels(CompressSource p_source = COMPRESS_SOURCE_GENERIC); + void optimize_channels(); + + Color get_pixelv(const Point2i &p_point) const; + Color get_pixel(int p_x, int p_y) const; + void set_pixelv(const Point2i &p_point, const Color &p_color); + void set_pixel(int p_x, int p_y, const Color &p_color); + + void set_as_black(); + + void copy_internals_from(const Ref<Image> &p_image) { + ERR_FAIL_COND_MSG(p_image.is_null(), "It's not a reference to a valid Image object."); + format = p_image->format; + width = p_image->width; + height = p_image->height; + mipmaps = p_image->mipmaps; + data = p_image->data; + } +}; + +VARIANT_ENUM_CAST(Image::Format) +VARIANT_ENUM_CAST(Image::Interpolation) +VARIANT_ENUM_CAST(Image::CompressMode) +VARIANT_ENUM_CAST(Image::CompressSource) +VARIANT_ENUM_CAST(Image::UsedChannels) +VARIANT_ENUM_CAST(Image::AlphaMode) +VARIANT_ENUM_CAST(Image::RoughnessChannel) + +#endif // IMAGE_H diff --git a/core/io/image_loader.cpp b/core/io/image_loader.cpp index b1e92eb87f..f6d8668349 100644 --- a/core/io/image_loader.cpp +++ b/core/io/image_loader.cpp @@ -30,7 +30,7 @@ #include "image_loader.h" -#include "core/print_string.h" +#include "core/string/print_string.h" bool ImageFormatLoader::recognize(const String &p_extension) const { List<String> extensions; diff --git a/core/io/image_loader.h b/core/io/image_loader.h index 9682f144c7..d5fb4678eb 100644 --- a/core/io/image_loader.h +++ b/core/io/image_loader.h @@ -31,11 +31,11 @@ #ifndef IMAGE_LOADER_H #define IMAGE_LOADER_H -#include "core/image.h" +#include "core/io/image.h" #include "core/io/resource_loader.h" -#include "core/list.h" #include "core/os/file_access.h" -#include "core/ustring.h" +#include "core/string/ustring.h" +#include "core/templates/list.h" class ImageLoader; diff --git a/core/io/ip.cpp b/core/io/ip.cpp index 24b8ec7cc1..9f3540efad 100644 --- a/core/io/ip.cpp +++ b/core/io/ip.cpp @@ -30,9 +30,9 @@ #include "ip.h" -#include "core/hash_map.h" #include "core/os/semaphore.h" #include "core/os/thread.h" +#include "core/templates/hash_map.h" VARIANT_ENUM_CAST(IP::ResolverStatus); diff --git a/core/io/ip.h b/core/io/ip.h index d434d02f9b..32572b8eb2 100644 --- a/core/io/ip.h +++ b/core/io/ip.h @@ -42,7 +42,6 @@ class IP : public Object { public: enum ResolverStatus { - RESOLVER_STATUS_NONE, RESOLVER_STATUS_WAITING, RESOLVER_STATUS_DONE, @@ -50,7 +49,6 @@ public: }; enum Type { - TYPE_NONE = 0, TYPE_IPV4 = 1, TYPE_IPV6 = 2, diff --git a/core/io/ip_address.cpp b/core/io/ip_address.cpp index c7a0ae5605..7d730e5ae8 100644 --- a/core/io/ip_address.cpp +++ b/core/io/ip_address.cpp @@ -31,7 +31,6 @@ #include "ip_address.h" /* IP_Address::operator Variant() const { - return operator String(); }*/ @@ -71,7 +70,7 @@ static void _parse_hex(const String &p_string, int p_start, uint8_t *p_dst) { } int n = 0; - CharType c = p_string[i]; + char32_t c = p_string[i]; if (c >= '0' && c <= '9') { n = c - '0'; } else if (c >= 'a' && c <= 'f') { @@ -101,7 +100,7 @@ void IP_Address::_parse_ipv6(const String &p_string) { int parts_idx = 0; for (int i = 0; i < p_string.length(); i++) { - CharType c = p_string[i]; + char32_t c = p_string[i]; if (c == ':') { if (i == 0) { continue; // next must be a ":" diff --git a/core/io/ip_address.h b/core/io/ip_address.h index 2f8f83503e..7a813230f5 100644 --- a/core/io/ip_address.h +++ b/core/io/ip_address.h @@ -31,7 +31,7 @@ #ifndef IP_ADDRESS_H #define IP_ADDRESS_H -#include "core/ustring.h" +#include "core/string/ustring.h" struct IP_Address { private: diff --git a/core/io/json.cpp b/core/io/json.cpp index 8bdd6385cb..d61c2b8236 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -30,7 +30,7 @@ #include "json.h" -#include "core/print_string.h" +#include "core/string/print_string.h" const char *JSON::tk_name[TK_MAX] = { "'{'", @@ -125,7 +125,7 @@ String JSON::print(const Variant &p_var, const String &p_indent, bool p_sort_key return _print_var(p_var, p_indent, 0, p_sort_keys); } -Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str) { +Error JSON::_get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str) { while (p_len > 0) { switch (p_str[index]) { case '\n': { @@ -180,12 +180,12 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to } else if (p_str[index] == '\\') { //escaped characters... index++; - CharType next = p_str[index]; + char32_t next = p_str[index]; if (next == 0) { r_err_str = "Unterminated String"; return ERR_PARSE_ERROR; } - CharType res = 0; + char32_t res = 0; switch (next) { case 'b': @@ -206,7 +206,7 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to case 'u': { // hex number for (int j = 0; j < 4; j++) { - CharType c = p_str[index + j + 1]; + char32_t c = p_str[index + j + 1]; if (c == 0) { r_err_str = "Unterminated String"; return ERR_PARSE_ERROR; @@ -215,7 +215,7 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to r_err_str = "Malformed hex constant in string"; return ERR_PARSE_ERROR; } - CharType v; + char32_t v; if (c >= '0' && c <= '9') { v = c - '0'; } else if (c >= 'a' && c <= 'f') { @@ -264,7 +264,7 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to if (p_str[index] == '-' || (p_str[index] >= '0' && p_str[index] <= '9')) { //a number - const CharType *rptr; + const char32_t *rptr; double number = String::to_float(&p_str[index], &rptr); index += (rptr - &p_str[index]); r_token.type = TK_NUMBER; @@ -293,7 +293,7 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to return ERR_PARSE_ERROR; } -Error JSON::_parse_value(Variant &value, Token &token, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str) { +Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) { if (token.type == TK_CURLY_BRACKET_OPEN) { Dictionary d; Error err = _parse_object(d, p_str, index, p_len, line, r_err_str); @@ -337,7 +337,7 @@ Error JSON::_parse_value(Variant &value, Token &token, const CharType *p_str, in } } -Error JSON::_parse_array(Array &array, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str) { +Error JSON::_parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) { Token token; bool need_comma = false; @@ -375,7 +375,7 @@ Error JSON::_parse_array(Array &array, const CharType *p_str, int &index, int p_ return ERR_PARSE_ERROR; } -Error JSON::_parse_object(Dictionary &object, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str) { +Error JSON::_parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) { bool at_key = true; String key; Token token; @@ -439,7 +439,7 @@ Error JSON::_parse_object(Dictionary &object, const CharType *p_str, int &index, } Error JSON::parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line) { - const CharType *str = p_json.ptr(); + const char32_t *str = p_json.ptr(); int idx = 0; int len = p_json.length(); Token token; @@ -455,3 +455,35 @@ Error JSON::parse(const String &p_json, Variant &r_ret, String &r_err_str, int & return err; } + +Error JSONParser::parse_string(const String &p_json_string) { + return JSON::parse(p_json_string, data, err_text, err_line); +} +String JSONParser::get_error_text() const { + return err_text; +} +int JSONParser::get_error_line() const { + return err_line; +} +Variant JSONParser::get_data() const { + return data; +} + +Error JSONParser::decode_data(const Variant &p_data, const String &p_indent, bool p_sort_keys) { + string = JSON::print(p_data, p_indent, p_sort_keys); + data = p_data; + return OK; +} + +String JSONParser::get_string() const { + return string; +} + +void JSONParser::_bind_methods() { + ClassDB::bind_method(D_METHOD("parse_string", "json_string"), &JSONParser::parse_string); + ClassDB::bind_method(D_METHOD("get_error_text"), &JSONParser::get_error_text); + ClassDB::bind_method(D_METHOD("get_error_line"), &JSONParser::get_error_line); + ClassDB::bind_method(D_METHOD("get_data"), &JSONParser::get_data); + ClassDB::bind_method(D_METHOD("decode_data", "data", "indent", "sort_keys"), &JSONParser::decode_data, DEFVAL(""), DEFVAL(true)); + ClassDB::bind_method(D_METHOD("get_string"), &JSONParser::get_string); +} diff --git a/core/io/json.h b/core/io/json.h index 4fc5630a93..431b252e55 100644 --- a/core/io/json.h +++ b/core/io/json.h @@ -31,8 +31,8 @@ #ifndef JSON_H #define JSON_H -#include "core/variant.h" - +#include "core/object/reference.h" +#include "core/variant/variant.h" class JSON { enum TokenType { TK_CURLY_BRACKET_OPEN, @@ -49,7 +49,6 @@ class JSON { }; enum Expecting { - EXPECT_OBJECT, EXPECT_OBJECT_KEY, EXPECT_COLON, @@ -65,14 +64,35 @@ class JSON { static String _print_var(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys); - static Error _get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str); - static Error _parse_value(Variant &value, Token &token, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str); - static Error _parse_array(Array &array, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str); - static Error _parse_object(Dictionary &object, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str); + static Error _get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str); + static Error _parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str); + static Error _parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str); + static Error _parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str); public: static String print(const Variant &p_var, const String &p_indent = "", bool p_sort_keys = true); static Error parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line); }; +class JSONParser : public Reference { + GDCLASS(JSONParser, Reference); + + Variant data; + String string; + String err_text; + int err_line = 0; + +protected: + static void _bind_methods(); + +public: + Error parse_string(const String &p_json_string); + String get_error_text() const; + int get_error_line() const; + Variant get_data() const; + + Error decode_data(const Variant &p_data, const String &p_indent = "", bool p_sort_keys = true); + String get_string() const; +}; + #endif // JSON_H diff --git a/core/io/logger.cpp b/core/io/logger.cpp index 886e5695b1..241c72d310 100644 --- a/core/io/logger.cpp +++ b/core/io/logger.cpp @@ -30,9 +30,10 @@ #include "logger.h" +#include "core/config/project_settings.h" #include "core/os/dir_access.h" #include "core/os/os.h" -#include "core/print_string.h" +#include "core/string/print_string.h" #if defined(MINGW_ENABLED) || defined(_MSC_VER) #define sprintf sprintf_s @@ -152,7 +153,7 @@ void RotatedFileLogger::rotate_file() { char timestamp[21]; OS::Date date = OS::get_singleton()->get_date(); OS::Time time = OS::get_singleton()->get_time(); - sprintf(timestamp, "_%04d-%02d-%02d_%02d-%02d-%02d", date.year, date.month, date.day, time.hour, time.min, time.sec); + sprintf(timestamp, "_%04d-%02d-%02d_%02d.%02d.%02d", date.year, date.month, date.day, time.hour, time.min, time.sec); String backup_name = base_path.get_basename() + timestamp; if (base_path.get_extension() != String()) { @@ -201,15 +202,14 @@ void RotatedFileLogger::logv(const char *p_format, va_list p_list, bool p_err) { } va_end(list_copy); file->store_buffer((uint8_t *)buf, len); + if (len >= static_buf_size) { Memory::free_static(buf); } -#ifdef DEBUG_ENABLED - const bool need_flush = true; -#else - bool need_flush = p_err; -#endif - if (need_flush) { + + if (p_err || GLOBAL_GET("application/run/flush_stdout_on_print")) { + // Don't always flush when printing stdout to avoid performance + // issues when `print()` is spammed in release builds. file->flush(); } } @@ -228,9 +228,11 @@ void StdLogger::logv(const char *p_format, va_list p_list, bool p_err) { vfprintf(stderr, p_format, p_list); } else { vprintf(p_format, p_list); -#ifdef DEBUG_ENABLED - fflush(stdout); -#endif + if (GLOBAL_GET("application/run/flush_stdout_on_print")) { + // Don't always flush when printing stdout to avoid performance + // issues when `print()` is spammed in release builds. + fflush(stdout); + } } } diff --git a/core/io/logger.h b/core/io/logger.h index 277be9ed35..9eaf506c51 100644 --- a/core/io/logger.h +++ b/core/io/logger.h @@ -32,8 +32,8 @@ #define LOGGER_H #include "core/os/file_access.h" -#include "core/ustring.h" -#include "core/vector.h" +#include "core/string/ustring.h" +#include "core/templates/vector.h" #include <stdarg.h> diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index eb39b1433f..db12fcb7f7 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -30,9 +30,9 @@ #include "marshalls.h" +#include "core/object/reference.h" #include "core/os/keyboard.h" -#include "core/print_string.h" -#include "core/reference.h" +#include "core/string/print_string.h" #include <limits.h> #include <stdio.h> @@ -420,7 +420,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } } break; - case Variant::_RID: { + case Variant::RID: { r_variant = RID(); } break; case Variant::OBJECT: { @@ -1003,10 +1003,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } } break; - case Variant::STRING: { - _encode_string(p_variant, buf, r_len); - - } break; + case Variant::STRING: case Variant::STRING_NAME: { _encode_string(p_variant, buf, r_len); @@ -1172,7 +1169,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4 * 4; } break; - case Variant::_RID: { + case Variant::RID: { } break; case Variant::CALLABLE: { } break; diff --git a/core/io/marshalls.h b/core/io/marshalls.h index c8ed497528..6969a9b500 100644 --- a/core/io/marshalls.h +++ b/core/io/marshalls.h @@ -31,9 +31,9 @@ #ifndef MARSHALLS_H #define MARSHALLS_H -#include "core/reference.h" +#include "core/object/reference.h" #include "core/typedefs.h" -#include "core/variant.h" +#include "core/variant/variant.h" /** * Miscellaneous helpers for marshalling data types, and encoding diff --git a/core/io/multiplayer_api.cpp b/core/io/multiplayer_api.cpp index 2f17d9e746..afab00ebd6 100644 --- a/core/io/multiplayer_api.cpp +++ b/core/io/multiplayer_api.cpp @@ -142,6 +142,10 @@ void MultiplayerAPI::set_root_node(Node *p_node) { root_node = p_node; } +Node *MultiplayerAPI::get_root_node() { + return root_node; +} + void MultiplayerAPI::set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_peer) { if (p_peer == network_peer) { return; // Nothing to do @@ -1202,6 +1206,7 @@ bool MultiplayerAPI::is_object_decoding_allowed() const { void MultiplayerAPI::_bind_methods() { ClassDB::bind_method(D_METHOD("set_root_node", "node"), &MultiplayerAPI::set_root_node); + ClassDB::bind_method(D_METHOD("get_root_node"), &MultiplayerAPI::get_root_node); ClassDB::bind_method(D_METHOD("send_bytes", "bytes", "id", "mode"), &MultiplayerAPI::send_bytes, DEFVAL(NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST), DEFVAL(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE)); ClassDB::bind_method(D_METHOD("has_network_peer"), &MultiplayerAPI::has_network_peer); ClassDB::bind_method(D_METHOD("get_network_peer"), &MultiplayerAPI::get_network_peer); @@ -1221,6 +1226,7 @@ void MultiplayerAPI::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_object_decoding"), "set_allow_object_decoding", "is_object_decoding_allowed"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_network_connections"), "set_refuse_new_network_connections", "is_refusing_new_network_connections"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "NetworkedMultiplayerPeer", 0), "set_network_peer", "get_network_peer"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root_node", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_root_node", "get_root_node"); ADD_PROPERTY_DEFAULT("refuse_new_network_connections", false); ADD_SIGNAL(MethodInfo("network_peer_connected", PropertyInfo(Variant::INT, "id"))); diff --git a/core/io/multiplayer_api.h b/core/io/multiplayer_api.h index 06eab7796c..ca52a1c689 100644 --- a/core/io/multiplayer_api.h +++ b/core/io/multiplayer_api.h @@ -32,7 +32,7 @@ #define MULTIPLAYER_API_H #include "core/io/networked_multiplayer_peer.h" -#include "core/reference.h" +#include "core/object/reference.h" class MultiplayerAPI : public Reference { GDCLASS(MultiplayerAPI, Reference); @@ -102,7 +102,6 @@ public: }; enum RPCMode { - RPC_MODE_DISABLED, // No rpc for this method, calls to this will be blocked (default) RPC_MODE_REMOTE, // Using rpc() on it will call method / set property in all remote peers RPC_MODE_MASTER, // Using rpc() on it will call method on wherever the master is, be it local or remote @@ -115,6 +114,7 @@ public: void poll(); void clear(); void set_root_node(Node *p_node); + Node *get_root_node(); void set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_peer); Ref<NetworkedMultiplayerPeer> get_network_peer() const; Error send_bytes(Vector<uint8_t> p_data, int p_to = NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST, NetworkedMultiplayerPeer::TransferMode p_mode = NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE); diff --git a/core/io/net_socket.h b/core/io/net_socket.h index 746945eced..67d0253985 100644 --- a/core/io/net_socket.h +++ b/core/io/net_socket.h @@ -32,7 +32,7 @@ #define NET_SOCKET_H #include "core/io/ip.h" -#include "core/reference.h" +#include "core/object/reference.h" class NetSocket : public Reference { protected: diff --git a/core/io/packed_data_container.cpp b/core/io/packed_data_container.cpp new file mode 100644 index 0000000000..fbe8fa8a28 --- /dev/null +++ b/core/io/packed_data_container.cpp @@ -0,0 +1,395 @@ +/*************************************************************************/ +/* packed_data_container.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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 "packed_data_container.h" + +#include "core/core_string_names.h" +#include "core/io/marshalls.h" + +Variant PackedDataContainer::getvar(const Variant &p_key, bool *r_valid) const { + bool err = false; + Variant ret = _key_at_ofs(0, p_key, err); + if (r_valid) { + *r_valid = !err; + } + if (err) { + return Object::getvar(p_key, r_valid); + } + return ret; +} + +int PackedDataContainer::size() const { + return _size(0); +} + +Variant PackedDataContainer::_iter_init_ofs(const Array &p_iter, uint32_t p_offset) { + Array ref = p_iter; + uint32_t size = _size(p_offset); + if (size == 0 || ref.size() != 1) { + return false; + } else { + ref[0] = 0; + return true; + } +} + +Variant PackedDataContainer::_iter_next_ofs(const Array &p_iter, uint32_t p_offset) { + Array ref = p_iter; + int size = _size(p_offset); + if (ref.size() != 1) { + return false; + } + int pos = ref[0]; + if (pos < 0 || pos >= size) { + return false; + } + pos += 1; + ref[0] = pos; + return pos != size; +} + +Variant PackedDataContainer::_iter_get_ofs(const Variant &p_iter, uint32_t p_offset) { + int size = _size(p_offset); + int pos = p_iter; + if (pos < 0 || pos >= size) { + return Variant(); + } + + const uint8_t *rd = data.ptr(); + const uint8_t *r = &rd[p_offset]; + uint32_t type = decode_uint32(r); + + bool err = false; + if (type == TYPE_ARRAY) { + uint32_t vpos = decode_uint32(rd + p_offset + 8 + pos * 4); + return _get_at_ofs(vpos, rd, err); + + } else if (type == TYPE_DICT) { + uint32_t vpos = decode_uint32(rd + p_offset + 8 + pos * 12 + 4); + return _get_at_ofs(vpos, rd, err); + } else { + ERR_FAIL_V(Variant()); + } +} + +Variant PackedDataContainer::_get_at_ofs(uint32_t p_ofs, const uint8_t *p_buf, bool &err) const { + uint32_t type = decode_uint32(p_buf + p_ofs); + + if (type == TYPE_ARRAY || type == TYPE_DICT) { + Ref<PackedDataContainerRef> pdcr = memnew(PackedDataContainerRef); + Ref<PackedDataContainer> pdc = Ref<PackedDataContainer>((PackedDataContainer *)this); + + pdcr->from = pdc; + pdcr->offset = p_ofs; + return pdcr; + } else { + Variant v; + Error rerr = decode_variant(v, p_buf + p_ofs, datalen - p_ofs, nullptr, false); + + if (rerr != OK) { + err = true; + ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant."); + } + return v; + } +} + +uint32_t PackedDataContainer::_type_at_ofs(uint32_t p_ofs) const { + const uint8_t *rd = data.ptr(); + const uint8_t *r = &rd[p_ofs]; + uint32_t type = decode_uint32(r); + + return type; +} + +int PackedDataContainer::_size(uint32_t p_ofs) const { + const uint8_t *rd = data.ptr(); + ERR_FAIL_COND_V(!rd, 0); + const uint8_t *r = &rd[p_ofs]; + uint32_t type = decode_uint32(r); + + if (type == TYPE_ARRAY) { + uint32_t len = decode_uint32(r + 4); + return len; + + } else if (type == TYPE_DICT) { + uint32_t len = decode_uint32(r + 4); + return len; + } + + return -1; +} + +Variant PackedDataContainer::_key_at_ofs(uint32_t p_ofs, const Variant &p_key, bool &err) const { + const uint8_t *rd = data.ptr(); + const uint8_t *r = &rd[p_ofs]; + uint32_t type = decode_uint32(r); + + if (type == TYPE_ARRAY) { + if (p_key.is_num()) { + int idx = p_key; + int len = decode_uint32(r + 4); + if (idx < 0 || idx >= len) { + err = true; + return Variant(); + } + uint32_t ofs = decode_uint32(r + 8 + 4 * idx); + return _get_at_ofs(ofs, rd, err); + + } else { + err = true; + return Variant(); + } + + } else if (type == TYPE_DICT) { + uint32_t hash = p_key.hash(); + uint32_t len = decode_uint32(r + 4); + + bool found = false; + for (uint32_t i = 0; i < len; i++) { + uint32_t khash = decode_uint32(r + 8 + i * 12 + 0); + if (khash == hash) { + Variant key = _get_at_ofs(decode_uint32(r + 8 + i * 12 + 4), rd, err); + if (err) { + return Variant(); + } + if (key == p_key) { + //key matches, return value + return _get_at_ofs(decode_uint32(r + 8 + i * 12 + 8), rd, err); + } + found = true; + } else { + if (found) { + break; + } + } + } + + err = true; + return Variant(); + + } else { + err = true; + return Variant(); + } +} + +uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpdata, Map<String, uint32_t> &string_cache) { + switch (p_data.get_type()) { + case Variant::STRING: { + String s = p_data; + if (string_cache.has(s)) { + return string_cache[s]; + } + + string_cache[s] = tmpdata.size(); + + [[fallthrough]]; + } + case Variant::NIL: + case Variant::BOOL: + case Variant::INT: + case Variant::FLOAT: + case Variant::VECTOR2: + case Variant::RECT2: + case Variant::VECTOR3: + case Variant::TRANSFORM2D: + case Variant::PLANE: + case Variant::QUAT: + case Variant::AABB: + case Variant::BASIS: + case Variant::TRANSFORM: + case Variant::PACKED_BYTE_ARRAY: + case Variant::PACKED_INT32_ARRAY: + case Variant::PACKED_INT64_ARRAY: + case Variant::PACKED_FLOAT32_ARRAY: + case Variant::PACKED_FLOAT64_ARRAY: + case Variant::PACKED_STRING_ARRAY: + case Variant::PACKED_VECTOR2_ARRAY: + case Variant::PACKED_VECTOR3_ARRAY: + case Variant::PACKED_COLOR_ARRAY: + case Variant::STRING_NAME: + case Variant::NODE_PATH: { + uint32_t pos = tmpdata.size(); + int len; + encode_variant(p_data, nullptr, len, false); + tmpdata.resize(tmpdata.size() + len); + encode_variant(p_data, &tmpdata.write[pos], len, false); + return pos; + + } break; + // misc types + case Variant::RID: + case Variant::OBJECT: { + return _pack(Variant(), tmpdata, string_cache); + } break; + case Variant::DICTIONARY: { + Dictionary d = p_data; + //size is known, use sort + uint32_t pos = tmpdata.size(); + int len = d.size(); + tmpdata.resize(tmpdata.size() + len * 12 + 8); + encode_uint32(TYPE_DICT, &tmpdata.write[pos + 0]); + encode_uint32(len, &tmpdata.write[pos + 4]); + + List<Variant> keys; + d.get_key_list(&keys); + List<DictKey> sortk; + + for (List<Variant>::Element *E = keys.front(); E; E = E->next()) { + DictKey dk; + dk.hash = E->get().hash(); + dk.key = E->get(); + sortk.push_back(dk); + } + + sortk.sort(); + + int idx = 0; + for (List<DictKey>::Element *E = sortk.front(); E; E = E->next()) { + encode_uint32(E->get().hash, &tmpdata.write[pos + 8 + idx * 12 + 0]); + uint32_t ofs = _pack(E->get().key, tmpdata, string_cache); + encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 4]); + ofs = _pack(d[E->get().key], tmpdata, string_cache); + encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 8]); + idx++; + } + + return pos; + + } break; + case Variant::ARRAY: { + Array a = p_data; + //size is known, use sort + uint32_t pos = tmpdata.size(); + int len = a.size(); + tmpdata.resize(tmpdata.size() + len * 4 + 8); + encode_uint32(TYPE_ARRAY, &tmpdata.write[pos + 0]); + encode_uint32(len, &tmpdata.write[pos + 4]); + + for (int i = 0; i < len; i++) { + uint32_t ofs = _pack(a[i], tmpdata, string_cache); + encode_uint32(ofs, &tmpdata.write[pos + 8 + i * 4]); + } + + return pos; + + } break; + + default: { + } + } + + return OK; +} + +Error PackedDataContainer::pack(const Variant &p_data) { + Vector<uint8_t> tmpdata; + Map<String, uint32_t> string_cache; + _pack(p_data, tmpdata, string_cache); + datalen = tmpdata.size(); + data.resize(tmpdata.size()); + uint8_t *w = data.ptrw(); + copymem(w, tmpdata.ptr(), tmpdata.size()); + + return OK; +} + +void PackedDataContainer::_set_data(const Vector<uint8_t> &p_data) { + data = p_data; + datalen = data.size(); +} + +Vector<uint8_t> PackedDataContainer::_get_data() const { + return data; +} + +Variant PackedDataContainer::_iter_init(const Array &p_iter) { + return _iter_init_ofs(p_iter, 0); +} + +Variant PackedDataContainer::_iter_next(const Array &p_iter) { + return _iter_next_ofs(p_iter, 0); +} + +Variant PackedDataContainer::_iter_get(const Variant &p_iter) { + return _iter_get_ofs(p_iter, 0); +} + +void PackedDataContainer::_bind_methods() { + ClassDB::bind_method(D_METHOD("_set_data"), &PackedDataContainer::_set_data); + ClassDB::bind_method(D_METHOD("_get_data"), &PackedDataContainer::_get_data); + ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainer::_iter_init); + ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainer::_iter_get); + ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainer::_iter_next); + ClassDB::bind_method(D_METHOD("pack", "value"), &PackedDataContainer::pack); + ClassDB::bind_method(D_METHOD("size"), &PackedDataContainer::size); + + ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "__data__"), "_set_data", "_get_data"); +} + +////////////////// + +Variant PackedDataContainerRef::_iter_init(const Array &p_iter) { + return from->_iter_init_ofs(p_iter, offset); +} + +Variant PackedDataContainerRef::_iter_next(const Array &p_iter) { + return from->_iter_next_ofs(p_iter, offset); +} + +Variant PackedDataContainerRef::_iter_get(const Variant &p_iter) { + return from->_iter_get_ofs(p_iter, offset); +} + +bool PackedDataContainerRef::_is_dictionary() const { + return from->_type_at_ofs(offset) == PackedDataContainer::TYPE_DICT; +} + +void PackedDataContainerRef::_bind_methods() { + ClassDB::bind_method(D_METHOD("size"), &PackedDataContainerRef::size); + ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainerRef::_iter_init); + ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainerRef::_iter_get); + ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainerRef::_iter_next); + ClassDB::bind_method(D_METHOD("_is_dictionary"), &PackedDataContainerRef::_is_dictionary); +} + +Variant PackedDataContainerRef::getvar(const Variant &p_key, bool *r_valid) const { + bool err = false; + Variant ret = from->_key_at_ofs(offset, p_key, err); + if (r_valid) { + *r_valid = !err; + } + return ret; +} + +int PackedDataContainerRef::size() const { + return from->_size(offset); +} diff --git a/core/io/file_access_buffered.h b/core/io/packed_data_container.h index 99d5ce903d..3899c14bb4 100644 --- a/core/io/file_access_buffered.h +++ b/core/io/packed_data_container.h @@ -1,5 +1,5 @@ /*************************************************************************/ -/* file_access_buffered.h */ +/* packed_data_container.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -28,64 +28,78 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifndef FILE_ACCESS_BUFFERED_H -#define FILE_ACCESS_BUFFERED_H +#ifndef PACKED_DATA_CONTAINER_H +#define PACKED_DATA_CONTAINER_H -#include "core/os/file_access.h" +#include "core/io/resource.h" -#include "core/ustring.h" +class PackedDataContainer : public Resource { + GDCLASS(PackedDataContainer, Resource); -class FileAccessBuffered : public FileAccess { -public: enum { - DEFAULT_CACHE_SIZE = 128 * 1024, + TYPE_DICT = 0xFFFFFFFF, + TYPE_ARRAY = 0xFFFFFFFE, }; -private: - int cache_size = DEFAULT_CACHE_SIZE; + struct DictKey { + uint32_t hash; + Variant key; + bool operator<(const DictKey &p_key) const { return hash < p_key.hash; } + }; - int cache_data_left() const; - mutable Error last_error; + Vector<uint8_t> data; + int datalen = 0; -protected: - Error set_error(Error p_error) const; + uint32_t _pack(const Variant &p_data, Vector<uint8_t> &tmpdata, Map<String, uint32_t> &string_cache); - mutable struct File { - bool open = false; - int size = 0; - int offset = 0; - String name; - int access_flags = 0; - } file; + Variant _iter_init_ofs(const Array &p_iter, uint32_t p_offset); + Variant _iter_next_ofs(const Array &p_iter, uint32_t p_offset); + Variant _iter_get_ofs(const Variant &p_iter, uint32_t p_offset); - mutable struct Cache { - Vector<uint8_t> buffer; - int offset = 0; - } cache; + Variant _iter_init(const Array &p_iter); + Variant _iter_next(const Array &p_iter); + Variant _iter_get(const Variant &p_iter); - virtual int read_data_block(int p_offset, int p_size, uint8_t *p_dest = nullptr) const = 0; + friend class PackedDataContainerRef; + Variant _key_at_ofs(uint32_t p_ofs, const Variant &p_key, bool &err) const; + Variant _get_at_ofs(uint32_t p_ofs, const uint8_t *p_buf, bool &err) const; + uint32_t _type_at_ofs(uint32_t p_ofs) const; + int _size(uint32_t p_ofs) const; - void set_cache_size(int p_size); - int get_cache_size(); +protected: + void _set_data(const Vector<uint8_t> &p_data); + Vector<uint8_t> _get_data() const; + static void _bind_methods(); public: - virtual size_t get_position() const; ///< get position in the file - virtual size_t get_len() const; ///< get size of the file + virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override; + Error pack(const Variant &p_data); - virtual void seek(size_t p_position); ///< seek to a given position - virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file + int size() const; - virtual bool eof_reached() const; + PackedDataContainer() {} +}; - virtual uint8_t get_8() const; - virtual int get_buffer(uint8_t *p_dest, int p_length) const; ///< get an array of bytes +class PackedDataContainerRef : public Reference { + GDCLASS(PackedDataContainerRef, Reference); - virtual bool is_open() const; + friend class PackedDataContainer; + uint32_t offset = 0; + Ref<PackedDataContainer> from; + +protected: + static void _bind_methods(); + +public: + Variant _iter_init(const Array &p_iter); + Variant _iter_next(const Array &p_iter); + Variant _iter_get(const Variant &p_iter); + bool _is_dictionary() const; - virtual Error get_error() const; + int size() const; + virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override; - FileAccessBuffered() {} - virtual ~FileAccessBuffered() {} + PackedDataContainerRef() {} }; -#endif +#endif // PACKED_DATA_CONTAINER_H diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp index dacd548a3e..3da494a8ef 100644 --- a/core/io/packet_peer.cpp +++ b/core/io/packet_peer.cpp @@ -30,8 +30,8 @@ #include "packet_peer.h" +#include "core/config/project_settings.h" #include "core/io/marshalls.h" -#include "core/project_settings.h" /* helpers / binders */ @@ -151,11 +151,6 @@ void PacketPeer::_bind_methods() { /***************/ -void PacketPeerStream::_set_stream_peer(REF p_peer) { - ERR_FAIL_COND_MSG(p_peer.is_null(), "It's not a reference to a valid Resource object."); - set_stream_peer(p_peer); -} - void PacketPeerStream::_bind_methods() { ClassDB::bind_method(D_METHOD("set_stream_peer", "peer"), &PacketPeerStream::set_stream_peer); ClassDB::bind_method(D_METHOD("get_stream_peer"), &PacketPeerStream::get_stream_peer); @@ -263,10 +258,8 @@ int PacketPeerStream::get_max_packet_size() const { } void PacketPeerStream::set_stream_peer(const Ref<StreamPeer> &p_peer) { - //ERR_FAIL_COND(p_peer.is_null()); - if (p_peer.ptr() != peer.ptr()) { - ring_buffer.advance_read(ring_buffer.data_left()); // reset the ring buffer + ring_buffer.advance_read(ring_buffer.data_left()); // Reset the ring buffer. } peer = p_peer; diff --git a/core/io/packet_peer.h b/core/io/packet_peer.h index 92cdbc4151..a25fa03875 100644 --- a/core/io/packet_peer.h +++ b/core/io/packet_peer.h @@ -32,8 +32,8 @@ #define PACKET_PEER_H #include "core/io/stream_peer.h" -#include "core/object.h" -#include "core/ring_buffer.h" +#include "core/object/class_db.h" +#include "core/templates/ring_buffer.h" class PacketPeer : public Reference { GDCLASS(PacketPeer, Reference); @@ -86,7 +86,6 @@ class PacketPeerStream : public PacketPeer { Error _poll_buffer() const; protected: - void _set_stream_peer(REF p_peer); static void _bind_methods(); public: diff --git a/core/io/packet_peer_dtls.cpp b/core/io/packet_peer_dtls.cpp index 632f86a9f6..9f6fccc993 100644 --- a/core/io/packet_peer_dtls.cpp +++ b/core/io/packet_peer_dtls.cpp @@ -29,8 +29,8 @@ /*************************************************************************/ #include "packet_peer_dtls.h" +#include "core/config/project_settings.h" #include "core/os/file_access.h" -#include "core/project_settings.h" PacketPeerDTLS *(*PacketPeerDTLS::_create)() = nullptr; bool PacketPeerDTLS::available = false; diff --git a/core/io/packet_peer_udp.cpp b/core/io/packet_peer_udp.cpp index e633a56d54..488cfbeaa8 100644 --- a/core/io/packet_peer_udp.cpp +++ b/core/io/packet_peer_udp.cpp @@ -178,7 +178,6 @@ Error PacketPeerUDP::listen(int p_port, const IP_Address &p_bind_address, int p_ } _sock->set_blocking_enabled(false); - _sock->set_reuse_address_enabled(true); _sock->set_broadcasting_enabled(broadcast); err = _sock->bind(p_bind_address, p_port); diff --git a/core/io/pck_packer.cpp b/core/io/pck_packer.cpp index 374b2a5e07..5480d3c535 100644 --- a/core/io/pck_packer.cpp +++ b/core/io/pck_packer.cpp @@ -30,36 +30,58 @@ #include "pck_packer.h" +#include "core/crypto/crypto_core.h" +#include "core/io/file_access_encrypted.h" #include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION #include "core/os/file_access.h" #include "core/version.h" -static uint64_t _align(uint64_t p_n, int p_alignment) { - if (p_alignment == 0) { - return p_n; +static int _get_pad(int p_alignment, int p_n) { + int rest = p_n % p_alignment; + int pad = 0; + if (rest > 0) { + pad = p_alignment - rest; } - uint64_t rest = p_n % p_alignment; - if (rest == 0) { - return p_n; - } else { - return p_n + (p_alignment - rest); - } -} - -static void _pad(FileAccess *p_file, int p_bytes) { - for (int i = 0; i < p_bytes; i++) { - p_file->store_8(0); - } + return pad; } void PCKPacker::_bind_methods() { - ClassDB::bind_method(D_METHOD("pck_start", "pck_name", "alignment"), &PCKPacker::pck_start, DEFVAL(0)); - ClassDB::bind_method(D_METHOD("add_file", "pck_path", "source_path"), &PCKPacker::add_file); + ClassDB::bind_method(D_METHOD("pck_start", "pck_name", "alignment", "key", "encrypt_directory"), &PCKPacker::pck_start, DEFVAL(0), DEFVAL(String()), DEFVAL(false)); + ClassDB::bind_method(D_METHOD("add_file", "pck_path", "source_path", "encrypt"), &PCKPacker::add_file, DEFVAL(false)); ClassDB::bind_method(D_METHOD("flush", "verbose"), &PCKPacker::flush, DEFVAL(false)); } -Error PCKPacker::pck_start(const String &p_file, int p_alignment) { +Error PCKPacker::pck_start(const String &p_file, int p_alignment, const String &p_key, bool p_encrypt_directory) { + ERR_FAIL_COND_V_MSG((p_key.empty() || !p_key.is_valid_hex_number(false) || p_key.length() != 64), ERR_CANT_CREATE, "Invalid Encryption Key (must be 64 characters long)."); + + String _key = p_key.to_lower(); + key.resize(32); + for (int i = 0; i < 32; i++) { + int v = 0; + if (i * 2 < _key.length()) { + char32_t ct = _key[i * 2]; + if (ct >= '0' && ct <= '9') { + ct = ct - '0'; + } else if (ct >= 'a' && ct <= 'f') { + ct = 10 + ct - 'a'; + } + v |= ct << 4; + } + + if (i * 2 + 1 < _key.length()) { + char32_t ct = _key[i * 2 + 1]; + if (ct >= '0' && ct <= '9') { + ct = ct - '0'; + } else if (ct >= 'a' && ct <= 'f') { + ct = 10 + ct - 'a'; + } + v |= ct; + } + key.write[i] = v; + } + enc_dir = p_encrypt_directory; + if (file != nullptr) { memdelete(file); } @@ -76,16 +98,19 @@ Error PCKPacker::pck_start(const String &p_file, int p_alignment) { file->store_32(VERSION_MINOR); file->store_32(VERSION_PATCH); - for (int i = 0; i < 16; i++) { - file->store_32(0); // reserved + uint32_t pack_flags = 0; + if (enc_dir) { + pack_flags |= PACK_DIR_ENCRYPTED; } + file->store_32(pack_flags); // flags files.clear(); + ofs = 0; return OK; } -Error PCKPacker::add_file(const String &p_file, const String &p_src) { +Error PCKPacker::add_file(const String &p_file, const String &p_src, bool p_encrypt) { FileAccess *f = FileAccess::open(p_src, FileAccess::READ); if (!f) { return ERR_FILE_CANT_OPEN; @@ -94,8 +119,32 @@ Error PCKPacker::add_file(const String &p_file, const String &p_src) { File pf; pf.path = p_file; pf.src_path = p_src; + pf.ofs = ofs; pf.size = f->get_len(); - pf.offset_offset = 0; + + Vector<uint8_t> data = FileAccess::get_file_as_array(p_src); + { + unsigned char hash[16]; + CryptoCore::md5(data.ptr(), data.size(), hash); + pf.md5.resize(16); + for (int i = 0; i < 16; i++) { + pf.md5.write[i] = hash[i]; + } + } + pf.encrypted = p_encrypt; + + uint64_t _size = pf.size; + if (p_encrypt) { // Add encryption overhead. + if (_size % 16) { // Pad to encryption block size. + _size += 16 - (_size % 16); + } + _size += 16; // hash + _size += 8; // data size + _size += 16; // iv + } + + int pad = _get_pad(alignment, ofs + _size); + ofs = ofs + _size + pad; files.push_back(pf); @@ -108,27 +157,64 @@ Error PCKPacker::add_file(const String &p_file, const String &p_src) { Error PCKPacker::flush(bool p_verbose) { ERR_FAIL_COND_V_MSG(!file, ERR_INVALID_PARAMETER, "File must be opened before use."); - // write the index + int64_t file_base_ofs = file->get_position(); + file->store_64(0); // files base + for (int i = 0; i < 16; i++) { + file->store_32(0); // reserved + } + + // write the index file->store_32(files.size()); + FileAccessEncrypted *fae = nullptr; + FileAccess *fhead = file; + + if (enc_dir) { + fae = memnew(FileAccessEncrypted); + ERR_FAIL_COND_V(!fae, ERR_CANT_CREATE); + + Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false); + ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE); + + fhead = fae; + } + for (int i = 0; i < files.size(); i++) { - file->store_pascal_string(files[i].path); - files.write[i].offset_offset = file->get_position(); - file->store_64(0); // offset - file->store_64(files[i].size); // size - - // # empty md5 - file->store_32(0); - file->store_32(0); - file->store_32(0); - file->store_32(0); + int string_len = files[i].path.utf8().length(); + int pad = _get_pad(4, string_len); + + fhead->store_32(string_len + pad); + fhead->store_buffer((const uint8_t *)files[i].path.utf8().get_data(), string_len); + for (int j = 0; j < pad; j++) { + fhead->store_8(0); + } + + fhead->store_64(files[i].ofs); + fhead->store_64(files[i].size); // pay attention here, this is where file is + fhead->store_buffer(files[i].md5.ptr(), 16); //also save md5 for file + + uint32_t flags = 0; + if (files[i].encrypted) { + flags |= PACK_FILE_ENCRYPTED; + } + fhead->store_32(flags); + } + + if (fae) { + fae->release(); + memdelete(fae); } - uint64_t ofs = file->get_position(); - ofs = _align(ofs, alignment); + int header_padding = _get_pad(alignment, file->get_position()); + for (int i = 0; i < header_padding; i++) { + file->store_8(Math::rand() % 256); + } - _pad(file, ofs - file->get_position()); + int64_t file_base = file->get_position(); + file->seek(file_base_ofs); + file->store_64(file_base); // update files base + file->seek(file_base); const uint32_t buf_max = 65536; uint8_t *buf = memnew_arr(uint8_t, buf_max); @@ -137,26 +223,41 @@ Error PCKPacker::flush(bool p_verbose) { for (int i = 0; i < files.size(); i++) { FileAccess *src = FileAccess::open(files[i].src_path, FileAccess::READ); uint64_t to_write = files[i].size; + + fae = nullptr; + FileAccess *ftmp = file; + if (files[i].encrypted) { + fae = memnew(FileAccessEncrypted); + ERR_FAIL_COND_V(!fae, ERR_CANT_CREATE); + + Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false); + ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE); + ftmp = fae; + } + while (to_write > 0) { int read = src->get_buffer(buf, MIN(to_write, buf_max)); - file->store_buffer(buf, read); + ftmp->store_buffer(buf, read); to_write -= read; } - uint64_t pos = file->get_position(); - file->seek(files[i].offset_offset); // go back to store the file's offset - file->store_64(ofs); - file->seek(pos); + if (fae) { + fae->release(); + memdelete(fae); + } - ofs = _align(ofs + files[i].size, alignment); - _pad(file, ofs - pos); + int pad = _get_pad(alignment, file->get_position()); + for (int j = 0; j < pad; j++) { + file->store_8(Math::rand() % 256); + } src->close(); memdelete(src); count += 1; - if (p_verbose && files.size() > 0) { + const int file_num = files.size(); + if (p_verbose && (file_num > 0)) { if (count % 100 == 0) { - printf("%i/%i (%.2f)\r", count, files.size(), float(count) / files.size() * 100); + printf("%i/%i (%.2f)\r", count, file_num, float(count) / file_num * 100); fflush(stdout); } } diff --git a/core/io/pck_packer.h b/core/io/pck_packer.h index 2929967a68..56be1b52df 100644 --- a/core/io/pck_packer.h +++ b/core/io/pck_packer.h @@ -31,7 +31,7 @@ #ifndef PCK_PACKER_H #define PCK_PACKER_H -#include "core/reference.h" +#include "core/object/reference.h" class FileAccess; @@ -39,21 +39,27 @@ class PCKPacker : public Reference { GDCLASS(PCKPacker, Reference); FileAccess *file = nullptr; - int alignment; + int alignment = 0; + uint64_t ofs = 0; + + Vector<uint8_t> key; + bool enc_dir = false; static void _bind_methods(); struct File { String path; String src_path; - int size; - uint64_t offset_offset; + uint64_t ofs = 0; + uint64_t size = 0; + bool encrypted = false; + Vector<uint8_t> md5; }; Vector<File> files; public: - Error pck_start(const String &p_file, int p_alignment = 0); - Error add_file(const String &p_file, const String &p_src); + Error pck_start(const String &p_file, int p_alignment = 0, const String &p_key = String(), bool p_encrypt_directory = false); + Error add_file(const String &p_file, const String &p_src, bool p_encrypt = false); Error flush(bool p_verbose = false); PCKPacker() {} diff --git a/core/io/resource.cpp b/core/io/resource.cpp new file mode 100644 index 0000000000..58ab9a8cde --- /dev/null +++ b/core/io/resource.cpp @@ -0,0 +1,534 @@ +/*************************************************************************/ +/* resource.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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 "resource.h" + +#include "core/core_string_names.h" +#include "core/io/resource_loader.h" +#include "core/object/script_language.h" +#include "core/os/file_access.h" +#include "core/os/os.h" +#include "scene/main/node.h" //only so casting works + +#include <stdio.h> + +void Resource::emit_changed() { + emit_signal(CoreStringNames::get_singleton()->changed); +} + +void Resource::_resource_path_changed() { +} + +void Resource::set_path(const String &p_path, bool p_take_over) { + if (path_cache == p_path) { + return; + } + + if (path_cache != "") { + ResourceCache::lock->write_lock(); + ResourceCache::resources.erase(path_cache); + ResourceCache::lock->write_unlock(); + } + + path_cache = ""; + + ResourceCache::lock->read_lock(); + bool has_path = ResourceCache::resources.has(p_path); + ResourceCache::lock->read_unlock(); + + if (has_path) { + if (p_take_over) { + ResourceCache::lock->write_lock(); + Resource **res = ResourceCache::resources.getptr(p_path); + if (res) { + (*res)->set_name(""); + } + ResourceCache::lock->write_unlock(); + } else { + ResourceCache::lock->read_lock(); + bool exists = ResourceCache::resources.has(p_path); + ResourceCache::lock->read_unlock(); + + ERR_FAIL_COND_MSG(exists, "Another resource is loaded from path '" + p_path + "' (possible cyclic resource inclusion)."); + } + } + path_cache = p_path; + + if (path_cache != "") { + ResourceCache::lock->write_lock(); + ResourceCache::resources[path_cache] = this; + ResourceCache::lock->write_unlock(); + } + + _change_notify("resource_path"); + _resource_path_changed(); +} + +String Resource::get_path() const { + return path_cache; +} + +void Resource::set_subindex(int p_sub_index) { + subindex = p_sub_index; +} + +int Resource::get_subindex() const { + return subindex; +} + +void Resource::set_name(const String &p_name) { + name = p_name; + _change_notify("resource_name"); +} + +String Resource::get_name() const { + return name; +} + +bool Resource::editor_can_reload_from_file() { + return true; //by default yes +} + +void Resource::reload_from_file() { + String path = get_path(); + if (!path.is_resource_file()) { + return; + } + + Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), true); + + if (!s.is_valid()) { + return; + } + + List<PropertyInfo> pi; + s->get_property_list(&pi); + + for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) { + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { + continue; + } + if (E->get().name == "resource_path") { + continue; //do not change path + } + + set(E->get().name, s->get(E->get().name)); + } +} + +Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache) { + List<PropertyInfo> plist; + get_property_list(&plist); + + Ref<Resource> r = Object::cast_to<Resource>(ClassDB::instance(get_class())); + ERR_FAIL_COND_V(r.is_null(), Ref<Resource>()); + + r->local_scene = p_for_scene; + + for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { + continue; + } + Variant p = get(E->get().name); + if (p.get_type() == Variant::OBJECT) { + RES sr = p; + if (sr.is_valid()) { + if (sr->is_local_to_scene()) { + if (remap_cache.has(sr)) { + p = remap_cache[sr]; + } else { + RES dupe = sr->duplicate_for_local_scene(p_for_scene, remap_cache); + p = dupe; + remap_cache[sr] = dupe; + } + } + } + } + + r->set(E->get().name, p); + } + + return r; +} + +void Resource::configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache) { + List<PropertyInfo> plist; + get_property_list(&plist); + + local_scene = p_for_scene; + + for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { + continue; + } + Variant p = get(E->get().name); + if (p.get_type() == Variant::OBJECT) { + RES sr = p; + if (sr.is_valid()) { + if (sr->is_local_to_scene()) { + if (!remap_cache.has(sr)) { + sr->configure_for_local_scene(p_for_scene, remap_cache); + remap_cache[sr] = sr; + } + } + } + } + } +} + +Ref<Resource> Resource::duplicate(bool p_subresources) const { + List<PropertyInfo> plist; + get_property_list(&plist); + + Ref<Resource> r = (Resource *)ClassDB::instance(get_class()); + ERR_FAIL_COND_V(r.is_null(), Ref<Resource>()); + + for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { + continue; + } + Variant p = get(E->get().name); + + if ((p.get_type() == Variant::DICTIONARY || p.get_type() == Variant::ARRAY)) { + r->set(E->get().name, p.duplicate(p_subresources)); + } else if (p.get_type() == Variant::OBJECT && (p_subresources || (E->get().usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) { + RES sr = p; + if (sr.is_valid()) { + r->set(E->get().name, sr->duplicate(p_subresources)); + } + } else { + r->set(E->get().name, p); + } + } + + return r; +} + +void Resource::_set_path(const String &p_path) { + set_path(p_path, false); +} + +void Resource::_take_over_path(const String &p_path) { + set_path(p_path, true); +} + +RID Resource::get_rid() const { + return RID(); +} + +void Resource::register_owner(Object *p_owner) { + owners.insert(p_owner->get_instance_id()); +} + +void Resource::unregister_owner(Object *p_owner) { + owners.erase(p_owner->get_instance_id()); +} + +void Resource::notify_change_to_owners() { + for (Set<ObjectID>::Element *E = owners.front(); E; E = E->next()) { + Object *obj = ObjectDB::get_instance(E->get()); + ERR_CONTINUE_MSG(!obj, "Object was deleted, while still owning a resource."); //wtf + //TODO store string + obj->call("resource_changed", RES(this)); + } +} + +#ifdef TOOLS_ENABLED + +uint32_t Resource::hash_edited_version() const { + uint32_t hash = hash_djb2_one_32(get_edited_version()); + + List<PropertyInfo> plist; + get_property_list(&plist); + + for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { + if (E->get().usage & PROPERTY_USAGE_STORAGE && E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) { + RES res = get(E->get().name); + if (res.is_valid()) { + hash = hash_djb2_one_32(res->hash_edited_version(), hash); + } + } + } + + return hash; +} + +#endif + +void Resource::set_local_to_scene(bool p_enable) { + local_to_scene = p_enable; +} + +bool Resource::is_local_to_scene() const { + return local_to_scene; +} + +Node *Resource::get_local_scene() const { + if (local_scene) { + return local_scene; + } + + if (_get_local_scene_func) { + return _get_local_scene_func(); + } + + return nullptr; +} + +void Resource::setup_local_to_scene() { + if (get_script_instance()) { + get_script_instance()->call("_setup_local_to_scene"); + } +} + +Node *(*Resource::_get_local_scene_func)() = nullptr; + +void Resource::set_as_translation_remapped(bool p_remapped) { + if (remapped_list.in_list() == p_remapped) { + return; + } + + if (ResourceCache::lock) { + ResourceCache::lock->write_lock(); + } + + if (p_remapped) { + ResourceLoader::remapped_list.add(&remapped_list); + } else { + ResourceLoader::remapped_list.remove(&remapped_list); + } + + if (ResourceCache::lock) { + ResourceCache::lock->write_unlock(); + } +} + +bool Resource::is_translation_remapped() const { + return remapped_list.in_list(); +} + +#ifdef TOOLS_ENABLED +//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored +void Resource::set_id_for_path(const String &p_path, int p_id) { + if (p_id == -1) { + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->write_lock(); + } + ResourceCache::resource_path_cache[p_path].erase(get_path()); + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->write_unlock(); + } + } else { + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->write_lock(); + } + ResourceCache::resource_path_cache[p_path][get_path()] = p_id; + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->write_unlock(); + } + } +} + +int Resource::get_id_for_path(const String &p_path) const { + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->read_lock(); + } + if (ResourceCache::resource_path_cache[p_path].has(get_path())) { + int result = ResourceCache::resource_path_cache[p_path][get_path()]; + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->read_unlock(); + } + return result; + } else { + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->read_unlock(); + } + return -1; + } +} +#endif + +void Resource::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path); + ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path); + ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path); + ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name); + ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name); + ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid); + ClassDB::bind_method(D_METHOD("set_local_to_scene", "enable"), &Resource::set_local_to_scene); + ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene); + ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene); + ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene); + + ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false)); + ADD_SIGNAL(MethodInfo("changed")); + ADD_GROUP("Resource", "resource_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path"); + ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "resource_name"), "set_name", "get_name"); + + BIND_VMETHOD(MethodInfo("_setup_local_to_scene")); +} + +Resource::Resource() : + remapped_list(this) {} + +Resource::~Resource() { + if (path_cache != "") { + ResourceCache::lock->write_lock(); + ResourceCache::resources.erase(path_cache); + ResourceCache::lock->write_unlock(); + } + if (owners.size()) { + WARN_PRINT("Resource is still owned."); + } +} + +HashMap<String, Resource *> ResourceCache::resources; +#ifdef TOOLS_ENABLED +HashMap<String, HashMap<String, int>> ResourceCache::resource_path_cache; +#endif + +RWLock *ResourceCache::lock = nullptr; +#ifdef TOOLS_ENABLED +RWLock *ResourceCache::path_cache_lock = nullptr; +#endif + +void ResourceCache::setup() { + lock = RWLock::create(); +#ifdef TOOLS_ENABLED + path_cache_lock = RWLock::create(); +#endif +} + +void ResourceCache::clear() { + if (resources.size()) { + ERR_PRINT("Resources still in use at exit (run with --verbose for details)."); + if (OS::get_singleton()->is_stdout_verbose()) { + const String *K = nullptr; + while ((K = resources.next(K))) { + Resource *r = resources[*K]; + print_line(vformat("Resource still in use: %s (%s)", *K, r->get_class())); + } + } + } + + resources.clear(); + memdelete(lock); +#ifdef TOOLS_ENABLED + memdelete(path_cache_lock); +#endif +} + +void ResourceCache::reload_externals() { +} + +bool ResourceCache::has(const String &p_path) { + lock->read_lock(); + bool b = resources.has(p_path); + lock->read_unlock(); + + return b; +} + +Resource *ResourceCache::get(const String &p_path) { + lock->read_lock(); + + Resource **res = resources.getptr(p_path); + + lock->read_unlock(); + + if (!res) { + return nullptr; + } + + return *res; +} + +void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) { + lock->read_lock(); + const String *K = nullptr; + while ((K = resources.next(K))) { + Resource *r = resources[*K]; + p_resources->push_back(Ref<Resource>(r)); + } + lock->read_unlock(); +} + +int ResourceCache::get_cached_resource_count() { + lock->read_lock(); + int rc = resources.size(); + lock->read_unlock(); + + return rc; +} + +void ResourceCache::dump(const char *p_file, bool p_short) { +#ifdef DEBUG_ENABLED + lock->read_lock(); + + Map<String, int> type_count; + + FileAccess *f = nullptr; + if (p_file) { + f = FileAccess::open(p_file, FileAccess::WRITE); + ERR_FAIL_COND_MSG(!f, "Cannot create file at path '" + String(p_file) + "'."); + } + + const String *K = nullptr; + while ((K = resources.next(K))) { + Resource *r = resources[*K]; + + if (!type_count.has(r->get_class())) { + type_count[r->get_class()] = 0; + } + + type_count[r->get_class()]++; + + if (!p_short) { + if (f) { + f->store_line(r->get_class() + ": " + r->get_path()); + } + } + } + + for (Map<String, int>::Element *E = type_count.front(); E; E = E->next()) { + if (f) { + f->store_line(E->key() + " count: " + itos(E->get())); + } + } + if (f) { + f->close(); + memdelete(f); + } + + lock->read_unlock(); +#endif +} diff --git a/core/io/resource.h b/core/io/resource.h new file mode 100644 index 0000000000..6e0bd7d7f4 --- /dev/null +++ b/core/io/resource.h @@ -0,0 +1,172 @@ +/*************************************************************************/ +/* resource.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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 RESOURCE_H +#define RESOURCE_H + +#include "core/object/class_db.h" +#include "core/object/reference.h" +#include "core/templates/safe_refcount.h" +#include "core/templates/self_list.h" + +#define RES_BASE_EXTENSION(m_ext) \ +public: \ + static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension(m_ext, get_class_static()); } \ + virtual String get_base_extension() const override { return m_ext; } \ + \ +private: + +class Resource : public Reference { + GDCLASS(Resource, Reference); + OBJ_CATEGORY("Resources"); + +public: + static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension("res", get_class_static()); } + virtual String get_base_extension() const { return "res"; } + +private: + Set<ObjectID> owners; + + friend class ResBase; + friend class ResourceCache; + + String name; + String path_cache; + int subindex = 0; + + virtual bool _use_builtin_script() const { return true; } + +#ifdef TOOLS_ENABLED + uint64_t last_modified_time = 0; + uint64_t import_last_modified_time = 0; + String import_path; +#endif + + bool local_to_scene = false; + friend class SceneState; + Node *local_scene = nullptr; + + SelfList<Resource> remapped_list; + +protected: + void emit_changed(); + + void notify_change_to_owners(); + + virtual void _resource_path_changed(); + static void _bind_methods(); + + void _set_path(const String &p_path); + void _take_over_path(const String &p_path); + +public: + static Node *(*_get_local_scene_func)(); //used by editor + + virtual bool editor_can_reload_from_file(); + virtual void reload_from_file(); + + void register_owner(Object *p_owner); + void unregister_owner(Object *p_owner); + + void set_name(const String &p_name); + String get_name() const; + + virtual void set_path(const String &p_path, bool p_take_over = false); + String get_path() const; + + void set_subindex(int p_sub_index); + int get_subindex() const; + + virtual Ref<Resource> duplicate(bool p_subresources = false) const; + Ref<Resource> duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache); + void configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache); + + void set_local_to_scene(bool p_enable); + bool is_local_to_scene() const; + virtual void setup_local_to_scene(); + + Node *get_local_scene() const; + +#ifdef TOOLS_ENABLED + + uint32_t hash_edited_version() const; + + virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; } + uint64_t get_last_modified_time() const { return last_modified_time; } + + virtual void set_import_last_modified_time(uint64_t p_time) { import_last_modified_time = p_time; } + uint64_t get_import_last_modified_time() const { return import_last_modified_time; } + + void set_import_path(const String &p_path) { import_path = p_path; } + String get_import_path() const { return import_path; } + +#endif + + void set_as_translation_remapped(bool p_remapped); + bool is_translation_remapped() const; + + virtual RID get_rid() const; // some resources may offer conversion to RID + +#ifdef TOOLS_ENABLED + //helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored + void set_id_for_path(const String &p_path, int p_id); + int get_id_for_path(const String &p_path) const; +#endif + + Resource(); + ~Resource(); +}; + +typedef Ref<Resource> RES; + +class ResourceCache { + friend class Resource; + friend class ResourceLoader; //need the lock + static RWLock *lock; + static HashMap<String, Resource *> resources; +#ifdef TOOLS_ENABLED + static HashMap<String, HashMap<String, int>> resource_path_cache; // each tscn has a set of resource paths and IDs + static RWLock *path_cache_lock; +#endif // TOOLS_ENABLED + friend void unregister_core_types(); + static void clear(); + friend void register_core_types(); + static void setup(); + +public: + static void reload_externals(); + static bool has(const String &p_path); + static Resource *get(const String &p_path); + static void dump(const char *p_file = nullptr, bool p_short = false); + static void get_cached_resources(List<Ref<Resource>> *p_resources); + static int get_cached_resource_count(); +}; + +#endif // RESOURCE_H diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 5097f6d98b..aeb859aabd 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -30,18 +30,17 @@ #include "resource_format_binary.h" -#include "core/image.h" +#include "core/config/project_settings.h" #include "core/io/file_access_compressed.h" +#include "core/io/image.h" #include "core/io/marshalls.h" #include "core/os/dir_access.h" -#include "core/project_settings.h" #include "core/version.h" //#define print_bl(m_what) print_line(m_what) #define print_bl(m_what) (void)(m_what) enum { - //numbering must be different from variant, in case new variant types are added (variant must be always contiguous for jumptable optimization) VARIANT_NIL = 1, VARIANT_BOOL = 2, @@ -90,7 +89,6 @@ enum { FORMAT_VERSION = 3, FORMAT_VERSION_CAN_RENAME_DEPS = 1, FORMAT_VERSION_NO_NODEPATH_PROPERTY = 3, - }; void ResourceLoaderBinary::_advance_padding(uint32_t p_len) { @@ -863,7 +861,8 @@ void ResourceLoaderBinary::open(FileAccess *p_f) { if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) { f->close(); - ERR_FAIL_MSG("File format '" + itos(FORMAT_VERSION) + "." + itos(ver_major) + "." + itos(ver_minor) + "' is too new! Please upgrade to a new engine version: " + local_path + "."); + ERR_FAIL_MSG(vformat("File '%s' can't be loaded, as it uses a format version (%d) or engine version (%d.%d) which are not supported by your engine version (%s).", + local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH)); } type = get_unicode_string(); @@ -1136,7 +1135,9 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) { memdelete(f); memdelete(fw); - ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "File format '" + itos(FORMAT_VERSION) + "." + itos(ver_major) + "." + itos(ver_minor) + "' is too new! Please upgrade to a new engine version: " + local_path + "."); + ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, + vformat("File '%s' can't be loaded, as it uses a format version (%d) or engine version (%d.%d) which are not supported by your engine version (%s).", + local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH)); } // Since we're not actually converting the file contents, leave the version @@ -1464,7 +1465,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } } break; - case Variant::_RID: { + case Variant::RID: { f->store_32(VARIANT_RID); WARN_PRINT("Can't save RIDs."); RID val = p_property; diff --git a/core/io/resource_importer.cpp b/core/io/resource_importer.cpp index 9ed159bd20..9b14d2c763 100644 --- a/core/io/resource_importer.cpp +++ b/core/io/resource_importer.cpp @@ -30,8 +30,9 @@ #include "resource_importer.h" +#include "core/config/project_settings.h" #include "core/os/os.h" -#include "core/variant_parser.h" +#include "core/variant/variant_parser.h" bool ResourceFormatImporter::SortImporterByName::operator()(const Ref<ResourceImporter> &p_a, const Ref<ResourceImporter> &p_b) const { return p_a->get_importer_name() < p_b->get_importer_name(); @@ -191,10 +192,6 @@ bool ResourceFormatImporter::recognize_path(const String &p_path, const String & return FileAccess::exists(p_path + ".import"); } -bool ResourceFormatImporter::can_be_imported(const String &p_path) const { - return ResourceFormatLoader::recognize_path(p_path); -} - int ResourceFormatImporter::get_import_order(const String &p_path) const { Ref<ResourceImporter> importer; @@ -374,7 +371,7 @@ Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const St } String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const { - return "res://.import/" + p_for_file.get_file() + "-" + p_for_file.md5_text(); + return ProjectSettings::IMPORTED_FILES_PATH.plus_file(p_for_file.get_file() + "-" + p_for_file.md5_text()); } bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) const { diff --git a/core/io/resource_importer.h b/core/io/resource_importer.h index d31a9a0194..1b300bf656 100644 --- a/core/io/resource_importer.h +++ b/core/io/resource_importer.h @@ -70,7 +70,6 @@ public: virtual String get_import_group_file(const String &p_path) const; virtual bool exists(const String &p_path) const; - virtual bool can_be_imported(const String &p_path) const; virtual int get_import_order(const String &p_path) const; String get_internal_resource_path(const String &p_path) const; @@ -102,6 +101,7 @@ public: virtual String get_resource_type() const = 0; virtual float get_priority() const { return 1.0; } virtual int get_import_order() const { return 0; } + virtual int get_format_version() const { return 0; } struct ImportOption { PropertyInfo option; diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index 534f3e44de..a8ca6a817e 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -30,13 +30,13 @@ #include "resource_loader.h" +#include "core/config/project_settings.h" #include "core/io/resource_importer.h" #include "core/os/file_access.h" #include "core/os/os.h" -#include "core/print_string.h" -#include "core/project_settings.h" -#include "core/translation.h" -#include "core/variant_parser.h" +#include "core/string/print_string.h" +#include "core/string/translation.h" +#include "core/variant/variant_parser.h" #ifdef DEBUG_LOAD_THREADED #define print_lt(m_text) print_line(m_text) @@ -195,7 +195,8 @@ RES ResourceLoader::_load(const String &p_path, const String &p_original_path, c return res; } - ERR_FAIL_COND_V_MSG(found, RES(), "Failed loading resource: " + p_path + "."); + ERR_FAIL_COND_V_MSG(found, RES(), + vformat("Failed loading resource: %s. Make sure resources have been imported by opening the project in the editor at least once.", p_path)); #ifdef TOOLS_ENABLED FileAccessRef file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES); @@ -999,6 +1000,9 @@ void ResourceLoader::load_translation_remaps() { void ResourceLoader::clear_translation_remaps() { translation_remaps.clear(); + while (remapped_list.first() != nullptr) { + remapped_list.remove(remapped_list.first()); + } } void ResourceLoader::load_path_remaps() { @@ -1053,7 +1057,7 @@ bool ResourceLoader::add_custom_resource_format_loader(String script_path) { ERR_FAIL_COND_V_MSG(obj == nullptr, false, "Cannot instance script as custom resource loader, expected 'ResourceFormatLoader' inheritance, got: " + String(ibt) + "."); - ResourceFormatLoader *crl = Object::cast_to<ResourceFormatLoader>(obj); + Ref<ResourceFormatLoader> crl = Object::cast_to<ResourceFormatLoader>(obj); crl->set_script(s); ResourceLoader::add_resource_format_loader(crl); diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index 9322b5273a..02c668f214 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -31,9 +31,9 @@ #ifndef RESOURCE_LOADER_H #define RESOURCE_LOADER_H +#include "core/io/resource.h" #include "core/os/semaphore.h" #include "core/os/thread.h" -#include "core/resource.h" class ResourceFormatLoader : public Reference { GDCLASS(ResourceFormatLoader, Reference); diff --git a/core/io/resource_saver.cpp b/core/io/resource_saver.cpp index a8da215b61..6ded27d82f 100644 --- a/core/io/resource_saver.cpp +++ b/core/io/resource_saver.cpp @@ -29,10 +29,10 @@ /*************************************************************************/ #include "resource_saver.h" +#include "core/config/project_settings.h" #include "core/io/resource_loader.h" +#include "core/object/script_language.h" #include "core/os/file_access.h" -#include "core/project_settings.h" -#include "core/script_language.h" Ref<ResourceFormatSaver> ResourceSaver::saver[MAX_SAVERS]; @@ -214,7 +214,7 @@ bool ResourceSaver::add_custom_resource_format_saver(String script_path) { ERR_FAIL_COND_V_MSG(obj == nullptr, false, "Cannot instance script as custom resource saver, expected 'ResourceFormatSaver' inheritance, got: " + String(ibt) + "."); - ResourceFormatSaver *crl = Object::cast_to<ResourceFormatSaver>(obj); + Ref<ResourceFormatSaver> crl = Object::cast_to<ResourceFormatSaver>(obj); crl->set_script(s); ResourceSaver::add_resource_format_saver(crl); diff --git a/core/io/resource_saver.h b/core/io/resource_saver.h index 8b4cdd86f8..c724c4a6e5 100644 --- a/core/io/resource_saver.h +++ b/core/io/resource_saver.h @@ -31,7 +31,7 @@ #ifndef RESOURCE_SAVER_H #define RESOURCE_SAVER_H -#include "core/resource.h" +#include "core/io/resource.h" class ResourceFormatSaver : public Reference { GDCLASS(ResourceFormatSaver, Reference); @@ -63,7 +63,6 @@ class ResourceSaver { public: enum SaverFlags { - FLAG_RELATIVE_PATHS = 1, FLAG_BUNDLE_RESOURCES = 2, FLAG_CHANGE_PATH = 4, diff --git a/core/io/stream_peer.h b/core/io/stream_peer.h index 39097a57f2..8c1e918dd8 100644 --- a/core/io/stream_peer.h +++ b/core/io/stream_peer.h @@ -31,7 +31,7 @@ #ifndef STREAM_PEER_H #define STREAM_PEER_H -#include "core/reference.h" +#include "core/object/reference.h" class StreamPeer : public Reference { GDCLASS(StreamPeer, Reference); diff --git a/core/io/stream_peer_ssl.cpp b/core/io/stream_peer_ssl.cpp index 3dc31c6769..daf36a5350 100644 --- a/core/io/stream_peer_ssl.cpp +++ b/core/io/stream_peer_ssl.cpp @@ -30,7 +30,7 @@ #include "stream_peer_ssl.h" -#include "core/engine.h" +#include "core/config/engine.h" StreamPeerSSL *(*StreamPeerSSL::_create)() = nullptr; diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp index cce728c30a..aa9c409528 100644 --- a/core/io/stream_peer_tcp.cpp +++ b/core/io/stream_peer_tcp.cpp @@ -30,7 +30,7 @@ #include "stream_peer_tcp.h" -#include "core/project_settings.h" +#include "core/config/project_settings.h" Error StreamPeerTCP::_poll_connection() { ERR_FAIL_COND_V(status != STATUS_CONNECTING || !_sock.is_valid() || !_sock->is_open(), FAILED); diff --git a/core/io/stream_peer_tcp.h b/core/io/stream_peer_tcp.h index 45205866b4..173f92e2b6 100644 --- a/core/io/stream_peer_tcp.h +++ b/core/io/stream_peer_tcp.h @@ -42,7 +42,6 @@ class StreamPeerTCP : public StreamPeer { public: enum Status { - STATUS_NONE, STATUS_CONNECTING, STATUS_CONNECTED, diff --git a/core/io/translation_loader_po.cpp b/core/io/translation_loader_po.cpp index 11aeddee09..34cccca540 100644 --- a/core/io/translation_loader_po.cpp +++ b/core/io/translation_loader_po.cpp @@ -31,27 +31,35 @@ #include "translation_loader_po.h" #include "core/os/file_access.h" -#include "core/translation.h" +#include "core/string/translation.h" +#include "core/string/translation_po.h" RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { enum Status { STATUS_NONE, STATUS_READING_ID, STATUS_READING_STRING, + STATUS_READING_CONTEXT, + STATUS_READING_PLURAL, }; Status status = STATUS_NONE; String msg_id; String msg_str; + String msg_context; + Vector<String> msgs_plural; String config; if (r_error) { *r_error = ERR_FILE_CORRUPT; } - Ref<Translation> translation = Ref<Translation>(memnew(Translation)); + Ref<TranslationPO> translation = Ref<TranslationPO>(memnew(TranslationPO)); int line = 1; + int plural_forms = 0; + int plural_index = -1; + bool entered_context = false; bool skip_this = false; bool skip_next = false; bool is_eof = false; @@ -63,40 +71,107 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { // If we reached last line and it's not a content line, break, otherwise let processing that last loop if (is_eof && l.empty()) { - if (status == STATUS_READING_ID) { + if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || (status == STATUS_READING_PLURAL && plural_index != plural_forms - 1)) { memdelete(f); - ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading 'msgid' at: " + path + ":" + itos(line)); + ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading PO file at: " + path + ":" + itos(line)); } else { break; } } - if (l.begins_with("msgid")) { + if (l.begins_with("msgctxt")) { + if (status != STATUS_READING_STRING && status != STATUS_READING_PLURAL) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line)); + } + + // In PO file, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read + // and set "entered_context" to true to prevent adding twice. + if (!skip_this && msg_id != "") { + if (status == STATUS_READING_STRING) { + translation->add_message(msg_id, msg_str, msg_context); + } else if (status == STATUS_READING_PLURAL) { + if (plural_index != plural_forms - 1) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line)); + } + translation->add_plural_message(msg_id, msgs_plural, msg_context); + } + } + msg_context = ""; + l = l.substr(7, l.length()).strip_edges(); + status = STATUS_READING_CONTEXT; + entered_context = true; + } + + if (l.begins_with("msgid_plural")) { + if (plural_forms == 0) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "PO file uses 'msgid_plural' but 'Plural-Forms' is invalid or missing in header: " + path + ":" + itos(line)); + } else if (status != STATUS_READING_ID) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line)); + } + // We don't record the message in "msgid_plural" itself as tr_n(), TTRN(), RTRN() interfaces provide the plural string already. + // We just have to reset variables related to plurals for "msgstr[]" later on. + l = l.substr(12, l.length()).strip_edges(); + plural_index = -1; + msgs_plural.clear(); + msgs_plural.resize(plural_forms); + status = STATUS_READING_PLURAL; + } else if (l.begins_with("msgid")) { if (status == STATUS_READING_ID) { memdelete(f); ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line)); } if (msg_id != "") { - if (!skip_this) { - translation->add_message(msg_id, msg_str); + if (!skip_this && !entered_context) { + if (status == STATUS_READING_STRING) { + translation->add_message(msg_id, msg_str, msg_context); + } else if (status == STATUS_READING_PLURAL) { + if (plural_index != plural_forms - 1) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line)); + } + translation->add_plural_message(msg_id, msgs_plural, msg_context); + } } } else if (config == "") { config = msg_str; + // Record plural rule. + int p_start = config.find("Plural-Forms"); + if (p_start != -1) { + int p_end = config.find("\n", p_start); + translation->set_plural_rule(config.substr(p_start, p_end - p_start)); + plural_forms = translation->get_plural_forms(); + } } l = l.substr(5, l.length()).strip_edges(); status = STATUS_READING_ID; + // If we did not encounter msgctxt, we reset context to empty to reset it. + if (!entered_context) { + msg_context = ""; + } msg_id = ""; msg_str = ""; skip_this = skip_next; skip_next = false; + entered_context = false; } - if (l.begins_with("msgstr")) { + if (l.begins_with("msgstr[")) { + if (status != STATUS_READING_PLURAL) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line)); + } + plural_index++; // Increment to add to the next slot in vector msgs_plural. + l = l.substr(9, l.length()).strip_edges(); + } else if (l.begins_with("msgstr")) { if (status != STATUS_READING_ID) { memdelete(f); - ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' while parsing: " + path + ":" + itos(line)); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line)); } l = l.substr(6, l.length()).strip_edges(); @@ -108,7 +183,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { skip_next = true; } line++; - continue; //nothing to read or comment + continue; // Nothing to read or comment. } if (!l.begins_with("\"") || status == STATUS_NONE) { @@ -146,8 +221,12 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { if (status == STATUS_READING_ID) { msg_id += l; - } else { + } else if (status == STATUS_READING_STRING) { msg_str += l; + } else if (status == STATUS_READING_CONTEXT) { + msg_context += l; + } else if (status == STATUS_READING_PLURAL && plural_index >= 0) { + msgs_plural.write[plural_index] = msgs_plural[plural_index] + l; } line++; @@ -155,14 +234,23 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { memdelete(f); + // Add the last set of data from last iteration. if (status == STATUS_READING_STRING) { if (msg_id != "") { if (!skip_this) { - translation->add_message(msg_id, msg_str); + translation->add_message(msg_id, msg_str, msg_context); } } else if (config == "") { config = msg_str; } + } else if (status == STATUS_READING_PLURAL) { + if (!skip_this && msg_id != "") { + if (plural_index != plural_forms - 1) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line)); + } + translation->add_plural_message(msg_id, msgs_plural, msg_context); + } } ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + path + "."); diff --git a/core/io/translation_loader_po.h b/core/io/translation_loader_po.h index a196a37dc0..16c9f1e680 100644 --- a/core/io/translation_loader_po.h +++ b/core/io/translation_loader_po.h @@ -33,7 +33,7 @@ #include "core/io/resource_loader.h" #include "core/os/file_access.h" -#include "core/translation.h" +#include "core/string/translation.h" class TranslationLoaderPO : public ResourceFormatLoader { public: diff --git a/core/io/xml_parser.cpp b/core/io/xml_parser.cpp index b11267b60f..85143c0f04 100644 --- a/core/io/xml_parser.cpp +++ b/core/io/xml_parser.cpp @@ -30,13 +30,13 @@ #include "xml_parser.h" -#include "core/print_string.h" +#include "core/string/print_string.h" //#define DEBUG_XML VARIANT_ENUM_CAST(XMLParser::NodeType); -static bool _equalsn(const CharType *str1, const CharType *str2, int len) { +static bool _equalsn(const char32_t *str1, const char32_t *str2, int len) { int i; for (i = 0; i < len && str1[i] && str2[i]; ++i) { if (str1[i] != str2[i]) { @@ -64,7 +64,7 @@ String XMLParser::_replace_special_characters(const String &origstr) { int specialChar = -1; for (int i = 0; i < (int)special_characters.size(); ++i) { - const CharType *p = &origstr[pos] + 1; + const char32_t *p = &origstr[pos] + 1; if (_equalsn(&special_characters[i][1], p, special_characters[i].length() - 1)) { specialChar = i; diff --git a/core/io/xml_parser.h b/core/io/xml_parser.h index ee2174d52c..d8cc26b4c1 100644 --- a/core/io/xml_parser.h +++ b/core/io/xml_parser.h @@ -31,10 +31,10 @@ #ifndef XML_PARSER_H #define XML_PARSER_H +#include "core/object/reference.h" #include "core/os/file_access.h" -#include "core/reference.h" -#include "core/ustring.h" -#include "core/vector.h" +#include "core/string/ustring.h" +#include "core/templates/vector.h" /* Based on irrXML (see their zlib license). Added mainly for compatibility with their Collada loader. |