Compositor: add new node: Kuwahara filter #107015
|
@ -24,7 +24,10 @@ void KuwaharaOperation::deinit_execution()
|
||||||
image_reader_ = nullptr;
|
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];
|
float input_value[4];
|
||||||
image_reader_->read_sampled(input_value, x, y, sampler);
|
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;
|
const int y = it.y;
|
||||||
it.out[3] = image->get_value(x, y, 3);
|
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 sum[4] = {0.0f, 0.0f, 0.0f, 0.0f};
|
||||||
float var[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};
|
int cnt[4] = {0, 0, 0, 0};
|
||||||
|
|
||||||
for (int dy = -kernel_size_; dy <= kernel_size_; dy++)
|
/* Split surroundings of */
|
||||||
{
|
for (int dy = -kernel_size_; dy <= kernel_size_; dy++) {
|
||||||
for (int dx = -kernel_size_; dx <= kernel_size_; dx++)
|
for (int dx = -kernel_size_; dx <= kernel_size_; dx++) {
|
||||||
{
|
|
||||||
|
|
||||||
int xx = x + dx;
|
int xx = x + dx;
|
||||||
int yy = y + dy;
|
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) {
|
||||||
|
|||||||
{
|
|
||||||
float v;
|
float v;
|
||||||
v = image->get_value(xx, yy, ch);
|
v = image->get_value(xx, yy, ch);
|
||||||
|
|
||||||
if (dx <= 0 && dy <= 0)
|
if (dx <= 0 && dy <= 0) {
|
||||||
{
|
|
||||||
sum[0] += v;
|
sum[0] += v;
|
||||||
var[0] += v * v;
|
var[0] += v * v;
|
||||||
cnt[0]++;
|
cnt[0]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dx >= 0 && dy <= 0)
|
if (dx >= 0 && dy <= 0) {
|
||||||
{
|
|
||||||
sum[1] += v;
|
sum[1] += v;
|
||||||
var[1] += v * v;
|
var[1] += v * v;
|
||||||
cnt[1]++;
|
cnt[1]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dx <= 0 && dy >= 0)
|
if (dx <= 0 && dy >= 0) {
|
||||||
{
|
|
||||||
sum[2] += v;
|
sum[2] += v;
|
||||||
var[2] += v * v;
|
var[2] += v * v;
|
||||||
cnt[2]++;
|
cnt[2]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dx >= 0 && dy >= 0)
|
if (dx >= 0 && dy >= 0) {
|
||||||
{
|
|
||||||
sum[3] += v;
|
sum[3] += v;
|
||||||
var[3] += v * v;
|
var[3] += v * v;
|
||||||
cnt[3]++;
|
cnt[3]++;
|
||||||
|
@ -116,19 +111,24 @@ void KuwaharaOperation::update_memory_buffer_partial(MemoryBuffer *output,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::pair<double, int>> vec;
|
/* Compute region variances */
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++) {
|
||||||
{
|
|
||||||
sum[i] = cnt[i] != 0 ? sum[i] / cnt[i] : 0.0f;
|
sum[i] = cnt[i] != 0 ? sum[i] / cnt[i] : 0.0f;
|
||||||
var[i] = cnt[i] != 0 ? var[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]);
|
const float temp = sum[i] * sum[i];
|
||||||
vec.push_back(std::make_pair(var[i], i));
|
var[i] = var[i] > temp ? sqrt(var[i] - temp) : 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
sort(vec.begin(), vec.end());
|
/* Choose the region with lowest variance */
|
||||||
float res = static_cast<float>(sum[vec[0].second]);
|
float min_var = FLT_MAX;
|
||||||
|
int min_index = 0;
|
||||||
output->get_value(x, y, ch) = res;
|
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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,14 +22,13 @@ class KuwaharaOperation : public MultiThreadedOperation {
|
||||||
|
|
||||||
void set_kernel_size(int kernel_size);
|
void set_kernel_size(int kernel_size);
|
||||||
int get_kernel_size();
|
int get_kernel_size();
|
||||||
|
|
||||||
void set_variation(int variation);
|
void set_variation(int variation);
|
||||||
int get_variation();
|
int get_variation();
|
||||||
|
|
||||||
void update_memory_buffer_partial(MemoryBuffer *output,
|
void update_memory_buffer_partial(MemoryBuffer *output,
|
||||||
const rcti &area,
|
const rcti &area,
|
||||||
Span<MemoryBuffer *> inputs) override;
|
Span<MemoryBuffer *> inputs) override;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace blender::compositor
|
} // namespace blender::compositor
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
* \ingroup cmpnodes
|
* \ingroup cmpnodes
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
//#include "COM_shader_node.hh"
|
//#include "COM_shader_node.hh"
|
||||||
#include "COM_node_operation.hh"
|
#include "COM_node_operation.hh"
|
||||||
zazizizou marked this conversation as resolved
Outdated
Sergey Sharybin
commented
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);
|
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
|
} // namespace blender::nodes::node_composite_kuwahara_cc
|
||||||
|
|
||||||
void register_node_type_cmp_kuwahara()
|
void register_node_type_cmp_kuwahara()
|
||||||
|
@ -91,7 +56,7 @@ void register_node_type_cmp_kuwahara()
|
||||||
ntype.initfunc = file_ns::node_composit_init_kuwahara;
|
ntype.initfunc = file_ns::node_composit_init_kuwahara;
|
||||||
node_type_storage(
|
node_type_storage(
|
||||||
&ntype, "NodeKuwaharaData", node_free_standard_storage, node_copy_standard_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);
|
nodeRegisterType(&ntype);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Suggest using early
continue
and un-indent the block.