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

📄 bmpdat.cpp

📁 毕业设计程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// BmpDat.cpp: implementation of the CBmpDat class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "毕业设计.h"
#include "BmpDat.h"

#include <time.h>
#include <stdlib.h>
#include <math.h>

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

extern CStatusBar * mmcsb;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBmpDat::CBmpDat(CDC * dc){
	undo = 0;
	redo = 2;
	width = 0;
	height = 0;
	path = _T("");
	bgcol = RGB(255,255,255);
	pecol = RGB(0,0,0);
	pdc.CreateCompatibleDC(dc);
	bfdc.CreateCompatibleDC(dc);
	mdc[0].CreateCompatibleDC(dc);
	mdc[1].CreateCompatibleDC(dc);
	CBitmap *obp;
//	bp.CreateCompatibleBitmap(dc,1024,786);
	bp.LoadBitmap(IDB_BBB);
	////////////////////////////////////将位图选入DC
	obp = pdc.SelectObject(&bp);
	obp->DeleteObject();
	//////////////////////////////////////创建一个白色画笔
//	CPen pen,*op;
//	pen.CreatePen(PS_SOLID,796,RGB(255,255,255));
	//////////////////////////////////////使用白色画笔
//	op = pdc.SelectObject(&pen);
	//////////////////////////////////////
//	pdc.MoveTo(0,393);
//	pdc.LineTo(1024,393);
	////////////////////////////////////////
//	pdc.SelectObject(op);
	pdc.SetTextColor(pecol);
	pdc.SetBkMode(0);

	//////////////////////////
	CBitmap *obp2;
//	bp2.CreateCompatibleBitmap(dc,1024,786);
	bp2.LoadBitmap(IDB_BBB);
	////////////////////////////////////将位图选入DC
	obp2 = bfdc.SelectObject(&bp2);
	obp2->DeleteObject();

	CBitmap *obp3;
//	bp3.CreateCompatibleBitmap(dc,1024,786);
	bp3.LoadBitmap(IDB_BBB);
	////////////////////////////////////将位图选入DC
	obp3 = mdc[0].SelectObject(&bp3);
	obp3->DeleteObject();

	CBitmap *obp4;
//	bp4.CreateCompatibleBitmap(dc,1024,786);
	bp4.LoadBitmap(IDB_BBB);
	////////////////////////////////////将位图选入DC
	obp4 = mdc[1].SelectObject(&bp4);
	obp4->DeleteObject();

//	pen.DeleteObject();
}

CBmpDat::~CBmpDat(){
}

//////////////
void CBmpDat::Cancel(){
	pdc.BitBlt(0,0,width,height,&bfdc,0,0,SRCCOPY);
	for(int i = 0;i<width;i++){
		for(int j = 0;j<height;j++){
			pix[0][i][j] = bfpix[0][i][j];
			pix[1][i][j] = bfpix[1][i][j];
			pix[2][i][j] = bfpix[2][i][j];
		}
	}
}
///////////////////////////////////////////////
void CBmpDat::BeiFen(){
	int i,j;
	if(undo==2){
		undo--;
		mdc[0].BitBlt(0,0,width,height,&mdc[1],0,0,SRCCOPY);
		for(i = 0;i<width;i++){
			for(j = 0;j<height;j++){
				mpix[0][0][i][j] = mpix[1][0][i][j];
				mpix[0][1][i][j] = mpix[1][1][i][j];
				mpix[0][2][i][j] = mpix[1][2][i][j];
			}
		}
		lastw[0] = lastw[1];
		lasth[0] = lasth[1];
	}
	mdc[undo].BitBlt(0,0,width,height,&bfdc,0,0,SRCCOPY);
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			mpix[undo][0][i][j] = bfpix[0][i][j];
			mpix[undo][1][i][j] = bfpix[1][i][j];
			mpix[undo][2][i][j] = bfpix[2][i][j];
		}
	}
	bfdc.BitBlt(0,0,width,height,&pdc,0,0,SRCCOPY);
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			bfpix[0][i][j] = pix[0][i][j];
			bfpix[1][i][j] = pix[1][i][j];
			bfpix[2][i][j] = pix[2][i][j];
		}
	}
	lastw[undo] = width;
	lasth[undo] = height;
	undo ++;
	redo = 2;
}

void CBmpDat::UnDo(){
	if(!UnDoOK())
		return;
	int i,j;
	int ow,oh;
	undo --;
	redo --;
	ow = width;
	oh = height;
	width = lastw[undo];
	height = lasth[undo];
	int tw,th;
	tw = ow>width?ow:width;
	th = oh>height?oh:height;
	pdc.BitBlt(0,0,tw,th,&mdc[undo],0,0,SRCCOPY);
	for(i = 0;i<tw;i++){
		for(j = 0;j<th;j++){
			pix[0][i][j] = mpix[undo][0][i][j];
			pix[1][i][j] = mpix[undo][1][i][j];
			pix[2][i][j] = mpix[undo][2][i][j];
		}
	}
	mdc[undo].BitBlt(0,0,tw,th,&bfdc,0,0,SRCCOPY);
	for(i = 0;i<tw;i++){
		for(j = 0;j<th;j++){
			mpix[undo][0][i][j] = bfpix[0][i][j];
			mpix[undo][1][i][j] = bfpix[1][i][j];
			mpix[undo][2][i][j] = bfpix[2][i][j];
		}
	}
	bfdc.BitBlt(0,0,tw,th,&pdc,0,0,SRCCOPY);
	for(i = 0;i<tw;i++){
		for(j = 0;j<th;j++){
			bfpix[0][i][j] = pix[0][i][j];
			bfpix[1][i][j] = pix[1][i][j];
			bfpix[2][i][j] = pix[2][i][j];
		}
	}
	lastw[undo] = ow;
	lasth[undo] = oh;
//	oldwidth = width;
//	oldheight = height;
}
void CBmpDat::ReDo(){
	if(!ReDoOK())
		return;
	int i,j;
	int ow,oh;
	ow = width;
	oh = height;
	width = lastw[undo];
	height = lasth[undo];
	int tw,th;
	tw = ow>width?ow:width;
	th = oh>height?oh:height;
	pdc.BitBlt(0,0,tw,th,&mdc[undo],0,0,SRCCOPY);
	for(i = 0;i<tw;i++){
		for(j = 0;j<th;j++){
			pix[0][i][j] = mpix[undo][0][i][j];
			pix[1][i][j] = mpix[undo][1][i][j];
			pix[2][i][j] = mpix[undo][2][i][j];
		}
	}
	mdc[undo].BitBlt(0,0,tw,th,&bfdc,0,0,SRCCOPY);
	for(i = 0;i<tw;i++){
		for(j = 0;j<th;j++){
			mpix[undo][0][i][j] = bfpix[0][i][j];
			mpix[undo][1][i][j] = bfpix[1][i][j];
			mpix[undo][2][i][j] = bfpix[2][i][j];
		}
	}
	bfdc.BitBlt(0,0,tw,th,&pdc,0,0,SRCCOPY);
	for(i = 0;i<tw;i++){
		for(j = 0;j<th;j++){
			bfpix[0][i][j] = pix[0][i][j];
			bfpix[1][i][j] = pix[1][i][j];
			bfpix[2][i][j] = pix[2][i][j];
		}
	}
	lastw[undo] = ow;
	lasth[undo] = oh;
	undo ++;
	redo ++;
}
////////////////////////////////////////////////////
int CBmpDat::Save(CString p){
	if(p.GetLength()>0){
		path = p;
	}
	if(path.GetLength()==0){
		CFileDialog ff(false,NULL,NULL,OFN_OVERWRITEPROMPT,"*.bmp|*.bmp|",NULL);
		if(ff.DoModal()==IDOK){
			path = ff.GetPathName();
			StringEnd(path,".bmp");
		}
		else{
			return -1;
		}
	}
	CDC memDC;
	memDC.CreateCompatibleDC(&pdc);
	CBitmap bm;
	bm.CreateCompatibleBitmap(&pdc, width, height);
	CBitmap*  pOld = memDC.SelectObject(&bm);
	memDC.BitBlt(0, 0, width, height, &pdc, 0, 0, SRCCOPY);
	memDC.SelectObject(pOld);
	BITMAP  btm;
	bm.GetBitmap(&btm);
	DWORD  size = btm.bmWidthBytes * btm.bmHeight;
	LPSTR lpData = (LPSTR)GlobalAlloc(GPTR, size);
	BITMAPFILEHEADER   bfh;
	/////////////////////////////////////////////
	BITMAPINFOHEADER  bih;
	bih.biBitCount = btm.bmBitsPixel;
	bih.biClrImportant = 0;
	bih.biClrUsed = 0;
	bih.biCompression = 0;
	bih.biHeight = btm.bmHeight;
	bih.biPlanes = 1;
	bih.biSize = sizeof(BITMAPINFOHEADER);
	bih.biSizeImage = size;
	bih.biWidth = btm.bmWidth;
	bih.biXPelsPerMeter = 0;
	bih.biYPelsPerMeter = 0;
	GetDIBits(pdc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
	//bm.GetBitmapBits(size,lpData);//此函数在处理5-5-5模式的16位色下会出现颜色混乱
	bfh.bfReserved1 = bfh.bfReserved2 = 0;
	bfh.bfType = ((WORD)('M'<< 8)|'B');
	bfh.bfSize = 54 + size;
	bfh.bfOffBits = 54;

	CFile  bf;
	if(bf.Open(path,CFile::modeCreate|CFile::modeWrite))	{
		bf.WriteHuge(&bfh, sizeof(BITMAPFILEHEADER));
		bf.WriteHuge(&bih, sizeof(BITMAPINFOHEADER));
		bf.WriteHuge(lpData, size);
		bf.Close();
	}
	GlobalFree (lpData);
	return 0;
}

void CBmpDat::New(int w,int h){
	int i,j;
	/////////////////////////////////////创建一个DC
	/////////////////////////////////////创建一个位图
	CBitmap *bp,*obp;
	bp = new CBitmap;
//	bp->CreateCompatibleBitmap(&pdc,1024,786);
	bp->LoadBitmap(IDB_BBB);
	////////////////////////////////////将位图选入DC
	obp = pdc.SelectObject(bp);
	obp->DeleteObject();
	//////////////////////////////////////创建一个白色画笔
//	CPen pen,*op;
//	pen.CreatePen(PS_SOLID,796,RGB(255,255,255));
	//////////////////////////////////////使用白色画笔
//	op = pdc.SelectObject(&pen);
	//////////////////////////////////////
//	pdc.MoveTo(0,393);
//	pdc.LineTo(1024,393);
	////////////////////////////////////////
//	pdc.SelectObject(op);
//	pen.DeleteObject();

	///////////////////////////
	path = _T("");
	height = h>786?786:h;
	width = w>1024?1024:w;
	oldheight = h>786?786:h;
	oldwidth = w>1024?1024:w;
	for(i = 0;i<1024;i++){
		for(j = 0;j<786;j++){
			pix[0][i][j] = 255;
			pix[1][i][j] = 255;
			pix[2][i][j] = 255;
			bfpix[0][i][j] = 255;
			bfpix[1][i][j] = 255;
			bfpix[2][i][j] = 255;
			mpix[0][0][i][j] = 255;
			mpix[0][1][i][j] = 255;
			mpix[0][2][i][j] = 255;
			mpix[1][0][i][j] = 255;
			mpix[1][1][i][j] = 255;
			mpix[1][2][i][j] = 255;
		}
	}
	BeiFen();
	undo = 0;
	redo = 2;
}


void CBmpDat::Open(CString p){
	/////读取位图文件
	int i,j;
	HBITMAP bitmap;
	CFile file;
	if(file.Open(p,CFile::modeRead|CFile::shareDenyNone)){//打开文件
		j = width;
		BITMAPINFO bmpinfo;
		BITMAPFILEHEADER bmphead;
		file.Read(&bmphead,sizeof(BITMAPFILEHEADER));
		file.Read(&bmpinfo,sizeof(BITMAPINFO));
		width = bmpinfo.bmiHeader.biWidth;
		height = bmpinfo.bmiHeader.biHeight;
		oldwidth = width;
		oldheight = height;
		file.Close();
	}
	else{
		AfxMessageBox("打开文件失败!");
		return;
	}
	bitmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(),p,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
	ASSERT(bitmap);

	New(width,height);
	CDC mdc;
	mdc.CreateCompatibleDC(&pdc);
	mdc.SelectObject(bitmap);
	pdc.BitBlt(0,0,width,height,&mdc,0,0,SRCCOPY);

	path = p;
	COLORREF clr;
	CString str;
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			clr = pdc.GetPixel(i,j);
			pix[0][i][j] = GetRValue(clr);
			pix[1][i][j] = GetGValue(clr);
			pix[2][i][j] = GetBValue(clr);
			bfpix[0][i][j] = pix[0][i][j];
			bfpix[1][i][j] = pix[1][i][j];
			bfpix[2][i][j] = pix[2][i][j];
		}
		str.Format("已完成%d%%",i*100/width);
		mmcsb->SetWindowText(str);
	}
	BeiFen();
	mmcsb->SetWindowText("就绪");
	undo = 0;
	redo = 2;
}

//////////////////////边缘提取///////////////////////////////
void CBmpDat::BY(){
	BeiFen();
	HDTX(0);
	CString str;
	int i,j;
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			pix[0][i][j] = BY(0,i,j);
			pix[1][i][j] = BY(1,i,j);
			pix[2][i][j] = BY(2,i,j);
			pdc.SetPixel(i,j,RGB(pix[0][i][j],pix[1][i][j],pix[2][i][j]));
		}
		str.Format("已完成%d%%",i*100/width);
		mmcsb->SetWindowText(str);
	}
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			bfpix[0][i][j] = pix[0][i][j];
			bfpix[1][i][j] = pix[1][i][j];
			bfpix[2][i][j] = pix[2][i][j];
		}
	}
	bfdc.BitBlt(0,0,width,height,&pdc,0,0,SRCCOPY);
	mmcsb->SetWindowText("就绪");
}
BYTE CBmpDat::BY(int i,int x,int y){
	int t1 = 0,t2 = 0,t3 = 0,t4 = 0,t;
	if(x>0){
		t1 = bfpix[i][x-1][y]-bfpix[i][x][y];
		if(t1<0){t1 = -t1;}
		if(y>0){
			t2 = bfpix[i][x-1][y-1]-bfpix[i][x][y];
			if(t2<0){t2 = -t2;}
		}
	}
	if(x<width-1){
		t3 = bfpix[i][x+1][y]-pix[i][x][y];
		if(t3<0){t3 = -t3;}
		if(y<height-1){
			t4 = bfpix[i][x+1][y+1]-bfpix[i][x][y];
			if(t4<0){t4 = -t4;}
		}
	}
	t = t1+t2+t3+t4;
	return t>255?255:t;
}

///////////////////////////浮雕/////////////////////////////////
void CBmpDat::FD(){
	BeiFen();
	HDTX(0);
	CString str;
	int i,j;
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			pix[0][i][j] = FD(0,i,j);
			pix[1][i][j] = FD(1,i,j);
			pix[2][i][j] = FD(2,i,j);
			pdc.SetPixel(i,j,RGB(pix[0][i][j],pix[1][i][j],pix[2][i][j]));
		}
		str.Format("已完成%d%%",i*100/width);
		mmcsb->SetWindowText(str);
	}
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			bfpix[0][i][j] = pix[0][i][j];
			bfpix[1][i][j] = pix[1][i][j];
			bfpix[2][i][j] = pix[2][i][j];
		}
	}
	bfdc.BitBlt(0,0,width,height,&pdc,0,0,SRCCOPY);
	mmcsb->SetWindowText("就绪");
}
BYTE CBmpDat::FD(int i,int x,int y){
	int t = 0;
	if(x<width-2){
		if(y<height){
			t = bfpix[i][x][y] - bfpix[i][x+2][y+1] + 125;
		}
		else{
			t = bfpix[i][x][y-1] - bfpix[i][x+2][y] + 125;
		}
	}
	else if(x<width-1){
		if(y<height){
			t = bfpix[i][x][y] - bfpix[i][x+1][y+1] + 125;
		}else{
			t = bfpix[i][x][y-1] - bfpix[i][x+1][y] + 125;
		}
	}
	else{
		if(y<height){
			t = bfpix[i][x-1][y] - bfpix[i][x][y+1] + 125;
		}else{
			t = bfpix[i][x-1][y-1] - bfpix[i][x][y] + 125;
		}
	}
	if(t<0){t = 0;}
	if(t>255){t = 255;}
	return t;
}


//////////////////////////去噪////////////////////////////
int Mid(int n,int c[]){
	int m = n / 2;
	for(int i = 0;i<m+1;i++){
		for(int j = 0;j<n-i-1;j++){
			if(c[j]>c[j+1]){
				int t = c[j];c[j] = c[j+1];c[j+1] = t;
			}
		}
	}
	return c[m];
}
BYTE CBmpDat::LB(int i,int x,int y,int z){
	int tn[9];
	int n = 0;
	if(x>0){
		if(y>0){
			tn[n++] = bfpix[i][x-1][y-1];
		}
		tn[n++] = bfpix[i][x-1][y];
		if(y<height-1){
			tn[n++] = bfpix[i][x-1][y+1];
		}
	}
	if(y>0){
		tn[n++] = bfpix[i][x][y-1];
	}
	tn[n++] = bfpix[i][x][y];
	if(y<height-1){
		tn[n++] = bfpix[i][x][y+1];
	}
	if(x<width-1){
		if(y>0){
			tn[n++] = bfpix[i][x+1][y-1];
		}
		tn[n++] = bfpix[i][x+1][y];
		if(y<height-1){
			tn[n++] = bfpix[i][x+1][y+1];
		}
	}
	int t1,t2;
	t1 = Mid(n,tn);
	t2 = t1 - bfpix[i][x][y];
	if(t2<-z||t2>z){
		return t1;
	}
	else{
		return bfpix[i][x][y];
	}
}
///////////////////模糊////////////////////////
BYTE CBmpDat::MH(int i,int x,int y){
	int temp = 2*(int)(bfpix[i][x][y]);
	int n = 2;
	if(x>0){
		if(y>0){
			temp += (int)bfpix[i][x-1][y-1];
			n++;
		}

⌨️ 快捷键说明

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