Update the changelog
[opencv] / apps / HMMDemo / HMMDemoDoc.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*/// HMMDemoDoc.cpp : implementation of the CHMMDemoDoc class
41 //
42
43 #include "stdafx.h"
44 #include "HMMDemo.h"
45 #include "InfoDialogs.h"
46 #include "HMMDemoDoc.h"
47 #include "ImageBaseView.h"
48
49 #include "MainFrm.h"
50
51 #ifdef _DEBUG
52 #define new DEBUG_NEW
53 #undef THIS_FILE
54 static char THIS_FILE[] = __FILE__;
55 #endif
56
57 /////////////////////////////////////////////////////////////////////////////
58 // CHMMDemoDoc
59
60 IMPLEMENT_DYNCREATE(CHMMDemoDoc, CDocument)
61
62 BEGIN_MESSAGE_MAP(CHMMDemoDoc, CDocument)
63         //{{AFX_MSG_MAP(CHMMDemoDoc)
64         //}}AFX_MSG_MAP
65 END_MESSAGE_MAP()
66
67 /////////////////////////////////////////////////////////////////////////////
68 // CHMMDemoDoc construction/destruction
69
70 CHMMDemoDoc::CHMMDemoDoc()
71 {
72 }
73
74 CHMMDemoDoc::~CHMMDemoDoc()
75 {
76 }
77
78 BOOL CHMMDemoDoc::OnNewDocument()
79 {
80         if (!CDocument::OnNewDocument())
81                 return FALSE;
82
83     m_base.SetBaseView( GetImageBaseView() );
84     m_base.SetFileName("untitled_base.txt");
85     m_base.SetModified(false);
86
87         return TRUE;
88 }
89
90
91 /////////////////////////////////////////////////////////////////////////////
92 // CHMMDemoDoc serialization
93
94 BOOL CHMMDemoDoc::OnOpenDocument(LPCTSTR path)
95 {
96     bool result = false;
97     
98     int length = strlen( path );
99     const char* tmp = path + length - 3;
100
101     if( _stricmp(tmp,"bmp") == 0 || _stricmp(tmp,"jpg") == 0 || 
102         _stricmp(tmp-1,"jpeg") == 0 )
103     {
104         CHMMDemoView* camera_view = GetCameraView();
105         CCamera& camera = camera_view->Camera();
106
107         if ( camera.IsRunning() ) camera.Stop();
108
109         //load image from disk
110         CImage& image = camera.GetFrame();
111         image.Load( path, 1 );
112
113         camera_view->Invalidate();
114         camera_view->SetSelection(0); 
115         
116     }
117     else if( _stricmp(tmp,"txt") == 0 )
118     {   
119         DeleteContents();
120         SetModifiedFlag();  // dirty during de-serialize
121
122         m_base.SetBaseView( GetImageBaseView() );
123
124         {
125             CWaitCursor wait;
126             m_base.SetFileName( path );
127             result = m_base.Load();
128         }
129
130         if( !result )
131         {
132             m_base.SetFileName("");
133             AfxMessageBox("Failed to open database");
134         }
135         else
136         {
137             SetModifiedFlag(FALSE);     // start off with unmodified
138
139             //try to read appropriate config file.
140             //create cfg file name
141             CString cfg_name = path;
142             cfg_name.Replace( ".txt", "CFG.txt" );
143             
144             //reset parameters (HMM, Sampling etc.) for whole application
145             CHMMDemoApp* app = (CHMMDemoApp*)AfxGetApp();
146             app->LoadConfig( cfg_name, true );
147         }
148     }
149     return  result;
150 }
151
152
153 BOOL CHMMDemoDoc::OnSaveDocument(LPCTSTR path)
154 {
155     bool result = false;
156
157     CWaitCursor wait;
158     result = m_base.Save();
159
160     if( !result )
161     {
162         AfxMessageBox("Failed to save database");
163     }
164
165     SetModifiedFlag(FALSE);     // start off with unmodified
166     return  result;
167 }
168
169
170 /////////////////////////////////////////////////////////////////////////////
171 // CHMMDemoDoc commands
172
173 void CHMMDemoDoc::DeleteContents() 
174 {
175     m_base.Unload();
176     CDocument::DeleteContents();
177 }
178
179
180 CPerson* CHMMDemoDoc::AskPersonName()
181 {
182     CPerson* person = 0;
183     CPersonName dlg( m_base );
184
185     if( dlg.DoModal() == IDOK )
186     {
187         CString name = dlg.GetPersonName();
188         if( name.GetLength() != 0 )
189         {
190             person = m_base.FindPersonByName( name );
191             if( !person )
192             {
193                 person = m_base.AddPerson( name, 0, false );
194                 if( !person )
195                 {
196                     AfxMessageBox("Can't create person folder\n");
197                 }
198             }
199         }
200     }
201
202     return person;
203 }
204
205 void  CHMMDemoDoc::AddObj( CImage& img, CRect roi, CStringList* otherImages )
206 {
207     CImageBaseView* view = GetImageBaseView();
208
209     if( view )
210     {
211         int new_person_index = 0;
212         int index = view->GetPersonIndex();
213         CPerson* person = 0;
214
215         if( index < 0 ) // no person selected
216         {
217             person = AskPersonName();
218             if( !person ) return;
219             
220             new_person_index = m_base.GetPersonIndex( person );
221         }
222         else
223         {
224             person = GetFaceBase().GetPerson(index);
225             if( !person )
226             {
227                 ASSERT( 0 );
228                 return;
229             }
230         }
231
232         person->AddImage( 0, &img, roi );
233         if( otherImages )
234         {
235             POSITION pos = otherImages->GetHeadPosition();
236             //skip first image, bacause it have already beeb added 
237             otherImages->GetNext( pos );
238
239             while( pos )
240             {
241                 CString path = otherImages->GetNext(pos);
242                 char drive[1024], dir[1024];
243                 _splitpath( path, drive, dir, 0, 0 );
244                 strcat( drive, dir );
245                 if( _stricmp( drive, person->GetFolder()) == 0 )
246                 {
247                     person->AddImage( path, 0, CRect(0,0,0,0));
248                 }
249                 else
250                 {
251                     CImage img;
252                     img.Load( path, 0 );
253                     person->AddImage( 0, &img, CRect(0,0,0,0));
254                 }
255             }
256         }
257        
258         if( index >= 0 )
259             view->RefreshView();
260         else
261             view->SwitchMode( new_person_index, true );
262     }
263 }
264
265 bool  CHMMDemoDoc::RemoveObj( int person_index, int active_index )
266 {
267     bool removed = false;
268     
269     if( person_index >= 0 )
270     {
271         CPerson* person = m_base.GetPerson( person_index );
272         if( person )
273         {
274             POSITION pos = person->GetImgList().FindIndex( active_index );
275             if( pos )
276             {
277                 int res = AfxMessageBox( "Remove image?", MB_YESNO );
278                 if( res == IDYES )
279                 {
280                     person->RemoveImage( pos );
281                     removed = true;
282                     
283                 }
284             }
285         }
286     }
287     else
288     {
289         POSITION pos = m_base.GetPersonList().FindIndex( active_index );
290         if( pos )
291         {
292             int res = AfxMessageBox( "Remove person from the base?\n"
293                                      "(files will not be deleted physically)",
294                                      MB_YESNO );
295             if( res == IDYES )
296             {
297                 m_base.RemovePerson( pos );
298                 removed = true;
299             }
300         }
301     }
302
303     return removed;
304 }
305
306 void CHMMDemoDoc::ChangeBaseParams()
307 {
308     CBaseInfo dlg( m_base );
309
310     if( dlg.DoModal() == IDOK )
311     {
312         m_base.SetName( dlg.GetBaseName());
313         m_base.SetFileName( dlg.GetFileName());
314         m_base.SetModified();
315     }
316 }
317
318 void  CHMMDemoDoc::DeleteHMMInfo( int person_index )
319 {
320     if( person_index >= 0 )
321     {
322         CPerson* person = m_base.GetPerson(person_index);
323         if( person )
324         {
325             person->DeleteHMMInfo();
326         }
327     }
328     else
329     {
330         m_base.DeleteHMMInfo();
331     }
332 }
333