void StretchColors(void* pDest, int nDestWidth, int nDestHeight, int nDestBits, void* pSrc, int nSrcWidth, int nSrcHeight, int nSrcBits)
{ //參數(shù)有效性檢查 //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; //計(jì)算單個源位圖顏色和目的位圖顏色所占字節(jié)數(shù) const int nSrcColorLen = nSrcBits/8; const int nDestColorLen = nDestBits/8; //進(jìn)行圖片縮放計(jì)算 for(int i = 0; i<nDestHeight; i++) //處理第i行 for(int j = 0; j<nDestWidth; j++) //處理第i行中的j列 { //------------------------------------------------------ //以下代碼將計(jì)算nLine和nRow的值,并把目的矩陣中的(i, j)點(diǎn) //映射為源矩陣中的(nLine, nRow)點(diǎn),其中,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位置的前三字節(jié)拷貝到pDestPos區(qū)域 *pDestPos++ = *pSrcPos++; *pDestPos++ = *pSrcPos++; *pDestPos++ = *pSrcPos++; if(nDestColorLen == 4) *pDestPos = 0; } } |
|