- [Contrast Blur] Improve algorithm.

This commit is contained in:
Tanasart 2024-01-24 19:45:06 +07:00
parent 77271b7de2
commit f5c4752056
8 changed files with 36 additions and 87 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 58 KiB

View file

@ -47,25 +47,14 @@ function Node_Blur_Contrast(_x, _y, _group = noone) : Node_Processor(_x, _y, _gr
var ww = surface_get_width_safe(_surf);
var hh = surface_get_height_safe(_surf);
temp_surface[0] = surface_verify(temp_surface[0], ww, hh, attrDepth());
surface_set_shader(temp_surface[0], sh_blur_box_contrast);
shader_set_surface("baseSurface", _surf);
surface_set_shader(_outSurf, sh_blur_box_contrast);
shader_set_f("dimension", [ ww, hh ]);
shader_set_f("size", _size);
shader_set_f("treshold", _tres);
shader_set_i("direction", 0);
shader_set_f("size", _size);
shader_set_f("treshold", _tres);
draw_surface_safe(_surf, 0, 0);
surface_reset_shader();
surface_set_shader(_outSurf, sh_blur_box_contrast);
shader_set(sh_blur_box_contrast);
shader_set_i("direction", 1);
draw_surface_safe(temp_surface[0], 0, 0);
surface_reset_shader();
__process_mask_modifier(_data);
_outSurf = mask_apply(_data[0], _outSurf, _mask, _mix);
_outSurf = channel_apply(_data[0], _outSurf, _data[6]);

View file

@ -512,7 +512,7 @@ function __initNodes() {
addNodeObject(filter, "Displace", s_node_displace, "Node_Displace", [1, Node_Displace], ["distort"], "Distort image using another image as a map.");
addNodeObject(filter, "Texture Remap", s_node_texture_map, "Node_Texture_Remap", [1, Node_Texture_Remap],, "Remap image using texture map. Where red channel control x position and green channel control y position.");
addNodeObject(filter, "Time Remap", s_node_time_map, "Node_Time_Remap", [1, Node_Time_Remap],, "Remap image using texture as time map. Where brighter pixel means using pixel from an older frame.");
addNodeObject(filter, "Morph Surface", s_node_morph_surface, "Node_Morph_Surface", [1, Node_Morph_Surface],, "Morph pixel bewteen two surfaces.").setVersion(1141);
addNodeObject(filter, "Morph Surface", s_node_morph_surface, "Node_Morph_Surface", [1, Node_Morph_Surface],, "Morph pixel between two surfaces.").setVersion(1141);
ds_list_add(filter, "Effects");
addNodeObject(filter, "Outline", s_node_border, "Node_Outline", [1, Node_Outline], ["border"], "Add border to the image.");

View file

@ -1,16 +1,15 @@
//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform sampler2D baseSurface;
#define TAU 6.283185307179586
#define MAX_STRENGTH 64.
uniform vec2 dimension;
uniform float size;
uniform float treshold;
uniform int direction;
vec3 rgb2xyz( vec3 c ) {
vec3 rgb2xyz( vec3 c ) { #region
vec3 tmp;
tmp.x = ( c.r > 0.04045 ) ? pow( ( c.r + 0.055 ) / 1.055, 2.4 ) : c.r / 12.92;
tmp.y = ( c.g > 0.04045 ) ? pow( ( c.g + 0.055 ) / 1.055, 2.4 ) : c.g / 12.92,
@ -19,83 +18,47 @@ vec3 rgb2xyz( vec3 c ) {
mat3( 0.4124, 0.3576, 0.1805,
0.2126, 0.7152, 0.0722,
0.0193, 0.1192, 0.9505 );
}
} #endregion
vec3 xyz2lab( vec3 c ) {
vec3 xyz2lab( vec3 c ) { #region
vec3 n = c / vec3( 95.047, 100, 108.883 );
vec3 v;
v.x = ( n.x > 0.008856 ) ? pow( n.x, 1.0 / 3.0 ) : ( 7.787 * n.x ) + ( 16.0 / 116.0 );
v.y = ( n.y > 0.008856 ) ? pow( n.y, 1.0 / 3.0 ) : ( 7.787 * n.y ) + ( 16.0 / 116.0 );
v.z = ( n.z > 0.008856 ) ? pow( n.z, 1.0 / 3.0 ) : ( 7.787 * n.z ) + ( 16.0 / 116.0 );
return vec3(( 116.0 * v.y ) - 16.0, 500.0 * ( v.x - v.y ), 200.0 * ( v.y - v.z ));
}
} #endregion
vec3 rgb2lab(vec3 c) {
vec3 rgb2lab(vec3 c) { #region
vec3 lab = xyz2lab( rgb2xyz( c ) );
return vec3( lab.x / 100.0, 0.5 + 0.5 * ( lab.y / 127.0 ), 0.5 + 0.5 * ( lab.z / 127.0 ));
}
} #endregion
float colorDifferent(in vec4 c1, in vec4 c2) {
vec3 lab1 = rgb2lab(c1.rgb * c1.a);
vec3 lab2 = rgb2lab(c2.rgb * c2.a);
return length(lab1 - lab2);
}
float colorDifferent(in vec4 c1, in vec4 c2) { #region
return length(c1.rgb - c2.rgb) / 1.7320508076;
} #endregion
void main() {
vec4 base_col = texture2D( baseSurface, v_vTexcoord);
vec4 curr_col = texture2D( gm_BaseTexture, v_vTexcoord);
vec2 tx = 1. / dimension;
vec4 col = texture2D( gm_BaseTexture, v_vTexcoord);
vec4 c = col;
float div = 1.;
float sz2 = size * size;
if(base_col.a > 0.5) {
vec4 col = curr_col;
vec4 _bcol, _ccol;
float mulp = 1.;
if(direction == 0) {
for(float i = 1.; i < 32.; i++) {
if(i >= size) break;
_bcol = texture2D( baseSurface, v_vTexcoord + vec2(i / dimension.x, 0.));
if(_bcol.a > 0.5 && colorDifferent(base_col, _bcol) < treshold) {
_ccol = texture2D( gm_BaseTexture, v_vTexcoord + vec2(i / dimension.x, 0.));
col += _ccol;
mulp++;
}
_bcol = texture2D( baseSurface, v_vTexcoord - vec2(i / dimension.x, 0.));
if(_bcol.a > 0.5 && colorDifferent(base_col, _bcol) < treshold) {
_ccol = texture2D( gm_BaseTexture, v_vTexcoord - vec2(i / dimension.x, 0.));
col += _ccol;
mulp++;
}
}
for(float i = -MAX_STRENGTH; i <= MAX_STRENGTH; i++)
for(float j = -MAX_STRENGTH; j <= MAX_STRENGTH; j++) {
float dist = i * i + j * j;
if(dist >= sz2) continue;
col /= mulp;
} else if(direction == 1) {
for(float i = 1.; i < 32.; i++) {
if(i >= size) break;
_bcol = texture2D( baseSurface, v_vTexcoord + vec2(0., i / dimension.y));
if(_bcol.a > 0.5 && colorDifferent(base_col, _bcol) < treshold) {
_ccol = texture2D( gm_BaseTexture, v_vTexcoord + vec2(0., i / dimension.y));
col += _ccol;
mulp++;
}
_bcol = texture2D( baseSurface, v_vTexcoord - vec2(0., i / dimension.y));
if(_bcol.a > 0.5 && colorDifferent(base_col, _bcol) < treshold) {
_ccol = texture2D( gm_BaseTexture, v_vTexcoord - vec2(0., i / dimension.y));
col += _ccol;
mulp++;
}
}
col /= mulp;
float amp = 1. - dist / sz2;
vec2 pxs = v_vTexcoord + vec2( i, j ) * tx;
vec4 bcol = texture2D( gm_BaseTexture, pxs);
if(c.a == bcol.a && colorDifferent(c, bcol) <= treshold) {
col += bcol * amp;
div += amp;
}
col.a = base_col.a;
gl_FragColor = col;
} else {
gl_FragColor = base_col;
}
gl_FragColor = col / div;
}

View file

@ -1,9 +1,8 @@
//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
#define TAU 6.283185307179586
uniform vec2 dimension;
uniform vec2 borderStart;
@ -27,8 +26,6 @@ uniform sampler2D blend_alphaSurf;
uniform int sampleMode;
uniform int outline_only;
#define TAU 6.283185307179586
vec2 round(in vec2 v) { #region
v.x = fract(v.x) > 0.5? ceil(v.x) : floor(v.x);
v.y = fract(v.y) > 0.5? ceil(v.y) : floor(v.y);