Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_GalleryImageReader.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 <FBaseBuffer.h>
19 #include <FGrpBitmap.h>
20 #include <FMediaImageTypes.h>
21 #include <FBaseSysLog.h>
22 #include <FGrp_BitmapImpl.h>
23 #include <FMedia_ImageDecoder.h>
24 #include "FUiCtrl_GalleryImageReader.h"
25 #include "FUiCtrl_GalleryBitmap.h"
26 #include "FUiCtrl_GalleryModel.h"
27 #include "FUiCtrl_GalleryItem.h"
28
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Graphics;
32 using namespace Tizen::Media;
33
34 namespace Tizen { namespace Ui { namespace Controls {
35
36 _GalleryImageReader::_GalleryImageReader(_GalleryModel& galleryModel)
37         : __galleryModel(galleryModel)
38         , __pTmpFileBitmap(null)
39         , __currentLoadBitmapIndex(-1)
40         , __currentLoadBitmapSize(0, 0)
41 {
42         // Do nothing
43 }
44
45 _GalleryImageReader::~_GalleryImageReader(void)
46 {
47         delete __pTmpFileBitmap;
48 }
49
50 _GalleryBitmap*
51 _GalleryImageReader::GetPartialImageFromFileN(int index, const Rectangle& partialBounds, const Dimension& imageSize) const
52 {
53         SysTryReturn(NID_UI_CTRL, index >= 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The index argument is negative value.");
54         SysTryReturn(NID_UI_CTRL, index < __galleryModel.GetItemCount(), null, E_OUT_OF_RANGE,
55                                 "[E_OUT_OF_RANGE] The index argument is out of range");
56
57         if (__currentLoadBitmapIndex != index || __currentLoadBitmapSize != imageSize)
58         {
59                 const_cast<_GalleryImageReader*>(this)->LoadPartialImageFromFile(index, imageSize);
60         }
61
62         _GalleryBitmap* pPartialGalleryBitmap = null;
63         Bitmap* pPartialBitmap = new(std::nothrow) Bitmap();
64         SysTryReturn(NID_UI_CTRL, pPartialBitmap != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
65
66         result r = pPartialBitmap->Construct(*__pTmpFileBitmap, partialBounds);
67         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
68
69         pPartialGalleryBitmap = _GalleryBitmap::CreateGalleryBitmapN(pPartialBitmap);
70         SysTryCatch(NID_UI_CTRL, pPartialGalleryBitmap != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
71
72         SetLastResult(E_SUCCESS);
73         delete pPartialBitmap;
74
75         return pPartialGalleryBitmap;
76
77 CATCH:
78         //delete pPartialGalleryBitmap;
79         delete pPartialBitmap;
80
81         return null;
82 }
83
84 _GalleryBitmap*
85 _GalleryImageReader::GetItemImage(int index) const
86 {
87         SysTryReturn(NID_UI_CTRL, index >= 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The index argument is negative value.");
88         SysTryReturn(NID_UI_CTRL, index < __galleryModel.GetItemCount(), null, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index argument is out of range.");
89
90         _GalleryItem* pItem = __galleryModel.GetItem(index);
91         SysTryReturn(NID_UI_CTRL, pItem != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
92
93         return pItem->GetGalleryItemImage();
94 }
95
96 void
97 _GalleryImageReader::LoadPartialImageFromFile(int index, const Dimension& imageSize)
98 {
99         _GalleryBitmap* pPartialGalleryBitmap = null;
100         Dimension bitmapSize;
101         ByteBuffer* pBuffer = null;
102         MediaPixelFormat mediaPixelFormat = MEDIA_PIXEL_FORMAT_NONE;
103         result r = E_SUCCESS;
104
105         _GalleryItem* pItem = __galleryModel.GetItem(index);
106         SysTryCatch(NID_UI_CTRL, pItem != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
107
108         if (pItem->GetGalleryItemImage() == null)
109         {
110                 SysTryCatch(NID_UI_CTRL, false, , E_SYSTEM, "[E_SYSTEM] The gallery bitmap not exist.");
111         }
112
113         switch (pItem->GetGalleryItemImage()->GetInternalBitmap()->GetPixelColorFormat())
114         {
115         case BITMAP_PIXEL_FORMAT_RGB565:
116                 mediaPixelFormat = MEDIA_PIXEL_FORMAT_RGB565LE;
117                 break;
118         case BITMAP_PIXEL_FORMAT_ARGB8888:
119                 mediaPixelFormat = MEDIA_PIXEL_FORMAT_BGRA8888;
120                 break;
121         case BITMAP_PIXEL_FORMAT_R8G8B8A8:
122                 mediaPixelFormat = MEDIA_PIXEL_FORMAT_RGBA8888;
123                 break;
124         default:
125                 SysAssert(false);
126                 break;
127         }
128
129         pBuffer = _ImageDecoder::DecodeToBufferN(pItem->GetGalleryItemFilePath(), mediaPixelFormat, bitmapSize.width, bitmapSize.height);
130         r = GetLastResult();
131         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
132
133         delete __pTmpFileBitmap;
134         __pTmpFileBitmap = new (std::nothrow) Bitmap();
135         SysTryCatch(NID_UI_CTRL, __pTmpFileBitmap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
136
137         r = __pTmpFileBitmap->Construct(*pBuffer, bitmapSize, pItem->GetGalleryItemImage()->GetInternalBitmap()->GetPixelColorFormat());
138         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
139
140         pPartialGalleryBitmap = _GalleryBitmap::CreateGalleryBitmapN(__pTmpFileBitmap);
141         SysTryCatch(NID_UI_CTRL, pPartialGalleryBitmap != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
142
143         pPartialGalleryBitmap->SetBitmapRotation(pItem->GetGalleryRotation());
144
145         r = pPartialGalleryBitmap->Initialize(imageSize, GALLERY_FITTING_TYPE_FIT);
146         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
147
148         r = pPartialGalleryBitmap->SetSize(imageSize);
149         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
150
151         delete __pTmpFileBitmap;
152         __pTmpFileBitmap = _BitmapImpl::CloneN(*pPartialGalleryBitmap->GetInternalBitmap());
153         SysTryCatch(NID_UI_CTRL, __pTmpFileBitmap != null, , r, "[%s] Propagating.", GetErrorMessage(r));
154
155         SetLastResult(E_SUCCESS);
156
157         __currentLoadBitmapIndex = index;
158         __currentLoadBitmapSize = imageSize;
159
160         delete pBuffer;
161         delete pPartialGalleryBitmap;
162         return;
163
164 CATCH:
165         delete pBuffer;
166         delete __pTmpFileBitmap;
167         __pTmpFileBitmap = null;
168         delete pPartialGalleryBitmap;
169 }
170
171 }}} // Tizen::Ui::Controls