Compositor: add new node: Kuwahara filter #107015
|
@ -55,99 +55,82 @@ int KuwaharaOperation::get_variation()
|
||||||
return variation_;
|
return variation_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void KuwaharaOperation::update_memory_buffer_partial(MemoryBuffer *output,
|
||||||
void KuwaharaOperation::update_memory_buffer(MemoryBuffer *output,
|
const rcti &area,
|
||||||
const rcti &area,
|
Span<MemoryBuffer *> inputs)
|
||||||
Span<MemoryBuffer *> inputs)
|
|
||||||
{
|
{
|
||||||
MemoryBuffer *image = inputs[0];
|
MemoryBuffer *image = inputs[0];
|
||||||
|
|
||||||
const int bwidth = area.xmax - area.xmin;
|
for (BuffersIterator<float> it = output->iterate_with(inputs, area); !it.is_end(); ++it) {
|
||||||
const int bheight = area.ymax - area.ymin;
|
const int x = it.x;
|
||||||
|
const int y = it.y;
|
||||||
|
it.out[3] = image->get_value(x, y, 3);
|
||||||
|
|
||||||
for (int y = 0; y < bheight; y++)
|
|
||||||
{
|
|
||||||
for (int x = 0; x < bwidth; x++)
|
|
||||||
{
|
|
||||||
output->get_value(x, y, 3) = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int ch = 0; ch < 3; ch++)
|
for(int ch = 0; ch < 3; ch++)
|
||||||
{
|
|
||||||
for (int y = 0; y < bheight; y++)
|
|
||||||
{
|
{
|
||||||
for (int x = 0; x < bwidth; x++)
|
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++)
|
||||||
{
|
{
|
||||||
float sum[4] = {0.0f, 0.0f, 0.0f, 0.0f};
|
for (int dx = -kernel_size_; dx <= kernel_size_; dx++)
|
||||||
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++)
|
|
||||||
|
int xx = x + dx;
|
||||||
|
int yy = y + dy;
|
||||||
|
if (xx >= 0 && yy >= 0 && xx < area.xmax && yy < area.ymax)
|
||||||
|
|||||||
{
|
{
|
||||||
int xx = x + dx;
|
float v;
|
||||||
int yy = y + dy;
|
v = image->get_value(xx, yy, ch);
|
||||||
if (xx >= 0 && yy >= 0 && xx < area.xmax && yy < area.ymax)
|
|
||||||
|
if (dx <= 0 && dy <= 0)
|
||||||
{
|
{
|
||||||
//double v = temp.at<float>(yy, xx * dim + c);
|
sum[0] += v;
|
||||||
float v;
|
var[0] += v * v;
|
||||||
v = image->get_value(xx, yy, ch);
|
cnt[0]++;
|
||||||
|
}
|
||||||
|
|
||||||
if (dx <= 0 && dy <= 0)
|
if (dx >= 0 && dy <= 0)
|
||||||
{
|
{
|
||||||
sum[0] += v;
|
sum[1] += v;
|
||||||
var[0] += v * v;
|
var[1] += v * v;
|
||||||
cnt[0]++;
|
cnt[1]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dx >= 0 && dy <= 0)
|
if (dx <= 0 && dy >= 0)
|
||||||
{
|
{
|
||||||
sum[1] += v;
|
sum[2] += v;
|
||||||
var[1] += v * v;
|
var[2] += v * v;
|
||||||
cnt[1]++;
|
cnt[2]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dx <= 0 && dy >= 0)
|
if (dx >= 0 && dy >= 0)
|
||||||
{
|
{
|
||||||
sum[2] += v;
|
sum[3] += v;
|
||||||
var[2] += v * v;
|
var[3] += v * v;
|
||||||
cnt[2]++;
|
cnt[3]++;
|
||||||
}
|
|
||||||
|
|
||||||
if (dx >= 0 && dy >= 0)
|
|
||||||
{
|
|
||||||
sum[3] += v;
|
|
||||||
var[3] += v * v;
|
|
||||||
cnt[3]++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::pair<double, int>> vec;
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
sort(vec.begin(), vec.end());
|
|
||||||
float res = static_cast<float>(sum[vec[0].second]);
|
|
||||||
|
|
||||||
output->get_value(x, y, ch) = res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::pair<double, int>> vec;
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
sort(vec.begin(), vec.end());
|
||||||
|
float res = static_cast<float>(sum[vec[0].second]);
|
||||||
|
|
||||||
|
output->get_value(x, y, ch) = res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void KuwaharaOperation::get_area_of_interest(const int /*input_idx*/,
|
|
||||||
const rcti & /*output_area*/,
|
|
||||||
rcti &r_input_area)
|
|
||||||
{
|
|
||||||
r_input_area = this->get_canvas();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace blender::compositor
|
} // namespace blender::compositor
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "COM_NodeOperation.h"
|
#include "COM_MultiThreadedOperation.h"
|
||||||
|
|
||||||
namespace blender::compositor {
|
namespace blender::compositor {
|
||||||
|
|
||||||
class KuwaharaOperation : public NodeOperation {
|
class KuwaharaOperation : public MultiThreadedOperation {
|
||||||
SocketReader *image_reader_;
|
SocketReader *image_reader_;
|
||||||
|
|
||||||
int kernel_size_;
|
int kernel_size_;
|
||||||
|
@ -26,21 +26,9 @@ class KuwaharaOperation : public NodeOperation {
|
||||||
void set_variation(int variation);
|
void set_variation(int variation);
|
||||||
int get_variation();
|
int get_variation();
|
||||||
|
|
||||||
void update_memory_buffer(MemoryBuffer *output,
|
void update_memory_buffer_partial(MemoryBuffer *output,
|
||||||
const rcti &area,
|
const rcti &area,
|
||||||
Span<MemoryBuffer *> inputs) override;
|
Span<MemoryBuffer *> inputs) override;
|
||||||
|
|
||||||
void get_area_of_interest(const int input_idx,
|
|
||||||
const rcti & output_area,
|
|
||||||
rcti &r_input_area) override;
|
|
||||||
//
|
|
||||||
// bool determine_depending_area_of_interest(rcti *input,
|
|
||||||
// ReadBufferOperation *read_operation,
|
|
||||||
// rcti *output) override;
|
|
||||||
//
|
|
||||||
// void update_memory_buffer_partial(MemoryBuffer *output,
|
|
||||||
// const rcti &area,
|
|
||||||
// Span<MemoryBuffer *> inputs) override;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ static void node_composit_init_kuwahara(bNodeTree * /*ntree*/, bNode *node)
|
||||||
NodeKuwaharaData *data = MEM_cnew<NodeKuwaharaData>(__func__);
|
NodeKuwaharaData *data = MEM_cnew<NodeKuwaharaData>(__func__);
|
||||||
node->storage = data;
|
node->storage = data;
|
||||||
|
|
||||||
data->kernel_size = 4.1f;
|
data->kernel_size = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void node_composit_buts_kuwahara(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
|
static void node_composit_buts_kuwahara(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
|
||||||
|
|
Loading…
Reference in New Issue
Suggest using early
continue
and un-indent the block.