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

📄 seqprocessdlgfile.cpp

📁 在人脸检测的基础之上,对嘴部的运动表情进行分析,进行语音模拟.
💻 CPP
字号:
// SeqProcessDlgFile.cpp : implementation file
//

#include "stdafx.h"
#include "SeqProcess.h"
#include "SeqProcessDlg.h"
#include "FnMath.h"
#include "FnBitmap.h"
#include "InitialDirectory.h"

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

// Create a math functions object
static FnMath fMath;

////////////////////////////////////////////////////////////////////////////////

void CSeqProcessDlg::InitializeFile() 
{
	//strInitialDir = INITIALDIRECTORY_IMAGES;

	// Initialize menu item properties
	DisableFileMenuItems();
	GetMenu()->EnableMenuItem(ID_FILE_OPEN_BMP, MF_ENABLED);

	// dlgLoadSequence parameters initialization
	dlgLoadSequence.m_from = 0;
	dlgLoadSequence.m_to = 0;

	// Set currentframe to -1 to indicate no image in memory
	currentFrame = -1;
	dibSeq = NULL;
	m_width = 320;
	m_height = 240;

	// Create memory device contexts for display
	CClientDC dc(this);
	dcMemDraw.CreateCompatibleDC( &dc);
	NewImage = TRUE; 

}
////////////////////////////////////////////////////////////////////////////////

void CSeqProcessDlg::EnableFileMenuItems()
{
	GetMenu()->EnableMenuItem(ID_FILE_OPEN_BMP, MF_ENABLED);
	GetMenu()->EnableMenuItem(ID_FILE_LOADSEQUENCE, MF_ENABLED);
}
////////////////////////////////////////////////////////////////////////////////

void CSeqProcessDlg::DisableFileMenuItems()
{
	GetMenu()->EnableMenuItem(ID_FILE_OPEN_BMP, MF_GRAYED);
	GetMenu()->EnableMenuItem(ID_FILE_LOADSEQUENCE, MF_GRAYED);
}

////////////////////////////////////////////////////////////////////////////////

void CSeqProcessDlg::OnFileExit() 
{
	EndDialog(IDOK);
	
}

////////////////////////////////////////////////////////////////////////////////

BOOL CSeqProcessDlg::OpenFile( CString& strFileName, CString& strFileTitle, 
							   CString strFileExtension, CString strInitialDir,
							   CString strOpenTitle, 
							   char* strFilter, BOOL isReadFile)
{
	char FileName[500];
	char FileTitle[100];

	OPENFILENAME ofn;
	memset(&ofn, 0, sizeof(ofn));
	ofn.lStructSize = sizeof(OPENFILENAME);

	ofn.hwndOwner = m_hWnd;
	ofn.hInstance = NULL;

	ofn.lpstrFilter = strFilter;
	ofn.lpstrCustomFilter = NULL;
	ofn.nMaxCustFilter = 0;
	ofn.nFilterIndex = 1;
	ofn.lpstrFile = FileName;
	ofn.nMaxFile = 500;
	ofn.lpstrFileTitle = FileTitle;
	ofn.nMaxFileTitle = 99;
	ofn.lpstrInitialDir = (LPCTSTR)strInitialDir;
	ofn.lpstrTitle = (LPCTSTR)strOpenTitle; //"Open BMP File";
	ofn.Flags = OFN_FILEMUSTEXIST;
	ofn.lpstrDefExt = (LPCTSTR)strFileExtension;
	ofn.lCustData = NULL;
	ofn.lpfnHook = NULL;
	ofn.lpTemplateName = NULL;

	FileName[0] = '\0';
	if( isReadFile) GetOpenFileName(&ofn);
	else GetSaveFileName(&ofn);

	if(FileName[0] == '\0')	return FALSE;

	strFileName = FileName;
	strFileTitle = FileTitle;

	return TRUE;
}

////////////////////////////////////////////////////////////////////////////////

void CSeqProcessDlg::OnFileOpenBmp() 
{
	CString strFileName; //return value
	CString strFileTitle; //return value
	CString strFileExtension = "bmp";
//	CString strInitialDir = INITIALDIRECTORY_IMAGES;
	CString strOpenTitle = "Open BMP File";

	char* strFilter;
	strFilter = new char[256];
	strFilter = TEXT("Bitmap picture files *.bmp\0*.bmp\0All Files *\0*\0\0");
	
	if( ! OpenFile( strFileName, strFileTitle, strFileExtension, 
		m_initDir.Image, strOpenTitle, strFilter, TRUE)) return;
	m_initDir.Image = strFileName.Left( strFileName.GetLength() -  strFileTitle.GetLength() - 1); 

	CString BaseDirectory;
	BaseDirectory = strFileName.Left( strFileName.GetLength() -  strFileTitle.GetLength() - 5);
	m_initDir.ChangeDirectoryNames( BaseDirectory);

	// Load the requested image
	// also sets the image size
	if( ! LoadAndDisplaySingleImage( strFileName))
	{
		MessageBox("Failed to load image!");
		currentFrame = -1;
		return;
	}

	if( dlgLoadSequence.prepareLoadSequence(strFileName, strFileTitle))
	{
		// Image file is part of a sequence. Hence, LoadSequence is enabled.
		GetMenu()->EnableMenuItem(ID_FILE_LOADSEQUENCE, MF_ENABLED); //***
	}
	else
	{
		GetMenu()->EnableMenuItem(ID_FILE_LOADSEQUENCE, MF_GRAYED);
	}

	NewImage = TRUE; 
	DisablePlayDlgItems();

	DrawCurrentImage();

}

////////////////////////////////////////////////////////////////////////////////
BOOL CSeqProcessDlg::LoadAndDisplaySingleImage( CString name)
////////////////////////////////////////////////////////////////////////////////
{
	// Initialize dib sequence
	if( dibSeq != NULL) delete[] dibSeq;
	dibSeq = new CDib[1];
	currentFrame = 0;

	CFile myFile;
	myFile.Open( (LPCTSTR)name, CFile::modeRead);
	CClientDC dc(this);
	CDib dib; 
	if( ! dib.ReadSection( &myFile, &dc))
	{
		myFile.Close();
		return FALSE;
	}
	//dibSeq[0].DeepCopy(dib, &dc);
	
	if( dib.m_lpBMIH->biBitCount == 8)
	{
		dibSeq[0].DeepCopyWithoutColorMap(dib, &dc);
	} 
	else
	{
		dibSeq[0].DeepCopy(dib, &dc);
	}
	
	myFile.Close();
	
	dc.FillSolidRect( xPosWindow, yPosWindow, scale*m_width, scale*m_height, RGB(192, 192, 192));
	
	if( m_width != dibSeq[0].m_lpBMIH->biWidth || (int) m_height != dibSeq[0].m_lpBMIH->biHeight)
	{
		m_width = dibSeq[0].m_lpBMIH->biWidth;
		m_height = dibSeq[0].m_lpBMIH->biHeight;
		InitializeWindowDisplay();
	}
	
	CPoint originDisp;
	originDisp.x = xPosWindow;
	originDisp.y = yPosWindow;
	CSize sizeDisp;
	sizeDisp.cx = m_width;
	sizeDisp.cy = m_height;
	
	return TRUE;
}

////////////////////////////////////////////////////////////////////////////////
void CSeqProcessDlg::OnFileLoadsequence() 
////////////////////////////////////////////////////////////////////////////////
{
	CClientDC dc(this);

	long frFrom = dlgLoadSequence.m_from;
	long frTo = dlgLoadSequence.m_to;

	if( dlgLoadSequence.DoModal() == IDCANCEL)
	{
		// Load sequence operation cancelled by the dialog
		// Reassign previous values of m_from and m_to
		// m_start and m_end remain as they were obtained by prepareLoadSequence()
		dlgLoadSequence.m_from = frFrom;
		dlgLoadSequence.m_to = frTo;
		return;
	}

	frFrom = dlgLoadSequence.m_from;
	frTo = dlgLoadSequence.m_to;

	m_startframe = frFrom;
	m_fromframe = frFrom;
	m_endframe = frTo;
	m_toframe = frTo;

	CFile myFile;
	long i;
	CString strTmpNumber;
	CString strTmpFileName;
	if( dibSeq != NULL) delete[] dibSeq;
	int length = frTo - frFrom + 1;
	dibSeq = new CDib[length];
	CDib dib; 
	for( i=frFrom; i<=frTo; i++)
	{
		strTmpNumber.Format("%04d", i);
		strTmpFileName = dlgLoadSequence.m_seqNameFull + strTmpNumber + ".bmp";
		myFile.Open( (LPCTSTR) strTmpFileName, CFile::modeRead);
		dib.ReadSection( &myFile, &dc);
		//dibSeq[i-frFrom].DeepCopy(dib, &dc);
		
		if( dib.m_lpBMIH->biBitCount == 8)
		{
			dibSeq[i-frFrom].DeepCopyWithoutColorMap(dib, &dc);
		} 
		else
		{
			dibSeq[i-frFrom].DeepCopy(dib, &dc);
		}
		
		//m_bmp.SetFrame(i-frFrom, strTmpFileName);
		myFile.Close();
	}

	currentFrame = 0;

	DrawCurrentImage();

	// Display the frame numbers on the interface
	m_startframe = frFrom;
	m_endframe = frTo;
	m_fromframe = frFrom;
	m_toframe = frTo;
	m_currentframe = currentFrame + frFrom;

	// Set the slider range and position
	m_sliderframe.SetRange(frFrom, frTo);
	m_sliderframe.SetPos(m_currentframe);
	UpdateData(FALSE);

	EnablePlayButtons();

	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(FALSE);

	EnablePlayDlgItems();

}
//---------------------------------------------------------------------------------------------
BOOL CSeqProcessDlg::GetFileName( CString& strFileName, CString& text, BOOL IsReadFile, 
								 CString strInitialDir )
{
	// Filename contains the full path of the file 

	CString strFileTitle; //return value
	CString strFileExtension = "";
	CString strOpenTitle = text;
	char* strFilter;
	strFilter = new char[256];
	strFilter = TEXT("All Files *\0*\0\0");
	if( ! OpenFile( strFileName, strFileTitle, strFileExtension, 
		strInitialDir, strOpenTitle, strFilter, IsReadFile)) return FALSE;
	strInitialDir = strFileName.Left( strFileName.GetLength() -  strFileTitle.GetLength() - 1); 
	return TRUE;
}
//---------------------------------------------------------------------------------------------
void CSeqProcessDlg::SaveMatrix( matrix& img, CString& title, CString strInitialDir)
{
	CString strFileTitle; //return value
	CString strFileName; //return value
	CString strFileExtension = ".bmp";
	CString strOpenTitle = title;
	char* strFilter;
	strFilter = new char[256];
	strFilter = TEXT("Bitmap files *.bmp\0 *.bmp\0All Files *\0*\0\0");
	if( ! OpenFile( strFileName, strFileTitle, strFileExtension, 
		strInitialDir, strOpenTitle, strFilter, FALSE)) return;
	strInitialDir = strFileName.Left( strFileName.GetLength() -  strFileTitle.GetLength() - 1); 
	// Convert the matrix to CDib

	CDib dib;
	CClientDC dc(this); 
	dib.Create( img.xsize, img.ysize, 8*img.numbands, &dc);

	ConvertMat2Dib ( dib, img);

	// write the CDib image as bitmap, first get the name of the .bmp file 

	CFile myfile;

	myfile.Open(strFileName, CFile::modeCreate | CFile::modeWrite);

	// write as bitmap 
	dib.Write( &myfile); 
}

//**********************************************************
matrix CSeqProcessDlg::ReadBmp( CString filename)
//**********************************************************
{
	CClientDC	dc(this);
	CFile		bmpFile;
	CDib		bmpDib;
	matrix		image_matrix;

	bmpFile.Open(filename, CFile::modeRead);
	if( !bmpDib.ReadSection( &bmpFile, &dc))
	{
		bmpFile.Close();
		return image_matrix;
	}
	bmpFile.Close();

	// Convert the bitmaps to matrices
	ConvertDib2Mat( image_matrix, bmpDib);
	
	return image_matrix;
}


//**********************************************************
void   CSeqProcessDlg::SaveAsBmp( matrix image, CString filename)
//**********************************************************
{

	CDib dib;
	CClientDC dc(this); 
	dib.Create( image.xsize, image.ysize, 8*image.numbands, &dc);

	CFile outFile;

	ConvertMat2Dib(dib, image);
	
	if (outFile.Open( filename, CFile::modeWrite | CFile::modeCreate) )
	{
		// write as bitmap 
		dib.Write( &outFile); 	
		outFile.Close();
	}
	else
		MessageBox("Cannot open file for  output"); 

}

⌨️ 快捷键说明

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