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

📄 weimin_bmp.cpp

📁 能实现图像的各种效果
💻 CPP
字号:
// weimin_Bmp.cpp: implementation of the Cweimin_Bmp class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "test.h"
#include "weimin_Bmp.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Cweimin_Bmp::Cweimin_Bmp()
{
  m_nPixels=0;
  m_pInfo=NULL;
  m_pPixels=NULL;

}

Cweimin_Bmp::~Cweimin_Bmp()
{

}

void Cweimin_Bmp::DrawDIB(CDC *pDC, int x, int y)
{
 ASSERT(pDC);
  HDC hdc=pDC->GetSafeHdc();
  if(m_pInfo)
	  StretchDIBits(hdc,x,y,m_pInfo->bmiHeader.biWidth,
	  m_pInfo->bmiHeader.biHeight,0,0,m_pInfo->bmiHeader.biWidth,
	  m_pInfo->bmiHeader.biHeight,m_pPixels,m_pInfo,
	  DIB_RGB_COLORS,SRCCOPY);

}

bool Cweimin_Bmp::Load(CString filename)
{
  ASSERT(filename);
  bool fReturn=TRUE;
  CFile dibFile(filename,CFile::modeRead);
    delete[](BYTE*)m_pInfo;
    delete[] m_pPixels;
    m_pInfo=NULL;
    m_pPixels=NULL;

    DWORD dwStart=dibFile.GetPosition();
    BITMAPFILEHEADER fileHeader;
    dibFile.Read(&fileHeader,sizeof(BITMAPFILEHEADER));
    BITMAPINFOHEADER infoHeader;
    dibFile.Read(&infoHeader,sizeof(BITMAPINFOHEADER));
   
  int cInfo=sizeof(BITMAPINFOHEADER);
  m_nPixels=fileHeader.bfSize-fileHeader.bfOffBits;
  m_pInfo=(BITMAPINFO*) new BYTE[cInfo];
  memcpy(m_pInfo,&infoHeader,sizeof(BITMAPINFOHEADER));
  m_pPixels=new BYTE[m_nPixels];
  dibFile.Seek(dwStart+fileHeader.bfOffBits,CFile::begin);
  dibFile.Read(m_pPixels,m_nPixels);

  return fReturn;

}

void Cweimin_Bmp::Color_To_Gray()
{
  int nWidth=m_pInfo->bmiHeader.biWidth;
  int nHeight=m_pInfo->bmiHeader.biHeight;
  int LineByte=WIDTHBYTE(nWidth*24);
  for(int i=0;i<nHeight;i++)
	  for(int j=0;j<LineByte;j+=3)
	  {
	   int temp=(0.299*m_pPixels[i*LineByte+j]+\
		   0.587*m_pPixels[i*LineByte+j+1]+\
		   0.114*m_pPixels[i*LineByte+j+2]);
       m_pPixels[i*LineByte+j]=m_pPixels[i*LineByte+j+1]=\
           m_pPixels[i*LineByte+j+2]=temp;
	  }

}

void Cweimin_Bmp::GAuss()
{
    int nWidth=m_pInfo->bmiHeader.biWidth;
	int nHeight=m_pInfo->bmiHeader.biHeight;
    int LineByte=WIDTHBYTE(nWidth*24);
    for(int i=1;i<nHeight-1;i++)
		for(int j=1;j<LineByte-1;j+=3)
	{
		int R=(m_pPixels[(i-1)*LineByte+j-3]
			+2*m_pPixels[(i-1)*LineByte+j]
			+m_pPixels[(i-1)*LineByte+j+3]
			+2*m_pPixels[i*LineByte+j-3]
			+4*m_pPixels[i*LineByte+j]
			+2*m_pPixels[i*LineByte+j+3]
			+m_pPixels[(i+1)*LineByte+j-3]
			+2*m_pPixels[(i+1)*LineByte+j]
			+m_pPixels[(i+1)*LineByte+j+3])/16;

		int G=(m_pPixels[(i-1)*LineByte+j-2]
			+2*m_pPixels[(i-1)*LineByte+j+1]
			+m_pPixels[(i-1)*LineByte+j+4]
			+2*m_pPixels[i*LineByte+j-2]
			+4*m_pPixels[i*LineByte+j+1]
			+2*m_pPixels[i*LineByte+j+4]
			+m_pPixels[(i+1)*LineByte+j-2]
			+2*m_pPixels[(i+1)*LineByte+j+1]
			+m_pPixels[(i+1)*LineByte+j+4])/16;

		int B=(m_pPixels[(i-1)*LineByte+j-1]
			+2*m_pPixels[(i-1)*LineByte+j+2]
			+m_pPixels[(i-1)*LineByte+j+5]
			+2*m_pPixels[i*LineByte+j-1]
			+4*m_pPixels[i*LineByte+j+2]
			+2*m_pPixels[i*LineByte+j+5]
			+m_pPixels[(i+1)*LineByte+j-1]
			+2*m_pPixels[(i+1)*LineByte+j+2]
			+m_pPixels[(i+1)*LineByte+j+5])/16;		
	
		m_pPixels[i*LineByte+j]=R;
		m_pPixels[i*LineByte+j+1]=G;
		m_pPixels[i*LineByte+j+2]=B;
	}
	
}

void Cweimin_Bmp::AUERAGE()
{
    int nWidth=m_pInfo->bmiHeader.biWidth;
	int nHeight=m_pInfo->bmiHeader.biHeight;
    int LineByte=WIDTHBYTE(nWidth*24);
    for(int y=1;y<nHeight-1;y++)
		for(int x=1;x<LineByte-1;x+=3)
	{
		int R=m_pPixels[x+y*LineByte]=MiddleFunc(m_pPixels[x-3+y*LineByte],
			                                     m_pPixels[x+y*LineByte],
												 m_pPixels[x+3+y*LineByte]);
			

		int G=m_pPixels[x+1+y*LineByte]=MiddleFunc(m_pPixels[x-2+y*LineByte],
			                                     m_pPixels[x+1+y*LineByte],
												 m_pPixels[x+4+y*LineByte]);

		int B=m_pPixels[x+2+y*LineByte]=MiddleFunc(m_pPixels[x-1+y*LineByte],
			                                     m_pPixels[x+2+y*LineByte],
												 m_pPixels[x+5+y*LineByte]);	
	
		
	}
}

void Cweimin_Bmp::ERZH()
{
	CValue dlg;
	int value;
	if(dlg.DoModal()==IDOK)
		value=dlg.m_value;
	
    int nWidth=m_pInfo->bmiHeader.biWidth;
	int nHeight=m_pInfo->bmiHeader.biHeight;
    int LineByte=WIDTHBYTE(nWidth*24);
    for(int i=0;i<nHeight;i++)
		for(int j=0;j<LineByte; j+=3)
		{
		  int max=COM_MAX(m_pPixels[i*LineByte+j],
			              m_pPixels[i*LineByte+j+1],
						  m_pPixels[i*LineByte+j+2]);
		if(max<value)
			m_pPixels[i*LineByte+j]=m_pPixels[i*LineByte+j+1]=\
			                        m_pPixels[i*LineByte+j+2]=0;
		else 
            m_pPixels[i*LineByte+j]=m_pPixels[i*LineByte+j+1]=\
			                        m_pPixels[i*LineByte+j+2]=255;
		}
}

int Cweimin_Bmp::COM_MAX(int x,int y,int z)
{
	return (((x>y)?x:y>z)?((x>y)?x:y):z);
}

int Cweimin_Bmp::MiddleFunc(int x,int y,int z)
{
    int temp;
    x=(((x>y)?x:y>z)?((x>y)?x:y):z);
	if(y>z)
      temp=y;
	else if(z>y)
     temp=z;
	return temp;
}

void Cweimin_Bmp::TXPY()
{
    int x;
	int y;
	Cpingyi pingyi;
	if(pingyi.DoModal()==IDOK)
	{
	 x=24*pingyi.m_offx;
	 y=8*pingyi.m_offy;
	}
	int nWidth=m_pInfo->bmiHeader.biWidth;
	int nHeight=m_pInfo->bmiHeader.biHeight;
    int LineByte=WIDTHBYTE(nWidth*24);
    for(int i=0;i<nHeight;i++)
	{	
	  for(int j=1;j<LineByte; j+=3)

      if((j<LineByte-x)&&(i<nHeight-y))
	  {
	   int R=m_pPixels[(nHeight-i)*LineByte-j]=
		   m_pPixels[(nHeight-i-y)*LineByte-j-x];
       int G=m_pPixels[(nHeight-i)*LineByte-j-1]=
		   m_pPixels[(nHeight-i-y)*LineByte-j-x-1];
       int B=m_pPixels[(nHeight-i)*LineByte-j-2]=
		   m_pPixels[(nHeight-i-y)*LineByte-j-x-2];
	  }

        else  	  
		{ 
		  int R=m_pPixels[(nHeight-i)*LineByte-j]=128;
 	      int G=m_pPixels[(nHeight-i)*LineByte-j-1]=128; 	   
	      int B=m_pPixels[(nHeight-i)*LineByte-j-2]=128;
		}
	}
}

void Cweimin_Bmp::SPJX()
{
    int nWidth=m_pInfo->bmiHeader.biWidth;
	int nHeight=m_pInfo->bmiHeader.biHeight;
    int LineByte=WIDTHBYTE(nWidth*24);
    for(int y=1;y<nHeight;y++)
	  for(int x=1;x<LineByte/2;x+=3)
	   {
		   int R=m_pPixels[y*LineByte+x];
		     m_pPixels[y*LineByte+x]=m_pPixels[(y+1)*LineByte-x-4];
             m_pPixels[(y+1)*LineByte-x-4]=R;
		  int G=m_pPixels[y*LineByte+x+1];
		     m_pPixels[y*LineByte+x+1]=m_pPixels[(y+1)*LineByte-x-3];
             m_pPixels[(y+1)*LineByte-x-3]=G;
		  int B=m_pPixels[y*LineByte+x+2];
		     m_pPixels[y*LineByte+x+2]=m_pPixels[(y+1)*LineByte-x-2];
             m_pPixels[(y+1)*LineByte-x-2]=B;
		
		
		} 
}

void Cweimin_Bmp::CZJX()
{
    int nWidth=m_pInfo->bmiHeader.biWidth;
	int nHeight=m_pInfo->bmiHeader.biHeight;
    int LineByte=WIDTHBYTE(nWidth*24);
    for(int y=1;y<nHeight;y+=3)
		for(int x=1;x<LineByte/2;x++)
		{
		   int R=m_pPixels[x*nHeight+(y-2)];
		     m_pPixels[x*nHeight+(y-2)]=m_pPixels[(LineByte-x)*nHeight+(y-2)];
             m_pPixels[(LineByte-x)*nHeight+(y-2)]=R;
		  int G=m_pPixels[x*nHeight+(y-1)];
		     m_pPixels[x*nHeight+(y-1)]=m_pPixels[(LineByte-x)*nHeight+(y-1)];
             m_pPixels[(LineByte-x)*nHeight+(y-1)]=G;
		  int B=m_pPixels[x*nHeight+y];
		     m_pPixels[x*nHeight+y]=m_pPixels[(LineByte-x)*nHeight+(y)];
             m_pPixels[(LineByte-x)*nHeight+(y)]=B;
		} 
}

void Cweimin_Bmp::zftjh()
{
  
	int nWidth=m_pInfo->bmiHeader.biWidth;
	int nHeight=m_pInfo->bmiHeader.biHeight;
    int LineByte=WIDTHBYTE(nWidth*24);
    //统计各亮度的象素点个数
    double r[256], g[256], b[256];
	for(int i=0;i<256;i++) r[i] = g[i] = b[i] = 0.0;
     for(int a=0;a<256;a++)
	 {
	   for(int i=0;i<nHeight;i++)
		for(int j=0;j<LineByte;j+=3)
		{
		  if (a==m_pPixels[i*LineByte+j])
		    r[a]+=1; 
	  	  if (a==m_pPixels[i*LineByte+j+1])
			g[a]+=1;
		  if (a==m_pPixels[i*LineByte+j+2])
		    b[a]+=1;
			
		}
	 }
	double pr[256], pg[256], pb[256]; 
	for(int m=0;m<256;m++)
	{
		pr[m] = pg[m] = pb[m] = 0.0;
		for(int j=0;j<=m;j++)
		{
			pr[m] += r[j];
			pg[m] += g[j];
			pb[m] += b[j];
		}
		pr[m] *=3*(255.0/(double)(LineByte*nHeight));
		pg[m] *=3*(255.0/(double)(LineByte*nHeight));
		pb[m] *=3*(255.0/(double)(LineByte*nHeight));
			
	}
	for(int n=0;n<nHeight;n++)
	{	
	  for(int j=0;j<LineByte; j+=3)
	  {
         m_pPixels[n*LineByte+j]=
            pr[m_pPixels[n*LineByte+j]];
         m_pPixels[n*LineByte+j+1]=
            pg[m_pPixels[n*LineByte+j+1]];
         m_pPixels[n*LineByte+j+2]=
            pb[m_pPixels[n*LineByte+j+2]];
	  }
	}

}

void Cweimin_Bmp::txtc()
{
    int nWidth=m_pInfo->bmiHeader.biWidth;
	int nHeight=m_pInfo->bmiHeader.biHeight;
    int LineByte=WIDTHBYTE(nWidth*24);
	for(int i=1;i<nHeight-1;i++)
		if(m_pPixels[i*LineByte+LineByte-1]<=128)
		{
		 for(int j=0;j<LineByte;j++)
		 {
		   if(m_pPixels[(i-1)*LineByte+LineByte-1-j]>=128&&
			  m_pPixels[(i+1)*LineByte+LineByte-1-j]>=128)
              m_pPixels[i*LineByte+LineByte-1-j]=255;
		   else if(m_pPixels[(i-1)*LineByte+LineByte-1-j]<128&&
			       m_pPixels[(i+1)*LineByte+LineByte-1-j]<128)
                   m_pPixels[i*LineByte+LineByte-1-j]=0;
		 }
		}
}

⌨️ 快捷键说明

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