Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / FGrpBitmap.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 /*
19  * @file        FGrpBitmap.cpp
20  * @brief       This is the implementation file for Bitmap class.
21  *
22  */
23
24 #include <new>
25
26 #include <FGrpBitmap.h>
27
28 #include <FBaseSysLog.h>
29
30 #include "FGrp_BitmapImpl.h"
31 #include "FGrp_BitmapUtil.h"
32 #include "util/FGrp_Util.h"
33
34
35 #define CHECK_INSTANCE \
36         SysTryReturnResult(NID_GRP, this->__pImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Fails to allocate memory.")
37
38 #define CHECK_INSTANCE_EX(_result) \
39         SysTryReturn(NID_GRP, this->__pImpl != null, _result, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Fails to allocate memory.")
40
41 #define CHECK_INSTANCE_VOID     \
42         SysTryReturnVoidResult(NID_GRP, this->__pImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Fails to allocate memory.")
43
44
45 #define CHECK_NOT_CONSTRUCTED \
46         CHECK_INSTANCE; \
47         SysAssertf(!this->__pImpl->IsConstructed(), \
48                 "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
49
50 #define CHECK_CONSTRUCTED \
51         SysAssertf((this->__pImpl != null) && this->__pImpl->IsConstructed(), \
52                 "Not yet constructed. Construct() should be called before use.");
53
54 #define CHECK_CONSTRUCTED_EX(_result) \
55         SysAssertf((this->__pImpl != null) && this->__pImpl->IsConstructed(), \
56                 "Not yet constructed. Construct() should be called before use.");
57
58 #define CHECK_CONSTRUCTED_VOID \
59         SysAssertf((this->__pImpl != null) && this->__pImpl->IsConstructed(), \
60                 "Not yet constructed. Construct() should be called before use.");
61
62
63 namespace Tizen { namespace Graphics
64 {
65
66 Bitmap::Bitmap(void)
67         : __pImpl(new (std::nothrow)_BitmapImpl)
68 {
69 }
70
71 Bitmap::~Bitmap(void)
72 {
73         delete __pImpl;
74 }
75
76 result
77 Bitmap::Construct(const Rectangle& vc_rect)
78 {
79         CHECK_NOT_CONSTRUCTED;
80
81         result r = this->__pImpl->Construct(vc_rect);
82
83         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
84
85         return E_SUCCESS;
86 }
87
88 result
89 Bitmap::Construct(const Dimension& vc_dim, BitmapPixelFormat pixelFormat)
90 {
91         CHECK_NOT_CONSTRUCTED;
92
93         result r = this->__pImpl->Construct(vc_dim, pixelFormat);
94
95         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
96
97         return E_SUCCESS;
98 }
99
100 result
101 Bitmap::Construct(const Canvas& canvas, const Rectangle& vc_rect)
102 {
103         CHECK_NOT_CONSTRUCTED;
104
105         SysTryReturnResult(NID_GRP, _Util::CheckValidity(&canvas), E_INVALID_ARG, "The source canvas is invalid.\n");
106
107         result r = this->__pImpl->Construct(*_CanvasImpl::GetInstance(canvas), vc_rect);
108
109         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
110
111         return E_SUCCESS;
112 }
113
114 result
115 Bitmap::Construct(const Bitmap& bitmap, const Rectangle& vc_rect)
116 {
117         CHECK_NOT_CONSTRUCTED;
118
119         SysTryReturnResult(NID_GRP, _Util::CheckValidity(&bitmap), E_INVALID_ARG, "The source bitmap is invalid.\n");
120
121         const Tizen::Graphics::_BitmapImpl& impl = *bitmap.__pImpl;
122
123         result r = this->__pImpl->Construct(impl, vc_rect);
124
125         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
126
127         return E_SUCCESS;
128 }
129
130 result
131 Bitmap::Construct(const Tizen::Base::ByteBuffer& buffer, const Dimension& rq_dim, BitmapPixelFormat pixelFormat)
132 {
133         CHECK_NOT_CONSTRUCTED;
134
135         // notice!!
136         result r = this->Construct(buffer, rq_dim, pixelFormat, true);
137
138         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
139
140         return E_SUCCESS;
141 }
142
143 result
144 Bitmap::Construct(const Tizen::Base::ByteBuffer& buffer, const Dimension& rq_dim, BitmapPixelFormat pixelFormat, bool autoScaling)
145 {
146         CHECK_NOT_CONSTRUCTED;
147
148         result r = this->__pImpl->Construct(buffer, rq_dim, pixelFormat, autoScaling);
149
150         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
151
152         return E_SUCCESS;
153 }
154
155 result
156 Bitmap::Construct(const byte* pBuffer, int bufSize, const Dimension& rq_dim, BitmapPixelFormat pixelFormat, bool autoScaling)
157 {
158         CHECK_NOT_CONSTRUCTED;
159
160         result r = this->__pImpl->Construct(pBuffer, bufSize, rq_dim, pixelFormat, autoScaling);
161
162         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
163
164         return E_SUCCESS;
165 }
166
167 result
168 Bitmap::Construct(const Tizen::Base::ByteBuffer& buffer, const Dimension& rq_dim, BitmapPixelFormat pixelFormat,
169                                   BufferScaling bufferScaling)
170 {
171         CHECK_NOT_CONSTRUCTED;
172
173         result r = this->__pImpl->Construct(buffer, rq_dim, pixelFormat, bufferScaling);
174
175         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
176
177         return E_SUCCESS;
178 }
179
180 result
181 Bitmap::Scale(const Dimension& vc_dim)
182 {
183         CHECK_CONSTRUCTED;
184
185         result r = this->__pImpl->Scale(vc_dim);
186
187         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
188
189         return E_SUCCESS;
190 }
191
192 result
193 Bitmap::Merge(const Point& vc_dest, const Bitmap& src, const Rectangle& vc_srcRect)
194 {
195         CHECK_CONSTRUCTED;
196
197         SysTryReturnResult(NID_GRP, _Util::CheckValidity(&src), E_INVALID_ARG, "The source bitmap is invalid.\n");
198
199         const Tizen::Graphics::_BitmapImpl& impl = *src.__pImpl;
200
201         result r = this->__pImpl->Merge(vc_dest, impl, vc_srcRect);
202
203         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
204
205         return E_SUCCESS;
206 }
207
208 int
209 Bitmap::GetHeight() const
210 {
211         CHECK_CONSTRUCTED_EX(-1);
212
213         return this->__pImpl->GetHeight();
214 }
215
216 int
217 Bitmap::GetWidth() const
218 {
219         CHECK_CONSTRUCTED_EX(-1);
220
221         return this->__pImpl->GetWidth();
222 }
223
224 int
225 Bitmap::GetBitsPerPixel() const
226 {
227         CHECK_CONSTRUCTED_EX(-1);
228
229         return this->__pImpl->GetBitsPerPixel();
230 }
231
232 BitmapPixelFormat
233 Bitmap::GetPixelColorFormat() const
234 {
235         CHECK_CONSTRUCTED_EX(BITMAP_PIXEL_FORMAT_MAX);
236
237         return this->__pImpl->GetPixelColorFormat();
238 }
239
240 result
241 Bitmap::SetScalingQuality(BitmapScalingQuality quality)
242 {
243         CHECK_CONSTRUCTED;
244
245         result r = this->__pImpl->SetScalingQuality(quality);
246
247         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r,"[%s] Propagating.", GetErrorMessage(r));
248
249         return E_SUCCESS;
250 }
251
252 BitmapScalingQuality
253 Bitmap::GetScalingQuality(void) const
254 {
255         CHECK_CONSTRUCTED_EX(BITMAP_SCALING_QUALITY_LOW);
256
257         return this->__pImpl->GetScalingQuality();
258 }
259
260 result
261 Bitmap::SetMaskingColor(const Color* pColor)
262 {
263         CHECK_CONSTRUCTED;
264
265         result r = this->__pImpl->SetMaskingColor(pColor);
266
267         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
268
269         return E_SUCCESS;
270 }
271
272 result
273 Bitmap::GetMaskingColor(Color& color) const
274 {
275         CHECK_CONSTRUCTED;
276
277         result r = this->__pImpl->GetMaskingColor(color);
278
279         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
280
281         return E_SUCCESS;
282 }
283
284 void
285 Bitmap::SetAlphaConstant(int opacity)
286 {
287         CHECK_CONSTRUCTED_VOID;
288
289         return this->__pImpl->SetAlphaConstant(opacity);
290 }
291
292 int
293 Bitmap::GetAlphaConstant(void) const
294 {
295         CHECK_CONSTRUCTED_EX(-1);
296
297         return this->__pImpl->GetAlphaConstant();
298 }
299
300 bool
301 Bitmap::IsNinePatchedBitmap(void) const
302 {
303         CHECK_CONSTRUCTED_EX(false);
304
305         return this->__pImpl->IsNinePatchedBitmap();
306 }
307
308 result
309 Bitmap::Lock(BufferInfo& info, long timeout)
310 {
311         CHECK_CONSTRUCTED;
312
313         result r = this->__pImpl->Lock(info, timeout);
314
315         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
316
317         return E_SUCCESS;
318 }
319
320 result
321 Bitmap::Unlock()
322 {
323         CHECK_CONSTRUCTED;
324
325         result r = this->__pImpl->Unlock();
326
327         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
328
329         return E_SUCCESS;
330 }
331
332 Bitmap*
333 Bitmap::GetNonScaledBitmapN(const Tizen::Base::ByteBuffer& buffer, const Dimension& dim, BitmapPixelFormat pixelFormat)
334 {
335         _BitmapImpl* pBitmapImpl = _BitmapImpl::GetNonScaledBitmapImplN(buffer, dim, pixelFormat);
336
337         if (pBitmapImpl == null)
338         {
339                 return null;
340         }
341
342         Bitmap* pReturnBitmap = Tizen::Graphics::_BitmapUtil::CreateBitmapN(pBitmapImpl);
343
344         if (pReturnBitmap == null)
345         {
346                 result r = GetLastResult();
347
348                 SysTryReturn(NID_GRP, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
349
350                 return null;
351         }
352
353         return pReturnBitmap;
354 }
355
356 }} // Tizen::Graphics