实现一:存在栈溢出的风险,来自:https://blog.csdn.net/wangjiannuaa/article/details/6598041
1 /**@func gif_get_scale_rgb 2 * @brief 双线性差值算法缩放RGB图片 3 * @param[in] uDstWidth 目的图像宽度 4 * @param[in] uDstHeigth 目的图像高度 5 * @param[out] pDstRgbBuffer 目的图像地址 6 * @param[in] uSrcWidth 源图像宽度 7 * @param[in] uSrcHeigth 源图像高度 8 * @param[in] pSrcRgbBuffer 源图像地址 9 * @return 成功返回:OK 失败返回:ERROR 10 */ 11 static INT32 gif_get_scale_rgb(UINT16 uDstWidth, UINT16 uDstHeigth, UINT8 *pDstRgbBuffer, UINT16 uSrcWidth, UINT16 uSrcHeigth, UINT8 *pSrcRgbBuffer) 12 { 13 DOUBLE fRateW = uDstWidth * 1.0 / uSrcWidth; 14 DOUBLE fRateH = uDstHeigth * 1.0 / uSrcHeigth; 15 16 INT32 iIndexW = 0; 17 INT32 iIndexH = 0; 18 19 DOUBLE fX, fY; 20 21 INT32 iStepSrcImg = uSrcWidth; 22 //INT32 iStepDstImg = uSrcWidth * fRateW; 23 INT32 iStepDstImg = uDstWidth; 24 25 INT32 iX, iY; 26 27 UINT8 bUpLeft, bUpRight, bDownLeft, bDownRight; 28 UINT8 gUpLeft, gUpRight, gDownLeft, gDownRight; 29 UINT8 rUpLeft, rUpRight, rDownLeft, rDownRight; 30 31 UINT8 uB, uG, uR; 32 33 if((NULL == pDstRgbBuffer) || (NULL == pSrcRgbBuffer)) 34 { 35 GIF_ERRORS("Ptr is NULL \n"); 36 return ERROR; 37 } 38 39 for(iIndexH = 0; iIndexH < uDstHeigth/* uSrcHeigth * fRateH */ ; iIndexH++) 40 { 41 for(iIndexW = 0; iIndexW < uDstWidth/* uSrcWidth * fRateW */ ; iIndexW++) 42 { 43 fX = ((double)iIndexW) /fRateW; 44 fY = ((double)iIndexH) /fRateH; 45 46 iX = (int)fX; 47 iY = (int)fY; 48 49 bUpLeft = pSrcRgbBuffer[iY * iStepSrcImg * 3 + iX * 3 + 0]; 50 bUpRight = pSrcRgbBuffer[iY * iStepSrcImg * 3 + (iX + 1) * 3 + 0]; 51 52 bDownLeft = pSrcRgbBuffer[(iY + 1) * iStepSrcImg * 3 + iX * 3 + 0]; 53 bDownRight = pSrcRgbBuffer[(iY + 1) * iStepSrcImg * 3 + (iX + 1) * 3 + 0]; 54 55 gUpLeft = pSrcRgbBuffer[iY * iStepSrcImg * 3 + iX * 3 + 1]; 56 gUpRight = pSrcRgbBuffer[iY * iStepSrcImg * 3 + (iX + 1) * 3 + 1]; 57 58 gDownLeft = pSrcRgbBuffer[(iY + 1) * iStepSrcImg * 3 + iX * 3 + 1]; 59 gDownRight = pSrcRgbBuffer[(iY + 1) * iStepSrcImg * 3 + (iX + 1) * 3 + 1]; 60 61 rUpLeft = pSrcRgbBuffer[iY * iStepSrcImg * 3 + iX * 3 + 2]; 62 rUpRight = pSrcRgbBuffer[iY * iStepSrcImg * 3 + (iX + 1) * 3 + 2]; 63 64 rDownLeft = pSrcRgbBuffer[(iY + 1) * iStepSrcImg * 3 + iX * 3 + 2]; 65 rDownRight = pSrcRgbBuffer[(iY + 1) * iStepSrcImg * 3 + (iX + 1) * 3 + 2]; 66 67 uB = bUpLeft * (iX + 1 - fX) * (iY + 1 - fY) + bUpRight * (fX - iX) * (iY + 1 - fY) 68 + bDownLeft * (iX + 1 - fX) * (fY - iY) + bDownRight * (fX - iX) * (fY - iY); 69 70 uG = gUpLeft * (iX + 1 - fX) * (iY + 1 - fY) + gUpRight * (fX - iX) * (iY + 1 - fY) 71 + gDownLeft * (iX + 1 - fX) * (fY - iY) + gDownRight * (fX - iX) * (fY - iY); 72 73 uR = rUpLeft * (iX + 1 - fX) * (iY + 1 - fY) + rUpRight * (fX - iX) * (iY + 1 - fY) 74 + rDownLeft * (iX + 1 - fX) * (fY - iY) + rDownRight * (fX - iX) * (fY - iY); 75 76 if(iY >= 0 && iY <= uSrcHeigth * 2 && iX >= 0 && iX <= uSrcWidth * 2) 77 { 78 pDstRgbBuffer[iIndexH * iStepDstImg * 3 + iIndexW * 3 + 0] = uB; //B 79 pDstRgbBuffer[iIndexH * iStepDstImg * 3 + iIndexW * 3 + 1] = uG; //G 80 pDstRgbBuffer[iIndexH * iStepDstImg * 3 + iIndexW * 3 + 2] = uR; //R 81 } 82 } 83 } 84 85 return OK; 86 }
实现二:改写自:https://blog.csdn.net/weixin_40647819/article/details/86601070
1 /**@func gif_get_scale_rgb 2 * @brief 双线性差值算法缩放RGB图片 3 * @param[in] uDstWidth 目的图像宽度 4 * @param[in] uDstHeigth 目的图像高度 5 * @param[out] pDstRgbBuffer 目的图像地址 6 * @param[in] uSrcWidth 源图像宽度 7 * @param[in] uSrcHeigth 源图像高度 8 * @param[in] pSrcRgbBuffer 源图像地址 9 * @return 成功返回:OK 失败返回:ERROR 10 */ 11 static INT32 gif_get_scale_rgb_new(UINT16 uDstWidth, UINT16 uDstHeigth, UINT8 *pDstRgbBuffer, UINT16 uSrcWidth, UINT16 uSrcHeigth, UINT8 *pSrcRgbBuffer) 12 { 13 DOUBLE fRateW = uDstWidth * 1.0 / uSrcWidth; 14 DOUBLE fRateH = uDstHeigth * 1.0 / uSrcHeigth; 15 16 INT32 iIndexW = 0; 17 INT32 iIndexH = 0; 18 19 DOUBLE dCenterH; 20 DOUBLE dCenterW; 21 DOUBLE dPointPartu; 22 DOUBLE dPointPartv; 23 24 INT32 iHeightDown; 25 INT32 iHeightUp; 26 INT32 iWidthLeft; 27 INT32 iWidthRight; 28 29 if((NULL == pDstRgbBuffer) || (NULL == pSrcRgbBuffer)) 30 { 31 GIF_ERRORS("Ptr is NULL \n"); 32 return ERROR; 33 } 34 35 for(iIndexH = 0; iIndexH < uDstHeigth; iIndexH++) 36 { 37 //几何中心对齐 38 dCenterH = (iIndexH + 0.5) / fRateH - 0.5; 39 //防止越界 40 if (dCenterH<0) 41 { 42 dCenterH = 0; 43 } 44 if (dCenterH >= uSrcHeigth - 1) 45 { 46 dCenterH = uSrcHeigth - 2; 47 } 48 //相邻4*4像素的行(坐标) 49 iHeightDown = floor(dCenterH); 50 iHeightUp = ceil(dCenterH); 51 //u为得到浮点型坐标行的小数部分 52 dPointPartu = dCenterH - iHeightDown; 53 for(iIndexW = 0; iIndexW < uDstWidth; iIndexW++) 54 { 55 //几何中心对齐 56 dCenterW = (iIndexW + 0.5) / fRateW - 0.5; 57 //防止越界 58 if (dCenterW < 0) 59 { 60 dCenterW = 0; 61 } 62 if (dCenterW >= uSrcWidth- 1) 63 { 64 dCenterW = uSrcWidth - 2; 65 } 66 //相邻4*4像素的列(坐标) 67 iWidthLeft = floor(dCenterW); 68 iWidthRight = ceil(dCenterW); 69 //v为得到浮点型坐标列的小数部分 70 dPointPartv = dCenterW - iWidthLeft; 71 72 //彩色图像 73 pDstRgbBuffer[iIndexH * uDstWidth * 3 + iIndexW * 3 + 0] = (1 - dPointPartu) * (1 - dPointPartv) * pSrcRgbBuffer[iHeightDown * uSrcWidth * 3 + iWidthLeft * 3 + 0] + (1 - dPointPartu) * dPointPartv * pSrcRgbBuffer[iHeightDown * uSrcWidth * 3 + iWidthRight * 3 + 0] + dPointPartu * (1 - dPointPartv) * pSrcRgbBuffer[iHeightUp * uSrcWidth * 3 + iWidthLeft * 3 + 0] + dPointPartu * dPointPartv * pSrcRgbBuffer[iHeightUp * uSrcWidth * 3 + iWidthRight * 3 + 0]; 74 pDstRgbBuffer[iIndexH * uDstWidth * 3 + iIndexW * 3 + 1] = (1 - dPointPartu) * (1 - dPointPartv) * pSrcRgbBuffer[iHeightDown * uSrcWidth * 3 + iWidthLeft * 3 + 1] + (1 - dPointPartu) * dPointPartv * pSrcRgbBuffer[iHeightDown * uSrcWidth * 3 + iWidthRight * 3 + 1] + dPointPartu * (1 - dPointPartv) * pSrcRgbBuffer[iHeightUp * uSrcWidth * 3 + iWidthLeft * 3 + 1] + dPointPartu * dPointPartv * pSrcRgbBuffer[iHeightUp * uSrcWidth * 3 + iWidthRight * 3 + 1]; 75 pDstRgbBuffer[iIndexH * uDstWidth * 3 + iIndexW * 3 + 2] = (1 - dPointPartu) * (1 - dPointPartv) * pSrcRgbBuffer[iHeightDown * uSrcWidth * 3 + iWidthLeft * 3 + 2] + (1 - dPointPartu) * dPointPartv * pSrcRgbBuffer[iHeightDown * uSrcWidth * 3 + iWidthRight * 3 + 2] + dPointPartu * (1 - dPointPartv) * pSrcRgbBuffer[iHeightUp * uSrcWidth * 3 + iWidthLeft * 3 + 2] + dPointPartu * dPointPartv * pSrcRgbBuffer[iHeightUp * uSrcWidth * 3 + iWidthRight * 3 + 2]; 76 } 77 } 78 79 return OK; 80 }
标签:iY,iX,缩放,pSrcRgbBuffer,fY,uSrcWidth,fX,RGB,双线性 From: https://www.cnblogs.com/peifx/p/16849180.html