Work on fixing histogram generation.

This commit is contained in:
Relintai 2023-03-04 19:40:30 +01:00
parent a3da8b16ba
commit c54ad5594b
6 changed files with 61 additions and 22 deletions

View File

@ -5664,13 +5664,13 @@ Ref<Image> MMAlgos::generate_histogram(const Ref<Image> &input, const int textur
for (int x = 0; x < size.x; ++x) {
for (int y = 0; y < size.y; ++y) {
float e = 1.0 / size.y;
float uv_y = 1.0 / size.y * y;
Color sum = Color();
for (float yy = 0.5 * e; yy < 1.0; yy += e) {
Color sic = scaled_input->get_pixel(x, yy * size.y);
sic -= Color(e, e, e, e);
for (int yy = 0; yy < size.y; ++yy) {
Color sic = scaled_input->get_pixel(x, yy);
sic -= Color(uv_y, uv_y, uv_y, uv_y);
sic.r = MAX(0, 1.0 - 16.0 * ABS(sic.r));
sic.g = MAX(0, 1.0 - 16.0 * ABS(sic.g));
@ -5714,7 +5714,7 @@ Ref<Image> MMAlgos::generate_histogram(const Ref<Image> &input, const int textur
Ref<Image> step_2;
step_2.instance();
step_2->copy_internals_from(step_1);
step_2->resize(texture_size, 2);
step_2->resize(texture_size, 1);
scaled_input->lock();
step_2->lock();
@ -5723,11 +5723,10 @@ Ref<Image> MMAlgos::generate_histogram(const Ref<Image> &input, const int textur
for (int x = 0; x < size.x; ++x) {
for (int y = 0; y < size.y; ++y) {
float e = 1.0 / size.y;
Color sum = Color();
for (float yy = 0.5 * e; yy < 1.0; yy += e) {
Color sic = scaled_input->get_pixel(yy * size.y, x);
for (int yy = 0; yy < size.y; ++yy) {
Color sic = scaled_input->get_pixel(yy, x);
sum += sic;
}
@ -5790,35 +5789,40 @@ Ref<Image> MMAlgos::generate_histogram(const Ref<Image> &input, const int textur
for (int y = 0; y < size.y; ++y) {
float e = 1.0 / size.y;
float uv_y = e * y;
float uv_y = e * static_cast<float>(y);
if (ABS(MMAlgos::fractf(uv_y + gradient_width)) < 2.0 * gradient_width) {
result->set_pixel(x, y, Color(uv_x, uv_x, uv_x, 1));
} else {
Color highest = Color(0, 0, 0, 0);
for (float xx = 0.5 * uv_1_x; xx < 1.0; xx += uv_1_x) {
Color sic = step_2->get_pixel(xx * uv_1_x, 0);
for (int xx = 0; xx < size.x; ++xx) {
Color sic = step_2->get_pixel(xx, 0);
highest.r = MAX(highest.r, sic.r);
highest.g = MAX(highest.g, sic.g);
highest.b = MAX(highest.b, sic.b);
highest.a = MAX(highest.a, sic.a);
if (highest < sic) {
highest = sic;
}
}
Color raw_value = step_2->get_pixel(x * uv_1_x, 0);
Color raw_value = step_2->get_pixel(x, 0);
//vec4 value = step(vec4(1.0 - gradient_width - UV.y) * highest / (1.0 - 2.0 * gradient_width), raw_value);
Vector4 highest_v4 = Vector4(highest.r, highest.g, highest.b, highest.a);
float gv = 1.0 - gradient_width - uv_y;
Vector4 value = Vector4(gv, gv, gv, gv) * highest_v4 / (1.0 - 2.0 * gradient_width);
//-- [1.0 - gradient_width - UV.y]
float gvu = 1.0 - gradient_width - uv_y;
float gv = 1.0 - 2.0 * gradient_width;
Vector4 value = Vector4(gvu, gvu, gvu, gvu) * highest_v4 / Vector4(gv, gv, gv, gv);
value.x = step(value.x, raw_value.r);
value.y = step(value.y, raw_value.g);
value.z = step(value.z, raw_value.b);
value.w = step(value.w, raw_value.a);
//float alpha = step(2.0 * gradient_width, dot(value, vec4(1.0)));
float alpha = step(2.0 * gradient_width, value.dot(Vector4(1, 1, 1, 1)));
//COLOR = vec4(mix(value.rgb, vec3(0.5), 0.3 * value.a), alpha);
Vector3 val3 = Vector3(value.x, value.y, value.z);
val3 = val3.linear_interpolate(Vector3(0.5, 0.5, 0.5), 0.3 * value.w);

View File

@ -195,7 +195,7 @@ int MMGraphNode::add_slot_tones() {
MMTonesEditor *te = memnew(MMTonesEditor);
int slot_idx = add_slot(MMNodeUniversalProperty::SLOT_TYPE_NONE, MMNodeUniversalProperty::SLOT_TYPE_NONE, "", "", te);
te->set_value(_node);
te->set_value(_material, _node);
//ge.texture = _node.call(getter, _material, slot_idx);
//properties[slot_idx].append(ge.texture);
return slot_idx;

View File

@ -4,6 +4,7 @@
#include "scene/resources/texture.h"
#include "../../../nodes/filter/tones.h"
#include "../../../nodes/mm_material.h"
#include "scene/gui/button.h"
#include "scene/gui/option_button.h"
@ -13,7 +14,9 @@
#include "tones_editor_cursor.h"
void MMTonesEditor::set_value(const Ref<MMTones> &v) {
void MMTonesEditor::set_value(const Ref<MMMaterial> &material, const Ref<MMTones> &v) {
_material = material;
if (_node == v) {
return;
}
@ -254,11 +257,12 @@ MMTonesEditor::~MMTonesEditor() {
}
void MMTonesEditor::on_input_property_changed() {
if (!_node.is_valid()) {
if (!_node.is_valid() || !_material.is_valid()) {
_histogram_tr->set_texture(make_default_histogram());
return;
}
//Ref<Image> img = _node->render_original_image(_material);
Ref<Image> img = _node->get_image()->get_active_image();
if (!img.is_valid()) {

View File

@ -10,6 +10,7 @@ class OptionButton;
class TextureRect;
class ImageTexture;
class MMTonesEditorCursor;
class MMMaterial;
class MMTonesEditor : public VBoxContainer {
GDCLASS(MMTonesEditor, VBoxContainer);
@ -27,7 +28,7 @@ public:
HISTOGRAM_IMAGE_SIZE = 128,
};
void set_value(const Ref<MMTones> &v);
void set_value(const Ref<MMMaterial> &material, const Ref<MMTones> &v);
enum ParameterTypes {
PARAMETER_TYPE_IN_MIN = 0,
@ -61,6 +62,7 @@ protected:
static void _bind_methods();
Ref<MMMaterial> _material;
Ref<MMTones> _node;
Modes _current_mode;

View File

@ -3,6 +3,7 @@
#include "../../algos/mm_algos.h"
#include "../../editor/mm_graph_node.h"
#include "core/io/image.h"
#include "../mm_material.h"
Ref<MMNodeUniversalProperty> MMTones::get_input() {
@ -114,6 +115,29 @@ Color MMTones::_get_value_for(const Vector2 &uv, const int pseed) {
return MMAlgos::adjust_levels(c, _in_min, _in_mid, _in_max, _out_min, _out_max);
}
Ref<Image> MMTones::render_original_image(const Ref<MMMaterial> &material) {
Ref<Image> image;
image.instance();
image->create(material->image_size.x, material->image_size.y, false, Image::FORMAT_RGBA8);
image->lock();
float w = image->get_width();
float h = image->get_height();
for (int x = 0; x < image->get_width(); ++x) { //x in range(image.get_width())
for (int y = 0; y < image->get_height(); ++y) { //y in range(image.get_height())
Vector2 v = Vector2(x / w, y / h);
Color col = input->get_value(v);
image->set_pixel(x, y, col);
}
}
image->unlock();
return image;
}
MMTones::MMTones() {
_in_max = Color(1, 1, 1, 1);
_in_mid = Color(0.5, 0.5, 0.5, 0.5);

View File

@ -4,6 +4,9 @@
#include "../mm_node.h"
#include "../mm_node_universal_property.h"
class MMMaterial;
class Image;
class MMTones : public MMNode {
GDCLASS(MMTones, MMNode);
@ -38,6 +41,8 @@ public:
void _render(const Ref<MMMaterial> &material);
Color _get_value_for(const Vector2 &uv, const int pseed);
Ref<Image> render_original_image(const Ref<MMMaterial> &material);
MMTones();
~MMTones();