双线性插值的实现
双线性插值根据周围4个点及与4个点的距离,来计算待插值像素的值
http://baike.baidu.com/link?url=4it85nPzCC9J0oZZGU7HEGuXbcDcOmzhtKuwTP9Nar9HLRyZdwKaocnn1yZOFJ7pdZjnXmTUpiKStH5nPs1qTq2V57th0nHQqc1m0_fzQXIklY4hFLnYg3T5kbqTibxqIUa_JFNV0sy8Y3_YX_XopK
下面是根据插值算法做的一个实现
void bilinear_c_int(uchar *pSrc, int srcWidth, int srcHeight, uchar *pDst, int dstWidth,
int dstHeight) {
const int N = 8;
const int FACTOR = pow(2, N);
float fx = (float)srcWidth / dstWidth;
float fy = (float)srcHeight / dstHeight;
int *x = new int[dstWidth];
int *a = new int[dstWidth];
int *y = new int[dstHeight];
int *b = new int[dstHeight];
for (int j = 0; j < dstWidth; j++) {
x[j] = (int)(fx * j);
a[j] = (fx*j - x[j]) * FACTOR ;
if (x[j] > srcWidth - 2) {
x[j] = srcWidth - 2;
a[j] = FACTOR;
}
}
for (int i = 0; i < dstHeight; i++) {
y[i] = (int)(fy * i);
b[i] = (fy *i - y[i]) * FACTOR;
if (y[i] > srcHeight - 2) {
y[i] = srcHeight - 2;
b[i] = FACTOR;
}
}
int index, p0, p1;
for (int i = 0; i < dstHeight; i++) {
for (int j = 0; j < dstWidth; j++) {
index = y[i] * srcWidth + x[j];
p0 = pSrc[index] * (FACTOR - a[j]) + pSrc[index + 1] * a[j];
p1 = pSrc[index + srcWidth + 1] * (FACTOR - a[j]) + pSrc[index + srcWidth + 1] * a[j];
pDst[i * dstWidth + j] = (p0 * (FACTOR - b[i]) + p1 * b[i]) >> (2 * N);
}
}
delete[] x;
delete[] a;
delete[] y;
delete[] b;
}