19,502
社区成员
发帖
与我相关
我的任务
分享
// 优化乘法 1
int xTmp = newWidth;
for(int i = 0; i < newHeight; i++)
{
for(int j = 0; j < newWidth; j++)
{
// 省略100字。。。
newImage[xTmp +j]=255;
}
xTmp += newWidth;
}
// 优化乘法 和 加法(大叔你知不知道i++比++i慢一倍)
int xTmp = newWidth;
for(int i = 0; i < newHeight; ++i)
{
uchar* pTmp = newImage + xTmp;
for(int j = 0; j < newWidth; ++j)
{
// 省略100字。。。
*pTmp =255;
++pTmp;
}
xTmp += newWidth;
}
void Rotate(uchar *image,int width,int height,uchar *&newImage, int &newWidth,int &newHeight, float fRotateAngle)
{
// 旋转角度的正弦和余弦
float fSina, fCosa;
// 两个中间常量
float f1,f2;
// 象素在源DIB中的坐标
int i0;
int j0;
// 源图四个角的坐标(以图像中心为坐标系原点)
float fSrcX1,fSrcY1,fSrcX2,fSrcY2,fSrcX3,fSrcY3,fSrcX4,fSrcY4;
// 旋转后四个角的坐标(以图像中心为坐标系原点)
float fDstX1,fDstY1,fDstX2,fDstY2,fDstX3,fDstY3,fDstX4,fDstY4;
// 计算旋转角度的正弦
fSina = (float) sin((double)fRotateAngle);
// 计算旋转角度的余弦
fCosa= (float) cos((double)fRotateAngle);
// 计算原图的四个角的坐标(以图像中心为坐标系原点)
fSrcX1 = (float) (- (width - 1) / 2);
fSrcY1 = (float) ( (height - 1) / 2);
fSrcX2 = (float) ( (width - 1) / 2);
fSrcY2 = (float) ( (height - 1) / 2);
fSrcX3 = (float) (- (width - 1) / 2);
fSrcY3 = (float) (- (height - 1) / 2);
fSrcX4 = (float) ( (width - 1) / 2);
fSrcY4 = (float) (- (height - 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;
// 计算旋转后的图像实际宽度
newWidth = (int) ( max( fabs(fDstX4 - fDstX1), fabs(fDstX3 - fDstX2) ) + 0.5);
// 计算旋转后的图像高度
newHeight = (int) ( max( fabs(fDstY4 - fDstY1), fabs(fDstY3 - fDstY2) ) + 0.5);
//int length= newWidth* newHeight;
newImage=new uchar[ newWidth* newHeight];
// 两个常数,这样不用以后每次都计算了
f1 = (float) (-0.5 * (newWidth - 1) * fCosa - 0.5 * (newHeight - 1) * fSina
+ 0.5 * (width - 1));
f2 = (float) ( 0.5 * (newWidth - 1) * fSina - 0.5 * (newHeight - 1) * fCosa
+ 0.5 * (height - 1));
// 针对图像每行进行操作
for(int i = 0; i < newHeight; i++)
{
// 针对图像每列进行操作
for(int j = 0; j < newWidth; j++)
{
// 计算该象素在源DIB中的坐标
i0 = int(-((float) j) * fSina + ((float) i) * fCosa + f2+0.5);
j0 = int(((float) j) * fCosa + ((float) i) * fSina + f1+0.5);
if( (i0 < 0) || (j0 > width - 1) || (j0 < 0) || (i0 > height - 1))
{
// 要计算的点不在源图范围内,直接返回255。
newImage[newWidth*(i)+j]=255;
}
else
newImage[newWidth* (i) +j]=image[width*(i0)+j0];
}
}
}