3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.tizenopensource.org/license
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.
21 #include "ImageResizeForm.h"
22 #include "BrowserForm.h"
23 #include "PixelFormatMapper.h"
24 #include "UnitTestFactory.h"
26 using namespace Osp::Media;
27 using namespace Osp::Io;
30 #define SECTION_NAME L"ImageResize"
33 MULTI_FORM_REGISTER(ImageResizeForm, L"ImageResizeForm");
34 DECLARE_TC(L"Image", L"5.Image Resize", ImageResizeForm::TestFunc, 0);
37 ImageResizeForm::TestFunc(ImageResizeFormParam* pParam)
39 MultiForm::ActivateForm(L"ImageResizeForm", pParam, MultiForm::GetCurrentForm(), null);
44 ImageResizeForm::ImageResizeForm()
60 ImageResizeForm::~ImageResizeForm()
62 SAFE_DELETE(__pDstBitmap);
66 ImageResizeForm::Initialize(void)
68 // Construct an XML form
69 Construct(L"IDF_IMAGE_RESIZE");
75 ImageResizeForm::OnInitializing(void)
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);
85 __pBmpCtrl = BitmapControl::ConstructN(GetControl(L"IDC_BUTTON_IMG"), false);
86 TryCatch(__pBmpCtrl, r = GetLastResult(), "BitmapControl::ContructN failed:%s", GetErrorMessage(GetLastResult()));
87 AddControl(*__pBmpCtrl);
89 __pSliderSize = static_cast< Slider* >(GetControl(L"IDC_SLIDER_SIZE"));
90 TryCatch(__pSliderSize, r = E_SYSTEM, "pSliderSize is null");
91 __pSliderSize->AddAdjustmentEventListener(*this);
93 __pLog = LogControl::ConstructN(GetControl(L"IDC_EDITAREA_LOG"));
94 TryCatch(__pLog, r = E_SYSTEM, "LogControl::ConstructN failed:%s", GetErrorMessage(GetLastResult()));
97 FitControlBounds(*__pBmpCtrl, GetHeader(), __pLog, 2, 2, false);
98 FitControlBounds(*__pLog, __pSliderSize, GetFooter(), 2, 2, true);
100 __pSliderSize->SetRange(1, 200);
102 r = __img.Construct();
103 TryCatch(r == E_SUCCESS, , "img.Construct failed:%s", GetErrorMessage(r));
111 ImageResizeForm::OnActivate(void* param)
113 result r = E_SUCCESS;
116 int count = __pConfig->GetInt(0, SECTION_NAME, L"count");
118 SetStepInfo(count, 0, 0, 1);
131 ImageResizeForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
133 result r = E_SUCCESS;
140 AppLog("OnActionPerformed Back key pressed");
147 int index = PrevStep();
155 int index = NextStep();
167 ImageResizeForm::OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList* pArgs)
169 result r = E_SUCCESS;
171 AppLog("OnUserEventReceivedN req id = %d", requestId);
176 r = Process(index, true);
180 case ID_PROCESS_SLIDER:
182 r = Process(index, false);
189 ImageResizeForm::OnAdjustmentValueChanged(const Osp::Ui::Control& source, int adjustment)
192 AppLog("OnAdjustmentValueChanged invoked");
193 if (&source == __pSliderSize)
196 SendUserEvent(ID_PROCESS_SLIDER, 0);
201 ImageResizeForm::UpdateSlider()
204 int dstDim = __pSliderSize->GetValue();
205 __dstWidth = __srcWidth * dstDim / 100;
206 __dstHeight = __srcHeight * dstDim / 100;
212 if (__dstHeight < 16)
217 if (__srcPixel == MEDIA_PIXEL_FORMAT_YUV420P)
219 __dstWidth = (__dstWidth / 2) * 2;
220 __dstHeight = (__dstHeight / 2) * 2;
223 str.Format(32, L"Dimension:%dx%d", __dstWidth, __dstHeight);
224 __pSliderSize->SetTitleText(str);
225 __pSliderSize->RequestRedraw();
230 ImageResizeForm::LoadConfig(int index)
232 String format, key, str;
233 result r = E_SUCCESS;
234 String srcPixel, dstPixel;
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);
241 __dstWidth = __srcWidth * __resize / 100;
242 __dstHeight = __srcHeight * __resize / 100;
244 srcPixel = __pConfig->GetString(SECTION_NAME, "%d.srcpixel", index);
246 __srcPixel = PixelFormatMapper::GetFormat(srcPixel, MEDIA_PIXEL_FORMAT_BGRA8888);
247 __bmpFormat = PixelFormatMapper::GetFormat(__srcPixel, BITMAP_PIXEL_FORMAT_ARGB8888);
248 __bytesPerPixel = PixelFormatMapper::GetBytesPerPixel(__srcPixel);
255 ImageResizeForm::Process(int index, bool reload)
257 result r = E_SUCCESS;
260 AppLog("Enter index=%d", index);
264 r = LoadConfig(index);
265 TryCatch(r == E_SUCCESS, , "LoadConfig(i) failed:%s", 0, GetErrorMessage(r));
267 AppLog("Enter %ls %d %d %d", __srcName.GetPointer(), __srcWidth, __srcHeight, (int) __srcPixel);
270 TryCatch(r == E_SUCCESS, , "LoadSource failed:%s", GetErrorMessage(r));
272 __pSliderSize->SetValue(__resize);
277 TryCatch(r == E_SUCCESS, , "ResizeImage failed:%s", GetErrorMessage(r));
278 TryCatch(__pDstBitmap, r = E_SYSTEM, "pFlipBitmap is null");
280 __pBmpCtrl->SetBitmap(*__pDstBitmap);
281 __pBmpCtrl->RequestRedraw();
286 SAFE_DELETE(__pDstBitmap);
293 ImageResizeForm::LoadSource()
295 result r = E_SUCCESS;
300 if (__srcPixel == MEDIA_PIXEL_FORMAT_YUV420P)
302 if (__pSrcBuffer != null)
307 if (__pDstBuffer != null)
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);
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);
323 dim.width = __srcWidth;
324 dim.height = __srcHeight;
328 BufferInfo bufferInfo;
330 if (__pSrcBuffer != null)
335 if (__pDstBuffer != null)
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;
352 ImageResizeForm::ResizeImage()
354 result r = E_SUCCESS;
355 Dimension srcDim, dstDim;
357 if (__pDstBitmap != null)
362 AppLog("%d %d %d %d %d", __srcWidth, __srcHeight, __dstWidth, __dstHeight, (int) __srcPixel);
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);
369 srcDim.width = __srcWidth;
370 srcDim.height = __srcHeight;
371 dstDim.width = __dstWidth;
372 dstDim.height = __dstHeight;
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);
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);
391 __pDstBitmap = GetBitmapN(*__pDstBuffer, dstDim, __srcPixel);
392 TryCatch(__pDstBitmap, r = GetLastResult(), "GetBitmapN failed:%s", GetErrorMessage(GetLastResult()));
395 if (__pDstBuffer != null)
400 AppLog("Resize exit : %s", GetErrorMessage(r));
405 ImageResizeForm::GetBitmapN(ByteBuffer& srcBuf, Dimension dim, MediaPixelFormat pixelFormat)
407 result r = E_SUCCESS;
408 Bitmap* pBitmap = null;
410 if (pixelFormat == MEDIA_PIXEL_FORMAT_YUV420P)
412 pBitmap = yuv2rgb.GetBitmapN(srcBuf, dim);
413 TryCatch(pBitmap, r = GetLastResult(), "yuv2rgb.GetBitmapN failed:%s", GetErrorMessage(GetLastResult()));
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()));
431 ImageResizeForm::UpdateLog(result r)
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,
442 ImageResizeForm::OnFormBackRequested(Osp::Ui::Controls::Form& source)
444 AppLog("ImageResizeForm::OnFormBackRequested called");
446 if (__pDstBuffer != null)
452 __pBmpCtrl->RequestRedraw();