RGB图象缩放算法

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值