From 9b717b0a87775e3411544a64829b23e1d3e163ba Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 24 Apr 2022 09:49:59 +0200 Subject: [PATCH] Added reset method to the ImageIndexer, and made it's index_image method additive. --- modules/wfc/image_indexer.cpp | 18 ++++++++++-------- modules/wfc/image_indexer.h | 3 +++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/modules/wfc/image_indexer.cpp b/modules/wfc/image_indexer.cpp index e37d58fe5..be736ac5c 100644 --- a/modules/wfc/image_indexer.cpp +++ b/modules/wfc/image_indexer.cpp @@ -1,8 +1,6 @@ #include "image_indexer.h" -#include "core/oa_hash_map.h" - PoolColorArray ImageIndexer::get_colors() { return _colors; } @@ -16,8 +14,6 @@ void ImageIndexer::index_image(Ref image) { _colors.resize(0); _color_indices.resize(0); - OAHashMap col_map; - image->lock(); int w = image->get_width(); @@ -28,10 +24,10 @@ void ImageIndexer::index_image(Ref image) { Color c = image->get_pixel(x, y); int color_index = 0; - if (!col_map.lookup(c, color_index)) { + if (!_col_map.lookup(c, color_index)) { color_index = _colors.size(); _colors.push_back(c); - col_map.set(c, color_index); + _col_map.set(c, color_index); } _color_indices.push_back(color_index); @@ -41,6 +37,12 @@ void ImageIndexer::index_image(Ref image) { image->unlock(); } +void ImageIndexer::reset() { + _colors.resize(0); + _color_indices.resize(0); + _col_map.clear(); +} + PoolByteArray ImageIndexer::indices_to_argb8_data(const PoolIntArray &indices) { PoolByteArray arr; arr.resize(indices.size() * 4); @@ -71,6 +73,6 @@ ImageIndexer::~ImageIndexer() { void ImageIndexer::_bind_methods() { ClassDB::bind_method(D_METHOD("get_colors"), &ImageIndexer::get_colors); ClassDB::bind_method(D_METHOD("get_color_indices"), &ImageIndexer::get_color_indices); - ClassDB::bind_method(D_METHOD("index_image"), &ImageIndexer::index_image); - ClassDB::bind_method(D_METHOD("indices_to_argb8_data"), &ImageIndexer::indices_to_argb8_data); + ClassDB::bind_method(D_METHOD("index_image", "image"), &ImageIndexer::index_image); + ClassDB::bind_method(D_METHOD("reset"), &ImageIndexer::reset); } diff --git a/modules/wfc/image_indexer.h b/modules/wfc/image_indexer.h index 5e4476198..0d2569882 100644 --- a/modules/wfc/image_indexer.h +++ b/modules/wfc/image_indexer.h @@ -4,6 +4,7 @@ #include "core/image.h" #include "core/reference.h" #include "core/variant.h" +#include "core/oa_hash_map.h" class ImageIndexer : public Reference { GDCLASS(ImageIndexer, Reference); @@ -13,6 +14,7 @@ public: PoolIntArray get_color_indices(); void index_image(Ref image); + void reset(); PoolByteArray indices_to_argb8_data(const PoolIntArray &indices); @@ -25,6 +27,7 @@ protected: private: PoolColorArray _colors; PoolIntArray _color_indices; + OAHashMap _col_map; }; #endif