SysTry -> SysSecureTry
[platform/framework/native/vision.git] / src / FUixVision_ImageFeatureManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 "FUixVision_ImageFeatureManagerImpl.h"
19 #include "FUixVision_ImageFeatureInfoImpl.h"
20 #include "FText.h"
21 #include <FMedia.h>
22 #include <FBaseSysLog.h>
23 #include <string>
24
25 namespace Tizen { namespace Uix { namespace Vision
26 {
27
28 _ImageFeatureManagerImpl::_ImageFeatureManagerImpl(void)
29     : __imageFeatureManager(0)
30     , __readOnly(false)
31 {
32 }
33
34 _ImageFeatureManagerImpl::~_ImageFeatureManagerImpl(void)
35 {
36     if (__imageFeatureManager)
37     {
38         delete __imageFeatureManager;
39     }
40 }
41
42 _ImageFeatureManagerImpl*
43 _ImageFeatureManagerImpl::GetInstance(ImageFeatureManager& imageFeatureManager)
44 {
45     return imageFeatureManager.__pImageFeatureManagerImpl;
46 }
47
48 const _ImageFeatureManagerImpl*
49 _ImageFeatureManagerImpl::GetInstance(const ImageFeatureManager& imageFeatureManager)
50 {
51     return imageFeatureManager.__pImageFeatureManagerImpl;
52 }
53
54 void
55 _ImageFeatureManagerImpl::InitDB(void)
56 {
57     __indices.RemoveAll();
58     if (__imageFeatureManager)
59     {
60         delete __imageFeatureManager;
61     }
62     __imageFeatureManager = new sari2::ImageFeatureManager();
63     __imageFeatureManager->initDB();
64 }
65
66 int
67 _ImageFeatureManagerImpl::AddFeature(const Tizen::Base::String& filepath)
68 {
69     Tizen::Media::ImageBuffer imBuf;
70     result r = imBuf.Construct(filepath, null, false);
71     if (IsFailed(r))
72     {
73         return -1;
74     }
75
76     Tizen::Base::ByteBuffer* yuvBuf = imBuf.GetByteBufferN(Tizen::Media::MEDIA_PIXEL_FORMAT_YUV420P);
77     if (yuvBuf == null)
78     {
79         return -1;
80     }
81
82     int res = AddFeature(yuvBuf->GetPointer(), imBuf.GetWidth(), imBuf.GetHeight(), filepath);
83     delete yuvBuf;
84
85     return res;
86 }
87
88 int
89 _ImageFeatureManagerImpl::AddFeature(const unsigned char* data, int width, int height, const Tizen::Base::String& info)
90 {
91     if (!__imageFeatureManager)
92     {
93         return -1;
94     }
95     Tizen::Text::Utf8Encoding utf8;
96     int index = (int) __imageFeatureManager->addImageToDB(data, width, height, (char*) utf8.GetBytesN(info)->GetPointer());
97
98     if (index > -1)
99     {
100         __indices.Add(index);
101         return __indices.GetCount() - 1;
102     }
103     return -1;
104 }
105
106 bool
107 _ImageFeatureManagerImpl::DeleteFeature(int index)
108 {
109     if (!__imageFeatureManager)
110     {
111         return false;
112     }
113
114     if (index < __indices.GetCount() && index >= 0)
115     {
116         int realIndex(0);
117         __indices.GetAt(index, realIndex);
118         if (__imageFeatureManager->deleteImageFromDB(realIndex))
119         {
120             __indices.RemoveAt(index);
121             return true;
122         }
123     }
124     else
125     {
126         SetLastResult(E_INVALID_ARG);
127     }
128     
129     return false;
130 }
131
132 unsigned int
133 _ImageFeatureManagerImpl::GetTotalNumberOfFeatures(void)
134 {
135     return __indices.GetCount();
136 }
137
138 result
139 _ImageFeatureManagerImpl::SaveDB(bool optimizeDatabase, const Tizen::Base::String* dbPath)
140 {
141     SysSecureTryReturnResult(NID_UIX, (!__path.IsEmpty()) || (null != dbPath), E_INVALID_ARG,
142             "You need to open some database first or specify path to save data. [E_INVALID_ARG]");
143
144     UpdateDB();
145
146     __indices.RemoveAll();
147     result result = E_FAILURE;
148
149     if (!__imageFeatureManager)
150     {
151         return E_FAILURE;
152     }
153
154
155     if (null != dbPath)
156     {
157         SysSecureTryReturnResult(NID_UIX, !dbPath->IsEmpty(), E_INVALID_ARG,
158                 "database path must not be empty. [E_INVALID_ARG]");
159         Tizen::Text::Utf8Encoding utf8;
160         result = __imageFeatureManager->saveDB(optimizeDatabase, (const char*) utf8.GetBytesN(*dbPath)->GetPointer()) ? E_SUCCESS : E_FAILURE;
161     }
162     else
163     {
164         if (!__path.IsEmpty())
165         {
166             Tizen::Text::Utf8Encoding utf8;
167             result = __imageFeatureManager->saveDB(optimizeDatabase, (const char*) utf8.GetBytesN(__path)->GetPointer()) ? E_SUCCESS : E_FAILURE;
168             if (E_FAILURE == result && __readOnly)
169             {
170                 result = E_INVALID_ARG;
171             }
172         }
173     }
174
175     return result;
176 }
177
178 bool
179 _ImageFeatureManagerImpl::UpdateDB()
180 {
181     __indices.RemoveAll();
182
183     if (__imageFeatureManager->updateDB())
184     {
185         UpdateFeatureIndices();
186         return true;
187     }
188     else
189     {
190         return false;
191     }
192 }
193
194 bool
195 _ImageFeatureManagerImpl::OpenDB(const Tizen::Base::String& dbpath)
196 {
197     if (__imageFeatureManager)
198     {
199         delete __imageFeatureManager;
200     }
201     __imageFeatureManager = new sari2::ImageFeatureManager();
202     __imageFeatureManager->initDB();
203
204     Tizen::Text::Utf8Encoding utf8;
205     switch (__imageFeatureManager->openDB((const char*) utf8.GetBytesN(dbpath)->GetPointer()))
206     {
207     case 0: //ok
208         __path = dbpath;
209         UpdateFeatureIndices();
210         break;
211     case -2: //read only feature set file
212         __path = dbpath;
213         UpdateFeatureIndices();
214         __readOnly = true;
215         break;
216     default: //error
217         return false;
218     }
219
220     return true;
221 }
222
223 void
224 _ImageFeatureManagerImpl::ReleaseDB(void)
225 {
226     if (!__imageFeatureManager)
227     {
228         return;
229     }
230
231     __imageFeatureManager->releaseDB();
232     __indices.RemoveAll();
233 }
234
235 const Tizen::Uix::Vision::ImageFeatureInfo*
236 _ImageFeatureManagerImpl::GetImageFeatureInfo(int index)
237 {
238     if (!__imageFeatureManager)
239     {
240         return null;
241     }
242
243     if (index < __indices.GetCount() && index >= 0)
244     {
245         Tizen::Uix::Vision::ImageFeatureInfo* di = _ImageFeatureInfoImpl::CreateImageFeatureInfoN();
246         int realIndex(0);
247         __indices.GetAt(index, realIndex);
248         _ImageFeatureInfoImpl::GetInstance(di)->SetSariPointer(sari2::ImageFeatureInfo(__imageFeatureManager, realIndex));
249         return di;
250     }
251     else
252     {
253         SetLastResult(E_INVALID_ARG);
254         return null;
255     }
256 }
257
258 void
259 _ImageFeatureManagerImpl::UpdateFeatureIndices(void)
260 {
261     __indices.RemoveAll();
262
263     if (!__imageFeatureManager)
264     {
265         return;
266     }
267
268     int total = __imageFeatureManager->totalNumberOfImages();
269     for (int i = 0; i < total; i++)
270     {
271         if (sari2::ImageFeatureInfo(__imageFeatureManager, i).isActive())
272         {
273             __indices.Add(i);
274         }
275     }
276 }
277
278 } } } //Tizen::Uix::Vision