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

📄 bmpdat.cpp

📁 毕业设计程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		temp += int(bfpix[i][x-1][y]);
		n ++;
		if(y<height-1){
			temp += int(bfpix[i][x-1][y+1]);
			n++;
		}
	}
	if(y>0){
		temp += int(bfpix[i][x][y-1]);
		n ++;
	}
	if(y<height-1){
		temp += int(bfpix[i][x][y+1]);
		n ++;
	}
	if(x<width-1){
		if(y>0){
			temp += int(bfpix[i][x+1][y-1]);
			n ++;
		}
		temp += int(bfpix[i][x+1][y]);
		n ++;
		if(y<height-1){
			temp += int(bfpix[i][x+1][y+1]);
			n ++;
		}
	}
	return BYTE(temp/n);
}

//////////////////////////亮度变化//////////////////////////////
BYTE CBmpDat::LA(int i,int x,int y,int z,int c){//c==1增加,c==0减少
	int n = 255 - z;
	int t = bfpix[i][x][y];
	if(c){
		t = z + t*(255 - z)/255;
	}
	else{
		t = t*z/255;
	}
	return t;
}

///////////////////////////设置画笔/////////////////////
void CBmpDat::SetPen(COLORREF col){
	pecol = col;
	CPen * p, * op;
	p = new CPen;
	p->CreatePen(PS_SOLID,1,pecol);
	op = pdc.SelectObject(p);
	op->DeleteObject();
	pdc.SetTextColor(col);
}
///////////////////////////////////种子填充算法//////////////////////////////////////
void CBmpDat::TC(int x,int y,int m){
	BYTE ** sp;
	int i,j;
	/////////////作标记//////////////
	sp = new BYTE*[width];
	for(i = 0;i<width;i++){
		sp[i] = new BYTE[height];
		for(j = 0;j<height;j++){
			sp[i][j] = 1;
		}
	}
	////////////////////////////////
	COLORREF col = pecol;
	CPen pen,*open;
	if(m)
		col = bgcol;
	pen.CreatePen(PS_SOLID,1,col);
	open = pdc.SelectObject(&pen);
	
	int sx[100],sy[100],top = 0;//栈
	int tr,tg,tb;//待填充色,
	int fr,fg,fb;//填充色
	fr = GetRValue(col);
	fg = GetGValue(col);
	fb = GetBValue(col);
	tr = pix[0][x][y];
	tg = pix[1][x][y];
	tb = pix[2][x][y];
	if(tr == fr&&tg == fg&&tb == fb){//颜色相等则不用填充
		return;
	}

	///////////////////////////////
	for(i = x;i>=0&&tr==pix[0][i][y]&&tg==pix[1][i][y]&&tb==pix[2][i][y];i--);//找到最左端的点
	sx[top] = i+1;sy[top] = y;top++;
	int lx,ly;
	while(top>0){//栈非空时填充
		////////////////////出栈
		lx = sx[top - 1];
		ly = sy[top - 1];
		top --;
		///////////////////填充
		if(sp[lx][ly]){
			for(i = lx;i<width&&pix[0][i][ly]==tr&&pix[1][i][ly]==tg&&pix[2][i][ly]==tb;i++){
				pix[0][i][ly] = fr;
				pix[1][i][ly] = fg;
				pix[2][i][ly] = fb;
//				bfpix[0][i][ly] = fr;
//				bfpix[1][i][ly] = fg;
//				bfpix[2][i][ly] = fb;
				sp[i][ly] = 0;
			}
			pdc.MoveTo(lx,ly);
			pdc.LineTo(i,ly);
			//////////////////////////////////////上看
			for(j = i-1;ly>=0&&j>=lx;j--){
				if(pix[0][j][ly-1]==tr&&pix[1][j][ly-1]==tg&&pix[2][j][ly-1]==tb){
					for(;j>=0&&pix[0][j][ly-1]==tr&&pix[1][j][ly-1]==tg&&pix[2][j][ly-1]==tb;j--);
					sx[top] = j+1;
					sy[top] = ly-1;
					top ++;
				}
			}
			//////////////////////////////////////下看
			for(j = i-1;ly<height&&j>=lx;j--){
				if(pix[0][j][ly+1]==tr&&pix[1][j][ly+1]==tg&&pix[2][j][ly+1]==tb){
					for(;j>=0&&pix[0][j][ly+1]==tr&&pix[1][j][ly+1]==tg&&pix[2][j][ly+1]==tb;j--);
					sx[top] = j+1;
					sy[top] = ly+1;
					top ++;
				}
			}
		}
	}
//	bfdc.BitBlt(0,0,width,height,&pdc,0,0,SRCCOPY);
	pdc.SelectObject(open);
}
///////////////////////////以下是基本操作////////////////////////////////////////

////////////////////////////Bezier曲线///////////////////////////////
int End(float x1,float y1,float x2,float y2,float x3,float y3){
	if(x3-x2<1.0000 && x3-x2>-1.00 && y3-y2<1.0000 && y3-y2>-1.0){
		if(x1-x3<1.0000 && x1-x3>-1.0 && y1-y3<1.0 && y1-y3>-1.0){
			return 1;
		}
	}
	return 0;
}
////////////////////////
void CBmpDat::DrawBezier(float x[4],float y[4],COLORREF col){
	int rpix = GetRValue(col),gpix = GetGValue(col),bpix = GetBValue(col);
	float tx1,ty1;
	float tx2,ty2;
	float tx3,ty3;
	float tx4,ty4;
	float tx5,ty5;
	float tx6,ty6;
	tx1 = (x[0]+x[1])/2;	ty1 = (y[0]+y[1])/2;
	tx2 = (x[2]+x[1])/2;	ty2 = (y[2]+y[1])/2;
	tx3 = (x[2]+x[3])/2;	ty3 = (y[2]+y[3])/2;
	tx4 = (tx1+tx2)/2;		ty4 = (ty1+ty2)/2;
	tx5 = (tx3+tx2)/2;		ty5 = (ty3+ty2)/2;
	tx6 = (tx4+tx5)/2;		ty6 = (ty4+ty5)/2;
	int xx = int(tx6+0.5f),yy = int(ty6+0.5f);
	if(End(x[0],y[0],x[3],y[3],tx6,ty6)){
		if(pdc.GetPixel(xx+1,yy)==col&&pdc.GetPixel(xx,yy+1)==col){
			return;
		}
		if(pdc.GetPixel(xx-1,yy)==col&&pdc.GetPixel(xx,yy+1)==col){
			return;
		}
		if(pdc.GetPixel(xx+1,yy)==col&&pdc.GetPixel(xx,yy-1)==col){
			return;
		}
		if(pdc.GetPixel(xx-1,yy)==col&&pdc.GetPixel(xx,yy-1)==col){
			return;
		}
		if(xx>=0&&xx<width&&yy>=0&&yy<=height){
			pdc.SetPixel(xx,yy,col);
			pix[0][xx][yy] = rpix;
			pix[1][xx][yy] = gpix;
			pix[2][xx][yy] = bpix;
		}
	}
	else{
		float tx[4],ty[4];
		if(xx>=0&&xx<width&&yy>=0&&yy<=height){
			pdc.SetPixel(xx,yy,col);
			pix[0][xx][yy] = rpix;
			pix[1][xx][yy] = gpix;
			pix[2][xx][yy] = bpix;
		}
		tx[0] = x[0];tx[1] = tx1;tx[2] = tx4;tx[3] = tx6;
		ty[0] = y[0];ty[1] = ty1;ty[2] = ty4;ty[3] = ty6;
		DrawBezier(tx,ty,col);
		tx[0] = tx6;tx[1] = tx5;tx[2] = tx3;tx[3] = x[3];
		ty[0] = ty6;ty[1] = ty5;ty[2] = ty3;ty[3] = y[3];
		DrawBezier(tx,ty,col);
	}
}
void CBmpDat::Bezier(int sx[],int sy[],int m){
	COLORREF col;
	if(m){
		col = bgcol;
	}
	else{
		col = pecol;
	}
	float x[4],y[4];
	for(int i = 0;i<4;i++){
		x[i] = (float)sx[i];
		y[i] = (float)sy[i];
	}
	DrawBezier(x,y,col);
}
void CBmpDat::ReBezier(int sx[],int sy[],int m){
	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];
		}
	}
	Bezier(sx,sy,m);
}
//////////////////////////////////////////////////////////////////////////////
void CBmpDat::Line(int sx,int sy,int tx,int ty,int m){
	COLORREF col = pecol;
	if(m){
		col = bgcol;
	}
	if(tx>sx){
		if(ty>sy){
			if(ty-sy>tx-sx){
				Line(sy,sx,ty,tx,1,1,1,col);
			}
			else{
				Line(sx,sy,tx,ty,0,1,1,col);
			}
		}
		else{
			if(sy-ty>tx-sx){
				Line(-sy,sx,-ty,tx,1,-1,1,col);
			}
			else{
				Line(sx,-sy,tx,-ty,0,1,-1,col);
			}
		}
	}
	else{
		if(ty>sy){
			if(ty-sy>sx-tx){
				Line(sy,-sx,ty,-tx,1,1,-1,col);
			}
			else{
				Line(-sx,sy,-tx,ty,0,-1,1,col);
			}
		}
		else{
			if(sy-ty>sx-tx){
				Line(-sy,-sx,-ty,-tx,1,-1,-1,col);
			}
			else{
				Line(-sx,-sy,-tx,-ty,0,-1,-1,col);
			}
		}
	}
}

//////////////////////////中点画线算法////////////////////////////////
void CBmpDat::Line(int sx,int sy,int tx,int ty,int c,int dx,int dy,COLORREF col){
//	pdc->MoveTo(sx,sy);
//	pdc->LineTo(tx,ty);
	int tr,tg,tb;
	tr = GetRValue(col);
	tg = GetGValue(col);
	tb = GetBValue(col);
	int a,b,d1,d2,d,x,y;
	a = sy - ty;
	b = tx - sx;
	d = a+a+b;
	d1 = a+a;
	d2 = a+a+b+b;
	x = sx;
	y = sy;
	if(c){
		if(dy*y>=0&&dx*x>=0&&dx*x<height&&dy*y<width){
			pdc.SetPixel(dy*y,dx*x,col);
			pix[0][dy*y][dx*x] = tr;
			pix[1][dy*y][dx*x] = tg;
			pix[2][dy*y][dx*x] = tb;
		}
	}
	else{
		if(dy*y>=0&&dx*x>=0&&dx*x<width&&dy*y<height){
			pdc.SetPixel(dx*x,dy*y,col);
			pix[0][dx*x][dy*y] = tr;
			pix[1][dx*x][dy*y] = tg;
			pix[2][dx*x][dy*y] = tb;
		}
	}
	int ddd,ddd2;
	if(c){
		ddd2 = width;
		ddd = height;
	}
	else{
		ddd = width;
		ddd2 = height;
	}
	while(x<tx){
		if(d<0){
			x++;y++;
			d += d2;
		}
		else{
			x++;
			d += d1;
		}
		if(dx*x>=(ddd)||dy*y>=(ddd2)||dx*x<0||dy*y<0){
			break;
		}
		if(c){
			if(dy*y>=0&&dx*x>=0&&dx*x<ddd&&dy*y<ddd2){
				pdc.SetPixel(dy*y,dx*x,col);
				pix[0][dy*y][dx*x] = tr;
				pix[1][dy*y][dx*x] = tg;
				pix[2][dy*y][dx*x] = tb;
			}
		}
		else{
			if(dy*y>=0&&dx*x>=0&&dx*x<ddd&&dy*y<ddd2){
				pdc.SetPixel(dx*x,dy*y,col);
				pix[0][dx*x][dy*y] = tr;
				pix[1][dx*x][dy*y] = tg;
				pix[2][dx*x][dy*y] = tb;
			}
		}
	}
}

void CBmpDat::ReLine(int sx,int sy,int tx,int ty,int c,int m){
	pdc.BitBlt(0,0,width,height,&bfdc,0,0,SRCCOPY);
	if(c){
		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];
			}
		}
	}
	Line(sx,sy,tx,ty,m);
}

void CBmpDat::Darw(int x,int y,int m){
	int tr,tg,tb;
	COLORREF col = pecol;
	if(m)
		col = bgcol;
	tr = GetRValue(col);
	tg = GetGValue(col);
	tb = GetBValue(col);
	pix[0][x][y] = tr;
	pix[1][x][y] = tg;
	pix[2][x][y] = tb;
	bfpix[0][x][y] = tr;
	bfpix[1][x][y] = tg;
	bfpix[2][x][y] = tb;
	pdc.SetPixel(x,y,col);
	bfdc.SetPixel(x,y,col);
}

/////////////////////////////////////////////////
void CBmpDat::Clear(int x,int y){
	int i,j;
	int bi,ei,bj,ej;
	BYTE tr,tg,tb;
	tr = GetRValue(bgcol);
	tg = GetGValue(bgcol);
	tb = GetBValue(bgcol);
	CPen pen1,*op1;
//	CPen pen2,*op2;
	pen1.CreatePen(PS_SOLID,1,bgcol);
	op1 = pdc.SelectObject(&pen1);
//	pen2.CreatePen(PS_SOLID,1,bgcol);
//	op2 = bfdc.SelectObject(&pen2);
	bi = x-5<0?0:x-5;
	ei = x+6>width?width:x+6;
	bj = y-5<0?0:y-5;
	ej = y+6>height?height:y+6;
	for(i = bi;i<ei;i++){
		for(j = bj;j<ej;j++){
			pix[0][i][j] = tr;
			pix[1][i][j] = tg;
			pix[2][i][j] = tb;
//			bfpix[0][i][j] = tr;
//			bfpix[1][i][j] = tg;
//			bfpix[2][i][j] = tb;
		}
		pdc.MoveTo(i,bj);
		pdc.LineTo(i,ej);
//		bfdc.MoveTo(i,bj);
//		bfdc.LineTo(i,ej);
	}
	pdc.SelectObject(op1);
//	pdc.SelectObject(op2);
}

////////////////////////////////////////////////////////////
void CBmpDat::CZ(int cn,int a[],int cr,int cg,int cb){
	int i,j;
	CString str;
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			if(cr)
				pix[0][i][j] = XZ(0,i,j,cn,a);
			if(cg)
				pix[1][i][j] = XZ(1,i,j,cn,a);
			if(cb)
				pix[2][i][j] = XZ(2,i,j,cn,a);
			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);
	}
	mmcsb->SetWindowText("就绪");
	BeiFen();
}
BYTE CBmpDat::XZ(int i,int x,int y,int cn,int a[]){
	switch(cn){
//	case 0:return BY(i,x,y);//边缘
//	case 1:return FD(i,x,y);//浮雕
	case 2:return LB(i,x,y,a[0]);//滤波
	case 3:return MH(i,x,y);//模糊
	case 4:return LA(i,x,y,a[0],1);//亮度增加
	case 5:return LA(i,x,y,a[0],0);//亮度减少
	}
	return 0;
}
///////////////////////////////////////////////////////
void CBmpDat::DBD(){
	int i,j;
	int maxr = 0,maxg = 0,maxb = 0;
	int minr = 255,ming = 255,minb = 255;
	int maxp = 0,minp = 255;
	CString str;
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			if(pix[0][i][j]>maxr){
				maxr = pix[0][i][j];
			}
			if(pix[0][i][j]<minr){
				minr = pix[0][i][j];
			}
			if(pix[1][i][j]>maxg){
				maxg = pix[1][i][j];
			}
			if(pix[1][i][j]<ming){
				ming = pix[1][i][j];
			}
			if(pix[2][i][j]>maxb){
				maxb = pix[2][i][j];
			}
			if(pix[2][i][j]<minb){
				minb = pix[2][i][j];
			}//*/
		}
		str.Format("已完成%d%%",i*20/width);
		mmcsb->SetWindowText(str);
	}
	////////////
//	maxr = 125;
//	maxg = 125;
//	maxb = 125;
	////////////
	int dr = maxr - minr;
	int dg = maxg - ming;
	int db = maxb - minb;
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			if(dr)pix[0][i][j] = ((int)pix[0][i][j] - minr)*255/dr;
			if(dg)pix[1][i][j] = ((int)pix[1][i][j] - ming)*255/dg;
			if(db)pix[2][i][j] = ((int)pix[2][i][j] - minb)*255/db;
			pdc.SetPixel(i,j,RGB(pix[0][i][j],pix[1][i][j],pix[2][i][j]));
		}
		str.Format("已完成%d%%",i*80/width+20);
		mmcsb->SetWindowText(str);
	}
	mmcsb->SetWindowText("就绪");
	BeiFen();
}
//////////////////////////
void CBmpDat::SMXG(int bl,int bw,int dx,int dy){
	bl++;
	int cou = 0;
	int tot = width*height;
	int i,j,k;
	int len = 0,ln;
	HDTX(0);
	srand(time(NULL));
	CString str;
	COLORREF tcl;
	if(dy){
		for(i = 0;i<width;i++){
			for(j = i,k = 0;j<width&&j>=0&&k<height&&k>=0;j+=dx,k+=dy){
				if(len == 0){
					len = rand()%bl+1;
					if(bw ==0){
						ln = 0;
					}
					else if(bw == 100){
						ln = 256;
					}
					else if(bw<11){
						ln = rand()%(bw*10);
					}
					else if(bw<95){
						ln = rand()%110+bw-10;
					}
					else{
						ln = rand()%100+(bw+14*(bw-95));
					}
				}
				tcl = SMXG(j,k,ln);
				pix[0][j][k] = GetRValue(tcl);
				pix[1][j][k] = GetGValue(tcl);
				pix[2][j][k] = GetBValue(tcl);
				pdc.SetPixel(j,k,tcl);
				len --;
				cou++;
			}
			str.Format("已完成%d%%",cou*100/tot);
			mmcsb->SetWindowText(str);
		}
	}
	if(dx){
		for(i = 1;i<height;i++){
			if(dx*dy==-1){
				j = width-1;
			}
			else{
				j = 0;

⌨️ 快捷键说明

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