Fork for IVI: mesa fixing
[profile/ivi/uifw.git] / src / ui / controls / FUiCtrl_GalleryBitmap.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 #include <FBaseErrors.h>
18 #include <FGrpBitmap.h>
19 #include <FBaseSysLog.h>
20 #include <FGrp_Effect.h>
21 #include <FGrp_BitmapImpl.h>
22 #include <../util/FGrp_Util.h>
23 #include "FUi_Math.h"
24 #include "FUiCtrl_GalleryBitmap.h"
25
26
27 using namespace Tizen::Graphics;
28
29 namespace Tizen { namespace Ui { namespace Controls {
30
31 _GalleryBitmap::_GalleryBitmap(void)
32         : __pBitmap(null)
33         , __rotation(GALLERY_INTERNAL_IMAGE_ROTATION_0)
34         , __bitmapSizeAndRotateAdjusted(false)
35 {
36         // Do nothing
37 }
38
39 _GalleryBitmap::~_GalleryBitmap(void)
40 {
41         delete __pBitmap;
42 }
43
44 _GalleryBitmap*
45 _GalleryBitmap::CloneN(_GalleryBitmap* pBitmap)
46 {
47         Bitmap* pInternalBitmap = null;
48         _GalleryBitmap* pCloneBitmap = new(std::nothrow) _GalleryBitmap();
49         SysTryCatch(NID_UI_CTRL, pCloneBitmap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
50
51         pInternalBitmap = _BitmapImpl::CloneN(*pBitmap->GetInternalBitmap());
52         SysTryCatch(NID_UI_CTRL, pInternalBitmap != null, , GetLastResult(), "[%s] Propagating", GetErrorMessage(GetLastResult()));
53
54         pCloneBitmap->SetInternalBitmap(pInternalBitmap);
55
56         pCloneBitmap->SetAdjustBitmapFlag(pBitmap->IsInitialize());
57
58         pCloneBitmap->SetBitmapRotation(pBitmap->GetBitmapRotation());
59
60         SetLastResult(E_SUCCESS);
61
62         return pCloneBitmap;
63
64 CATCH:
65         delete pCloneBitmap;
66
67         return null;
68 }
69
70 _GalleryBitmap*
71 _GalleryBitmap::CreateGalleryBitmapN(Bitmap* pBitmap)
72 {
73         Bitmap* pInternalBitmap = null;
74         _GalleryBitmap* pCloneBitmap = new(std::nothrow) _GalleryBitmap();
75         SysTryCatch(NID_UI_CTRL, pCloneBitmap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
76
77         pInternalBitmap = _BitmapImpl::CloneN(*pBitmap);
78         SysTryCatch(NID_UI_CTRL, pInternalBitmap != null, , GetLastResult(), "[%s] Propagating", GetErrorMessage(GetLastResult()));
79
80         pCloneBitmap->SetInternalBitmap(pInternalBitmap);
81
82         SetLastResult(E_SUCCESS);
83
84         return pCloneBitmap;
85
86 CATCH:
87         delete pCloneBitmap;
88
89         return null;
90 }
91
92 result
93 _GalleryBitmap::SetSize(const Dimension& size)
94 {
95         if (size.width == GetSize().width && size.height == GetSize().height)
96         {
97                 return E_SUCCESS;
98         }
99
100         Dimension scaledSize = size;
101         float width = GetSize().width;
102         float height = GetSize().height;
103
104         float widthFactor = (float)size.width / width;
105         float heightFactor = (float)size.height / height;
106
107         if (widthFactor > heightFactor)
108         {
109                 scaledSize.width = width * heightFactor + 0.5f;
110         }
111         else
112         {
113                 scaledSize.height = height * widthFactor + 0.5f;
114         }
115
116         result r = __pBitmap->Scale(scaledSize);
117         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
118
119         return E_SUCCESS;
120 }
121
122 Dimension
123 _GalleryBitmap::GetSize(void) const
124 {
125         Dimension dim;
126         dim.width = __pBitmap->GetWidth();
127         dim.height = __pBitmap->GetHeight();
128
129         return dim;
130 }
131
132 result
133 _GalleryBitmap::Initialize(const Dimension& size, _GalleryFittingType fittingType)
134 {
135         if (IsInitialize() == false)
136         {
137                 result r = AdjustBitmapRotate();
138                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
139
140                 r = AdjustBitmapSize(size, fittingType);
141                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
142
143                 SetAdjustBitmapFlag(true);
144         }
145
146         return E_SUCCESS;
147 }
148
149 result
150 _GalleryBitmap::AdjustBitmapSize(const Dimension& size, _GalleryFittingType fittingType)
151 {
152         Dimension scaledSize = size;
153         if (fittingType == GALLERY_FITTING_TYPE_NONE)
154         {
155                 return E_SUCCESS;
156         }
157
158         if (fittingType != GALLERY_FITTING_TYPE_FIT_XY)
159         {
160                 float width = GetSize().width;
161                 float height = GetSize().height;
162
163                 float widthFactor = (float)size.width / width;
164                 float heightFactor = (float)size.height / height;
165
166                 if (fittingType == GALLERY_FITTING_TYPE_STRECTABLE)
167                 {
168                         if (widthFactor < 1.0f && heightFactor < 1.0f)
169                         {
170                                 return E_SUCCESS;
171                         }
172                 }
173                 else if (fittingType == GALLERY_FITTING_TYPE_SHRINKABLE)
174                 {
175                         if (widthFactor > 1.0f && heightFactor > 1.0f)
176                         {
177                                 return E_SUCCESS;
178                         }
179                 }
180
181                 if (widthFactor > heightFactor)
182                 {
183                         scaledSize.width = width * heightFactor + 0.5f;
184                 }
185                 else
186                 {
187                         scaledSize.height = height * widthFactor + 0.5f;
188                 }
189         }
190
191         result r = __pBitmap->Scale(scaledSize);
192         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
193
194         return E_SUCCESS;
195 }
196
197 result
198 _GalleryBitmap::AdjustBitmapRotate(void)
199 {
200         Bitmap* pImage = null;
201         _Util::LockManager* pSrcLock = null;
202         _Util::LockManager* pDstLock = null;
203         Bitmap* pBitmap = null;
204         result r = E_SUCCESS;
205
206         Dimension dim;
207         int degree = 0;
208         switch (__rotation)
209         {
210         case GALLERY_INTERNAL_IMAGE_ROTATION_0:
211                 dim.SetSize(__pBitmap->GetWidth(), __pBitmap->GetHeight());
212                 break;
213         case GALLERY_INTERNAL_IMAGE_ROTATION_90:
214                 degree = 90;
215                 dim.SetSize(__pBitmap->GetHeight(), __pBitmap->GetWidth());
216                 break;
217         case GALLERY_INTERNAL_IMAGE_ROTATION_180:
218                 degree = 180;
219                 dim.SetSize(__pBitmap->GetWidth(), __pBitmap->GetHeight());
220                 break;
221         case GALLERY_INTERNAL_IMAGE_ROTATION_270:
222                 degree = 270;
223                 dim.SetSize(__pBitmap->GetHeight(), __pBitmap->GetWidth());
224                 break;
225         default:
226                 SysAssert(false);
227                 break;
228         }
229
230         if (__rotation != GALLERY_INTERNAL_IMAGE_ROTATION_0)
231         {
232                 pImage = new(std::nothrow) Bitmap();
233                 SysTryReturn(NID_UI_CTRL, pImage != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
234
235                 r = pImage->Construct(dim, __pBitmap->GetPixelColorFormat());
236                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating", GetErrorMessage(r));
237
238                 pSrcLock = new (std::nothrow) _Util::LockManager(*_BitmapImpl::GetInstance(*__pBitmap));
239                 SysTryCatch(NID_UI_CTRL, pSrcLock != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
240
241                 pDstLock = new (std::nothrow) _Util::LockManager(*_BitmapImpl::GetInstance(*pImage));
242                 SysTryCatch(NID_UI_CTRL, pDstLock != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
243
244                 {
245                         const BufferInfo& srcBufferInfo = pSrcLock->GetBufferInfo();
246                         const BufferInfo& dstBufferInfo = pDstLock->GetBufferInfo();
247
248                         _Util::Pixmap srcImage(srcBufferInfo.width, srcBufferInfo.height, srcBufferInfo.bitsPerPixel, srcBufferInfo.pPixels, srcBufferInfo.pitch);
249                         _Util::Pixmap dstImage(dstBufferInfo.width, dstBufferInfo.height, dstBufferInfo.bitsPerPixel, dstBufferInfo.pPixels, dstBufferInfo.pitch);
250
251                         _Effect::RotateDesc rotateDesc = { degree, __pBitmap->GetWidth() >> 1 , __pBitmap->GetHeight() >> 1}; //???
252                         bool pass = _Effect::RotateImage(dstImage, dim.width >> 1, dim.height >> 1, srcImage, rotateDesc, 255);
253                         //bool pass = _Effect::RotateImage(dstImage, dstBufferInfo.width >> 1, dstBufferInfo.height >> 1, srcImage, rotateDesc, 255);
254                         if (pass == true)
255                         {
256                                 pBitmap = new (std::nothrow) Bitmap();
257                                 SysTryCatch(NID_UI_CTRL, pBitmap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
258
259                                 Rectangle rect(0, 0, dim.width, dim.height);
260                                 r = pBitmap->Construct(*pImage, rect);
261                                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating", GetErrorMessage(r));
262
263                                 delete __pBitmap;
264                                 __pBitmap = pBitmap;
265                         }
266                         else
267                         {
268                                 r = GetLastResult();
269                         }
270                         delete pSrcLock;
271                         delete pDstLock;
272                 }
273                 delete pImage;
274         }
275
276         return r;
277
278 CATCH:
279         delete pBitmap;
280         delete pDstLock;
281         delete pSrcLock;
282         delete pImage;
283
284         return r;
285 }
286
287 bool
288 _GalleryBitmap::IsInitialize(void) const
289 {
290         return __bitmapSizeAndRotateAdjusted;
291 }
292
293 void
294 _GalleryBitmap::SetBitmapRotation(_GalleryImageRotation rotation)
295 {
296         __rotation = rotation;
297 }
298
299 _GalleryImageRotation
300 _GalleryBitmap::GetBitmapRotation(void) const
301 {
302         return __rotation;
303 }
304
305 Bitmap*
306 _GalleryBitmap::GetInternalBitmap(void) const
307 {
308         return __pBitmap;
309 }
310
311 void
312 _GalleryBitmap::SetAdjustBitmapFlag(bool initialize)
313 {
314         __bitmapSizeAndRotateAdjusted = initialize;
315 }
316
317 void
318 _GalleryBitmap::SetInternalBitmap(Bitmap* pBitmap)
319 {
320         __pBitmap = pBitmap;
321 }
322
323 }}} // Tizen::Ui::Controls