87d137bccda4e63f4bdaf3495ff4e2ba69552ca5
[framework/osp/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     SysTryReturnResult(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         SysTryReturnResult(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
179 bool
180 _ImageFeatureManagerImpl::UpdateDB()
181 {
182     __indices.RemoveAll();
183
184     if (__imageFeatureManager->updateDB())
185     {
186         UpdateFeatureIndices();
187         return true;
188     }
189     else
190     {
191         return false;
192     }
193 }
194
195 bool
196 _ImageFeatureManagerImpl::OpenDB(const Tizen::Base::String& dbpath)
197 {
198     if (__imageFeatureManager)
199     {
200         delete __imageFeatureManager;
201     }
202     __imageFeatureManager = new sari2::ImageFeatureManager();
203     __imageFeatureManager->initDB();
204
205     Tizen::Text::Utf8Encoding utf8;
206     switch (__imageFeatureManager->openDB((const char*) utf8.GetBytesN(dbpath)->GetPointer()))
207     {
208     case 0: //ok
209         __path = dbpath;
210         UpdateFeatureIndices();
211         break;
212     case -2: //read only feature set file
213         __path = dbpath;
214         UpdateFeatureIndices();
215         __readOnly = true;
216         break;
217     default: //error
218         return false;
219     }
220
221     return true;
222 }
223
224 void
225 _ImageFeatureManagerImpl::ReleaseDB(void)
226 {
227     if (!__imageFeatureManager)
228     {
229         return;
230     }
231
232     __imageFeatureManager->releaseDB();
233     __indices.RemoveAll();
234 }
235
236 const Tizen::Uix::Vision::ImageFeatureInfo*
237 _ImageFeatureManagerImpl::GetImageFeatureInfo(int index)
238 {
239     if (!__imageFeatureManager)
240     {
241         return null;
242     }
243
244     if (index < __indices.GetCount() && index >= 0)
245     {
246         Tizen::Uix::Vision::ImageFeatureInfo* di = _ImageFeatureInfoImpl::CreateImageFeatureInfoN();
247         int realIndex(0);
248         __indices.GetAt(index, realIndex);
249         _ImageFeatureInfoImpl::GetInstance(di)->SetSariPointer(sari2::ImageFeatureInfo(__imageFeatureManager, realIndex));
250         return di;
251     }
252     else
253     {
254         SetLastResult(E_INVALID_ARG);
255         return null;
256     }
257 }
258
259 void
260 _ImageFeatureManagerImpl::UpdateFeatureIndices(void)
261 {
262     __indices.RemoveAll();
263
264     if (!__imageFeatureManager)
265     {
266         return;
267     }
268
269     int total = __imageFeatureManager->totalNumberOfImages();
270     for (int i = 0; i < total; i++)
271     {
272         if (sari2::ImageFeatureInfo(__imageFeatureManager, i).isActive())
273         {
274             __indices.Add(i);
275         }
276     }
277 }
278
279 } } } //Tizen::Uix::Vision