设为首页 加入收藏

TOP

图像编程学习笔记3――图像旋转 (四)
2015-01-25 00:08:40 】 浏览:1936
Tags:图像 编程 学习 笔记 旋转
{
FILE *fp = fopen(fileName,"rb"); //以二进制读方式打开
if (NULL == fp)
{
cout<<"The file is opened failure!"<
return FALSE;
}
//读取信息到相应的变量中
fread(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
fread(&bmpInfoHeader,sizeof(BITMAPINFOHEADER),1,fp);
pColorTable = new RGBQUAD[256];
fread(pColorTable,sizeof(RGBQUAD),256,fp);
int imgSize = bmpInfoHeader.biSizeImage; //文图数据的大小,4的倍数
pBmpData = new unsigned char[imgSize];
fread(pBmpData,sizeof(unsigned char),imgSize,fp);
fclose(fp);
return TRUE;
}
/**
* 函数名: rotation
* 参 数: rotAngle--要转换的角度
* 功 能: 实现bmp图像的旋转
*/
void rotation(int rotAngle)
{
double cosa,sina,srcX[4],srcY[4],dstX[4],dstY[4],rad;
int oldWidth,oldHeight,newWidth,newHeight;
rad = (double)RADIAN(rotAngle);
cosa = cos(rad);
sina = sin(rad);
//原图宽与高
oldWidth = bmpInfoHeader.biWidth;
oldHeight = bmpInfoHeader.biHeight;
//原图四个角的坐标
srcX[0] = -0.5 * oldWidth;
srcY[0] = 0.5 * oldHeight;
srcX[1] = 0.5 * oldWidth;
srcY[1] = 0.5 * oldHeight;
srcX[2] = 0.5 * oldWidth;
srcY[2] = -0.5 * oldHeight;
srcX[3] = -0.5 * oldWidth;
srcY[3] = -0.5 * oldHeight;
//新图四个角坐标
for(int i = 0; i < 4; i++ )
{
dstX[i] = cosa * srcX[i] + sina * srcY[i];
dstY[i] = -sina * srcX[i] + cosa * srcY[i];
// cout<
}
//新图的宽与高,向上取整
bmpInfoHeader.biWidth = newWidth = (int)(max(fabs(dstX[0] - dstX[2]),fabs(dstX[1] - dstX[3])) + 0.5);
bmpInfoHeader.biHeight = newHeight = (int)(max(fabs(dstY[0] - dstY[2]),fabs(dstY[1] - dstY[3])) + 0.5);
// cout<
//新图位图数据大小
bmpInfoHeader.biSizeImage = newImgSize = newHeight * ((newWidth * bmpInfoHeader.biBitCount + 31) / 32 * 4);
pNewBmpData = new unsigned char[newImgSize];
double temp1,temp2; //计算矩阵(2.9)中的两个常数,这样不用以后每次都计算了
temp1 = -0.5 * newWidth * cosa - 0.5 * newHeight * sina + 0.5 * oldWidth;
temp2 = 0.5 * newWidth * sina - 0.5 * newHeight * cosa + 0.5 * oldHeight;
memset(pNewBmpData,(BYTE)255,newImgSize); //先全部填充成白色
int x0,y0,x1,y1;
unsigned char *pOldTemp,*pNewTemp;
int oldLineByte,newLineByte;
oldLineByte = (oldWidth * bmpInfoHeader.biBitCount + 31) / 32 * 4;
newLineByte = (newWidth * bmpInfoHeader.biBitCount + 31) / 32 * 4;
//把旋转后的图像数据对应存储到pNewBmpData相应位置
for(y1 = 0; y1 < newHeight; y1++)
{
for(x1 = 0; x1 < newWidth; x1++ )
{
x0 = (int)(x1 * cosa + y1 * sina + temp1);
y0 = (int)(-x1 * sina + y1 * cosa + temp2);
if((x0 >= 0 && x0 < oldWidth) && (y0 >= 0 && y0 < oldHeight)) //这里不能为<=oldWidth或oldHeight
{
pOldTemp = pBmpData + (oldHeight - 1 - y0) * oldLineByte + x0;
pNewTemp = pNewBmpData + (newHeight - 1 - y1) * newLineByte + x1;
*pNewTemp = *pOldTemp;
}
}
}
}
/**
* 函数名: writeBmp
* 参 数: bmpName -- 旋转后的bmp文件名
* 功 能: 新建一个bmp文件,把旋转后的图像信息存入其中
*/
void writeBmp(char *bmpName)
{
FILE *fp = fopen(bmpName,"wb"); //以二进制写方式打开
if(NULL == fp)
cout<<"The file is opened failure"<
//写入选装后图像信息
fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&bmpInfoHeader,sizeof(BITMAPINFOHEADER),1,fp);
fwrite(pColorTable,sizeof(RGBQUAD),256,fp);
fwrite(pNewBmpData,sizeof(unsigned char),newImgSize,fp);
fclose(fp);
delete []pColorTable;
delete []pNewBmpData;
delete []pBmpData;
}
/**
* 函数名: work
* 参 数: 无
* 功 能: 实现处理工作
*/
void work()
{
char readBmpName[] = "test.bmp";
if ( !readBmp(readBmpName))
cout<<"The file "<
cout<<"please input the angle to rotate(Clockwise):";
int rotAngle;
cin>>rotAngle;
rotation(rotAngle);
char writeBmpName[] = "test_new.bmp";
writeBm
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇出现"eh.h is only for C++!.. 下一篇WinVerifyTrust signature verifi..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目