e304b506eb7ada937e92cf076606585524c4e284
[platform/framework/native/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 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 <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,
52                 const FloatRectangle& partialBounds, const FloatDimension& imageSize) const
53 {
54         SysTryReturn(NID_UI_CTRL, index >= 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The index argument is negative value.");
55         SysTryReturn(NID_UI_CTRL, index < __galleryModel.GetItemCount(), null, E_OUT_OF_RANGE,
56                                 "[E_OUT_OF_RANGE] The index argument is out of range");
57
58         if (__currentLoadBitmapIndex != index || __currentLoadBitmapSize != imageSize)
59         {
60                 const_cast<_GalleryImageReader*>(this)->LoadPartialImageFromFile(index, imageSize);
61         }
62
63         _GalleryBitmap* pPartialGalleryBitmap = null;
64         Bitmap* pPartialBitmap = new(std::nothrow) Bitmap();
65         SysTryReturn(NID_UI_CTRL, pPartialBitmap != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
66
67         result r = pPartialBitmap->Construct(*__pTmpFileBitmap, partialBounds);
68         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
69
70         pPartialGalleryBitmap = _GalleryBitmap::CreateGalleryBitmapN(pPartialBitmap);
71         SysTryCatch(NID_UI_CTRL, pPartialGalleryBitmap != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
72
73         SetLastResult(E_SUCCESS);
74         delete pPartialBitmap;
75
76         return pPartialGalleryBitmap;
77
78 CATCH:
79         //delete pPartialGalleryBitmap;
80         delete pPartialBitmap;
81
82         return null;
83 }
84
85 _GalleryBitmap*
86 _GalleryImageReader::GetItemImage(int index) const
87 {
88         SysTryReturn(NID_UI_CTRL, index >= 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The index argument is negative value.");
89         SysTryReturn(NID_UI_CTRL, index < __galleryModel.GetItemCount(), null, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The index argument is out of range.");
90
91         _GalleryItem* pItem = __galleryModel.GetItem(index);
92         SysTryReturn(NID_UI_CTRL, pItem != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
93
94         return pItem->GetGalleryItemImage();
95 }
96
97 void
98 _GalleryImageReader::LoadPartialImageFromFile(int index, const FloatDimension& imageSize)
99 {
100         _GalleryBitmap* pPartialGalleryBitmap = null;
101         Dimension bitmapSize;
102         ByteBuffer* pBuffer = null;
103         MediaPixelFormat mediaPixelFormat = MEDIA_PIXEL_FORMAT_NONE;
104         result r = E_SUCCESS;
105
106         _GalleryItem* pItem = __galleryModel.GetItem(index);
107         SysTryCatch(NID_UI_CTRL, pItem != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
108
109         if (pItem->GetGalleryItemImage() == null)
110         {
111                 SysTryCatch(NID_UI_CTRL, false, , E_SYSTEM, "[E_SYSTEM] The gallery bitmap not exist.");
112         }
113
114         switch (pItem->GetGalleryItemImage()->GetInternalBitmap()->GetPixelColorFormat())
115         {
116         case BITMAP_PIXEL_FORMAT_RGB565:
117                 mediaPixelFormat = MEDIA_PIXEL_FORMAT_RGB565LE;
118                 break;
119         case BITMAP_PIXEL_FORMAT_ARGB8888:
120                 mediaPixelFormat = MEDIA_PIXEL_FORMAT_BGRA8888;
121                 break;
122         case BITMAP_PIXEL_FORMAT_R8G8B8A8:
123                 mediaPixelFormat = MEDIA_PIXEL_FORMAT_RGBA8888;
124                 break;
125         default:
126                 SysAssert(false);
127                 break;
128         }
129
130         pBuffer = _ImageDecoder::DecodeToBufferN(pItem->GetGalleryItemFilePath(), mediaPixelFormat, bitmapSize.width, bitmapSize.height, true);
131         r = GetLastResult();
132         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
133
134         delete __pTmpFileBitmap;
135         __pTmpFileBitmap = new (std::nothrow) Bitmap();
136         SysTryCatch(NID_UI_CTRL, __pTmpFileBitmap != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
137
138         // TODO : need Bitmap::ConstructF(ByteBuffer, )
139         //FloatDimension bitmapSizeF(bitmapSize.width, bitmapSize.height);
140         r = __pTmpFileBitmap->Construct(*pBuffer, bitmapSize, pItem->GetGalleryItemImage()->GetInternalBitmap()->GetPixelColorFormat());
141         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
142
143         r = __pTmpFileBitmap->Scale(imageSize);
144         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
145
146         pPartialGalleryBitmap = _GalleryBitmap::CreateGalleryBitmapN(__pTmpFileBitmap);
147         SysTryCatch(NID_UI_CTRL, pPartialGalleryBitmap != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
148
149         pPartialGalleryBitmap->SetBitmapRotation(pItem->GetGalleryRotation());
150
151         r = pPartialGalleryBitmap->Initialize(imageSize, GALLERY_FITTING_TYPE_FIT);
152         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
153
154         delete __pTmpFileBitmap;
155         __pTmpFileBitmap = _BitmapImpl::CloneN(*pPartialGalleryBitmap->GetInternalBitmap());
156         SysTryCatch(NID_UI_CTRL, __pTmpFileBitmap != null, , r, "[%s] Propagating.", GetErrorMessage(r));
157
158         SetLastResult(E_SUCCESS);
159
160         __currentLoadBitmapIndex = index;
161         __currentLoadBitmapSize = imageSize;
162
163         delete pBuffer;
164         delete pPartialGalleryBitmap;
165         return;
166
167 CATCH:
168         delete pBuffer;
169         delete __pTmpFileBitmap;
170         __pTmpFileBitmap = null;
171         delete pPartialGalleryBitmap;
172 }
173
174 void
175 _GalleryImageReader::ResetLoadedPartialImageIndex(void)
176 {
177         __currentLoadBitmapIndex = -1;
178 }
179
180 }}} // Tizen::Ui::Controls