小波变换View.cpp
上传用户:aqingfeng
上传日期:2014-03-25
资源大小:1839k
文件大小:32k
源码类别:

波变换

开发平台:

Visual C++

  1. // 小波变换View.cpp : implementation of the CMyView class
  2. //
  3. #include "stdafx.h"
  4. #include "小波变换.h"
  5. #include "小波变换Doc.h"
  6. #include "小波变换View.h"
  7. #include "dibapi.h"
  8. #include "math.h"
  9. #include "waveletlevel.h"
  10. #include "WaveletCoi.h"
  11. #include "Daubechiesdlg.h"
  12. #include "WaveTranform.h"
  13. #include "HebingDlg.h"
  14. #include "Symletsdlg.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CMyView
  22. IMPLEMENT_DYNCREATE(CMyView, CView)
  23. BEGIN_MESSAGE_MAP(CMyView, CView)
  24. //{{AFX_MSG_MAP(CMyView)
  25. ON_COMMAND(ID_COMPRESSION, OnCompression)
  26. ON_COMMAND(ID_DISCOMPRESSION, OnDiscompression)
  27. ON_COMMAND(ID_WAVELETCONSTRUCTION, OnWaveletconstruction)
  28. ON_COMMAND(IDM_DIPROC_FUSION2, OnDiprocFusion2)
  29. ON_COMMAND(IDM_DIPROC_FUSION3, OnDiprocFusion3)
  30. ON_COMMAND(IDM_DIPROC_FUSION4, OnDiprocFusion4)
  31.  ON_COMMAND(IDM_IMAGE_HEBING, OnImageHebing)
  32.  ON_COMMAND(ID_DETECT_TEMPLATE, OnDetectTemplate)
  33. //}}AFX_MSG_MAP
  34. // Standard printing commands
  35. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  36. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  37. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CMyView construction/destruction
  41. CMyView::CMyView()
  42. {
  43. // TODO: add construction code here
  44. m_nLevel=1;
  45. FWidth=0;
  46. wavefunction=-1;
  47. }
  48. CMyView::~CMyView()
  49. {
  50. }
  51. BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
  52. {
  53. // TODO: Modify the Window class or styles here by modifying
  54. //  the CREATESTRUCT cs
  55. return CView::PreCreateWindow(cs);
  56. }
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CMyView drawing
  59. void CMyView::OnDraw(CDC* pDC)
  60. {
  61. BeginWaitCursor();
  62. CMyDoc* pDoc = GetDocument();
  63. ASSERT_VALID(pDoc);
  64. // TODO: add draw code for native data here
  65. // 获取DIB
  66. HDIB hDIB = pDoc->GetHDIB();
  67.     HDIB m_hMulDIB=pDoc->m_hMulDIB;
  68. // 判断DIB是否为空
  69. if (hDIB != NULL)
  70. {
  71. LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
  72. // 获取DIB宽度
  73. int cxDIB = (int) ::DIBWidth(lpDIB);
  74. // 获取DIB高度
  75. int cyDIB = (int) ::DIBHeight(lpDIB);
  76. ::GlobalUnlock((HGLOBAL) hDIB);
  77. CRect rcDIB;
  78. rcDIB.top = rcDIB.left = 0;
  79. rcDIB.right = cxDIB;
  80. rcDIB.bottom = cyDIB;
  81. // 输出DIB
  82. ::PaintDIB(pDC->m_hDC, &rcDIB, pDoc->GetHDIB(),
  83. &rcDIB, pDoc->GetDocPalette());
  84. }
  85. if (m_hMulDIB!= NULL)
  86. {
  87. LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)m_hMulDIB);
  88. // 获取DIB宽度
  89. int cxDIB = (int) ::DIBWidth(lpDIB);
  90. // 获取DIB高度
  91. int cyDIB = (int) ::DIBHeight(lpDIB);
  92. ::GlobalUnlock((HGLOBAL)m_hMulDIB);
  93. CRect rcDIB;
  94. rcDIB.top =0;
  95. rcDIB.left =600;
  96. rcDIB.right = cxDIB;
  97. rcDIB.bottom = cyDIB;
  98. // 输出DIB
  99. ::PaintDIB(pDC->m_hDC, &rcDIB, pDoc->m_hMulDIB,
  100. &rcDIB, pDoc->GetDocPalette());
  101. }
  102. // 恢复正常光标
  103. }
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CMyView printing
  106. BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
  107. {
  108. // default preparation
  109. return DoPreparePrinting(pInfo);
  110. }
  111. void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  112. {
  113. // TODO: add extra initialization before printing
  114. }
  115. void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  116. {
  117. // TODO: add cleanup after printing
  118. }
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CMyView diagnostics
  121. #ifdef _DEBUG
  122. void CMyView::AssertValid() const
  123. {
  124. CView::AssertValid();
  125. }
  126. void CMyView::Dump(CDumpContext& dc) const
  127. {
  128. CView::Dump(dc);
  129. }
  130. CMyDoc* CMyView::GetDocument() // non-debug version is inline
  131. {
  132. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
  133. return (CMyDoc*)m_pDocument;
  134. }
  135. #endif //_DEBUG
  136. /////////////////////////////////////////////////////////////////////////////
  137. // CMyView message handlers
  138. void CMyView::OnWaveletconstruction() 
  139. {
  140. // TODO: Add your command handler code here
  141. //所谓的构建,其实是已经将数据存入了数组里。
  142. BeginWaitCursor();
  143. CMyDoc* pDoc = GetDocument();
  144. Ld = new double[20];  //分解尺度函数
  145. Hd = new double[20];  //分解母函数
  146. Lr = new double[20];  //重建尺度函数
  147. Hr = new double[20];  //重建母函数
  148. CWaveletCoi waveletcoi;
  149. if(waveletcoi.DoModal()==IDOK)
  150. {
  151. FWidth=waveletcoi.m_waveletlength;
  152. wavefunction=waveletcoi.m_waveletfunction;
  153. switch(wavefunction)
  154. {
  155. case 0://IDC_SHANNON
  156. {
  157. Ld[0]=1/sqrt(2);
  158. Ld[1]=1/sqrt(2);
  159. break;
  160. }
  161. case 1://IDC_DAUBECHIES
  162. {
  163. switch(FWidth)
  164. {
  165. case 2:
  166. Ld[0]=1/sqrt(2); Ld[1]=1/sqrt(2);
  167. m_wavetransform.m_preoffset=0;
  168. m_wavetransform.m_aftoffset=1;
  169. break;
  170. case 4:
  171. Ld[0]=0.4829629131445341; Ld[1]=0.8365163037378077; Ld[2]=0.2241438680420134;
  172. Ld[3]=-0.1294095225512603;
  173. m_wavetransform.m_preoffset=0;
  174. m_wavetransform.m_aftoffset=3;
  175. break;
  176. case 6:
  177. Ld[0]=0.3326705529500825; Ld[1]=0.8068915093110924; Ld[2]=0.4598775021184914;
  178. Ld[3]=-0.1350110200102546; Ld[4]=-0.0854412738820267; Ld[5]=0.035226218857095;
  179. m_wavetransform.m_preoffset=1;
  180. m_wavetransform.m_aftoffset=4;
  181. break;
  182. case 8:
  183. Ld[0]=0.230377813309; Ld[1]=0.714846570553; Ld[2]=0.630880767930;
  184. Ld[3]=-0.027983769417; Ld[4]=-0.187034811719; Ld[5]=0.030841381836;
  185. Ld[6]=0.032883011667; Ld[7]=-0.010597401785;
  186. m_wavetransform.m_preoffset=1;
  187. m_wavetransform.m_aftoffset=6;
  188. break;
  189. case 10:
  190. Ld[0]=0.160102397974; Ld[1]=0.603829269797; Ld[2]=0.724308528438; 
  191. Ld[3]=0.138428145901; Ld[4]=-0.242294887066; Ld[5]=-0.032244869585;
  192. Ld[6]=0.077571493840; Ld[7]=-0.006241490213; Ld[8]=-0.012580751999;
  193. Ld[9]=0.003335725285;
  194. m_wavetransform.m_preoffset=1;
  195. m_wavetransform.m_aftoffset=8;
  196. break;
  197. case 12:
  198. Ld[0]=0.111540743350;   Ld[1]=0.494623890398; Ld[2]=0.751133908021; 
  199. Ld[3]=0.315250351709; Ld[4]=-0.226264693965; Ld[5]=-0.129766867567;
  200. Ld[6]=0.097501605587; Ld[7]=0.027522865530; Ld[8]=-0.031582039318;
  201. Ld[9]=0.000553842201; Ld[10]=0.004777257511; Ld[11]=-0.001077301085;
  202. m_wavetransform.m_preoffset=1;
  203. m_wavetransform.m_aftoffset=10;
  204. break;
  205. case 14:
  206. Ld[0]=0.077852054085; Ld[1]=0.396539319482; Ld[2]=0.729132090846;
  207. Ld[3]=0.469782287405; Ld[4]=-0.143906003929; Ld[5]=-0.224036184994;
  208. Ld[6]=0.071309219267; Ld[7]=0.080612609151; Ld[8]=-0.038029936935;
  209. Ld[9]=-0.016574541631; Ld[10]=0.012550998556; Ld[11]=0.000429577973;
  210. Ld[12]=-0.001801640704; Ld[13]=0.000353713800;
  211. m_wavetransform.m_preoffset=1;
  212. m_wavetransform.m_aftoffset=12;
  213. break;
  214. case 16:
  215. Ld[0]=0.054415842243; Ld[1]=0.312871590914; Ld[2]=0.675630736297;
  216. Ld[3]=0.585354683654; Ld[4]=-0.015829105256; Ld[5]=-0.284015542962;
  217. Ld[6]=0.000472484574; Ld[7]=0.128747426620; Ld[8]=-0.017369301002;
  218. Ld[9]=-0.044088253931; Ld[10]=0.013981027917; Ld[11]=0.008746094047;
  219. Ld[12]=-0.004870352993; Ld[13]=-0.000391740373; Ld[14]=0.000675449406;
  220. Ld[15]=-0.000117476784;
  221. m_wavetransform.m_preoffset=1;
  222. m_wavetransform.m_aftoffset=14;
  223. break;
  224. case 18:
  225. Ld[0]=0.038077947364; Ld[1]=0.243834674613; Ld[2]=0.604823123690;
  226. Ld[3]=0.657288078051; Ld[4]=0.133197385825; Ld[5]=-0.293273783279;
  227. Ld[6]=-0.096840783223; Ld[7]=0.148540749338; Ld[8]=0.030725681479;
  228. Ld[9]=-0.067632829061; Ld[10]=0.000250947115; Ld[11]=0.022361662124;
  229. Ld[12]=-0.004723204758; Ld[13]=-0.004281503682; Ld[14]=0.001847646883;
  230. Ld[15]=0.000230385764; Ld[16]=-0.000251963189; Ld[17]=0.000039347320;
  231. m_wavetransform.m_preoffset=2;
  232. m_wavetransform.m_aftoffset=15;
  233. break;
  234. case 20:
  235. Ld[0]=0.026670057901; Ld[1]=0.188176800078; Ld[2]=0.527201188932;
  236. Ld[3]=0.688459039454; Ld[4]=0.281172343661; Ld[5]=-0.249846424327;
  237. Ld[6]=-0.195946274377; Ld[7]=0.127369340336; Ld[8]=0.093057364604;
  238. Ld[9]=-0.071394147166; Ld[10]=-0.029457536822; Ld[11]=0.033212674059;
  239. Ld[12]=0.003606553567; Ld[13]=-0.010733175483; Ld[14]=0.001395351747;
  240. Ld[15]=0.001992405295; Ld[16]=-0.000685856695; Ld[17]=-0.000116466855;
  241. Ld[18]=0.000093588670; Ld[19]=-0.000013264203;
  242. m_wavetransform.m_preoffset=2;
  243. m_wavetransform.m_aftoffset=17;
  244. break;
  245. }
  246. }
  247. break;
  248. case 2://IDC_MORLET
  249. break;
  250. case 3://IDC_MEXICANHAT
  251. break;
  252. case 4://IDC_MEYER
  253. break;
  254. case 5://IDC_BSPLINE
  255. break;
  256. case 6://IDC_SYMLETS
  257. {
  258. switch(FWidth)
  259. {
  260. case 8:
  261. Ld[0]=-0.107148901418/sqrt(2); Ld[1]=-0.041910965126/sqrt(2); Ld[2]=0.703739068656/sqrt(2);
  262. Ld[3]=1.136658243409/sqrt(2); Ld[4]=0.421234534204/sqrt(2); Ld[5]=-0.140317624179/sqrt(2);
  263. Ld[6]=-0.017824701442/sqrt(2); Ld[7]=0.045570345896/sqrt(2);
  264. m_wavetransform.m_preoffset=3;
  265. m_wavetransform.m_aftoffset=4;
  266. break;
  267. case 10:
  268. Ld[0]=0.038654795955/sqrt(2); Ld[1]=0.041746864422/sqrt(2); Ld[2]=-0.055344186117/sqrt(2);
  269. Ld[3]=0.281990696854/sqrt(2); Ld[4]=1.023052966894/sqrt(2); Ld[5]=0.896581648380/sqrt(2);
  270. Ld[6]=0.023478923136/sqrt(2); Ld[7]=-0.247951362613/sqrt(2); Ld[8]=-0.029842499869/sqrt(2);
  271. Ld[9]=0.027632152958/sqrt(2);
  272. m_wavetransform.m_preoffset=4;
  273. m_wavetransform.m_aftoffset=5;
  274. break;
  275. case 12:
  276. Ld[0]=0.021784700327/sqrt(2); Ld[1]=0.004936612372/sqrt(2); Ld[2]=-0.166863215412/sqrt(2);
  277. Ld[3]=-0.068323121587/sqrt(2); Ld[4]=0.0694457972958/sqrt(2); Ld[5]=1.113892783926/sqrt(2);
  278. Ld[6]=0.477904371333/sqrt(2); Ld[7]=-0.102724969862/sqrt(2); Ld[8]=-0.029783751299/sqrt(2);
  279. Ld[9]=0.063250562660/sqrt(2); Ld[10]=0.002499922093/sqrt(2); Ld[11]=-0.011031867509/sqrt(2);
  280. m_wavetransform.m_preoffset=5;
  281. m_wavetransform.m_aftoffset=6;
  282. break;
  283. case 14:
  284. Ld[0]=0.003792658534/sqrt(2); Ld[1]=-0.001481225915/sqrt(2); Ld[2]=-0.017870431651/sqrt(2);
  285. Ld[3]=0.043155452582/sqrt(2); Ld[4]=0.096014767936/sqrt(2); Ld[5]=-0.070078291222/sqrt(2);
  286. Ld[6]=0.024665659489/sqrt(2); Ld[7]=0.758162601964/sqrt(2); Ld[8]=1.085782709814/sqrt(2);
  287. Ld[9]=0.408183939725/sqrt(2); Ld[10]=-0.198056706807/sqrt(2); Ld[11]=-0.152463872896/sqrt(2);
  288. Ld[12]=0.005671342686/sqrt(2); Ld[13]=0.014521394762/sqrt(2);
  289. m_wavetransform.m_preoffset=7;
  290. m_wavetransform.m_aftoffset=6;
  291. break;
  292. case 16:
  293. Ld[0]=0.002672793393/sqrt(2); Ld[1]=-0.000428394300/sqrt(2); Ld[2]=-0.021145686528/sqrt(2);
  294. Ld[3]=0.005386388754/sqrt(2); Ld[4]=0.069490465911/sqrt(2); Ld[5]=-0.038493521263/sqrt(2);
  295. Ld[6]=-0.073462508761/sqrt(2); Ld[7]=0.515398670374/sqrt(2); Ld[8]=1.099106630537/sqrt(2);
  296. Ld[9]=0.680745347190/sqrt(2); Ld[10]=-0.086653615406/sqrt(2); Ld[11]=-0.202648655286/sqrt(2);
  297. Ld[12]=0.010758611751/sqrt(2); Ld[13]=0.044823623042/sqrt(2); Ld[14]=-0.000766690896/sqrt(2);
  298. Ld[15]=-0.004783458512/sqrt(2);
  299. m_wavetransform.m_preoffset=8;
  300. m_wavetransform.m_aftoffset=7;
  301. break;
  302. case 18:
  303. Ld[0]=0.001512487309/sqrt(2); Ld[1]=-0.000669141509/sqrt(2); Ld[2]=-0.014515578553/sqrt(2);
  304. Ld[3]=0.012528896242/sqrt(2); Ld[4]=0.087791251554/sqrt(2); Ld[5]=-0.025786445930/sqrt(2);
  305. Ld[6]=-0.270893783503/sqrt(2); Ld[7]=0.049882830959/sqrt(2); Ld[8]=0.873048407349/sqrt(2);
  306. Ld[9]=1.015259790832/sqrt(2); Ld[10]=0.337658923602/sqrt(2); Ld[11]=-0.077172161097/sqrt(2);
  307. Ld[12]=0.000825140929/sqrt(2); Ld[13]=0.042744433602/sqrt(2); Ld[14]=-0.016303351226/sqrt(2);
  308. Ld[15]=-0.018769396836/sqrt(2); Ld[16]=0.000876502539/sqrt(2); Ld[17]=0.001981193736/sqrt(2);
  309. m_wavetransform.m_preoffset=8;
  310. m_wavetransform.m_aftoffset=9;
  311. break;
  312. case 20:
  313. Ld[0]=0.001089170447/sqrt(2); Ld[1]=0.000135245020/sqrt(2); Ld[2]=-0.012220642630/sqrt(2);
  314. Ld[3]=-0.002072363923/sqrt(2); Ld[4]=0.064950924579/sqrt(2); Ld[5]=0.016418869426/sqrt(2);
  315. Ld[6]=-0.225558972234/sqrt(2); Ld[7]=-0.100240215031/sqrt(2); Ld[8]=0.667071338154/sqrt(2);
  316. Ld[9]=1.088251530500/sqrt(2); Ld[10]=0.542813011213/sqrt(2); Ld[11]=-0.050256540092/sqrt(2);
  317. Ld[12]=-0.045240772218/sqrt(2); Ld[13]=0.070703567550/sqrt(2); Ld[14]=0.008152816799/sqrt(2);
  318. Ld[15]=-0.028786231926/sqrt(2); Ld[16]=-0.001137535314/sqrt(2); Ld[17]=0.006495728375/sqrt(2);
  319. Ld[18]=0.000080661204/sqrt(2); Ld[19]=-0.000649589896/sqrt(2);
  320. m_wavetransform.m_preoffset=9;
  321. m_wavetransform.m_aftoffset=10;
  322. break;
  323. }
  324. }
  325. break;
  326. case 7://IDC_COIFLETS
  327. Ld[0]=-0.0802861503271*sqrt(2); Ld[1]=-0.0243085969067*sqrt(2); Ld[2]=0.362806341593*sqrt(2);
  328. Ld[3]=0.550576616156*sqrt(2); Ld[4]=0.229036357075*sqrt(2); Ld[5]=-0.0644368523121*sqrt(2);
  329. Ld[6]=-0.0115565483406*sqrt(2); Ld[7]=0.0381688330633*sqrt(2);
  330. m_wavetransform.m_preoffset=3;
  331. m_wavetransform.m_aftoffset=4;
  332. break;
  333. }
  334. for(int i=0;i<FWidth;i++)
  335. {
  336. Hd[i]=pow(-1,i+1)*Ld[-i-1+FWidth];
  337. }
  338. for(i=0;i<FWidth;i++)
  339. {
  340. Lr[i]=Ld[-i-1+FWidth];
  341. Hr[i]=Hd[-i-1+FWidth];
  342. }
  343. }
  344. pDoc->UpdateAllViews(NULL);
  345. EndWaitCursor();
  346. }
  347. void CMyView::OnCompression() 
  348. {
  349. // TODO: Add your command handler code here
  350. BeginWaitCursor();
  351. CMyDoc* pDoc = GetDocument();
  352. LPSTR lpDIB;
  353. // 指向DIB象素指针
  354. LPSTR    lpDIBBits;
  355. // 锁定DIB
  356. lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
  357. // 找到DIB图像象素起始位置
  358. lpDIBBits = ::FindDIBBits(lpDIB);
  359. CWaveletLevel leveldlg;
  360. if(leveldlg.DoModal()==IDOK)
  361. {
  362. m_nLevel=leveldlg.m_nlevel;
  363. //分解
  364. m_wavetransform.DIBWavelet(lpDIB,lpDIBBits,Ld,Hd,FWidth,m_nLevel);
  365. pDoc->SetModifiedFlag(TRUE);
  366. pDoc->UpdateAllViews(NULL);
  367. }
  368. // 解除锁定
  369. ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
  370. EndWaitCursor();
  371. }
  372. void CMyView::OnDiscompression() 
  373. {
  374. // TODO: Add your command handler code here
  375. CMyDoc* pDoc = GetDocument();
  376. LPSTR lpDIB;
  377. // 指向DIB象素指针
  378. LPSTR    lpDIBBits;
  379. // 锁定DIB
  380. lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
  381. // 找到DIB图像象素起始位置
  382. lpDIBBits = ::FindDIBBits(lpDIB);
  383. //重建
  384. m_wavetransform.DIBDisWavelet(lpDIB,lpDIBBits,Lr,Hr,FWidth,m_nLevel);
  385. pDoc->SetModifiedFlag(TRUE);
  386. pDoc->UpdateAllViews(NULL);
  387. // 解除锁定
  388. ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
  389. }
  390. void CMyView::OnDiprocFusion2()
  391. {   
  392. // TODO: Add your command handler code here
  393. //读取数字图像的文件头,获取图像的属性参数
  394.     CMyDoc* pDoc = GetDocument();
  395. LPSTR lpDIB1;
  396. // 指向DIB象素指针
  397. LPSTR    lpDIBBits1;
  398. LPSTR lpDIB2;
  399. // 指向DIB象素指针
  400. LPSTR    lpDIBBits2;
  401. // 锁定DIB
  402. lpDIB1 = (LPSTR) ::GlobalLock((HGLOBAL)pDoc->GetHDIB());
  403. // 找到DIB图像象素起始位置
  404. lpDIBBits1 = ::FindDIBBits(lpDIB1);
  405. unsigned long lWidth =::DIBWidth(lpDIB1);
  406. unsigned long lHeight = ::DIBHeight(lpDIB1);
  407.     Dwvlttrans(lpDIB1,lpDIBBits1);
  408. pDoc->SetModifiedFlag(TRUE);
  409. pDoc->UpdateAllViews(NULL);
  410. pDoc->Openanother();
  411. pDoc->SetModifiedFlag(TRUE);
  412.   //显示新打开的图像
  413.   pDoc->UpdateAllViews(NULL);
  414. lpDIB2 = (LPSTR) ::GlobalLock((HGLOBAL)pDoc->m_hMulDIB);
  415. // 找到DIB图像象素起始位置
  416. lpDIBBits2 = ::FindDIBBits(lpDIB2);
  417.     Dwvlttrans(lpDIB2,lpDIBBits2);
  418. pDoc->SetModifiedFlag(TRUE);
  419. pDoc->UpdateAllViews(NULL);
  420.    m_wavetransform.DIP_ImageFusion2(lpDIB1,lpDIBBits1,lpDIB2,lpDIBBits2,m_nLevel);
  421. pDoc->SetModifiedFlag(TRUE);
  422. pDoc->UpdateAllViews(NULL);
  423.     ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
  424. ::GlobalUnlock((HGLOBAL) pDoc->m_hMulDIB);
  425.     lpDIB1 = (LPSTR) ::GlobalLock((HGLOBAL)pDoc->GetHDIB() );
  426. // 找到DIB图像象素起始位置
  427. lpDIBBits1 = ::FindDIBBits(lpDIB1);
  428.     m_wavetransform.DIBDisWavelet(lpDIB1,lpDIBBits1,Lr,Hr,FWidth,m_nLevel);
  429. pDoc->SetModifiedFlag(TRUE);
  430. pDoc->UpdateAllViews(NULL);
  431. // 解除锁定
  432. ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
  433. ::GlobalUnlock((HGLOBAL) pDoc->m_hMulDIB);
  434. }
  435. void CMyView::OnDiprocFusion3()
  436. {
  437.   CMyDoc* pDoc = GetDocument();
  438. LPSTR lpDIB1;
  439. // 指向DIB象素指针
  440. LPSTR    lpDIBBits1;
  441. LPSTR lpDIB2;
  442. // 指向DIB象素指针
  443. LPSTR    lpDIBBits2;
  444. // 锁定DIB
  445. lpDIB1 = (LPSTR) ::GlobalLock((HGLOBAL)pDoc->GetHDIB());
  446. // 找到DIB图像象素起始位置
  447. lpDIBBits1 = ::FindDIBBits(lpDIB1);
  448. unsigned long lWidth =::DIBWidth(lpDIB1);
  449. unsigned long lHeight = ::DIBHeight(lpDIB1);
  450.     Swvlttrans(lpDIB1,lpDIBBits1);
  451. pDoc->SetModifiedFlag(TRUE);
  452. pDoc->UpdateAllViews(NULL);
  453. pDoc->Openanother();
  454. pDoc->SetModifiedFlag(TRUE);
  455.   //显示新打开的图像
  456.   pDoc->UpdateAllViews(NULL);
  457. lpDIB2 = (LPSTR) ::GlobalLock((HGLOBAL)pDoc->m_hMulDIB);
  458. // 找到DIB图像象素起始位置
  459. lpDIBBits2 = ::FindDIBBits(lpDIB2);
  460.     Swvlttrans(lpDIB2,lpDIBBits2);
  461. pDoc->SetModifiedFlag(TRUE);
  462. pDoc->UpdateAllViews(NULL);
  463.    m_wavetransform.DIP_ImageFusion2(lpDIB1,lpDIBBits1,lpDIB2,lpDIBBits2,m_nLevel);
  464. pDoc->SetModifiedFlag(TRUE);
  465. pDoc->UpdateAllViews(NULL);
  466.     ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
  467. ::GlobalUnlock((HGLOBAL) pDoc->m_hMulDIB);
  468.     lpDIB1 = (LPSTR) ::GlobalLock((HGLOBAL)pDoc->GetHDIB() );
  469. // 找到DIB图像象素起始位置
  470. lpDIBBits1 = ::FindDIBBits(lpDIB1);
  471.     m_wavetransform.DIBDisWavelet(lpDIB1,lpDIBBits1,Lr,Hr,FWidth,m_nLevel);
  472. pDoc->SetModifiedFlag(TRUE);
  473. pDoc->UpdateAllViews(NULL);
  474. // 解除锁定
  475. ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
  476. ::GlobalUnlock((HGLOBAL) pDoc->m_hMulDIB);
  477. }
  478. void CMyView::OnDiprocFusion4()
  479. {
  480. // TODO: Add your command handler code here
  481. //读取数字图像的文件头,获取图像的属性参数
  482.    CMyDoc* pDoc = GetDocument();
  483. LPSTR lpDIB1;
  484. // 指向DIB象素指针
  485. LPSTR    lpDIBBits1;
  486. LPSTR lpDIB2;
  487. // 指向DIB象素指针
  488. LPSTR    lpDIBBits2;
  489. // 锁定DIB
  490. lpDIB1 = (LPSTR) ::GlobalLock((HGLOBAL)pDoc->GetHDIB());
  491. // 找到DIB图像象素起始位置
  492. lpDIBBits1 = ::FindDIBBits(lpDIB1);
  493. unsigned long lWidth =::DIBWidth(lpDIB1);
  494. unsigned long lHeight = ::DIBHeight(lpDIB1);
  495.     Cwvlttrans(lpDIB1,lpDIBBits1);
  496. pDoc->SetModifiedFlag(TRUE);
  497. pDoc->UpdateAllViews(NULL);
  498. pDoc->Openanother();
  499. pDoc->SetModifiedFlag(TRUE);
  500.   //显示新打开的图像
  501.   pDoc->UpdateAllViews(NULL);
  502. lpDIB2 = (LPSTR) ::GlobalLock((HGLOBAL)pDoc->m_hMulDIB);
  503. // 找到DIB图像象素起始位置
  504. lpDIBBits2 = ::FindDIBBits(lpDIB2);
  505.     Cwvlttrans(lpDIB2,lpDIBBits2);
  506. pDoc->SetModifiedFlag(TRUE);
  507. pDoc->UpdateAllViews(NULL);
  508.     m_wavetransform.DIP_ImageFusion2(lpDIB1,lpDIBBits1,lpDIB2,lpDIBBits2,m_nLevel);
  509. pDoc->SetModifiedFlag(TRUE);
  510. pDoc->UpdateAllViews(NULL);
  511.     ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
  512. ::GlobalUnlock((HGLOBAL) pDoc->m_hMulDIB);
  513.     lpDIB1 = (LPSTR) ::GlobalLock((HGLOBAL)pDoc->GetHDIB());
  514. // 找到DIB图像象素起始位置
  515. lpDIBBits1 = ::FindDIBBits(lpDIB1);
  516.     m_wavetransform.DIBDisWavelet(lpDIB1,lpDIBBits1,Lr,Hr,FWidth,m_nLevel);
  517. pDoc->SetModifiedFlag(TRUE);
  518. pDoc->UpdateAllViews(NULL);
  519. // 解除锁定
  520. ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
  521. ::GlobalUnlock((HGLOBAL) pDoc->m_hMulDIB);
  522. }
  523. BOOL CMyView::Dwvlttrans(LPSTR lpDIB,LPSTR lpDIBBits)
  524. {
  525. CMyDoc* pDoc = GetDocument();
  526. Ld = new double[20];  //分解尺度函数
  527. Hd = new double[20];  //分解母函数
  528. Lr = new double[20];  //重建尺度函数
  529. Hr = new double[20];  //重建母函数
  530. CDaubechiesdlg daubechiesdlg;
  531.  if(daubechiesdlg.DoModal()==IDOK)
  532. {
  533. FWidth=2*daubechiesdlg.m_nlength;
  534. switch(FWidth)
  535. {
  536. case 2:
  537. Ld[0]=1/sqrt(2); Ld[1]=1/sqrt(2);
  538. m_wavetransform.m_preoffset=0;
  539. m_wavetransform.m_aftoffset=1;
  540. break;
  541. case 4:
  542. Ld[0]=0.4829629131445341; Ld[1]=0.8365163037378077; Ld[2]=0.2241438680420134;
  543. Ld[3]=-0.1294095225512603;
  544. m_wavetransform.m_preoffset=0;
  545. m_wavetransform.m_aftoffset=3;
  546. break;
  547. case 6:
  548. Ld[0]=0.3326705529500825; Ld[1]=0.8068915093110924; Ld[2]=0.4598775021184914;
  549. Ld[3]=-0.1350110200102546; Ld[4]=-0.0854412738820267; Ld[5]=0.035226218857095;
  550. m_wavetransform.m_preoffset=1;
  551. m_wavetransform.m_aftoffset=4;
  552. break;
  553. case 8:
  554. Ld[0]=0.230377813309; Ld[1]=0.714846570553; Ld[2]=0.630880767930;
  555. Ld[3]=-0.027983769417; Ld[4]=-0.187034811719; Ld[5]=0.030841381836;
  556. Ld[6]=0.032883011667; Ld[7]=-0.010597401785;
  557. m_wavetransform.m_preoffset=1;
  558. m_wavetransform.m_aftoffset=6;
  559. break;
  560. case 10:
  561. Ld[0]=0.160102397974; Ld[1]=0.603829269797; Ld[2]=0.724308528438; 
  562. Ld[3]=0.138428145901; Ld[4]=-0.242294887066; Ld[5]=-0.032244869585;
  563. Ld[6]=0.077571493840; Ld[7]=-0.006241490213; Ld[8]=-0.012580751999;
  564. Ld[9]=0.003335725285;
  565. m_wavetransform.m_preoffset=1;
  566. m_wavetransform.m_aftoffset=8;
  567. break;
  568. case 12:
  569. Ld[0]=0.111540743350;   Ld[1]=0.494623890398; Ld[2]=0.751133908021; 
  570. Ld[3]=0.315250351709; Ld[4]=-0.226264693965; Ld[5]=-0.129766867567;
  571. Ld[6]=0.097501605587; Ld[7]=0.027522865530; Ld[8]=-0.031582039318;
  572. Ld[9]=0.000553842201; Ld[10]=0.004777257511; Ld[11]=-0.001077301085;
  573. m_wavetransform.m_preoffset=1;
  574. m_wavetransform.m_aftoffset=10;
  575. break;
  576. case 14:
  577. Ld[0]=0.077852054085; Ld[1]=0.396539319482; Ld[2]=0.729132090846;
  578. Ld[3]=0.469782287405; Ld[4]=-0.143906003929; Ld[5]=-0.224036184994;
  579. Ld[6]=0.071309219267; Ld[7]=0.080612609151; Ld[8]=-0.038029936935;
  580. Ld[9]=-0.016574541631; Ld[10]=0.012550998556; Ld[11]=0.000429577973;
  581. Ld[12]=-0.001801640704; Ld[13]=0.000353713800;
  582. m_wavetransform.m_preoffset=1;
  583. m_wavetransform.m_aftoffset=12;
  584. break;
  585. case 16:
  586. Ld[0]=0.054415842243; Ld[1]=0.312871590914; Ld[2]=0.675630736297;
  587. Ld[3]=0.585354683654; Ld[4]=-0.015829105256; Ld[5]=-0.284015542962;
  588. Ld[6]=0.000472484574; Ld[7]=0.128747426620; Ld[8]=-0.017369301002;
  589. Ld[9]=-0.044088253931; Ld[10]=0.013981027917; Ld[11]=0.008746094047;
  590. Ld[12]=-0.004870352993; Ld[13]=-0.000391740373; Ld[14]=0.000675449406;
  591. Ld[15]=-0.000117476784;
  592. m_wavetransform.m_preoffset=1;
  593. m_wavetransform.m_aftoffset=14;
  594. break;
  595. case 18:
  596. Ld[0]=0.038077947364; Ld[1]=0.243834674613; Ld[2]=0.604823123690;
  597. Ld[3]=0.657288078051; Ld[4]=0.133197385825; Ld[5]=-0.293273783279;
  598. Ld[6]=-0.096840783223; Ld[7]=0.148540749338; Ld[8]=0.030725681479;
  599. Ld[9]=-0.067632829061; Ld[10]=0.000250947115; Ld[11]=0.022361662124;
  600. Ld[12]=-0.004723204758; Ld[13]=-0.004281503682; Ld[14]=0.001847646883;
  601. Ld[15]=0.000230385764; Ld[16]=-0.000251963189; Ld[17]=0.000039347320;
  602. m_wavetransform.m_preoffset=2;
  603. m_wavetransform.m_aftoffset=15;
  604. break;
  605. case 20:
  606. Ld[0]=0.026670057901; Ld[1]=0.188176800078; Ld[2]=0.527201188932;
  607. Ld[3]=0.688459039454; Ld[4]=0.281172343661; Ld[5]=-0.249846424327;
  608. Ld[6]=-0.195946274377; Ld[7]=0.127369340336; Ld[8]=0.093057364604;
  609. Ld[9]=-0.071394147166; Ld[10]=-0.029457536822; Ld[11]=0.033212674059;
  610. Ld[12]=0.003606553567; Ld[13]=-0.010733175483; Ld[14]=0.001395351747;
  611. Ld[15]=0.001992405295; Ld[16]=-0.000685856695; Ld[17]=-0.000116466855;
  612. Ld[18]=0.000093588670; Ld[19]=-0.000013264203;
  613. m_wavetransform.m_preoffset=2;
  614. m_wavetransform.m_aftoffset=17;
  615. break;
  616. }
  617. for(int i=0;i<FWidth;i++)
  618. {
  619. Hd[i]=pow(-1,i+1)*Ld[-i-1+FWidth];
  620. }
  621. for(i=0;i<FWidth;i++)
  622. {
  623. Lr[i]=Ld[-i-1+FWidth];
  624. Hr[i]=Hd[-i-1+FWidth];
  625. }
  626. }
  627. CWaveletLevel leveldlg;
  628. if(leveldlg.DoModal()==IDOK)
  629. {
  630. m_nLevel=leveldlg.m_nlevel;
  631. //分解
  632. m_wavetransform.DIBWavelet(lpDIB,lpDIBBits,Ld,Hd,FWidth,m_nLevel);
  633.     pDoc->SetModifiedFlag(TRUE);
  634. pDoc->UpdateAllViews(NULL);
  635. }
  636. return TRUE;// 返回
  637. }
  638. BOOL CMyView::Swvlttrans(LPSTR lpDIB,LPSTR lpDIBBits)
  639. {
  640. CMyDoc* pDoc = GetDocument();
  641. Ld = new double[20];  //分解尺度函数
  642. Hd = new double[20];  //分解母函数
  643. Lr = new double[20];  //重建尺度函数
  644. Hr = new double[20];  //重建母函数
  645. CSymletsdlg symdlg;
  646. if(symdlg.DoModal()==IDOK)
  647. { FWidth=2*symdlg.m_symlength;
  648.    switch(FWidth)
  649. {
  650. case 8:
  651. Ld[0]=-0.107148901418/sqrt(2); Ld[1]=-0.041910965126/sqrt(2); Ld[2]=0.703739068656/sqrt(2);
  652. Ld[3]=1.136658243409/sqrt(2); Ld[4]=0.421234534204/sqrt(2); Ld[5]=-0.140317624179/sqrt(2);
  653. Ld[6]=-0.017824701442/sqrt(2); Ld[7]=0.045570345896/sqrt(2);
  654. m_wavetransform.m_preoffset=3;
  655. m_wavetransform.m_aftoffset=4;
  656. break;
  657. case 10:
  658. Ld[0]=0.038654795955/sqrt(2); Ld[1]=0.041746864422/sqrt(2); Ld[2]=-0.055344186117/sqrt(2);
  659. Ld[3]=0.281990696854/sqrt(2); Ld[4]=1.023052966894/sqrt(2); Ld[5]=0.896581648380/sqrt(2);
  660. Ld[6]=0.023478923136/sqrt(2); Ld[7]=-0.247951362613/sqrt(2); Ld[8]=-0.029842499869/sqrt(2);
  661. Ld[9]=0.027632152958/sqrt(2);
  662. m_wavetransform.m_preoffset=4;
  663. m_wavetransform.m_aftoffset=5;
  664. break;
  665. case 12:
  666. Ld[0]=0.021784700327/sqrt(2); Ld[1]=0.004936612372/sqrt(2); Ld[2]=-0.166863215412/sqrt(2);
  667. Ld[3]=-0.068323121587/sqrt(2); Ld[4]=0.0694457972958/sqrt(2); Ld[5]=1.113892783926/sqrt(2);
  668. Ld[6]=0.477904371333/sqrt(2); Ld[7]=-0.102724969862/sqrt(2); Ld[8]=-0.029783751299/sqrt(2);
  669. Ld[9]=0.063250562660/sqrt(2); Ld[10]=0.002499922093/sqrt(2); Ld[11]=-0.011031867509/sqrt(2);
  670. m_wavetransform.m_preoffset=5;
  671. m_wavetransform.m_aftoffset=6;
  672. break;
  673. case 14:
  674. Ld[0]=0.003792658534/sqrt(2); Ld[1]=-0.001481225915/sqrt(2); Ld[2]=-0.017870431651/sqrt(2);
  675. Ld[3]=0.043155452582/sqrt(2); Ld[4]=0.096014767936/sqrt(2); Ld[5]=-0.070078291222/sqrt(2);
  676. Ld[6]=0.024665659489/sqrt(2); Ld[7]=0.758162601964/sqrt(2); Ld[8]=1.085782709814/sqrt(2);
  677. Ld[9]=0.408183939725/sqrt(2); Ld[10]=-0.198056706807/sqrt(2); Ld[11]=-0.152463872896/sqrt(2);
  678. Ld[12]=0.005671342686/sqrt(2); Ld[13]=0.014521394762/sqrt(2);
  679. m_wavetransform.m_preoffset=7;
  680. m_wavetransform.m_aftoffset=6;
  681. break;
  682. case 16:
  683. Ld[0]=0.002672793393/sqrt(2); Ld[1]=-0.000428394300/sqrt(2); Ld[2]=-0.021145686528/sqrt(2);
  684. Ld[3]=0.005386388754/sqrt(2); Ld[4]=0.069490465911/sqrt(2); Ld[5]=-0.038493521263/sqrt(2);
  685. Ld[6]=-0.073462508761/sqrt(2); Ld[7]=0.515398670374/sqrt(2); Ld[8]=1.099106630537/sqrt(2);
  686. Ld[9]=0.680745347190/sqrt(2); Ld[10]=-0.086653615406/sqrt(2); Ld[11]=-0.202648655286/sqrt(2);
  687. Ld[12]=0.010758611751/sqrt(2); Ld[13]=0.044823623042/sqrt(2); Ld[14]=-0.000766690896/sqrt(2);
  688. Ld[15]=-0.004783458512/sqrt(2);
  689. m_wavetransform.m_preoffset=8;
  690. m_wavetransform.m_aftoffset=7;
  691. break;
  692. case 18:
  693. Ld[0]=0.001512487309/sqrt(2); Ld[1]=-0.000669141509/sqrt(2); Ld[2]=-0.014515578553/sqrt(2);
  694. Ld[3]=0.012528896242/sqrt(2); Ld[4]=0.087791251554/sqrt(2); Ld[5]=-0.025786445930/sqrt(2);
  695. Ld[6]=-0.270893783503/sqrt(2); Ld[7]=0.049882830959/sqrt(2); Ld[8]=0.873048407349/sqrt(2);
  696. Ld[9]=1.015259790832/sqrt(2); Ld[10]=0.337658923602/sqrt(2); Ld[11]=-0.077172161097/sqrt(2);
  697. Ld[12]=0.000825140929/sqrt(2); Ld[13]=0.042744433602/sqrt(2); Ld[14]=-0.016303351226/sqrt(2);
  698. Ld[15]=-0.018769396836/sqrt(2); Ld[16]=0.000876502539/sqrt(2); Ld[17]=0.001981193736/sqrt(2);
  699. m_wavetransform.m_preoffset=8;
  700. m_wavetransform.m_aftoffset=9;
  701. break;
  702. case 20:
  703. Ld[0]=0.001089170447/sqrt(2); Ld[1]=0.000135245020/sqrt(2); Ld[2]=-0.012220642630/sqrt(2);
  704. Ld[3]=-0.002072363923/sqrt(2); Ld[4]=0.064950924579/sqrt(2); Ld[5]=0.016418869426/sqrt(2);
  705. Ld[6]=-0.225558972234/sqrt(2); Ld[7]=-0.100240215031/sqrt(2); Ld[8]=0.667071338154/sqrt(2);
  706. Ld[9]=1.088251530500/sqrt(2); Ld[10]=0.542813011213/sqrt(2); Ld[11]=-0.050256540092/sqrt(2);
  707. Ld[12]=-0.045240772218/sqrt(2); Ld[13]=0.070703567550/sqrt(2); Ld[14]=0.008152816799/sqrt(2);
  708. Ld[15]=-0.028786231926/sqrt(2); Ld[16]=-0.001137535314/sqrt(2); Ld[17]=0.006495728375/sqrt(2);
  709. Ld[18]=0.000080661204/sqrt(2); Ld[19]=-0.000649589896/sqrt(2);
  710. m_wavetransform.m_preoffset=9;
  711. m_wavetransform.m_aftoffset=10;
  712. break;
  713. }
  714.        for(int i=0;i<FWidth;i++)
  715. {
  716. Hd[i]=pow(-1,i+1)*Ld[-i-1+FWidth];
  717. }
  718. for(i=0;i<FWidth;i++)
  719. {
  720. Lr[i]=Ld[-i-1+FWidth];
  721. Hr[i]=Hd[-i-1+FWidth];
  722. }
  723. }
  724. CWaveletLevel leveldlg;
  725. if(leveldlg.DoModal()==IDOK)
  726. {
  727. m_nLevel=leveldlg.m_nlevel;
  728. //分解
  729. m_wavetransform.DIBWavelet(lpDIB,lpDIBBits,Ld,Hd,FWidth,m_nLevel);
  730.     pDoc->SetModifiedFlag(TRUE);
  731. pDoc->UpdateAllViews(NULL);
  732. }
  733. return TRUE;// 返回
  734. }
  735. BOOL CMyView::Cwvlttrans(LPSTR lpDIB,LPSTR lpDIBBits)
  736. {
  737. CMyDoc* pDoc = GetDocument();
  738. Ld = new double[20];  //分解尺度函数
  739. Hd = new double[20];  //分解母函数
  740. Lr = new double[20];  //重建尺度函数
  741. Hr = new double[20];  //重建母函数
  742. FWidth=8;
  743. Ld[0]=-0.0802861503271*sqrt(2); Ld[1]=-0.0243085969067*sqrt(2); Ld[2]=0.362806341593*sqrt(2);
  744. Ld[3]=0.550576616156*sqrt(2); Ld[4]=0.229036357075*sqrt(2); Ld[5]=-0.0644368523121*sqrt(2);
  745. Ld[6]=-0.0115565483406*sqrt(2); Ld[7]=0.0381688330633*sqrt(2);
  746. m_wavetransform.m_preoffset=3;
  747. m_wavetransform.m_aftoffset=4;
  748.        for(int i=0;i<FWidth;i++)
  749. {
  750. Hd[i]=pow(-1,i+1)*Ld[-i-1+FWidth];
  751. }
  752. for(i=0;i<FWidth;i++)
  753. {
  754. Lr[i]=Ld[-i-1+FWidth];
  755. Hr[i]=Hd[-i-1+FWidth];
  756. }
  757. CWaveletLevel leveldlg;
  758. if(leveldlg.DoModal()==IDOK)
  759. {
  760. m_nLevel=leveldlg.m_nlevel;
  761. //分解
  762. m_wavetransform.DIBWavelet(lpDIB,lpDIBBits,Ld,Hd,FWidth,m_nLevel);
  763.     pDoc->SetModifiedFlag(TRUE);
  764. pDoc->UpdateAllViews(NULL);
  765. }
  766. return TRUE;// 返回
  767. }
  768. void CMyView::OnImageHebing()
  769. {
  770. CHebingDlg dlg;
  771. dlg.DoModal();
  772. }
  773. void CMyView::OnDetectTemplate() 
  774. {
  775. // 获取文档
  776. CMyDoc* pDoc = GetDocument();
  777. // 指向DIB的指针
  778. LPSTR lpDIB,lpTemplateDIB;
  779. // 指向DIB象素指针
  780. LPSTR    lpDIBBits,lpTemplateDIBBits;
  781. //图像的宽度与高度
  782. long lWidth,lHeight;
  783. //模板的宽度与高度
  784. long lTemplateWidth,lTemplateHeight;
  785. HDIB hTemplateDIB;
  786. // 锁定DIB
  787. lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
  788. // 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的水平镜像,其它的可以类推)
  789. if (::DIBNumColors(lpDIB) != 256)
  790. {
  791. // 提示用户
  792. MessageBox("目前只支持256色位图的平移!", "系统提示" , MB_ICONINFORMATION | MB_OK);
  793. // 解除锁定
  794. ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
  795. // 返回
  796. return;
  797. }
  798. // 更改光标形状
  799. BeginWaitCursor();
  800. // 找到DIB图像象素起始位置
  801. lpDIBBits = ::FindDIBBits(lpDIB);
  802. lWidth = ::DIBWidth(lpDIB);
  803. lHeight = ::DIBHeight(lpDIB);
  804. CFileDialog dlg(TRUE,"bmp","*.bmp");
  805. if(dlg.DoModal() == IDOK)
  806. {
  807.  
  808.   CFile file;
  809.   CFileException fe;
  810.  
  811.   CString strPathName;
  812.  
  813. strPathName = dlg.GetPathName();
  814.  
  815. // 打开文件
  816. VERIFY(file.Open(strPathName, CFile::modeRead | CFile::shareDenyWrite, &fe));
  817. // 尝试调用ReadDIBFile()读取图像
  818. TRY
  819. {
  820.   hTemplateDIB = ::ReadDIBFile(file);
  821. }
  822. CATCH (CFileException, eLoad)
  823. {
  824. // 读取失败
  825.   file.Abort();
  826.  
  827. // 恢复光标形状
  828. EndWaitCursor();
  829.  
  830. // 报告失败
  831. //ReportSaveLoadException(strPathName, eLoad,
  832. // FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
  833. // 设置DIB为空
  834. hTemplateDIB = NULL;
  835. // 返回
  836. return;
  837. }
  838. END_CATCH
  839.  
  840. // 初始化DIB
  841. //InitDIBData();
  842.  
  843. // 判断读取文件是否成功
  844. if (hTemplateDIB == NULL)
  845. {
  846.   // 失败,可能非BMP格式
  847.   CString strMsg;
  848.   strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
  849.  
  850.   // 提示出错
  851.   MessageBox(strMsg, NULL, MB_ICONINFORMATION | MB_OK);
  852.   
  853.   // 恢复光标形状
  854.   EndWaitCursor();
  855.  
  856.   // 返回
  857.   return;
  858.   }
  859.   }
  860. else
  861. {
  862.   // 恢复光标形状
  863.   EndWaitCursor();
  864. return;
  865. }
  866. // 锁定DIB
  867. lpTemplateDIB = (LPSTR) ::GlobalLock((HGLOBAL) hTemplateDIB);
  868. // 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的模板匹配,其它的可以类推)
  869. if (::DIBNumColors(lpTemplateDIB) != 256)
  870. {
  871. // 提示用户
  872. MessageBox("目前只支持256色位图!", "系统提示" , MB_ICONINFORMATION | MB_OK);
  873. // 解除锁定
  874. ::GlobalUnlock((HGLOBAL) hTemplateDIB);
  875. // 返回
  876. return;
  877. }
  878. // 更改光标形状
  879. BeginWaitCursor();
  880. // 找到DIB图像象素起始位置
  881. lpTemplateDIBBits = ::FindDIBBits(lpTemplateDIB);
  882. lTemplateWidth = ::DIBWidth(lpTemplateDIB);
  883. lTemplateHeight = ::DIBHeight(lpTemplateDIB);
  884. if(lTemplateHeight > lHeight || lTemplateWidth > lWidth )
  885. {
  886. // 提示用户
  887. MessageBox("模板尺寸大于源图像尺寸!", "系统提示" , MB_ICONINFORMATION | MB_OK);
  888. // 解除锁定
  889. ::GlobalUnlock((HGLOBAL) hTemplateDIB);
  890. // 返回
  891. return;
  892. }
  893. // 调用TemplateMatchDIB()函数进行模板匹配
  894. if (TemplateMatchDIB(lpDIBBits,lpTemplateDIBBits, lWidth,lHeight, lTemplateWidth,lTemplateHeight))
  895. {
  896. // 设置脏标记
  897. pDoc->SetModifiedFlag(TRUE);
  898. // 更新视图
  899. pDoc->UpdateAllViews(NULL);
  900. }
  901. else
  902. {
  903. // 提示用户
  904. MessageBox("分配内存失败!", "系统提示" , MB_ICONINFORMATION | MB_OK);
  905. }
  906. // 解除锁定
  907. ::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
  908. ::GlobalUnlock((HGLOBAL) hTemplateDIB);
  909. // 恢复光标
  910. EndWaitCursor();
  911. }