设为首页 加入收藏

TOP

图像编程学习笔记3――图像旋转 (二)
2015-01-25 00:08:40 来源: 作者: 【 】 浏览:469
Tags:图像 编程 学习 笔记 旋转
? ? 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"; ?
? ? 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)
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇出现"eh.h is only for C++!.. 下一篇WinVerifyTrust signature verifi..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: