#include "stdio.h"
#include "Windows.h"
#include <math.h>
#define PI 3.14159265 /* 定义常量 */
//几个全局变量,存放读入图像的位图数据、宽、高、颜色表及每像素所占位数(比特)
//此处定义全局变量主要为了后面的图像数据访问及图像存储作准备
unsigned char *pBmpBuf;//读入图像数据的指针
int bmpWidth;//图像的宽
int bmpHeight;//图像的高
RGBQUAD *pColorTable;//颜色表指针
int biBitCount;//图像类型
RGBQUAD *pColorTable_Gray = new RGBQUAD[256];//灰度图像的颜色表
/*复数定义*/
typedef struct
{
double re;
double im;
}COMPLEX;
/*复数加运算*/
COMPLEX Add(COMPLEX c1, COMPLEX c2)
{
COMPLEX c;
c.re=c1.re+c2.re;
c.im=c1.im+c2.im;
return c;
}
/*复数减运算*/
COMPLEX Sub(COMPLEX c1, COMPLEX c2)
{
COMPLEX c;
c.re=c1.re-c2.re;
c.im=c1.im-c2.im;
return c;
}
/*复数乘运算*/
COMPLEX Mul(COMPLEX c1, COMPLEX c2)
{
COMPLEX c;
c.re=c1.re*c2.re-c1.im*c2.im;
c.im=c1.re*c2.im+c2.re*c1.im;
return c;
}
void FFT(COMPLEX *TD,COMPLEX *FD,int r);//快速FFT函数声明
/***********************************************************************
* 函数名称:
* readBmp()
* Download by http://www.codefans.net
*函数参数:
* char *bmpName -文件名字及路径
*
*返回值:
* 0为失败,1为成功
*
*说明:给定一个图像文件名及其路径,读图像的位图数据、宽、高、颜色表及每像素
* 位数等数据进内存,存放在相应的全局变量中
***********************************************************************/
bool readBmp(char *bmpName)
{
//二进制读方式打开指定的图像文件
FILE *fp=fopen(bmpName,"rb");
if(fp==0) return 0;
//跳过位图文件头结构BITMAPFILEHEADER
fseek(fp, sizeof(BITMAPFILEHEADER),0);
//定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中
BITMAPINFOHEADER head;
fread(&head, sizeof(BITMAPINFOHEADER), 1,fp);
//获取图像宽、高、每像素所占位数等信息
bmpWidth = head.biWidth;
bmpHeight = head.biHeight;
biBitCount = head.biBitCount;
//定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
int lineByte=(bmpWidth * biBitCount/8+3)/4*4;
//灰度图像有颜色表,且颜色表表项为256
if(biBitCount==8){
//申请颜色表所需要的空间,读颜色表进内存
pColorTable=new RGBQUAD[256];
fread(pColorTable,sizeof(RGBQUAD),256,fp);
}
//申请位图数据所需要的空间,读位图数据进内存
pBmpBuf=new unsigned char[lineByte * bmpHeight];
fread(pBmpBuf,1,lineByte * bmpHeight,fp);
//关闭文件
fclose(fp);
return 1;
}
/***********************************************************************
* 函数名称:
* saveBmp()
*
*函数参数:
* char *bmpName -文件名字及路径
* unsigned char *imgBuf -待存盘的位图数据
* int width -像素为单位待存盘位图的宽
* int height -像素为单位待存盘位图高
* int biBitCount -每像素所占位数
* RGBQUAD *pColorTable -颜色表指针
*返回值:
* 0为失败,1为成功
*
*说明:给定一个图像位图数据、宽、高、颜色表指针及每像素所占的位数等信息,
* 将其写到指定文件中
***********************************************************************/
bool saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height,
int biBitCount, RGBQUAD *pColorTable)
{
//如果位图数据指针为0,则没有数据传入,函数返回
if(!imgBuf)
return 0;
//颜色表大小,以字节为单位,灰度图像颜色表为1024字节,彩色图像颜色表大小为0
int colorTablesize=0;
if(biBitCount==8)
colorTablesize=1024;
//待存储图像数据每行字节数为4的倍数
int lineByte=(width * biBitCount/8+3)/4*4;
//以二进制写的方式打开文件
FILE *fp=fopen(bmpName,"wb");
if(fp==0) return 0;
//申请位图文件头结构变量,填写文件头信息
BITMAPFILEHEADER fileHead;
fileHead.bfType = 0x4D42;//bmp类型
//bfSize是图像文件4个组成部分之和
fileHead.bfSize= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
+ colorTablesize + lineByte*height;
fileHead.bfReserved1 = 0;
fileHead.bfReserved2 = 0;
//bfOffBits是图像文件前三个部分所需空间之和
fileHead.bfOffBits=54+colorTablesize;
//写文件头进文件
fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp);
//申请位图信息头结构变量,填写信息头信息
BITMAPINFOHEADER head;
head.biBitCount=biBitCount;
head.biClrImportant=0;
head.biClrUsed=0;
head.biCompression=0;
head.biHeight=height;
head.biPlanes=1;
head.biSize=40;
head.biSizeImage=lineByte*height;
head.biWidth=width;
head.biXPelsPerMeter=0;
head.biYPelsPerMeter=0;
//写位图信息头进内存
fwrite(&head, sizeof(BITMAPINFOHEADER),1, fp);
//如果灰度图像,有颜色表,写入文件
if(biBitCount==8)
fwrite(pColorTable, sizeof(RGBQUAD),256, fp);
//写位图数据进文件
fwrite(imgBuf, height*lineByte, 1, fp);
//关闭文件
fclose(fp);
return 1;
}
void main()
{
for(int i=0;i<256;i++){
pColorTable_Gray[i].rgbRed=pColorTable_Gray[i].rgbGreen=pColorTable_Gray[i].rgbBlue=i;
}
unsigned char* pBmpBuf_Gray;
//读入指定BMP文件进内存
char readPath[]="11.bmp";
readBmp(readPath);
//输出图像的信息
printf("width=%d,height=%d,biBitCount=%d\n",bmpWidth,bmpHeight,biBitCount);
//循环变量,图像的坐标
int i,j;
//每行字节数
int lineByte=(bmpWidth*biBitCount/8+3)/4*4;
int lineByte_fft = (bmpWidth+3)/4*4;;//fft图像每行的字节数
int biBitCount_Gray = 8;//灰色图片深度
//循环变量,针对彩色图像,遍历每像素的三个分量
int k;
//将彩色图像转成灰度图像 8位图 每个字节一个像素 24位图 三个字节一个像素 分别是RGB的值
if(biBitCount==8){//对于8位图像 只要用灰度图像颜色表即可
char writePath[]="修改图像.bmp";
saveBmp(writePath, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable_Gray);
}
else if(biBitCount==24){//对于 24位图 变成灰度图之后变为了8位图 需要创建一个新的8位图
pBmpBuf_Gray=new unsigned char[lineByte_fft * bmpHeight];//开辟一个存储灰度信息的空间 灰度图每行的字节数和fft图相同
for(i=0;i<bmpHeight;i++){
for(j=0;j<bmpWidth;j++){
int temp=0;
for(k=0;k<3;k++)
temp=temp+*(pBmpBuf+i*lineByte+j*3+k);
temp=temp/3;
*(pBmpBuf_Gray+i*lineByte_fft+j)=temp;
}
}
char writePath[]="修改图像.bmp";
saveBmp(writePath, pBmpBuf_Gray, bmpWidth, bmpHeight, biBitCount_Gray, pColorTable_Gray);
}
/************************以下为FFT程序***********************/
if(pBmpBuf ==NULL)
{
printf("没有读取数据或数据不正确\n");
}
// 进行付立叶变换的宽度和高度(2的整数次方)
LONG w = 1, h = 1;
int wp = 0, hp = 0;
// 计算进行付立叶变换的宽度和高度(2的整数次方)
while(w * 2 <= bmpWidth)
{
w *= 2;
wp++;
}
while(h * 2 <= bmpHeight)
{
h *= 2;
hp++;
}
// 分配内存;
COMPLEX *TD =(COMPLEX*)malloc(sizeof(COMPLEX)*w*h); //时间域
COMPLEX *FD =(COMPLEX*)malloc(sizeof(COMPLEX)*w*h); //频率域
unsigned char *pBmpBuf_fft = new unsigned char[lineByte_fft * bmpHeight];//创建一个fft图像
//设置初值
for(int i = 0; i < h; i++)
{
for(int j = 0; j < w; j++)
{
if(biBitCount == 8)
{
// 指向DIB第i行,第j个像素的指针
unsigned char *lpSrc=pBmpBuf+lineByte*i+j;
// 给时域赋值
TD[w * i + j].re = *lpSrc;
TD[w * i + j].im = 0;
}
else if(biBitCount == 24)
{
unsigned char *lpSrc = pBmpBuf+lineByte*i+j*3;
double gray=*(lpSrc+2)*0.299+(*(lpSrc+1))*0.587+(*lpSrc)*0.114;
TD[j+w*i].re = gray;
TD[j+w*i].im = 0;
}