Friday, May 6, 2011

Simple Fast Bilinear Color Interpolation


Simple, fast implementation of bilinear color interpolation.
The purpose of this snippet is to fill a rectangle with smooth colors interpolated from the four vertices.
This snippet is ripped from my Bengine voxel raycaster, where it is used to scale a single voxel and fill the gap of screen. Color differences are calculated to avoid multiplies, so there are only adds in the for loop. Certainly, the code can be optimized further, I leave it what it is because it's easier for understanding the algorithm.
If you only want to interpolate one single pixel use its neighbours, like the height interpolation in a terrain raycaster, try to find the implementation in this code.
By the way, the simple maths behind the algorithm:
http://en.wikipedia.org/wiki/Bilinear_interpolation

Source Code:
https://flaswf.googlecode.com/svn/trunk/Snippets/SimpleFastBilinearColorInterpolation/

2 comments:

  1. Hi Bruce, I wasn't sure if you'd see my fork on wonderfl, so I'm posting here too.

    I didn't do any benchmarks, but you can probably get a nice speed boost by using flash's built-in bilinear interpolation, see http://wonderfl.net/c/lC21

    ReplyDelete
  2. @yonatan:
    Nice! Thanks for pointing this out.
    In my custom rendering engine(Bengine), I still prefer manipulating pixel values directly.

    ReplyDelete

Sponsors