-

- 加为好友
- 发送私信
- 在线聊天
ljooo
- 等级:

- 可用分等级:
- 总技术分:
- 总技术分排名:
|
| 发表于:2008-08-08 10:02:175楼 得分:0 |
我知道要把i0,j0改成float。 问题是图片变得不可识别,那就是说整个方法都是错误的。 下面的源代码,我的代码风格不好。 我把bmp的数据赋给了c类型的字符串lpDIBBits了(这个修改可能会引起大家的困惑),目的是减少指针操作。原来的宽度和高度lWidth,lHeight。旋转的分别为lNewWidth,lNewHeight。缩放的为lNewWidth2,lNewHeight2。 要旋转的角度是 iRotateAngle,已经是弧度了。 原来的操作,代码是没有问题的。不过我在记事本里改了一下变量名和注释,可能会引进小的错误。 //图像旋转 // 旋转后图像的宽度和高度 int lNewWidth; int lNewHeight; // 旋转角度(弧度) float fRotateAngle; // 旋转角度的正弦和余弦 float fSina, fCosa; // 两个中间常量 float f1,f2; // 象素在源DIB中的坐标 LONG i0; LONG j0; // 源图四个角的坐标(以图像中心为坐标系原点) float fSrcX1,fSrcY1,fSrcX2,fSrcY2,fSrcX3,fSrcY3,fSrcX4,fSrcY4; // 旋转后四个角的坐标(以图像中心为坐标系原点) float fDstX1,fDstY1,fDstX2,fDstY2,fDstX3,fDstY3,fDstX4,fDstY4; // 将旋转角度从度转换到弧度 fRotateAngle=(float)iRotateAngle; // 计算旋转角度的正弦 fSina = (float) sin((double)fRotateAngle); // 计算旋转角度的余弦 fCosa= (float) cos((double)fRotateAngle); // 计算原图的四个角的坐标(以图像中心为坐标系原点) fSrcX1 = (float) (- (lWidth - 1) / 2); fSrcY1 = (float) ( (lHeight - 1) / 2); fSrcX2 = (float) ( (lWidth - 1) / 2); fSrcY2 = (float) ( (lHeight - 1) / 2); fSrcX3 = (float) (- (lWidth - 1) / 2); fSrcY3 = (float) (- (lHeight - 1) / 2); fSrcX4 = (float) ( (lWidth - 1) / 2); fSrcY4 = (float) (- (lHeight - 1) / 2); // 计算新图四个角的坐标(以图像中心为坐标系原点) fDstX1 = fCosa * fSrcX1 + fSina * fSrcY1; fDstY1 = -fSina * fSrcX1 + fCosa * fSrcY1; fDstX2 = fCosa * fSrcX2 + fSina * fSrcY2; fDstY2 = -fSina * fSrcX2 + fCosa * fSrcY2; fDstX3 = fCosa * fSrcX3 + fSina * fSrcY3; fDstY3 = -fSina * fSrcX3 + fCosa * fSrcY3; fDstX4 = fCosa * fSrcX4 + fSina * fSrcY4; fDstY4 = -fSina * fSrcX4 + fCosa * fSrcY4; // 计算旋转后的图像实际宽度 lNewWidth = (LONG) ( max( fabs(fDstX4 - fDstX1), fabs(fDstX3 - fDstX2) ) + 0.5); // 计算旋转后的图像高度 lNewHeight = (LONG) ( max( fabs(fDstY4 - fDstY1), fabs(fDstY3 - fDstY2) ) + 0.5); unsigned char *lpNewBits=new unsigned char[ lNewWidth* lNewHeight]; // 两个常数,这样不用以后每次都计算了 f1 = (float) (-0.5 * (lNewWidth - 1) * fCosa - 0.5 * (lNewHeight - 1) * fSina + 0.5 * (lWidth - 1)); f2 = (float) ( 0.5 * (lNewWidth - 1) * fSina - 0.5 * (lNewHeight - 1) * fCosa + 0.5 * (lHeight - 1)); // 针对图像每行进行操作 for(int i = 0; i < lNewHeight; i++) { // 针对图像每列进行操作 for(int j = 0; j < lNewWidth; j++) { // 计算该象素在源DIB中的坐标 i0 = LONG(-((float) j) * fSina + ((float) i) * fCosa + f2+0.5); j0 = LONG(((float) j) * fCosa + ((float) i) * fSina + f1+0.5); if( (i0 >= 0) || (j0 < lWidth ) || (j0 >= 0) || (i0 < lHeight )) { // 要计算的点在源图范围内 lpNewBits[lNewWidth* (i) +j]=lpDIBBits[lWidth*(i0)+j0]; } else // 要计算的点不在源图范围内,直接返回255。 lpNewBits[lNewWidth*(i)+j]=255; } } //图像缩放 //缩放比例 float fXZoomRatio; float fYZoomRatio; fXZoomRatio=0.5f; fYZoomRatio=0.5f; // 缩放后图像的宽度和高度 LONG lNewWidth2; LONG lNewHeight2; //// 计算缩放后的图像实际宽度 lNewWidth2 = (LONG) (lNewWidth * fXZoomRatio + 0.5); //// 计算缩放后的图像高度 lNewHeight2 = (LONG) (lNewHeight * fYZoomRatio + 0.5); unsigned char *lpNewBits2=new unsigned char[ lNewWidth2* lNewHeight2]; for(int i = 0; i < lNewHeight2; i++) { // 针对图像每列进行操作 for(int j = 0; j < lNewWidth2; j++) { // 计算该象素在源DIB中的坐标 i0 = (LONG) (i / fYZoomRatio + 0.5); j0 = (LONG) (j / fXZoomRatio + 0.5); // 判断是否在源图范围内 if( (j0 >= 0) && (j0 < lNewHeight) && (i0 >= 0) && (i0 < lNewHeight)) { lpNewBits2[lNewWidth2*( lNewHeight2-1-i)+j]=lpNewBits[lNewWidth*( lNewHeight-1-i0)+j0]; } else { // 对于源图中没有的象素,直接赋值为255 lpNewBits2[lNewWidth2*( lNewHeight2-1-i)+j] = 255; } } } 合并的情况 //图像旋转 // 旋转后图像的宽度和高度 int lNewWidth; int lNewHeight; // 旋转角度(弧度) float fRotateAngle; // 旋转角度的正弦和余弦 float fSina, fCosa; // 两个中间常量 float f1,f2; // 象素在源DIB中的坐标 LONG i0; LONG j0; // 源图四个角的坐标(以图像中心为坐标系原点) float fSrcX1,fSrcY1,fSrcX2,fSrcY2,fSrcX3,fSrcY3,fSrcX4,fSrcY4; // 旋转后四个角的坐标(以图像中心为坐标系原点) float fDstX1,fDstY1,fDstX2,fDstY2,fDstX3,fDstY3,fDstX4,fDstY4; // 将旋转角度从度转换到弧度 // fRotateAngle = (float) (iRotateAngle*3.1415926535/180); fRotateAngle=(float)iRotateAngle; // 计算旋转角度的正弦 fSina = (float) sin((double)fRotateAngle); // 计算旋转角度的余弦 fCosa= (float) cos((double)fRotateAngle); // 计算原图的四个角的坐标(以图像中心为坐标系原点) fSrcX1 = (float) (- (lWidth - 1) / 2); fSrcY1 = (float) ( (lHeight - 1) / 2); fSrcX2 = (float) ( (lWidth - 1) / 2); fSrcY2 = (float) ( (lHeight - 1) / 2); fSrcX3 = (float) (- (lWidth - 1) / 2); fSrcY3 = (float) (- (lHeight - 1) / 2); fSrcX4 = (float) ( (lWidth - 1) / 2); fSrcY4 = (float) (- (lHeight - 1) / 2); // 计算新图四个角的坐标(以图像中心为坐标系原点) fDstX1 = fCosa * fSrcX1 + fSina * fSrcY1; fDstY1 = -fSina * fSrcX1 + fCosa * fSrcY1; fDstX2 = fCosa * fSrcX2 + fSina * fSrcY2; fDstY2 = -fSina * fSrcX2 + fCosa * fSrcY2; fDstX3 = fCosa * fSrcX3 + fSina * fSrcY3; fDstY3 = -fSina * fSrcX3 + fCosa * fSrcY3; fDstX4 = fCosa * fSrcX4 + fSina * fSrcY4; fDstY4 = -fSina * fSrcX4 + fCosa * fSrcY4; // 计算旋转后的图像实际宽度 lNewWidth = (LONG) ( max( fabs(fDstX4 - fDstX1), fabs(fDstX3 - fDstX2) ) + 0.5); // 计算旋转后的图像高度 lNewHeight = (LONG) ( max( fabs(fDstY4 - fDstY1), fabs(fDstY3 - fDstY2) ) + 0.5); // 两个常数,这样不用以后每次都计算了 f1 = (float) (-0.5 * (lNewWidth - 1) * fCosa - 0.5 * (lNewHeight - 1) * fSina + 0.5 * (lWidth - 1)); f2 = (float) ( 0.5 * (lNewWidth - 1) * fSina - 0.5 * (lNewHeight - 1) * fCosa + 0.5 * (lHeight - 1)); //图像缩放 //缩放比例 float fXZoomRatio; float fYZoomRatio; fXZoomRatio=0.5f; fYZoomRatio=0.5f; // 缩放后图像的宽度和高度 LONG lNewWidth2; LONG lNewHeight2; long i1,j1; //// 计算缩放后的图像实际宽度 lNewWidth2 = (LONG) (lNewWidth * fXZoomRatio + 0.5); //// 计算缩放后的图像高度 lNewHeight2 = (LONG) (lNewHeight * fYZoomRatio + 0.5); unsigned char *lpNewBits2=new unsigned char[ lNewWidth2* lNewHeight2]; for(int i = 0; i < lNewHeight2; i++) { // 针对图像每列进行操作 for(int j = 0; j < lNewWidth2; j++) { // 计算该象素在源DIB中的坐标 i0 = i / fYZoomRatio ; j0 = j / fXZoomRatio ; i1 = LONG(-((float) j0) * fSina + ((float) i0) * fCosa + f2+0.5); j1 = LONG(((float) j0) * fCosa + ((float) i0) * fSina + f1+0.5); // 判断是否在源图范围内 if( (j1 >= 0) && (j1 < lWidth) && (i1 >= 0) && (i1 < lHeight)) { lpNewBits2[lNewWidth2*( lNewHeight2-1-i)+j]=lpDIBBits[lWidth*( lHeight-1-i1)+j1]; } else { // 对于源图中没有的象素,直接赋值为255 lpNewBits2[lNewWidth2*( lNewHeight2-1-i)+j] = 255; } } } | | |
修改
删除
举报
引用
回复
| |