Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / partner / cpp / Sample / Tizen C++ / MediaApp / MediaApp / project / src / Image / ImageConverterForm.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
19 #include "BrowserForm.h"
20 #include "ImageConverterForm.h"
21 #include "FIo.h"
22 #include "PixelFormatMapper.h"
23
24 #include "UnitTestFactory.h"
25
26 #define SECTION_NAME    L"ImageConverter"
27
28
29 using namespace Osp::Base;
30 using namespace Osp::Ui;
31 using namespace Osp::Ui::Controls;
32 using namespace Osp::Graphics;
33 using namespace Osp::Io;
34
35
36 MULTI_FORM_REGISTER(ImageConverterForm, L"ImageConverterForm");
37
38 DECLARE_TC(L"Image", L"2.Image Converter", ImageConverterForm::TestFunc, 0);
39
40 result
41 ImageConverterForm::TestFunc(void* pParam)
42 {
43         return MultiForm::ActivateForm(L"ImageConverterForm", pParam, MultiForm::GetCurrentForm(), null);
44 }
45
46 ImageConverterForm::ImageConverterForm(void)
47 {
48         __pButtonUseFile = null;
49         __pEditAreaLog = null;
50         __pSliderDstDim = null;
51         __pSliderDstSize = null;
52         __pSrcBitmap = null;
53         __pDstBitmap = null;
54         __srcWidth = 0;
55         __srcHeight = 0;
56         __encodedSize = 0;
57 }
58
59 ImageConverterForm::~ImageConverterForm(void)
60 {
61         if (__pSrcBitmap != null)
62         {
63                 delete __pSrcBitmap;
64                 __pSrcBitmap = null;
65         }
66
67         if (__pDstBitmap != null)
68         {
69                 delete __pDstBitmap;
70                 __pDstBitmap = null;
71         }
72 }
73
74 bool
75 ImageConverterForm::Initialize()
76 {
77         Construct(L"IDF_IMAGE_CONVERTER");
78
79         return true;
80 }
81
82 result
83 ImageConverterForm::OnInitializing(void)
84 {
85         result r = E_SUCCESS;
86         Rectangle forceRect;
87
88         __imgFormatLoop[IMG_FORMAT_JPG] = IMG_FORMAT_PNG;
89         __imgFormatLoop[IMG_FORMAT_PNG] = IMG_FORMAT_BMP;
90         __imgFormatLoop[IMG_FORMAT_BMP] = IMG_FORMAT_JPG;
91
92         AppLog("ImageConverterForm::OnInitializing\n");
93
94         SetHeaderText("Image Converter");
95         SetFooterStyle(FOOTER_STYLE_BUTTON_TEXT, ID_BACK, this);
96         AddFooterItem(L"Prev", ID_PREV);
97         AddFooterItem(L"Next", ID_NEXT);
98         AddFooterItem(L"Convert", ID_CONVERT);
99         SetStepInfo(0, 0, 0, 1);
100
101         __pSrcBmpCtrl = BitmapControl::ConstructN(GetControl(L"IDC_BUTTON_SRCIMAGE"));
102         if (__pSrcBmpCtrl == null)
103         {
104                 AppLog("BitmapControl::ConstructN failed:%s", GetErrorMessage(GetLastResult()));
105                 return GetLastResult();
106         }
107         AddControl(*__pSrcBmpCtrl);
108
109         __pDstBmpCtrl = BitmapControl::ConstructN(GetControl(L"IDC_BUTTON_DSTIMAGE"));
110         if (__pDstBmpCtrl == null)
111         {
112                 AppLog("BitmapControl::ConstructN failed:%s", GetErrorMessage(GetLastResult()));
113                 return GetLastResult();
114         }
115         AddControl(*__pDstBmpCtrl);
116
117         __pButtonFormat = static_cast< Button* >(GetControl(L"IDC_BUTTON_FORMAT"));
118         TryCatch(__pButtonFormat != null, r = E_SYSTEM, "pButtonFormat is null");
119         __pButtonFormat->SetActionId(ID_FORMAT);
120         __pButtonFormat->AddActionEventListener(*this);
121
122         __pButtonUseFile = static_cast< CheckButton* >(GetControl(L"IDC_CHECKBUTTON_USEFILE"));
123         TryCatch(__pButtonUseFile != null, r = E_SYSTEM, "pButtonUseFile is null");
124         __pButtonUseFile->SetActionId(ID_USE_FILE, ID_NOT_USE_FILE);
125         __pButtonUseFile->AddActionEventListener(*this);
126
127         __pSliderDstDim = static_cast< Slider* >(GetControl(L"IDC_SLIDER_DIMENSION"));
128         TryCatch(__pSliderDstDim != null, r = E_SYSTEM, "pSliderDimension  is null");
129         __pSliderDstDim->AddAdjustmentEventListener(*this);
130
131         __pSliderDstSize = static_cast< Slider* >(GetControl(L"IDC_SLIDER_FILESIZE"));
132         TryCatch(__pSliderDstSize, r = E_SYSTEM, "__pSliderFileSize  is null");
133         __pSliderDstSize->AddAdjustmentEventListener(*this);
134
135         __pEditAreaLog = static_cast< EditArea* >(GetControl(L"IDC_EDITAREA_LOG"));
136         TryCatch(__pEditAreaLog, r = E_SYSTEM, "__pEditAreaLog  is null");
137         __pEditAreaLog->SetKeypadEnabled(false);
138
139         FitControlBounds(*__pButtonFormat, __pSrcBmpCtrl, __pSliderDstDim, 2, 2, false);
140         FitControlBounds(*__pButtonUseFile, __pSrcBmpCtrl, __pSliderDstDim, 2, 2, false);
141         FitControlBounds(*__pSliderDstDim, __pButtonFormat, __pSliderDstSize, 2, 2, false);
142         FitControlBounds(*__pSliderDstSize, __pSliderDstDim, __pEditAreaLog, 2, 2, false);
143         AppLog("__pEditAreaLog coordinates following...");
144         FitControlBounds(*__pEditAreaLog, __pSliderDstSize, null, 2, 2, true);
145
146         r = __img.Construct();
147         TryCatch(r == E_SUCCESS, , "img.Construct failed:%s", GetErrorMessage(r));
148
149 CATCH:
150         return r;
151 }
152
153 result
154 ImageConverterForm::LoadConfig(int index)
155 {
156         String format;
157         String key;
158         String str;
159         int dstDim;
160         int dstSize;
161         result r = E_SUCCESS;
162         FileAttributes attr;
163
164         __dstName = __pConfig->GetContentPath(SECTION_NAME, L"dstfile");
165         __srcName = __pConfig->GetContentPath(SECTION_NAME, "%d.srcfile", index);
166         format = __pConfig->GetString(SECTION_NAME, "%d.format", index);
167         __useFile = __pConfig->GetInt(0, SECTION_NAME, "%d.usefile", index);
168         if (__useFile)
169         {
170                 __pButtonUseFile->SetSelected(true);
171                 __pButtonUseFile->RequestRedraw();
172         }
173         else
174         {
175                 __pButtonUseFile->SetSelected(false);
176                 __pButtonUseFile->RequestRedraw();
177         }
178
179         if (format.Equals(L"JPEG", false) || format.Equals(L"JPG", false))
180         {
181                 __dstFormat = IMG_FORMAT_JPG;
182         }
183         else if (format.Equals(L"PNG", false))
184         {
185                 __dstFormat = IMG_FORMAT_PNG;
186         }
187         else
188         {
189                 __dstFormat = IMG_FORMAT_BMP;
190         }
191         SetControlText(__pButtonFormat, "Format:%s", PixelFormatMapper::GetImageFormatString(__dstFormat));
192
193         dstDim = __pConfig->GetInt(50, SECTION_NAME, "%d.dimension", index);
194         dstSize = __pConfig->GetInt(50, SECTION_NAME, "%d.size", index);
195
196         __pSliderDstDim->SetValue(dstDim);
197         if (__dstFormat == IMG_FORMAT_JPG)
198         {
199                 __pSliderDstSize->SetValue(dstSize);
200                 __pSliderDstSize->SetEnabled(true);
201         }
202         else
203         {
204                 __pSliderDstSize->SetEnabled(false);
205         }
206
207         // load src image
208         if (__pSrcBitmap != null)
209         {
210                 delete __pSrcBitmap;
211                 __pSrcBitmap = null;
212         }
213
214         __srcFormat = __img.GetImageFormat(__srcName);
215         __pSrcBitmap = __img.DecodeN(__srcName, BITMAP_PIXEL_FORMAT_RGB565);
216         TryCatch(__pSrcBitmap, r = GetLastResult(), "img.DecodeN failed:%s", GetErrorMessage(r));
217
218         __srcWidth = __pSrcBitmap->GetWidth();
219         __srcHeight = __pSrcBitmap->GetHeight();
220         __dstWidth = __srcWidth * dstDim / 100;
221         __dstHeight = __srcHeight * dstDim / 100;
222
223         r = File::GetAttributes(__srcName, attr);
224         TryCatch(r == E_SUCCESS, , "File::GetAttributes failed:%s", GetErrorMessage(r));
225         __srcSize = attr.GetFileSize();
226         __dstSize = __srcSize * dstSize / 100;
227
228         str.Format(32, L"Dimension:%dx%d", __dstWidth, __dstHeight);
229         __pSliderDstDim->SetTitleText(str);
230         __pSliderDstDim->RequestRedraw();
231
232         str.Format(32, L"MaxSize(JPEG only):%d KB", __dstSize / 1000);
233         __pSliderDstSize->SetTitleText(str);
234         __pSliderDstSize->RequestRedraw();
235
236         return r;
237
238 CATCH:
239         __dstWidth = 0;
240         __dstHeight = 0;
241         __dstSize = 0;
242
243         str.Format(32, L"Dimension:%dx%d", __dstWidth, __dstHeight);
244         __pSliderDstDim->SetTitleText(str);
245         __pSliderDstDim->RequestRedraw();
246
247         str.Format(32, L"MaxSize(JPEG only):%d KB", __dstSize / 1000);
248         __pSliderDstSize->SetTitleText(str);
249         __pSliderDstSize->RequestRedraw();
250
251         return r;
252 }
253
254 result
255 ImageConverterForm::OnActivate(void* param)
256 {
257         int count = __pConfig->GetInt(0, SECTION_NAME, L"count");
258         SetStepInfo(count, 0, 0, 1);
259
260         if (count > 0)
261         {
262                 SendUserEvent(ID_PROCESS, 0);
263         }
264
265         return E_SUCCESS;
266 }
267
268 result
269 ImageConverterForm::Process(int index)
270 {
271         result r = E_SUCCESS;
272
273         r = LoadConfig(index);
274         TryCatch(r == E_SUCCESS, , "LoadConfig(i) failed:%s", 0, GetErrorMessage(r));
275
276         TryCatch(__pSrcBitmap, r = E_SYSTEM, "pSrcBitmap is null");
277
278         r = __useFile ? ConvertFile() : ConvertBuffer();
279         TryCatch(r == E_SUCCESS, , "Convert() failed:%s", GetErrorMessage(r));
280         TryCatch(__pDstBitmap, r = E_SYSTEM, "pDstBitmap is null");
281
282         __pSrcBmpCtrl->SetBitmap(*__pSrcBitmap);
283         __pSrcBmpCtrl->RequestRedraw();
284
285         __pDstBmpCtrl->SetBitmap(*__pDstBitmap);
286         __pDstBmpCtrl->RequestRedraw();
287
288 CATCH:
289         return r;
290 }
291
292 void
293 ImageConverterForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
294 {
295         result r = E_SUCCESS;
296
297         switch (actionId)
298         {
299         case ID_BACK:
300         {
301                 AppLog("Go back to ImageViewer form\n");
302                 Deactivate();
303         }
304         break;
305
306         case ID_PREV:
307         {
308                 int index = PrevStep();
309                 r = Process(index);
310                 UpdateLog(r);
311         }
312         break;
313
314         case ID_NEXT:
315         {
316                 int index = NextStep();
317                 r = Process(index);
318                 UpdateLog(r);
319         }
320         break;
321
322         case ID_CONVERT:
323         {
324                 r = __useFile ? ConvertFile() : ConvertBuffer();
325                 ;
326                 UpdateLog(r);
327         }
328         break;
329
330         case ID_FORMAT:
331                 __dstFormat = __imgFormatLoop[__dstFormat];
332                 SetControlText(__pButtonFormat, "Format:%s", PixelFormatMapper::GetImageFormatString(__dstFormat));
333                 if (__dstFormat == IMG_FORMAT_JPG)
334                 {
335                         __pSliderDstSize->SetEnabled(true);
336                         __pSliderDstSize->RequestRedraw();
337                 }
338                 else
339                 {
340                         __pSliderDstSize->SetEnabled(false);
341                         __pSliderDstSize->RequestRedraw();
342                 }
343                 break;
344
345         case ID_USE_FILE:
346                 __useFile = true;
347                 break;
348
349         case ID_NOT_USE_FILE:
350                 __useFile = false;
351                 break;
352
353         default:
354                 break;
355         }
356 }
357
358
359 void
360 ImageConverterForm::OnUserEventReceivedN(RequestId requestId, Osp::Base::Collection::IList* pArgs)
361 {
362         result r = E_SUCCESS;
363         switch (requestId)
364         {
365         case ID_PROCESS:
366                 int index = CurrStep();
367                 r = Process(index);
368                 UpdateLog(r);
369                 break;
370         }
371 }
372
373 result
374 ImageConverterForm::ConvertBuffer()
375 {
376         result r = E_SUCCESS;
377         Osp::Graphics::Bitmap* pTmpBitmap = null;
378         Osp::Base::ByteBuffer* pTmpBuffer = null;
379         Osp::Base::ByteBuffer* pDstBuffer = null;
380         Osp::Base::ByteBuffer srcBuffer;
381         File file;
382
383         r = file.Construct(__srcName, "rb");
384         TryCatch(r == E_SUCCESS, r = GetLastResult();
385                          __encodedSize = 0;
386                          SetControlText(__pEditAreaLog, "Result:%s(file.Construct)", GetErrorMessage(r)),
387                          "file.Construct failed:%s", GetErrorMessage(r));
388
389         r = srcBuffer.Construct(__srcSize);
390         TryCatch(r == E_SUCCESS, r = GetLastResult();
391                          __encodedSize = 0;
392                          SetControlText(__pEditAreaLog, "Result:%s(ByteBuffer)", GetErrorMessage(r)),
393                          "srcBuffer.Construct failed:%s", GetErrorMessage(r));
394
395         r = file.Read(srcBuffer);
396         TryCatch(r == E_SUCCESS, r = GetLastResult();
397                          __encodedSize = 0;
398                          SetControlText(__pEditAreaLog, "Result:%s(file.Read)", GetErrorMessage(r)),
399                          "file.Read failed:%s", GetErrorMessage(r));
400
401         srcBuffer.Flip();
402         if (__srcWidth != __dstWidth || __srcHeight != __dstHeight)
403         {
404                 pTmpBitmap = __img.DecodeN(srcBuffer, __srcFormat, BITMAP_PIXEL_FORMAT_RGB565, __dstWidth, __dstHeight);
405         }
406         else
407         {
408                 pTmpBitmap = __img.DecodeN(srcBuffer, __srcFormat, BITMAP_PIXEL_FORMAT_RGB565);
409         }
410         TryCatch(pTmpBitmap,
411                          r = GetLastResult();
412                          __encodedSize = 0;
413                          SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
414                          "img.DecodeN failed:%s", GetErrorMessage(r));
415
416         pDstBuffer = __img.EncodeToBufferN(*pTmpBitmap, __dstFormat);
417         TryCatch(pDstBuffer, r = GetLastResult();
418                          __encodedSize = 0;
419                          SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
420                          "img.EncodeToBufferN failed:%s", GetErrorMessage(r));
421
422         if (__dstFormat == IMG_FORMAT_JPG)
423         {
424                 if (__dstSize < pDstBuffer->GetLimit())
425                 {
426                         pTmpBuffer = pDstBuffer;
427                         pDstBuffer = __img.CompressJpegN(*pTmpBuffer, __dstSize);
428                         TryCatch(pDstBuffer, r = GetLastResult();
429                                          __encodedSize = 0;
430                                          SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
431                                          "img.CompressJpegN failed:%s %d %d %d %d", GetErrorMessage(GetLastResult()),
432                                          pTmpBuffer->GetPosition(), pTmpBuffer->GetLimit(),
433                                          __srcSize, __dstSize);
434                 }
435                 else
436                 {
437                         // nothing
438                 }
439         }
440         if (__pDstBitmap != null)
441         {
442                 delete __pDstBitmap;
443                 __pDstBitmap = null;
444         }
445         __encodedSize = pDstBuffer->GetLimit();
446
447         __pDstBitmap = __img.DecodeN(*pDstBuffer, __dstFormat, BITMAP_PIXEL_FORMAT_RGB565);
448         TryCatch(__pDstBitmap, r = GetLastResult();
449                          __encodedSize = 0;
450                          SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
451                          "img.DecodeN failed:%s %d %d", GetErrorMessage(GetLastResult()), __dstFormat, pDstBuffer->GetCapacity());
452
453 CATCH:
454         if (pTmpBitmap != null)
455         {
456                 delete pTmpBitmap;
457                 pTmpBitmap = null;
458         }
459
460         if (pTmpBuffer != null)
461         {
462                 delete pTmpBuffer;
463                 pTmpBuffer = null;
464         }
465
466         if (pDstBuffer != null)
467         {
468                 delete pDstBuffer;
469                 pDstBuffer = null;
470         }
471
472         return r;
473 }
474
475 result
476 ImageConverterForm::ConvertFile()
477 {
478         result r = E_SUCCESS;
479         Osp::Graphics::Bitmap* pTmpBitmap = null;
480         Osp::Base::ByteBuffer* pTmpBuffer = null;
481         Osp::Base::ByteBuffer* pDstBuffer = null;
482
483         if (__srcWidth == __dstWidth && __srcHeight == __dstHeight && __srcFormat == IMG_FORMAT_JPG)
484         {
485                 FileAttributes encodedAttr;
486
487                 r = __img.CompressJpeg(__srcName, __dstName, __dstSize);
488                 if (__pDstBitmap != null)
489                 {
490                         delete __pDstBitmap;
491                         __pDstBitmap = null;
492                 }
493
494                 __pDstBitmap = __img.DecodeN(__dstName, BITMAP_PIXEL_FORMAT_RGB565);
495                 TryCatch(__pDstBitmap, r = GetLastResult();
496                                  __encodedSize = 0;
497                                  SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
498                                  "img.DecodeN failed:%s", GetErrorMessage(r));
499
500                 r = File::GetAttributes(__dstName, encodedAttr);
501                 TryCatch(r == E_SUCCESS, __encodedSize = 0;
502                                  SetControlText(__pEditAreaLog, "Result:%s Failed to get encoded file size.",
503                                                                 GetErrorMessage(r)),
504                                  "File.GetAttributes failed:%s", GetErrorMessage(r));
505                 __encodedSize = encodedAttr.GetFileSize();
506         }
507         else if (__srcWidth == __dstWidth && __srcHeight == __dstHeight && __srcSize == __dstSize)
508         {
509                 pTmpBuffer = __img.ConvertN(__srcName, __dstFormat);
510                 TryCatch(pTmpBuffer, r = GetLastResult();
511                                  __encodedSize = 0;
512                                  SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
513                                  "img.ConvertN failed:%s", GetErrorMessage(r));
514
515                 if (__pDstBitmap)
516                 {
517                         delete __pDstBitmap;
518                         __pDstBitmap = null;
519                 }
520                 __pDstBitmap = __img.DecodeN(*pTmpBuffer, __srcFormat, BITMAP_PIXEL_FORMAT_RGB565);
521                 TryCatch(__pDstBitmap, r = GetLastResult();
522                                  __encodedSize = 0;
523                                  SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
524                                  "img.DecodeN failed:%s", GetErrorMessage(r));
525
526                 __encodedSize = pTmpBuffer->GetLimit();
527         }
528         else
529         {
530                 if (__srcWidth != __dstWidth || __srcHeight != __dstHeight)
531                 {
532                         pTmpBitmap = __img.DecodeN(__srcName, BITMAP_PIXEL_FORMAT_RGB565, __dstWidth, __dstHeight);
533                 }
534                 else
535                 {
536                         pTmpBitmap = __img.DecodeN(__srcName, BITMAP_PIXEL_FORMAT_RGB565);
537                 }
538                 TryCatch(pTmpBitmap,
539                                  r = GetLastResult();
540                                  __encodedSize = 0;
541                                  SetControlText(__pEditAreaLog, "Result:%s(DecodeN)", GetErrorMessage(r)),
542                                  "img.DecodeN failed:%s", GetErrorMessage(r));
543
544                 if (__dstFormat == IMG_FORMAT_JPG)
545                 {
546                         File file;
547                         pTmpBuffer = __img.EncodeToBufferN(*pTmpBitmap, IMG_FORMAT_JPG);
548                         TryCatch(pTmpBuffer, r = GetLastResult();
549                                          __encodedSize = 0;
550                                          SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
551                                          "img.EncodeToBufferN failed:%s", GetErrorMessage(r));
552
553                         if (__dstSize < pTmpBuffer->GetCapacity())
554                         {
555                                 pDstBuffer = __img.CompressJpegN(*pTmpBuffer, __dstSize);
556                                 TryCatch(pDstBuffer, r = GetLastResult();
557                                                  __encodedSize = 0;
558                                                  SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
559                                                  "img.CompressJpeg failed:%s %d %d %d %d", GetErrorMessage(GetLastResult()),
560                                                  pTmpBuffer->GetPosition(), pTmpBuffer->GetLimit(), pTmpBuffer->GetCapacity(), __dstSize);
561                         }
562                         else
563                         {
564                                 pDstBuffer = pTmpBuffer;
565                                 pTmpBuffer = null;
566                         }
567                         r = file.Construct(__dstName, "wb");
568                         TryCatch(r == E_SUCCESS, __encodedSize = 0;
569                                          SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
570                                          "file.Construct failed:%s", GetErrorMessage(r));
571
572                         r = file.Write(*pDstBuffer);
573                         TryCatch(r == E_SUCCESS, __encodedSize = 0;
574                                          SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
575                                          "file.Write failed:%s", GetErrorMessage(r));
576                         __encodedSize = pDstBuffer->GetLimit();
577                 }
578                 else
579                 {
580                         FileAttributes encodedAttr;
581
582                         r = __img.EncodeToFile(*pTmpBitmap, __dstFormat, __dstName, true);
583                         TryCatch(r == E_SUCCESS, __encodedSize = 0;
584                                          SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
585                                          "img.EncodeToFile failed:%s", GetErrorMessage(r));
586
587                         r = File::GetAttributes(__dstName, encodedAttr);
588                         TryCatch(r == E_SUCCESS, __encodedSize = 0;
589                                          SetControlText(__pEditAreaLog, "Result:%s Failed to get encoded file size.",
590                                                                         GetErrorMessage(r)),
591                                          "File.GetAttributes failed:%s", GetErrorMessage(r));
592                         __encodedSize = encodedAttr.GetFileSize();
593                 }
594
595                 if (__pDstBitmap != null)
596                 {
597                         delete __pDstBitmap;
598                         __pDstBitmap = null;
599                 }
600
601                 __pDstBitmap = __img.DecodeN(__dstName, BITMAP_PIXEL_FORMAT_RGB565);
602                 TryCatch(__pDstBitmap, r = GetLastResult();
603                                  __encodedSize = 0;
604                                  SetControlText(__pEditAreaLog, "Result:%s", GetErrorMessage(r)),
605                                  "img.DecodeN failed:%s", GetErrorMessage(r));
606         }
607 CATCH:
608         if (pTmpBitmap != null)
609         {
610                 delete pTmpBitmap;
611                 pTmpBitmap = null;
612         }
613         if (pTmpBuffer != null)
614         {
615                 delete pTmpBuffer;
616                 pTmpBuffer = null;
617         }
618         if (pDstBuffer != null)
619         {
620                 delete pDstBuffer;
621                 pDstBuffer = null;
622         }
623
624         return r;
625 }
626
627 void
628 ImageConverterForm::OnAdjustmentValueChanged(const Osp::Ui::Control& source, int adjustment)
629 {
630         String str;
631
632         if (&source == __pSliderDstDim)
633         {
634                 int dstDim = __pSliderDstDim->GetValue();
635                 __dstWidth = __srcWidth * dstDim / 100;
636                 __dstHeight = __srcHeight * dstDim / 100;
637
638                 str.Format(32, L"Dimension:%dx%d", __dstWidth, __dstHeight);
639                 __pSliderDstDim->SetTitleText(str);
640                 __pSliderDstDim->RequestRedraw();
641         }
642         else if (&source == __pSliderDstSize)
643         {
644                 int dstSize = __pSliderDstSize->GetValue();
645                 __dstSize = __srcSize * dstSize / 100;
646
647                 str.Format(32, L"MaxSize(JPEG only):%d KB", __dstSize / 1000);
648                 __pSliderDstSize->SetTitleText(str);
649                 __pSliderDstSize->RequestRedraw();
650         }
651 }
652
653 void
654 ImageConverterForm::UpdateLog(result r)
655 {
656         SetControlText(__pEditAreaLog, __srcName);
657         if (__dstFormat == IMG_FORMAT_JPG)
658         {
659                 AppendControlText(__pEditAreaLog, "\nFormat:%s=>%s\nDim:%dx%d=>%dx%d\nUseFile:%s\nSize limit: %d\nEncoded size: %d\nResult:%s",
660                                                   PixelFormatMapper::GetImageFormatString(__srcFormat),
661                                                   PixelFormatMapper::GetImageFormatString(__dstFormat),
662                                                   __srcWidth, __srcHeight, __dstWidth, __dstHeight,
663                                                   __useFile ? "true" : "false",
664                                                   __dstSize, __encodedSize,
665                                                   GetErrorMessage(r));
666         }
667         else
668         {
669                 AppendControlText(__pEditAreaLog, "\nFormat:%s=>%s\nDim:%dx%d=>%dx%d\nUseFile:%s\nResult:%s",
670                                                   PixelFormatMapper::GetImageFormatString(__srcFormat),
671                                                   PixelFormatMapper::GetImageFormatString(__dstFormat),
672                                                   __srcWidth, __srcHeight, __dstWidth, __dstHeight,
673                                                   __useFile ? "true" : "false",
674                                                   GetErrorMessage(r));
675         }
676 }
677
678 void
679 ImageConverterForm::OnFormBackRequested(Osp::Ui::Controls::Form& source)
680 {
681         AppLog("ImageConverterForm::OnFormBackRequested called");
682         __pEditAreaLog->Clear();
683
684         if (__pSrcBitmap != null)
685         {
686                 delete __pSrcBitmap;
687                 __pSrcBitmap = null;
688         }
689
690         if (__pDstBitmap != null)
691         {
692                 delete __pDstBitmap;
693                 __pDstBitmap = null;
694         }
695
696         __pSrcBmpCtrl->SetBitmap(*__pSrcBitmap);
697         __pDstBmpCtrl->SetBitmap(*__pDstBitmap);
698         Deactivate();
699 }