分享

RGB图象缩放算法

 都赢888 2013-04-10

RGB图象缩放算法

463人阅读 评论(0) 收藏 举报
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;
  }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多