Skip to content

Commit

Permalink
Seperate shared functions into detailed helper files
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku committed Jun 28, 2024
1 parent 6640fb4 commit 17c11b1
Show file tree
Hide file tree
Showing 20 changed files with 509 additions and 510 deletions.
2 changes: 1 addition & 1 deletion shaders/cBilateral.fx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"
#include "shared/cConvolution.fxh"

float4 PS_Bilateral(VS2PS_Quad Input) : SV_TARGET0
{
Expand Down
5 changes: 3 additions & 2 deletions shaders/cBlockMatching.fx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "shared/cBuffers.fxh"
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"
#include "shared/cVideoProcessing.fxh"
#include "shared/cColorSpaces.fxh"
#include "shared/cConvolution.fxh"
#include "shared/cMotionEstimation.fxh"

namespace cBlockMatching
{
Expand Down
2 changes: 1 addition & 1 deletion shaders/cChromaticity.fx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"
#include "shared/cColorSpaces.fxh"

/*
[Shader Options]
Expand Down
2 changes: 1 addition & 1 deletion shaders/cColorBand.fx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"
#include "shared/cProcedural.fxh"

/*
[Shader Options]
Expand Down
2 changes: 1 addition & 1 deletion shaders/cDiscBlur.fx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "shared/cBuffers.fxh"
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"
#include "shared/cConvolution.fxh"

namespace cDiscBlur
{
Expand Down
2 changes: 1 addition & 1 deletion shaders/cGaussianBlur.fx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"
#include "shared/cConvolution.fxh"

/*
[Shader Options]
Expand Down
2 changes: 1 addition & 1 deletion shaders/cGrain.fx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"
#include "shared/cConvolution.fxh"

/*
[Shader Options]
Expand Down
6 changes: 4 additions & 2 deletions shaders/cMotionBlur.fx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "shared/cBuffers.fxh"
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"
#include "shared/cVideoProcessing.fxh"
#include "shared/cColorSpaces.fxh"
#include "shared/cConvolution.fxh"
#include "shared/cMotionEstimation.fxh"
#include "shared/cProcedural.fxh"

namespace cMotionBlur
{
Expand Down
2 changes: 1 addition & 1 deletion shaders/cNoiseBlur.fx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"
#include "shared/cProcedural.fxh"

/*
[Shader Options]
Expand Down
5 changes: 3 additions & 2 deletions shaders/cOpticalFlow.fx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "shared/cBuffers.fxh"
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"
#include "shared/cVideoProcessing.fxh"
#include "shared/cColorSpaces.fxh"
#include "shared/cConvolution.fxh"
#include "shared/cMotionEstimation.fxh"

/*
MIT License
Expand Down
5 changes: 3 additions & 2 deletions shaders/kDatamosh.fx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "shared/cBuffers.fxh"
#include "shared/cGraphics.fxh"
#include "shared/cImageProcessing.fxh"
#include "shared/cVideoProcessing.fxh"
#include "shared/cColorSpaces.fxh"
#include "shared/cConvolution.fxh"
#include "shared/cMotionEstimation.fxh"

/*
This is free and unencumbered software released into the public domain.
Expand Down
3 changes: 2 additions & 1 deletion shaders/kMirror.fx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ float4 PS_Mirror(VS2PS_Quad Input) : SV_TARGET0
Phi += _Roll - _Offset;

// Convert back to the texture coordinate.
float2 PhiSinCos; sincos(Phi, PhiSinCos.x, PhiSinCos.y);
float2 PhiSinCos;
sincos(Phi, PhiSinCos.x, PhiSinCos.y);
Input.Tex0 = ((PhiSinCos.yx * Radius) * 0.5) + 0.5;

// Reflection at the border of the screen.
Expand Down
207 changes: 207 additions & 0 deletions shaders/shared/cColorSpaces.fxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@

#if !defined(INCLUDE_COLORSPACES)
#define INCLUDE_COLORSPACES

float3 GetSumChromaticity(float3 Color, int Method)
{
float Sum = 0.0;
float White = 0.0;

switch(Method)
{
case 0: // Length
Sum = length(Color);
White = 1.0 / sqrt(3.0);
break;
case 1: // Dot3 Average
Sum = dot(Color, 1.0 / 3.0);
White = 1.0;
break;
case 2: // Dot3 Sum
Sum = dot(Color, 1.0);
White = 1.0 / 3.0;
break;
case 3: // Max
Sum = max(max(Color.r, Color.g), Color.b);
White = 1.0;
break;
}

float3 Chromaticity = (Sum == 0.0) ? White : Color / Sum;
return Chromaticity;
}

/*
Ratio-based chromaticity
*/

float2 GetRatioRG(float3 Color)
{
float2 Ratio = (Color.z == 0.0) ? 1.0 : Color.xy / Color.zz;
// x / (x + 1.0) normalizes to [0, 1] range
return Ratio / (Ratio + 1.0);
}

/*
https://www.microsoft.com/en-us/research/publication/ycocg-r-a-color-space-with-rgb-reversibility-and-low-dynamic-range/
---
YCoCg-R: A Color Space with RGB Reversibility and Low Dynamic Range
Henrique S. Malvar, Gary Sullivan
MSR-TR-2003-103 | July 2003
---
Technical contribution to the H.264 Video Coding Standard. Joint Video Team (JVT) of ISO/IEC MPEG & ITU-T VCEG (ISO/IEC JTC1/SC29/WG11 and ITU-T SG16 Q.6) Document JVT-I014r3.
*/

float2 GetCoCg(float3 Color)
{
float2 CoCg = 0.0;
float2x3 MatCoCg = float2x3
(
float3(1.0, 0.0, -1.0),
float3(-0.5, 1.0, -0.5)
);

CoCg.x = dot(Color, MatCoCg[0]);
CoCg.y = dot(Color, MatCoCg[1]);

return (CoCg * 0.5) + 0.5;
}

/*
RGB to CrCb
---
https://docs.opencv.org/4.7.0/de/d25/imgproc_color_conversions.html
*/

float2 GetCrCb(float3 Color)
{
float Y = dot(Color, float3(0.299, 0.587, 0.114));
return ((Color.br - Y) * float2(0.564, 0.713)) + 0.5;
}

/*
RGB to saturation value.
---
Golland, Polina, and Alfred M. Bruckstein. "Motion from color."
Computer Vision and Image Understanding 68, no. 3 (1997): 346-362.
---
http://www.cs.technion.ac.il/users/wwwb/cgi-bin/tr-get.cgi/1995/CIS/CIS9513.pdf
*/

float SaturateRGB(float3 Color)
{
// Calculate min and max RGB
float MinColor = min(min(Color.r, Color.g), Color.b);
float MaxColor = max(max(Color.r, Color.g), Color.b);

// Calculate normalized RGB
float SatRGB = (MaxColor - MinColor) / MaxColor;
SatRGB = (MaxColor == 0.0) ? 0.0 : SatRGB;

return SatRGB;
}

/*
Color-space conversion
---
https://github.com/colour-science/colour
---
Copyright 2013 Colour Developers
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*/

float3 GetHSVfromRGB(float3 Color)
{
float MinRGB = min(min(Color.r, Color.g), Color.b);
float MaxRGB = max(max(Color.r, Color.g), Color.b);
float DeltaRGB = MaxRGB - MinRGB;

// Calculate hue
float3 Hue = (Color.gbr - Color.brg) / DeltaRGB;
Hue = (Hue * (60.0 / 360.0)) + (float3(0.0, 120.0, 240.0) / 360.0);
float OutputHue = 0.0;
OutputHue = (MaxRGB == Color.r) ? Hue.r : OutputHue;
OutputHue = (MaxRGB == Color.g) ? Hue.g : OutputHue;
OutputHue = (MaxRGB == Color.b) ? Hue.b : OutputHue;

float3 Output = 0.0;
Output.x = (DeltaRGB == 0.0) ? 0.0 : OutputHue;
Output.y = (DeltaRGB == 0.0) ? 0.0 : DeltaRGB / MaxRGB;
Output.z = MaxRGB;
return Output;
}

float3 GetHSLfromRGB(float3 Color)
{
float MinRGB = min(min(Color.r, Color.g), Color.b);
float MaxRGB = max(max(Color.r, Color.g), Color.b);
float AddRGB = MaxRGB + MinRGB;
float DeltaRGB = MaxRGB - MinRGB;

float Lightness = AddRGB / 2.0;
float Saturation = (Lightness < 0.5) ? DeltaRGB / AddRGB : DeltaRGB / (2.0 - AddRGB);

// Calculate hue
float3 Hue = (Color.gbr - Color.brg) / DeltaRGB;
Hue = (Hue * (60.0 / 360.0)) + (float3(0.0, 120.0, 240.0) / 360.0);
float OutputHue = 0.0;
OutputHue = (MaxRGB == Color.r) ? Hue.r : OutputHue;
OutputHue = (MaxRGB == Color.g) ? Hue.g : OutputHue;
OutputHue = (MaxRGB == Color.b) ? Hue.b : OutputHue;

float3 Output = 0.0;
Output.x = (DeltaRGB == 0.0) ? 0.0 : OutputHue;
Output.y = (DeltaRGB == 0.0) ? 0.0 : Saturation;
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
Link: https://www.researchgate.net/publication/4138051_Robust_optical_flow_from_photometric_invariants
*/

float2 GetSphericalRG(float3 Color)
{
const float HalfPi = 1.0 / acos(0.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
Loading

0 comments on commit 17c11b1

Please sign in to comment.