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

📄 bmpdat.cpp

📁 毕业设计程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			}
			for(k = i;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);
		}
	}
	mmcsb->SetWindowText("就绪");
	BeiFen();
}
COLORREF CBmpDat::SMXG(int x,int y,int ln){
	int t = bfpix[0][x][y],n = 1;
	if(x>0){
		if(y>0){
			t += bfpix[0][x-1][y-1];
			n++;
		}
		if(y<height-1){
			t += bfpix[0][x-1][y+1];
			n++;
		}
		t += bfpix[0][x-1][y];
		n++;
	}
	if(x<width-1){
		if(y>0){
			t += bfpix[0][x+1][y-1];
			n++;
		}
		if(y<height-1){
			t += bfpix[0][x+1][y+1];
			n++;
		}
		t += bfpix[0][x+1][y-1];
		n++;
	}
	return (t/n)>=(ln)?bgcol:pecol;
}
//////////////////灰度图像
void CBmpDat::HDTX(int s){
	int i,j;
	int temp;
	CString str;
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			temp = (30*int(pix[0][i][j]) + 59*int(pix[1][i][j]) + 11*int(pix[2][i][j]))/100;
			pix[0][i][j] = temp;
			pix[1][i][j] = temp;
			pix[2][i][j] = temp;
			bfpix[0][i][j] = temp;
			bfpix[1][i][j] = temp;
			bfpix[2][i][j] = temp;
			if(s){
				pdc.SetPixel(i,j,RGB(pix[0][i][j],pix[1][i][j],pix[2][i][j]));
			}
		}
		if(s){
			str.Format("已完成%d%%",i*100/width);
			mmcsb->SetWindowText(str);
		}
	}
	if(s){

		mmcsb->SetWindowText("就绪");
		BeiFen();
	}
}
//////////////////缩放///////////////////////////
void CBmpDat::Zoom(int w,int h){
	BeiFen();
	int i,j,oi;
	float temp;
	int f1,f2;
	CString str;
	////////////////////////////////////
	//宽度进行缩放
	for(j = 0;j<height;j++){
		bfpix[0][w-1][j] = pix[0][width-1][j];
		bfpix[1][w-1][j] = pix[1][width-1][j];
		bfpix[2][w-1][j] = pix[2][width-1][j];
//		pix[0][w-1][j] = bfpix[0][width-1][j];
//		pix[1][w-1][j] = bfpix[1][width-1][j];
//		pix[2][w-1][j] = bfpix[2][width-1][j];
//		pdc.SetPixel(w-1,j,RGB(pix[0][w-1][j],pix[1][w-1][j],pix[2][w-1][j]));
	}
	for(i = 0;i<w-1;i++){
		temp = float(i * (width-1)) / float(w-1);
		temp += 0.00001f;//结果校正
		oi = int(temp);
		f2 = int(temp*100) - oi*100;
		f1 = 100 - f2;
		for(j = 0;j<height;j++){
			bfpix[0][i][j] = (pix[0][oi][j]*f1 + pix[0][oi+1][j]*f2)/100;
			bfpix[1][i][j] = (pix[1][oi][j]*f1 + pix[1][oi+1][j]*f2)/100;
			bfpix[2][i][j] = (pix[2][oi][j]*f1 + pix[2][oi+1][j]*f2)/100;
//			pix[0][i][j] = (bfpix[0][oi][j]*f1 + bfpix[0][oi+1][j]*f2)/100;
//			pix[1][i][j] = (bfpix[1][oi][j]*f1 + bfpix[1][oi+1][j]*f2)/100;
//			pix[2][i][j] = (bfpix[2][oi][j]*f1 + bfpix[2][oi+1][j]*f2)/100;
//			pdc.SetPixel(i,j,RGB(pix[0][i][j],pix[1][i][j],pix[2][i][j]));
		}
	}
	////////////////////////////////////*/
	//高度进行缩放
	for(i = 0;i<w;i++){
		pix[0][i][h-1] = bfpix[0][i][height-1];
		pix[1][i][h-1] = bfpix[1][i][height-1];
		pix[2][i][h-1] = bfpix[2][i][height-1];
		pdc.SetPixel(i,h-1,RGB(pix[0][i][h-1],pix[1][i][h-1],pix[2][i][h-1]));
	}
	for(j = 0;j<h-1;j++){
		temp = float(j * (height-1)) / float(h-1);
		temp += 0.00001f;//结果校正
		oi = int(temp);
		f2 = int(temp*100) - oi*100;
		f1 = 100 - f2;
		for(i = 0;i<w;i++){
			pix[0][i][j] = (bfpix[0][i][oi]*f1 + bfpix[0][i][oi+1]*f2)/100;
			pix[1][i][j] = (bfpix[1][i][oi]*f1 + bfpix[1][i][oi+1]*f2)/100;
			pix[2][i][j] = (bfpix[2][i][oi]*f1 + bfpix[2][i][oi+1]*f2)/100;
			pdc.SetPixel(i,j,RGB(pix[0][i][j],pix[1][i][j],pix[2][i][j]));
		}
		str.Format("已完成%d%%",j*100/h);
		mmcsb->SetWindowText(str);
	}//*/
	for(i = 0;i<w;i++){
		for(j = 0;j<h;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,w,h,&pdc,0,0,SRCCOPY);
	oldwidth = width;
	width = w;
	oldheight = height;
	height = h;
	mmcsb->SetWindowText("就绪");
}
///////////////////平移/////////////////////
void CBmpDat::Move(int dx,int dy){
	int i,j;
	int tx,ty;
	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);

	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			tx = i - dx;
			ty = j - dy;
			if(tx>=width||tx<0||ty>=height||ty<0){
				pix[0][i][j] = 255;
				pix[1][i][j] = 255;
				pix[2][i][j] = 255;
			}
			else{
				pix[0][i][j] = bfpix[0][tx][ty];
				pix[1][i][j] = bfpix[1][tx][ty];
				pix[2][i][j] = bfpix[2][tx][ty];
			}
		}
	}
	pdc.BitBlt(dx,dy,width,height,&bfdc,0,0,SRCCOPY);
	BeiFen();
}
/////////////////////////////////
double Max(double n[]){
	double n1,n2;
	n1 = n[0]>n[1]?n[0]:n[1];
	n2 = n[2]>n[3]?n[2]:n[3];
	return n1>n2?n1:n2;
}
double Min(double n[]){
	double n1,n2;
	n1 = n[0]<n[1]?n[0]:n[1];
	n2 = n[2]<n[3]?n[2]:n[3];
	return n1<n2?n1:n2;
}
///////////////旋转//////////////
void CBmpDat::Turn(int de){
	BeiFen();
	double pdec = double(de) * 3.1415926 / 180.0;
	double sind = sin(pdec);
	double cosd = cos(pdec);
	double nx[4] = {0,0,0,0};
	double ny[4] = {0,0,0,0};
	nx[1] = width*cosd - height*sind;
	ny[1] = width*sind + height*cosd;
	nx[2] = -height*sind;
	ny[2] = height*cosd;
	nx[3] = width*cosd;
	ny[3] = width*sind;
	double maxy,miny,maxx,minx;
	maxy = Max(ny);
	maxx = Max(nx);
	miny = Min(ny);
	minx = Min(nx);
	int dx = int(maxx - minx + 0.5);	//新的宽度
	int dy = int(maxy - miny + 0.5);	//新的高度
	int mx = int(minx - 0.5);		//转换后应偏移的横坐标
	int my = int(miny - 0.5);		//转换后应偏移的纵坐标
	sind = -sind;

	int i,j;
	int fx1,fx2,fy1,fy2;
	int tx,ty,tp1[3],tp2[3],tp3[3],tp4[3];
	for(i = 0;i<dx;i++){
		for(j = 0;j<dy;j++){
			minx = (i + mx)*cosd - (j + my)*sind + 0.00001;
			miny = (i + mx)*sind + (j + my)*cosd + 0.00001;
			fx1 = int(minx);
			fy1 = int(miny);
			if(minx<0)
				fx1 --;
			if(miny<0)
				fy1--;
			fx2 = fx1 + 1;
			fy2 = fy1 + 1;
			tx = int(minx*100)-int(minx)*100;
			if(tx<0)
				tx = 100+tx;
			ty = int(miny*100)-int(miny)*100;
			if(ty<0)
				ty = 100+ty;

				if(fx1>=0&&fx1<width){
					if(fy1>=0&&fy1<height){
						tp1[0] = bfpix[0][fx1][fy1];
						tp1[1] = bfpix[1][fx1][fy1];
						tp1[2] = bfpix[2][fx1][fy1];
					}
					else{
						tp1[0] = 255;
						tp1[1] = 255;
						tp1[2] = 255;
					}
					if(fy2<height&&fy2>=0){
						tp2[0] = bfpix[0][fx1][fy2];
						tp2[1] = bfpix[1][fx1][fy2];
						tp2[2] = bfpix[2][fx1][fy2];
					}
					else{
						tp2[0] = 255;
						tp2[1] = 255;
						tp2[2] = 255;
					}
				}
				else{
					tp1[0] = 255;
					tp1[1] = 255;
					tp1[2] = 255;
					tp2[0] = 255;
					tp2[1] = 255;
					tp2[2] = 255;
				}
				if(fx2>=0&&fx2<width){
					if(fy1>=0&&fy1<height){
						tp3[0] = bfpix[0][fx2][fy1];
						tp3[1] = bfpix[1][fx2][fy1];
						tp3[2] = bfpix[2][fx2][fy1];
					}
					else {
						tp3[0] = 255;
						tp3[1] = 255;
						tp3[2] = 255;
					}
					if(fy2>=0&&fy2<height){
						tp4[0] = bfpix[0][fx2][fy2];
						tp4[1] = bfpix[1][fx2][fy2];
						tp4[2] = bfpix[2][fx2][fy2];
					}
					else{
						tp4[0] = 255;
						tp4[1] = 255;
						tp4[2] = 255;
					}
				}
				else{
					tp3[0] = 255;
					tp3[1] = 255;
					tp3[2] = 255;
					tp4[0] = 255;
					tp4[1] = 255;
					tp4[2] = 255;
				}
				pix[0][i][j] = ((100-ty)*(((100-tx)*tp1[0]+tx*tp3[0])/100)+ty*(((100-tx)*tp2[0]+tx*tp4[0])/100))/100;
				pix[1][i][j] = ((100-ty)*(((100-tx)*tp1[1]+tx*tp3[1])/100)+ty*(((100-tx)*tp2[1]+tx*tp4[1])/100))/100;
				pix[2][i][j] = ((100-ty)*(((100-tx)*tp1[2]+tx*tp3[2])/100)+ty*(((100-tx)*tp2[2]+tx*tp4[2])/100))/100;
				pdc.SetPixel(i,j,RGB(pix[0][i][j],pix[1][i][j],pix[2][i][j]));

		}
	}
	for(i = 0;i<dx;i++){
		for(j = 0;j<dy;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,dx,dy,&pdc,0,0,SRCCOPY);
	width = dx;
	height = dy;
}

/////////////////////////////////////////////
void CBmpDat::RH(){
	int i,j;
	CString str;
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			pix[0][i][j] = RH(0,i,j);
			pix[1][i][j] = RH(1,i,j);
			pix[2][i][j] = RH(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);
	}
	mmcsb->SetWindowText("就绪");
	BeiFen();
}
BYTE CBmpDat::RH(int i,int x,int y){
	int temp = 0;
	int count = 1;
	if(x>0){
		temp += int(bfpix[i][x-1][y]);
		count ++;
		if(y>0){
			temp += int(bfpix[i][x-1][y-1]);
			count ++;
		}
		if(y<height-1){
			temp += int(bfpix[i][x-1][y+1]);
			count ++;
		}
	}
	if(y>0){
		temp += int(bfpix[i][x][y-1]);
		count ++;
	}
	if(y<height-1){
		temp += int(bfpix[i][x][y+1]);
		count ++;
	}
	if(x<width){
		temp += int(bfpix[i][x+1][y]);
		count ++;
		if(y>0){
			temp += int(bfpix[i][x+1][y-1]);
			count ++;
		}
		if(y<height-1){
			temp += int(bfpix[i][x+1][y+1]);
			count ++;
		}
	}
	temp = ((count+3) * int(bfpix[i][x][y]) - temp)/4;
	return temp;
}
//////////////////////////////////////////////////////////
void CBmpDat::NOT(){
	int i,j;
	CString str;
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			pix[0][i][j] = 255-pix[0][i][j];
			pix[1][i][j] = 255-pix[1][i][j];
			pix[2][i][j] = 255-pix[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);
	}
	mmcsb->SetWindowText("就绪");
	BeiFen();
}
/////////////////////////////////////////////////////////

void CBmpDat::Copy(){
	int i,j;
	COLORREF clr;
	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);
		}
	}
}

////////////////////////////
void CBmpDat::TextTemp(int x,int y,CString s[],int n,int dy){
	pdc.BitBlt(0,0,width,height,&bfdc,0,0,SRCCOPY);
	for(int i = 0;i<n;i++){
		pdc.TextOut(x,y+i*dy,s[i]);
	}
}
void CBmpDat::TextOK(int x,int y,CString s[],int n,int dx[],int dy){
	pdc.BitBlt(0,0,width,height,&bfdc,0,0,SRCCOPY);
	int j,k;
	COLORREF col;
	mmcsb->SetWindowText("文字提交中,请等待...");
	for(int i = 0;i<n;i++){
		pdc.TextOut(x,y+i*dy,s[i]);
		dx[i] += x;
		for(j = x<0?0:x;j<width||j<dx[i];j++){
			for(k = y<0?0:y;k<height||k<y+i*dy;k++){
				col = pdc.GetPixel(j,k);
				pix[0][j][k] = GetRValue(col);
				pix[1][j][k] = GetGValue(col);
				pix[2][j][k] = GetBValue(col);
			}
		}
	}
	mmcsb->SetWindowText("就绪");
	BeiFen();
}

void CBmpDat::SetFont(LOGFONT & lpc){
	CFont * cf,*ocf;
	cf = new CFont;
	cf->CreateFontIndirect(&lpc);
	ocf = pdc.SelectObject(cf);
	ocf->DeleteObject();
}
void CBmpDat::GetFont(LOGFONT &lpc){
	pdc.GetCurrentFont()->GetLogFont(&lpc);
}
///////////////////////////
void CBmpDat::Size(int w,int h){
	BeiFen();
	int minw,maxw;
	int minh,maxh;
	if(w>width){
		maxw = w;
		minw = width;
	}
	else{
		maxw = width;
		minw = w;
	}
	if(h>height){
		maxh = h;
		minh = height;
	}
	else{
		minh = h;
		maxh = height;
	}
	int i,j;
	for(i = minw;i<maxw;i++){
		for(j = 0;j<minh;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;
		}
	}
	for(j = minh;j<maxh;j++){
		for(i = 0;i<maxw;i++){
			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;
		}
	}
	CDC mdc;
	mdc.CreateCompatibleDC(&pdc);
	CBitmap tb,*ob;
	tb.LoadBitmap(IDB_BBB);
	ob = mdc.SelectObject(&tb);
	pdc.BitBlt(minw,0,maxw-minw+1,maxh,&mdc,0,0,SRCCOPY);
	pdc.BitBlt(0,minh,maxw,maxh-minh+1,&mdc,0,0,SRCCOPY);
	bfdc.BitBlt(minw,0,maxw-minw+1,maxh,&mdc,0,0,SRCCOPY);
	bfdc.BitBlt(0,minh,maxw,maxh-minh+1,&mdc,0,0,SRCCOPY);
	mdc.SelectObject(ob);
	width = w;
	height = h;
}
///////////////////////////
void CBmpDat::TEST(int n){
	int i,j;
	COLORREF clr;
	CString str;
	HDTX(0);
	int x;int y;
	for(i = 0;i<width;i++){
		for(j = 0;j<height;j++){
			x = i/n*n,y = j/n*n;
			clr = (bfpix[0][x][y]>255-((i%n)*(i%n)+(j%n)*(j%n)+1)*245/((n-1)*(n-1)+1)+5)?bgcol:pecol;
			pdc.SetPixel(i,j,clr);
			pix[0][i][j] = GetRValue(clr);
			pix[1][i][j] = GetGValue(clr);
			pix[2][i][j] = GetBValue(clr);
		}
		str.Format("已完成%d%%",i*100/width);
		mmcsb->SetWindowText(str);
	}
	mmcsb->SetWindowText("就绪");
	BeiFen();
}

⌨️ 快捷键说明

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