Move the sources to trunk
[opencv] / apps / VMDemo / VMDemoDlg.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/// VMDemoDlg.cpp : implementation file
41 //
42
43 #include "stdafx.h"
44 #include "VMDemo.h"
45 #include "VMDemoDlg.h"
46 #include "raster.h"
47 #include <assert.h>
48 #include "resource.h"
49
50 #include "CV.h"
51
52 #ifdef _DEBUG
53 #define new DEBUG_NEW
54 #undef THIS_FILE
55 static char THIS_FILE[] = __FILE__;
56 #endif
57
58 /////////////////////////////////////////////////////////////////////////////
59 // CAboutDlg dialog used for App About
60
61 class CAboutDlg : public CDialog
62 {
63 public:
64     CAboutDlg();
65
66 // Dialog Data
67     //{{AFX_DATA(CAboutDlg)
68     enum { IDD = IDD_ABOUTBOX };
69     //}}AFX_DATA
70
71     // ClassWizard generated virtual function overrides
72     //{{AFX_VIRTUAL(CAboutDlg)
73     protected:
74     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
75     //}}AFX_VIRTUAL
76
77 // Implementation
78 protected:
79     //{{AFX_MSG(CAboutDlg)
80     //}}AFX_MSG
81     DECLARE_MESSAGE_MAP()
82 };
83
84
85 /*======================================================================================*/
86
87 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
88 {
89     //{{AFX_DATA_INIT(CAboutDlg)
90     //}}AFX_DATA_INIT
91 }
92
93
94 /*======================================================================================*/
95
96 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
97 {
98     CDialog::DoDataExchange(pDX);
99     //{{AFX_DATA_MAP(CAboutDlg)
100     //}}AFX_DATA_MAP
101 }
102
103
104 /*======================================================================================*/
105
106 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
107     //{{AFX_MSG_MAP(CAboutDlg)
108         // No message handlers
109     //}}AFX_MSG_MAP
110 END_MESSAGE_MAP()
111
112 /////////////////////////////////////////////////////////////////////////////
113 // CVMDemoDlg dialog
114
115 CVMDemoDlg::CVMDemoDlg(CWnd* pParent /*=NULL*/)
116     : CDialog(CVMDemoDlg::IDD, pParent)
117 {
118     //{{AFX_DATA_INIT(CVMDemoDlg)
119         // NOTE: the ClassWizard will add member initialization here
120     //}}AFX_DATA_INIT
121     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
122     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
123
124     numScanlines    = 0;
125     RasterLeft      = new CRaster;
126     RasterRight     = new CRaster;
127     RasterResult    = new CRaster;
128
129     HaveMorph       =   false;
130
131     scanlines_1         = 0;
132     scanlines_2         = 0;
133     scanlines_a         = 0;
134
135     lens_1          = 0;
136     lens_2          = 0;
137     morph_lens      = 0;
138
139     runs_1          = 0;
140     runs_2          = 0;
141
142     buffer_1        = 0;
143     buffer_2        = 0;
144     morph_buffer    = 0;
145
146     num_runs_1      = 0;
147     num_runs_2      = 0;
148
149     corr_1          = 0;
150     corr_2          = 0;
151
152
153     prevpos         = -100;
154
155     CanMorph        = false;
156
157 }
158
159
160 /*======================================================================================*/
161
162 void CVMDemoDlg::DoDataExchange(CDataExchange* pDX)
163 {
164     CDialog::DoDataExchange(pDX);
165     //{{AFX_DATA_MAP(CVMDemoDlg)
166         DDX_Control(pDX, IDC_SHOWSCANLINES, m_showscanlines);
167     DDX_Control(pDX, IDC_IMAGESIZE, m_imagesize);
168     DDX_Control(pDX, IDC_CAMERAPOS, m_camerapos);
169         //}}AFX_DATA_MAP
170 }
171
172
173 /*======================================================================================*/
174
175 BEGIN_MESSAGE_MAP(CVMDemoDlg, CDialog)
176     //{{AFX_MSG_MAP(CVMDemoDlg)
177     ON_WM_SYSCOMMAND()
178     ON_WM_PAINT()
179     ON_WM_QUERYDRAGICON()
180     ON_BN_CLICKED(IDC_OPENPROJECT, OnOpenproject)
181     ON_WM_HSCROLL()
182     ON_BN_CLICKED(IDC_ORIGINALSIZE, OnOriginalsize)
183     ON_BN_CLICKED(IDC_MAKEMORPHING, OnMakemorphing)
184         ON_BN_CLICKED(IDC_SHOWSCANLINES, OnShowscanlines)
185         //}}AFX_MSG_MAP
186 END_MESSAGE_MAP()
187
188 /////////////////////////////////////////////////////////////////////////////
189 // CVMDemoDlg message handlers
190
191 BOOL CVMDemoDlg::OnInitDialog()
192 {
193     CDialog::OnInitDialog();
194
195     // Add "About..." menu item to system menu.
196
197     // IDM_ABOUTBOX must be in the system command range.
198     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
199     ASSERT(IDM_ABOUTBOX < 0xF000);
200
201     CMenu* pSysMenu = GetSystemMenu(FALSE);
202     if (pSysMenu != NULL)
203     {
204         CString strAboutMenu;
205         strAboutMenu.LoadString(IDS_ABOUTBOX);
206         if (!strAboutMenu.IsEmpty())
207         {
208             pSysMenu->AppendMenu(MF_SEPARATOR);
209             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
210     }
211     }
212
213     // Set the icon for this dialog.  The framework does this automatically
214     //  when the application's main window is not a dialog
215     SetIcon(m_hIcon, TRUE);         // Set big icon
216     SetIcon(m_hIcon, FALSE);        // Set small icon
217
218     // TODO: Add extra initialization here
219
220     //////////////
221     RightImage  = 0;
222     LeftImage   = 0;
223
224     RECT rect;
225
226     rect.left   = 70;
227     rect.top    = 10;
228     rect.right  = 200;
229     rect.bottom = 300;
230
231     CString className;
232     className   = AfxRegisterWndClass(NULL);
233
234     LeftImage   = 0;
235     RightImage  = 0;
236     ResultImage = 0;
237
238     LeftImage   = new CVirtualImage;
239
240     LeftImage   ->CreateEx( WS_EX_OVERLAPPEDWINDOW,
241                             className,
242                             "Left Base Image",
243                             WS_VISIBLE | WS_OVERLAPPED |WS_THICKFRAME,
244                             rect,
245                             NULL,
246                             0);
247
248     RightImage  = new CVirtualImage;
249
250     RightImage  ->CreateEx( WS_EX_OVERLAPPEDWINDOW,
251                             className,
252                             "Right Base Image",
253                             WS_VISIBLE | WS_OVERLAPPED |WS_THICKFRAME,
254                             rect,
255                             NULL,
256                             0);
257
258
259
260 ////
261     ResultImage = new CVirtualImage;
262
263     ResultImage ->CreateEx( WS_EX_OVERLAPPEDWINDOW,
264                             className,
265                             "Virtual Image",
266                             WS_VISIBLE | WS_OVERLAPPED |WS_THICKFRAME,
267                             rect,
268                             NULL,
269                             0);
270
271     LeftImage   -> SetScanlines(&scanlines_2,&numScanlines);
272     LeftImage   -> SetRasterImage(RasterLeft);
273     LeftImage   -> ShowWindow(SW_SHOW);
274
275     RightImage  -> SetScanlines(&scanlines_1,&numScanlines);
276     RightImage  -> SetRasterImage(RasterRight);
277     RightImage  -> ShowWindow(SW_SHOW);
278
279     ResultImage -> SetScanlines(&scanlines_a,&numScanlines);
280     ResultImage -> SetRasterImage(RasterResult);
281     ResultImage -> ShowWindow(SW_SHOW);
282
283
284     SetWindowsToTop();
285
286     m_camerapos.SetRange(0,300);// ition.SetScrollRange(0,300);
287
288     m_imagesize.SetWindowText("");
289
290     return TRUE;  // return TRUE  unless you set the focus to a control
291 }
292
293
294 /*======================================================================================*/
295
296 void CVMDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
297 {
298     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
299     {
300         CAboutDlg dlgAbout;
301         dlgAbout.DoModal();
302     }
303     else
304     {
305         CDialog::OnSysCommand(nID, lParam);
306     }
307 }
308
309
310 /*======================================================================================*/
311
312 void CVMDemoDlg::OnPaint()
313 {
314     if (IsIconic())
315     {
316         CPaintDC dc(this); // device context for painting
317
318         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
319
320         // Center icon in client rectangle
321         int cxIcon = GetSystemMetrics(SM_CXICON);
322         int cyIcon = GetSystemMetrics(SM_CYICON);
323         CRect rect;
324         GetClientRect(&rect);
325         int x = (rect.Width() - cxIcon + 1) / 2;
326         int y = (rect.Height() - cyIcon + 1) / 2;
327
328         // Draw the icon
329         dc.DrawIcon(x, y, m_hIcon);
330     }
331     else
332     {
333         CDialog::OnPaint();
334     }
335 }
336
337
338 /*======================================================================================*/
339
340 HCURSOR CVMDemoDlg::OnQueryDragIcon()
341 {
342     return (HCURSOR) m_hIcon;
343 }
344
345
346 /*======================================================================================*/
347
348 void CVMDemoDlg::OnOpenproject()
349 {
350     SetWindowsToTop();
351
352     CFileDialog filedialog(
353                             TRUE,// Open Dialog
354                             NULL,//Default extension
355                             NULL,//Initial file name
356                             OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,//Flag
357                             "Project Files (*.ini;*.cpj)|*.ini;*.cpj|All Files (*.*)|*.*|");//Fiter
358
359     if (filedialog.DoModal()==IDOK) {
360         CString projectName=filedialog.GetPathName();
361
362         //char path_buffer[_MAX_PATH];
363         char drive[_MAX_DRIVE];
364         char dir[_MAX_DIR];
365         char fname[_MAX_FNAME];
366         char ext[_MAX_EXT];
367
368         _splitpath(projectName,drive,dir,fname,ext );
369
370         if (stricmp(ext,".cpj") == 0)
371         {
372             useScanlines = 1;
373         }
374         else
375         {
376             useScanlines = 0;
377         }
378
379         CString projname;
380
381
382         long numPicts;
383         
384         numPicts = ::GetPrivateProfileInt(    "Project",
385                                             "NumberOfPicturePairs",
386                                             0,
387                                             projectName);
388
389         if (numPicts > 0) {
390
391     
392         /* Load Left Image */
393             char    buff[_MAX_PATH];
394             ::GetPrivateProfileString(  "Pair1",
395                                         "Image1",
396                                         "",
397                                         buff,
398                                         _MAX_PATH,
399                                         projectName );
400
401             RasterLeft  ->LoadBMP(buff);
402             {// Flip the Image
403                 char    *tmpBuff;
404                 int     step,h,y;
405                 char    *src;
406                 char    *dest;
407
408                 step    = RasterLeft->GetSizeString();
409                 h       = RasterLeft->GetHeight();
410
411                 src = (char*)(RasterLeft->GetImage());
412                 dest = src + step * (h - 1);
413
414                 tmpBuff = (char*)calloc(step,1);
415
416                 for (y = 0; y < h/2; y++) {
417                     memcpy(tmpBuff,src,step);
418                     memcpy(src,dest,step);
419                     memcpy(dest,tmpBuff,step);
420
421                     src     += step;
422                     dest    -= step;
423                 }
424
425                 free(tmpBuff);
426             }
427
428             LeftImage   ->Invalidate();
429
430
431
432             ::GetPrivateProfileString(  "Pair1",
433                                         "Image2",
434                                         "",
435                                         buff,
436                                         _MAX_PATH,
437                                         projectName );
438
439             RasterRight ->LoadBMP(buff) ;
440             {// Flip the Image
441                 char    *tmpBuff;
442                 int     step,h,y;
443                 char    *src;
444                 char    *dest;
445
446                 step    = RasterRight->GetSizeString();
447                 h       = RasterRight->GetHeight();
448
449                 src = (char*)(RasterRight->GetImage());
450                 dest = src + step * (h - 1);
451
452                 tmpBuff = (char*)calloc(step,1);
453
454                 for (y = 0; y < h/2; y++) {
455                     memcpy(tmpBuff,src,step);
456                     memcpy(src,dest,step);
457                     memcpy(dest,tmpBuff,step);
458
459                     src     += step;
460                     dest    -= step;
461                 }
462
463                 free(tmpBuff);
464             }
465         
466             RightImage  ->Invalidate();
467
468
469         if (    (RasterLeft -> GetWidth()       != RasterRight -> GetWidth()) ||
470                 (RasterLeft -> GetHeight()      != RasterRight -> GetHeight())
471                 ) {
472             CanMorph    = false;
473             m_imagesize.SetWindowText("");
474
475         } else {
476             CanMorph    = true;
477             char st[300];
478             sprintf(st,"%dx%d",RasterLeft -> GetWidth(),RasterLeft -> GetHeight());
479             m_imagesize.SetWindowText(st);
480
481
482             RasterResult        -> CreateRaster(    RasterLeft -> GetWidth(),
483                                                 RasterLeft -> GetHeight(),
484                                                 24);
485
486             ResultImage         -> Invalidate();
487         }
488
489
490
491         /* Read Fundamental matrix */
492
493         char strf[102];
494 #if 0
495         ::GetPrivateProfileString(  "FundMatrix","F[0][0]","0.0",strf,100,projectName);
496         FundMatrix.m[0][0] = (float)atof(strf);
497
498         ::GetPrivateProfileString(  "FundMatrix","F[0][1]","0.0",strf,100,projectName);
499         FundMatrix.m[0][1] = (float)atof(strf);
500
501         ::GetPrivateProfileString(  "FundMatrix","F[0][2]","0.0",strf,100,projectName);
502         FundMatrix.m[0][2] = (float)atof(strf);
503
504         ::GetPrivateProfileString(  "FundMatrix","F[1][0]","0.0",strf,100,projectName);
505         FundMatrix.m[1][0] = (float)atof(strf);
506
507         ::GetPrivateProfileString(  "FundMatrix","F[1][1]","0.0",strf,100,projectName);
508         FundMatrix.m[1][1] = (float)atof(strf);
509
510         ::GetPrivateProfileString(  "FundMatrix","F[1][2]","0.0",strf,100,projectName);
511         FundMatrix.m[1][2] = (float)atof(strf);
512
513         ::GetPrivateProfileString(  "FundMatrix","F[2][0]","0.0",strf,100,projectName);
514         FundMatrix.m[2][0] = (float)atof(strf);
515
516         ::GetPrivateProfileString(  "FundMatrix","F[2][1]","0.0",strf,100,projectName);
517         FundMatrix.m[2][1] = (float)atof(strf);
518
519         ::GetPrivateProfileString(  "FundMatrix","F[2][2]","0.0",strf,100,projectName);
520         FundMatrix.m[2][2] = (float)atof(strf);
521 #endif
522
523         
524         {
525             /* Try to read points and compute fundamental matrix */
526             /* Get number of points */
527             int numPoints;
528             ::GetPrivateProfileString(  "Pair1","NumberOfPoints","0",strf,100,projectName);
529             numPoints = atoi(strf);
530             int numMatrs = 0;
531             if( numPoints > 6 )
532             {
533                 /* Create points */
534                 CvMat* points1;
535                 CvMat* points2;
536                 CvMat* fundMatr;
537                 points1 = cvCreateMat(2,numPoints,CV_32F);
538                 points2 = cvCreateMat(2,numPoints,CV_32F);
539                 fundMatr = cvCreateMat(3,3,CV_32F);
540
541                 double x1,y1,x2,y2;
542                 /* load points */
543                 char str[100];
544                 int i;
545                 for( i = 0; i < numPoints; i++ )
546                 {
547                     sprintf(str,"LX%d",i);
548                     ::GetPrivateProfileString(  "Points1",str,"0",strf,100,projectName);
549                     x1 = atof(strf);
550
551                     sprintf(str,"LY%d",i);
552                     ::GetPrivateProfileString(  "Points1",str,"0",strf,100,projectName);
553                     y1 = atof(strf);
554                 
555                     sprintf(str,"RX%d",i);
556                     ::GetPrivateProfileString(  "Points1",str,"0",strf,100,projectName);
557                     x2 = atof(strf);
558                 
559                     sprintf(str,"RY%d",i);
560                     ::GetPrivateProfileString(  "Points1",str,"0",strf,100,projectName);
561                     y2 = atof(strf);
562
563                     cvmSet(points1,0,i,x1);
564                     cvmSet(points1,1,i,y1);
565
566                     cvmSet(points2,0,i,x2);
567                     cvmSet(points2,1,i,y2);
568                 }
569
570                 if( numPoints > 7 )
571                 {
572                     numMatrs = cvFindFundamentalMat(points1,points2,fundMatr,CV_FM_RANSAC,1,0.99,0);
573                 }
574                 else
575                 {
576                     numMatrs = cvFindFundamentalMat(points1,points2,fundMatr,CV_FM_7POINT,0,0,0);
577                 }
578
579                 if( numMatrs == 1 )
580                 {
581                     for(i=0;i<9;i++)
582                     {
583
584                         FundMatrix.m[i/3][i%3] = (float)cvmGet(fundMatr,i/3,i%3);
585                     }
586
587                 }
588
589                 cvReleaseMat(&points1);
590                 cvReleaseMat(&points2);
591                 cvReleaseMat(&fundMatr);
592             }
593
594             if( numMatrs == 0 )
595             {
596                 int i;
597                 for(i=0;i<9;i++)
598                 {
599
600                     FundMatrix.m[i/3][i%3] = 0;
601                 }
602
603             }
604
605
606
607
608         }
609         
610
611
612         /////////////////////////////
613
614
615         FreeMemory();
616         AllocMemory();
617         HaveMorph   = false;
618
619
620
621         }/* in project > 0 pictures*/
622     }/* if dialog */
623
624 }
625
626
627 /*======================================================================================*/
628
629 void CVMDemoDlg::SetWindowsToTop()
630 {
631     CDC *dc;
632     dc      = GetDC();
633
634     int width,height;
635
636     width   = GetDeviceCaps( dc->m_hDC, HORZRES);
637     height  = GetDeviceCaps( dc->m_hDC, VERTRES);
638
639     ReleaseDC(dc);
640
641     LeftImage   -> MoveWindow(0, 0, width / 3, height / 3);
642     ResultImage -> MoveWindow(width / 3, 0, width / 3, height / 3);
643     RightImage  -> MoveWindow(width - width / 3, 0, width / 3,height / 3);
644 }
645
646
647 /*======================================================================================*/
648
649 void CVMDemoDlg::OnMakemorphing()
650 {
651     if (CanMorph) {
652         CvSize imgsize;
653         CvMatrix3  matrix;
654
655         IplImage* image1;
656         CvSize    size1;
657         
658         IplImage* image2;
659         CvSize    size2;
660
661         IplImage* imageRes;
662         CvSize    sizeRes;
663
664         image1   = cvCreateImageHeader( cvSize(1,1), 8, 3 );
665         image2   = cvCreateImageHeader( cvSize(1,1), 8, 3 );
666         imageRes = cvCreateImageHeader( cvSize(1,1), 8, 3 );
667
668
669         size1.width   = RasterLeft->GetWidth();
670         size1.height  = RasterLeft->GetHeight();
671
672         size2.width   = RasterRight->GetWidth();
673         size2.height  = RasterRight->GetHeight();
674
675         sizeRes.width   = RasterRight->GetWidth();
676         sizeRes.height  = RasterRight->GetHeight();
677
678         cvInitImageHeader(  image1,
679                             size1,
680                             8,
681                             3);
682
683         cvSetImageData( image1,
684                         RasterLeft->GetImage(),
685                         RasterLeft->GetSizeString() ); 
686         
687         cvInitImageHeader(  image2,
688                             size2,
689                             8,
690                             3);
691
692         cvSetImageData( image2,
693                         RasterRight->GetImage(),
694                         RasterRight->GetSizeString() ); 
695
696         cvInitImageHeader(  imageRes,
697                             sizeRes,
698                             8,
699                             3);
700         
701         cvSetImageData( imageRes,
702                         RasterResult->GetImage(),
703                         RasterResult->GetSizeString() ); 
704
705         imgsize.width   = RasterLeft->GetWidth();
706         imgsize.height  = RasterLeft->GetHeight();
707
708
709         float pos = float(m_camerapos.GetPos());
710         pos = pos / (m_camerapos.GetRangeMax() - m_camerapos.GetRangeMin());
711
712         float alpha = pos;
713
714         matrix = FundMatrix;
715
716         m_morpher.SetPan( alpha );
717
718 //#define SCANLINE_VARIANT 1
719
720         if( useScanlines == 1 )
721         {
722 //#if SCANLINE_VARIANT
723         cvMakeScanlines(    &matrix,
724                                     imgsize,
725                                     0,
726                                     0,
727                                     0,
728                                     0,
729                                     &numScanlines);
730         }
731 //#endif
732
733         if (!HaveMorph) {
734
735         if( useScanlines == 1 )
736         {
737 //#if SCANLINE_VARIANT
738             cvMakeScanlines(    &matrix,
739                                         imgsize,
740                                         scanlines_1,
741                                         scanlines_2,
742                                         lens_1,
743                                         lens_2,
744                                         &numScanlines);
745             /* Prewarp first image */
746             cvPreWarpImage( numScanlines,
747                                         image1,
748                                         buffer_1,
749                                         lens_1,
750                                         scanlines_1);
751             
752
753             /* Prewarp second image */
754             cvPreWarpImage( numScanlines,
755                                         image2,
756                                         buffer_2,
757                                         lens_2,
758                                         scanlines_2);
759             
760
761             /* Create Runs on first Image */
762             cvFindRuns(          numScanlines,
763                                         buffer_1,   /* prewarp image                            */
764                                         buffer_2,
765                                         lens_1,     /* prewarp line lens in pixels      */
766                                         lens_2,
767                                         runs_1,     /* first runs                                       */
768                                         runs_2,
769                                         num_runs_1, /* number of first runs                     */
770                                         num_runs_2);
771
772             cvDynamicCorrespondMulti(
773                                                 numScanlines,         // number of scanlines
774                                                 runs_1,         // s0|w0|s1|w1|...
775                                                 num_runs_1,    // numbers of runs
776                                                 runs_2,
777                                                 num_runs_2,
778                                                 corr_1,    // s0'|e0'|s1'|e1'|...
779
780                                                         corr_2 );
781         }
782         else
783         {
784 //#else
785             #ifndef PIXELTOPIXEL
786             m_morpher.SetPointCount( 200 );
787             #endif
788             m_morpher.SetLeftImage( image1 );
789             m_morpher.SetRightImage( image2 );
790 //#endif
791         }
792             HaveMorph   = true;
793
794         }
795
796         if( useScanlines == 1 )
797         {
798 //#if SCANLINE_VARIANT
799         cvMakeAlphaScanlines(    scanlines_1,
800                                         scanlines_2,
801                                         scanlines_a,
802                                         morph_lens,
803                                         numScanlines,
804                                         alpha);
805
806
807         cvMorphEpilinesMulti(
808                         numScanlines,       /* number of lines                              */
809                         buffer_1,       /* raster epilines from the first image         */
810                         lens_1,         /* number of pixel in first line                */
811                         buffer_2,       /* raster epilines from the second image        */
812                         lens_2,         /* number of pixel in second line               */
813                         morph_buffer,   /* raster epiline from the destination image    */
814                                         /* (it's an output parameter)                   */
815                         morph_lens,     /* numbers of pixel in output line              */
816                         alpha,          /* relative position of camera                  */
817                         runs_1,         /* first sequence of runs                       */
818                         num_runs_1,     /* it's length                                  */
819                         runs_2,         /* second sequence of runs                      */
820                         num_runs_2,     /*                                              */
821                         corr_1,         /* correspond information for the 1st seq       */
822                         corr_2);        /* correspond information for the 2nd seq       */
823         
824
825
826         memset(RasterResult->GetImage(),0,imgsize.width * imgsize.height*3);
827
828         cvPostWarpImage(
829                     numScanlines,                       /* number of scanlines  */
830                     morph_buffer,                   /* source buffers       */
831                     morph_lens,                     /* lens of buffers      */
832                     imageRes,       /* dest image           */
833                     scanlines_a                     /* scanline             */
834             );
835         
836         cvDeleteMoire(  imageRes);
837         }
838         else
839         {
840 //#else
841         m_morpher.SetPan( alpha );
842         {
843             IplImage* img = m_morpher.GetVirtualImage();
844             //IplImage* img = m_morpher.GetDisparityImage(); // if disparity image needed
845             //iplCopy( m_morpher.GetVirtualImage(), imageRes );
846             cvCopy( img, imageRes );
847         }
848 //#endif
849         }
850         ResultImage->RedrawWindow();
851
852         cvReleaseImageHeader(&image1);
853         cvReleaseImageHeader(&image2);
854         cvReleaseImageHeader(&imageRes);
855     }/* if CanMorph */
856 }
857
858
859 /*======================================================================================*/
860
861 CVMDemoDlg::~CVMDemoDlg()
862 {
863     if (RasterLeft      != 0)   delete RasterLeft;
864     if (RasterRight     != 0)   delete RasterRight;
865     if (RasterResult    != 0)   delete RasterResult;
866     if (LeftImage       != 0)   delete LeftImage;
867     if (RightImage      != 0)   delete RightImage;
868     if (ResultImage     != 0)   delete ResultImage;
869     FreeMemory();
870 }
871
872
873 /*======================================================================================*/
874
875 void CVMDemoDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
876 {
877     if (
878         ((CSliderCtrl*)(pScrollBar) == &m_camerapos)
879         ) {
880         if (abs(prevpos - m_camerapos.GetPos())>4 ) {
881
882             OnMakemorphing();
883         }
884     }
885     CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
886 }
887
888
889 /*======================================================================================*/
890
891 void CVMDemoDlg::OnOriginalsize()
892 {
893     SetWindowsToTop();
894 }
895
896
897 /*======================================================================================*/
898
899 void CVMDemoDlg::FreeMemory()
900 {
901
902     if (scanlines_1     != 0)   free (scanlines_1);
903     if (scanlines_2     != 0)   free (scanlines_2);
904     if (scanlines_a     != 0)   free (scanlines_a);
905
906     if (lens_1          != 0)   free (lens_1);
907     if (lens_2          != 0)   free (lens_2);
908     if (morph_lens      != 0)   free (morph_lens);
909
910     if (buffer_1        != 0)   free (buffer_1);
911     if (buffer_2        != 0)   free (buffer_2);
912     if (morph_buffer    != 0)   free (morph_buffer);
913
914     if (runs_1          != 0)   free (runs_1);
915     if (runs_2          != 0)   free (runs_2);
916
917     if (corr_1          != 0)   free (corr_1);
918     if (corr_2          != 0)   free (corr_2);
919
920     if (num_runs_1      != 0)   free (num_runs_1);
921     if (num_runs_2      != 0)   free (num_runs_2);
922
923
924     scanlines_1     = 0;
925     scanlines_2     = 0;
926     scanlines_a     = 0;
927
928     lens_1          = 0;
929     lens_2          = 0;
930     morph_lens      = 0;
931
932     runs_1          = 0;
933     runs_2          = 0;
934
935     buffer_1        = 0;
936     buffer_2        = 0;
937     morph_buffer    = 0;
938
939     num_runs_1      = 0;
940     num_runs_2      = 0;
941
942     corr_1          = 0;
943     corr_2          = 0;
944
945 }
946
947
948 /*======================================================================================*/
949
950 void CVMDemoDlg::AllocMemory()
951 {
952     CvSize     imgsize;
953
954     imgsize.width   = RasterLeft->GetWidth()*3;
955     imgsize.height  = RasterLeft->GetHeight()*3;
956
957     int numlines;
958
959     cvMakeScanlines(     0,
960                                 imgsize,
961                                 0,
962                                 0,
963                                 0,
964                                 0,
965                                 &numlines);
966
967     numlines=2400;
968     scanlines_1     = (int*)(calloc( numlines * 2, sizeof(int) * 4));
969     scanlines_2     = (int*)(calloc( numlines * 2, sizeof(int) * 4));
970     scanlines_a     = (int*)(calloc( numlines * 2, sizeof(int) * 4));
971
972     lens_1          = (int*)(calloc( numlines * 2, sizeof(int)*4));
973     lens_2          = (int*)(calloc( numlines * 2, sizeof(int)*4));
974     morph_lens      = (int*)(calloc( numlines * 2, sizeof(int)*4));
975
976     buffer_1        =
977         (uchar*)(malloc(imgsize.width * (imgsize.height+1) * 3 * sizeof(uchar)));
978
979     buffer_2        =
980         (uchar*)(malloc(imgsize.width * (imgsize.height+1) * 3 * sizeof(uchar)));
981
982     morph_buffer    =
983         (uchar*)(calloc(imgsize.width * (imgsize.height+1), 3 * sizeof(uchar)));
984
985     runs_1          = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
986     runs_2          = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
987
988     corr_1          = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
989     corr_2          = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
990
991     num_runs_1      = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
992     num_runs_2      = (int*)(calloc(imgsize.width * imgsize.height * 2, 2 * sizeof(int)));
993
994 }
995
996 /*======================================================================================*/
997
998 void CVMDemoDlg::OnShowscanlines() 
999 {
1000     bool canshow = (m_showscanlines.GetCheck()==1);
1001     LeftImage    ->showScanlines = canshow;
1002     RightImage   ->showScanlines = canshow;
1003     ResultImage ->showScanlines = canshow;
1004
1005     LeftImage    ->Invalidate();
1006     RightImage   ->Invalidate();
1007     ResultImage ->Invalidate();
1008
1009     // TODO: Add your control notification handler code here
1010         
1011 }