⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmpdemodoc.cpp

📁 主要是应用VC进行傅立叶变换和反变换的程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	int i,j;
	int Gray[256];
	if((src1<0)||(src1>255)||(des1<0)||(des1>255))
		return;
	for(i=0;i<src1;i++)
	{
		float temp=(float)i*(float)des1/(float)src1;
		Gray[i]=(int)temp;
	}
	if(src2>src1)
	{
		for(i=src1;i<src2;i++)
		{
			float temp=(float)des1+\
				(float)(i-src1)*(des2-des1)/(src2-src1);
			Gray[i]=(int)temp;
		}
		for(i=src2;i<256;i++)
		{
			float temp=255.0f-(float)(255-i)*(255-des2)/(255-src2);
			Gray[i]=(int)temp;
		}
	}
	else
	{
		for(i=src1;i<256;i++)
		{
			float temp=255.0f-(float)(255-i)*(255-des1)/(255-src1);
			Gray[i]=(int)temp;
		}
	}

	for(i=0;i<width;i++)
	{
		for(j=0;j<height;j++)
		{
			COLORREF color=m_mBmp.GetPixel(i,j);
			int r=GetRValue(color);
			int g=GetGValue(color);
			int b=GetBValue(color);
			float temp=(float)r*0.3f+(float)g*0.59f+(float)b*0.11f+0.5f;
			r=Gray[(int)temp];
			m_mBmp.SetPixel(i,j,RGB(BYTE(r),BYTE(r),BYTE(r)));
		}
	}
	SetModifiedFlag(UndoFlag);
	m_mBmp.DataToView(m_mViewBmp);
}

BOOL CBmpDemoDoc::OnSaveDocument(LPCTSTR lpszPathName) 
//保存文档
{
	// TODO: Add your specialized code here and/or call the base class
//	if(!CDocument::OnSaveDocument(lpszPathName))return FALSE;
	UndoFlag=FALSE;
	SetModifiedFlag(UndoFlag);
	return m_mBmp.Save(lpszPathName);
}

BOOL CBmpDemoDoc::IsMiOf2(int x)
{
	int i=0;
	int a=x;
	while(a)
	{
		a>>=1;
		i++;
	}
	i--;
	a=1;
	if(a<<i!=x)return FALSE;
	return TRUE;
}

BOOL CBmpDemoDoc::OnNewDocument() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDocument::OnNewDocument();
}


void CBmpDemoDoc::OnNegative() 
{
	m_mOldBmp=m_mBmp;
	UndoFlag=TRUE;
	int width=m_mBmp.GetWidth();
	int height=m_mBmp.GetHeight();
	int i,j;
	for(i=0;i<width;i++)
	{
		for(j=0;j<height;j++)
		{
			COLORREF color=m_mBmp.GetPixel(i,j);
			BYTE r=GetRValue(color);
			BYTE g=GetGValue(color);
			BYTE b=GetBValue(color);
			r=255-r;
			g=255-g;
			b=255-b;
			m_mBmp.SetPixel(i,j,RGB(r,g,b));
		}
	}
	SetModifiedFlag(UndoFlag);
	m_mBmp.DataToView(m_mViewBmp);
}

void CBmpDemoDoc::OnSinNoise(double Re,double Im,int dis,double jd,int even) 
{
	// TODO: Add your command handler code here
	int width=m_mBmp.GetWidth();
	int height=m_mBmp.GetHeight();
	if(!IsMiOf2(width)|!IsMiOf2(height))
	{
		AfxMessageBox("图象的长和宽为2的幂时频域滤波才可以使用FFT");
		return;
	}

	jd/=180;
	jd*=PIE;
	int xoff=int(dis*cos(jd));
	int yoff=int(dis*sin(jd));
	if(xoff>width/2||yoff>height/2)
	{
		AfxMessageBox("噪声超出图象范围");
		return;
	}

	if(flag==TRUE)delete m_bData;
	flag=FALSE;
	m_bData=new BYTE[(long)width*height*sizeof(wComplex)];
	m_Data=(wComplex *)m_bData;
	if(!m_Data)
	{
		AfxMessageBox("内存不够");
		return;
	}
	int i,j;
	for(i=0;i<width;i++)
	{
		for(j=0;j<height;j++)
		{
			COLORREF color=m_mBmp.GetPixel(i,j);
			int r=GetRValue(color);
			int g=GetGValue(color);
			int b=GetBValue(color);
			float gray1=float(r)*0.3f+(float)g*0.59f+(float)b*0.11f;
			if((i+j)&1==1)
				*(m_Data+j*width+i)=wComplex(-(double)gray1,0.0);
			else
				*(m_Data+j*width+i)=wComplex((double)gray1,0.0);
		}
	}

	m_mOldBmp=m_mBmp;
	UndoFlag=TRUE;
	ImageFFT(m_Data,m_mBmp);
	flag=TRUE;	

	int center=width*height/2+width/2;
	*(m_Data+center+xoff+yoff*width)+=wComplex(Re,Im);
	if(even==2)
		*(m_Data+center-xoff-yoff*width)+=wComplex(Re,Im);

	ImageNFFT(m_Data,m_mBmp);				
	SetModifiedFlag(UndoFlag);	
	m_mBmp.DataToView(m_mViewBmp);
}

void CBmpDemoDoc::OnZhongZhiFilter(BYTE flag) 
{
	// TODO: Add your command handler code here
	m_mOldBmp=m_mBmp;
	UndoFlag=TRUE;
	int width=m_mBmp.GetWidth();
	int height=m_mBmp.GetHeight();
	int i,j,k,l;

	if(flag==1)
	{
		for(j=0;j<height;j++)
		{
			for(i=1;i<width-1;i++)
			{
				int Grey[3];
				for(k=-1;k<=1;k++)
				{
					COLORREF color=m_mOldBmp.GetPixel(i+k,j);
					BYTE r=GetRValue(color);
					BYTE g=GetGValue(color);
					BYTE b=GetBValue(color);
					Grey[k+1]=int(float(r)*0.59+float(g)*0.3+float(b)*0.11+0.5f);
				}
				int GreyTemp;
				for(k=0;k<3;k++)
				{
					GreyTemp=Grey[k];
					for(l=0;l<3;l++)
					{
						if(Grey[l]<GreyTemp)
						{
							Grey[k]=Grey[l];
							Grey[l]=GreyTemp;
							GreyTemp=Grey[k];
						}
					}
				}
				GreyTemp=Grey[1];
				m_mBmp.SetPixel(i,j,RGB(GreyTemp,GreyTemp,GreyTemp));
			}
		}
		for(j=0;j<height;j++)
		{
			COLORREF color=m_mBmp.GetPixel(1,j);
			m_mBmp.SetPixel(0,j,color);
			color=m_mBmp.GetPixel(width-2,j);
			m_mBmp.SetPixel(width-1,j,color);
		}
	}

	else if(flag==2)
	{
		for(j=0;j<height;j++)
		{
			for(i=2;i<width-2;i++)
			{
				int Grey[5];
				for(k=-2;k<=2;k++)
				{
					COLORREF color=m_mOldBmp.GetPixel(i+k,j);
					BYTE r=GetRValue(color);
					BYTE g=GetGValue(color);
					BYTE b=GetBValue(color);
					Grey[k+2]=int(float(r)*0.59+float(g)*0.3+float(b)*0.11+0.5f);
				}
				int GreyTemp;
				for(k=0;k<5;k++)
				{
					GreyTemp=Grey[k];
					for(l=0;l<5;l++)
					{
						if(Grey[l]<GreyTemp)
						{
							Grey[k]=Grey[l];
							Grey[l]=GreyTemp;
							GreyTemp=Grey[k];
						}
					}
				}
				GreyTemp=Grey[2];
				m_mBmp.SetPixel(i,j,RGB(GreyTemp,GreyTemp,GreyTemp));
			}
		}
		for(j=0;j<height;j++)
		{
			COLORREF color=m_mBmp.GetPixel(2,j);
			m_mBmp.SetPixel(0,j,color);
			m_mBmp.SetPixel(1,j,color);
			color=m_mBmp.GetPixel(width-3,j);
			m_mBmp.SetPixel(width-1,j,color);
			m_mBmp.SetPixel(width-2,j,color);
		}
	}

	else if(flag==3)
	{
		for(i=0;i<width;i++)
		{
			for(j=1;j<height-1;j++)
			{
				int Grey[3];
				for(k=-1;k<=1;k++)
				{
					COLORREF color=m_mOldBmp.GetPixel(i,j+k);
					BYTE r=GetRValue(color);
					BYTE g=GetGValue(color);
					BYTE b=GetBValue(color);
					Grey[k+1]=int(float(r)*0.59+float(g)*0.3+float(b)*0.11+0.5f);
				}
				int GreyTemp;
				for(k=0;k<3;k++)
				{
					GreyTemp=Grey[k];
					for(l=0;l<3;l++)
					{
						if(Grey[l]<GreyTemp)
						{
							Grey[k]=Grey[l];
							Grey[l]=GreyTemp;
							GreyTemp=Grey[k];
						}
					}
				}
				GreyTemp=Grey[1];
				m_mBmp.SetPixel(i,j,RGB(GreyTemp,GreyTemp,GreyTemp));
			}
		}
		for(i=0;i<width;i++)
		{
			COLORREF color=m_mBmp.GetPixel(i,1);
			m_mBmp.SetPixel(i,0,color);
			color=m_mBmp.GetPixel(i,height-2);
			m_mBmp.SetPixel(i,height-1,color);
		}
	}

	else if(flag==4)
	{
		for(i=0;i<width;i++)
		{
			for(j=2;j<height-2;j++)
			{
				int Grey[5];
				for(k=-2;k<=2;k++)
				{
					COLORREF color=m_mOldBmp.GetPixel(i,j+k);
					BYTE r=GetRValue(color);
					BYTE g=GetGValue(color);
					BYTE b=GetBValue(color);
					Grey[k+2]=int(float(r)*0.59+float(g)*0.3+float(b)*0.11+0.5f);
				}
				int GreyTemp;
				for(k=0;k<5;k++)
				{
					GreyTemp=Grey[k];
					for(l=0;l<5;l++)
					{
						if(Grey[l]<GreyTemp)
						{
							Grey[k]=Grey[l];
							Grey[l]=GreyTemp;
							GreyTemp=Grey[k];
						}
					}
				}
				GreyTemp=Grey[2];
				m_mBmp.SetPixel(i,j,RGB(GreyTemp,GreyTemp,GreyTemp));
			}
		}
		for(i=0;i<width;i++)
		{
			COLORREF color=m_mBmp.GetPixel(i,2);
			m_mBmp.SetPixel(i,0,color);
			m_mBmp.SetPixel(i,1,color);
			color=m_mBmp.GetPixel(i,height-3);
			m_mBmp.SetPixel(i,height-1,color);
			m_mBmp.SetPixel(i,height-2,color);
		}
	}

	m_mBmp.DataToView(m_mViewBmp);
	SetModifiedFlag(UndoFlag);	
}

void CBmpDemoDoc::OnGreyDuiShuBianHuan(int var) 
{
	// TODO: Add your command handler code here
	m_mOldBmp=m_mBmp;
	UndoFlag=TRUE;
	int width=m_mBmp.GetWidth();
	int height=m_mBmp.GetHeight();
	int i,j;
	for(i=0;i<width;i++)
	{
		for(j=0;j<height;j++)
		{
			COLORREF color=m_mBmp.GetPixel(i,j);
			BYTE r=GetRValue(color);
			BYTE g=GetGValue(color);
			BYTE b=GetBValue(color);
			int Grey=int(float(r)*0.59+float(g)*0.3+float(b)*0.11+0.5f);
			Grey=int(log(1+Grey)*var);
			Grey=Grey>255?255:Grey;
			m_mBmp.SetPixel(i,j,RGB(BYTE(Grey),BYTE(Grey),BYTE(Grey)));
		}
	}
	m_mBmp.DataToView(m_mViewBmp);
	SetModifiedFlag(UndoFlag);	
}

void CBmpDemoDoc::OnRotate(int jd1) 
{
	// TODO: Add your command handler code here
	double jd;
	jd=(double)jd1/180;
	jd*=PIE;
	m_mOldBmp=m_mBmp;
	UndoFlag=TRUE;
	float scale=m_mBmp.GetScale();
	int width=m_mBmp.GetWidth();
	int height=m_mBmp.GetHeight();
	int OldDjx=int(sqrt(width*width+height*height)+0.5);//对角线长度
	double OldDjxJd=atan((double)height/(double)width); //对角线与水平方向所成角度
	double NewDjxJd=OldDjxJd+jd;
	double NewDjxJd1=OldDjxJd-jd;
	int NewWidth=(int)fabs((double)OldDjx*cos(NewDjxJd));
	int NewHeight=(int)fabs((double)OldDjx*sin(NewDjxJd));
	int NewWidth1=(int)fabs((double)OldDjx*cos(NewDjxJd1));
	int NewHeight1=(int)fabs((double)OldDjx*sin(NewDjxJd1));
	NewWidth=max(NewWidth,NewWidth1);
	NewHeight=max(NewHeight,NewHeight1);
	memBITMAP bmp1;
	bmp1.CreateDirect(NewWidth,NewHeight);
	
	int i,j;
	double RotateMatrixElement[4];
	RotateMatrixElement[0]=cos(jd);
	RotateMatrixElement[1]=-sin(jd);
	RotateMatrixElement[2]=sin(jd);
	RotateMatrixElement[3]=cos(jd);
	
	int temp=int((double)width*sin(jd));

	for(i=0;i<NewWidth;i++)
	{
		for(j=0;j<NewHeight;j++)
		{
			double xpos=RotateMatrixElement[0]*(i-NewWidth/2)+RotateMatrixElement[2]*(j-NewHeight/2)+0.5;
			double ypos=RotateMatrixElement[1]*(i-NewWidth/2)+RotateMatrixElement[3]*(j-NewHeight/2)+0.5;
			double x=xpos+(double)width/2;
			double y=ypos+(double)height/2;
			if( (x>=0) && (x<width) && (y>=0) && (y<height) )
			{
				if(x<=0)x=0;
				if(x>=width-1)x=width-1;
				if(y<=0)y=0;
				if(y>=height-1)y=height-1;
				if( ((int)x==0)||((int)x==width-1)||((int)y==0)||((int)y==height-1) )
				{
					COLORREF color=m_mBmp.GetPixel((int)x,(int)y);
					bmp1.SetPixel(i,j,color);
				}
				else
				{
					double r1,r2,r3,r4;
					double g1,g2,g3,g4;
					double b1,b2,b3,b4;
					COLORREF c1,c2,c3,c4;
					c1=m_mBmp.GetPixel((int)x,(int)y);
					c2=m_mBmp.GetPixel((int)x+1,(int)y);
					c3=m_mBmp.GetPixel((int)x,(int)y+1);
					c4=m_mBmp.GetPixel((int)x+1,(int)y+1);
					double dis1=x-(int)x;
					double dis2=1-x+(int)x;
					double dis3=y-(int)y;
					double dis4=1-y+(int)y;
					r1=GetRValue(c1);
					g1=GetGValue(c1);
					b1=GetBValue(c1);
					r2=GetRValue(c2);
					g2=GetGValue(c2);
					b2=GetBValue(c2);
					r3=GetRValue(c3);
					g3=GetGValue(c3);
					b3=GetBValue(c3);
					r4=GetRValue(c4);
					g4=GetGValue(c4);
					b4=GetBValue(c4);

					int r=int(dis4*(r1*dis2+r2*dis1)+dis3*(r3*dis2+r4*dis1));
					if(r>255)r=255;
					int g=int(dis4*(g1*dis2+g2*dis1)+dis3*(g3*dis2+g4*dis1));
					if(g>255)g=255;
					int b=int(dis4*(b1*dis2+b2*dis1)+dis3*(b3*dis2+b4*dis1));
					if(b>255)b=255;
					
					bmp1.SetPixel(i,j,RGB((BYTE)r,(BYTE)g,(BYTE)b));
				}
//				COLORREF color=m_mBmp.GetPixel(x+width/2,y+height/2);
//				bmp1.SetPixel(i,j,color);
			}
			else
			{
				bmp1.SetPixel(i,j,RGB(255,255,255));
			}
		}
	}
	m_mBmp=bmp1;
	m_mBmp.SetScale(scale);
	m_mBmp.DataToView(m_mViewBmp);
	SetModifiedFlag(UndoFlag);	
}

void CBmpDemoDoc::OnImageproperty(int cx,int cy) 
{
	// TODO: Add your command handler code here
/*	int width=m_mBmp.GetWidth();
	int height=m_mBmp.GetHeight();
	if( (cx==width)&&(cy==height))return;	

	m_mOldBmp=m_mBmp;
	UndoFlag=TRUE;
	double sx,sy;				//x和y方向的比例系数
	memBITMAP bmp1;
	bmp1.CreateDirect(cx,cy);
	sx=double(cx)/(double)width;
	sy=double(cy)/(double)height;
	int i,j;

	for(i=0;i<cx;i++)
	{
		for(j=0;j<cy;j++)
		{
			double x,y;
			x=(double)i/sx;
			y=(double)j/sy;
			if(x<=0)x=0;
			if(x>=width-1)x=width-1;
			if(y<=0)y=0;
			if(y>=height-1)y=height-1;
			if( ((int)x==0)||((int)x==width-1)||((int)y==0)||((int)y==height-1) )
			{
				COLORREF color=m_mBmp.GetPixel((int)x,(int)y);
				bmp1.SetPixel(i,j,color);
			}
			else
			{
				double r1,r2,r3,r4;
				double g1,g2,g3,g4;
				double b1,b2,b3,b4;
				COLORREF c1,c2,c3,c4;
				c1=m_mBmp.GetPixel((int)x,(int)y);
				c2=m_mBmp.GetPixel((int)x+1,(int)y);
				c3=m_mBmp.GetPixel((int)x,(int)y+1);
				c4=m_mBmp.GetPixel((int)x+1,(int)y+1);
				double dis1=x-(int)x;
				double dis2=1-x+(int)x;
				double dis3=y-(int)y;
				double dis4=1-y+(int)y;
				r1=GetRValue(c1);
				g1=GetGValue(c1);
				b1=GetBValue(c1);
				r2=GetRValue(c2);
				g2=GetGValue(c2);
				b2=GetBValue(c2);
				r3=GetRValue(c3);
				g3=GetGValue(c3);
				b3=GetBValue(c3);
				r4=GetRValue(c4);
				g4=GetGValue(c4);
				b4=GetBValue(c4);

				int r=int(dis4*(r1*dis2+r2*dis1)+dis3*(r3*dis2+r4*dis1));
				if(r>255)r=255;
				int g=int(dis4*(g1*dis2+g2*dis1)+dis3*(g3*dis2+g4*dis1));
				if(g>255)g=255;
				int b=int(dis4*(b1*dis2+b2*dis1)+dis3*(b3*dis2+b4*dis1));
				if(b>255)b=255;
				
				bmp1.SetPixel(i,j,RGB((BYTE)r,(BYTE)g,(BYTE)b));

//				COLORREF color=GetPixel(int(x+0.5),int(y+0.5));
//				bmp1.SetPixel(i,j,color);			
			}
		}
	}
	m_mBmp=bmp1;
	bmp1.Release();
*/
	UndoFlag=m_mBmp.ChangeSize(cx,cy);
	m_mBmp.DataToView(m_mViewBmp);
	SetModifiedFlag(UndoFlag);	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -