void StretchColors(void* pDest, int nDestWidth, int nDestHeight, int nDestBits, void* pSrc, int nSrcWidth, int nSrcHeight, int nSrcBits)
{
//参数有效性检查
//ASSERT_EXP(pDest != NULL);
//ASSERT_EXP((nDestBits == 32) || (nDestBits == 24));
//ASSERT_EXP((nDestWidth > 0) && (nDestHeight > 0));
//ASSERT_EXP(pSrc != NULL);
//ASSERT_EXP((nSrcBits == 32) || (nSrcBits == 24));
//ASSERT_EXP((nSrcWidth > 0) && (nSrcHeight > 0));
//令dfAmplificationX和dfAmplificationY分别存储水平和垂直方向的放大率
double dfAmplificationX = ((double)nDestWidth)/nSrcWidth;
double dfAmplificationY = ((double)nDestHeight)/nSrcHeight;
//计算单个源位图颜色和目的位图颜色所占字节数
const int nSrcColorLen = nSrcBits/8;
const int nDestColorLen = nDestBits/8;
//进行图片缩放计算
for(int i = 0; i<nDestHeight; i++) //处理第i行
for(int j = 0; j<nDestWidth; j++) //处理第i行中的j列
{
//------------------------------------------------------
//以下代码将计算nLine和nRow的值,并把目的矩阵中的(i, j)点
//映射为源矩阵中的(nLine, nRow)点,其中,nLine的取值范围为
//[0, nSrcHeight-1],nRow的取值范围为[0, nSrcWidth-1],
double tmp = i/dfAmplificationY;
int nLine = (int)tmp;
if(tmp - nLine > 0.5)
++nLine;
if(nLine >= nSrcHeight)
--nLine;
tmp = j/dfAmplificationX;
int nRow = (int)tmp;
if(tmp - nRow > 0.5)
++nRow;
if(nRow >= nSrcWidth)
--nRow;
unsigned char *pSrcPos = (unsigned char*)pSrc + (nLine*nSrcWidth + nRow)*nSrcColorLen;
unsigned char *pDestPos = (unsigned char*)pDest + (i*nDestWidth + j)*nDestColorLen;
//把pSrcPos位置的前三字节拷贝到pDestPos区域
*pDestPos++ = *pSrcPos++;
*pDestPos++ = *pSrcPos++;
*pDestPos++ = *pSrcPos++;
if(nDestColorLen == 4)
*pDestPos = 0;
}
}
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/hzb1983/archive/2007/10/09/1816200.aspx