命中击不中(vc实现)
击中击不中(vc实现)
用到的函数imageCopy如下:
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); } }