paint.cpp

来自「一个由Mike Gashler完成的机器学习方面的includes neural」· C++ 代码 · 共 1,283 行 · 第 1/3 页

CPP
1,283
字号
				for(x = l; x <= r; x++)
				{
					if((int)pImage->GetPixel(x, y) == nRegion)
						nCount++;
					nTot++;
				}
			}
			GAssert(nCount > 0, "something's wrong");
			pOutVector[OCR_HALF_VEC_SIZE + n] = (double)nCount /** w*/ / (nTot /** h*/);
		}

		// Find the best rotation
		double dBestScore = 0;
		double d;
		int nBest = -1;
		for(i = 0; i < 4; i++)
		{
			d = EvaluateOCRVector(pOutVector);
			if(d > dBestScore)
			{
				dBestScore = d;
				nBest = i;
			}
			RotateOCRVector(pOutVector);
		}
		GAssert(nBest >= 0, "something's wrong");
		for(i = 0; i < nBest; i++)
			RotateOCRVector(pOutVector);
	}


	void EvalRS(GImage* pImage, const char* szMaster, const char* szCand, int x, int y, GColor col)
	{
		// Load the master
		char szLine[256];
		FILE* pFile = fopen(szMaster, "r");
		GAssert(pFile, "Failed to open file");
		fgets(szLine, 256, pFile);
		int nLenMaster = atoi(szLine);
		double* pMaster = new double[nLenMaster];
		Holder<double*> hMaster(pMaster);
		fgets(szLine, 256, pFile);
		int nStartMaster = atoi(szLine);
		fgets(szLine, 256, pFile);
		int i;
		for(i = 0; i < nLenMaster; i++)
		{
			fgets(szLine, 256, pFile);
			pMaster[i] = atof(szLine);
			fgets(szLine, 256, pFile);
		}
		fclose(pFile);

		// Load the candidate
		pFile = fopen(szCand, "r");
		GAssert(pFile, "Failed to open file");
		fgets(szLine, 256, pFile);
		int nLenCand = atoi(szLine);
		double* pCand1 = new double[nLenCand];
		double* pCand2 = new double[nLenCand];
		Holder<double*> hCand1(pCand1);
		Holder<double*> hCand2(pCand2);
		fgets(szLine, 256, pFile);
		int nStartCand1 = atoi(szLine);
		fgets(szLine, 256, pFile);
		int nStartCand2 = atoi(szLine);
		for(i = 0; i < nLenCand; i++)
		{
			fgets(szLine, 256, pFile);
			pCand1[i] = atof(szLine);
			fgets(szLine, 256, pFile);
			pCand2[i] = atof(szLine);
		}
		fclose(pFile);

		// Measure the error
		double dError1 = 0;
		double dError2 = 0;
		double d;
		int j;
		for(i = 0; i < nLenMaster; i++)
		{
			j = i * nLenCand / nLenMaster;
			int indexMaster = (i + nStartMaster) % nLenMaster;
			int indexCand1 = (j + nStartCand1) % nLenCand;
			d = pMaster[indexMaster] - pCand1[indexCand1];
			d *= d;
			dError1 += d;
			
			int indexCand2 = (j + nStartCand2) % nLenCand;
			d = pMaster[indexMaster] - pCand2[indexCand2];
			d *= d;
			dError2 += d;
		}
		double dFinal = MIN(dError1, dError2) / nLenMaster;
		GRect r(x, y, 150, 28);
		sprintf(szLine, "%f", dFinal);
		pImage->DrawHardText(&r, szLine, col, 1);
	}


	virtual void OnSelectTextTab(GWidgetTextTab* pTab)
	{
		// Select the new tab
		m_pCurrentToolTab->SetSelected(false);
		m_pCurrentToolTab = pTab;
		m_pCurrentToolTab->SetSelected(true);

		// Get the new tool
		if(pTab == m_pTabPen)
			m_pController->SetCurrentTool(m_pToolPen);
		else if(pTab == m_pTabStretch)
			m_pController->SetCurrentTool(m_pToolStretch);
		else if(pTab == m_pTabSmartSelect)
			m_pController->SetCurrentTool(m_pToolSmartSelect);
		else if(pTab == m_pTabBorder)
			m_pController->SetCurrentTool(m_pToolBorder);
		else if(pTab == m_pTabBezier)
			m_pController->SetCurrentTool(m_pToolBezier);
		else
			GAssert(false, "unknown tool");
	}

	void SelectDefaultTool()
	{
		m_pController->SetCurrentTool(m_pToolPen);
	}
};


// ----------------------------------------------------------------------------

class PaintView : public ViewBase
{
friend class PaintController;
protected:
	GWidgetDialog** m_pTabDialogs;
	int m_nSelectedTab;
	PaintDialog* m_pMainDialog;
	GImage* m_pImage;

public:
	PaintView(PaintController* pController, GImage* pImage, GImage* pSelection);
	virtual ~PaintView();

	void OnSelectTab(int i);
	virtual void OnChar(char c);
	virtual void OnMouseDown(int nButton, int x, int y);
	virtual void OnMouseUp(int nButton, int x, int y);
	virtual bool OnMousePos(int x, int y);
	void SetImage(GImage* pImage) { m_pImage = pImage; }
	void OnLoadImage();
	void CleanCanvas();
	void RedrawCanvas();
	void ZoomIn();
	void ZoomOut();
	void SelectDefaultTool();
	void SetCurrentTool(PaintTool* pTool);

protected:
	virtual void Draw(SDL_Surface *pScreen);
};

PaintView::PaintView(PaintController* pController, GImage* pImage, GImage* pSelection)
: ViewBase()
{
	m_pTabDialogs = new GWidgetDialog*[PAINT_TAB_COUNT];
	m_pTabDialogs[0] = new TabOpen(pController, m_screenRect.w);
	m_pTabDialogs[1] = new TabSave(pController, m_screenRect.w);
	m_pTabDialogs[2] = new TabTools(pController, m_screenRect.w);
	m_nSelectedTab = 0;
	m_pMainDialog = new PaintDialog(pController, m_screenRect.w, m_screenRect.h - TOOL_AREA_SIZE, pImage, pSelection);
	m_pImage = NULL;
}

PaintView::~PaintView()
{
	int i;
	for(i = 0; i < PAINT_TAB_COUNT; i++)
		delete(m_pTabDialogs[i]);
	delete[] m_pTabDialogs;
	delete(m_pMainDialog);
}

/*virtual*/ void PaintView::Draw(SDL_Surface *pScreen)
{
	// Draw the tool tab
	GImage* pImage = m_pTabDialogs[m_nSelectedTab]->GetImage();
	BlitImage(pScreen, m_screenRect.x, m_screenRect.y, pImage);

	// Draw the canvas dialog
	pImage = m_pMainDialog->GetImage();
	BlitImage(pScreen, m_screenRect.x, m_screenRect.y + TOOL_AREA_SIZE, pImage);
}

void PaintView::OnSelectTab(int i)
{
	m_nSelectedTab = i;
}

void PaintView::OnChar(char c)
{
	m_pTabDialogs[m_nSelectedTab]->HandleChar(c);
}

void PaintView::OnMouseDown(int nButton, int x, int y)
{
	x -= m_screenRect.x;
	y -= m_screenRect.y;
	GWidgetAtomic* pWidget;
	if(y >= TOOL_AREA_SIZE)
	{
		pWidget = m_pMainDialog->FindAtomicWidget(x, y - TOOL_AREA_SIZE);
		m_pMainDialog->GrabWidget(pWidget, nButton, x, y - TOOL_AREA_SIZE);
	}
	else
	{
		pWidget = m_pTabDialogs[m_nSelectedTab]->FindAtomicWidget(x, y);
		m_pTabDialogs[m_nSelectedTab]->GrabWidget(pWidget, nButton, x, y);
	}
}

void PaintView::OnMouseUp(int nButton, int x, int y)
{
	m_pMainDialog->ReleaseWidget(nButton);
	m_pTabDialogs[m_nSelectedTab]->ReleaseWidget(nButton);
}

bool PaintView::OnMousePos(int x, int y)
{
	x -= m_screenRect.x;
	y -= m_screenRect.y;
	if(y >= TOOL_AREA_SIZE)
		return m_pMainDialog->HandleMousePos(x, y - TOOL_AREA_SIZE);
	else
		return m_pTabDialogs[m_nSelectedTab]->HandleMousePos(x, y);
}

void PaintView::OnLoadImage()
{
	m_pMainDialog->OnLoadImage();
}

void PaintView::CleanCanvas()
{
	m_pMainDialog->CleanCanvas();
}

void PaintView::RedrawCanvas()
{
	m_pMainDialog->RedrawCanvas();
}

void PaintView::ZoomIn()
{
	m_pMainDialog->ZoomIn();
}

void PaintView::ZoomOut()
{
	m_pMainDialog->ZoomOut();
}

void PaintView::SelectDefaultTool()
{
	((TabTools*)m_pTabDialogs[2])->SelectDefaultTool();
}

void PaintView::SetCurrentTool(PaintTool* pTool)
{
	m_pMainDialog->SetCurrentTool(pTool);
}

// -------------------------------------------------------------------------------

PaintController::PaintController()
: ControllerBase()
{
	m_pImage = new GImage();
	m_pImage->SetSize(300, 300);
	m_pImage->Clear(0xffffffff);
	m_pSelection = new GImage();
	m_pSelection->SetSize(300, 300);
	m_pSelection->Clear(0);
	m_pView = new PaintView(this, m_pImage, m_pSelection);
	((PaintView*)m_pView)->SelectDefaultTool();
}

PaintController::~PaintController()
{
	delete(m_pView);
	delete(m_pImage);
	delete(m_pSelection);
}

void PaintController::SetCurrentTool(PaintTool* pTool)
{
	((PaintView*)m_pView)->SetCurrentTool(pTool);
	pTool->OnSelect();
}

void PaintController::OnOpenFile(const char* szFilename)
{
	PathData pd;
	GFile::ParsePath(szFilename, &pd);
	const char* szExt = &szFilename[pd.extStart];
	if(stricmp(szExt, ".png") == 0)
	{
		if(!m_pImage->LoadPNGFile(szFilename))
			printf("Failed to load file: %s\n", szFilename);
	}
	else if(stricmp(szExt, ".bmp") == 0)
	{
		if(!m_pImage->LoadBMPFile(szFilename))
			printf("Failed to load file: %s\n", szFilename);
	}
	else if(stricmp(szExt, ".ppm") == 0)
	{
		if(!m_pImage->LoadPPMFile(szFilename))
			printf("Failed to load file: %s\n", szFilename);
	}
	else if(stricmp(szExt, ".pgm") == 0)
	{
		if(!m_pImage->LoadPGMFile(szFilename))
			printf("Failed to load file: %s\n", szFilename);
	}
	else
		GAssert(false, "unknown image type");
	m_pSelection->SetSize(m_pImage->GetWidth(), m_pImage->GetHeight());
	m_pSelection->Clear(0);
	((PaintView*)m_pView)->OnLoadImage();
	RedrawCanvas();
}

void PaintController::OnMaskFile(const char* szFilename)
{
	PathData pd;
	GFile::ParsePath(szFilename, &pd);
	const char* szExt = &szFilename[pd.extStart];
	GImage tmp;
	if(stricmp(szExt, ".png") == 0)
	{
		if(!tmp.LoadPNGFile(szFilename))
			printf("Failed to load file: %s\n", szFilename);
	}
	else if(stricmp(szExt, ".bmp") == 0)
	{
		if(!tmp.LoadBMPFile(szFilename))
			printf("Failed to load file: %s\n", szFilename);
	}
	else if(stricmp(szExt, ".ppm") == 0)
	{
		if(!tmp.LoadPPMFile(szFilename))
			printf("Failed to load file: %s\n", szFilename);
	}
	else if(stricmp(szExt, ".pgm") == 0)
	{
		if(!tmp.LoadPGMFile(szFilename))
			printf("Failed to load file: %s\n", szFilename);
	}
	else
		GAssert(false, "unknown image type");
	int x, y;
	for(y = 0; y < (int)m_pImage->GetHeight(); y++)
	{
		for(x = 0; x < (int)m_pImage->GetWidth(); x++)
		{
			if(gGray(m_pImage->GetPixel(x, y)) > 128 * 256 &&
				x < (int)tmp.GetWidth() && y < (int)tmp.GetHeight())
				m_pImage->SetPixel(x, y, tmp.GetPixel(x, y));
			else
				m_pImage->SetPixel(x, y, 0xff000000);
		}
	}
	((PaintView*)m_pView)->CleanCanvas();
	RedrawCanvas();
}

void PaintController::CleanCanvas()
{
	((PaintView*)m_pView)->CleanCanvas();
}

void PaintController::RedrawCanvas()
{
	((PaintView*)m_pView)->RedrawCanvas();
}

void PaintController::OnSelectTab(int i)
{
	((PaintView*)m_pView)->OnSelectTab(i);
}

void PaintController::RunModal()
{
	double timeOld = GTime::GetTime();
	double time;
	double timeUpdate = 0;
	m_pView->Update();
	while(m_bKeepRunning)
	{
		time = GTime::GetTime();
		if(HandleEvents(time - timeOld)) // HandleEvents returns true if it thinks the view needs to be updated
		{
			m_pView->Update();
			timeUpdate = time;
		}
		else
		{
			RedrawCanvas();
			m_pView->Update();
			GThread::sleep(20);
		}
		timeOld = time;
	}
}

void PaintController::ZoomIn()
{
	((PaintView*)m_pView)->ZoomIn();
}

void PaintController::ZoomOut()
{
	((PaintView*)m_pView)->ZoomOut();
}

⌨️ 快捷键说明

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