/*
   Paint filter from MariENB (second pass median smoothing)
   (C)2012-2022 Marisa the Magician
*/
#define luminance(x) dot(x,vec3(0.2126,0.7152,0.0722))

void main()
{
	vec2 coord = TexCoord;
	vec4 res = vec4(1.);
	vec2 bresl = textureSize(InputTexture,0);
	vec2 bof = 1./bresl;
	vec3 m1, m2, m3;
	vec3 a, b, c;
	a = texture(InputTexture,coord+vec2(-1,-1)*bof).rgb;
	b = texture(InputTexture,coord+vec2( 0,-1)*bof).rgb;
	c = texture(InputTexture,coord+vec2( 1,-1)*bof).rgb;
	m1 = (luminance(a)<luminance(b))?((luminance(b)<luminance(c))?b
		:max(a,c)):((luminance(a)<luminance(c))?a:max(b,c));
	a = texture(InputTexture,coord+vec2(-1, 0)*bof).rgb;
	b = texture(InputTexture,coord+vec2( 0, 0)*bof).rgb;
	c = texture(InputTexture,coord+vec2( 1, 0)*bof).rgb;
	m2 = (luminance(a)<luminance(b))?((luminance(b)<luminance(c))?b
		:max(a,c)):((luminance(a)<luminance(c))?a:max(b,c));
	a = texture(InputTexture,coord+vec2(-1, 1)*bof).rgb;
	b = texture(InputTexture,coord+vec2( 0, 1)*bof).rgb;
	c = texture(InputTexture,coord+vec2( 1, 1)*bof).rgb;
	m3 = (luminance(a)<luminance(b))?((luminance(b)<luminance(c))?b
		:max(a,c)):((luminance(a)<luminance(c))?a:max(b,c));
	res.rgb = (luminance(m1)<luminance(m2))?((luminance(m2)<luminance(m3))
		?m2:max(m1,m3)):((luminance(m1)<luminance(m3))?m1:max(m2,m3));
	FragColor = res;
}
