// testC.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int a[100][100] = { 0 };
int b[100][100] = { 0 };
/*生成初始参考权重*/
void genRefWgt(int(*a)[100],int w,int h)
{
int start_w = 0;
int start_h = 0;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
*(*(a + i) + j) = 20;
}
}
start_w = w * 10 / 100;
start_h = h * 10 / 100;
for (int i = start_h; i < h - start_h; i++)
{
for (int j = start_w; j < w - start_w; j++)
{
*(*(a + i) + j) = 80;
}
}
start_w = w * 30 / 100;
start_h = h * 30 / 100;
for (int i = start_h; i < h - start_h; i++)
{
for (int j = start_w; j < w - start_w; j++)
{
*(*(a + i) + j) = 100;
}
}
}
/*双线性插值权重*/
int getSrcWgt(int(*a)[100],float idxw,float idxh)
{
float result = 0;
int x0 = (int)idxw;
int y0 = (int)idxh;
float u = idxw - x0;
float v = idxh - y0;
result = (1 - u)*(1 - v) * (*(*(a + y0) + x0)) + (1 - u) * v * (*(*(a + y0 + 1) + x0)) + \
u * (1 - v) * (*(*(a + y0) + x0 + 1)) + u * v * (*(*(a + y0 + 1) + x0 + 1));
return (int)result;
}
/*生成目标权重*/
void genDstWgt(int(*a0)[100], int(*a1)[100], int w0, int h0, int w1, int h1)
{
float wRatio = (float)w0 / w1;
float hRatio = (float)h0 / h1;
float idxi = 0, idxj = 0;
for (int i = 0; i < h1; i++)
{
for (int j = 0; j < w1 ; j++)
{
idxj = wRatio * j;
idxi = hRatio * i;
if (j == w1 - 1 || i == h1 - 1)
{
*(*(a1 + i) + j) = *(*(a0 + (int)idxi) + (int)idxj);
}
else
{
*(*(a1 + i) + j) = getSrcWgt(a0, idxj, idxi);
}
}
}
}
int main(int argc, char* argv[])
{
int x0 = 15, y0 = 15;
genRefWgt(a, x0, y0);
for (int i = 0; i < y0; i++)
{
for (int j = 0; j < x0; j++)
{
cout << a[i][j] << " ,";
if (j == x0-1)
{
cout << endl;
}
}
}
int x1 = 15, y1 = 17;
genDstWgt(a, b, 15, 15, x1, y1);
cout << "#######################################" << endl;
for (int i = 0; i < y1; i++)
{
for (int j = 0; j < x1; j++)
{
cout << b[i][j] << " ,";
if (j == x1-1)
{
cout << endl;
}
}
}
system("pause");
return 0;
}