Changed indicator bg color.
[platform/framework/native/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 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 #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 FloatDimension
123 _GalleryBitmap::GetSize(void) const
124 {
125         FloatDimension dim;
126         dim.width = __pBitmap->GetWidth();
127         dim.height = __pBitmap->GetHeight();
128
129         return dim;
130 }
131
132 result
133 _GalleryBitmap::Initialize(const FloatDimension& 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                 SetAdjustBitmapFlag(true);
141         }
142
143         return E_SUCCESS;
144 }
145
146 result
147 _GalleryBitmap::AdjustBitmapRotate(void)
148 {
149         // TODO : need _Effect::RotateImageF()
150         Bitmap* pImage = null;
151         _Util::LockManager* pSrcLock = null;
152         _Util::LockManager* pDstLock = null;
153         Bitmap* pBitmap = null;
154         result r = E_SUCCESS;
155
156         Dimension dim;
157         int degree = 0;
158         switch (__rotation)
159         {
160         case GALLERY_INTERNAL_IMAGE_ROTATION_0:
161                 dim.SetSize(__pBitmap->GetWidth(), __pBitmap->GetHeight());
162                 break;
163         case GALLERY_INTERNAL_IMAGE_ROTATION_90:
164                 degree = 90;
165                 dim.SetSize(__pBitmap->GetHeight(), __pBitmap->GetWidth());
166                 break;
167         case GALLERY_INTERNAL_IMAGE_ROTATION_180:
168                 degree = 180;
169                 dim.SetSize(__pBitmap->GetWidth(), __pBitmap->GetHeight());
170                 break;
171         case GALLERY_INTERNAL_IMAGE_ROTATION_270:
172                 degree = 270;
173                 dim.SetSize(__pBitmap->GetHeight(), __pBitmap->GetWidth());
174                 break;
175         default:
176                 SysAssert(false);
177                 break;
178         }
179
180         if (__rotation != GALLERY_INTERNAL_IMAGE_ROTATION_0)
181         {
182                 pImage = new(std::nothrow) Bitmap();
183                 SysTryReturn(NID_UI_CTRL, pImage != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
184
185                 r = pImage->Construct(dim, __pBitmap->GetPixelColorFormat());
186                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating", GetErrorMessage(r));
187
188                 pSrcLock = new (std::nothrow) _Util::LockManager(*_BitmapImpl::GetInstance(*__pBitmap));
189                 SysTryCatch(NID_UI_CTRL, pSrcLock != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
190
191                 pDstLock = new (std::nothrow) _Util::LockManager(*_BitmapImpl::GetInstance(*pImage));
192                 SysTryCatch(NID_UI_CTRL, pDstLock != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
193
194                 {
195                         const BufferInfo& srcBufferInfo = pSrcLock->GetBufferInfo();
196                         const BufferInfo& dstBufferInfo = pDstLock->GetBufferInfo();
197
198                         _Util::Pixmap srcImage(srcBufferInfo.width, srcBufferInfo.height, srcBufferInfo.bitsPerPixel, srcBufferInfo.pPixels, srcBufferInfo.pitch);
199                         _Util::Pixmap dstImage(dstBufferInfo.width, dstBufferInfo.height, dstBufferInfo.bitsPerPixel, dstBufferInfo.pPixels, dstBufferInfo.pitch);
200
201                         _Effect::RotateDesc rotateDesc = { degree, __pBitmap->GetWidth() >> 1 , __pBitmap->GetHeight() >> 1}; //???
202                         bool pass = _Effect::RotateImage(dstImage, dim.width >> 1, dim.height >> 1, srcImage, rotateDesc, 255);
203                         //bool pass = _Effect::RotateImage(dstImage, dstBufferInfo.width >> 1, dstBufferInfo.height >> 1, srcImage, rotateDesc, 255);
204                         if (pass == true)
205                         {
206                                 pBitmap = new (std::nothrow) Bitmap();
207                                 SysTryCatch(NID_UI_CTRL, pBitmap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
208
209                                 Rectangle rect(0, 0, dim.width, dim.height);
210                                 r = pBitmap->Construct(*pImage, rect);
211                                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating", GetErrorMessage(r));
212
213                                 delete __pBitmap;
214                                 __pBitmap = pBitmap;
215                         }
216                         else
217                         {
218                                 r = GetLastResult();
219                         }
220                         delete pSrcLock;
221                         delete pDstLock;
222                 }
223                 delete pImage;
224         }
225
226         return r;
227
228 CATCH:
229         delete pBitmap;
230         delete pDstLock;
231         delete pSrcLock;
232         delete pImage;
233
234         return r;
235 }
236
237 bool
238 _GalleryBitmap::IsInitialize(void) const
239 {
240         return __bitmapSizeAndRotateAdjusted;
241 }
242
243 void
244 _GalleryBitmap::SetBitmapRotation(_GalleryImageRotation rotation)
245 {
246         __rotation = rotation;
247 }
248
249 _GalleryImageRotation
250 _GalleryBitmap::GetBitmapRotation(void) const
251 {
252         return __rotation;
253 }
254
255 Bitmap*
256 _GalleryBitmap::GetInternalBitmap(void) const
257 {
258         return __pBitmap;
259 }
260
261 void
262 _GalleryBitmap::SetAdjustBitmapFlag(bool initialize)
263 {
264         __bitmapSizeAndRotateAdjusted = initialize;
265 }
266
267 void
268 _GalleryBitmap::SetInternalBitmap(Bitmap* pBitmap)
269 {
270         __pBitmap = pBitmap;
271 }
272
273 }}} // Tizen::Ui::Controls