//新图位图数据大小
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";
writeBmp(writeBmpName);
}
int main()
{
work();
return 0;
}
/**
* 程序名: Rotation.cpp
* 功 能: 实现灰度图像的旋转,如果超出原图范围,则用白色填充
* 测试位图为test.bmp放到工程目录下
*/
#include
#include
#include
#include
#include
using namespace std;
#define PI 3.1415926535
#define RADIAN(angle) (((angle)*PI)/180.0)
BITMAPFILEHEADER bmpFileHeader; //bmp文件头
BITMAPINFOHEADER bmpInfoHeader;
//bmp信息头
RGBQUAD *pColorTable;
//bmp颜色表
unsigned char *pBmpData;
//bmp位图数据
unsigned char *pNewBmpData; //旋转后bmp位图数据
int newImgSize;
//旋转后图像大小
/**
* 函数名: readBmp
* 参 数: fileName--要读取文件的文件名
* 功 能: 读取bmp位图数据,成功返回TRUE,否则返回FALSE
*/
BOOL readBmp(char *fileName)