--- /dev/null
+//
+// Copyright (c) 2012 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Flora License, Version 1.1 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://floralicense.org/license/
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an AS IS BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file GlImageCrop.cpp
+ * @brief This is the implementation file for Image Crop.
+ */
+
+#include <FApp.h>
+#include <FMedia.h>
+#include <FSystem.h>
+#include <FText.h>
+#include "GlGalleryApp.h"
+#include "GlImageCropForm.h"
+#include "GlResourceManager.h"
+#include "GlTypes.h"
+
+using namespace Tizen::App;
+using namespace Tizen::Base;
+using namespace Tizen::Base::Collection;
+using namespace Tizen::Content;
+using namespace Tizen::Graphics;
+using namespace Tizen::Io;
+using namespace Tizen::Media;
+using namespace Tizen::System;
+using namespace Tizen::Text;
+using namespace Tizen::Ui;
+using namespace Tizen::Ui::Controls;
+using namespace Tizen::Ui::Scenes;
+
+static const unsigned int COLOR_GALLERY_BG = Color32<31,31,31>::Value;
+static const unsigned int CROP_BOX_RECTANGLE_COLOR = Color32<199, 110, 6>::Value;
+static const int CROP_BOX_LINE_WIDTH = 5;
+static const int H_CROP_RECTANGLE = 20;
+static const wchar_t* PATH_DATA_DIRECTORY_IN_CROP = L"data/";
+static const wchar_t* FILE_NAME_SEPARATOR = L"_";
+static const int MAX_FILE_LENGHT = 255;
+static const int CROP_BOX_OFFSET = 20;
+
+ImageCropForm::ImageCropForm(void)
+ : __sourceFilePath(L"")
+ , __cropMode(APPCONTROL_DATA_CROP_TYPE_FIT_TO_SCREEN)
+ , __pCanvas(null)
+ , __pCurrentBitmap(null)
+ , __pRectangleBitmap(null)
+ , __imageFormat(IMG_FORMAT_NONE)
+ , __pointLocation(OUTSIDE_CROPBOX)
+ , __isOrientationChanged(false)
+ , __imageHeight(0)
+ , __imageWidth(0)
+ , __statusValue(-1)
+ , __formHeight(0)
+ , __formWidth(0)
+ , __skipMoveEvent(0)
+{
+}
+
+ImageCropForm::~ImageCropForm(void)
+{
+ if (__pCurrentBitmap != null)
+ {
+ delete __pCurrentBitmap;
+ }
+ if (__pRectangleBitmap != null)
+ {
+ delete __pRectangleBitmap;
+ }
+ if (__pCanvas != null)
+ {
+ delete __pCanvas;
+ }
+}
+
+bool
+ImageCropForm::Initialize(void)
+{
+ Form::Construct(IDL_FORM_IMAGE_CROP);
+
+ return true;
+}
+
+result
+ImageCropForm::OnInitializing(void)
+{
+ AppLogDebug("OnInitializing");
+
+ AppResource* pAppResource = null;
+ pAppResource = UiApp::App::GetInstance()->GetAppResource();
+
+ if (pAppResource != null)
+ {
+ __pRectangleBitmap = pAppResource->GetBitmapN(IDB_IMAGE_CROP_RECTANGLE);
+ }
+
+ Footer* pFooter = GetFooter();
+ if (pFooter != null)
+ {
+ pFooter->AddActionEventListener(*this);
+ pFooter->RemoveAllItems();
+ pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
+
+ FooterItem footerItem1;
+ footerItem1.Construct(IDA_BUTTON_SAVE);
+ footerItem1.SetText(ResourceManager::GetString(L"IDS_COM_OPT_SAVE"));
+
+ pFooter->AddItem(footerItem1);
+ }
+
+ AddTouchEventListener(*this);
+ AddOrientationEventListener(*this);
+ SetFormBackEventListener(this);
+
+ return E_SUCCESS;
+}
+
+result
+ImageCropForm::OnDraw(void)
+{
+ AppLogDebug("OnDraw");
+ result r = E_SUCCESS;
+
+ if (__pCanvas != null)
+ {
+ __pCanvas->Clear();
+ r = __pCanvas->SetLineWidth(CROP_BOX_LINE_WIDTH);
+ r = __pCanvas->DrawBitmap(Rectangle(__imageBox.x, __imageBox.y, __imageBox.width, __imageBox.height), *__pCurrentBitmap); // Image to be cropped
+ r = __pCanvas->DrawRectangle(Rectangle(__cropBox.x, __cropBox.y, __cropBox.width, __cropBox.height)); // Cropbox rectangle
+ 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
+ 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
+ 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
+ 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
+ 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
+ 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
+ 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
+ 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
+ __pCanvas->SetBackgroundColor(COLOR_GALLERY_BG);
+ }
+
+ return r;
+}
+
+void
+ImageCropForm::OnFormBackRequested(Form& source)
+{
+ AppLogDebug("OnFormBackRequested");
+
+ SceneManager::GetInstance()->GoBackward(BackwardSceneTransition());
+}
+
+void
+ImageCropForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
+{
+ AppLogDebug("OnActionPerformed");
+ result r = E_SUCCESS;
+
+ switch(actionId)
+ {
+ case IDA_BUTTON_SAVE:
+ {
+ GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
+
+ if (pApp != null && pApp->GetAppControlOperationId() == APPCONTROL_OPERATION_ID_PICK)
+ {
+ ImageBuffer* pCropBuffer = null;
+ int count = 1;
+ int index = 0;
+
+ String destFilePath;
+ destFilePath.Append(App::GetInstance()->GetAppSharedPath());
+ destFilePath.Append(PATH_DATA_DIRECTORY_IN_CROP);
+ destFilePath.Append(GetFileName(__sourceFilePath));
+ destFilePath.Reverse();
+ r = destFilePath.IndexOf(FILE_EXT_SEPARATOR, 0, index);
+ destFilePath.Reverse();
+ if (r != E_SUCCESS)
+ {
+ index = 0;
+ destFilePath.Append(FILE_EXT_SEPARATOR);
+ }
+ destFilePath.Insert(FILE_NAME_SEPARATOR, destFilePath.GetLength() - index - 1);
+ destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
+
+ if (__imageFormat != IMG_FORMAT_JPG || __imageFormat != IMG_FORMAT_PNG || __imageFormat != IMG_FORMAT_BMP)
+ {
+ destFilePath.Remove(destFilePath.GetLength() - index, index);
+ destFilePath.Append(CONTENT_EXT_JPG);
+ }
+
+ while (File::IsFileExist(destFilePath) == true)
+ {
+ count++;
+ destFilePath.Clear();
+ destFilePath.Append(App::GetInstance()->GetAppSharedPath());
+ destFilePath.Append(PATH_DATA_DIRECTORY_IN_CROP);
+ destFilePath.Append(GetFileName(__sourceFilePath));
+ destFilePath.Insert(FILE_NAME_SEPARATOR, destFilePath.GetLength() - index - 1);
+ destFilePath.Insert(count, destFilePath.GetLength() - index - 1);
+ if (__imageFormat != IMG_FORMAT_JPG || __imageFormat != IMG_FORMAT_PNG || __imageFormat != IMG_FORMAT_BMP)
+ {
+ destFilePath.Remove(destFilePath.GetLength() - index, index);
+ destFilePath.Append(CONTENT_EXT_JPG);
+ }
+ }
+
+ AppCtrlResult appControlResult = APP_CTRL_RESULT_FAILED;
+ HashMap* pMap = null;
+ int inputLength = 0;
+ Utf8Encoding utf8;
+ r = utf8.GetByteCount(GetFileName(destFilePath), inputLength);
+
+ while (inputLength > MAX_FILE_LENGHT)
+ {
+ destFilePath.IndexOf(FILE_NAME_SEPARATOR, 0, index);
+ destFilePath.Remove(index - 1, 1);
+ r = utf8.GetByteCount(GetFileName(destFilePath), inputLength);
+ }
+
+ pCropBuffer = __imageBuffer.CropN(((__cropBox.x - __imageBox.x) * __imageWidth) / __imageBox.width, ((__cropBox.y - __imageBox.y) * __imageHeight) / __imageBox.height,
+ (__cropBox.width * __imageWidth) / __imageBox.width, (__cropBox.height * __imageHeight) / __imageBox.height);
+
+ if (pCropBuffer != null)
+ {
+ if (__imageFormat == IMG_FORMAT_JPG || __imageFormat == IMG_FORMAT_PNG || __imageFormat == IMG_FORMAT_BMP)
+ {
+ r = pCropBuffer->EncodeToFile(destFilePath, __imageFormat, true, 100);
+ }
+ else
+ {
+ r = pCropBuffer->EncodeToFile(destFilePath, IMG_FORMAT_JPG, true, 100);
+ }
+ delete pCropBuffer;
+
+ if (r == E_SUCCESS )
+ {
+ ArrayList* pList = new (std::nothrow) ArrayList(SingleObjectDeleter);
+ pList->Add(new (std::nothrow) String(destFilePath));
+
+ pMap = new (std::nothrow) HashMap(SingleObjectDeleter);
+ pMap->Construct();
+ pMap->Add(new (std::nothrow) String(APPCONTROL_KEY_DATA_SELECTED), pList);
+
+ appControlResult = APP_CTRL_RESULT_SUCCEEDED;
+ }
+ else
+ {
+ appControlResult = APP_CTRL_RESULT_FAILED;
+ }
+ }
+ else
+ {
+ appControlResult = APP_CTRL_RESULT_FAILED;
+ }
+
+ pApp->SendAppControlResult(appControlResult, pMap);
+ pApp->Terminate();
+ }
+ }
+ break;
+ }
+
+ AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
+ return;
+}
+
+void
+ImageCropForm::OnOrientationChanged(const Control &source, OrientationStatus orientationStatus)
+{
+ AppLogDebug("OnOrientationChanged");
+
+ __isOrientationChanged = true;
+ SetValue();
+
+ if (__pCanvas != null)
+ {
+ __pCanvas->Clear();
+ }
+ AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
+}
+
+void
+ImageCropForm::OnTouchPressed(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
+{
+ AppLogDebug("OnTouchPressed");
+
+ __touchStart = currentPosition;
+ CheckCurrentPosition(currentPosition);
+
+ AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
+}
+
+void
+ImageCropForm::OnTouchMoved(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
+{
+ if (++__skipMoveEvent % 5 != 0)
+ {
+ return;
+ }
+ else
+ {
+ __skipMoveEvent = 0;
+ }
+
+ int deltaX = currentPosition.x - __touchStart.x;
+ int deltaY = currentPosition.y - __touchStart.y;
+ if (__cropMode.Equals(APPCONTROL_DATA_CROP_TYPE_AUTO, false))
+ {
+ switch(__pointLocation)
+ {
+ case INSIDE_TOP_RECTANGLE:
+ {
+ if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3)))
+ {
+ __cropBox.y += deltaY;
+ __cropBox.height -= deltaY;
+ }
+ }
+ break;
+
+ case INSIDE_BOTTOM_RECTANGLE:
+ {
+ if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height)
+ && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3))
+ {
+ __cropBox.height += deltaY;
+ }
+ }
+ break;
+
+ case INSIDE_LEFT_RECTANGLE:
+ {
+ if ((__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
+ {
+ __cropBox.x += deltaX;
+ __cropBox.width -= deltaX;
+ }
+ }
+ break;
+
+ case INSIDE_RIGHT_RECTANGLE:
+ {
+ if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width)
+ && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3))
+ {
+ __cropBox.width += deltaX;
+ }
+ }
+ break;
+
+ case INSIDE_TOP_LEFT_RECTANGLE:
+ {
+ if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3))
+ && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
+ {
+ __cropBox.y += deltaY;
+ __cropBox.height -= deltaY;
+ __cropBox.x += deltaX;
+ __cropBox.width -= deltaX;
+ }
+ }
+ break;
+
+ case INSIDE_TOP_RIGHT_RECTANGLE:
+ {
+ if ((__cropBox.y + deltaY) > __imageBox.y && deltaY < (__cropBox.height - (H_CROP_RECTANGLE * 3))
+ && (__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3))
+ {
+ __cropBox.width += deltaX;
+ __cropBox.y += deltaY;
+ __cropBox.height -= deltaY;
+ }
+ }
+ break;
+
+ case INSIDE_BOTTOM_LEFT_RECTANGLE:
+ {
+ if ((__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3)
+ && (__cropBox.x + deltaX) > __imageBox.x && deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
+ {
+ __cropBox.x += deltaX;
+ __cropBox.width -= deltaX;
+ __cropBox.height += deltaY;
+ }
+ }
+ break;
+
+ case INSIDE_BOTTOM_RIGHT_RECTANGLE:
+ {
+ if ((__cropBox.x + __cropBox.width + deltaX) < (__imageBox.x + __imageBox.width) && __cropBox.width + deltaX > (H_CROP_RECTANGLE * 3)
+ && (__cropBox.y + __cropBox.height + deltaY) < (__imageBox.y + __imageBox.height) && (__cropBox.height + deltaY) > (H_CROP_RECTANGLE * 3))
+ {
+ __cropBox.width += deltaX;
+ __cropBox.height += deltaY;
+ }
+ }
+ break;
+
+ case INSIDE_CROPBOX:
+ {
+ if ((__cropBox.x + deltaX) > __imageBox.x)
+ {
+ __cropBox.x += deltaX;
+ }
+ else
+ {
+ __cropBox.x = __imageBox.x;
+ }
+ if ((__cropBox.y + deltaY) > __imageBox.y)
+ {
+ __cropBox.y += deltaY;
+ }
+ else
+ {
+ __cropBox.y = __imageBox.y;
+ }
+ if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
+ {
+ __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
+ }
+ if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
+ {
+ __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
+ }
+ }
+ break;
+
+ default:
+ break;
+ }
+ }
+ else if (__cropMode.Equals(APPCONTROL_DATA_CROP_TYPE_FIT_TO_SCREEN, false))
+ {
+ if (__pointLocation == INSIDE_LEFT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_LEFT_RECTANGLE
+ || __pointLocation == INSIDE_TOP_LEFT_RECTANGLE)
+ {
+ int temp = __cropBox.height;
+ int height = (__formHeight * (__cropBox.width - (2 * deltaX))) /__formWidth;
+ temp = height - temp;
+ if ((__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
+ {
+ if (__cropBox.x + deltaX >= __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
+ && __cropBox.y - (temp / 2) >= __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height
+ && (__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)))
+ {
+ __cropBox.x += deltaX;
+ __cropBox.width = __cropBox.width - (2 * deltaX);
+ __cropBox.height = height;
+ __cropBox.y = __cropBox.y - (temp / 2);
+ }
+ else if ((__cropBox.y + __cropBox.height) <= (__imageBox.y + __imageBox.height)
+ && __cropBox.width < __imageBox.width
+ && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
+ {
+ if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
+ && __cropBox.x + deltaX >= __imageBox.x)
+ {
+ __cropBox.x += deltaX;
+ }
+ else
+ {
+ __cropBox.x = __cropBox.x + (2 * deltaX);
+ }
+ __cropBox.width = __cropBox.width - (2 * deltaX);
+ __cropBox.height = height;
+ __cropBox.y = __cropBox.y - temp;
+ }
+ else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
+ && __cropBox.x + deltaX >= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
+ {
+ if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width)
+ {
+ __cropBox.x += deltaX;
+ }
+ else
+ {
+ __cropBox.x = __cropBox.x + (2 * deltaX);
+ }
+ __cropBox.width = __cropBox.width - (2 * deltaX);
+ __cropBox.height = height;
+ }
+ }
+ }
+ else if (__pointLocation == INSIDE_RIGHT_RECTANGLE || __pointLocation == INSIDE_BOTTOM_RIGHT_RECTANGLE
+ || __pointLocation == INSIDE_TOP_RIGHT_RECTANGLE)
+ {
+ deltaX *= -1;
+ int temp = __cropBox.height;
+ int height = (__formHeight * (__cropBox.width - (2 * deltaX))) / __formWidth;
+ temp = height - temp;
+ if ((__cropBox.width - (2 * deltaX) > (H_CROP_RECTANGLE * 3)) && (!(height >= __imageBox.height || (__cropBox.width - (2 * deltaX)) >= __imageBox.width)))
+ {
+ if (deltaX < __cropBox.width - (H_CROP_RECTANGLE * 3))
+ {
+ if (__cropBox.x + deltaX > __imageBox.x && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
+ && __cropBox.y - (temp / 2) > __imageBox.y && __cropBox.y + __cropBox.height + (temp / 2) < __imageBox.y + __imageBox.height)
+ {
+ __cropBox.x += deltaX;
+ __cropBox.width = __cropBox.width - (2 * deltaX);;
+ __cropBox.height = height;
+ __cropBox.y = __cropBox.y - (temp / 2);
+ }
+ else if ((__cropBox.y + __cropBox.height + (temp / 2)) >= (__imageBox.y + __imageBox.height)
+ && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
+ && __cropBox.x - deltaX >= __imageBox.x && __cropBox.y - temp >= __imageBox.y )
+ {
+ if (__cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
+ && __cropBox.x + deltaX >= __imageBox.x)
+ {
+ __cropBox.x += deltaX;
+ }
+ __cropBox.width = __cropBox.width - (2 * deltaX);
+ __cropBox.height = height;
+ __cropBox.y = __cropBox.y - temp;
+ }
+ else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.width - (deltaX) <= __imageBox.width
+ && __cropBox.x + deltaX <= __imageBox.x && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
+ {
+ if (__cropBox.x + deltaX >= __imageBox.x)
+ {
+ __cropBox.x += deltaX;
+ }
+ else
+ {
+ __cropBox.x = __imageBox.x;
+ }
+ __cropBox.width = __cropBox.width - (2 * deltaX);
+ __cropBox.height = height;
+ }
+ else if (__cropBox.y - (temp / 2) <= __imageBox.y && __cropBox.x + deltaX >= __imageBox.x
+ && __cropBox.x + __cropBox.width - deltaX <= __imageBox.x + __imageBox.width
+ && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
+ {
+ __cropBox.x += deltaX;
+ __cropBox.width = __cropBox.width - (2 * deltaX);
+ __cropBox.height = height;
+ }
+ else if (__cropBox.x + deltaX <= __imageBox.x && __cropBox.y - (temp / 2) >= __imageBox.y
+ && __cropBox.x + __cropBox.width - (2 * deltaX) <= __imageBox.x + __imageBox.width
+ && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height)
+ {
+ __cropBox.width = __cropBox.width - (2 * deltaX);
+ __cropBox.y = __cropBox.y - (temp / 2);
+ __cropBox.height = height;
+ }
+ }
+ }
+ }
+ else if (__pointLocation == INSIDE_TOP_RECTANGLE)
+ {
+ int temp = __cropBox.width;
+ int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
+ temp = width - temp;
+ if (__cropBox.height - (2 * deltaY) > (__formHeight * (H_CROP_RECTANGLE * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
+ {
+ if (__cropBox.x - (temp /2 ) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
+ && __cropBox.y + (deltaY) > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
+ {
+ __cropBox.y += deltaY;
+ __cropBox.height = __cropBox.height - (2 * deltaY);
+ __cropBox.width = width;
+ __cropBox.x = __cropBox.x - (temp / 2);
+ }
+ else if (__cropBox.x - (temp / 2) < __imageBox.x)
+ {
+ if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
+ {
+ __cropBox.width = width;
+ }
+ if (__cropBox.y + deltaY >= __imageBox.y)
+ {
+ __cropBox.y += deltaY;
+ }
+ if (__cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
+ {
+ __cropBox.height = __cropBox.height - (2 * deltaY);
+ }
+ }
+ else if (__cropBox.y + __cropBox.height - (2 * deltaY) >= __imageBox.x + __imageBox.height
+ && __cropBox.y + (2 * deltaY) >= __imageBox.y)
+ {
+ if (__cropBox.x - temp >= __imageBox.x
+ && __cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width)
+ {
+ __cropBox.x = __cropBox.x - temp;
+ }
+ else if (__cropBox.x - temp >= __imageBox.x)
+ {
+ __cropBox.x = __cropBox.x - temp / 2;
+ }
+ if (__cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width)
+ {
+ __cropBox.width = width;
+ }
+ if (__cropBox.y + (2 * deltaY) >= __imageBox.y)
+ {
+ __cropBox.y = __cropBox.y + (2 * deltaY);
+ __cropBox.height = __cropBox.height - (2 * deltaY);
+ }
+ }
+ else if (__cropBox.x + __cropBox.width + (temp / 2) > __imageBox.x + __imageBox.width
+ && __cropBox.x - temp >= __imageBox.x && __cropBox.y + deltaY >= __imageBox.y
+ && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
+ {
+ __cropBox.x = __cropBox.x - temp;
+ __cropBox.width = width;
+ __cropBox.y += deltaY;
+ __cropBox.height = __cropBox.height - (2 * deltaY);
+ }
+ }
+ }
+ else if (__pointLocation == INSIDE_BOTTOM_RECTANGLE)
+ {
+ deltaY = deltaY * (-1);
+ int temp = __cropBox.width;
+ int width = (__formWidth * (__cropBox.height - (2 * deltaY))) / __formHeight;
+ temp = width - temp;
+ if (__cropBox.height - (2 * deltaY) > (__formHeight * (H_CROP_RECTANGLE * 3)) / __formWidth && (!(__cropBox.height - (2 * deltaY) >= __imageBox.height || width >= __imageBox.width)))
+ {
+ if (__cropBox.x - (temp / 2) >= __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width
+ && __cropBox.y + deltaY > __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height)
+ {
+ __cropBox.y += deltaY;
+ __cropBox.height = __cropBox.height - (2 * deltaY);;
+ __cropBox.width = width;
+ __cropBox.x = __cropBox.x - (temp / 2);
+ }
+ else if (__cropBox.y + deltaY < __imageBox.y && __cropBox.y + __cropBox.height - (2 * deltaY) < __imageBox.y + __imageBox.height
+ && __cropBox.x - (temp / 2) > __imageBox.x && __cropBox.x + __cropBox.width + temp < __imageBox.x + __imageBox.width)
+ {
+ __cropBox.x = __cropBox.x - (temp / 2);
+ __cropBox.width = width;
+ __cropBox.height = __cropBox.height - (2 * deltaY);
+ }
+ else if (__cropBox.x + __cropBox.width + temp > __imageBox.x + __imageBox.width
+ && __cropBox.y + deltaY >= __imageBox.y && __cropBox.y + __cropBox.height + temp <= __imageBox.y + __imageBox.height
+ && __cropBox.x - temp >= __imageBox.x)
+ {
+ __cropBox.x = __cropBox.x - temp;
+ __cropBox.y += deltaY;
+ __cropBox.width = width;
+ __cropBox.height = __cropBox.height - (2 * deltaY);
+ }
+ else if (__cropBox.x + __cropBox.width + temp >= __imageBox.x + __imageBox.width
+ && __cropBox.y + deltaY <= __imageBox.y && __cropBox.x - temp > __imageBox.x
+ && __cropBox.y + __cropBox.height + temp < __imageBox.y + __imageBox.height)
+ {
+ __cropBox.x -= temp;
+ __cropBox.width = width;
+ __cropBox.height = __cropBox.height - (2 * deltaY);
+ }
+ else if (__cropBox.x - temp < __imageBox.x && __cropBox.x + __cropBox.width + temp <= __imageBox.x + __imageBox.width
+ && __cropBox.y + __cropBox.height - (2 * deltaY) <= __imageBox.y + __imageBox.height)
+ {
+ if (__cropBox.y + deltaY >= __imageBox.y)
+ {
+ __cropBox.y += deltaY;
+ }
+ __cropBox.width = width;
+ __cropBox.height = __cropBox.height - (2 * deltaY);
+ }
+ }
+ }
+ else if (__pointLocation == INSIDE_CROPBOX)
+ {
+ if ((__cropBox.x + deltaX) > __imageBox.x)
+ {
+ __cropBox.x += deltaX;
+ }
+ else
+ {
+ __cropBox.x = __imageBox.x;
+ }
+ if ((__cropBox.y + deltaY) > __imageBox.y)
+ {
+ __cropBox.y += deltaY;
+ }
+ else
+ {
+ __cropBox.y = __imageBox.y;
+ }
+ if ((__cropBox.x + __cropBox.width + deltaX) > (__imageBox.x + __imageBox.width))
+ {
+ __cropBox.x = __imageBox.x + __imageBox.width - __cropBox.width;
+ }
+ if ((__cropBox.y + __cropBox.height + deltaY) > (__imageBox.y + __imageBox.height))
+ {
+ __cropBox.y = __imageBox.y + __imageBox.height - __cropBox.height;
+ }
+ }
+ }
+ RequestRedraw(false);
+ __touchStart = currentPosition;
+}
+
+void
+ImageCropForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
+{
+ AppLogDebug("OnTouchReleased");
+
+ __pointLocation = OUTSIDE_CROPBOX;
+}
+
+void
+ImageCropForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
+{
+ AppLogDebug("OnSceneActivatedN");
+
+ if (pArgs != null)
+ {
+ __sourceFilePath = *(static_cast<String*>(pArgs->GetAt(0)));
+ pArgs->RemoveAll(true);
+ delete pArgs;
+
+ if (__sourceFilePath.IsEmpty() == false)
+ {
+ result r = __imageBuffer.Construct(__sourceFilePath.GetPointer());
+ if (r == E_SUCCESS)
+ {
+ __imageWidth = __imageBuffer.GetWidth();
+ __imageHeight = __imageBuffer.GetHeight();
+
+ int physicalHeight = CoordinateSystem::ConvertToPhysicalY(__imageHeight);
+ int physicalWidth = CoordinateSystem::ConvertToPhysicalX(__imageWidth);
+ __pCurrentBitmap = __imageBuffer.GetBitmapN(BITMAP_PIXEL_FORMAT_ARGB8888, physicalWidth, physicalHeight);
+ SetValue();
+
+ Image img;
+ r = img.Construct();
+ if (r == E_SUCCESS)
+ {
+ __imageFormat = img.GetImageFormat(__sourceFilePath);
+ }
+ }
+ else
+ {
+ GalleryApp* pApp = dynamic_cast<GalleryApp*>(UiApp::GetInstance());
+ if (pApp != null && pApp->GetAppControlOperationId() == APPCONTROL_OPERATION_ID_PICK)
+ {
+ pApp->SendAppControlResult(APP_CTRL_RESULT_FAILED, null);
+ pApp->Terminate();
+ }
+ }
+ }
+ }
+ AppLogDebug("EXIT(%s)", GetErrorMessage(GetLastResult()));
+}
+
+void
+ImageCropForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
+{
+ AppLogDebug("OnSceneDeactivated");
+
+ RemoveOrientationEventListener(*this);
+}
+
+void
+ImageCropForm::CheckCurrentPosition(const Point currentPosition)
+{
+ AppLogDebug("CheckCurrentPosition");
+
+ 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)
+ && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
+ {
+ __pointLocation = INSIDE_TOP_RECTANGLE;
+ }
+ else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
+ && 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))
+ {
+ __pointLocation = INSIDE_LEFT_RECTANGLE;
+ }
+ 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)
+ && 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))
+ {
+ __pointLocation = INSIDE_RIGHT_RECTANGLE;
+ }
+ 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)
+ && 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))
+ {
+ __pointLocation = INSIDE_BOTTOM_RECTANGLE;
+ }
+ else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
+ && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y +(H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
+ {
+ __pointLocation = INSIDE_TOP_LEFT_RECTANGLE;
+ }
+ 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)
+ && currentPosition.y > (__cropBox.y - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.y < (__cropBox.y +(H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET))
+ {
+ __pointLocation = INSIDE_TOP_RIGHT_RECTANGLE;
+ }
+ 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)
+ && 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))
+ {
+ __pointLocation = INSIDE_BOTTOM_RIGHT_RECTANGLE;
+ }
+ else if (currentPosition.x > (__cropBox.x - (H_CROP_RECTANGLE / 2) - CROP_BOX_OFFSET) && currentPosition.x < (__cropBox.x + (H_CROP_RECTANGLE / 2) + CROP_BOX_OFFSET)
+ && 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))
+ {
+ __pointLocation = INSIDE_BOTTOM_LEFT_RECTANGLE;
+ }
+ else if (currentPosition.x > __cropBox.x && currentPosition.x < (__cropBox.x + __cropBox.width) && currentPosition.y > __cropBox.y && currentPosition.y < (__cropBox.y + __cropBox.height))
+ {
+ __pointLocation = INSIDE_CROPBOX;
+ }
+ else
+ {
+ __pointLocation = OUTSIDE_CROPBOX;
+ }
+ return;
+}
+
+void
+ImageCropForm::SetValue(void)
+{
+ int prevHeight = __imageBox.height;
+ int prevWidth = __imageBox.width;
+ int prevImageboxX = __imageBox.x;
+ int prevImageBoxY = __imageBox.y;
+
+ if (GetOrientationStatus() == ORIENTATION_STATUS_PORTRAIT)
+ {
+ __formHeight = Form::GetBounds().height;
+ __formWidth = Form::GetBounds().width;
+ }
+ else if (GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE || GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
+ {
+ __formHeight = Form::GetBounds().width;
+ __formWidth = Form::GetBounds().height;
+ }
+
+ float clientAreaRatio = (GetClientAreaBoundsF().height) / GetClientAreaBoundsF().width;
+
+ if (__imageWidth >= GetClientAreaBounds().width || __imageHeight > GetClientAreaBounds().height)
+ {
+ float imageRatio = __imageHeight / (float) __imageWidth;
+ if (imageRatio < clientAreaRatio)
+ {
+ __imageBox.width = GetClientAreaBounds().width;
+ __imageBox.height = (__imageHeight * __imageBox.width) / __imageWidth;
+ __imageBox.x = GetClientAreaBounds().x;
+ __imageBox.y = GetClientAreaBounds().y + (GetClientAreaBounds().height - __imageBox.height) / 2;
+ }
+ else
+ {
+ __imageBox.height = GetClientAreaBounds().height;
+ __imageBox.width = (__imageWidth * __imageBox.height) / __imageHeight;
+ __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
+ __imageBox.y = GetClientAreaBounds().y;
+ }
+ }
+ else
+ {
+ __imageBox.height = __imageHeight;
+ __imageBox.width = __imageWidth;
+ __imageBox.x = (GetClientAreaBounds().width - __imageBox.width) / 2;
+ __imageBox.y = GetClientAreaBounds().y + (GetClientAreaBounds().height - __imageBox.height) / 2 ;
+ }
+ if (__isOrientationChanged == false)
+ {
+ if (__imageBox.height > __imageBox.width)
+ {
+ __cropBox.width = (2 * __imageBox.width) / 3;
+ __cropBox.height = (__formHeight * __cropBox.width / __formWidth);
+ if (__cropBox.height > __imageBox.height)
+ {
+ __cropBox.height = (2 * __imageBox.height) / 3;
+ __cropBox.width = (__cropBox.height * __formWidth) / __formHeight;
+ }
+ __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
+ __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
+ }
+ else
+ {
+ __cropBox.height = (2 *__imageBox.height) / 3;
+ __cropBox.width = (__formWidth * __cropBox.height / __formHeight);
+ if (__cropBox.width > __imageBox.width)
+ {
+ __cropBox.width = (2 * __imageBox.width) / 3;
+ __cropBox.height = (__formHeight * __cropBox.width) / __formWidth;
+ }
+ __cropBox.y = __imageBox.y + (__imageBox.height - __cropBox.height) / 2;
+ __cropBox.x = __imageBox.x + (__imageBox.width - __cropBox.width) / 2;
+ }
+ }
+ else
+ {
+ float temp = __imageBox.x + ((__cropBox.x - prevImageboxX) * __imageBox.width) / (float) prevWidth;
+ __cropBox.x = (temp + 0.5);
+ temp = __imageBox.y + ((__cropBox.y - prevImageBoxY) * __imageBox.height) / (float) prevHeight;
+ __cropBox.y = (temp + 0.5);
+ temp = (__cropBox.height * __imageBox.height) / (float) prevHeight;
+ __cropBox.height = (temp + 0.5);
+ temp = (__cropBox.width * __imageBox.width) / (float) prevWidth;
+ __cropBox.width = (temp + 0.5);
+ }
+ __pCanvas = GetCanvasN(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height));
+ if (__pCanvas != null)
+ {
+ __pCanvas->SetBackgroundColor(COLOR_GALLERY_BG);
+ __pCanvas->SetForegroundColor(CROP_BOX_RECTANGLE_COLOR);
+ }
+}
+
+String
+ImageCropForm::GetFileName(const String& filePath, bool isCheckExe) const
+{
+ String strResult;
+ int lastIndex = -1 ;
+
+ filePath.LastIndexOf(DIRECTORY_SEPARATOR, filePath.GetLength() - 1, lastIndex);
+ filePath.SubString(lastIndex+1, strResult);
+
+ if (isCheckExe == true)
+ {
+ String strTemp = strResult;
+
+ lastIndex = -1;
+ strTemp.LastIndexOf(FILE_EXT_SEPARATOR, strTemp.GetLength() - 1, lastIndex);
+ strTemp.SubString(0, lastIndex, strResult);
+ }
+
+ return strResult;
+}