lsbanalysisdoc.cpp

来自「信息隐藏中用于数字隐写的常用算法:LSB替换LSB匹配,包括随机的和排序的,以及」· C++ 代码 · 共 1,714 行 · 第 1/4 页

CPP
1,714
字号
	WORD  wBpp = image->GetBpp();                 //位数


	BYTE* pImg  = NULL;                 //图像数据指针
	pImg = image->GetBits();             

	BYTE* pData = NULL;				 //待嵌入数据指针
	pData = ReadPRNData(dwImgSize);       
	if (pData == NULL)
		return;

//...................序贯LSB Match嵌入...........................
	LSBMatchEmbed2(pImg, pData, dwImgSize, fRatio);

	//另存为新的图像文件
	CString m_strPrefix = _T("");
	m_strPrefix.Format("_Serial_%d", int(fRatio * 100));
	char cDrive[_MAX_DRIVE];              //驱动器名字符串
	char cDir  [_MAX_DIR];                //子路径字符串
    char cFname[_MAX_FNAME + _MAX_EXT];   //纯文件名字符串
    char cExt  [_MAX_EXT];                //后缀名字符串
	char cPath [_MAX_DRIVE + _MAX_DIR];   //全路径名字符串

	//从文件全名中分割得到驱动名和子路径
	_splitpath((LPCTSTR)m_strPathName, cDrive, cDir, cFname, cExt);
	strcpy(cPath, cDrive);
	strcat(cPath, cDir);
	CString m_strSaveName = (CString)cPath + (CString)cFname + m_strPrefix + (CString)cExt;

	memcpy(image->info.pImage, pImg, dwHeight * dwEffWidth);
	image->Save(m_strSaveName, nImgType);    
	
	if (pData != NULL)
	{	delete []pData;	pData = NULL;}
	
}


//.................文件夹随机LSB Match嵌入................//
void CLSBAnalysisDoc::OnEmbedLsbmatch3() 
{
//.............对选择的目录进行操作................
	BROWSEINFO bi;  
	char cDispName[MAX_PATH], cPath[MAX_PATH];  
	bi.hwndOwner = 0;  
	bi.pidlRoot = 0;  
	bi.pszDisplayName = cDispName;  
	bi.lpszTitle = "请选择目录:";  
	bi.ulFlags = BIF_RETURNONLYFSDIRS;  
	bi.lpfn = 0;  
	bi.lParam = 0;  
	bi.iImage = 0;  	
	
	//得到路径名
	CStringList myList;       //supports lists of CString objects
	ITEMIDLIST * pidl;        //List of item identifiers. 
	if (pidl = SHBrowseForFolder(&bi))
	{
		SHGetPathFromIDList(pidl, cPath);  
		myList.AddTail(cPath);
	}

	else
	{	return;	}


	//找到目录中图像文件
	CFileFind myFinder;
	CString m_strFindPath = _T("");   //找图像文件的路径名
	UINT m_nFileCnt=0;                //文件计数

	//创建结果保存目录
	strcat(cPath, "\\100+100\\");
	BOOL bExsist = FALSE;
	bExsist = myFinder.FindFile( LPCTSTR(cPath) ); 
	if (!bExsist)
	{
		::CreateDirectory(LPCTSTR(cPath), NULL);
	}


	float fRatio;
//	int   nSeed = 7836;        //种子数(嵌入密码)默认为:12
	DWORD dwHeight = 0;      //高
	DWORD dwEffWidth = 0;    //实际宽度,对灰度图象为4倍数后的宽度;对彩色图像,为灰度图象宽度的3倍
	DWORD dwImgSize = 0;     //图像数据大小
	BYTE* pImg  = NULL;     //图像数据指针
	BYTE* pData = NULL;	 //待嵌入数据指针
	int* m_pImgPerm = NULL;  //图像置乱后的排序指针
	char cFname[_MAX_FNAME + _MAX_EXT];   //纯文件名字符串
	char cExt  [_MAX_EXT];                //后缀名字符串

    if (!myList.IsEmpty())
	{
		m_strFindPath = myList.GetHead()+_T("\\*.bmp");

		BOOL bWorking = FALSE;
		bWorking = myFinder.FindFile(LPCTSTR(m_strFindPath));
		BeginWaitCursor(); 
		srand( (unsigned)time( NULL ) ); 

		if (bWorking)
		{
			//循环找文件进行校正
			while (bWorking)
			{
				bWorking = myFinder.FindNextFile();
				m_nFileCnt = m_nFileCnt+1;        //文件计数 

				if (myFinder.IsDots() || myFinder.IsDirectory()) continue;    //子目录越过,避免重复搜索
//                 if(m_nFileCnt>100) return;
				CString m_strSrcFileName = myFinder.GetFilePath();            //源文件全路径名
				CString ext(FindExtension(m_strSrcFileName));
				ext.MakeLower();
				nImgType = FindType(ext);
				image = new CxImage(m_strSrcFileName, nImgType);
				if (!image->IsValid())
				{
					AfxMessageBox(image->GetLastError());
					delete image;
					image = NULL;
					return ;
				}

			    //................得到图像基本信息....................
				dwHeight = image->GetHeight();           //高
				dwEffWidth  = image->GetEffWidth();      //实际宽度,对灰度图象为4倍数后的宽度;对彩色图像,为灰度图象宽度的3倍
				dwImgSize  = dwHeight * dwEffWidth;      //图像数据大小

				pImg = image->GetBits();             //得到图像数据    
				if (pImg == NULL)
				{	delete image;	image = NULL;	return;	}
				BYTE* pTemp = NULL;                  //用于存储原始图像,进行嵌入的临时内存
				pTemp = new BYTE[dwImgSize];
				ASSERT(pTemp != NULL);
				memset(pTemp, 0, dwImgSize * sizeof(BYTE));

				//................得到待嵌入的秘密信息....................
				pData = ReadPRNData(dwImgSize);      //得到嵌入数据    
				if (pData == NULL)
				{	delete image;	image = NULL;	return;	}

			    //.................得到图像置乱的排序...................
				m_pImgPerm = new int[dwImgSize];
				memset(m_pImgPerm, 0, dwImgSize * sizeof(int));
				
				Rand_Perm(m_pImgPerm, dwImgSize);

				//.................另存为新的图像文件...................
				//从文件全名中分割得到驱动名和子路径
				_splitpath((LPCTSTR)m_strSrcFileName, NULL, NULL, cFname, cExt);
//				strcat(cPath, "\\");
				
			//	for (DWORD i = 1; i < 11; i++)
			//				{
			
					//嵌入率从0.05->0.1....->1.0
//					Rand_Perm((int)&pData, dwImgSize);
					memcpy(pTemp, pImg, dwImgSize * sizeof(BYTE));    //非常重要

					// fRatio = float(0.1 * i);
					fRatio = float(1.0 );
					int nRatio = int(fRatio * 100);
					//.................随机LSB Match嵌入....................
					LSBMatchEmbed(pTemp, pData, m_pImgPerm, dwImgSize, fRatio);
					//CString m_strSuffix = _T("");
					//					m_strSuffix.Format("_Random_%d", nRatio);
					//					CString m_strSaveName = (CString)cPath + (CString)cFname + m_strSuffix + (CString)cExt;
					CString m_strSaveName = (CString)cPath + (CString)cFname + (CString)cExt;
					memcpy(image->info.pImage, pTemp, dwHeight * dwEffWidth);
					image->Save(m_strSaveName, nImgType);    					
				// }


				//........................释放内存..........................
				if (pData != NULL)
				{	delete []pData;	pData = NULL;	}
				if (m_pImgPerm != NULL)
				{	delete []m_pImgPerm; m_pImgPerm = NULL;	}

				//新加释放image内存
				delete []pTemp;	pTemp = NULL;
				delete image;	image = NULL;

			}

		}
		else
		{
		   AfxMessageBox("当前选择目录下没有BMP图象文件!");
		}
	}

	myList.RemoveHead();

  EndWaitCursor();		

}


//.................文件夹序贯LSB Match嵌入................//
void CLSBAnalysisDoc::OnEmbedLsbmatch4() 
{
//.............对选择的目录进行操作................
	BROWSEINFO bi;  
	char cDispName[MAX_PATH], cPath[MAX_PATH];  
	bi.hwndOwner = 0;  
	bi.pidlRoot = 0;  
	bi.pszDisplayName = cDispName;  
	bi.lpszTitle = "请选择目录:";  
	bi.ulFlags = BIF_RETURNONLYFSDIRS;  
	bi.lpfn = 0;  
	bi.lParam = 0;  
	bi.iImage = 0;  	
	
	//得到路径名
	CStringList myList;       //supports lists of CString objects
	ITEMIDLIST * pidl;        //List of item identifiers. 
	if (pidl = SHBrowseForFolder(&bi))
	{
		SHGetPathFromIDList(pidl, cPath);  
		myList.AddTail(cPath);
	}

	else
	{	return;	}


	//找到目录中图像文件
	CFileFind myFinder;
	CString m_strFindPath = _T("");   //找图像文件的路径名
	UINT m_nFileCnt=0;                //文件计数

	//创建结果保存目录
	strcat(cPath, "\\results\\");
	BOOL bExsist = FALSE;
	bExsist = myFinder.FindFile( LPCTSTR(cPath) ); 
	if (!bExsist)
	{
		::CreateDirectory(LPCTSTR(cPath), NULL);
	}


	float fRatio;
//	int   nSeed = 5498;        //种子数(嵌入密码)默认为:12
	DWORD dwHeight = 0;      //高
	DWORD dwEffWidth = 0;    //实际宽度,对灰度图象为4倍数后的宽度;对彩色图像,为灰度图象宽度的3倍
	DWORD dwImgSize = 0;     //图像数据大小
	BYTE* pImg  = NULL;     //图像数据指针
	BYTE* pData = NULL;	 //待嵌入数据指针
	char cFname[_MAX_FNAME];   //纯文件名字符串
	char cExt  [_MAX_EXT];                //后缀名字符串

    if (!myList.IsEmpty())
	{
		m_strFindPath = myList.GetHead()+_T("\\*.bmp");

		BOOL bWorking = FALSE;
		bWorking = myFinder.FindFile(LPCTSTR(m_strFindPath));
		BeginWaitCursor(); 

		if (bWorking)
		{
			//循环找文件进行校正
			while (bWorking)
			{
				bWorking = myFinder.FindNextFile();
				m_nFileCnt = m_nFileCnt+1;        //文件计数 

				if (myFinder.IsDots() || myFinder.IsDirectory()) continue;    //子目录越过,避免重复搜索

				CString m_strSrcFileName = myFinder.GetFilePath();            //源文件全路径名
				CString ext(FindExtension(m_strSrcFileName));
				ext.MakeLower();
				nImgType = FindType(ext);
				image = new CxImage(m_strSrcFileName, nImgType);
				if (!image->IsValid())
				{
					AfxMessageBox(image->GetLastError());
					delete image;
					image = NULL;
					return ;
				}

			    //................得到图像基本信息....................
				dwHeight = image->GetHeight();           //高
				dwEffWidth  = image->GetEffWidth();      //实际宽度,对灰度图象为每行4倍数后的宽度;对彩色图像,为灰度图象宽度的3倍
				dwImgSize  = dwHeight * dwEffWidth;      //图像数据大小

				pImg = image->GetBits();             //得到图像数据    
				if (pImg == NULL)
				{	delete image;	image = NULL;	return;	}

				BYTE* pTemp = NULL;                  //用于存储原始图像,进行嵌入的临时内存
				pTemp = new BYTE[dwImgSize];
				ASSERT(pTemp != NULL);
				memset(pTemp, 0, dwImgSize * sizeof(BYTE));

				//................得到待嵌入的秘密信息....................
				pData = ReadPRNData(dwImgSize);      //得到嵌入数据    
				if (pData == NULL)
				{	delete image;	image = NULL;	return;	}

				//.................另存为新的图像文件...................

				//从文件全名中分割得到驱动名和子路径
				_splitpath((LPCTSTR)m_strSrcFileName, NULL, NULL, cFname, cExt);
//				strcat(cPath, "\\");
				
				for (DWORD i = 1; i < 21; i++)
				{
					//嵌入率从0.05->0.1....->1.0
//					Rand_Perm(&pData, dwImgSize);
					memcpy(pTemp, pImg, dwImgSize * sizeof(BYTE));
					fRatio = float(0.05 * i);
					int nRatio = int(fRatio * 100);
					//.................序贯LSB Match嵌入....................
					LSBMatchEmbed2(pTemp, pData, dwImgSize, fRatio);
					CString m_strSuffix = _T("");
					m_strSuffix.Format("_Serial_%d", nRatio);
					CString m_strSaveName = (CString)cPath + (CString)cFname + m_strSuffix + (CString)cExt;
					memcpy(image->info.pImage, pTemp, dwHeight * dwEffWidth);
					image->Save(m_strSaveName, nImgType);    					
				}


				//........................释放内存..........................
				if (pData != NULL)
				{	delete []pData;	pData = NULL;	}
								
				//新加释放image内存
				delete []pTemp;	pTemp = NULL;
				delete image;   image = NULL;

			}

		}
		else
		{
		   AfxMessageBox("当前选择目录下没有BMP图象文件!");
		}
	}

	myList.RemoveHead();

  EndWaitCursor();		
	
}



//......................单文件随机LSB Match提取.................
void CLSBAnalysisDoc::OnExtractLsbmatch() 
{

	//确保打开文件不为空
	ASSERT(m_strPathName != _T(""));

//...........得到嵌入率和密码,密码作为种子数..................
	CRatioDlg RatioDlg;
	float fRatio;
	int   nSeed;
	
	//得到嵌入率和密码
	if(RatioDlg.DoModal()==IDOK)
	{
		fRatio = RatioDlg.m_fRatio;
		nSeed = RatioDlg.m_key;
	}
	else
	{
		fRatio = 1.0;
		nSeed = 12;
	}

//.................得到图像基本信息..........................
	DWORD dwHeight = image->GetHeight();           //高
	DWORD dwWidth = image->GetWidth();             //宽
	DWORD dwEffWidth  = image->GetEffWidth();      //实际宽度
	DWORD dwImgSize  = dwHeight * dwEffWidth;      //图像数据大小
	DWORD dwColors = image->GetNumColors();        //颜色数
	WORD  wBpp = image->GetBpp();                  //位数


	BYTE* pImg  = NULL;                 //图像数据指针
	pImg = image->GetBits();             

//.................得到图像置乱的排序...................
	int* m_pImgPerm = NULL;
	m_pImgPerm = new int[dwImgSize];
	memset(m_pImgPerm, 0, dwImgSize * sizeof(int));
	srand(nSeed);
	Rand_Perm(m_pImgPerm, dwImgSize);

	BYTE* pData = NULL;				 //待提取数据指针
	//序贯LSB Match提取
	pData = LSBMatchExtract(pImg, m_pImgPerm, dwImgSize, fRatio);       
	if (pData == NULL)
	{
		delete []m_pImgPerm;	m_pImgPerm = NULL;	
		return;
	}
	
//...................另存为新的数据文件.............................
	char cDrive[_MAX_DRIVE];              //驱动器名字符串
	char cDir  [_MAX_DIR];                //子路径字符串   
    char cFname[_MAX_FNAME];              //纯文件名字符串
	char cPath [_MAX_DRIVE + _MAX_DIR + _MAX_FNAME + _MAX_EXT];   //全路径名字符串

	//从文件全名中分割得到驱动名和子路径
	_splitpath((LPCTSTR)m_strPathName, cDrive, cDir, cFname, NULL);
	strcpy(cPath, cDrive);
	strcat(cPath, cDir);
	strcat(cPath, cFname);
	strcat(cPath, ".dat");
	CString m_strDataName = cPath;
	FILE *fp = fopen(m_strDataName, "wb");
	fwrite(pData, 1, (DWORD)(dwImgSize * fRatio / 8), fp);
	fclose(fp);

//.........................释放内存.................................
	if (pData != NULL)
	{	delete []pData;    pData = NULL;	}
	if (m_pImgPerm != NULL)
	{	delete []m_pImgPerm;	m_pImgPerm = NULL;	}

}


⌨️ 快捷键说明

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