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)
31 , __minSupportedSize(100)
32 , __maxSupportedResolution(16000000)
36 _ImageFeatureManagerImpl::~_ImageFeatureManagerImpl(void)
38 if (__imageFeatureManager)
40 delete __imageFeatureManager;
44 _ImageFeatureManagerImpl*
45 _ImageFeatureManagerImpl::GetInstance(ImageFeatureManager& imageFeatureManager)
47 return imageFeatureManager.__pImageFeatureManagerImpl;
50 const _ImageFeatureManagerImpl*
51 _ImageFeatureManagerImpl::GetInstance(const ImageFeatureManager& imageFeatureManager)
53 return imageFeatureManager.__pImageFeatureManagerImpl;
57 _ImageFeatureManagerImpl::InitDB(void)
59 __indices.RemoveAll();
60 if (__imageFeatureManager)
62 delete __imageFeatureManager;
64 __imageFeatureManager = new sari2::ImageFeatureManager();
65 __imageFeatureManager->initDB();
69 _ImageFeatureManagerImpl::AddFeature(const Tizen::Base::String& filepath)
71 Tizen::Media::ImageFormat imgFormat;
72 int imgWidth = 0, imgHeight = 0;
73 result r = Tizen::Media::ImageBuffer::GetImageInfo(filepath, imgFormat, imgWidth, imgHeight);
77 SetLastResult(E_INVALID_ARG);
81 if ((imgWidth < __minSupportedSize || imgHeight < __minSupportedSize) || (imgWidth * imgHeight > __maxSupportedResolution))
83 SetLastResult(E_INVALID_ARG);
87 Tizen::Media::ImageBuffer imBuf;
88 r = imBuf.Construct(filepath, null, false);
92 SetLastResult(E_INVALID_ARG);
96 Tizen::Base::ByteBuffer* yuvBuf = imBuf.GetByteBufferN(Tizen::Media::MEDIA_PIXEL_FORMAT_YUV420P);
102 int res = AddFeature(yuvBuf->GetPointer(), imBuf.GetWidth(), imBuf.GetHeight(), filepath);
109 _ImageFeatureManagerImpl::AddFeature(const unsigned char* data, int width, int height, const Tizen::Base::String& info)
111 if (!__imageFeatureManager)
116 if ((width < __minSupportedSize || height < __minSupportedSize) || (width * height > __maxSupportedResolution))
118 SetLastResult(E_INVALID_ARG);
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());
128 __indices.Add(index);
129 return __indices.GetCount() - 1;
135 _ImageFeatureManagerImpl::DeleteFeature(int index)
137 if (!__imageFeatureManager)
142 if (index < __indices.GetCount() && index >= 0)
145 __indices.GetAt(index, realIndex);
146 if (__imageFeatureManager->deleteImageFromDB(realIndex))
148 __indices.RemoveAt(index);
154 SetLastResult(E_INVALID_ARG);
161 _ImageFeatureManagerImpl::GetTotalNumberOfFeatures(void)
163 return __indices.GetCount();
167 _ImageFeatureManagerImpl::SaveDB(bool optimizeDatabase, const Tizen::Base::String* dbPath)
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]");
174 __indices.RemoveAll();
175 result result = E_FAILURE;
177 if (!__imageFeatureManager)
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;
193 if (!__path.IsEmpty())
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)
200 result = E_INVALID_ARG;
209 _ImageFeatureManagerImpl::UpdateDB()
211 __indices.RemoveAll();
213 if (__imageFeatureManager->updateDB())
215 UpdateFeatureIndices();
225 _ImageFeatureManagerImpl::OpenDB(const Tizen::Base::String& dbpath)
227 if (__imageFeatureManager)
229 delete __imageFeatureManager;
231 __imageFeatureManager = new sari2::ImageFeatureManager();
232 __imageFeatureManager->initDB();
234 Tizen::Text::Utf8Encoding utf8;
235 std::unique_ptr<Tizen::Base::ByteBuffer> pBuffer(utf8.GetBytesN(dbpath));
236 switch (__imageFeatureManager->openDB((const char*) pBuffer->GetPointer()))
240 UpdateFeatureIndices();
242 case -2: //read only feature set file
244 UpdateFeatureIndices();
255 _ImageFeatureManagerImpl::ReleaseDB(void)
257 if (!__imageFeatureManager)
262 __imageFeatureManager->releaseDB();
263 __indices.RemoveAll();
266 const Tizen::Uix::Vision::ImageFeatureInfo*
267 _ImageFeatureManagerImpl::GetImageFeatureInfo(int index)
269 if (!__imageFeatureManager)
274 if (index < __indices.GetCount() && index >= 0)
276 Tizen::Uix::Vision::ImageFeatureInfo* di = _ImageFeatureInfoImpl::CreateImageFeatureInfoN();
278 __indices.GetAt(index, realIndex);
279 _ImageFeatureInfoImpl::GetInstance(di)->SetSariPointer(sari2::ImageFeatureInfo(__imageFeatureManager, realIndex));
284 SetLastResult(E_INVALID_ARG);
290 _ImageFeatureManagerImpl::UpdateFeatureIndices(void)
292 __indices.RemoveAll();
294 if (!__imageFeatureManager)
299 int total = __imageFeatureManager->totalNumberOfImages();
300 for (int i = 0; i < total; i++)
302 if (sari2::ImageFeatureInfo(__imageFeatureManager, i).isActive())
309 } } } //Tizen::Uix::Vision