Bilinear filtering is the process of extracting several pixels of the original texture and then averaging their values to get the final pixel value. The concept of “ bilinear filtering ”, just like the similar concept of “ trilinear filtering ”, applies only to two-dimensional textures. For three-dimensional, for example, this concept is not applicable, and the concept of trilinear filtering has a completely different meaning.
Bilinear filtering source code example
In code [1] , it is assumed that the texture is square (the most common case), and also that there is only one data channel.
double getBilinearFilteredPixelColor ( Texture tex , double u , double v ) { u * = tex . size ; v * = tex . size ; int x = floor ( u ); int y = floor ( v ); double u_ratio = u - x ; double v_ratio = v - y ; double u_opposite = 1 - u_ratio ; double v_opposite = 1 - v_ratio ; double result = ( tex [ x ] [ y ] * u_opposite + tex [ x + 1 ] [ y ] * u_ratio ) * v_opposite + ( tex [ x ] [ y + 1 ] * u_opposite + tex [ x + 1 ] [ y + 1 ] * u_ratio ) * v_ratio ; return result ; }
The same example in the HLSL shader language
float4 Bilinear ( sampler2D tex , float2 texCoord , int texSize )
{
float2 trTexCoord = texCoord * texSize ;
float2 texf = floor ( trTexCoord );
float2 ratio = trTexCoord - texf ;
float2 opposite = 1.0 - ratio ;
float4 result = ( tex2D ( tex , texf / texSize ) * opposite . x + tex2D ( tex , ( texf + float2 ( 1 , 0 )) / texSize ) * ratio . x ) * opposite . y +
( tex2D ( tex , ( texf + float2 ( 0 , 1 )) / texSize ) * opposite . x + tex2D ( tex , ( texf + float2 ( 1 , 1 )) / texSize ) * ratio . x ) * ratio . y ;
return result ;
}
Notes
- ↑ Language - C
See also
- Texture filtering
- Bilinear interpolation