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

📄 bmp.cpp

📁 this program show how to get the value of every pixel,and you can modify it based on this case.
💻 CPP
字号:
// BMP.cpp: implementation of the CBMP class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BMP.h"

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

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

CBMP::CBMP()
{
	rgb=NULL;
    buf=NULL;

}


CBMP::modify_rgb(int startx,int starty,int endx,int endy,int step)
{
 

int xrow=startx,xcol=starty;
int tmp,i=0;
//endx=lheight
//endy=lwidth
if(!(xrow<0&&xcol<0&&endx>lHeight&&endy>lWidth))
{
//R
for(xrow=startx;xrow<endx;xrow++)
 for(xcol=starty;xcol<endy;xcol=xcol+step)
 {tmp=0;
 for(i=0;i<step;i++)
  tmp=tmp+pArray_R[xrow][xcol+i];
 for(i=0;i<step;i++)
 pArray_R[xrow][xcol+i]=tmp/step; 
 }

for(xcol=starty;xcol<endy;xcol++)
 for(xrow=startx;xrow<endx;xrow=xrow+step)
 {tmp=0;
 for(i=0;i<step;i++)
  tmp=tmp+pArray_R[xrow+i][xcol];
 for(i=0;i<step;i++)
  pArray_R[xrow+i][xcol]=tmp/step;
 
 }
 
//g
for(xrow=startx;xrow<endx;xrow++)
 for(xcol=starty;xcol<endy;xcol=xcol+step)
 {tmp=0;
 for(i=0;i<step;i++)
  tmp=tmp+pArray_G[xrow][xcol+i];
 for(i=0;i<step;i++)
 pArray_G[xrow][xcol+i]=tmp/step; 
 }

for(xcol=starty;xcol<endy;xcol++)
 for(xrow=startx;xrow<endx;xrow=xrow+step)
 {tmp=0;
 for(i=0;i<step;i++)
  tmp=tmp+pArray_G[xrow+i][xcol];
 for(i=0;i<step;i++)
  pArray_G[xrow+i][xcol]=tmp/step;
 
 }
//b
 
for(xrow=startx;xrow<endx;xrow++)
 for(xcol=starty;xcol<endy;xcol=xcol+step)
 {
 tmp=0;
 for(i=0;i<step;i++)
  tmp=tmp+pArray_B[xrow][xcol+i];
 for(i=0;i<step;i++)
 pArray_B[xrow][xcol+i]=tmp/step; 
 }

for(xcol=starty;xcol<endy;xcol++)
 for(xrow=startx;xrow<endx;xrow=xrow+step)
 {tmp=0;
 for(i=0;i<step;i++)
  tmp=tmp+pArray_B[xrow+i][xcol];
 for(i=0;i<step;i++)
  pArray_B[xrow+i][xcol]=tmp/step;
 
 }
MessageBox(0,0,"masaic end!",0);

}else{
  MessageBox(0,0,"区域越界!",0);
}
}


CBMP::~CBMP()
{
free(buf);
}

/*************************************
功能:将图象的RGB转换为数组
      现只能处理24位彩色
参数:*bmpFileName:图象文件名
		
返回参数:
		0:	成功
		-1:	文件打开失败
		-2:	该文件不是BMP文件
		-3:	内存不够
*************************************/
int CBMP::OpenBMPFile (char * bmpFileName)
{	CFile  FileBmp;//
	BITMAPFILEHEADER bfh;//文件标题结构
	BITMAPINFOHEADER bih;//信息标题结构

	if(!(FileBmp.Open(bmpFileName, CFile::modeRead)))
	{       
		MessageBox(0,"File open failed",bmpFileName,0);
		return -1;//Failed to open FileBmp
	}

	FileBmp.Read((void*)&bfh,sizeof(bfh));
	if (bfh.bfType != 0X4d42)
	{//判断是否位图文件,0X4d42是“BM”的数值
		FileBmp.Close();
		return -2;//not a bmp FileBmp  
	}

	FileBmp.Read((unsigned char*)&bih,sizeof(bih));
	//24位图
	if((bih.biBitCount == 24)&&(bih.biCompression ==BI_RGB))
	{
		iBmpStyle = 24;

		//取得图像高宽
         lHeight = bih.biHeight ;
		 lWidth = bih.biWidth ;
		// if(lHeight > MAXHEIGHT) lHeight = MAXHEIGHT;
	//	 if(lWidth > MAXWIDTH)   lWidth = MAXWIDTH;

        int iRow, iCol;//index of the specified region.
	    short iRGB;
	    
	    int BytesPerRow;    //每行中数据字节数
		//每行中数据字节长度为4的倍数
	    BytesPerRow = (bih.biWidth*3+3)& (~3); // fffffffc

		rgb=(unsigned char*)malloc((lWidth)*3);
		buf=(unsigned char*)malloc(BytesPerRow*lHeight);
    	if( rgb == NULL )
		{
	    	MessageBox(0, "Insufficient memory available\n", "Error", 0);
	    	return -3;
		}
        
		//读取文件数据
		for(iRow = 0;iRow<lHeight;iRow++)
		{
			FileBmp.Seek ((bih.biHeight - 1 -iRow) *BytesPerRow + bfh.bfOffBits,CFile::begin);
			FileBmp.Read (rgb,lWidth*3L);
			memcpy(buf+(lHeight-iRow-1)*BytesPerRow,rgb,lWidth*3L);
			for(iCol=0,iRGB=0;iCol<lWidth;iCol++)
			{
				pArray_B[iRow][iCol] = rgb[iRGB++];
				pArray_G[iRow][iCol] = rgb[iRGB++];
				pArray_R[iRow][iCol] = rgb[iRGB++];
			}
			
		}

		
		FileBmp.Close ();
		bOpen = true;
		return 0;
	}
	free(rgb);
	return 0;
}

void rgb(int startx,int starty,int endx,int endy,int step,BYTE p[][MAXWIDTH])
{
int xrow=startx,xcol=starty;
int tmp,i=0;

	for(xrow=startx;xrow<endx;xrow++)
 for(xcol=starty;xcol<endy;xcol=xcol+step)
 {tmp=0;
 for(i=0;i<step;i++)
  tmp=tmp+p[xrow][xcol+i];
 for(i=0;i<step;i++)
 p[xrow][xcol+i]=tmp/step; 
 }

for(xcol=starty;xcol<endy;xcol++)
 for(xrow=startx;xrow<endx;xrow=xrow+step)
 {tmp=0;
 for(i=0;i<step;i++)
  tmp=tmp+p[xrow+i][xcol];
 for(i=0;i<step;i++)
  p[xrow+i][xcol]=tmp/step;
 
 }
}

⌨️ 快捷键说明

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