#include <fstream>
#include <math.h>
#include <stdio.h>
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
//定义原始图像的宽和高
unsigned int Height = 0;
unsigned int Width = 0;
//定义循环变量
int i=0;
int j=0;
//定义pi
double pi=3.1415926;
BITMAPFILEHEADER bmpfileheader; //文件头
BITMAPINFOHEADER bmpinfoheader; //信息头
//24位像素点RGB结构体
typedef struct tagRGB
{
BYTE blue;
BYTE green;
BYTE red;
}RGBDATA;
FILE *fpin; //读取操作流
FILE *fpout; //读出操作流
fpin=fopen("picture.bmp","rb");
fread(&bmpfileheader,sizeof(BITMAPFILEHEADER),1,fpin);//读取文件头
fread(&bmpinfoheader,sizeof(BITMAPINFOHEADER),1,fpin);//读取信息头
Height=bmpinfoheader.biHeight;
Width=bmpinfoheader.biWidth;
//动态创建二维数组
RGBDATA** RGBin;
RGBin = (RGBDATA **)malloc(sizeof(RGBDATA*) * Height);
for (i = 0; i < Height; i++)
{
RGBin[i] = (RGBDATA *)malloc(sizeof(RGBDATA) * Width);
}
//读入像素信息
for(i=0;i<Height;i++)
{
fread(RGBin[i], sizeof(tagRGB), Width, fpin);
}
//动态创建二维数组
RGBDATA** RGBout;
RGBout = (RGBDATA **)malloc(sizeof(RGBDATA*) * Height);
for (i = 0; i < Height; i++)
{
RGBout[i] = (RGBDATA *)malloc(sizeof(RGBDATA) * Width);
}
//将输出图像的底色变为灰色
for(i=0;i<Width;i++)
{
for(j=0;j<Height;j++)
{
RGBout[j][i].blue=127;
RGBout[j][i].green=127;
RGBout[j][i].red=127;
}
}
cout<<"请输入所要旋转的角度:";
double degree;
cin>>degree;
//将角度换算成弧度
degree = (degree/180) * pi ;
cout<<"degree ="<<degree<<endl;
cout<<"cos(degree)="<<cos(degree)<<endl;
cout<<"sin(degree)="<<sin(degree)<<endl;
//定义旋转中心点
int X0=(Width-1)/2;
int Y0=(Height-1)/2;
//定义旋转后的点的坐标
int X=0;
int Y=0;
//定义当前点到 旋转中心点的差值
int _X=0;
int _Y=0;
int src_i,src_j,dst_i,dst_j;
//将原始图像旋转 ---双线性插值的旋转处理
for(i=0;i<Height;i++)
{
for(j=0;j<Width;j++)
{
src_i=i-Y0;
src_j=j-X0;
dst_i=(int)(cos(degree)*src_j+sin(degree)*src_i)+Y0;
dst_j=(int)(cos(degree)*src_i-sin(degree)*src_j)+X0;
if(dst_i>=0&&dst_i<Height&&dst_j>=0&&dst_j<Width)
{
RGBout[j][i].blue=RGBin[dst_i][dst_j].blue;
RGBout[j][i].green=RGBin[dst_i][dst_j].green;
RGBout[j][i].red=RGBin[dst_i][dst_j].red;
}
}
}
//将信息写入文件
fpout=fopen("picture_out.bmp","wb");
fwrite(&bmpfileheader,sizeof(BITMAPFILEHEADER),1,fpout);
fwrite(&bmpinfoheader,sizeof(BITMAPINFOHEADER),1,fpout);
for (i=0;i<Height;i++)
{
fwrite(RGBout[i],sizeof(tagRGB),Width,fpout);
}
printf("生成新图片成功!\n");
fclose(fpin);
fclose(fpout);
return 0;
}
版权声明:本文为weixin_44474840原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。