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

📄 shiqian0197view.cpp

📁 这是一个用ppt做的软件工程实习有关的资料
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	// TODO: Add your command handler code here
		CShiqian0197Doc *pDoc=GetDocument();
	ASSERT_VALID(pDoc);
    
	CRotate  dlg;
    if(dlg.DoModal()==IDOK)
	{
	  UpdateData(false);
	}	

	HDIB hDIB=pDoc->GetHDIB();

/***************对图象的基本信息进行设置和声明****************/

	LPSTR lpDIB;                   //指向原图像的指针
	LPSTR lpDIBBits;                //指向原图像的指针
	LPSTR lpSrc;                    //指向原图像数据区图像各像素的指针

	lpDIB = (char * ) ::GlobalLock((HGLOBAL) hDIB);
	lpDIBBits = ::FindDIBBits(lpDIB);

	long lWidth = ::DIBWidth(lpDIB);
	long lLineBytes = WIDTHBYTES(lWidth * 8);
	long lHeight = ::DIBHeight(lpDIB);


	HDIB  hNewDIB;                        //旋转后位图句柄	
	LPSTR lpNewDIB;                       //指向旋转后图像的指针
	LPSTR lpNewDIBBits;                   //指向旋转后图像数据区的指针
	LPSTR lpDst;                          //指向旋转后图像各像素的指针
    
	long lNewWidth;                       //旋转后图像宽度                                   
	long lNewLineBytes;                   //旋转后图像每行字节数
	long lNewHeight;                      //旋转后图像高度

	LPBITMAPINFOHEADER lpinfo;            //指向BITMAPINFO结构的指针


/****************得到旋转后图象的宽和高***************/

	float fRotateAngle;//旋转角度
	float fSina,fCosa;//旋转角度的正弦和余弦
	
	float fSrcX1,fSrcY1,fSrcX2,fSrcY2,fSrcX3,fSrcY3,fSrcX4,fSrcY4;//原图像四个角点坐标,以图像中心为坐标系原点
    float fDstX1,fDstY1,fDstX2,fDstY2,fDstX3,fDstY3,fDstX4,fDstY4;//旋转后图像四个角点坐标,以图像中心为坐标系原点

	float f1,f2;
	float iRotateAngle=dlg.m_iRotate;

    fRotateAngle = iRotateAngle*PAI/180;
//	fRotateAngle = (float) RADIAN(iRotateAngle);             //将旋转角度从度转换到弧度
	fSina = (float) sin((double)fRotateAngle);
	fCosa = (float) cos((double)fRotateAngle);


    //计算原图四个角点坐标
	fSrcX1 = (float) (- (lWidth  - 1)/2);
	fSrcY1 = (float) (  (lHeight - 1)/2);
	fSrcX2 = (float) (  (lWidth  - 1)/2);
	fSrcY2 = (float) (  (lHeight - 1)/2);
	fSrcX3 = (float) (- (lWidth  - 1)/2);
	fSrcY3 = (float) (- (lHeight - 1)/2);
	fSrcX4 = (float) (  (lWidth  - 1)/2);
	fSrcY4 = (float) (- (lHeight - 1)/2);
    //计算旋转后四个角点坐标
	fDstX1 =  fCosa * fSrcX1 + fSina * fSrcY1;
	fDstY1 = -fSina * fSrcX1 + fCosa * fSrcY1;
	fDstX2 =  fCosa * fSrcX2 + fSina * fSrcY2;
	fDstY2 = -fSina * fSrcX2 + fCosa * fSrcY2;
	fDstX3 =  fCosa * fSrcX3 + fSina * fSrcY3;
	fDstY3 = -fSina * fSrcX3 + fCosa * fSrcY3;
	fDstX4 =  fCosa * fSrcX4 + fSina * fSrcY4;
	fDstY4 = -fSina * fSrcX4 + fCosa * fSrcY4;

	lNewWidth = (LONG) (max(fabs(fDstY4 - fDstY1),fabs(fDstY3 - fDstY2) ) + 0.5);
	lNewLineBytes = WIDTHBYTES(lNewWidth * 8);
	lNewHeight = (LONG) (max( fabs(fDstY4 - fDstY1), fabs(fDstY3 - fDstY2) ) + 0.5);

	f1 = (float) (-0.5 * (lNewWidth - 1) * fCosa - 0.5 * (lNewHeight - 1) * fSina
		+ 0.5 * (lWidth - 1));
	f2 = (float) ( 0.5 * (lNewWidth - 1) * fSina - 0.5 * (lNewHeight - 1) * fCosa
		+ 0.5 * (lHeight - 1));

/**********************对新的句柄进行操作********************/

	hNewDIB = (HDIB) ::GlobalAlloc(GHND,lNewLineBytes * lNewHeight + *(LPDWORD)lpDIB
		+ ::PaletteSize(lpDIB));                //为新图象申请内存块

	if(hDIB == NULL)                            //检验句柄
	{
		return ;
	}

	lpNewDIB = (char * ) ::GlobalLock((HGLOBAL) hNewDIB);            //锁定句柄
	memcpy(lpNewDIB,lpDIB, *(LPDWORD)lpDIB + ::PaletteSize(lpDIB));  //复制位图信息头和调色板
    lpNewDIBBits = ::FindDIBBits(lpNewDIB);                    //得到数据区的指针
    lpinfo = (LPBITMAPINFOHEADER)lpNewDIB;                     //初始化信息头

	lpinfo->biWidth = lNewWidth;                //更新位图中图像的高度和宽度
	lpinfo->biHeight = lNewHeight;              //针对图像每行进行操作
   	lpinfo->biSizeImage=lNewHeight*lNewWidth;


    //循环变量,像素在新位图中的坐标
	LONG i;
	LONG j;
    //循环变量,像素在原位图中的坐标
	LONG i0;
	LONG j0;
	for(i = 0; i < lNewHeight; i++)
	{
		//针对图像每列进行操作
		for(j = 0; j< lNewWidth; j++)
		{
			//指向新位图第i行,第j列像素的指针
			lpDst = (char *)lpNewDIBBits + lNewLineBytes * (lNewHeight - 1 - i) + j;
            //计算该像素在原位图中的坐标
			i0 = (LONG) (-((float) j) * fSina + ((float) i) * fCosa + f2 + 0.5);
			j0 = (LONG) ( ((float) j) * fCosa + ((float) i) * fSina + f1 + 0.5);
            //判断是否在原图范围内
			if((j0 >= 0) && (j0 < lWidth) && (i0 >= 0) && (i0 < lHeight))
			{
				lpSrc = (char *)lpDIBBits + lLineBytes * (lHeight - 1 - i0) + j0;
				*lpDst = *lpSrc;//复制像素
			}
			else
			{
				* ((unsigned char*)lpDst) = 255;//对于超出原图范围的像素置为255
			}
		}
	}
 /****************后续处理,使其显示****************/   
		pDoc->ReplaceHDIB(hNewDIB);			     // 替换DIB,同时释放旧DIB对象		
		pDoc->InitDIBData();				     // 更新DIB大小和调色板		
		pDoc->SetModifiedFlag(TRUE);		     // 设置脏标记		
		SetScrollSizes(MM_TEXT, pDoc->GetDocSize());// 重新设置滚动视图大小		
		pDoc->UpdateAllViews(NULL);			    // 更新视图

		::GlobalUnlock(pDoc->GetHDIB());
		EndWaitCursor();
}

void CShiqian0197View::OnTzMoravec() 
{
	// TODO: Add your command handler code here
	
}

void CShiqian0197View::OnTzForstner() 
{
	// TODO: Add your command handler code here
	
}

void CShiqian0197View::OnXingtaiTranstobinary() 
{
	// TODO: Add your command handler code here

    CShiqian0197Doc* pDoc = GetDocument();   
	ASSERT_VALID(pDoc);

	CYuzhiDlg  dlg;
    if(dlg.DoModal()==IDOK)
	{
	  UpdateData(false);
	}	

	HDIB hDIB=pDoc->GetHDIB();

	LPSTR lpDIB;                   //指向原图像的指针
	LPSTR lpDIBBits;                //指向原图像的指针
	LPSTR lpSrc;                    //指向原图像数据区图像各像素的指针

	lpDIB = (char * ) ::GlobalLock((HGLOBAL) hDIB);
	lpDIBBits = ::FindDIBBits(lpDIB);

	long lWidth = ::DIBWidth(lpDIB);
	long lLineBytes = WIDTHBYTES(lWidth * 8);
	long lHeight = ::DIBHeight(lpDIB);

    LPBITMAPINFOHEADER lpinfo;            //指向BITMAPINFO结构的指针
	
	LONG	i;              // 循环变量
	LONG	j;
    int     bThre=dlg.m_iYuZhi;

/***********************进行二值化************************/	

	for(i = 0; i < lHeight; i++)        // 每行
	{	
		for(j = 0; j < lWidth; j++)   	// 每列
		{
			// 指向DIB第i行,第j个象素的指针
			lpSrc = (char*)lpDIBBits + lLineBytes * (lHeight - 1 - i) + j;
			
			// 判断是否小于阈值
			if ((*lpSrc) < bThre)
			{
				// 直接赋值为0
				*lpSrc = 0;
			}
			else
			{
				// 直接赋值为255
				*lpSrc = 255;
			}
		}
	}

		pDoc->UpdateAllViews(NULL);	
	
}

void CShiqian0197View::OnXingtaiFushiShuiping() 
{
	// TODO: Add your command handler code here
		// TODO: Add your command handler code here
	CShiqian0197Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc); 

	HDIB hDIB=pDoc->GetHDIB();

	LPSTR lpDIB;                 
	LPSTR lpDIBBits;                            
	lpDIB = (char * ) ::GlobalLock((HGLOBAL) hDIB);
	lpDIBBits = ::FindDIBBits(lpDIB);

	long lWidth = ::DIBWidth(lpDIB);
	long lLineBytes = WIDTHBYTES(lWidth * 8);
	long lHeight = ::DIBHeight(lpDIB);


	HDIB  hNewDIBBits;                       
	LPSTR lpNewDIBBits;                                           
	hNewDIBBits = (HDIB)LocalAlloc(LHND, lLineBytes*lHeight);
	lpNewDIBBits = (char * )LocalLock(hNewDIBBits);

	memcpy(lpNewDIBBits, lpDIBBits, lLineBytes *lHeight); 

	int p,q;
	long  n;

	for( p=0;p<lHeight;p++)
		for( q=0;q<lWidth;q++)
		{
			if(q==0)
			{

			     if(
			    	  *(lpDIBBits+(lHeight-p-1)*lWidth+q)==0
//			    	||*(lpDIBBits+(lHeight-p)*lWidth+q)==0
//				    ||*(lpDIBBits+(lHeight-p-2)*lWidth+q)==0
				    ||*(lpDIBBits+(lHeight-p-1)*lWidth+q+1)==0
//				    ||*(lpDIBBits+(lHeight-p-1)*lWidth+q-1)==0
			    	)

				     n=0;
			      else 
				     n=255;				

		              *(lpNewDIBBits+(lHeight-p-1)*lWidth+q)=(unsigned char)n;
			}
			else if(q==lWidth-1)
			{
				if(
			    	  *(lpDIBBits+(lHeight-p-1)*lWidth+q)==0
//			    	||*(lpDIBBits+(lHeight-p)*lWidth+q)==0
//				    ||*(lpDIBBits+(lHeight-p-2)*lWidth+q)==0
//				    ||*(lpDIBBits+(lHeight-p-1)*lWidth+q+1)==0
				    ||*(lpDIBBits+(lHeight-p-1)*lWidth+q-1)==0
			    	)

				     n=0;
			      else 
				     n=255;				

		              *(lpNewDIBBits+(lHeight-p-1)*lWidth+q)=(unsigned char)n;
			}
			     else
				 {
					 if(
			    	  *(lpDIBBits+(lHeight-p-1)*lWidth+q)==0
//			    	||*(lpDIBBits+(lHeight-p)*lWidth+q)==0
//				    ||*(lpDIBBits+(lHeight-p-2)*lWidth+q)==0
				    ||*(lpDIBBits+(lHeight-p-1)*lWidth+q+1)==0
				    ||*(lpDIBBits+(lHeight-p-1)*lWidth+q-1)==0
			    	)

				     n=0;
			      else 
				     n=255;				

		              *(lpNewDIBBits+(lHeight-p-1)*lWidth+q)=(unsigned char)n;
				 }
		}

	memcpy(lpDIBBits, lpNewDIBBits, lLineBytes*lHeight);
	LocalUnlock(hNewDIBBits);
	LocalFree(hNewDIBBits);
    InvalidateRect(NULL,TRUE);	
}

void CShiqian0197View::OnXingtaiFushiChuizhi() 
{
	// TODO: Add your command handler code here
	CShiqian0197Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc); 

	HDIB hDIB=pDoc->GetHDIB();

	LPSTR lpDIB;                 
	LPSTR lpDIBBits;                            
	lpDIB = (char * ) ::GlobalLock((HGLOBAL) hDIB);
	lpDIBBits = ::FindDIBBits(lpDIB);

	long lWidth = ::DIBWidth(lpDIB);
	long lLineBytes = WIDTHBYTES(lWidth * 8);
	long lHeight = ::DIBHeight(lpDIB);


	HDIB  hNewDIBBits;                       
	LPSTR lpNewDIBBits;                                           
	hNewDIBBits = (HDIB)LocalAlloc(LHND, lLineBytes*lHeight);
	lpNewDIBBits = (char * )LocalLock(hNewDIBBits);

	memcpy(lpNewDIBBits, lpDIBBits, lLineBytes *lHeight); 

	int p,q;
	long  n;

	for( p=0;p<lHeight;p++)
		for( q=0;q<lWidth;q++)
		{
			if(p==0)
			{
               if(
			          *(lpDIBBits+(lHeight-p-1)*lWidth+q)==0
//				    ||*(lpDIBBits+(lHeight-p)*lWidth+q)==0
				    ||*(lpDIBBits+(lHeight-p-2)*lWidth+q)==0
//			        ||*(lpDIBBits+(lHeight-p-1)*lWidth+q+1)==0
//			        ||*(lpDIBBits+(lHeight-p-1)*lWidth+q-1)==0
			       	)

				      n=0;
		        	else 
				      n=255;				

		            *(lpNewDIBBits+(lHeight-p-1)*lWidth+q)=(unsigned char)n;
			}
			else if(p==lHeight)
			{
				if(
			          *(lpDIBBits+(lHeight-p-1)*lWidth+q)==0
				    ||*(lpDIBBits+(lHeight-p)*lWidth+q)==0
//				    ||*(lpDIBBits+(lHeight-p-2)*lWidth+q)==0
//			        ||*(lpDIBBits+(lHeight-p-1)*lWidth+q+1)==0
//			        ||*(lpDIBBits+(lHeight-p-1)*lWidth+q-1)==0
			       	)

				      n=0;
		        	else 
				      n=255;				

		            *(lpNewDIBBits+(lHeight-p-1)*lWidth+q)=(unsigned char)n;
			}
			    else
                {
			      if(
			          *(lpDIBBits+(lHeight-p-1)*lWidth+q)==0
				    ||*(lpDIBBits+(lHeight-p)*lWidth+q)==0
				    ||*(lpDIBBits+(lHeight-p-2)*lWidth+q)==0
//			        ||*(lpDIBBits+(lHeight-p-1)*lWidth+q+1)==0
//			        ||*(lpDIBBits+(lHeight-p-1)*lWidth+q-1)==0
			       	)

				      n=0;
		        	else 
				      n=255;				

		            *(lpNewDIBBits+(lHeight-p-1)*lWidth+q)=(unsigned char)n;
				}
		}

	memcpy(lpDIBBits, lpNewDIBBits, lLineBytes*lHeight);
	LocalUnlock(hNewDIBBits);
	LocalFree(hNewDIBBits);
    InvalidateRect(NULL,TRUE);		
}

void CShiqian0197View::OnXingtaiFushiQuanfangwei() 
{
	// TODO: Add your command handler code here
		CShiqian0197Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc); 

	HDIB hDIB=pDoc->GetHDIB();

	LPSTR lpDIB;                 
	LPSTR lpDIBBits;                            
	lpDIB = (char * ) ::GlobalLock((HGLOBAL) hDIB);
	lpDIBBits = ::FindDIBBits(lpDIB);

	long lWidth = ::DIBWidth(lpDIB);
	long lLineBytes = WIDTHBYTES(lWidth * 8);
	long lHeight = ::DIBHeight(lpDIB);


	HDIB  hNewDIBBits;                       
	LPSTR lpNewDIBBits;                                           
	hNewDIBBits = (HDIB)LocalAlloc(LHND, lLineBytes*lHeight);
	lpNewDIBBits = (char * )LocalLock(hNewDIBBits);

	memcpy(lpNewDIBBits, lpDIBBits, lLineBytes *lHeight); 

	int p,q;
	long  n;

	for( p=0;p<lHeight;p++)
		for( q=0;q<lWidth;q++)
		{
		  if(p==0&&q==0)
		  {
			 if(
				*(lpDIBBits+(lHeight-p-1)*lWidth+q)==0
//				||*(lpDIBBits+(lHeight-p)*lWidth+q)==0
				||*(lpDIBBits+(lHeight-p-2)*lWidth+q)==0
				||*(lpDIBBits+(lHeight-p-1)*lWidth+q+1)==0
//				||*(lpDIBBits+(lHeight-p-1)*lWidth+q-1)==0
		    	)

				n=0;
			else 
				n=255;				

		    *(lpNewDIBBits+(lHeight-p-1)*lWidth+q)=(unsigned char)n;
		  }
		  if(p>=1&&(p<=lHeight-2)&&q==0)
		  {
			  if(
				*(lpDIBBits+(lHeight-p-1)*lWidth+q)==0
				||*(lpDIBBits+(lHeight-p)*lWidth+q)==0
				||*(lpDIBBits+(lHeight-p-2)*lWidth+q)==0
				||*(lpDIBBits+(lHeight-p-1)*lWidth+q+1)==0
//				||*(lpDIBBits+(lHeight-p-1)*lWidth+q-1)==0
		    	)

				n=0;
			else 
				n=255;				

		    *(lpNewDIBBits+(lHeight-p-1)*lWidth+q)=(unsigned char)n;
		  }
		  if((p==lWidth-1)&&q==0)
		  {
			  if(
				*(lpDIBBits+(lHeight-p-1)*lWidth+q)==0
				||*(lpDIBBits+(lHeight-p)*lWidth+q)==0
//				||*(lpDIBBits+(lHeight-p-2)*lWidth+q)==0
				||*(lpDIBBits+(lHeight-p-1)*lWidth+q+1)==0
//				||*(lpDIBBits+(lHeight-p-1)*lWidth+q-1)==0
		    	)

				n=0;
			else 
				n=255;				

			*(lpNewDIBBits+(lHeight-p-1)*lWidth+q)=(unsigned char)n;
		  }
		  if((p==lWidth-1)&&q>=1&&(q<=lWidth-2))
		  {
			  if(
				*(lpDIBBits+(lHeight-p-1)*lWidth+q)==0
				||*(lpDIBBits+(lHeight-p)*lWidth+q)==0
//				||*(lpDIBBits+(lHeight-p-2)*lWidth+q)==0
				||*(lpDIBBits+(lHeight-p-1)*lWidth+q+1)==0
				||*(lpDIBBits+(lHeight-p-1)*lWidth+q-1)==0

⌨️ 快捷键说明

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