Tuesday, December 16, 2014

Fractional Bilinear Interpolation

I've recently developed a new image scaling algorithm.  Out of the box, DirectX hardware supports two basic texture scaling methods: nearest-neighbor interpolation and bilinear interpolation, neither of which work very well for upscaling NES graphics.  Nearest-neighbor interpolation results in aliasing for non-integer scaling ratios, and bilinear interpolation results in blurry images.  Fractional Bilinear Interpolation, so called because bilinear filtering is applied to only a portion of the image (and because it makes for a decent backronym), results in crisp, yet smooth upscaling.

In the following examples, the images have been scaled 2x vertically, and 2.333x horizontally (emulating how the image would be displayed on a 4:3 TV).

Nearest-neighbor interpolation retains the crisp edges of the individual pixels, but it doesn't work very well for non-integer scaling ratios.  Note the jagged black line on the hill and the inconsistent scaling on the floor.

Bilinear interpolation fixes the jagged lines, but results in an overly blurry image.

Iñigo Quílez developed an improved bilinear filtering method that is a pretty good compromise between nearest-neighbor and bilinear interpolation.  The idea is to tweak the input to a bilinear texture sampler, applying a curve so that less filtering is applied when the sample is farther away from a texel boundary.

The result is pretty good, but still a bit too blurry.  Additionally, there's interpolation on the y axis where none is required.

FBI uses the same basic technique of modifying the input to the texture sampler, but a different algorithm.

/* Copyright (c) 2013 James Slepicka Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ Texture2D tex; SamplerState sam_linear { Filter = MIN_MAG_MIP_LINEAR; AddressU = Clamp; AddressV = Clamp; }; matrix world; matrix view; matrix proj; float2 tex_size; float2 input_size; float2 output_size; float sharpness = 1.0; struct VS_INPUT { float4 pos : POSITION; float2 tex : TEXCOORD0; }; struct PS_INPUT { float4 pos : SV_POSITION; float2 tex : TEXCOORD0; }; PS_INPUT VS (VS_INPUT input) { PS_INPUT output = (PS_INPUT)0; output.pos = mul (input.pos, world); output.pos = mul (output.pos, view); output.pos = mul (output.pos, proj); output.tex = input.tex; return output; } float4 PS (PS_INPUT input) : SV_Target { float2 scale = output_size / input_size; float2 interp = saturate((scale - 1.0)/(scale * 2.0) * sharpness); float2 location = input.tex.xy * tex_size + .5; float2 location_int_part = floor(location); float2 location_frac_part = location - location_int_part; location_frac_part = saturate((location_frac_part - interp) / (1.0 - interp * 2.0)); location = ((location_int_part + location_frac_part) - .5) / tex_size; float4 r = tex.Sample(sam_linear, location); r.a = 1.0; return r; } technique10 render { pass P0 { SetVertexShader(CompileShader(vs_4_0, VS())); SetGeometryShader(NULL); SetPixelShader(CompileShader(ps_4_0, PS())); } }
blah blah blah

No comments:

Post a Comment