N_SE-45845 issue fix
[apps/native/sample/ImageFeatureManager.git] / project / src / FeatureSetGenerator.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "FeatureSetGenerator.h"
19
20 #include <FIo.h>
21 #include <FSysSystemTime.h>
22
23 FeatureSetGenThread::FeatureSetGenThread(void)
24     : __pListener(0)
25         , __pImageFeatureManager(0)
26     , __isGenerating(false)
27 {
28 }
29
30 FeatureSetGenThread::~FeatureSetGenThread(void)
31 {
32 }
33
34 result
35 FeatureSetGenThread::Construct(void)
36 {
37     return Thread::Construct();
38 }
39
40 Tizen::Base::Object*
41 FeatureSetGenThread::Run(void)
42 {
43     __resultInfo.appendTime = 0;
44     __resultInfo.totalObjectsNumber = 0;
45     __resultInfo.appendedObjectsNumber = 0;
46     __resultInfo.rejectedObjectsNumber = 0;
47
48     __isGenerating = true;
49
50     Generate(__imagePath, __resultInfo);
51
52     __isGenerating = false;
53
54     if (__pListener)
55     {
56         __pListener->OnFinish();
57     }
58
59     return null;
60 }
61
62 void
63 FeatureSetGenThread::SetListener(IFeatureSetGeneratorListener* pListener)
64 {
65     if (__pListener == pListener)
66     {
67         return;
68     }
69     __pListener = pListener;
70 }
71
72 bool
73 FeatureSetGenThread::Generate(Tizen::Base::String imPath, ResultInfo& resInfo)
74 {
75     bool isRes = true;
76     int currObject = 0;
77
78     if (!__pImageFeatureManager)
79     {
80         return false;
81     }
82
83     Tizen::Base::TimeSpan start(0, 0, 1), finish(0, 0, 1);
84     Tizen::System::SystemTime::GetUptime(start);
85     Tizen::Io::Directory dir;
86
87     result r = dir.Construct(imPath);
88     if (!IsFailed(r))
89     {
90
91         Tizen::Base::String folder(imPath);
92         if (folder[folder.GetLength() - 1] != '/')
93         {
94             folder += "/";
95         }
96
97         Tizen::Io::DirEnumerator* pDirEnum = dir.ReadN();
98         if (!pDirEnum)
99         {
100             return false;
101         }
102
103         while(pDirEnum->MoveNext() == E_SUCCESS)
104         {
105             Tizen::Io::DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
106             if (!dirEntry.IsDirectory())
107             {
108                 if (Tizen::Io::File::IsFileExist(imPath + "/" + dirEntry.GetName()))
109                 {
110                     __files.Add(imPath + "/" + dirEntry.GetName());
111                 }
112             }
113         }
114         pDirEnum->Reset();
115         delete pDirEnum;
116
117     }
118     else
119     {
120
121         if (CheckExt(imPath) && Tizen::Io::File::IsFileExist(imPath))
122         {
123             __files.Add(imPath);
124         }
125         else
126         {
127             return false;
128         }
129     }
130
131     StrComparer* pStrComparer = new StrComparer();
132     __files.Sort(*pStrComparer);
133     delete pStrComparer;
134
135     // Uses an enumerator to access elements in the list
136     Tizen::Base::Collection::IEnumeratorT<Tizen::Base::String>*    pEnum = __files.GetEnumeratorN();
137     Tizen::Base::String     name;
138
139     while (pEnum->MoveNext() == E_SUCCESS)
140     {
141         pEnum->GetCurrent(name);
142
143         ++currObject;
144         if (__pListener)
145         {
146             __pListener->OnImageProcessed(currObject, __files.GetCount());
147         }
148
149         if (!CheckExt(name))
150         {
151                 ++resInfo.rejectedObjectsNumber;
152                 continue;
153         }
154
155         if ( __pImageFeatureManager->AddFeature(name) >= 0)
156         {
157             ++resInfo.appendedObjectsNumber;
158         }
159         else
160         {
161             ++resInfo.rejectedObjectsNumber;
162         }
163     }
164     delete pEnum;
165     __files.RemoveAll();
166
167     Tizen::System::SystemTime::GetUptime(finish);
168     resInfo.appendTime = 60000 * (finish - start).GetMinutes() + 1000 * (finish - start).GetSeconds()
169         + (finish - start).GetMilliseconds();
170
171     resInfo.totalObjectsNumber = __pImageFeatureManager->GetTotalNumberOfFeatures();
172
173     return isRes;
174 }
175
176 void
177 FeatureSetGenThread::SetImagePath(const Tizen::Base::String& imPath)
178 {
179     __imagePath = imPath;
180 }
181
182 void
183 FeatureSetGenThread::GetResults(ResultInfo& resInfo)
184 {
185     resInfo = __resultInfo;
186 }
187
188 bool
189 FeatureSetGenThread::CheckExt(Tizen::Base::String fileName)
190 {
191     int period = 0;
192     const Tizen::Base::String period_symbol = ".";
193     if (E_SUCCESS != fileName.LastIndexOf(period_symbol, fileName.GetLength()-1, period))
194     {
195         return false;
196     }
197     Tizen::Base::String extension;
198     if (E_SUCCESS != fileName.SubString(period + 1, extension))
199     {
200         return false;
201     }
202
203     return ( (extension.Equals(L"jpg", false)) ||
204                  (extension.Equals(L"bmp", false)) ||
205                  (extension.Equals(L"png", false)) ||
206                  (extension.Equals(L"jpeg", false)));
207 }
208
209 void
210 FeatureSetGenThread::SetFeatureManager(Tizen::Uix::Vision::ImageFeatureManager* pImageFeatureManager)
211 {
212     __pImageFeatureManager = pImageFeatureManager;
213 }