📄 mywatermarkdlg.cpp
字号:
// MyWaterMarkDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyWaterMark.h"
#include "MyWaterMarkDlg.h"
#include "WvltTrans.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CString MarkFile;
extern unsigned char *lpDataMark ;//水印图像数据,无论是灰度图像还是彩色图像,此数组中每像素均占3字节
extern int* WaterMarkOrigin;//一维数组,存放原始水印信息(二值信息)
extern int* WaterMarkForLoad;
//水印全局变量
extern unsigned long MarkLength;//水印长度
extern unsigned long MarkHeight;//水印图像高像素数
extern unsigned long MarkWidth;//水印图像宽像素数
extern unsigned long bmSizeMark;
extern int* WaterMarkOrigin;//一维数组,存放原始水印信息(二值信息)
extern BYTE* pMarkData;//水印数据数组
extern BYTE* DistillMarkData;//提取出的水印数据
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyWaterMarkDlg dialog
CMyWaterMarkDlg::CMyWaterMarkDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyWaterMarkDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyWaterMarkDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyWaterMarkDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyWaterMarkDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyWaterMarkDlg, CDialog)
//{{AFX_MSG_MAP(CMyWaterMarkDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_MarkFile, OnMarkFile)
ON_BN_CLICKED(IDC_LoadMark, OnLoadMark)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_CheckMark, OnCheckMark)
ON_BN_CLICKED(IDC_LoadOne, OnLoadOne)
ON_BN_CLICKED(IDC_CheckOne, OnCheckOne)
ON_BN_CLICKED(IDC_PSNR, OnPsnr)
ON_BN_CLICKED(IDC_DistillOne, OnDistillOne)
ON_BN_CLICKED(IDC_DistillLib, OnDistillLib)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyWaterMarkDlg message handlers
BOOL CMyWaterMarkDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyWaterMarkDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMyWaterMarkDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyWaterMarkDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//读取水印文件并转化为原始水印信息
void CMyWaterMarkDlg::OnMarkFile()
{
CString strFilter = "BMP File (*.bmp)|*.bmp|";
//打开文件对话框
CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
strFilter, NULL);
if (IDOK == dlgOpen.DoModal())
{
MarkFile = dlgOpen.GetPathName();
}
CWvltTrans *pTrans=NULL;
//读取水印文件,得到水印宽高及水印长度等值,并为WaterMarkOrigin分配内存
pTrans->ReadBMPMark(MarkFile);
//pTrans->GrayToBinary(lpDataMark);//将水印数据lpDataMark二值化后放入WaterMarkOrigin中
//pTrans->LogisticWaterMark();//对WaterMarkOrigin进行混沌调制生成二值水印
pTrans->changeMarkValue();//得到原始水印信息
/*
//水印图像属性
MarkWidth = 8;//水印图像宽像素数,二值图像每像素1位
MarkHeight = 8;//水印图像高像素数
bmSizeMark = (DWORD)8;//水印二值图像大小字节数
MarkLength = MarkWidth * MarkHeight;//水印长度位数
//直接为原始水印赋初值
WaterMarkOrigin = new int [MarkLength];//一维数组,存放原始水印信息(二值信息)
for(int i=0; i<MarkLength; i++)
{
WaterMarkOrigin[i] = 1;
}
*/
}
//对目录下所有图像加载水印
void CMyWaterMarkDlg::OnLoadMark()
{
// TODO: Add your control notification handler code here
CString Title = "请选择待加载水印的目录";
CString loadPath = BrowseFolder(Title);//得到待加载水印的目录
CWvltTrans *pTrans=NULL;
pTrans->loadWaterMarkToLib(loadPath);
}
void CMyWaterMarkDlg::OnExit()
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();
}
//检测指定目录中的图像是否有水印
void CMyWaterMarkDlg::OnCheckMark()
{
// TODO: Add your control notification handler code here
CString Title = "请选择待检测图像的目录";
CString Path = BrowseFolder( Title);//得到待检测目录
//检测目录中的所有图像是否含有水印
CWvltTrans *pTrans = NULL;
pTrans->CheckWaterMarkToLib(Path);
}
CString CMyWaterMarkDlg::BrowseFolder( LPCTSTR lpTitle)
{
// 调用 SHBrowseForFolder 取得目录(文件夹)名称
// 参数 hWnd: 父窗口句柄
// 参数 lpTitle: 窗口标题
char szPath[MAX_PATH]={0}; BROWSEINFO m_bi;
m_bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
m_bi.hwndOwner = this->m_hWnd;
m_bi.pidlRoot = NULL;
m_bi.lpszTitle = lpTitle;
m_bi.lpfn = NULL;
m_bi.lParam = NULL;
m_bi.pszDisplayName = szPath;
LPITEMIDLIST pidl = ::SHBrowseForFolder( &m_bi );
if ( pidl )
{
if( !::SHGetPathFromIDList ( pidl, szPath ) )
szPath[0]=0;
IMalloc * pMalloc = NULL;
if ( SUCCEEDED ( ::SHGetMalloc( &pMalloc ) ) ) // 取得IMalloc分配器接口
{
pMalloc->Free( pidl ); // 释放内存
pMalloc->Release(); // 释放接口
}
}
return szPath;
}
//对单个图像加载水印
void CMyWaterMarkDlg::OnLoadOne()
{
// TODO: Add your control notification handler code here
CString bmpfile = "C:\\Documents and Settings\\fengsh\\桌面\\Wavelets_texture(BMP图像的小波变换)061013\\测试图像\\Sample1.bmp";
CString strFilter = "BMP File (*.bmp)|*.bmp|";
//打开文件对话框
AfxMessageBox("请选择要加载水印的文件");
CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
strFilter, NULL);
if (IDOK == dlgOpen.DoModal())
{
bmpfile = dlgOpen.GetPathName();
}
CWvltTrans *pTrans = NULL;
pTrans->LoadWaterMarkOne(bmpfile);
}
//检测单个图像是否含有水印
void CMyWaterMarkDlg::OnCheckOne()
{
// TODO: Add your control notification handler code here
CString checkfile = "D:\\MyWaterMark\\bird-0109.bmp";
CString strFilter = "BMP File (*.bmp)|*.bmp|";
//打开文件对话框
AfxMessageBox("请选择待检测的文件");
CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
strFilter, NULL);
if (IDOK == dlgOpen.DoModal())
{
checkfile = dlgOpen.GetPathName();
}
CWvltTrans *pTrans = NULL;
pTrans->CheckWaterMark(checkfile);
}
//计算加载水印后图像与原始图像的相似度psnr与提取出和水印图像与原始水印图像的相似度(用归一化相关系数表示)
void CMyWaterMarkDlg::OnPsnr()
{
//选择原始图像
CString Sourcefile = "";
CString strFilter = "BMP File (*.bmp)|*.bmp|";
//打开文件对话框
AfxMessageBox("请选择原始图像路径");
CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
strFilter, NULL);
if (IDOK == dlgOpen.DoModal())
{
Sourcefile = dlgOpen.GetPathName();
}
CString Thansfile = "";
//打开文件对话框
AfxMessageBox("请选择加水印后图像路径");
CFileDialog dlgOpen2(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY, strFilter, NULL);
if (IDOK == dlgOpen2.DoModal())
{
Thansfile = dlgOpen2.GetPathName();
}
int pos = Thansfile.ReverseFind('\\');
CString ThansfileName = Thansfile.Mid(pos+1);//加水印后文件名
CWvltTrans *pTrans = NULL;
float fpsnr = pTrans->psnrForOne(Sourcefile,Thansfile);//计算原始图像与加载了水印的图像的相似度
float fnc = pTrans->nc(pMarkData, DistillMarkData);//计算原始水印与提取出的水印的相似度
//将结果写到文件中
CString TextFilePath = Thansfile.Left(pos) + "\\resust.txt";
pTrans->WriteResultToFile(TextFilePath,fpsnr,fnc,ThansfileName);
}
//提取单个图像中的水印
void CMyWaterMarkDlg::OnDistillOne()
{
// TODO: Add your control notification handler code here
CString CheckFilePath = "";//待提取的图像路径
CString strFilter = "BMP File (*.bmp)|*.bmp|";
AfxMessageBox("请选择待提取的文件");
//打开文件对话框
CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
strFilter, NULL);
if (IDOK == dlgOpen.DoModal())
{
CheckFilePath = dlgOpen.GetPathName();
}
CWvltTrans *pTrans = NULL;
pTrans->DistillWaterMark(CheckFilePath);
}
void CMyWaterMarkDlg::OnDistillLib()
{
// TODO: Add your control notification handler code here
CString Title = "请选择待检测图像的目录";
CString Path = BrowseFolder( Title);//得到待检测目录
CWvltTrans *pTrans = NULL;
pTrans->DistillWaterMarkToLib(Path);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -