DataHiderView.cpp
上传用户:ynjin1970
上传日期:2014-10-13
资源大小:6438k
文件大小:9k
源码类别:

中间件编程

开发平台:

Visual C++

  1. // DataHiderView.cpp : implementation of the CDataHiderView class
  2. //
  3. #include "stdafx.h"
  4. #include "DataHider.h"
  5. #include "DataHiderDoc.h"
  6. #include "DataHiderView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CDataHiderView
  14. IMPLEMENT_DYNCREATE(CDataHiderView, CFormView)
  15. BEGIN_MESSAGE_MAP(CDataHiderView, CFormView)
  16. //{{AFX_MSG_MAP(CDataHiderView)
  17. ON_BN_CLICKED(IDC_BASEBMP, OnBasebmp)
  18. ON_BN_CLICKED(IDC_HIDEFILE, OnHidefile)
  19. ON_BN_CLICKED(IDC_DOHIDE, OnDohide)
  20. ON_BN_CLICKED(IDC_RESUMEDATA, OnResumedata)
  21. ON_BN_CLICKED(IDC_BASEFILE, OnBasefile)
  22. //}}AFX_MSG_MAP
  23. END_MESSAGE_MAP()
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CDataHiderView construction/destruction
  26. CDataHiderView::CDataHiderView()
  27. : CFormView(CDataHiderView::IDD)
  28. {
  29. //{{AFX_DATA_INIT(CDataHiderView)
  30. // NOTE: the ClassWizard will add member initialization here
  31. //}}AFX_DATA_INIT
  32. // TODO: add construction code here
  33. t=0.035; //融合系数
  34. }
  35. CDataHiderView::~CDataHiderView()
  36. {
  37. }
  38. void CDataHiderView::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CFormView::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CDataHiderView)
  42. // NOTE: the ClassWizard will add DDX and DDV calls here
  43. //}}AFX_DATA_MAP
  44. }
  45. BOOL CDataHiderView::PreCreateWindow(CREATESTRUCT& cs)
  46. {
  47. // TODO: Modify the Window class or styles here by modifying
  48. //  the CREATESTRUCT cs
  49. return CFormView::PreCreateWindow(cs);
  50. }
  51. void CDataHiderView::OnInitialUpdate()
  52. {
  53. CFormView::OnInitialUpdate();
  54. GetParentFrame()->RecalcLayout();
  55. ResizeParentToFit();
  56. }
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CDataHiderView diagnostics
  59. #ifdef _DEBUG
  60. void CDataHiderView::AssertValid() const
  61. {
  62. CFormView::AssertValid();
  63. }
  64. void CDataHiderView::Dump(CDumpContext& dc) const
  65. {
  66. CFormView::Dump(dc);
  67. }
  68. CDataHiderDoc* CDataHiderView::GetDocument() // non-debug version is inline
  69. {
  70. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDataHiderDoc)));
  71. return (CDataHiderDoc*)m_pDocument;
  72. }
  73. #endif //_DEBUG
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CDataHiderView message handlers
  76. void CDataHiderView::OnBasebmp() 
  77. {
  78. CFileDialog dlg(TRUE,"bmp","*.bmp",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"载体位图文件(*.bmp)|*.bmp||",NULL);
  79. if(dlg.DoModal()==IDOK)
  80. {
  81. m_bCanConv1=true;
  82. m_sCarrierFile=dlg.GetPathName();
  83. }
  84. else
  85. m_bCanConv1=false;
  86. }
  87. void CDataHiderView::OnHidefile() 
  88. {
  89. CFileDialog dlg(TRUE,"*","*.*",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"待隐藏文件(*.*)|*.*||",NULL);
  90. if(dlg.DoModal()==IDOK)
  91. {
  92. m_bCanConv2=true;
  93. m_sHideFile=dlg.GetPathName();
  94. m_sHideFileName=dlg.GetFileName();
  95. }
  96. else
  97. m_bCanConv2=false;
  98. }
  99. void CDataHiderView::OnDohide() 
  100. {
  101. CFile BaseFile,HideFile;
  102. DWORD BaseFileLen,HideFileLen;
  103. unsigned char* BaseData;
  104. unsigned char* HideData;
  105. if(m_bCanConv1==true && m_bCanConv2==true)
  106. {
  107. BaseFile.Open(m_sCarrierFile,CFile::modeReadWrite);
  108. BaseFileLen=BaseFile.GetLength();
  109. HideFile.Open(m_sHideFile,CFile::modeReadWrite);
  110. HideFileLen=HideFile.GetLength();
  111. //信息头:文件名*文件长*
  112. CString InfoHead;
  113. InfoHead.Format("%s*%d*",m_sHideFileName,HideFileLen);
  114. int HeadLen=InfoHead.GetLength();
  115. if(BaseFileLen<((HeadLen+HideFileLen)*8)/3+100)
  116. {
  117. AfxMessageBox("待隐藏文件过大!请换一较大载体位图文件。");
  118. return;
  119. }
  120. BaseData=new unsigned char[BaseFileLen];
  121. BaseFile.Read(BaseData,BaseFileLen);
  122. HideData=new unsigned char[BaseFileLen];
  123. //对明文进行组帧
  124. for(int k=0;k<HeadLen;k++)
  125. HideData[k]=InfoHead.GetAt(k);
  126. HideFile.Read(HideData+HeadLen,HideFileLen);
  127. for(DWORD kk=HeadLen+HideFileLen;kk<BaseFileLen;kk++)
  128. HideData[kk]=0;
  129. if(BaseData[28]!=24)
  130. {
  131. AfxMessageBox("载体文件格式错误!");
  132. BaseFile.Close();
  133. if(BaseData!=NULL)
  134. delete BaseData;
  135. return;
  136. }
  137. unsigned char Temp=0;
  138. DWORD j=0;
  139. for(DWORD i=54;i<BaseFileLen;i++)
  140. {
  141. Temp=((HideData[j]&224)>>5)<<5; //1
  142. BaseData[i]=HideCalculate(BaseData[i],Temp);
  143. i++;
  144. Temp=((HideData[j]&28)>>2)<<5; //2
  145. BaseData[i]=HideCalculate(BaseData[i],Temp);
  146. i++;
  147. Temp=(HideData[j]&3)<<1; //3
  148. j++;
  149. Temp|=(HideData[j]&128)>>7;
  150. Temp*=32;
  151. BaseData[i]=HideCalculate(BaseData[i],Temp);
  152. i++;
  153. Temp=((HideData[j]&112)>>4)<<5; //4
  154. BaseData[i]=HideCalculate(BaseData[i],Temp);
  155. i++;
  156. Temp=((HideData[j]&14)>>1)<<5; //5
  157. BaseData[i]=HideCalculate(BaseData[i],Temp);
  158. i++;
  159. Temp=(HideData[j]&1)<<2; //6
  160. j++;
  161. Temp|=(HideData[j]&192)>>6;
  162. Temp*=32;
  163. BaseData[i]=HideCalculate(BaseData[i],Temp);
  164. i++;
  165. Temp=((HideData[j]&56)>>3)<<5; //7
  166. BaseData[i]=HideCalculate(BaseData[i],Temp);
  167. i++;
  168. Temp=(HideData[j]&7)<<5; //8
  169. BaseData[i]=HideCalculate(BaseData[i],Temp);
  170. j++;
  171. }
  172. BaseFile.Close();
  173. HideFile.Close();
  174. CFile file;
  175. file.Open("融合图像.bmp",CFile::modeCreate|CFile::modeReadWrite);
  176. file.Write(BaseData,BaseFileLen);
  177. file.Close();
  178. if(BaseData!=NULL)
  179. delete BaseData;
  180. if(HideData!=NULL)
  181. delete HideData;
  182. }
  183. }
  184. void CDataHiderView::OnBasefile() 
  185. {
  186. CFileDialog dlg(TRUE,"bmp","*.bmp",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"原始文件(*.bmp)|*.bmp||",NULL);
  187. if(dlg.DoModal()==IDOK)
  188. {
  189. m_bCanConv3=true;
  190. m_sBaseFile=dlg.GetPathName();
  191. }
  192. else
  193. m_bCanConv3=false;
  194. }
  195. void CDataHiderView::OnResumedata() 
  196. {
  197. CFile BaseFile;
  198. CFile CarrierFile;
  199. DWORD BaseFileLen;
  200. DWORD CarrierFileLen;
  201. unsigned char* BaseData;
  202. unsigned char* CarrierData;
  203. if(m_bCanConv1==true && m_bCanConv3==true)
  204. {
  205. CarrierFile.Open(m_sCarrierFile,CFile::modeReadWrite);
  206. CarrierFileLen=CarrierFile.GetLength();
  207. BaseFile.Open(m_sBaseFile,CFile::modeReadWrite);
  208. BaseFileLen=BaseFile.GetLength();
  209. if(BaseFileLen!=CarrierFileLen)
  210. {
  211. AfxMessageBox("选取的不是充作载体的原始文件!");
  212. CarrierFile.Close();
  213. BaseFile.Close();
  214. return;
  215. }
  216. CarrierData=new unsigned char[CarrierFileLen];
  217. CarrierFile.Read(CarrierData,CarrierFileLen);
  218. BaseData=new unsigned char[CarrierFileLen];
  219. BaseFile.Read(BaseData,CarrierFileLen);
  220. if(CarrierData[28]!=24)
  221. {
  222. AfxMessageBox("载体文件格式错误!");
  223. CarrierFile.Close();
  224. BaseFile.Close();
  225. if(CarrierData!=NULL)
  226. delete CarrierData;
  227. if(BaseData!=NULL)
  228. delete BaseData;
  229. return;
  230. }
  231. if(BaseData[28]!=24)
  232. {
  233. AfxMessageBox("原始文件格式错误!");
  234. CarrierFile.Close();
  235. BaseFile.Close();
  236. if(CarrierData!=NULL)
  237. delete CarrierData;
  238. if(BaseData!=NULL)
  239. delete BaseData;
  240. return;
  241. }
  242. unsigned char Temp=0;
  243. DWORD j=0;
  244. for(DWORD i=54;i<CarrierFileLen;i++)
  245. {
  246. Temp=(ShowCalculate(CarrierData[i],BaseData[i])&7)<<5;
  247. i++;
  248. Temp|=(ShowCalculate(CarrierData[i],BaseData[i])&7)<<2;
  249. i++;
  250. Temp|=(ShowCalculate(CarrierData[i],BaseData[i])&6)>>1;
  251. CarrierData[j]=Temp;
  252. j++;
  253. Temp=(ShowCalculate(CarrierData[i],BaseData[i])&1)<<7;
  254. i++;
  255. Temp|=(ShowCalculate(CarrierData[i],BaseData[i])&7)<<4;
  256. i++;
  257. Temp|=(ShowCalculate(CarrierData[i],BaseData[i])&7)<<1;
  258. i++;
  259. Temp|=(ShowCalculate(CarrierData[i],BaseData[i])&4)>>2;
  260. CarrierData[j]=Temp;
  261. j++;
  262. Temp=(ShowCalculate(CarrierData[i],BaseData[i])&3)<<6;
  263. i++;
  264. Temp|=(ShowCalculate(CarrierData[i],BaseData[i])&7)<<3;
  265. i++;
  266. Temp|=ShowCalculate(CarrierData[i],BaseData[i])&7;
  267. CarrierData[j]=Temp;
  268. j++;
  269. }
  270. BaseFile.Close();
  271. CarrierFile.Close();
  272. j=0;
  273. CString FileName="";
  274. while(true)
  275. {
  276. if(CarrierData[j]=='*')
  277. {
  278. j++;
  279. break;
  280. }
  281. else
  282. {
  283. FileName+=CString(CarrierData[j]);
  284. j++;
  285. }
  286. }
  287. CString FileLen="";
  288. while(true)
  289. {
  290. if(CarrierData[j]=='*')
  291. {
  292. j++;
  293. break;
  294. }
  295. else
  296. {
  297. FileLen+=CString(CarrierData[j]);
  298. j++;
  299. }
  300. }
  301. BaseFileLen=atoi(FileLen);
  302. CFile file;
  303. file.Open(FileName,CFile::modeCreate|CFile::modeReadWrite);
  304. file.Write(CarrierData+j,BaseFileLen);
  305. file.Close();
  306. if(BaseData!=NULL)
  307. delete BaseData;
  308. if(CarrierData!=NULL)
  309. delete CarrierData;
  310. }
  311. }
  312. unsigned char CDataHiderView::HideCalculate(unsigned char B, unsigned char H)
  313. {
  314. unsigned char R=(int)((1-t)*B+0.5f)+(int)(t*H+0.5f);
  315. return R;
  316. }
  317. unsigned char CDataHiderView::ShowCalculate(unsigned char C, unsigned char B)
  318. {
  319. unsigned char R=(int)((C- (int)((1-t)*B+0.5f))/t+0.5f);
  320. if(R%32>15)
  321. R+=15;
  322. R=(int)(R/32.0f+0.5f);
  323. return R;
  324. }