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

📄 iplabdoc.cpp

📁 最小二乘法(least squares analysis)是一种 数学 优化 技术
💻 CPP
字号:
// IPLabDoc.cpp : implementation of the CIPLabDoc class
//

#include "stdafx.h"
#include "IPLab.h"
#include "IPLabDoc.h"

#include "FastHitTest.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CIPLabDoc

IMPLEMENT_DYNCREATE(CIPLabDoc, CDocument)

BEGIN_MESSAGE_MAP(CIPLabDoc, CDocument)
	//{{AFX_MSG_MAP(CIPLabDoc)
	ON_COMMAND(ID_LEASTSQUARESFITTING, OnLeastsquaresfitting)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIPLabDoc construction/destruction

CIPLabDoc::CIPLabDoc()
{
	pPicture = NULL;
	m_Zoom = (float)0.15;
}

CIPLabDoc::~CIPLabDoc()
{
	if (pPicture) {
		delete pPicture;
	}
}

BOOL CIPLabDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return FALSE;
}



/////////////////////////////////////////////////////////////////////////////
// CIPLabDoc serialization

void CIPLabDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CIPLabDoc diagnostics

#ifdef _DEBUG
void CIPLabDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CIPLabDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CIPLabDoc commands

BOOL CIPLabDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	CString s = lpszPathName;
	pPicture = new Bitmap(s.AllocSysString());
	if (!pPicture) {
		return FALSE;
	}
	
	return TRUE;
}

void CIPLabDoc::Draw(CDC* pDC)
{
	Graphics g(pDC->GetSafeHdc());
	g.DrawImage(pPicture, Rect(0,0,(int)(m_Zoom*(float)pPicture->GetWidth()),(int)(m_Zoom*(float)pPicture->GetHeight())),
		0,0,
		pPicture->GetWidth(),pPicture->GetHeight(),
		UnitPixel,
		NULL);
}

//////////////////////////////////////////////////////////////////////////
//Image Processing Function

void CIPLabDoc::IPFuncInv()
{
	if (!pPicture) {
		return;
	}

	BYTE inv[256*3];
	int i;
	for(i=0;i<256;i++)
	{
		inv[i+256*0]=255-i;	//b
		inv[i+256*1]=255-i;	//g
		inv[i+256*2]=255-i;	//r
	}

	if (1) {  
		//汇编方式
		HistogramAdjust(inv,inv,inv);
	}
	else
	{
	//c方式
	/*
	BitmapData bitmapData;
	Rect rect(0, 0, pPicture->GetWidth(), pPicture->GetHeight());//900,900);
	
	Status status = pPicture->LockBits(
		&rect,
		ImageLockModeRead | ImageLockModeWrite,
		PixelFormat32bppARGB,
		&bitmapData);
	
	  
	  if (status != Ok ) {
	  return;
	  }

	unsigned char* pixels = (unsigned char*)bitmapData.Scan0;
	int x,y;
	int offx = bitmapData.Stride - bitmapData.Width*4;
	BYTE* pHistogramProjection = inv;
	
	for(y=rect.GetTop();y<rect.GetBottom();y++)
	{
		for(x=rect.GetLeft();x<rect.GetRight();x++)
		{			
			//b
			*pixels = pHistogramProjection[*pixels];
			pixels++;
			pHistogramProjection += 256;
			
			//g
			*pixels = pHistogramProjection[*pixels];
			pixels++;
			pHistogramProjection += 256;
			
			//r
			*pixels = pHistogramProjection[*pixels];
			pixels++;
			pHistogramProjection -= 256*2;
			
			//a
			pixels++;
		}
		pixels += offx;
	}
	pPicture->UnlockBits(&bitmapData);
	*/

	}
	UpdateAllViews(NULL);
	
}

void CIPLabDoc::IPFuncInvInRegion(GraphicsPath* pWorkingRegion)
{
	if (!pPicture || !pWorkingRegion) {
		return;
	}
	
	BitmapData bitmapData;
	Rect imageRect(0, 0, pPicture->GetWidth(), pPicture->GetHeight());//900,900);
	Rect rect;

	pWorkingRegion->GetBounds(&rect);
	rect.Intersect(imageRect);
	
	BYTE inv[256*3];
	int i;
	for(i=0;i<256;i++)
	{
		inv[i+256*0]=255-i;	//b
		inv[i+256*1]=255-i;	//g
		inv[i+256*2]=255-i;	//r
	}
	
	Status status = pPicture->LockBits(
		&rect,
		ImageLockModeRead | ImageLockModeWrite,
		PixelFormat24bppRGB,
		&bitmapData);
	
	if (status != Ok ) {
		return;
	}
	unsigned char* pixels = (unsigned char*)bitmapData.Scan0;
	int x,y;
	int offx = bitmapData.Stride - bitmapData.Width*3;
	BYTE* pHistogramProjection = inv;
	
	Region region(pWorkingRegion);
	
	for(y=rect.GetTop();y<rect.GetBottom();y++)
	{
		for(x=rect.GetLeft();x<rect.GetRight();x++)
		{
			if (region.IsVisible(x,y)) {
				
				//b
				*pixels = pHistogramProjection[*pixels];
				pixels++;
				pHistogramProjection += 256;

				//g
				*pixels = pHistogramProjection[*pixels];
				pixels++;
				pHistogramProjection += 256;
				
				//r
				*pixels = pHistogramProjection[*pixels];
				pixels++;
				pHistogramProjection -= 256*2;
				
			}
			else
			{
				pixels += 3;
			}
		}
		pixels += offx;
	}
	pPicture->UnlockBits(&bitmapData);
	
	UpdateAllViews(NULL);
}

void CIPLabDoc::IPFuncInvInRegion_F(GraphicsPath* pWorkingRegion)
{
	if (!pPicture || !pWorkingRegion) {
		return;
	}
	
	BitmapData bitmapData;
	Rect imageRect(0, 0, pPicture->GetWidth(), pPicture->GetHeight());//900,900);
	Rect rect;
	
	pWorkingRegion->GetBounds(&rect);
	rect.Intersect(imageRect);
	
	BYTE inv[256*3];
	int i;
	for(i=0;i<256;i++)
	{
		inv[i+256*0]=255-i;	//b
		inv[i+256*1]=255-i;	//g
		inv[i+256*2]=255-i;	//r
	}
	
	Status status = pPicture->LockBits(
		&rect,
		ImageLockModeRead | ImageLockModeWrite,
		PixelFormat24bppRGB,
		&bitmapData);
	
	if (status != Ok ) {
		return;
	}
	unsigned char* pixels = (unsigned char*)bitmapData.Scan0;
	int x,y;
	int offx = bitmapData.Stride - bitmapData.Width*3;
	BYTE* pHistogramProjection = inv;
	
	FastHitTest fHitTest(pWorkingRegion);
	
	for(y=rect.GetTop();y<rect.GetBottom();y++)
	{
		for(x=rect.GetLeft();x<rect.GetRight();x++)
		{
			if (fHitTest.IsVisible(x,y)) {
				
				//b
				*pixels = pHistogramProjection[*pixels];
				pixels++;
				pHistogramProjection += 256;
				
				//g
				*pixels = pHistogramProjection[*pixels];
				pixels++;
				pHistogramProjection += 256;
				
				//r
				*pixels = pHistogramProjection[*pixels];
				pixels++;
				pHistogramProjection -= 256*2;
			}
			else
			{
				pixels += 3;
			}
		}
		pixels += offx;
	}
	pPicture->UnlockBits(&bitmapData);
	
	UpdateAllViews(NULL);
}

void CIPLabDoc::IPFuncInvInRegion_O(CRgn* pRgn)
{
	if (!pPicture || !pRgn) {
		return;
	}
	
	BitmapData bitmapData;
	Rect imageRect(0, 0, pPicture->GetWidth(), pPicture->GetHeight());//900,900);
	Rect rect;
	CRect tRect;
	
	pRgn->GetRgnBox(&tRect);
	rect.X = tRect.left;
	rect.Y = tRect.top;
	rect.Width = tRect.Width();
	rect.Height = tRect.Height();
	rect.Intersect(imageRect);
	
	BYTE inv[256*3];
	int i;
	for(i=0;i<256;i++)
	{
		inv[i+256*0]=255-i;	//b
		inv[i+256*1]=255-i;	//g
		inv[i+256*2]=255-i;	//r
	}
	
	Status status = pPicture->LockBits(
		&rect,
		ImageLockModeRead | ImageLockModeWrite,
		PixelFormat24bppRGB,
		&bitmapData);
	
	if (status != Ok ) {
		return;
	}
	unsigned char* pixels = (unsigned char*)bitmapData.Scan0;
	int x,y;
	int offx = bitmapData.Stride - bitmapData.Width*3;
	BYTE* pHistogramProjection = inv;
	
	for(y=rect.GetTop();y<rect.GetBottom();y++)
	{
		for(x=rect.GetLeft();x<rect.GetRight();x++)
		{
			if (pRgn->PtInRegion(x,y)) {
				
				//b
				*pixels = pHistogramProjection[*pixels];
				pixels++;
				pHistogramProjection += 256;
				
				//g
				*pixels = pHistogramProjection[*pixels];
				pixels++;
				pHistogramProjection += 256;
				
				//r
				*pixels = pHistogramProjection[*pixels];
				pixels++;
				pHistogramProjection -= 256*2;
			}
			else
			{
				pixels += 3;
			}
		}
		pixels += offx;
	}

	pPicture->UnlockBits(&bitmapData);
	
	UpdateAllViews(NULL);
}

void CIPLabDoc::HistogramAdjust(BYTE R[256],BYTE G[256],BYTE B[256])
{
	Bitmap* tWorkingImage = this->GetBitmap();
	if (tWorkingImage) {
		BitmapData bmData;
		Rect tImageRect(0,0,tWorkingImage->GetWidth(),tWorkingImage->GetHeight());
		Status status = tWorkingImage->LockBits(&tImageRect, 
			ImageLockModeRead | ImageLockModeWrite, 
			PixelFormat24bppRGB,
			&bmData);

		if (status != Ok ) {
			return;
		}
		
		int _pixels = (int)bmData.Scan0;
		int _strideoff = bmData.Stride-3*bmData.Width;
		unsigned short _letf=tImageRect.GetLeft();
		unsigned short _top=tImageRect.GetTop();
		unsigned short _right= tImageRect.GetRight();
		unsigned short _bottom=tImageRect.GetBottom();
		BYTE _proj[256*3];
		
		int i;
		for(i=0;i<256;i++)
		{
			_proj[256*2+i] = R[i];
			_proj[256*1+i] = G[i];
			_proj[256*0+i] = B[i];
		}
		
		/*
		int x,y;
		BYTE* _p_proj = _proj;
		for(y=_top;y<_bottom;y++)
		{
			for(x=_letf;x<_right;x++)
			{
				*_pixels = _proj[*pixels];//b
				_pixels++;
				_p_proj += 256;

				*_pixels = _proj[*pixels];//g
				_pixels++;
				_p_proj += 256;

				*_pixels = _proj[*pixels];//r
				_pixels++;
				_p_proj -= 2*256;
			}
			_pixels += _strideoff;
		}
		*/	
		__asm{
			xor ecx,ecx;
			lea esi,_proj;
			mov edx, _pixels;
			mov di,	_letf;
			mov bx, _right;
			BSWAP ebx;
			mov bx, _bottom;
			mov ax, _top;		
	yloop:	
			cmp ax, bx;
			jge exityloop;
			BSWAP eax;
			BSWAP ebx;
			mov ax,di;
	xloop:	
			cmp ax,bx;
			jge exitxloop;
			
			;b;
			mov cl,[edx];
			mov cl,[esi+ecx];
			mov [edx],cl;
			inc edx;
			add esi,256;
			
			;g;
			mov cl,[edx];
			mov cl,[esi+ecx];
			mov [edx],cl;
			inc edx;
			add esi,256;
			
			;r;
			mov cl,[edx];
			mov cl,[esi+ecx];
			mov [edx],cl;
			inc edx;
			sub esi,256*2;
			
			inc ax;
			jmp xloop;
	exitxloop:
			add edx,_strideoff;
			BSWAP eax;
			BSWAP ebx;
			inc ax;
			jmp yloop;
	exityloop:
		}
		
		tWorkingImage->UnlockBits(&bmData);
	}
	return;
}

void CIPLabDoc::OnLeastsquaresfitting() 
{
	// TODO: Add your command handler code here
	
}

⌨️ 快捷键说明

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