2010-05-01 82 views
0

我试图港口一些图像插值算法为HLSL代码,现在我得到:HLSL,计划像素着色器不同的Texture2D缩小算法

float2 texSize; 
float scale; 
int method; 

sampler TextureSampler : register(s0); 


float4 PixelShader(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0 
{ 
float2 newTexSize = texSize * scale; 
float4 tex2; 

if(texCoord[0] * texSize[0] > newTexSize[0] || 
texCoord[1] * texSize[1] > newTexSize[1]) 
{ 
tex2 = float4(0, 0, 0, 0); 

} else { 
if (method == 0) { 
     tex2 = tex2D(TextureSampler, float2(texCoord[0]/scale, texCoord[1]/scale)); 
    } else { 
     float2 step = float2(1/texSize[0], 1/texSize[1]); 

     float4 px1 = tex2D(TextureSampler, float2(texCoord[0]/scale-step[0], texCoord[1]/scale-step[1])); 
     float4 px2 = tex2D(TextureSampler, float2(texCoord[0]/scale  , texCoord[1]/scale-step[1])); 
     float4 px3 = tex2D(TextureSampler, float2(texCoord[0]/scale+step[0], texCoord[1]/scale-step[1])); 
     float4 px4 = tex2D(TextureSampler, float2(texCoord[0]/scale-step[0], texCoord[1]/scale  )); 
     float4 px5 = tex2D(TextureSampler, float2(texCoord[0]/scale+step[0], texCoord[1]/scale  )); 
     float4 px6 = tex2D(TextureSampler, float2(texCoord[0]/scale-step[0], texCoord[1]/scale+step[1])); 
     float4 px7 = tex2D(TextureSampler, float2(texCoord[0]/scale  , texCoord[1]/scale+step[1])); 
     float4 px8 = tex2D(TextureSampler, float2(texCoord[0]/scale+step[0], texCoord[1]/scale+step[1])); 
     tex2 = (px1+px2+px3+px4+px5+px6+px7+px8)/8; 
     tex2.a = 1; 

    } 
} 
return tex2; 
} 


technique Resample 
{ 
pass Pass1 
{ 
    PixelShader = compile ps_2_0 PixelShader(); 
} 
} 

的问题是,编程像素着色器需要不同的方法,因为我们没有对当前位置的控制,只有通过像素的实际循环的“内部”部分。

我一直在谷歌搜索了大约一整天,发现没有开放源码库与缩放算法在循环中使用。是否有这样的图书馆,我可以移植一些方法?

我发现http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx,但我真的不明白他对这个问题的方法,并且将它移植将是一个痛苦的...

维基百科告诉matematic方法。所以我的问题是:我在哪里可以找到易于移植的图形开放源代码库,其中包括简单的缩放算法?当然,如果这样的库甚至存在:)

回答

1

问题是着色器是一个功能域。你指的大多数算法都是用普通语言完成的,所以它们不会很容易移植。

您可以在这太问题看近邻图像等东西MATLAB缩放功能......比如找到一些伟大的信息:
Nearest-neighbor interpolation algorithm in MATLAB