summaryrefslogtreecommitdiff
path: root/core/io
diff options
context:
space:
mode:
Diffstat (limited to 'core/io')
-rw-r--r--core/io/compression.cpp10
-rw-r--r--core/io/compression.h6
-rw-r--r--core/io/config_file.cpp8
-rw-r--r--core/io/config_file.h10
-rw-r--r--core/io/dtls_server.cpp6
-rw-r--r--core/io/dtls_server.h4
-rw-r--r--core/io/file_access_buffered.cpp150
-rw-r--r--core/io/file_access_buffered.h91
-rw-r--r--core/io/file_access_buffered_fa.h139
-rw-r--r--core/io/file_access_compressed.cpp10
-rw-r--r--core/io/file_access_compressed.h4
-rw-r--r--core/io/file_access_encrypted.cpp14
-rw-r--r--core/io/file_access_encrypted.h8
-rw-r--r--core/io/file_access_memory.cpp8
-rw-r--r--core/io/file_access_memory.h8
-rw-r--r--core/io/file_access_network.cpp10
-rw-r--r--core/io/file_access_network.h4
-rw-r--r--core/io/file_access_pack.cpp38
-rw-r--r--core/io/file_access_pack.h34
-rw-r--r--core/io/file_access_zip.cpp5
-rw-r--r--core/io/file_access_zip.h8
-rw-r--r--core/io/http_client.cpp63
-rw-r--r--core/io/http_client.h13
-rw-r--r--core/io/image.cpp3602
-rw-r--r--core/io/image.h413
-rw-r--r--core/io/image_loader.cpp6
-rw-r--r--core/io/image_loader.h10
-rw-r--r--core/io/ip.cpp8
-rw-r--r--core/io/ip.h6
-rw-r--r--core/io/ip_address.cpp5
-rw-r--r--core/io/ip_address.h6
-rw-r--r--core/io/json.cpp42
-rw-r--r--core/io/json.h30
-rw-r--r--core/io/logger.cpp28
-rw-r--r--core/io/logger.h8
-rw-r--r--core/io/marshalls.cpp17
-rw-r--r--core/io/marshalls.h8
-rw-r--r--core/io/multiplayer_api.cpp10
-rw-r--r--core/io/multiplayer_api.h8
-rw-r--r--core/io/net_socket.cpp4
-rw-r--r--core/io/net_socket.h6
-rw-r--r--core/io/networked_multiplayer_peer.cpp4
-rw-r--r--core/io/networked_multiplayer_peer.h4
-rw-r--r--core/io/packed_data_container.cpp395
-rw-r--r--core/io/packed_data_container.h105
-rw-r--r--core/io/packet_peer.cpp15
-rw-r--r--core/io/packet_peer.h9
-rw-r--r--core/io/packet_peer_dtls.cpp6
-rw-r--r--core/io/packet_peer_dtls.h4
-rw-r--r--core/io/packet_peer_udp.cpp5
-rw-r--r--core/io/packet_peer_udp.h4
-rw-r--r--core/io/pck_packer.cpp6
-rw-r--r--core/io/pck_packer.h14
-rw-r--r--core/io/resource.cpp534
-rw-r--r--core/io/resource.h172
-rw-r--r--core/io/resource_format_binary.cpp12
-rw-r--r--core/io/resource_format_binary.h4
-rw-r--r--core/io/resource_importer.cpp12
-rw-r--r--core/io/resource_importer.h6
-rw-r--r--core/io/resource_loader.cpp14
-rw-r--r--core/io/resource_loader.h6
-rw-r--r--core/io/resource_saver.cpp10
-rw-r--r--core/io/resource_saver.h7
-rw-r--r--core/io/stream_peer.cpp4
-rw-r--r--core/io/stream_peer.h6
-rw-r--r--core/io/stream_peer_ssl.cpp6
-rw-r--r--core/io/stream_peer_ssl.h4
-rw-r--r--core/io/stream_peer_tcp.cpp6
-rw-r--r--core/io/stream_peer_tcp.h5
-rw-r--r--core/io/tcp_server.cpp4
-rw-r--r--core/io/tcp_server.h4
-rw-r--r--core/io/translation_loader_po.cpp10
-rw-r--r--core/io/translation_loader_po.h6
-rw-r--r--core/io/udp_server.cpp4
-rw-r--r--core/io/udp_server.h4
-rw-r--r--core/io/xml_parser.cpp6
-rw-r--r--core/io/xml_parser.h10
-rw-r--r--core/io/zip_io.cpp4
-rw-r--r--core/io/zip_io.h4
79 files changed, 5624 insertions, 679 deletions
diff --git a/core/io/compression.cpp b/core/io/compression.cpp
index 7480262835..456023e2a6 100644
--- a/core/io/compression.cpp
+++ b/core/io/compression.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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"
@@ -257,13 +257,13 @@ int Compression::decompress_dynamic(Vector<uint8_t> *p_dst_vect, int p_max_dst_s
} while (ret != Z_STREAM_END);
// If all done successfully, resize the output if it's larger than the actual output
- if (ret == Z_STREAM_END && (unsigned long)p_dst_vect->size() > strm.total_out) {
+ 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 ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
+ return Z_OK;
}
int Compression::zlib_level = Z_DEFAULT_COMPRESSION;
diff --git a/core/io/compression.h b/core/io/compression.h
index c103fa8eae..cbfed74124 100644
--- a/core/io/compression.h
+++ b/core/io/compression.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -31,8 +31,8 @@
#ifndef COMPRESSION_H
#define COMPRESSION_H
+#include "core/templates/vector.h"
#include "core/typedefs.h"
-#include "core/vector.h"
class Compression {
public:
diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp
index 1af9142317..015c1f0d90 100644
--- a/core/io/config_file.cpp
+++ b/core/io/config_file.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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;
@@ -67,7 +67,7 @@ void ConfigFile::set_value(const String &p_section, const String &p_key, const V
return; // ?
}
values[p_section].erase(p_key);
- if (values[p_section].empty()) {
+ if (values[p_section].is_empty()) {
values.erase(p_section);
}
diff --git a/core/io/config_file.h b/core/io/config_file.h
index ae06960f02..386d304f07 100644
--- a/core/io/config_file.h
+++ b/core/io/config_file.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..288b2efe0e 100644
--- a/core/io/dtls_server.cpp
+++ b/core/io/dtls_server.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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/dtls_server.h b/core/io/dtls_server.h
index ae1d3bcd98..92b6caf508 100644
--- a/core/io/dtls_server.h
+++ b/core/io/dtls_server.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
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.h b/core/io/file_access_buffered.h
deleted file mode 100644
index 99d5ce903d..0000000000
--- a/core/io/file_access_buffered.h
+++ /dev/null
@@ -1,91 +0,0 @@
-/*************************************************************************/
-/* file_access_buffered.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_H
-#define FILE_ACCESS_BUFFERED_H
-
-#include "core/os/file_access.h"
-
-#include "core/ustring.h"
-
-class FileAccessBuffered : public FileAccess {
-public:
- enum {
- DEFAULT_CACHE_SIZE = 128 * 1024,
- };
-
-private:
- int cache_size = DEFAULT_CACHE_SIZE;
-
- int cache_data_left() const;
- mutable Error last_error;
-
-protected:
- Error set_error(Error p_error) const;
-
- mutable struct File {
- bool open = false;
- int size = 0;
- int offset = 0;
- String name;
- int access_flags = 0;
- } file;
-
- mutable struct Cache {
- Vector<uint8_t> buffer;
- int offset = 0;
- } cache;
-
- virtual int read_data_block(int p_offset, int p_size, uint8_t *p_dest = nullptr) const = 0;
-
- void set_cache_size(int p_size);
- int get_cache_size();
-
-public:
- virtual size_t get_position() const; ///< get position in the file
- virtual size_t get_len() const; ///< get size of the file
-
- 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
-
- virtual bool eof_reached() const;
-
- virtual uint8_t get_8() const;
- virtual int get_buffer(uint8_t *p_dest, int p_length) const; ///< get an array of bytes
-
- virtual bool is_open() const;
-
- virtual Error get_error() const;
-
- FileAccessBuffered() {}
- virtual ~FileAccessBuffered() {}
-};
-
-#endif
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..9ec2b27e88 100644
--- a/core/io/file_access_compressed.cpp
+++ b/core/io/file_access_compressed.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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_compressed.h b/core/io/file_access_compressed.h
index 52284b347e..118d05ea57 100644
--- a/core/io/file_access_compressed.h
+++ b/core/io/file_access_compressed.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp
index eb684f457e..8b4c57ce64 100644
--- a/core/io/file_access_encrypted.cpp
+++ b/core/io/file_access_encrypted.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -32,8 +32,8 @@
#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>
@@ -256,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++) {
@@ -272,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 fddc6842f3..969052d04f 100644
--- a/core/io/file_access_encrypted.h
+++ b/core/io/file_access_encrypted.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -47,8 +47,8 @@ private:
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;
diff --git a/core/io/file_access_memory.cpp b/core/io/file_access_memory.cpp
index a65ff92a89..04270de77f 100644
--- a/core/io/file_access_memory.cpp
+++ b/core/io/file_access_memory.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..0e3b0ad7b1 100644
--- a/core/io/file_access_memory.h
+++ b/core/io/file_access_memory.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..1d9aa846eb 100644
--- a/core/io/file_access_network.cpp
+++ b/core/io/file_access_network.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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());
@@ -350,7 +350,7 @@ void FileAccessNetwork::_queue_page(int p_page) const {
if (p_page >= pages.size()) {
return;
}
- if (pages[p_page].buffer.empty() && !pages[p_page].queued) {
+ if (pages[p_page].buffer.is_empty() && !pages[p_page].queued) {
FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton;
{
MutexLock lock(nc->blockrequest_mutex);
@@ -386,7 +386,7 @@ int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const {
if (page != last_page) {
buffer_mutex.lock();
- if (pages[page].buffer.empty()) {
+ if (pages[page].buffer.is_empty()) {
waiting_on_page = page;
for (int j = 0; j < read_ahead; j++) {
_queue_page(page + j);
diff --git a/core/io/file_access_network.h b/core/io/file_access_network.h
index dc5ce1e883..6aec2869fc 100644
--- a/core/io/file_access_network.h
+++ b/core/io/file_access_network.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp
index 8fdbb650d4..faf4fca14f 100644
--- a/core/io/file_access_pack.cpp
+++ b/core/io/file_access_pack.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -31,7 +31,7 @@
#include "file_access_pack.h"
#include "core/io/file_access_encrypted.h"
-#include "core/script_language.h"
+#include "core/object/script_language.h"
#include "core/version.h"
#include <stdio.h>
@@ -89,7 +89,7 @@ void PackedData::add_path(const String &pkg_path, const String &path, uint64_t o
}
String filename = path.get_file();
// Don't add as a file if the path points to a directory
- if (!filename.empty()) {
+ if (!filename.is_empty()) {
cd->files.insert(filename);
}
}
@@ -442,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://", "");
@@ -483,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) {
@@ -507,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 d934b0deb5..3c84e6b656 100644
--- a/core/io/file_access_pack.h
+++ b/core/io/file_access_pack.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -31,11 +31,11 @@
#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
@@ -122,6 +122,9 @@ public:
_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();
};
@@ -199,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;
@@ -206,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();
@@ -235,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 ce402fe8ed..01f9337a80 100644
--- a/core/io/file_access_zip.cpp
+++ b/core/io/file_access_zip.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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 {
diff --git a/core/io/file_access_zip.h b/core/io/file_access_zip.h
index c251b3c424..8559f871ce 100644
--- a/core/io/file_access_zip.h
+++ b/core/io/file_access_zip.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -34,7 +34,7 @@
#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;
diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp
index a25413b21b..a2fcf074ae 100644
--- a/core/io/http_client.cpp
+++ b/core/io/http_client.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -109,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;
}
@@ -137,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?
@@ -178,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;
}
@@ -204,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..ec4b86b26f 100644
--- a/core/io/http_client.h
+++ b/core/io/http_client.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..986c29b539
--- /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-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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]->is_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 normal map
+ int normal_w = 0, normal_h = 0;
+
+ ERR_FAIL_COND_V_MSG(p_normal_map.is_null() || p_normal_map->is_empty(), ERR_INVALID_PARAMETER, "Must provide a valid normal map 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 (is_empty()) {
+ return;
+ }
+
+ int ofs, w, h;
+ _get_mipmap_offset_and_size(1, ofs, w, h);
+ data.resize(ofs);
+
+ mipmaps = false;
+}
+
+bool Image::is_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::is_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("normal_map_to_xy"), &Image::normal_map_to_xy);
+ ClassDB::bind_method(D_METHOD("rgbe_to_srgb"), &Image::rgbe_to_srgb);
+ ClassDB::bind_method(D_METHOD("bump_map_to_normal_map", "bump_scale"), &Image::bump_map_to_normal_map, 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::normal_map_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::bump_map_to_normal_map(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 (is_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..b894be7df4
--- /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-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 is_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 normal_map_to_xy();
+ Ref<Image> rgbe_to_srgb();
+ Ref<Image> get_image_from_mipmap(int p_mipamp) const;
+ void bump_map_to_normal_map(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..8ca1cb3beb 100644
--- a/core/io/image_loader.cpp
+++ b/core/io/image_loader.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..bf67e1486f 100644
--- a/core/io/image_loader.h
+++ b/core/io/image_loader.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..6fb812e78d 100644
--- a/core/io/ip.cpp
+++ b/core/io/ip.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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);
@@ -189,7 +189,7 @@ void IP::erase_resolve_item(ResolverID p_id) {
void IP::clear_cache(const String &p_hostname) {
MutexLock lock(resolver->mutex);
- if (p_hostname.empty()) {
+ if (p_hostname.is_empty()) {
resolver->cache.clear();
} else {
resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE));
diff --git a/core/io/ip.h b/core/io/ip.h
index d434d02f9b..ae080b8e26 100644
--- a/core/io/ip.h
+++ b/core/io/ip.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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 d0fb63b958..5f98eb69e8 100644
--- a/core/io/ip_address.cpp
+++ b/core/io/ip_address.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -31,7 +31,6 @@
#include "ip_address.h"
/*
IP_Address::operator Variant() const {
-
return operator String();
}*/
diff --git a/core/io/ip_address.h b/core/io/ip_address.h
index 2f8f83503e..49bf83d72f 100644
--- a/core/io/ip_address.h
+++ b/core/io/ip_address.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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 1b89d966fd..bc4527869b 100644
--- a/core/io/json.cpp
+++ b/core/io/json.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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] = {
"'{'",
@@ -47,7 +47,7 @@ const char *JSON::tk_name[TK_MAX] = {
static String _make_indent(const String &p_indent, int p_size) {
String indent_text = "";
- if (!p_indent.empty()) {
+ if (!p_indent.is_empty()) {
for (int i = 0; i < p_size; i++) {
indent_text += p_indent;
}
@@ -59,7 +59,7 @@ String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_
String colon = ":";
String end_statement = "";
- if (!p_indent.empty()) {
+ if (!p_indent.is_empty()) {
colon += " ";
end_statement += "\n";
}
@@ -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 9122228163..537477666e 100644
--- a/core/io/json.h
+++ b/core/io/json.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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,
@@ -75,4 +74,25 @@ public:
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..da200f5717 100644
--- a/core/io/logger.cpp
+++ b/core/io/logger.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..b8e615b436 100644
--- a/core/io/logger.h
+++ b/core/io/logger.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..218a612da2 100644
--- a/core/io/marshalls.cpp
+++ b/core/io/marshalls.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..cc0e9ba301 100644
--- a/core/io/marshalls.h
+++ b/core/io/marshalls.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..6b550e69c8 100644
--- a/core/io/multiplayer_api.cpp
+++ b/core/io/multiplayer_api.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..7f88b53a27 100644
--- a/core/io/multiplayer_api.h
+++ b/core/io/multiplayer_api.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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.cpp b/core/io/net_socket.cpp
index 130a2e245e..b51d26ba83 100644
--- a/core/io/net_socket.cpp
+++ b/core/io/net_socket.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/net_socket.h b/core/io/net_socket.h
index 746945eced..bc09477693 100644
--- a/core/io/net_socket.h
+++ b/core/io/net_socket.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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/networked_multiplayer_peer.cpp b/core/io/networked_multiplayer_peer.cpp
index f521f2bb79..b6af046e77 100644
--- a/core/io/networked_multiplayer_peer.cpp
+++ b/core/io/networked_multiplayer_peer.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/networked_multiplayer_peer.h b/core/io/networked_multiplayer_peer.h
index dc76237f45..7c90f97d88 100644
--- a/core/io/networked_multiplayer_peer.h
+++ b/core/io/networked_multiplayer_peer.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/packed_data_container.cpp b/core/io/packed_data_container.cpp
new file mode 100644
index 0000000000..a0b97772e6
--- /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-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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/packed_data_container.h b/core/io/packed_data_container.h
new file mode 100644
index 0000000000..7791e21bb3
--- /dev/null
+++ b/core/io/packed_data_container.h
@@ -0,0 +1,105 @@
+/*************************************************************************/
+/* packed_data_container.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 PACKED_DATA_CONTAINER_H
+#define PACKED_DATA_CONTAINER_H
+
+#include "core/io/resource.h"
+
+class PackedDataContainer : public Resource {
+ GDCLASS(PackedDataContainer, Resource);
+
+ enum {
+ TYPE_DICT = 0xFFFFFFFF,
+ TYPE_ARRAY = 0xFFFFFFFE,
+ };
+
+ struct DictKey {
+ uint32_t hash;
+ Variant key;
+ bool operator<(const DictKey &p_key) const { return hash < p_key.hash; }
+ };
+
+ Vector<uint8_t> data;
+ int datalen = 0;
+
+ uint32_t _pack(const Variant &p_data, Vector<uint8_t> &tmpdata, Map<String, uint32_t> &string_cache);
+
+ 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);
+
+ Variant _iter_init(const Array &p_iter);
+ Variant _iter_next(const Array &p_iter);
+ Variant _iter_get(const Variant &p_iter);
+
+ 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;
+
+protected:
+ void _set_data(const Vector<uint8_t> &p_data);
+ Vector<uint8_t> _get_data() const;
+ static void _bind_methods();
+
+public:
+ virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override;
+ Error pack(const Variant &p_data);
+
+ int size() const;
+
+ PackedDataContainer() {}
+};
+
+class PackedDataContainerRef : public Reference {
+ GDCLASS(PackedDataContainerRef, Reference);
+
+ 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;
+
+ int size() const;
+ virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override;
+
+ PackedDataContainerRef() {}
+};
+
+#endif // PACKED_DATA_CONTAINER_H
diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp
index dacd548a3e..318fd10243 100644
--- a/core/io/packet_peer.cpp
+++ b/core/io/packet_peer.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..9e03c44750 100644
--- a/core/io/packet_peer.h
+++ b/core/io/packet_peer.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..bac98e20e7 100644
--- a/core/io/packet_peer_dtls.cpp
+++ b/core/io/packet_peer_dtls.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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_dtls.h b/core/io/packet_peer_dtls.h
index c2ff4e1a7f..31c52f539f 100644
--- a/core/io/packet_peer_dtls.h
+++ b/core/io/packet_peer_dtls.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/packet_peer_udp.cpp b/core/io/packet_peer_udp.cpp
index e633a56d54..d8d63d976f 100644
--- a/core/io/packet_peer_udp.cpp
+++ b/core/io/packet_peer_udp.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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/packet_peer_udp.h b/core/io/packet_peer_udp.h
index 9a44a1ebea..4bac6994fc 100644
--- a/core/io/packet_peer_udp.h
+++ b/core/io/packet_peer_udp.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/pck_packer.cpp b/core/io/pck_packer.cpp
index 5480d3c535..a0697ca18b 100644
--- a/core/io/pck_packer.cpp
+++ b/core/io/pck_packer.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -53,7 +53,7 @@ void PCKPacker::_bind_methods() {
}
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).");
+ ERR_FAIL_COND_V_MSG((p_key.is_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);
diff --git a/core/io/pck_packer.h b/core/io/pck_packer.h
index a6054dff2c..dec8f8748d 100644
--- a/core/io/pck_packer.h
+++ b/core/io/pck_packer.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -31,7 +31,7 @@
#ifndef PCK_PACKER_H
#define PCK_PACKER_H
-#include "core/reference.h"
+#include "core/object/reference.h"
class FileAccess;
@@ -39,7 +39,7 @@ class PCKPacker : public Reference {
GDCLASS(PCKPacker, Reference);
FileAccess *file = nullptr;
- int alignment;
+ int alignment = 0;
uint64_t ofs = 0;
Vector<uint8_t> key;
@@ -50,9 +50,9 @@ class PCKPacker : public Reference {
struct File {
String path;
String src_path;
- uint64_t ofs;
- uint64_t size;
- bool encrypted;
+ uint64_t ofs = 0;
+ uint64_t size = 0;
+ bool encrypted = false;
Vector<uint8_t> md5;
};
Vector<File> files;
diff --git a/core/io/resource.cpp b/core/io/resource.cpp
new file mode 100644
index 0000000000..716da5e395
--- /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-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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..eda18a8538
--- /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-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 21de7835ce..ae4643a19f 100644
--- a/core/io/resource_format_binary.cpp
+++ b/core/io/resource_format_binary.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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) {
@@ -1467,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_format_binary.h b/core/io/resource_format_binary.h
index 54cddca49e..428725f1d2 100644
--- a/core/io/resource_format_binary.h
+++ b/core/io/resource_format_binary.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/resource_importer.cpp b/core/io/resource_importer.cpp
index 4d980bcf1a..d86877ee14 100644
--- a/core/io/resource_importer.cpp
+++ b/core/io/resource_importer.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -30,9 +30,9 @@
#include "resource_importer.h"
+#include "core/config/project_settings.h"
#include "core/os/os.h"
-#include "core/project_settings.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();
@@ -192,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;
diff --git a/core/io/resource_importer.h b/core/io/resource_importer.h
index d31a9a0194..d31273e3cb 100644
--- a/core/io/resource_importer.h
+++ b/core/io/resource_importer.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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 b5c598e860..821e468aee 100644
--- a/core/io/resource_loader.cpp
+++ b/core/io/resource_loader.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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)
@@ -1057,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..dbf6be46c5 100644
--- a/core/io/resource_loader.h
+++ b/core/io/resource_loader.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..7ebc7f34b3 100644
--- a/core/io/resource_saver.cpp
+++ b/core/io/resource_saver.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..2c9e8f1aa3 100644
--- a/core/io/resource_saver.h
+++ b/core/io/resource_saver.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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.cpp b/core/io/stream_peer.cpp
index 403f61bb24..8407d55196 100644
--- a/core/io/stream_peer.cpp
+++ b/core/io/stream_peer.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/stream_peer.h b/core/io/stream_peer.h
index 39097a57f2..dadedbd2dc 100644
--- a/core/io/stream_peer.h
+++ b/core/io/stream_peer.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..6f5a06474c 100644
--- a/core/io/stream_peer_ssl.cpp
+++ b/core/io/stream_peer_ssl.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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_ssl.h b/core/io/stream_peer_ssl.h
index 81b95b856d..1df9ced08c 100644
--- a/core/io/stream_peer_ssl.h
+++ b/core/io/stream_peer_ssl.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp
index cce728c30a..760710a9eb 100644
--- a/core/io/stream_peer_tcp.cpp
+++ b/core/io/stream_peer_tcp.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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..10b90908d4 100644
--- a/core/io/stream_peer_tcp.h
+++ b/core/io/stream_peer_tcp.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -42,7 +42,6 @@ class StreamPeerTCP : public StreamPeer {
public:
enum Status {
-
STATUS_NONE,
STATUS_CONNECTING,
STATUS_CONNECTED,
diff --git a/core/io/tcp_server.cpp b/core/io/tcp_server.cpp
index d7061b6bf4..323d2bbd7f 100644
--- a/core/io/tcp_server.cpp
+++ b/core/io/tcp_server.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/tcp_server.h b/core/io/tcp_server.h
index eb715a745c..f06ddd2d99 100644
--- a/core/io/tcp_server.h
+++ b/core/io/tcp_server.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/translation_loader_po.cpp b/core/io/translation_loader_po.cpp
index d8ddb213c3..ce2c3eb1cd 100644
--- a/core/io/translation_loader_po.cpp
+++ b/core/io/translation_loader_po.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -31,8 +31,8 @@
#include "translation_loader_po.h"
#include "core/os/file_access.h"
-#include "core/translation.h"
-#include "core/translation_po.h"
+#include "core/string/translation.h"
+#include "core/string/translation_po.h"
RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
enum Status {
@@ -70,7 +70,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
is_eof = f->eof_reached();
// 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 (is_eof && l.is_empty()) {
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 PO file at: " + path + ":" + itos(line));
diff --git a/core/io/translation_loader_po.h b/core/io/translation_loader_po.h
index a196a37dc0..a524972588 100644
--- a/core/io/translation_loader_po.h
+++ b/core/io/translation_loader_po.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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/udp_server.cpp b/core/io/udp_server.cpp
index acd15aadc6..f56fb431ef 100644
--- a/core/io/udp_server.cpp
+++ b/core/io/udp_server.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/udp_server.h b/core/io/udp_server.h
index 3175b09b19..bbd2f951c9 100644
--- a/core/io/udp_server.h
+++ b/core/io/udp_server.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/xml_parser.cpp b/core/io/xml_parser.cpp
index fc75ac7d1e..905be6089d 100644
--- a/core/io/xml_parser.cpp
+++ b/core/io/xml_parser.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -30,7 +30,7 @@
#include "xml_parser.h"
-#include "core/print_string.h"
+#include "core/string/print_string.h"
//#define DEBUG_XML
diff --git a/core/io/xml_parser.h b/core/io/xml_parser.h
index ee2174d52c..01af6a90ad 100644
--- a/core/io/xml_parser.h
+++ b/core/io/xml_parser.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
@@ -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.
diff --git a/core/io/zip_io.cpp b/core/io/zip_io.cpp
index b8e7fd34d0..4b4a46e198 100644
--- a/core/io/zip_io.cpp
+++ b/core/io/zip_io.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */
diff --git a/core/io/zip_io.h b/core/io/zip_io.h
index ba2065b5d1..52691c65e9 100644
--- a/core/io/zip_io.h
+++ b/core/io/zip_io.h
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 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 */