f0,f1,f2,f3为取样点所在行的指针,u,v为小数,dst指针指向目标像素
1.双线性插值-BiLinear Interpolation
typedef double FLPT;
void BiLinear(UCHAR *f0, UCHAR *f1, FLPT u, FLPT v, UCHAR *dst)
{
FLPT r0, r1, rt;
for(int i = 0; i < 3; ++i)
{
r0 = (1- u) * f0[i] + u * f0[i+3];
r1 = (1- u) * f1[i] + u * f1[i+3];
rt = (1-v)*r0+v*r1 + 0.5;
if(rt < 0) { rt = 0; }
else if(rt > 255) { rt = 255; }
dst[i] = (UCHAR)rt;
}
}
2.双三次插值-BiCubic Interpolation
typedef double FLPT;
FLPT Cubic(FLPT x)
{
FLPT tmp = fabs(x);
if(tmp < 1)
{
FLPT k2,k3;
k2 = tmp*tmp;
k3 = tmp*k2;
return 1 - 2*k2 + k3;
}
else if(tmp < 2)
{
FLPT k1,k2,k3;
k1 = tmp;
k2 = k1*k1;
k3 = k1*k2;
return 4 - 8*k1 + 5*k2 - k3;
}
else
{ return 0; }
}
void BiCubic(UCHAR *f0, UCHAR *f1, UCHAR *f2, UCHAR *f3, FLPT u, FLPT v, UCHAR *dst)
{
FLPT Su0, Su1, Su2, Su3;
FLPT Sv0, Sv1, Sv2, Sv3;
Su0 = Cubic(1 + u);
Su1 = Cubic(u);
Su2 = Cubic(1 - u);
Su3 = Cubic(2 - u);
Sv0 = Cubic(1 + v);
Sv1 = Cubic(v);
Sv2 = Cubic(1 - v);
Sv3 = Cubic(2 - v);
FLPT r0, r1, r2, r3, rt;
for(int k = 0; k < 3; ++k)
{
r0 = f0[k]*Su0 + f0[k+3]*Su1 + f0[k+6]*Su2 + f0[k+9]*Su3;
r1 = f1[k]*Su0 + f1[k+3]*Su1 + f1[k+6]*Su2 + f1[k+9]*Su3;
r2 = f2[k]*Su0 + f2[k+3]*Su1 + f2[k+6]*Su2 + f2[k+9]*Su3;
r3 = f3[k]*Su0 + f3[k+3]*Su1 + f3[k+6]*Su2 + f3[k+9]*Su3;
rt = r0*Sv0 + r1*Sv1 + r2*Sv2 + r3*Sv3 + 0.5;
if(rt > 255) { rt = 255; }
else if(rt < 0) { rt = 0; }
dst[k] = (UCHAR)rt;
}
}
wiki上的相应链接:
1.http://zh.wikipedia.org/zh-hans/%E5%8F%8C%E7%BA%BF%E6%80%A7%E6%8F%92%E5%80%BC