I'm trying to implement a bicubic interpolation algorithm to reconstruct higher-resolution data from a heightmap. After some falstarts and sets of instructions containing nearly-incomprehensible math (it's been some years since I had calculus and I no longer remember much beyond the basics) , I've found the article "Bicubic Interpolation for Image Scaling" by Paul Bourke containing what appeared to be a fairly simple and easy to implement algorithm. http://paulbourke.net/texture_colour/imageprocess/
However, instead of producing an interpolation result even remotely resembling the one on Wikipedia, instead I'm getting this (from the same input data):
What is causing the error, and more importantly - how to fix it?
PHP code below (yes, this probably should - and will - be reimplemented in C; when it's working)
class BicubicInterpolator
{
private $data;
public function Set_data($d)
{
$this->data=$this->denull($d);
}
public function Interpolate($dx,$dy)
{
$r=0;
for ($m=-1; $m<2; $m++)
for ($n=-1; $n<2; $n++)
$r+=$this->data[$m+1][$n+1] * $this->R($m-$dx) * $this->R($dy-$n);
return $r;
}
private function denull($d)
{
//Substituting null values with nearest known values as per "A Review of Some Image Pixel Interpolation Algorithms" by Don Lancaster (supposed to produce same output as example image)
if ($d[0][1]===null) for ($i=0; $i<4; $i++) $d[0][$i]=$d[1][$i];
if ($d[1][0]===null) for ($i=0; $i<4; $i++) $d[$i][0]=$d[$i][1];
if ($d[3][1]===null) for ($i=0; $i<4; $i++) $d[3][$i]=$d[2][$i];
if ($d[1][3]===null) for ($i=0; $i<4; $i++) $d[$i][3]=$d[$i][2];
return $d;
}
function R($x)
{
return ( $this->P($x+2)
- 4 * $this->P($x+1)
+ 6 * $this->P($x)
- 4 * $this->P($x-1) )/6;
}
function P($x)
{
if ($x>0) return $x*$x*$x;
return 0;
}