e71bafb08108c08eed11f69a32be844d6b070024
[platform/framework/native/vision.git] / src / FUixVisionImageFeatureManager.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 <FUixVisionImageFeatureManager.h>
19 #include "FUixVision_ImageFeatureManagerImpl.h"
20 #include <FMedia.h>
21 #include <FGraphics.h>
22 #include <FText.h>
23 #include <FBaseSysLog.h>
24
25 namespace Tizen { namespace Uix { namespace Vision
26 {
27
28 ImageFeatureManager::ImageFeatureManager(void)
29     : __pImageFeatureManagerImpl(0)
30 {
31 }
32
33 ImageFeatureManager::~ImageFeatureManager(void)
34 {
35     if (__pImageFeatureManagerImpl != null)
36     {
37         delete __pImageFeatureManagerImpl;
38         __pImageFeatureManagerImpl = null;
39     }
40 }
41
42 result
43 ImageFeatureManager::Construct(void)
44 {
45     SysAssertf(__pImageFeatureManagerImpl == null, "Already constructed! ",
46             "Calling Construct() twice or more on a same instance is not allowed for this class.");
47
48     std::unique_ptr<_ImageFeatureManagerImpl> pImageFeatureManagerImpl (new (std::nothrow) _ImageFeatureManagerImpl());
49     SysTryReturn(NID_UIX, pImageFeatureManagerImpl != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
50
51     __pImageFeatureManagerImpl = pImageFeatureManagerImpl.release();
52     __pImageFeatureManagerImpl->InitDB();
53     return E_SUCCESS;
54 }
55
56 result
57 ImageFeatureManager::DeleteFeature(int featureIndex)
58 {
59     SysAssertf(__pImageFeatureManagerImpl != null, "Not yet constructed! Consruct() should be called before use.");
60
61     SysTryReturn(NID_UIX, featureIndex >= 0, E_FAILURE, E_INVALID_ARG,
62             "index should be greater than 0. [E_INVALID_ARG]");
63
64     return __pImageFeatureManagerImpl->DeleteFeature(featureIndex) ? E_SUCCESS : E_FAILURE;
65 }
66
67 int
68 ImageFeatureManager::GetTotalNumberOfFeatures(void)
69 {
70     SysAssertf(__pImageFeatureManagerImpl != null, "Not yet constructed! Consruct() should be called before use.");
71     return __pImageFeatureManagerImpl->GetTotalNumberOfFeatures();
72 }
73
74 result
75 ImageFeatureManager::DeleteAllFeatures(void)
76 {
77     SysAssertf(__pImageFeatureManagerImpl != null, "Not yet constructed! Consruct() should be called before use.");
78     SysTryReturnResult(NID_UIX, __pImageFeatureManagerImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
79     __pImageFeatureManagerImpl->ReleaseDB();
80     __pImageFeatureManagerImpl->InitDB();
81     return E_SUCCESS;
82 }
83
84 int
85 ImageFeatureManager::AddFeature(const Tizen::Base::ByteBuffer& imageBuffer, int width, int height, const Tizen::Base::String& descr)
86 {
87
88     SysAssertf(__pImageFeatureManagerImpl != null, "Not yet constructed! Consruct() should be called before use.");
89
90     SysTryReturn(NID_UIX, width > 0 && height > 0, -1, E_INVALID_ARG,
91             "Width and height must me more than 0. [E_INVALID_ARG]");
92
93     const unsigned char* image = (unsigned char*) imageBuffer.GetPointer();
94
95     SysTryReturn(NID_UIX, image != null, -1, E_INVALID_ARG,
96             "image pointer must not be null. [E_INVALID_ARG]");
97
98     return (int) __pImageFeatureManagerImpl->AddFeature(image, width, height, descr);
99 }
100
101 int
102 ImageFeatureManager::AddFeature(const Tizen::Base::String& imagePath)
103 {
104     SysAssertf(__pImageFeatureManagerImpl != null, "Not yet constructed! Consruct() should be called before use.");
105
106     SysTryReturn(NID_UIX, !imagePath.IsEmpty(), -1, E_INVALID_ARG,
107             "image path must not be empty. [E_INVALID_ARG]");
108
109     SysTryReturn(NID_UIX, Tizen::Io::File::IsFileExist(imagePath), -1, E_INVALID_ARG,
110                 "image path must exist. [E_INVALID_ARG]");
111
112         Tizen::Io::FileAttributes _fileAtt;
113
114         SysTryReturn(NID_UIX, Tizen::Io::File::GetAttributes(imagePath, _fileAtt) == E_SUCCESS, -1, E_INVALID_ARG,
115                         "image file must have attributes. [E_INVALID_ARG]");
116
117         SysTryReturn(NID_UIX, !_fileAtt.IsDirectory(), -1, E_INVALID_ARG,
118                         "image file must exist. [E_INVALID_ARG]");
119
120     return (int) __pImageFeatureManagerImpl->AddFeature(imagePath);
121 }
122
123 result
124 ImageFeatureManager::Flush(const Tizen::Base::String* featureSetFilePath)
125 {
126     SysAssertf(__pImageFeatureManagerImpl != null, "Not yet constructed! Consruct() should be called before use.");
127
128     return __pImageFeatureManagerImpl->SaveDB(true, featureSetFilePath);
129 }
130
131 result
132 ImageFeatureManager::Load(const Tizen::Base::String& featureSetFilePath)
133 {
134     SysAssertf(__pImageFeatureManagerImpl != null, "Not yet constructed! Consruct() should be called before use.");
135
136     SysTryReturnResult(NID_UIX, !featureSetFilePath.IsEmpty(), E_INVALID_ARG,
137             "feature set path must not be empty. [E_INVALID_ARG]");
138
139     SysTryReturnResult(NID_UIX, Tizen::Io::File::IsFileExist(featureSetFilePath), E_INVALID_ARG,
140             "feature set path must exist. [E_INVALID_ARG]");
141
142         Tizen::Io::FileAttributes _fileAtt;
143
144     SysTryReturnResult(NID_UIX, Tizen::Io::File::GetAttributes(featureSetFilePath, _fileAtt) == E_SUCCESS, E_INVALID_ARG,
145             "feature set file must have attributes. [E_INVALID_ARG]");
146
147     SysTryReturnResult(NID_UIX, !_fileAtt.IsDirectory(), E_INVALID_ARG,
148             "feature set file must exist. [E_INVALID_ARG]");
149
150     return __pImageFeatureManagerImpl->OpenDB(featureSetFilePath) ? E_SUCCESS : E_FAILURE;
151 }
152
153 Tizen::Base::Collection::IListT<Tizen::Media::ImageFormat>*
154 ImageFeatureManager::GetSupportedImageFileFormatsListN(void)
155 {
156     std::unique_ptr<Tizen::Base::Collection::ArrayListT<Tizen::Media::ImageFormat> > pFormatList(new (std::nothrow) Tizen::Base::Collection::ArrayListT<Tizen::Media::ImageFormat>);
157     SysTryReturn(NID_UIX, pFormatList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
158
159     pFormatList->Add(Tizen::Media::IMG_FORMAT_BMP);
160     pFormatList->Add(Tizen::Media::IMG_FORMAT_JPG);
161     pFormatList->Add(Tizen::Media::IMG_FORMAT_PNG);
162     return pFormatList.release();
163 }
164
165 const Tizen::Uix::Vision::ImageFeatureInfo*
166 ImageFeatureManager::GetImageFeatureInfo(int index) const
167 {
168     SysAssertf(__pImageFeatureManagerImpl != null, "Not yet constructed! Consruct() should be called before use.");
169     return __pImageFeatureManagerImpl->GetImageFeatureInfo(index);
170 }
171
172 } } } //Tizen::Uix::Vision