Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / MediaApp / MediaApp / project / src / Image / ImageResizeForm.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 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://www.tizenopensource.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 #include <FIo.h>
19 #include <FApp.h>
20
21 #include "ImageResizeForm.h"
22 #include "BrowserForm.h"
23 #include "PixelFormatMapper.h"
24 #include "UnitTestFactory.h"
25
26 using namespace Osp::Media;
27 using namespace Osp::Io;
28
29
30 #define SECTION_NAME    L"ImageResize"
31
32
33 MULTI_FORM_REGISTER(ImageResizeForm, L"ImageResizeForm");
34 DECLARE_TC(L"Image", L"5.Image Resize", ImageResizeForm::TestFunc, 0);
35
36 result
37 ImageResizeForm::TestFunc(ImageResizeFormParam* pParam)
38 {
39         MultiForm::ActivateForm(L"ImageResizeForm", pParam, MultiForm::GetCurrentForm(), null);
40         return E_SUCCESS;
41 }
42
43
44 ImageResizeForm::ImageResizeForm()
45 {
46         __pDstBitmap = NULL;
47         __pSrcBuffer = null;
48         __pDstBuffer = null;
49         __pLog = null;
50         __pBmpCtrl = null;
51         __pSliderSize = null;
52         __srcWidth = 0;
53         __srcHeight = 0;
54         __dstWidth = 0;
55         __dstHeight = 0;
56         __resize = 0;
57         __bytesPerPixel = 0;
58 }
59
60 ImageResizeForm::~ImageResizeForm()
61 {
62         SAFE_DELETE(__pDstBitmap);
63 }
64
65 bool
66 ImageResizeForm::Initialize(void)
67 {
68         // Construct an XML form
69         Construct(L"IDF_IMAGE_RESIZE");
70
71         return true;
72 }
73
74 result
75 ImageResizeForm::OnInitializing(void)
76 {
77         result r = E_SUCCESS;
78
79         SetHeaderText("Image Resize");
80         SetFooterStyle(FOOTER_STYLE_BUTTON_TEXT, ID_BACK, this);
81         AddFooterItem(L"Prev", ID_PREV);
82         AddFooterItem(L"Next", ID_NEXT);
83         SetStepInfo(0, 0, 0, 1);
84
85         __pBmpCtrl = BitmapControl::ConstructN(GetControl(L"IDC_BUTTON_IMG"), false);
86         TryCatch(__pBmpCtrl, r = GetLastResult(), "BitmapControl::ContructN failed:%s", GetErrorMessage(GetLastResult()));
87         AddControl(*__pBmpCtrl);
88
89         __pSliderSize = static_cast< Slider* >(GetControl(L"IDC_SLIDER_SIZE"));
90         TryCatch(__pSliderSize, r = E_SYSTEM, "pSliderSize is null");
91         __pSliderSize->AddAdjustmentEventListener(*this);
92
93         __pLog = LogControl::ConstructN(GetControl(L"IDC_EDITAREA_LOG"));
94         TryCatch(__pLog, r = E_SYSTEM, "LogControl::ConstructN failed:%s", GetErrorMessage(GetLastResult()));
95         AddControl(*__pLog);
96
97         FitControlBounds(*__pBmpCtrl, GetHeader(), __pLog, 2, 2, false);
98         FitControlBounds(*__pLog, __pSliderSize, GetFooter(), 2, 2, true);
99
100         __pSliderSize->SetRange(1, 200);
101
102         r = __img.Construct();
103         TryCatch(r == E_SUCCESS, , "img.Construct failed:%s", GetErrorMessage(r));
104
105 CATCH:
106         return r;
107 }
108
109
110 result
111 ImageResizeForm::OnActivate(void* param)
112 {
113         result r = E_SUCCESS;
114         int index = 0;
115
116         int count = __pConfig->GetInt(0, SECTION_NAME, L"count");
117
118         SetStepInfo(count, 0, 0, 1);
119         if (count > 0)
120         {
121                 index = CurrStep();
122                 r = Process(index);
123                 UpdateLog(r);
124         }
125
126         return r;
127 }
128
129
130 void
131 ImageResizeForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
132 {
133         result r = E_SUCCESS;
134
135         switch (actionId)
136         {
137         case ID_BACK:
138         {
139                 __pLog->Clear();
140                 AppLog("OnActionPerformed Back key pressed");
141                 Deactivate();
142         }
143         break;
144
145         case ID_PREV:
146         {
147                 int index = PrevStep();
148                 r = Process(index);
149                 UpdateLog(r);
150         }
151         break;
152
153         case ID_NEXT:
154         {
155                 int index = NextStep();
156                 r = Process(index);
157                 UpdateLog(r);
158         }
159         break;
160
161         default:
162                 break;
163         }
164 }
165
166 void
167 ImageResizeForm::OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList* pArgs)
168 {
169         result r = E_SUCCESS;
170         int index;
171         AppLog("OnUserEventReceivedN req id = %d", requestId);
172         switch (requestId)
173         {
174         case ID_PROCESS:
175                 index = CurrStep();
176                 r = Process(index, true);
177                 UpdateLog(r);
178                 break;
179
180         case ID_PROCESS_SLIDER:
181                 index = CurrStep();
182                 r = Process(index, false);
183                 UpdateLog(r);
184                 break;
185         }
186 }
187
188 void
189 ImageResizeForm::OnAdjustmentValueChanged(const Osp::Ui::Control& source, int adjustment)
190 {
191         String str;
192         AppLog("OnAdjustmentValueChanged invoked");
193         if (&source == __pSliderSize)
194         {
195                 UpdateSlider();
196                 SendUserEvent(ID_PROCESS_SLIDER, 0);
197         }
198 }
199
200 void
201 ImageResizeForm::UpdateSlider()
202 {
203         String str;
204         int dstDim = __pSliderSize->GetValue();
205         __dstWidth = __srcWidth * dstDim / 100;
206         __dstHeight = __srcHeight * dstDim / 100;
207
208         if (__dstWidth < 16)
209         {
210                 __dstWidth = 16;
211         }
212         if (__dstHeight < 16)
213         {
214                 __dstHeight = 16;
215         }
216
217         if (__srcPixel == MEDIA_PIXEL_FORMAT_YUV420P)
218         {
219                 __dstWidth = (__dstWidth / 2) * 2;
220                 __dstHeight = (__dstHeight / 2) * 2;
221         }
222
223         str.Format(32, L"Dimension:%dx%d", __dstWidth, __dstHeight);
224         __pSliderSize->SetTitleText(str);
225         __pSliderSize->RequestRedraw();
226 }
227
228
229 result
230 ImageResizeForm::LoadConfig(int index)
231 {
232         String format, key, str;
233         result r = E_SUCCESS;
234         String srcPixel, dstPixel;
235
236         __srcName = __pConfig->GetContentPath(SECTION_NAME, "%d.srcfile", index);
237         __srcWidth = __pConfig->GetInt(0, SECTION_NAME, "%d.width", index);
238         __srcHeight = __pConfig->GetInt(0, SECTION_NAME, "%d.height", index);
239         __resize = __pConfig->GetInt(0, SECTION_NAME, "%d.resize", index);
240
241         __dstWidth = __srcWidth * __resize / 100;
242         __dstHeight = __srcHeight * __resize / 100;
243
244         srcPixel = __pConfig->GetString(SECTION_NAME, "%d.srcpixel", index);
245
246         __srcPixel = PixelFormatMapper::GetFormat(srcPixel, MEDIA_PIXEL_FORMAT_BGRA8888);
247         __bmpFormat = PixelFormatMapper::GetFormat(__srcPixel, BITMAP_PIXEL_FORMAT_ARGB8888);
248         __bytesPerPixel = PixelFormatMapper::GetBytesPerPixel(__srcPixel);
249
250
251         return r;
252 }
253
254 result
255 ImageResizeForm::Process(int index, bool reload)
256 {
257         result r = E_SUCCESS;
258         Dimension dim;
259
260         AppLog("Enter index=%d", index);
261
262         if (reload)
263         {
264                 r = LoadConfig(index);
265                 TryCatch(r == E_SUCCESS, , "LoadConfig(i) failed:%s", 0, GetErrorMessage(r));
266
267                 AppLog("Enter %ls %d %d %d", __srcName.GetPointer(), __srcWidth, __srcHeight, (int) __srcPixel);
268
269                 r = LoadSource();
270                 TryCatch(r == E_SUCCESS, , "LoadSource failed:%s", GetErrorMessage(r));
271
272                 __pSliderSize->SetValue(__resize);
273                 UpdateSlider();
274         }
275
276         r = ResizeImage();
277         TryCatch(r == E_SUCCESS, , "ResizeImage failed:%s", GetErrorMessage(r));
278         TryCatch(__pDstBitmap, r = E_SYSTEM, "pFlipBitmap is null");
279
280         __pBmpCtrl->SetBitmap(*__pDstBitmap);
281         __pBmpCtrl->RequestRedraw();
282
283         return r;
284
285 CATCH:
286         SAFE_DELETE(__pDstBitmap);
287         __pBmpCtrl->Clear();
288         return r;
289 }
290
291
292 result
293 ImageResizeForm::LoadSource()
294 {
295         result r = E_SUCCESS;
296         File file;
297         FileAttributes attr;
298         Dimension dim;
299
300         if (__srcPixel == MEDIA_PIXEL_FORMAT_YUV420P)
301         {
302                 if (__pSrcBuffer != null)
303                 {
304                         delete __pSrcBuffer;
305                         __pSrcBuffer = null;
306                 }
307                 if (__pDstBuffer != null)
308                 {
309                         delete __pDstBuffer;
310                         __pDstBuffer = null;
311                 }
312                 __pSrcBuffer = new (std::nothrow) ByteBuffer();
313                 TryCatch(__pSrcBuffer, r = GetLastResult(), "pSrcBuffer failed:%s", GetErrorMessage(GetLastResult()));
314                 r = __pSrcBuffer->Construct(__srcWidth * __srcHeight * 3 / 2);
315                 TryCatch(r == E_SUCCESS, , "pSrcBuffer.Construct failed:%s %d", GetErrorMessage(r), __srcWidth * __srcHeight * 3 / 2);
316
317                 r = file.Construct(__srcName, "rb");
318                 TryCatch(r == E_SUCCESS, , "file.Construct failed:%s %ls", GetErrorMessage(r), __srcName.GetPointer());
319                 r = file.Read(*__pSrcBuffer);
320                 __pSrcBuffer->SetPosition(0);
321                 TryCatch(r == E_SUCCESS, , "file.Read failed:%s %ls %d %d %d", GetErrorMessage(r), __srcName.GetPointer(), __pSrcBuffer->GetCapacity(), __srcWidth, __srcHeight);
322
323                 dim.width = __srcWidth;
324                 dim.height = __srcHeight;
325         }
326         else
327         {
328                 BufferInfo bufferInfo;
329                 // load src image
330                 if (__pSrcBuffer != null)
331                 {
332                         delete __pSrcBuffer;
333                         __pSrcBuffer = null;
334                 }
335                 if (__pDstBuffer != null)
336                 {
337                         delete __pDstBuffer;
338                         __pDstBuffer = null;
339                 }
340                 __pSrcBuffer = __img.DecodeToBufferN(__srcName, __bmpFormat, __srcWidth, __srcHeight);
341                 TryCatch(__pSrcBuffer, r = GetLastResult(), "img.DecodeToBufferN failed:%s %ls %d %d %d",
342                                  GetErrorMessage(GetLastResult()), __srcName.GetPointer(), __srcWidth, __srcHeight, __bmpFormat);
343                 dim.width = __srcWidth;
344                 dim.height = __srcHeight;
345         }
346
347 CATCH:
348         return r;
349 }
350
351 result
352 ImageResizeForm::ResizeImage()
353 {
354         result r = E_SUCCESS;
355         Dimension srcDim, dstDim;
356
357         if (__pDstBitmap != null)
358         {
359                 delete __pDstBitmap;
360                 __pDstBitmap = null;
361         }
362         AppLog("%d %d %d %d %d", __srcWidth, __srcHeight, __dstWidth, __dstHeight, (int) __srcPixel);
363
364         __pDstBuffer = new (std::nothrow) ByteBuffer();
365         TryCatch(__pDstBuffer, , "pDstBuffer failed:%s", GetErrorMessage(GetLastResult()));
366         r = __pDstBuffer->Construct(__dstWidth * __dstHeight * __bytesPerPixel);
367         TryCatch(r == E_SUCCESS, r = GetLastResult(), "pDstBuffer.Construct failed:%s %d %d %f", GetErrorMessage(GetLastResult()), __srcWidth, __srcHeight, __bytesPerPixel);
368
369         srcDim.width = __srcWidth;
370         srcDim.height = __srcHeight;
371         dstDim.width = __dstWidth;
372         dstDim.height = __dstHeight;
373
374         if (__resize == 100)
375         {
376                 __pDstBuffer->SetPosition(0);
377                 __pSrcBuffer->SetPosition(0);
378                 r = __pDstBuffer->CopyFrom(*__pSrcBuffer);
379                 TryCatch(r == E_SUCCESS, , "CopyFrom failed:%s", GetErrorMessage(r));
380                 __pDstBuffer->SetPosition(0);
381                 __pSrcBuffer->SetPosition(0);
382         }
383         else
384         {
385                 r = ImageUtil::Resize(*__pSrcBuffer, *__pDstBuffer, srcDim, dstDim, __srcPixel);
386                 TryCatch(r == E_SUCCESS, , "Resize failed:%s %d %d %d %d", GetErrorMessage(r),
387                                  __srcWidth, __srcHeight, (int) __srcPixel, (int) __resize);
388         }
389
390
391         __pDstBitmap = GetBitmapN(*__pDstBuffer, dstDim, __srcPixel);
392         TryCatch(__pDstBitmap, r = GetLastResult(), "GetBitmapN failed:%s", GetErrorMessage(GetLastResult()));
393
394 CATCH:
395         if (__pDstBuffer != null)
396         {
397                 delete __pDstBuffer;
398                 __pDstBuffer = null;
399         }
400         AppLog("Resize exit : %s", GetErrorMessage(r));
401         return r;
402 }
403
404 Bitmap*
405 ImageResizeForm::GetBitmapN(ByteBuffer& srcBuf, Dimension dim, MediaPixelFormat pixelFormat)
406 {
407         result r = E_SUCCESS;
408         Bitmap* pBitmap = null;
409
410         if (pixelFormat == MEDIA_PIXEL_FORMAT_YUV420P)
411         {
412                 pBitmap = yuv2rgb.GetBitmapN(srcBuf, dim);
413                 TryCatch(pBitmap, r = GetLastResult(), "yuv2rgb.GetBitmapN failed:%s", GetErrorMessage(GetLastResult()));
414         }
415         else
416         {
417                 BitmapPixelFormat bmpFormat = PixelFormatMapper::GetFormat(pixelFormat, BITMAP_PIXEL_FORMAT_ARGB8888);
418                 pBitmap = new (std::nothrow) Bitmap();
419                 TryCatch(pBitmap, r = GetLastResult(), "Bitmap alloc failed:%s", GetErrorMessage(GetLastResult()));
420                 r = pBitmap->Construct(srcBuf, dim, bmpFormat);
421                 TryCatch(r == E_SUCCESS, , "pBitmap->Construct failed:%s", GetErrorMessage(GetLastResult()));
422         }
423
424 CATCH:
425         SetLastResult(r);
426         return pBitmap;
427 }
428
429
430 void
431 ImageResizeForm::UpdateLog(result r)
432 {
433         __pLog->Clear();
434         __pLog->Log(__srcName);
435         __pLog->Log("\nPixelFormat:%s\nDimension:%dx%d=>%dx%d\nResult:%s",
436                                 PixelFormatMapper::GetFormatString(__srcPixel, "Unknown"),
437                                 __srcWidth, __srcHeight, __dstWidth, __dstHeight,
438                                 GetErrorMessage(r));
439 }
440
441 void
442 ImageResizeForm::OnFormBackRequested(Osp::Ui::Controls::Form& source)
443 {
444         AppLog("ImageResizeForm::OnFormBackRequested called");
445
446         if (__pDstBuffer != null)
447         {
448                 delete __pDstBuffer;
449                 __pDstBuffer = null;
450         }
451         __pBmpCtrl->Clear();
452         __pBmpCtrl->RequestRedraw();
453         __pLog->Clear();
454         Deactivate();
455 }