2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "FUixVision_ImageFeatureManagerImpl.h"
19 #include "FUixVision_ImageFeatureInfoImpl.h"
22 #include <FBaseSysLog.h>
25 namespace Tizen { namespace Uix { namespace Vision
28 _ImageFeatureManagerImpl::_ImageFeatureManagerImpl(void)
29 : __imageFeatureManager(0)
34 _ImageFeatureManagerImpl::~_ImageFeatureManagerImpl(void)
36 if (__imageFeatureManager)
38 delete __imageFeatureManager;
42 _ImageFeatureManagerImpl*
43 _ImageFeatureManagerImpl::GetInstance(ImageFeatureManager& imageFeatureManager)
45 return imageFeatureManager.__pImageFeatureManagerImpl;
48 const _ImageFeatureManagerImpl*
49 _ImageFeatureManagerImpl::GetInstance(const ImageFeatureManager& imageFeatureManager)
51 return imageFeatureManager.__pImageFeatureManagerImpl;
55 _ImageFeatureManagerImpl::InitDB(void)
57 __indices.RemoveAll();
58 if (__imageFeatureManager)
60 delete __imageFeatureManager;
62 __imageFeatureManager = new sari2::ImageFeatureManager();
63 __imageFeatureManager->initDB();
67 _ImageFeatureManagerImpl::AddFeature(const Tizen::Base::String& filepath)
69 Tizen::Media::ImageBuffer imBuf;
70 result r = imBuf.Construct(filepath, null, false);
76 Tizen::Base::ByteBuffer* yuvBuf = imBuf.GetByteBufferN(Tizen::Media::MEDIA_PIXEL_FORMAT_YUV420P);
82 int res = AddFeature(yuvBuf->GetPointer(), imBuf.GetWidth(), imBuf.GetHeight(), filepath);
89 _ImageFeatureManagerImpl::AddFeature(const unsigned char* data, int width, int height, const Tizen::Base::String& info)
91 if (!__imageFeatureManager)
95 Tizen::Text::Utf8Encoding utf8;
96 int index = (int) __imageFeatureManager->addImageToDB(data, width, height, (char*) utf8.GetBytesN(info)->GetPointer());
100 __indices.Add(index);
101 return __indices.GetCount() - 1;
107 _ImageFeatureManagerImpl::DeleteFeature(int index)
109 if (!__imageFeatureManager)
114 if (index < __indices.GetCount() && index >= 0)
117 __indices.GetAt(index, realIndex);
118 if (__imageFeatureManager->deleteImageFromDB(realIndex))
120 __indices.RemoveAt(index);
126 SetLastResult(E_INVALID_ARG);
133 _ImageFeatureManagerImpl::GetTotalNumberOfFeatures(void)
135 return __indices.GetCount();
139 _ImageFeatureManagerImpl::SaveDB(bool optimizeDatabase, const Tizen::Base::String* dbPath)
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]");
146 __indices.RemoveAll();
147 result result = E_FAILURE;
149 if (!__imageFeatureManager)
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;
164 if (!__path.IsEmpty())
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)
170 result = E_INVALID_ARG;
179 _ImageFeatureManagerImpl::UpdateDB()
181 __indices.RemoveAll();
183 if (__imageFeatureManager->updateDB())
185 UpdateFeatureIndices();
195 _ImageFeatureManagerImpl::OpenDB(const Tizen::Base::String& dbpath)
197 if (__imageFeatureManager)
199 delete __imageFeatureManager;
201 __imageFeatureManager = new sari2::ImageFeatureManager();
202 __imageFeatureManager->initDB();
204 Tizen::Text::Utf8Encoding utf8;
205 switch (__imageFeatureManager->openDB((const char*) utf8.GetBytesN(dbpath)->GetPointer()))
209 UpdateFeatureIndices();
211 case -2: //read only feature set file
213 UpdateFeatureIndices();
224 _ImageFeatureManagerImpl::ReleaseDB(void)
226 if (!__imageFeatureManager)
231 __imageFeatureManager->releaseDB();
232 __indices.RemoveAll();
235 const Tizen::Uix::Vision::ImageFeatureInfo*
236 _ImageFeatureManagerImpl::GetImageFeatureInfo(int index)
238 if (!__imageFeatureManager)
243 if (index < __indices.GetCount() && index >= 0)
245 Tizen::Uix::Vision::ImageFeatureInfo* di = _ImageFeatureInfoImpl::CreateImageFeatureInfoN();
247 __indices.GetAt(index, realIndex);
248 _ImageFeatureInfoImpl::GetInstance(di)->SetSariPointer(sari2::ImageFeatureInfo(__imageFeatureManager, realIndex));
253 SetLastResult(E_INVALID_ARG);
259 _ImageFeatureManagerImpl::UpdateFeatureIndices(void)
261 __indices.RemoveAll();
263 if (!__imageFeatureManager)
268 int total = __imageFeatureManager->totalNumberOfImages();
269 for (int i = 0; i < total; i++)
271 if (sari2::ImageFeatureInfo(__imageFeatureManager, i).isActive())
278 } } } //Tizen::Uix::Vision