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

📄 video.cpp

📁 一个由Mike Gashler完成的机器学习方面的includes neural net, naive bayesian classifier, decision tree, KNN, a genet
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	virtual void OnReleaseTextButton(GWidgetTextButton* pButton)
	{
	}

	virtual void OnSelectFilename(GWidgetFileSystemBrowser* pBrowser, const char* szFilename)
	{
		m_pController->OnOpenVideo1(szFilename);
	}
};


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

class VideoTabOpen2 : public VideoTabDialog
{
protected:
	GWidgetFileSystemBrowser* m_pFileSystemBrowser;

public:
	VideoTabOpen2(VideoController* pController, int w)
	 : VideoTabDialog(pController, w)
	{
		m_pFileSystemBrowser = new GWidgetFileSystemBrowser(this, 0, 0, 500, TOOL_AREA_SIZE, ".png;.bmp;.pgm;.ppm");
	}

	virtual ~VideoTabOpen2()
	{
	}

	virtual void OnReleaseTextButton(GWidgetTextButton* pButton)
	{
	}

	virtual void OnSelectFilename(GWidgetFileSystemBrowser* pBrowser, const char* szFilename)
	{
		m_pController->OnOpenVideo2(szFilename);
	}
};


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

class VideoTabTools : public VideoTabDialog
{
protected:
	GWidgetTextTab* m_pCurrentToolTab;
	GWidgetTextButton* m_pMorphButton;
	GWidgetTextButton* m_pPlayButton;
	GWidgetTextButton* m_pStopButton;
	GWidgetTextButton* m_pSaveButton;
	GWidgetTextButton* m_pLoadButton;

	GWidgetTextTab* m_pTabPen;
	VideoToolPen* m_pToolPen;

	GWidgetTextTab* m_pTabSmartSelect;
	VideoToolSmartSelect* m_pToolSmartSelect;

public:
	VideoTabTools(VideoController* pController, int w)
	 : VideoTabDialog(pController, w)
	{
		GString s;
		m_pMorphButton = new GWidgetTextButton(this, 175, 30, 80, 20, "Morph");
		m_pPlayButton = new GWidgetTextButton(this, 20, 54, 80, 20, "Play");
		m_pStopButton = new GWidgetTextButton(this, 110, 54, 80, 20, "Stop");
		m_pSaveButton = new GWidgetTextButton(this, 20, 80, 80, 20, "Save Seeds");
		m_pLoadButton = new GWidgetTextButton(this, 110, 80, 80, 20, "Load Seeds");

		m_pTabPen = new GWidgetTextTab(this, 5, 5, 50, 20, "Pen");
		m_pToolPen = new VideoToolPen(pController);
		m_pCurrentToolTab = m_pTabPen;
		m_pCurrentToolTab->SetSelected(true);

		m_pTabSmartSelect = new GWidgetTextTab(this, 55, 5, 100, 20, "Smart Select");
		m_pToolSmartSelect = new VideoToolSmartSelect(pController);
	}

	virtual ~VideoTabTools()
	{
		delete(m_pToolPen);
		delete(m_pToolSmartSelect);
	}

	virtual void OnReleaseTextButton(GWidgetTextButton* pButton)
	{
		GImage* pImage = m_pController->GetVideo1()->GetFrame(0);
		if(pButton == m_pMorphButton)
			m_pToolSmartSelect->Morph();
		else if(pButton == m_pPlayButton)
			m_pController->Play();
		else if(pButton == m_pStopButton)
			m_pController->Stop();
		else if(pButton == m_pSaveButton)
			m_pToolSmartSelect->SaveSeeds();
		else if(pButton == m_pLoadButton)
			m_pToolSmartSelect->LoadSeeds();
		else
			GAssert(false, "unrecognized button");

		// Redraw the canvas
		m_pController->RedrawCanvas();
	}

	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_pTabSmartSelect)
			m_pController->SetCurrentTool(m_pToolSmartSelect);
		else
			GAssert(false, "unknown tool");
	}

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


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

class VideoView : public ViewBase
{
friend class VideoController;
protected:
	bool m_bPlaying;
	GWidgetDialog** m_pTabDialogs;
	int m_nSelectedTab;
	VideoDialog* m_pMainDialog;
	GImage* m_pImage;

public:
	VideoView(VideoController* pController);
	virtual ~VideoView();

	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 CleanCanvas();
	void RedrawCanvas();
	void SelectDefaultTool();
	void SetCurrentTool(VideoTool* pTool);
	void SetVideo1(GVideo* pVideo, GVideo* pSelection);
	void SetVideo2(GVideo* pVideo, GVideo* pSelection);
	void OnScrollWheel(bool bDirection);

	void Play()
	{
		m_bPlaying = true;
		m_pMainDialog->ChangeFrame1(0);
		m_pMainDialog->ChangeFrame2(0);
	}

	void Stop()
	{
		m_bPlaying = false;
	}

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

VideoView::VideoView(VideoController* pController)
: ViewBase()
{
	m_bPlaying = false;
	m_pTabDialogs = new GWidgetDialog*[VIDEO_TAB_COUNT];
	m_pTabDialogs[0] = new VideoTabOpen1(pController, m_screenRect.w);
	m_pTabDialogs[1] = new VideoTabOpen2(pController, m_screenRect.w);
	m_pTabDialogs[2] = new VideoTabTools(pController, m_screenRect.w);
	m_nSelectedTab = 0;
	m_pMainDialog = new VideoDialog(pController, m_screenRect.w, m_screenRect.h - TOOL_AREA_SIZE);
	m_pImage = NULL;
}

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

void VideoView::SetVideo1(GVideo* pVideo, GVideo* pSelection)
{
	m_pMainDialog->SetVideo1(pVideo, pSelection);
	CleanCanvas();
}

void VideoView::SetVideo2(GVideo* pVideo, GVideo* pSelection)
{
	m_pMainDialog->SetVideo2(pVideo, pSelection);
	CleanCanvas();
}

/*virtual*/ void VideoView::Draw(SDL_Surface *pScreen)
{
	if(m_bPlaying)
		m_pMainDialog->PlayFrame();

	// 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 VideoView::OnSelectTab(int i)
{
	m_nSelectedTab = i;
}

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

void VideoView::OnScrollWheel(bool bDirection)
{
	m_pMainDialog->OnScrollWheel(bDirection);
}

void VideoView::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 VideoView::OnMouseUp(int nButton, int x, int y)
{
	m_pMainDialog->ReleaseWidget(nButton);
	m_pTabDialogs[m_nSelectedTab]->ReleaseWidget(nButton);
}

bool VideoView::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 VideoView::CleanCanvas()
{
	m_pMainDialog->CleanCanvas();
}

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

void VideoView::SelectDefaultTool()
{
	((VideoTabTools*)m_pTabDialogs[2])->SelectDefaultTool();
}

void VideoView::SetCurrentTool(VideoTool* pTool)
{
	m_pMainDialog->SetCurrentTool(pTool);
}

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

VideoController::VideoController()
: ControllerBase()
{
	m_bPlaying = false;
	m_pVideo1 = NULL;
	m_pVideo2 = NULL;
	m_pSelection1 = NULL;
	m_pSelection2 = NULL;
	m_pView = new VideoView(this);
	((VideoView*)m_pView)->SelectDefaultTool();
}

VideoController::~VideoController()
{
	delete(m_pView);
	delete(m_pVideo1);
	delete(m_pVideo2);
	delete(m_pSelection1);
	delete(m_pSelection2);
}

void VideoController::SetCurrentTool(VideoTool* pTool)
{
	((VideoView*)m_pView)->SetCurrentTool(pTool);
	pTool->OnSelect();
}

void VideoController::OnOpenVideo1(const char* szFilename)
{
	// Change to the directory of the specified file
	char szOldDir[256];
	getcwd(szOldDir, 256);
	PathData pd;
	GFile::ParsePath(szFilename, &pd);
	GTEMPBUF(char, szNewDir, pd.fileStart + 1);
	memcpy(szNewDir, szFilename, pd.fileStart);
	szNewDir[pd.fileStart] = '\0';
	chdir(szNewDir);

	// Load the sample image
	GImage sample;
	if(!sample.LoadPNGFile(szFilename))
		GAssert(false, "failed to load the sample frame");
	if(m_pVideo1)
		m_pVideo1->SetSize(sample.GetWidth(), sample.GetHeight());
	else
		m_pVideo1 = new GVideo(sample.GetWidth(), sample.GetHeight());
	if(m_pSelection1)
		m_pSelection1->SetSize(sample.GetWidth(), sample.GetHeight());
	else
		m_pSelection1 = new GVideo(sample.GetWidth(), sample.GetHeight());

	// Load all the frames
	char szTmp[16];
	int i;
	for(i = 1; ; i++)
	{
		sprintf(szTmp, "%08d.png", i);
		if(!GFile::DoesFileExist(szTmp))
			break;
		if(!m_pVideo1->LoadFrame(szTmp))
		{
			GAssert(false, "Failed to load frame");
			break;
		}
		if(m_pSelection1->GetFrameCount() < m_pVideo1->GetFrameCount())
			m_pSelection1->AddBlankFrame();
	}
	if(i <= 1)
		GAssert(false, "didn't load any frames");
	chdir(szOldDir);
	((VideoView*)m_pView)->SetVideo1(m_pVideo1, m_pSelection1);
	RedrawCanvas();
}

void VideoController::OnOpenVideo2(const char* szFilename)
{
	// Change to the directory of the specified file
	char szOldDir[256];
	getcwd(szOldDir, 256);
	PathData pd;
	GFile::ParsePath(szFilename, &pd);
	GTEMPBUF(char, szNewDir, pd.fileStart + 1);
	memcpy(szNewDir, szFilename, pd.fileStart);
	szNewDir[pd.fileStart] = '\0';
	chdir(szNewDir);

	// Load the sample image
	GImage sample;
	if(!sample.LoadPNGFile(szFilename))
		GAssert(false, "failed to load the sample frame");
	if(m_pVideo2)
		m_pVideo2->SetSize(sample.GetWidth(), sample.GetHeight());
	else
		m_pVideo2 = new GVideo(sample.GetWidth(), sample.GetHeight());
	if(m_pSelection2)
		m_pSelection2->SetSize(sample.GetWidth(), sample.GetHeight());
	else
		m_pSelection2 = new GVideo(sample.GetWidth(), sample.GetHeight());

//m_pVideo2->MakeGradientMagnitudeVideo(m_pVideo1, true);

	// Load all the frames
	char szTmp[16];
	int i;
	for(i = 1; ; i++)
	{
		sprintf(szTmp, "%08d.png", i);
		if(!GFile::DoesFileExist(szTmp))
			break;
		if(!m_pVideo2->LoadFrame(szTmp))
		{
			GAssert(false, "Failed to load frame");
			break;
		}
		if(m_pSelection2->GetFrameCount() < m_pVideo2->GetFrameCount())
			m_pSelection2->AddBlankFrame();
	}
	if(i <= 1)
		GAssert(false, "didn't load any frames"); 
	chdir(szOldDir);
	((VideoView*)m_pView)->SetVideo2(m_pVideo2, m_pSelection2);
	RedrawCanvas();
}

void VideoController::CleanCanvas()
{
	((VideoView*)m_pView)->CleanCanvas();
}

void VideoController::RedrawCanvas()
{
	((VideoView*)m_pView)->RedrawCanvas();
}

void VideoController::OnSelectTab(int i)
{
	((VideoView*)m_pView)->OnSelectTab(i);
}

void VideoController::OnScrollWheel(bool bDirection)
{
	((VideoView*)m_pView)->OnScrollWheel(bDirection);
}

void VideoController::Play()
{
	m_bPlaying = true;
	((VideoView*)m_pView)->Play();
}

void VideoController::Stop()
{
	m_bPlaying = false;
	((VideoView*)m_pView)->Stop();
}

void VideoController::RunModal()
{
	double timeOld = GTime::GetTime();
	double time;
	double timeUpdate = 0;
	m_pView->Update();
	while(m_bKeepRunning)
	{
		time = GTime::GetTime();
		if(HandleEvents(time - timeOld) || m_bPlaying) // 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;
	}
}

⌨️ 快捷键说明

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