Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mpc-hc Edge Sharpen mod and optimization
#define Show_Edge 20
//[0, 1, 2, 20] default 0: edge sharpen, 1: show edges in Red, 2: show luma edges, 20 luma edges on dark background
#define Show_SplitScreen 1 //[0, 1, 2] default 0: no splitscreen, 1: horizontal split, 2 vertical split
/*
Edge detection and Edge sharpening, using Four 3x3 Prewitt operators.
Software Licence: GPL v3
the original Edge Sharpen is distributed with MPC-HC: https://www.videohelp.com/software/Media-Player-Classic-Home-Cinema
2020: - modification and optimization by butterw (9 texture, 47 arithmetic) >> (9 texture, 34 arithmetic)
- edge detection on Luma (Show_Edge 20, 8 texture, 30 arithmetic)
Can be used pre or post-resize (Show_SplitScreen post-resize only really works in fullscreen). Source must be clean high-res to get nice visible edges.
*/
sampler s0: register(s0);
float2 p1: register(c1);
#define Edge_Threshold 0.2
#define Sharpen_val0 2.0
#define Sharpen_val1 0.125
//#define NbPixel 1.
#define dx p1.x //NbPixel*p1.x
#define dy p1.y //NbPixel*p1.y
#define CoefLuma float4(0.212656, 0.715158, 0.072186, 0) // BT.709 & sRBG luma coef (Monitors, HDTV)
#define Red float4(1, 0, 0, 0)
#define Black16 float4(16, 16, 16, 0)/255.
float4 main(float2 tex: TEXCOORD0): COLOR {
// Get neighbor points:
// [ 1, 2, 3 ]
// [ 4, 0, 5 ]
// [ 6, 7, 8 ]
float4 c0 = tex2D(s0, tex);
#if Show_SplitScreen == 1
if (tex.x<0.5) return c0;
#elif Show_SplitScreen == 2
if (tex.y<0.5) return c0;
#endif
float4 c1 = tex2D(s0, tex - p1);
float4 c2 = tex2D(s0, tex + float2( 0, -dy));
float4 c3 = tex2D(s0, tex + float2( dx, -dy));
float4 c4 = tex2D(s0, tex + float2(-dx, 0));
float4 c5 = tex2D(s0, tex + float2( dx, 0));
float4 c6 = tex2D(s0, tex + float2(-dx, dy));
float4 c7 = tex2D(s0, tex + float2( 0, dy));
float4 c8 = tex2D(s0, tex + p1);
/* Computation of the 3 derived vectors (hor, vert, diag1, diag2)
// float4 delta1 = c6 + c4 + c1 - c3 - c5 - c8;
// float4 delta2 = c1 + c2 + c4 - c5 - c8 - c7;
// float4 delta3 = c1 + c2 + c3 - c8 - c7 - c6;
// float4 delta4 = c2 + c3 + c5 - c7 - c6 - c4;
Perf optimized by reusing calculated expressions: */
float4 delta1 = -c3 + c6 + c4;
float4 delta2 = c1 - c5 - c8;
float4 delta4 = c2 - c7 + c5 - delta1;
delta1+= delta2;
delta2+= c2 - c7 + c4;
c1 = c1 + c2 + c3;
c6 = c6 + c7 + c8;
float4 delta3 = c1 - c6;
// Computation of the Prewitt operator
// float edge = length(abs(delta1) + abs(delta2) + abs(delta3) + abs(delta4))/6;
float edge = sqrt(dot(abs(delta1) + abs(delta2) + abs(delta3) + abs(delta4), CoefLuma))*0.25; // Luma Edge detection
if (edge > Edge_Threshold) {
#if Show_Edge == 0 // If we have an edge (vector length > Edge_threshold) => apply sharpen filter
c0 = c0*Sharpen_val0 - Sharpen_val1*(c1 + c4 + c5 + c6);
#elif Show_Edge == 1 // Display edges in Red
return Red;
#elif (Show_Edge == 2 || Show_Edge == 20) // Display Luma edges
return edge;
#endif
}
#if Show_Edge == 20 // Show Edge on Dark background
return Black16;
#endif
return c0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.