Skip to content

Commit

Permalink
Added HSI chromaticity option
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku committed May 1, 2024
1 parent 766bd71 commit 2f43d7f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
2 changes: 1 addition & 1 deletion shaders/cBlockMatching.fx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace cBlockMatching
float2 PS_Normalize(VS2PS_Quad Input) : SV_TARGET0
{
float3 Color = tex2D(CShade_SampleColorTex, Input.Tex0).rgb;
return GetSumChromaticity(Color, 3).xy;
return GetSphericalRG(Color).xy;
}

float4 PS_Copy_0(VS2PS_Quad Input) : SV_TARGET0
Expand Down
13 changes: 8 additions & 5 deletions shaders/cChromaticity.fx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ uniform int _Select <
ui_label = "Method";
ui_tooltip = "Select Chromaticity";
ui_type = "combo";
ui_items = " Length (XY)\0 Length (XYZ)\0 Average (XY)\0 Average (XYZ)\0 Sum (XY)\0 Sum (XYZ)\0 Max (XY)\0 Max (XYZ)\0 Ratio (XY)\0 Spherical (XY)\0 Hue-Saturation (HSL)\0 Hue-Saturation (HSV)\0 CoCg (XY)\0 CrCb (XY)\0";
ui_items = " Length (XY)\0 Length (XYZ)\0 Average (XY)\0 Average (XYZ)\0 Sum (XY)\0 Sum (XYZ)\0 Max (XY)\0 Max (XYZ)\0 Ratio (XY)\0 Spherical (XY)\0 Hue-Saturation (HSI)\0 Hue-Saturation (HSL)\0 Hue-Saturation (HSV)\0 CoCg (XY)\0 CrCb (XY)\0";
> = 0;

/*
Expand Down Expand Up @@ -54,16 +54,19 @@ float4 PS_Chromaticity(VS2PS_Quad Input) : SV_TARGET0
case 9: // Spherical (XY)
Chromaticity.rg = GetSphericalRG(Color);
break;
case 10: // Hue-Saturation (HSL)
case 10: // Hue-Saturation (HSI)
Chromaticity.rg = GetHSIfromRGB(Color).gg;
break;
case 11: // Hue-Saturation (HSL)
Chromaticity.rg = GetHSLfromRGB(Color).rg;
break;
case 11: // Hue-Saturation (HSV)
case 12: // Hue-Saturation (HSV)
Chromaticity.rg = GetHSVfromRGB(Color).rg;
break;
case 12: // CoCg (XY)
case 13: // CoCg (XY)
Chromaticity.rg = GetCoCg(Gamma);
break;
case 13: // CrCb (XY)
case 14: // CrCb (XY)
Chromaticity.rg = GetCrCb(Gamma);
break;
default: // No Chromaticity
Expand Down
2 changes: 1 addition & 1 deletion shaders/cMotionBlur.fx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace cMotionBlur
float2 PS_Normalize(VS2PS_Quad Input) : SV_TARGET0
{
float3 Color = tex2D(CShade_SampleColorTex, Input.Tex0).rgb;
return GetSumChromaticity(Color, 3).xy;
return GetSphericalRG(Color).xy;
}

float2 PS_HBlur_Prefilter(VS2PS_Quad Input) : SV_TARGET0
Expand Down
2 changes: 1 addition & 1 deletion shaders/cOpticalFlow.fx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ namespace cOpticalFlow
float2 PS_Normalize(VS2PS_Quad Input) : SV_TARGET0
{
float3 Color = tex2D(CShade_SampleColorTex, Input.Tex0).rgb;
return GetSumChromaticity(Color, 3).xy;
return GetSphericalRG(Color).xy;
}

float2 PS_HBlur_Prefilter(VS2PS_Quad Input) : SV_TARGET0
Expand Down
2 changes: 1 addition & 1 deletion shaders/kDatamosh.fx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ namespace kDatamosh
float2 PS_Normalize(VS2PS_Quad Input) : SV_TARGET0
{
float3 Color = tex2D(CShade_SampleColorTex, Input.Tex0).rgb;
return GetSumChromaticity(Color, 3).xy;
return GetSphericalRG(Color).xy;
}

float2 PS_HBlur_Prefilter(VS2PS_Quad Input) : SV_TARGET0
Expand Down
51 changes: 39 additions & 12 deletions shaders/shared/cImageProcessing.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -378,18 +378,6 @@
return SatRGB;
}

float2 GetSphericalRG(float3 Color)
{
const float IHalfPi = 1.0 / acos(0.0);
const float2 White = acos(rsqrt(float2(2.0, 3.0)));

float2 DotC = 0.0;
DotC.x = dot(Color.xy, Color.xy);
DotC.y = dot(Color.xyz, Color.xyz);
float2 P = (DotC == 0.0) ? White : acos(abs(Color.xz * rsqrt(DotC)));
return saturate(P * IHalfPi);
}

/*
Color-space conversion
---
Expand Down Expand Up @@ -453,4 +441,43 @@
Output.z = Lightness;
return Output;
}

/*
This code is based on the algorithm described in the following paper:
Author(s): Joost van de Weijer, T. Gevers
Title: "Robust optical flow from photometric invariants"
Year: 2004
DOI: 10.1109/ICIP.2004.1421433
*/

float2 GetSphericalRG(float3 Color)
{
const float HalfPi = 1.0 / acos(0.0);
const float2 White = acos(rsqrt(float2(2.0, 3.0)));

// Precalculate (x*x + y*y)^0.5 and (x*x + y*y + z*z)^0.5
float L1 = length(Color.rg);
float L2 = length(Color.rgb);

float2 Angles = 0.0;
Angles[0] = (L1 == 0.0) ? 1.0 / sqrt(2.0) : Color.g / L1;
Angles[1] = (L2 == 0.0) ? 1.0 / sqrt(3.0) : L1 / L2;

return saturate(asin(abs(Angles)) * HalfPi);
}

float3 GetHSIfromRGB(float3 Color)
{
float3 O = Color.rrr;
O += (Color.ggg * float3(-1.0, 1.0, 1.0));
O += (Color.bbb * float3(0.0, -2.0, 1.0));
O *= rsqrt(float3(2.0, 6.0, 3.0));

float H = atan(O.x/O.y) / acos(0.0);
H = (H * 0.5) + 0.5; // We also scale to [0,1] range
float S = length(O.xy);
float I = O.z;

return float3(H, S, I);
}
#endif

0 comments on commit 2f43d7f

Please sign in to comment.