Skip to content

Commit

Permalink
kContour: Added Frei-Chen option
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku committed Jul 27, 2024
1 parent f970114 commit 60b1e1e
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 214 deletions.
3 changes: 2 additions & 1 deletion shaders/cAntiAliasing.fx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ float4 PS_Prefilter(CShade_VS2PS_Quad Input) : SV_TARGET0
float3 SampleC = tex2D(CShade_SampleGammaTex, EdgeTex1.xy).rgb;
float3 SampleD = tex2D(CShade_SampleGammaTex, EdgeTex1.zw).rgb;
float3 SampleE = tex2D(CShade_SampleGammaTex, Input.Tex0).rgb;

float3 Mean = SampleA + SampleB + SampleC + SampleD;
float3 Edges = 4.0 * (abs(Mean - (SampleE * 4.0)) / Mean);
float3 Edges = 4.0 * abs(Mean - (SampleE * 4.0));
float EdgesLuma = GetIntensity(Edges);

return float4(SampleE, EdgesLuma);
Expand Down
80 changes: 0 additions & 80 deletions shaders/cCensusTransform.fx

This file was deleted.

80 changes: 80 additions & 0 deletions shaders/cContrastNormalization.fx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

#include "shared/cShade.fxh"
#include "shared/cMath.fxh"

/*
[Pixel Shaders]
*/

float4 GetCensusTransform(sampler2D Image, float2 Tex)
{
float4 Transform = 0.0;

float2 Delta = fwidth(Tex);
float4 Tex0 = Tex.xyyy + (float4(-1.0, 1.0, 0.0, -1.0) * Delta.xyyy);
float4 Tex1 = Tex.xyyy + (float4(0.0, 1.0, 0.0, -1.0) * Delta.xyyy);
float4 Tex2 = Tex.xyyy + (float4(1.0, 1.0, 0.0, -1.0) * Delta.xyyy);

const int Neighbors = 8;
float4 SampleNeighbor[Neighbors];
SampleNeighbor[0] = tex2D(CShade_SampleColorTex, Tex0.xy);
SampleNeighbor[1] = tex2D(CShade_SampleColorTex, Tex1.xy);
SampleNeighbor[2] = tex2D(CShade_SampleColorTex, Tex2.xy);
SampleNeighbor[3] = tex2D(CShade_SampleColorTex, Tex0.xz);
SampleNeighbor[4] = tex2D(CShade_SampleColorTex, Tex2.xz);
SampleNeighbor[5] = tex2D(CShade_SampleColorTex, Tex0.xw);
SampleNeighbor[6] = tex2D(CShade_SampleColorTex, Tex1.xw);
SampleNeighbor[7] = tex2D(CShade_SampleColorTex, Tex2.xw);
float4 CenterSample = tex2D(CShade_SampleColorTex, Tex1.xz);

// Generate 8-bit integer from the 8-pixel neighborhood
for(int i = 0; i < Neighbors; i++)
{
float4 Comparison = step(SampleNeighbor[i], CenterSample);
Transform += ldexp(Comparison, i);
}

// Convert the 8-bit integer to float
return Transform * (1.0 / (exp2(8) - 1));
}

float4 GetLocalContrastNormalization(sampler2D Image, float2 Tex)
{
float2 Delta = fwidth(Tex);

float4 S[5];
S[0] = tex2D(Image, Tex);
S[1] = tex2D(Image, Tex + (float2(-1.5, 0.0) * Delta));
S[2] = tex2D(Image, Tex + (float2(1.5, 0.0) * Delta));
S[3] = tex2D(Image, Tex + (float2(0.0, -1.5) * Delta));
S[4] = tex2D(Image, Tex + (float2(0.0, 1.5) * Delta));
float4 Mean = (S[0] + S[1] + S[2] + S[3] + S[4]) / 5.0;

// Calculate standard deviation
float4 StdDev = 0.0;
for (int i = 0; i < 5; i++)
{
float4 G = S[i] - Mean;
StdDev += (G * G);
}

StdDev = sqrt(max(StdDev / 5.0, 1e-4));
return (S[0] - Mean) / StdDev;
}

float4 PS_ContrastNormalization(CShade_VS2PS_Quad Input) : SV_TARGET0
{
float4 LCN = GetLocalContrastNormalization(CShade_SampleColorTex, Input.Tex0);
return (dot(LCN.rgb, 1.0 / 3.0) * 0.5) + 0.5;
}

technique CShade_ContrastNormalization
{
pass
{
SRGBWriteEnable = WRITE_SRGB;

VertexShader = CShade_VS_Quad;
PixelShader = PS_ContrastNormalization;
}
}
121 changes: 0 additions & 121 deletions shaders/cTemplateMatching.fx

This file was deleted.

31 changes: 19 additions & 12 deletions shaders/kContour.fx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "shared/cShade.fxh"
#include "shared/cColor.fxh"
#include "shared/cEdge.fxh"

/*
Expand Down Expand Up @@ -32,7 +33,7 @@
uniform int _Method <
ui_label = "Edge Detection Method";
ui_type = "combo";
ui_items = "ddx(),ddy()\0Sobel: Bilinear 3x3\0Prewitt: Bilinear 5x5\0Sobel: Bilinear 5x5\0Prewitt: 3x3\0Scharr: 3x3\0";
ui_items = "ddx(),ddy()\0Sobel: Bilinear 3x3\0Prewitt: Bilinear 5x5\0Sobel: Bilinear 5x5\0Prewitt: 3x3\0Scharr: 3x3\0Frei-Chen\0";
> = 0;

uniform float _Threshold <
Expand Down Expand Up @@ -74,41 +75,47 @@ uniform float4 _BackColor <
[Pixel Shaders]
*/

float GetGradientLuma(CEdge_Gradient Input)
{
return sqrt(dot(Input.Ix.rgb, Input.Ix.rgb) + dot(Input.Iy.rgb, Input.Iy.rgb));
}

float3 PS_Grad(CShade_VS2PS_Quad Input) : SV_TARGET0
{
CEdge_Gradient Gradient;
float I = 0.0;

switch(_Method)
{
case 0: // ddx(), ddy()
Gradient = CEdge_GetDDXY(CShade_SampleColorTex, Input.Tex0);
I = GetGradientLuma(CEdge_GetDDXY(CShade_SampleColorTex, Input.Tex0));
break;
case 1: // Bilinear 3x3 Sobel
Gradient = CEdge_GetBilinearSobel3x3(CShade_SampleColorTex, Input.Tex0);
I = GetGradientLuma(CEdge_GetBilinearSobel3x3(CShade_SampleColorTex, Input.Tex0));
break;
case 2: // Bilinear 5x5 Prewitt
Gradient = CEdge_GetBilinearPrewitt5x5(CShade_SampleColorTex, Input.Tex0);
I = GetGradientLuma(CEdge_GetBilinearPrewitt5x5(CShade_SampleColorTex, Input.Tex0));
break;
case 3: // Bilinear 5x5 Sobel by CeeJayDK
Gradient = CEdge_GetBilinearSobel5x5(CShade_SampleColorTex, Input.Tex0);
I = GetGradientLuma(CEdge_GetBilinearSobel5x5(CShade_SampleColorTex, Input.Tex0));
break;
case 4: // 3x3 Prewitt
Gradient = CEdge_GetPrewitt3x3(CShade_SampleColorTex, Input.Tex0);
I = GetGradientLuma(CEdge_GetPrewitt3x3(CShade_SampleColorTex, Input.Tex0));
break;
case 5: // 3x3 Scharr
Gradient = CEdge_GetScharr3x3(CShade_SampleColorTex, Input.Tex0);
I = GetGradientLuma(CEdge_GetScharr3x3(CShade_SampleColorTex, Input.Tex0));
break;
case 6: // Frei-Chen
I = CColor_GetLuma(CEdge_GetFreiChen(CShade_SampleColorTex, Input.Tex0).rgb, 3);
break;
}

float4 I = sqrt(dot(Gradient.Ix.rgb, Gradient.Ix.rgb) + dot(Gradient.Iy.rgb, Gradient.Iy.rgb));

// Thresholding
I = I * _ColorSensitivity;
I = saturate((I - _Threshold) * _InverseRange);

float3 Base = tex2D(CShade_SampleColorTex, Input.Tex0.xy).rgb;
I = saturate((I - _Threshold) * _InverseRange);
float3 BackgoundColor = lerp(Base.rgb, _BackColor.rgb, _BackColor.a);
return lerp(BackgoundColor, _FrontColor.rgb, I.a * _FrontColor.a);
return lerp(BackgoundColor, _FrontColor.rgb, I * _FrontColor.a);
}

technique CShade_KinoContour
Expand Down

0 comments on commit 60b1e1e

Please sign in to comment.