diff --git a/mlpp/lin_alg/mlpp_tensor3.cpp b/mlpp/lin_alg/mlpp_tensor3.cpp index d9b4e95..a62ea2a 100644 --- a/mlpp/lin_alg/mlpp_tensor3.cpp +++ b/mlpp/lin_alg/mlpp_tensor3.cpp @@ -1,6 +1,129 @@ #include "mlpp_tensor3.h" +#include "core/io/image.h" + +void MLPPTensor3::add_feature_maps_image(const Ref &p_img, const int p_channels) { + ERR_FAIL_COND(!p_img.is_valid()); + + Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); + + int channel_count = 0; + int channels[4]; + + if (p_channels & IMAGE_CHANNEL_R) { + channels[channel_count] = 0; + ++channel_count; + } + + if (p_channels & IMAGE_CHANNEL_G) { + channels[channel_count] = 1; + ++channel_count; + } + + if (p_channels & IMAGE_CHANNEL_B) { + channels[channel_count] = 2; + ++channel_count; + } + + if (p_channels & IMAGE_CHANNEL_A) { + channels[channel_count] = 3; + ++channel_count; + } + + ERR_FAIL_COND(channel_count == 0); + + if (unlikely(_size == Size3i())) { + resize(Size3i(img_size.x, img_size.y, channel_count)); + } + + Size2i fms = feature_map_size(); + + ERR_FAIL_COND(img_size != fms); + + int start_channel = _size.y; + + _size.y += channel_count; + + resize(_size); + + Ref img = p_img; + + img->lock(); + + for (int y = 0; y < fms.y; ++y) { + for (int x = 0; x < fms.x; ++x) { + Color c = img->get_pixel(x, y); + + for (int i = 0; i < channel_count; ++i) { + set_element(y, x, start_channel + i, c[channels[i]]); + } + } + } + + img->unlock(); +} + +Ref MLPPTensor3::get_feature_map_image(const int p_index_z) { + ERR_FAIL_INDEX_V(p_index_z, _size.z, Ref()); + + Ref image; + image.instance(); + + if (data_size() == 0) { + return image; + } + + PoolByteArray arr; + + int fmsi = calculate_feature_map_index(p_index_z); + int fms = feature_map_data_size(); + + arr.resize(fms); + + PoolByteArray::Write w = arr.write(); + uint8_t *wptr = w.ptr(); + + for (int i = 0; i < fms; ++i) { + wptr[i] = static_cast(_data[fmsi + i] * 255.0); + } + + image->create(_size.x, _size.y, false, Image::FORMAT_L8, arr); + + return image; +} +Ref MLPPTensor3::get_feature_maps_image(const int p_index_r, const int p_index_g, const int p_index_b, const int p_index_a) { + ERR_FAIL_INDEX_V(p_index_r, _size.z, Ref()); + ERR_FAIL_INDEX_V(p_index_g, _size.z, Ref()); + ERR_FAIL_INDEX_V(p_index_b, _size.z, Ref()); + ERR_FAIL_INDEX_V(p_index_a, _size.z, Ref()); + + return Ref(); +} + +void MLPPTensor3::get_feature_map_into_image(Ref p_target, const int p_index_z, const int p_target_channels) const { + ERR_FAIL_INDEX(p_index_z, _size.z); +} +void MLPPTensor3::get_feature_map_into_image(Ref p_target, const int p_index_r, const int p_index_g, const int p_index_b, const int p_index_a) const { + ERR_FAIL_INDEX(p_index_r, _size.z); + ERR_FAIL_INDEX(p_index_g, _size.z); + ERR_FAIL_INDEX(p_index_b, _size.z); + ERR_FAIL_INDEX(p_index_a, _size.z); +} + +void MLPPTensor3::set_feature_map_image(const Ref &p_img, const int p_index_z, const int p_image_channel) { + ERR_FAIL_INDEX(p_index_z, _size.z); +} +void MLPPTensor3::set_feature_maps_image(const Ref &p_img, const int p_index_r, const int p_index_g, const int p_index_b, const int p_index_a) { + ERR_FAIL_INDEX(p_index_r, _size.z); + ERR_FAIL_INDEX(p_index_g, _size.z); + ERR_FAIL_INDEX(p_index_b, _size.z); + ERR_FAIL_INDEX(p_index_a, _size.z); +} + +void MLPPTensor3::set_from_image(const Ref &p_img, const int p_channels) { +} + String MLPPTensor3::to_string() { String str; diff --git a/mlpp/lin_alg/mlpp_tensor3.h b/mlpp/lin_alg/mlpp_tensor3.h index 723a339..c60ad23 100644 --- a/mlpp/lin_alg/mlpp_tensor3.h +++ b/mlpp/lin_alg/mlpp_tensor3.h @@ -15,6 +15,8 @@ #include "mlpp_matrix.h" #include "mlpp_vector.h" +class Image; + class MLPPTensor3 : public Reference { GDCLASS(MLPPTensor3, Reference); @@ -615,6 +617,38 @@ public: } } +public: + //Image api + + enum ImageChannels { + IMAGE_CHANNEL_R = 1 << 0, + IMAGE_CHANNEL_G = 1 << 1, + IMAGE_CHANNEL_B = 1 << 2, + IMAGE_CHANNEL_A = 1 << 3, + + IMAGE_CHANNEL_NONE = 0, + IMAGE_CHANNEL_RG = IMAGE_CHANNEL_R | IMAGE_CHANNEL_G, + IMAGE_CHANNEL_RGB = IMAGE_CHANNEL_R | IMAGE_CHANNEL_G | IMAGE_CHANNEL_B, + IMAGE_CHANNEL_GB = IMAGE_CHANNEL_G | IMAGE_CHANNEL_B, + IMAGE_CHANNEL_GBA = IMAGE_CHANNEL_G | IMAGE_CHANNEL_B | IMAGE_CHANNEL_A, + IMAGE_CHANNEL_BA = IMAGE_CHANNEL_B | IMAGE_CHANNEL_A, + IMAGE_CHANNEL_RGBA = IMAGE_CHANNEL_R | IMAGE_CHANNEL_G | IMAGE_CHANNEL_B | IMAGE_CHANNEL_A, + }; + + void add_feature_maps_image(const Ref &p_img, const int p_channels = IMAGE_CHANNEL_RGBA); + + Ref get_feature_map_image(const int p_index_z); + Ref get_feature_maps_image(const int p_index_r = -1, const int p_index_g = -1, const int p_index_b = -1, const int p_index_a = -1); + + void get_feature_map_into_image(Ref p_target, const int p_index_z, const int p_target_channels = IMAGE_CHANNEL_RGBA) const; + void get_feature_map_into_image(Ref p_target, const int p_index_r = -1, const int p_index_g = -1, const int p_index_b = -1, const int p_index_a = -1) const; + + void set_feature_map_image(const Ref &p_img, const int p_index_z, const int p_image_channel = IMAGE_CHANNEL_R); + void set_feature_maps_image(const Ref &p_img, const int p_index_r = -1, const int p_index_g = -1, const int p_index_b = -1, const int p_index_a = -1); + + void set_from_image(const Ref &p_img, const int p_channels = IMAGE_CHANNEL_RGBA); + +public: void fill(real_t p_val) { if (!_data) { return; @@ -925,4 +959,6 @@ protected: real_t *_data; }; +VARIANT_ENUM_CAST(MLPPTensor3::ImageChannels); + #endif