2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.1 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file GlImageCrop.cpp
19 * @brief This is the implementation file for Image Crop.
26 #include "GlGalleryApp.h"
27 #include "GlImageCropForm.h"
28 #include "GlResourceManager.h"
31 using namespace Tizen::App;
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Collection;
34 using namespace Tizen::Content;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Io;
37 using namespace Tizen::Media;
38 using namespace Tizen::System;
39 using namespace Tizen::Text;
40 using namespace Tizen::Ui;
41 using namespace Tizen::Ui::Controls;
42 using namespace Tizen::Ui::Scenes;
44 static const unsigned int COLOR_GALLERY_BG = Color32<31,31,31>::Value;
45 static const unsigned int CROP_BOX_RECTANGLE_COLOR = Color32<199, 110, 6>::Value;
46 static const int CROP_BOX_LINE_WIDTH = 5;
47 static const int H_CROP_RECTANGLE = 20;
48 static const wchar_t* PATH_DATA_DIRECTORY_IN_CROP = L"data/";
49 static const wchar_t* FILE_NAME_SEPARATOR = L"_";
50 static const int MAX_FILE_LENGHT = 255;
51 static const int CROP_BOX_OFFSET = 20;
53 ImageCropForm::ImageCropForm(void)
54 : __sourceFilePath(L"")
55 , __cropMode(APPCONTROL_DATA_CROP_TYPE_FIT_TO_SCREEN)
57 , __pCurrentBitmap(null)
58 , __pRectangleBitmap(null)
59 , __imageFormat(IMG_FORMAT_NONE)
60 , __pointLocation(OUTSIDE_CROPBOX)
61 , __isOrientationChanged(false)
71 ImageCropForm::~ImageCropForm(void)
73 if (__pCurrentBitmap != null)
75 delete __pCurrentBitmap;
77 if (__pRectangleBitmap != null)
79 delete __pRectangleBitmap;
81 if (__pCanvas != null)
88 ImageCropForm::Initialize(void)
90 Form::Construct(IDL_FORM_IMAGE_CROP);
96 ImageCropForm::OnInitializing(void)
98 AppLogDebug("OnInitializing");
100 AppResource* pAppResource = null;
101 pAppResource = UiApp::App::GetInstance()->GetAppResource();
103 if (pAppResource != null)
105 __pRectangleBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_RECTANGLE);
108 Footer* pFooter = GetFooter();
111 pFooter->AddActionEventListener(*this);
112 pFooter->RemoveAllItems();
113 pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
115 FooterItem footerItem1;
116 footerItem1.Construct(IDA_BUTTON_SAVE);
117 footerItem1.SetText(ResourceManager::GetString(L"IDS_COM_OPT_SAVE"));
119 pFooter->AddItem(footerItem1);
122 AddTouchEventListener(*this);
123 AddOrientationEventListener(*this);
124 SetFormBackEventListener(this);
130 ImageCropForm::OnDraw(void)
132 AppLogDebug("OnDraw");
133 result r = E_SUCCESS;
135 if (__pCanvas != null)
138 r = __pCanvas->SetLineWidth(CROP_BOX_LINE_WIDTH);
139 r = __pCanvas->DrawBitmap(Rectangle(__imageBox.x, __imageBox.y, __imageBox.width, __imageBox.height), *__pCurrentBitmap); // Image to be cropped
140 r = __pCanvas->DrawRectangle(Rectangle(__cropBox.x, __cropBox.y, __cropBox.width, __cropBox.height)); // Cropbox rectangle
141 r = __pCanvas->DrawBitmap(Rectangle((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2), __cropBox.y - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Top Rectangle
142 r = __pCanvas->DrawBitmap(Rectangle((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Bottom Rectangle
143 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); //Left Rectangle
144 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); //Right Rectangle
145 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (H_CROP_RECTANGLE / 2), __cropBox.y - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Top Left Rectangle
146 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2), __cropBox.y - (H_CROP_RECTANGLE / 2), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Top Right Rectangle
147 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Bottom Right Rectangle
148 r = __pCanvas->DrawBitmap(Rectangle(__cropBox.x - (H_CROP_RECTANGLE / 2), (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2)), H_CROP_RECTANGLE, H_CROP_RECTANGLE), *__pRectangleBitmap); // Bottom Left Rectangle
149 __pCanvas->SetBackgroundColor(COLOR_GALLERY_BG);
156 ImageCropForm::OnFormBackRequested(Form& source)
158 AppLogDebug("OnFormBackRequested");
160 SceneManager::GetInstance()->GoBackward(BackwardSceneTransition());
164 ImageCropForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
166 AppLogDebug("OnActionPerformed");
167 result r = E_SUCCESS;
171 case IDA_BUTTON_SAVE:
173 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
175 if (pApp != null && pApp->GetAppControlOperationId() == APPCONTROL_OPERATION_ID_PICK)
177 ImageBuffer* pCropBuffer = null;
182 destFilePath.Append(App::GetInstance()->GetAppSharedPath());
183 destFilePath.Append(PATH_DATA_DIRECTORY_IN_CROP);
184 destFilePath.Append(GetFileName(__sourceFilePath));
185 destFilePath.Reverse();
186 r = destFilePath.IndexOf(FILE_EXT_SEPARATOR, 0, index);
187 destFilePath.Reverse();
191 destFilePath.Append(FILE_EXT_SEPARATOR);
193 destFilePath.Insert(FILE_NAME_SEPARATOR, destFilePath.GetLength() - index - 1);
194 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
196 if (__imageFormat != IMG_FORMAT_JPG || __imageFormat != IMG_FORMAT_PNG || __imageFormat != IMG_FORMAT_BMP)
198 destFilePath.Remove(destFilePath.GetLength() - index, index);
199 destFilePath.Append(CONTENT_EXT_JPG);
202 while (File::IsFileExist(destFilePath) == true)
205 destFilePath.Clear();
206 destFilePath.Append(App::GetInstance()->GetAppSharedPath());
207 destFilePath.Append(PATH_DATA_DIRECTORY_IN_CROP);
208 destFilePath.Append(GetFileName(__sourceFilePath));
209 destFilePath.Insert(FILE_NAME_SEPARATOR, destFilePath.GetLength() - index - 1);
210 destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
211 if (__imageFormat != IMG_FORMAT_JPG || __imageFormat != IMG_FORMAT_PNG || __imageFormat != IMG_FORMAT_BMP)
213 destFilePath.Remove(destFilePath.GetLength() - index, index);
214 destFilePath.Append(CONTENT_EXT_JPG);
218 AppCtrlResult appControlResult = APP_CTRL_RESULT_FAILED;
219 HashMap* pMap = null;
222 r = utf8.GetByteCount(GetFileName(destFilePath), inputLength);
224 while (inputLength > MAX_FILE_LENGHT)
226 destFilePath.IndexOf(FILE_NAME_SEPARATOR, 0, index);
227 destFilePath.Remove(index - 1, 1);
228 r = utf8.GetByteCount(GetFileName(destFilePath), inputLength);
231 pCropBuffer = __imageBuffer.CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
232 (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
234 if (pCropBuffer != null)
236 if (__imageFormat == IMG_FORMAT_JPG || __imageFormat == IMG_FORMAT_PNG || __imageFormat == IMG_FORMAT_BMP)
238 r = pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
242 r = pCropBuffer->EncodeToFile(destFilePath, IMG_FORMAT_JPG, true, 100);
248 ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
249 pList->Add(new (std::nothrow) String(destFilePath));
251 pMap = new (std::nothrow) HashMap(SingleObjectDeleter);
253 pMap->Add(new (std::nothrow) String(APPCONTROL_KEY_DATA_SELECTED), pList);
255 appControlResult = APP_CTRL_RESULT_SUCCEEDED;
259 appControlResult = APP_CTRL_RESULT_FAILED;
264 appControlResult = APP_CTRL_RESULT_FAILED;
267 pApp->SendAppControlResult(appControlResult, pMap);
274 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
279 ImageCropForm::OnOrientationChanged(const Control &source, OrientationStatus orientationStatus)
281 AppLogDebug("OnOrientationChanged");
283 __isOrientationChanged = true;
286 if (__pCanvas != null)
290 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
294 ImageCropForm::OnTouchPressed(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
296 AppLogDebug("OnTouchPressed");
298 __touchStart = currentPosition;
299 CheckCurrentPosition(currentPosition);
301 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
305 ImageCropForm::OnTouchMoved(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
307 if (++__skipMoveEvent % 5 != 0)
316 int deltaX = currentPosition.x - __touchStart.x;
317 int deltaY = currentPosition.y - __touchStart.y;
318 if (__cropMode.Equals(APPCONTROL_DATA_CROP_TYPE_AUTO, false))
320 switch(__pointLocation)
322 case INSIDE_TOP_RECTANGLE:
324 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3)))
326 __cropBox.y += deltaY;
327 __cropBox.height -= deltaY;
332 case INSIDE_BOTTOM_RECTANGLE:
334 if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height)
335 && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3))
337 __cropBox.height += deltaY;
342 case INSIDE_LEFT_RECTANGLE:
344 if ((__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
346 __cropBox.x += deltaX;
347 __cropBox.width -= deltaX;
352 case INSIDE_RIGHT_RECTANGLE:
354 if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width)
355 && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3))
357 __cropBox.width += deltaX;
362 case INSIDE_TOP_LEFT_RECTANGLE:
364 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3))
365 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
367 __cropBox.y += deltaY;
368 __cropBox.height -= deltaY;
369 __cropBox.x += deltaX;
370 __cropBox.width -= deltaX;
375 case INSIDE_TOP_RIGHT_RECTANGLE:
377 if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3))
378 && (__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3))
380 __cropBox.width += deltaX;
381 __cropBox.y += deltaY;
382 __cropBox.height -= deltaY;
387 case INSIDE_BOTTOM_LEFT_RECTANGLE:
389 if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3)
390 && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
392 __cropBox.x += deltaX;
393 __cropBox.width -= deltaX;
394 __cropBox.height += deltaY;
399 case INSIDE_BOTTOM_RIGHT_RECTANGLE:
401 if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3)
402 && (__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3))
404 __cropBox.width += deltaX;
405 __cropBox.height += deltaY;
412 if ((__cropBox.x + deltaX) > __imageBox.x)
414 __cropBox.x += deltaX;
418 __cropBox.x = __imageBox.x;
420 if ((__cropBox.y + deltaY) > __imageBox.y)
422 __cropBox.y += deltaY;
426 __cropBox.y = __imageBox.y;
428 if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
430 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
432 if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
434 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
443 else if (__cropMode.Equals(APPCONTROL_DATA_CROP_TYPE_FIT_TO_SCREEN, false))
445 if (__pointLocation == INSIDE_LEFT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_LEFT_RECTANGLE
446 || __pointLocation == INSIDE_TOP_LEFT_RECTANGLE)
448 int temp = __cropBox.height;
449 int height = (__formHeight * (__cropBox.width - (2 * deltaX))) /__formWidth;
450 temp = height - temp;
451 if ((__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
453 if (__cropBox.x + deltaX >= __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
454 && __cropBox.y - (temp / 2) >= __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height
455 && (__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)))
457 __cropBox.x += deltaX;
458 __cropBox.width = __cropBox.width - (2 * deltaX);
459 __cropBox.height = height;
460 __cropBox.y = __cropBox.y - (temp / 2);
462 else if ((__cropBox.y + __cropBox.height) <= (__imageBox.y + __imageBox.height)
463 && __cropBox.width < __imageBox.width
464 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
466 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
467 && __cropBox.x + deltaX >= __imageBox.x)
469 __cropBox.x += deltaX;
473 __cropBox.x = __cropBox.x + (2 * deltaX);
475 __cropBox.width = __cropBox.width - (2 * deltaX);
476 __cropBox.height = height;
477 __cropBox.y = __cropBox.y - temp;
479 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
480 && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
482 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width)
484 __cropBox.x += deltaX;
488 __cropBox.x = __cropBox.x + (2 * deltaX);
490 __cropBox.width = __cropBox.width - (2 * deltaX);
491 __cropBox.height = height;
495 else if (__pointLocation == INSIDE_RIGHT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_RIGHT_RECTANGLE
496 || __pointLocation == INSIDE_TOP_RIGHT_RECTANGLE)
499 int temp = __cropBox.height;
500 int height = (__formHeight * (__cropBox.width - (2 * deltaX))) / __formWidth;
501 temp = height - temp;
502 if ((__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
504 if (deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
506 if (__cropBox.x + deltaX > __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
507 && __cropBox.y - (temp / 2) > __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height)
509 __cropBox.x += deltaX;
510 __cropBox.width = __cropBox.width - (2 * deltaX);;
511 __cropBox.height = height;
512 __cropBox.y = __cropBox.y - (temp / 2);
514 else if ((__cropBox.y + __cropBox.height + (temp / 2)) >= (__imageBox.y + __imageBox.height)
515 && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
516 && __cropBox.x - deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
518 if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
519 && __cropBox.x + deltaX >= __imageBox.x)
521 __cropBox.x += deltaX;
523 __cropBox.width = __cropBox.width - (2 * deltaX);
524 __cropBox.height = height;
525 __cropBox.y = __cropBox.y - temp;
527 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
528 && __cropBox.x + deltaX <= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
530 if (__cropBox.x + deltaX >= __imageBox.x)
532 __cropBox.x += deltaX;
536 __cropBox.x = __imageBox.x;
538 __cropBox.width = __cropBox.width - (2 * deltaX);
539 __cropBox.height = height;
541 else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.x + deltaX >= __imageBox.x
542 && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
543 && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
545 __cropBox.x += deltaX;
546 __cropBox.width = __cropBox.width - (2 * deltaX);
547 __cropBox.height = height;
549 else if (__cropBox.x + deltaX <= __imageBox.x && __cropBox.y - (temp / 2) >= __imageBox.y
550 && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
551 && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
553 __cropBox.width = __cropBox.width - (2 * deltaX);
554 __cropBox.y = __cropBox.y - (temp / 2);
555 __cropBox.height = height;
560 else if (__pointLocation == INSIDE_TOP_RECTANGLE)
562 int temp = __cropBox.width;
563 int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
565 if (__cropBox.height - (2 * deltaY) > (__formHeight * (H_CROP_RECTANGLE * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
567 if (__cropBox.x - (temp /2 ) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
568 && __cropBox.y + (deltaY) > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
570 __cropBox.y += deltaY;
571 __cropBox.height = __cropBox.height - (2 * deltaY);
572 __cropBox.width = width;
573 __cropBox.x = __cropBox.x - (temp / 2);
575 else if (__cropBox.x - (temp / 2) < __imageBox.x)
577 if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
579 __cropBox.width = width;
581 if (__cropBox.y + deltaY >= __imageBox.y)
583 __cropBox.y += deltaY;
585 if (__cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
587 __cropBox.height = __cropBox.height - (2 * deltaY);
590 else if (__cropBox.y + __cropBox.height - (2 * deltaY) >= __imageBox.x + __imageBox.height
591 && __cropBox.y + (2 * deltaY) >= __imageBox.y)
593 if (__cropBox.x - temp >= __imageBox.x
594 && __cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width)
596 __cropBox.x = __cropBox.x - temp;
598 else if (__cropBox.x - temp >= __imageBox.x)
600 __cropBox.x = __cropBox.x - temp / 2;
602 if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
604 __cropBox.width = width;
606 if (__cropBox.y + (2 * deltaY) >= __imageBox.y)
608 __cropBox.y = __cropBox.y + (2 * deltaY);
609 __cropBox.height = __cropBox.height - (2 * deltaY);
612 else if (__cropBox.x + __cropBox.width + (temp / 2) > __imageBox.x + __imageBox.width
613 && __cropBox.x - temp >= __imageBox.x && __cropBox.y + deltaY >= __imageBox.y
614 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
616 __cropBox.x = __cropBox.x - temp;
617 __cropBox.width = width;
618 __cropBox.y += deltaY;
619 __cropBox.height = __cropBox.height - (2 * deltaY);
623 else if (__pointLocation == INSIDE_BOTTOM_RECTANGLE)
625 deltaY = deltaY * (-1);
626 int temp = __cropBox.width;
627 int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
629 if (__cropBox.height - (2 * deltaY) > (__formHeight * (H_CROP_RECTANGLE * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
631 if (__cropBox.x - (temp / 2) >= __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
632 && __cropBox.y + deltaY > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
634 __cropBox.y += deltaY;
635 __cropBox.height = __cropBox.height - (2 * deltaY);;
636 __cropBox.width = width;
637 __cropBox.x = __cropBox.x - (temp / 2);
639 else if (__cropBox.y + deltaY < __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height
640 && __cropBox.x - (temp / 2) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width)
642 __cropBox.x = __cropBox.x - (temp / 2);
643 __cropBox.width = width;
644 __cropBox.height = __cropBox.height - (2 * deltaY);
646 else if (__cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width
647 && __cropBox.y + deltaY >= __imageBox.y && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height
648 && __cropBox.x - temp >= __imageBox.x)
650 __cropBox.x = __cropBox.x - temp;
651 __cropBox.y += deltaY;
652 __cropBox.width = width;
653 __cropBox.height = __cropBox.height - (2 * deltaY);
655 else if (__cropBox.x + __cropBox.width + temp >= __imageBox.x + __imageBox.width
656 && __cropBox.y + deltaY <= __imageBox.y && __cropBox.x - temp > __imageBox.x
657 && __cropBox.y + __cropBox.height + temp < __imageBox.y + __imageBox.height)
660 __cropBox.width = width;
661 __cropBox.height = __cropBox.height - (2 * deltaY);
663 else if (__cropBox.x - temp < __imageBox.x && __cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width
664 && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
666 if (__cropBox.y + deltaY >= __imageBox.y)
668 __cropBox.y += deltaY;
670 __cropBox.width = width;
671 __cropBox.height = __cropBox.height - (2 * deltaY);
675 else if (__pointLocation == INSIDE_CROPBOX)
677 if ((__cropBox.x + deltaX) > __imageBox.x)
679 __cropBox.x += deltaX;
683 __cropBox.x = __imageBox.x;
685 if ((__cropBox.y + deltaY) > __imageBox.y)
687 __cropBox.y += deltaY;
691 __cropBox.y = __imageBox.y;
693 if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
695 __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
697 if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
699 __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
703 RequestRedraw(false);
704 __touchStart = currentPosition;
708 ImageCropForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
710 AppLogDebug("OnTouchReleased");
712 __pointLocation = OUTSIDE_CROPBOX;
716 ImageCropForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
718 AppLogDebug("OnSceneActivatedN");
722 __sourceFilePath = *(static_cast<String*>(pArgs->GetAt(0)));
723 pArgs->RemoveAll(true);
726 if (__sourceFilePath.IsEmpty() == false)
728 result r = __imageBuffer.Construct(__sourceFilePath.GetPointer());
731 __imageWidth = __imageBuffer.GetWidth();
732 __imageHeight = __imageBuffer.GetHeight();
734 int physicalHeight = CoordinateSystem::ConvertToPhysicalY(__imageHeight);
735 int physicalWidth = CoordinateSystem::ConvertToPhysicalX(__imageWidth);
736 __pCurrentBitmap = __imageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_ARGB8888, physicalWidth, physicalHeight);
743 __imageFormat = img.GetImageFormat(__sourceFilePath);
748 GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
749 if (pApp != null && pApp->GetAppControlOperationId() == APPCONTROL_OPERATION_ID_PICK)
751 pApp->SendAppControlResult(APP_CTRL_RESULT_FAILED, null);
757 AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
761 ImageCropForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
763 AppLogDebug("OnSceneDeactivated");
765 RemoveOrientationEventListener(*this);
769 ImageCropForm::CheckCurrentPosition(const Point currentPosition)
771 AppLogDebug("CheckCurrentPosition");
773 if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
774 && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
776 __pointLocation = INSIDE_TOP_RECTANGLE;
778 else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
779 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
781 __pointLocation = INSIDE_LEFT_RECTANGLE;
783 else if (currentPosition.x > (__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
784 && currentPosition.y > ((__cropBox.y + __cropBox.height / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < ((__cropBox.y + __cropBox.height / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
786 __pointLocation = INSIDE_RIGHT_RECTANGLE;
788 else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < ((__cropBox.x + __cropBox.width / 2) + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
789 && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
791 __pointLocation = INSIDE_BOTTOM_RECTANGLE;
793 else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
794 && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y +(H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
796 __pointLocation = INSIDE_TOP_LEFT_RECTANGLE;
798 else if (currentPosition.x > ((__cropBox.x + __cropBox.width / 2) - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
799 && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y +(H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
801 __pointLocation = INSIDE_TOP_RIGHT_RECTANGLE;
803 else if (currentPosition.x > (__cropBox.x + __cropBox.width - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + __cropBox.width + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
804 && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
806 __pointLocation = INSIDE_BOTTOM_RIGHT_RECTANGLE;
808 else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
809 && currentPosition.y > (__cropBox.y + __cropBox.height - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + __cropBox.height + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
811 __pointLocation = INSIDE_BOTTOM_LEFT_RECTANGLE;
813 else if (currentPosition.x > __cropBox.x && currentPosition.x < (__cropBox.x + __cropBox.width) && currentPosition.y > __cropBox.y && currentPosition.y < (__cropBox.y + __cropBox.height))
815 __pointLocation = INSIDE_CROPBOX;
819 __pointLocation = OUTSIDE_CROPBOX;
825 ImageCropForm::SetValue(void)
827 int prevHeight = __imageBox.height;
828 int prevWidth = __imageBox.width;
829 int prevImageboxX = __imageBox.x;
830 int prevImageBoxY = __imageBox.y;
832 if (GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT)
834 __formHeight = Form::GetBounds().height;
835 __formWidth = Form::GetBounds().width;
837 else if (GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE || GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
839 __formHeight = Form::GetBounds().width;
840 __formWidth = Form::GetBounds().height;
843 float clientAreaRatio = (GetClientAreaBoundsF().height) / GetClientAreaBoundsF().width;
845 if (__imageWidth >= GetClientAreaBounds().width || __imageHeight > GetClientAreaBounds().height)
847 float imageRatio = __imageHeight / (float) __imageWidth;
848 if (imageRatio < clientAreaRatio)
850 __imageBox.width = GetClientAreaBounds().width;
851 __imageBox.height = (__imageHeight * __imageBox.width) / __imageWidth;
852 __imageBox.x = GetClientAreaBounds().x;
853 __imageBox.y = GetClientAreaBounds().y + (GetClientAreaBounds().height - __imageBox.height) / 2;
857 __imageBox.height = GetClientAreaBounds().height;
858 __imageBox.width = (__imageWidth * __imageBox.height) / __imageHeight;
859 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
860 __imageBox.y = GetClientAreaBounds().y;
865 __imageBox.height = __imageHeight;
866 __imageBox.width = __imageWidth;
867 __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
868 __imageBox.y = GetClientAreaBounds().y + (GetClientAreaBounds().height - __imageBox.height) / 2 ;
870 if (__isOrientationChanged == false)
872 if (__imageBox.height > __imageBox.width)
874 __cropBox.width = (2 * __imageBox.width) / 3;
875 __cropBox.height = (__formHeight * __cropBox.width / __formWidth);
876 if (__cropBox.height > __imageBox.height)
878 __cropBox.height = (2 * __imageBox.height) / 3;
879 __cropBox.width = (__cropBox.height * __formWidth) / __formHeight;
881 __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
882 __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
886 __cropBox.height = (2 *__imageBox.height) / 3;
887 __cropBox.width = (__formWidth * __cropBox.height / __formHeight);
888 if (__cropBox.width > __imageBox.width)
890 __cropBox.width = (2 * __imageBox.width) / 3;
891 __cropBox.height = (__formHeight * __cropBox.width) / __formWidth;
893 __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
894 __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
899 float temp = __imageBox.x + ((__cropBox.x - prevImageboxX) * __imageBox.width) / (float) prevWidth;
900 __cropBox.x = (temp + 0.5);
901 temp = __imageBox.y + ((__cropBox.y - prevImageBoxY) * __imageBox.height) / (float) prevHeight;
902 __cropBox.y = (temp + 0.5);
903 temp = (__cropBox.height * __imageBox.height) / (float) prevHeight;
904 __cropBox.height = (temp + 0.5);
905 temp = (__cropBox.width * __imageBox.width) / (float) prevWidth;
906 __cropBox.width = (temp + 0.5);
908 __pCanvas = GetCanvasN(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height));
909 if (__pCanvas != null)
911 __pCanvas->SetBackgroundColor(COLOR_GALLERY_BG);
912 __pCanvas->SetForegroundColor(CROP_BOX_RECTANGLE_COLOR);
917 ImageCropForm::GetFileName(const String& filePath, bool isCheckExe) const
922 filePath.LastIndexOf(DIRECTORY_SEPARATOR, filePath.GetLength() - 1, lastIndex);
923 filePath.SubString(lastIndex+1, strResult);
925 if (isCheckExe == true)
927 String strTemp = strResult;
930 strTemp.LastIndexOf(FILE_EXT_SEPARATOR, strTemp.GetLength() - 1, lastIndex);
931 strTemp.SubString(0, lastIndex, strResult);