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:

Source Code:


  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

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

  3. I realize that nothing is fair but I'm still trying. I just want to do my best and what is possible