| |
|
/*
|
|
Experimental 2x resize shader - fragment file (v0.001)
|
|
Copyright (C) 2007 guest(r) - guest.r@gmail.com
|
|
I'll try to code a shader which does some double image resizing.
|
9+ texels (box style) input, 4 result texels output, classic approach - so to say.
|
|
Since it's going to be a single pass shader, it'll do some bilinear interpolation afterwards.
|
The idea is borrowed from the Scale2xSaI algorithm from mr. Derek Liauw Kie Fa.
|
|
More patterns are to be added with time...
|
|
*/
|
|
|
|
const float p = 1.0; // "linear" Interpolation type
|
|
|
varying vec2 dx,dy,d1,d2;
|
|
uniform vec4 OGL2Size;
|
uniform sampler2D OGL2Texture;
|
|
void main()
|
{
|
|
// Calculating texel coordinates
|
|
vec2 OGL2Pos = gl_TexCoord[0].xy*OGL2Size.xy;
|
vec2 fp = fract(OGL2Pos);
|
|
vec2 d11 = floor(OGL2Pos)/OGL2Size.xy;
|
vec2 d00 = d11 - d1;
|
vec2 d10 = d11 - dy;
|
vec2 d20 = d11 - d2;
|
vec2 d01 = d11 - dx;
|
vec2 d21 = d11 + dx;
|
vec2 d02 = d11 + d2;
|
vec2 d12 = d11 + dy;
|
vec2 d22 = d11 + d1;
|
|
|
// Reading the texels
|
|
vec3 c00 = texture2D(OGL2Texture,d00).xyz;
|
vec3 c10 = texture2D(OGL2Texture,d10).xyz;
|
vec3 c20 = texture2D(OGL2Texture,d20).xyz;
|
vec3 c01 = texture2D(OGL2Texture,d01).xyz;
|
vec3 c11 = texture2D(OGL2Texture,d11).xyz;
|
vec3 c21 = texture2D(OGL2Texture,d21).xyz;
|
vec3 c02 = texture2D(OGL2Texture,d02).xyz;
|
vec3 c12 = texture2D(OGL2Texture,d12).xyz;
|
vec3 c22 = texture2D(OGL2Texture,d22).xyz;
|
vec3 dt = vec3(1.0,1.0,1.0);
|
|
|
// Calculating color differences
|
|
float f00 = dot(abs(c00-c11),dt);
|
float f10 = dot(abs(c10-c11),dt);
|
float f20 = dot(abs(c20-c11),dt);
|
float f01 = dot(abs(c01-c11),dt);
|
float f11 = dot(abs(c11-c11),dt);
|
float f21 = dot(abs(c21-c11),dt);
|
float f02 = dot(abs(c02-c11),dt);
|
float f12 = dot(abs(c12-c11),dt);
|
float f22 = dot(abs(c22-c11),dt);
|
float hrl = dot(abs(c01-c21),dt);
|
float vrl = dot(abs(c10-c12),dt);
|
float dg1 = dot(abs(c00-c22),dt);
|
float dg2 = dot(abs(c02-c20),dt);
|
|
|
// Calculating produtcs
|
|
vec3 p00,p10,p01,p11;
|
|
if ((f10==f21 && f01==f12 && f10!=f01) || (f01==f10 && f12==f21 && f01!=f12))
|
{
|
p00 = (c01+c11+c10)/3.0;
|
p10 = (c21+c11+c10)/3.0;
|
p01 = (c01+c11+c12)/3.0;
|
p11 = (c12+c11+c21)/3.0;
|
}
|
else
|
|
if (max(f00,f22)==0.0 && f02!=f20)
|
{
|
p00 = c11;
|
p10 = (c21+c11+c10)/3.0;
|
p01 = (c01+c11+c12)/3.0;
|
p11 = c11;
|
}
|
else
|
|
if (max(f02,f20)==0.0 && f00!=f22)
|
{
|
p00 = (c01+c11+c10)/3.0;
|
p10 = c11;
|
p01 = c11;
|
p11 = (c12+c11+c21)/3.0;
|
}
|
else
|
|
{
|
p00 = c11;
|
p10 = c11;
|
p01 = c11;
|
p11 = c11;
|
}
|
|
|
// product interpolation
|
|
vec2 x = vec2(pow(1.0-fp.x,p),pow(fp.x,p));
|
vec2 y = vec2(pow(1.0-fp.y,p),pow(fp.y,p));
|
|
gl_FragColor.xyz = ((p00*x.x+p10*x.y)*y.x+(p01*x.x+p11*x.y)*y.y)/((x.x+x.y)*(y.x+y.y));
|
|
}
|
|
|