Merge commits Tizen 2.2 to Tizen 2.2.1
[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     , __minSupportedSize(100)
32     , __maxSupportedResolution(16000000)
33 {
34 }
35
36 _ImageFeatureManagerImpl::~_ImageFeatureManagerImpl(void)
37 {
38     if (__imageFeatureManager)
39     {
40         delete __imageFeatureManager;
41     }
42 }
43
44 _ImageFeatureManagerImpl*
45 _ImageFeatureManagerImpl::GetInstance(ImageFeatureManager& imageFeatureManager)
46 {
47     return imageFeatureManager.__pImageFeatureManagerImpl;
48 }
49
50 const _ImageFeatureManagerImpl*
51 _ImageFeatureManagerImpl::GetInstance(const ImageFeatureManager& imageFeatureManager)
52 {
53     return imageFeatureManager.__pImageFeatureManagerImpl;
54 }
55
56 void
57 _ImageFeatureManagerImpl::InitDB(void)
58 {
59     __indices.RemoveAll();
60     if (__imageFeatureManager)
61     {
62         delete __imageFeatureManager;
63     }
64     __imageFeatureManager = new sari2::ImageFeatureManager();
65     __imageFeatureManager->initDB();
66 }
67
68 int
69 _ImageFeatureManagerImpl::AddFeature(const Tizen::Base::String& filepath)
70 {
71     Tizen::Media::ImageFormat imgFormat;
72     int imgWidth = 0, imgHeight = 0;
73     result r = Tizen::Media::ImageBuffer::GetImageInfo(filepath, imgFormat, imgWidth, imgHeight);
74
75     if (IsFailed(r))
76     {
77         SetLastResult(E_INVALID_ARG);
78         return -1;
79     }
80
81     if ((imgWidth < __minSupportedSize || imgHeight < __minSupportedSize) || (imgWidth * imgHeight > __maxSupportedResolution))
82     {
83         SetLastResult(E_INVALID_ARG);
84         return -1;
85     }
86
87     Tizen::Media::ImageBuffer imBuf;
88     r = imBuf.Construct(filepath, null, false);
89
90     if(IsFailed(r))
91     {
92         SetLastResult(E_INVALID_ARG);
93         return -1;
94     }
95
96     Tizen::Base::ByteBuffer* yuvBuf = imBuf.GetByteBufferN(Tizen::Media::MEDIA_PIXEL_FORMAT_YUV420P);
97     if (yuvBuf == null)
98     {
99         return -1;
100     }
101
102     int res = AddFeature(yuvBuf->GetPointer(), imBuf.GetWidth(), imBuf.GetHeight(), filepath);
103     delete yuvBuf;
104
105     return res;
106 }
107
108 int
109 _ImageFeatureManagerImpl::AddFeature(const unsigned char* data, int width, int height, const Tizen::Base::String& info)
110 {
111     if (!__imageFeatureManager)
112     {
113         return -1;
114     }
115
116     if ((width < __minSupportedSize || height < __minSupportedSize) || (width * height > __maxSupportedResolution))
117     {
118         SetLastResult(E_INVALID_ARG); 
119         return -1;
120     }
121
122     Tizen::Text::Utf8Encoding utf8;
123     std::unique_ptr<Tizen::Base::ByteBuffer> pBuffer(utf8.GetBytesN(info));
124     int index = (int) __imageFeatureManager->addImageToDB(data, width, height, (char*) pBuffer->GetPointer());
125
126     if (index > -1)
127     {
128         __indices.Add(index);
129         return __indices.GetCount() - 1;
130     }
131     return -1;
132 }
133
134 bool
135 _ImageFeatureManagerImpl::DeleteFeature(int index)
136 {
137     if (!__imageFeatureManager)
138     {
139         return false;
140     }
141
142     if (index < __indices.GetCount() && index >= 0)
143     {
144         int realIndex(0);
145         __indices.GetAt(index, realIndex);
146         if (__imageFeatureManager->deleteImageFromDB(realIndex))
147         {
148             __indices.RemoveAt(index);
149             return true;
150         }
151     }
152     else
153     {
154         SetLastResult(E_INVALID_ARG);
155     }
156
157     return false;
158 }
159
160 unsigned int
161 _ImageFeatureManagerImpl::GetTotalNumberOfFeatures(void)
162 {
163     return __indices.GetCount();
164 }
165
166 result
167 _ImageFeatureManagerImpl::SaveDB(bool optimizeDatabase, const Tizen::Base::String* dbPath)
168 {
169     SysSecureTryReturnResult(NID_UIX, (!__path.IsEmpty()) || (null != dbPath), E_INVALID_ARG,
170             "You need to open some database first or specify path to save data. [E_INVALID_ARG]");
171
172     UpdateDB();
173
174     __indices.RemoveAll();
175     result result = E_FAILURE;
176
177     if (!__imageFeatureManager)
178     {
179         return E_FAILURE;
180     }
181
182
183     if (null != dbPath)
184     {
185         SysSecureTryReturnResult(NID_UIX, !dbPath->IsEmpty(), E_INVALID_ARG,
186                 "database path must not be empty. [E_INVALID_ARG]");
187         Tizen::Text::Utf8Encoding utf8;
188         std::unique_ptr<Tizen::Base::ByteBuffer> pBuffer(utf8.GetBytesN(*dbPath));
189         result = __imageFeatureManager->saveDB(optimizeDatabase, (const char*) pBuffer->GetPointer()) ? E_SUCCESS : E_FAILURE;
190     }
191     else
192     {
193         if (!__path.IsEmpty())
194         {
195             Tizen::Text::Utf8Encoding utf8;
196             std::unique_ptr<Tizen::Base::ByteBuffer> pBuffer(utf8.GetBytesN(__path));
197             result = __imageFeatureManager->saveDB(optimizeDatabase, (const char*) pBuffer->GetPointer()) ? E_SUCCESS : E_FAILURE;
198             if (E_FAILURE == result && __readOnly)
199             {
200                 result = E_INVALID_ARG;
201             }
202         }
203     }
204
205     return result;
206 }
207
208 bool
209 _ImageFeatureManagerImpl::UpdateDB()
210 {
211     __indices.RemoveAll();
212
213     if (__imageFeatureManager->updateDB())
214     {
215         UpdateFeatureIndices();
216         return true;
217     }
218     else
219     {
220         return false;
221     }
222 }
223
224 bool
225 _ImageFeatureManagerImpl::OpenDB(const Tizen::Base::String& dbpath)
226 {
227     if (__imageFeatureManager)
228     {
229         delete __imageFeatureManager;
230     }
231     __imageFeatureManager = new sari2::ImageFeatureManager();
232     __imageFeatureManager->initDB();
233
234     Tizen::Text::Utf8Encoding utf8;
235     std::unique_ptr<Tizen::Base::ByteBuffer> pBuffer(utf8.GetBytesN(dbpath));
236     switch (__imageFeatureManager->openDB((const char*) pBuffer->GetPointer()))
237     {
238     case 0: //ok
239         __path = dbpath;
240         UpdateFeatureIndices();
241         break;
242     case -2: //read only feature set file
243         __path = dbpath;
244         UpdateFeatureIndices();
245         __readOnly = true;
246         break;
247     default: //error
248         return false;
249     }
250
251     return true;
252 }
253
254 void
255 _ImageFeatureManagerImpl::ReleaseDB(void)
256 {
257     if (!__imageFeatureManager)
258     {
259         return;
260     }
261
262     __imageFeatureManager->releaseDB();
263     __indices.RemoveAll();
264 }
265
266 const Tizen::Uix::Vision::ImageFeatureInfo*
267 _ImageFeatureManagerImpl::GetImageFeatureInfo(int index)
268 {
269     if (!__imageFeatureManager)
270     {
271         return null;
272     }
273
274     if (index < __indices.GetCount() && index >= 0)
275     {
276         Tizen::Uix::Vision::ImageFeatureInfo* di = _ImageFeatureInfoImpl::CreateImageFeatureInfoN();
277         int realIndex(0);
278         __indices.GetAt(index, realIndex);
279         _ImageFeatureInfoImpl::GetInstance(di)->SetSariPointer(sari2::ImageFeatureInfo(__imageFeatureManager, realIndex));
280         return di;
281     }
282     else
283     {
284         SetLastResult(E_INVALID_ARG);
285         return null;
286     }
287 }
288
289 void
290 _ImageFeatureManagerImpl::UpdateFeatureIndices(void)
291 {
292     __indices.RemoveAll();
293
294     if (!__imageFeatureManager)
295     {
296         return;
297     }
298
299     int total = __imageFeatureManager->totalNumberOfImages();
300     for (int i = 0; i < total; i++)
301     {
302         if (sari2::ImageFeatureInfo(__imageFeatureManager, i).isActive())
303         {
304             __indices.Add(i);
305         }
306     }
307 }
308
309 } } } //Tizen::Uix::Vision