Compositor: add new node: Kuwahara filter #107015

Merged
Habib Gahbiche merged 22 commits from zazizizou/blender:com-kuwahara-filter-node into main 2023-06-08 16:14:51 +02:00
3 changed files with 29 additions and 65 deletions
Showing only changes of commit f8c4f1a0e1 - Show all commits

View File

@ -24,7 +24,10 @@ void KuwaharaOperation::deinit_execution()
image_reader_ = nullptr;
}
void KuwaharaOperation::execute_pixel_sampled(float output[4], float x, float y, PixelSampler sampler)
void KuwaharaOperation::execute_pixel_sampled(float output[4],
float x,
float y,
PixelSampler sampler)
{
float input_value[4];
image_reader_->read_sampled(input_value, x, y, sampler);
@ -66,48 +69,40 @@ void KuwaharaOperation::update_memory_buffer_partial(MemoryBuffer *output,
const int y = it.y;
it.out[3] = image->get_value(x, y, 3);
for(int ch = 0; ch < 3; ch++)
{
for (int ch = 0; ch < 3; ch++) {
float sum[4] = {0.0f, 0.0f, 0.0f, 0.0f};
float var[4] = {0.0f, 0.0f, 0.0f, 0.0f};
int cnt[4] = {0, 0, 0, 0};
for (int dy = -kernel_size_; dy <= kernel_size_; dy++)
{
for (int dx = -kernel_size_; dx <= kernel_size_; dx++)
{
/* Split surroundings of */
for (int dy = -kernel_size_; dy <= kernel_size_; dy++) {
for (int dx = -kernel_size_; dx <= kernel_size_; dx++) {
int xx = x + dx;
int yy = y + dy;
if (xx >= 0 && yy >= 0 && xx < area.xmax && yy < area.ymax)
{
if (xx >= 0 && yy >= 0 && xx < area.xmax && yy < area.ymax) {

Suggest using early continue and un-indent the block.

Suggest using early `continue` and un-indent the block.
float v;
v = image->get_value(xx, yy, ch);
if (dx <= 0 && dy <= 0)
{
if (dx <= 0 && dy <= 0) {
sum[0] += v;
var[0] += v * v;
cnt[0]++;
}
if (dx >= 0 && dy <= 0)
{
if (dx >= 0 && dy <= 0) {
sum[1] += v;
var[1] += v * v;
cnt[1]++;
}
if (dx <= 0 && dy >= 0)
{
if (dx <= 0 && dy >= 0) {
sum[2] += v;
var[2] += v * v;
cnt[2]++;
}
if (dx >= 0 && dy >= 0)
{
if (dx >= 0 && dy >= 0) {
sum[3] += v;
var[3] += v * v;
cnt[3]++;
@ -116,19 +111,24 @@ void KuwaharaOperation::update_memory_buffer_partial(MemoryBuffer *output,
}
}
std::vector<std::pair<double, int>> vec;
for (int i = 0; i < 4; i++)
{
/* Compute region variances */
for (int i = 0; i < 4; i++) {
sum[i] = cnt[i] != 0 ? sum[i] / cnt[i] : 0.0f;
var[i] = cnt[i] != 0 ? var[i] / cnt[i] : 0.0f;
var[i] = sqrt(var[i] - sum[i] * sum[i]);
vec.push_back(std::make_pair(var[i], i));
const float temp = sum[i] * sum[i];
var[i] = var[i] > temp ? sqrt(var[i] - temp) : 0.0f;
}
sort(vec.begin(), vec.end());
float res = static_cast<float>(sum[vec[0].second]);
output->get_value(x, y, ch) = res;
/* Choose the region with lowest variance */
float min_var = FLT_MAX;
int min_index = 0;
for (int i = 0; i < 4; i++) {
if (var[i] < min_var) {
min_var = var[i];
min_index = i;
}
}
output->get_value(x, y, ch) = sum[min_index];
}
}
}

View File

@ -22,14 +22,13 @@ class KuwaharaOperation : public MultiThreadedOperation {
void set_kernel_size(int kernel_size);
int get_kernel_size();
void set_variation(int variation);
int get_variation();
void update_memory_buffer_partial(MemoryBuffer *output,
const rcti &area,
Span<MemoryBuffer *> inputs) override;
};
} // namespace blender::compositor

View File

@ -5,7 +5,6 @@
* \ingroup cmpnodes
*/
//#include "COM_shader_node.hh"
#include "COM_node_operation.hh"
zazizizou marked this conversation as resolved Outdated

Remove the commented out code.

Remove the commented out code.
@ -43,40 +42,6 @@ static void node_composit_buts_kuwahara(uiLayout *layout, bContext * /*C*/, Poin
uiItemR(col, ptr, "kernel_size", 0, nullptr, ICON_NONE);
}
//using namespace blender::realtime_compositor;
//
//class KuwaharaShaderNode : public NodeOperation {
// public:
// using NodeOperation::NodeOperation;
//
// void execute() override
// {
// get_input("Image").pass_through(get_result("Image"));
// context().set_info_message("Viewport compositor setup not fully supported");
// }
// using ShaderNode::ShaderNode;
//
// void compile(GPUMaterial *material) override
// {
// GPUNodeStack *inputs = get_inputs_array();
// GPUNodeStack *outputs = get_outputs_array();
//
// GPU_stack_link(material, &bnode(), "node_composite_kuwahara", inputs, outputs);
// }
//};
//
//static NodeOperation *get_compositor_operation(Context *context, DNode node)
//{
// return new KuwaharaOperation(context, node);
//}
//static ShaderNode *get_compositor_shader_node(DNode node)
//{
// return new KuwaharaShaderNode(node);
//}
} // namespace blender::nodes::node_composite_kuwahara_cc
void register_node_type_cmp_kuwahara()
@ -91,7 +56,7 @@ void register_node_type_cmp_kuwahara()
ntype.initfunc = file_ns::node_composit_init_kuwahara;
node_type_storage(
&ntype, "NodeKuwaharaData", node_free_standard_storage, node_copy_standard_storage);
// ntype.get_compositor_shader_node = file_ns::get_compositor_shader_node;
// ntype.get_compositor_shader_node = file_ns::get_compositor_shader_node;
nodeRegisterType(&ntype);
}