击中击不中(vc实现)



void CISLSView::OnHMTA()
	{
		//击中击不中  
		//李立宗  lilizong@gmail.com
		//2012-8-23
		int structure1[3][3]={1,1,1,1,0,0,1,0,0};
		int structure2[3][3]={0,0,0,0,1,1,0,1,0};
		CImage myImage1Complement,myImage1Copy,hitImage,missImage;
		erosionBin(structure1);
		//将击中的结果保存到hitImage中
		imageCopy(hitImage,myImage2);
		//求myImage1的补图像
		myImage1Complement.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0);
		int maxY = myImage1.GetHeight();
		int maxX=myImage1.GetWidth();
		byte* pRealData1;
		byte* pRealDataTemp;
		pRealData1=(byte*)myImage1.GetBits();
		pRealDataTemp=(byte*)myImage1Complement.GetBits();
		int pit1=myImage1.GetPitch();
		int pitTemp=myImage1Complement.GetPitch();
		int bitCount1=myImage1.GetBPP()/8;
		int bitCountTemp=myImage1Complement.GetBPP()/8;
		int temp,tempR,tempG,tempB;
		for (int y=0; y<maxY; y++) {
			for (int x=0; x<maxX; x++) {
				temp=*(pRealData1+pit1*(y)+(x)*bitCount1);
				if(bitCount1==3)
				{
					tempR=*(pRealData1+pit1*(y)+(x)*bitCount1);
					tempG=*(pRealData1+pit1*(y)+(x)*bitCount1+1);
					tempB=*(pRealData1+pit1*(y)+(x)*bitCount1+2);
					temp=(int)(tempR*0.49+tempG*0.31+tempB*0.2+0.5);
					//temp=(int)((tempR+tempG+tempB)/3);
				}
				*(pRealDataTemp+pitTemp*(y)+(x)*bitCountTemp)=255-temp;
				*(pRealDataTemp+pitTemp*(y)+(x)*bitCountTemp+1)=255-temp;
				*(pRealDataTemp+pitTemp*(y)+(x)*bitCountTemp+2)=255-temp;

			}
		}
		//将myImage1进行备份,以使运算结束后能正常显示初始myImage1
		imageCopy(myImage1Copy,myImage1);
		//将temp复制回myImage1,以便进行下一步的计算
		imageCopy(myImage1,myImage1Complement);
		erosionBin(structure2);
		//将计算结果保存到missImage
		imageCopy(missImage,myImage2);
		byte* pRealDataHit;
		byte* pRealDataMiss;
		byte* pRealData2;
		pRealDataHit=(byte*)hitImage.GetBits();
		pRealDataMiss=(byte*)missImage.GetBits();
		pRealData2=(byte*)myImage2.GetBits();
		int pitHit=hitImage.GetPitch();
		int pitMiss=missImage.GetPitch();
		int pit2=myImage2.GetPitch();
		int bitCountHit=hitImage.GetBPP()/8;
		int bitCountMiss=missImage.GetBPP()/8;
		int bitCount2=myImage2.GetBPP()/8;
		for (int y=0; y<maxY; y++) {
			for (int x=0; x<maxX; x++) {
				if((*(pRealDataHit+pitHit*(y)+(x)*bitCountHit)==255) &&
					(*(pRealDataMiss+pitMiss*(y)+(x)*bitCountMiss)==255))
				{
					*(pRealData2+pit2*(y)+(x)*bitCount2)=255;
					*(pRealData2+pit2*(y)+(x)*bitCount2+1)=255;
					*(pRealData2+pit2*(y)+(x)*bitCount2+2)=255;
				}
				else
				{
					*(pRealData2+pit2*(y)+(x)*bitCount2)=0;
					*(pRealData2+pit2*(y)+(x)*bitCount2+1)=0;
					*(pRealData2+pit2*(y)+(x)*bitCount2+2)=0;
				}
			}
		}
		imageCopy(myImage1,myImage1Copy);
		Invalidate();
	}

用到的函数imageCopy如下:

void CISLSView::imageCopy(CImage &destImg, CImage &srcImg)
	{
		int width = srcImg.GetWidth();
		int pitch = srcImg.GetPitch();
		int height = srcImg.GetHeight();
		int bytesPerPixel = srcImg.GetBPP() / 8;

		if (!destImg.IsNull())
		{
			destImg.Destroy();
		}

		destImg.Create(width, height, bytesPerPixel * 8, 0);
		int _pitch = destImg.GetPitch();

		BYTE *pDestD = (BYTE *)destImg.GetBits();
		BYTE *pSrcD = (BYTE *)srcImg.GetBits();

		for (int y=0; y<height; y++)
		{
			memcpy(pDestD + y * _pitch, pSrcD + y * pitch, abs(pitch));
		}

		if (srcImg.GetBPP() <= 8)
		{
			RGBQUAD  D_pal[256];
			srcImg.GetColorTable(0, 256, D_pal);
			destImg.SetColorTable(0, 256, D_pal);
		}
	}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

superdont

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值