[dali_1.0.51] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / image-scaling-and-filtering / image-scaling-and-filtering-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali-toolkit/devel-api/controls/popup/popup.h>
21 #include "shared/view.h"
22 #include <iostream>
23
24 using namespace Dali;
25 using Toolkit::TextLabel;
26
27 namespace
28 {
29
30 const char* BACKGROUND_IMAGE( DALI_IMAGE_DIR "background-gradient.jpg" );
31 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
32
33 const int MARGIN_SIZE = 10;
34
35 const char* const NEXT_BUTTON_ID = "NEXT_BUTTON";
36 const char* const PREVIOUS_BUTTON_ID = "PREVIOUS_BUTTON";
37 const char * const DALI_ICON_PLAY = DALI_IMAGE_DIR "icon-play.png";
38
39 const char* const FITTING_BUTTON_ID = "FITTING_BUTTON";
40 const char* const SAMPLING_BUTTON_ID = "SAMPLING_BUTTON";
41 const char* const FITTING_BUTTON_TEXT = "Fitting";
42 const char* const SAMPLING_BUTTON_TEXT = "Sampling";
43
44 const char* const STYLE_LABEL_TEXT  = "grouplabel";
45 const char* const STYLE_BUTTON_TEXT = "buttonlabel";
46
47
48
49 const char* IMAGE_PATHS[] =
50 {
51   // Worst case for aliasing in downscaling, 2k x 2k 1 bit per pixel dithered
52   // black and white image:
53   DALI_IMAGE_DIR "gallery-large-14.wbmp",
54   // Variety of sizes, shapes and formats:
55   DALI_IMAGE_DIR "animation-list.png",
56   DALI_IMAGE_DIR "layer1.png",
57   DALI_IMAGE_DIR "layer2.png",
58   DALI_IMAGE_DIR "music-libray-main-screen.png",
59   DALI_IMAGE_DIR "music-libray-record-cover.png",
60   DALI_IMAGE_DIR "contacts-background.png",
61   DALI_IMAGE_DIR "portrait_screen_primitive_shapes.gif",
62   DALI_IMAGE_DIR "landscape_screen_primitive_shapes.gif",
63   DALI_IMAGE_DIR "square_primitive_shapes.bmp",
64   DALI_IMAGE_DIR "dali-logo.png",
65   DALI_IMAGE_DIR "com.samsung.dali-demo.ico",
66   DALI_IMAGE_DIR "gallery-large-14.jpg",
67   DALI_IMAGE_DIR "book-landscape-cover.jpg",
68   DALI_IMAGE_DIR "book-portrait-p1.jpg",
69   DALI_IMAGE_DIR "book-landscape-cover-back.jpg",
70   DALI_IMAGE_DIR "background-1.jpg",
71   DALI_IMAGE_DIR "background-blocks.jpg",
72   DALI_IMAGE_DIR "background-magnifier.jpg",
73   DALI_IMAGE_DIR "gallery-large-14.jpg",
74   NULL
75 };
76 const int NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u;
77
78 /** Cycle the scaling mode options. */
79 FittingMode::Type NextScalingMode( FittingMode::Type oldMode )
80 {
81   FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
82   switch ( oldMode )
83   {
84     case FittingMode::SHRINK_TO_FIT:
85       newMode = FittingMode::SCALE_TO_FILL;
86       break;
87     case FittingMode::SCALE_TO_FILL:
88       newMode = FittingMode::FIT_WIDTH;
89       break;
90     case FittingMode::FIT_WIDTH:
91       newMode = FittingMode::FIT_HEIGHT;
92       break;
93     case FittingMode::FIT_HEIGHT:
94       newMode = FittingMode::SHRINK_TO_FIT;
95       break;
96   }
97   return newMode;
98 }
99
100 /** Cycle through filter mode options. */
101 SamplingMode::Type NextFilterMode( SamplingMode::Type oldMode )
102 {
103   SamplingMode::Type newMode = SamplingMode::BOX;
104
105   switch ( oldMode )
106   {
107     case SamplingMode::BOX:
108       newMode = SamplingMode::NEAREST;
109       break;
110     case SamplingMode::NEAREST:
111       newMode = SamplingMode::LINEAR;
112       break;
113     case SamplingMode::LINEAR:
114       newMode = SamplingMode::BOX_THEN_NEAREST;
115       break;
116     case SamplingMode::BOX_THEN_NEAREST:
117       newMode = SamplingMode::BOX_THEN_LINEAR;
118       break;
119     case SamplingMode::BOX_THEN_LINEAR:
120       newMode = SamplingMode::NO_FILTER;
121       break;
122     case SamplingMode::NO_FILTER:
123       newMode = SamplingMode::BOX;
124       break;
125     case SamplingMode::DONT_CARE:
126       newMode = SamplingMode::BOX;
127       break;
128   }
129   return newMode;
130 }
131
132 const char* StringFromScalingMode( FittingMode::Type scalingMode )
133 {
134   return scalingMode == FittingMode::SCALE_TO_FILL ? "SCALE_TO_FILL" : scalingMode == FittingMode::SHRINK_TO_FIT ? "SHRINK_TO_FIT" : scalingMode == FittingMode::FIT_WIDTH ? "FIT_WIDTH" : scalingMode == FittingMode::FIT_HEIGHT ? "FIT_HEIGHT" : "UnknownScalingMode";
135 }
136
137 const char* StringFromFilterMode( SamplingMode::Type filterMode )
138 {
139   return filterMode == SamplingMode::BOX ? "BOX" : filterMode == SamplingMode::BOX_THEN_NEAREST ? "BOX_THEN_NEAREST" : filterMode == SamplingMode::BOX_THEN_LINEAR ? "BOX_THEN_LINEAR" : filterMode == SamplingMode::NEAREST ? "NEAREST" : filterMode == SamplingMode::LINEAR ? "LINEAR" : filterMode == SamplingMode::NO_FILTER ? "NO_FILTER" : filterMode == SamplingMode::DONT_CARE ? "DONT_CARE" : "UnknownFilterMode";
140 }
141
142 }
143
144 // This example shows the load-time image scaling and filtering features.
145 //
146 class ImageScalingAndFilteringController : public ConnectionTracker
147 {
148 public:
149
150   ImageScalingAndFilteringController( Application& application )
151   : mApplication( application ),
152     mImageStageScale( 0.5f, 0.5f ),
153     mCurrentPath( 0 ),
154     mFittingMode( FittingMode::SCALE_TO_FILL ),
155     mSamplingMode( SamplingMode::BOX_THEN_LINEAR)
156   {
157     // Connect to the Application's Init signal
158     mApplication.InitSignal().Connect( this, &ImageScalingAndFilteringController::Create );
159   }
160
161   ~ImageScalingAndFilteringController()
162   {
163     // Nothing to do here;
164   }
165
166   // The Init signal is received once (only) during the Application lifetime
167   void Create( Application& application )
168   {
169     // Get a handle to the stage
170     Stage stage = Stage::GetCurrent();
171
172     // Background image:
173     ResourceImage backgroundImage = ResourceImage::New( BACKGROUND_IMAGE, ImageDimensions( stage.GetSize().width, stage.GetSize().height ), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR );
174     Toolkit::ImageView background = Toolkit::ImageView::New( backgroundImage );
175     background.SetZ( -2.0f );
176     background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
177     background.SetSize( stage.GetSize() );
178     stage.Add( background );
179
180     // Make grey pixels for the desired box, the desired height the desired width:
181     BufferImage desiredBackground = BufferImage::WHITE();
182
183     BufferImage heightBackground = BufferImage::WHITE();
184     PixelBuffer* const heightPixel = heightBackground.GetBuffer();
185     heightPixel[0] = 0x8f;
186     heightPixel[1] = 0x8f;
187     heightPixel[2] = 0x8f;
188
189     BufferImage widthBackground = BufferImage::WHITE();
190     PixelBuffer* const widthPixel = widthBackground.GetBuffer();
191     widthPixel[0] = 0x4f;
192     widthPixel[1] = 0x4f;
193     widthPixel[2] = 0x4f;
194
195     mHeightBox = Toolkit::ImageView::New( heightBackground );
196     mHeightBox.SetOpacity( 0.2f );
197     stage.Add( mHeightBox );
198
199     mWidthBox = Toolkit::ImageView::New( widthBackground );
200     mWidthBox.SetOpacity( 0.2f );
201     stage.Add( mWidthBox );
202
203     mDesiredBox = Toolkit::ImageView::New( desiredBackground );
204     stage.Add( mDesiredBox );
205
206     mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
207     mDesiredBox.SetParentOrigin( ParentOrigin::CENTER );
208     mDesiredBox.SetAnchorPoint( AnchorPoint::CENTER );
209     mDesiredBox.SetPosition( 0, 0, -1 );
210
211     mHeightBox.SetSize( stage.GetSize().width,  (stage.GetSize() * mImageStageScale).height );
212     mHeightBox.SetParentOrigin( ParentOrigin::CENTER );
213     mHeightBox.SetAnchorPoint( AnchorPoint::CENTER );
214     mHeightBox.SetPosition( 0, 0, -1 );
215
216     mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
217     mWidthBox.SetParentOrigin( ParentOrigin::CENTER );
218     mWidthBox.SetAnchorPoint( AnchorPoint::CENTER );
219     mWidthBox.SetPosition( 0, 0, -1 );
220
221     // Make a grab-handle for resizing the image:
222     mGrabCorner = Toolkit::PushButton::New();
223     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
224     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
225     mGrabCorner.SetName( "GrabCorner" );
226     mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
227     mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
228     mGrabCorner.SetSize( Vector2( stage.GetSize().width*0.08f, stage.GetSize().width*0.08f ) );
229     mGrabCorner.SetOpacity( 0.6f );
230
231     Layer grabCornerLayer = Layer::New();
232     grabCornerLayer.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
233     grabCornerLayer.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
234
235     grabCornerLayer.Add( mGrabCorner );
236     mDesiredBox.Add( grabCornerLayer );
237     mPanGestureDetector = PanGestureDetector::New();
238     mPanGestureDetector.Attach( mGrabCorner );
239     mPanGestureDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPan );
240
241     // Initialize the actor
242     mImageActor = ImageActor::New();
243
244     // Reposition the actor
245     mImageActor.SetParentOrigin( ParentOrigin::CENTER );
246     mImageActor.SetAnchorPoint( AnchorPoint::CENTER );
247     mImageActor.SetSortModifier(5.f);
248
249     // Display the actor on the stage
250     stage.Add( mImageActor );
251
252     mImageActor.SetSize( stage.GetSize() * mImageStageScale );
253
254     // Setup the pinch detector for scaling the desired image load dimensions:
255     mPinchDetector = PinchGestureDetector::New();
256     mPinchDetector.Attach( mImageActor );
257     mPinchDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPinch );
258
259     // Tie-in input event handlers:
260     stage.KeyEventSignal().Connect( this, &ImageScalingAndFilteringController::OnKeyEvent );
261
262     CreateControls();
263
264     ResizeImage();
265   }
266
267   /**
268    * Create the GUI controls which float above the scene
269    */
270   void CreateControls()
271   {
272     Stage stage = Stage::GetCurrent();
273
274     Dali::Layer controlsLayer = Dali::Layer::New();
275     controlsLayer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
276     controlsLayer.SetSizeModeFactor( Vector3( 1.0f, 1.0f, 1.0f ) );
277     controlsLayer.SetAnchorPoint( AnchorPoint::TOP_LEFT);
278     controlsLayer.SetParentOrigin( ParentOrigin::TOP_LEFT);
279     stage.Add( controlsLayer );
280
281     // Back and next image buttons in corners of stage:
282     unsigned int playWidth = std::min( stage.GetSize().x * (1 / 5.0f), 58.0f );
283     Image playImage = ResourceImage::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ), FittingMode::SHRINK_TO_FIT, SamplingMode::BOX_THEN_LINEAR );
284     Toolkit::ImageView imagePrevious = Toolkit::ImageView::New( playImage );
285
286     // Last image button:
287     imagePrevious.SetAnchorPoint( AnchorPoint::TOP_LEFT );
288     imagePrevious.RotateBy( Radian(3.14159265358979323846f), Vector3( 0, 1.0f, 0 ) );
289     imagePrevious.SetY( playWidth * 0.5f );
290     imagePrevious.SetX( playWidth + playWidth * 0.5f );
291     imagePrevious.SetOpacity( 0.6f );
292     controlsLayer.Add( imagePrevious );
293     imagePrevious.SetName( PREVIOUS_BUTTON_ID );
294     imagePrevious.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
295
296     // Next image button:
297     Toolkit::ImageView imageNext = Toolkit::ImageView::New( playImage );
298     imageNext.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
299     imageNext.SetY( playWidth * 0.5f );
300     imageNext.SetX( stage.GetSize().x - playWidth * 0.5f );
301     imageNext.SetOpacity( 0.6f );
302     controlsLayer.Add( imageNext );
303     imageNext.SetName( NEXT_BUTTON_ID );
304     imageNext.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
305
306     // Buttons to popup selectors for fitting and sampling modes:
307
308     // Wrapper table to hold two buttons side by side:
309     Toolkit::TableView modesGroupBackground = Toolkit::TableView::New( 1, 2 );
310     modesGroupBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
311     modesGroupBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
312     modesGroupBackground.SetBackgroundColor( BACKGROUND_COLOUR );
313     modesGroupBackground.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE ) );
314     modesGroupBackground.SetFitHeight( 0 );
315
316     modesGroupBackground.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
317     modesGroupBackground.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
318     modesGroupBackground.SetPosition( 0.0f, 0.0f );
319
320     controlsLayer.Add( modesGroupBackground );
321
322     {
323       // Vertical table to hold label and button:
324       Toolkit::TableView fittingModeGroup = Toolkit::TableView::New( 2, 1 );
325       fittingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
326       fittingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
327       fittingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
328       fittingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
329       fittingModeGroup.SetFitHeight( 0 );
330       fittingModeGroup.SetFitHeight( 1 );
331
332       TextLabel label = TextLabel::New( "Image fitting mode:" );
333       label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
334       fittingModeGroup.Add( label );
335
336       Toolkit::PushButton button = CreateButton( FITTING_BUTTON_ID, StringFromScalingMode( mFittingMode ) );
337       fittingModeGroup.Add( button );
338       mFittingModeButton = button;
339
340       modesGroupBackground.Add( fittingModeGroup );
341     }
342
343     {
344       // Vertical table to hold label and button:
345       Toolkit::TableView samplingModeGroup = Toolkit::TableView::New( 2, 1 );
346       samplingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
347       samplingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
348       samplingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
349       samplingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
350       samplingModeGroup.SetFitHeight( 0 );
351       samplingModeGroup.SetFitHeight( 1 );
352
353       TextLabel label = TextLabel::New( "Image sampling mode:" );
354       label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
355       samplingModeGroup.Add( label );
356
357       Toolkit::PushButton button = CreateButton( SAMPLING_BUTTON_ID, StringFromFilterMode( mSamplingMode ) );
358       samplingModeGroup.Add( button );
359       mSamplingModeButton = button;
360
361       modesGroupBackground.Add( samplingModeGroup );
362     }
363   }
364
365   Toolkit::PushButton CreateButton( const char * id, const char * label )
366   {
367     Toolkit::PushButton button = Toolkit::PushButton::New();
368     button.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
369     button.SetName( id );
370     button.SetLabelText( label );
371     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
372     button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
373     button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
374     return button;
375   }
376
377   Toolkit::Popup CreatePopup()
378   {
379     Stage stage = Stage::GetCurrent();
380     const float POPUP_WIDTH_DP = stage.GetSize().width * 0.75f;
381
382     Toolkit::Popup popup = Toolkit::Popup::New();
383     popup.SetName( "POPUP" );
384     popup.SetParentOrigin( ParentOrigin::CENTER );
385     popup.SetAnchorPoint( AnchorPoint::CENTER );
386     popup.SetSize( POPUP_WIDTH_DP, 0.0f );
387     popup.HideTail();
388
389     popup.OutsideTouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnPopupOutsideTouched );
390
391     return popup;
392   }
393
394   //void CreatePopupButton( Toolkit::Popup popup, const char* id )
395   Toolkit::PushButton CreatePopupButton( Actor parent, const char* id )
396   {
397     Toolkit::PushButton button = Toolkit::PushButton::New();
398     button.SetName( id );
399     button.SetLabelText( id );
400
401     button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
402     button.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
403     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
404     button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
405
406     button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
407
408     parent.Add( button );
409     return button;
410   }
411
412   bool OnButtonClicked( Toolkit::Button button )
413   {
414     if( button.GetName() == FITTING_BUTTON_ID )
415     {
416       mPopup = CreatePopup();
417
418       // Four-row table to hold buttons:
419       Toolkit::TableView fittingModes = Toolkit::TableView::New( 4, 1 );
420       fittingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
421       fittingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
422       fittingModes.SetBackgroundColor( BACKGROUND_COLOUR );
423       fittingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
424       fittingModes.SetFitHeight( 0 );
425       fittingModes.SetFitHeight( 1 );
426       fittingModes.SetFitHeight( 2 );
427       fittingModes.SetFitHeight( 3 );
428
429       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SCALE_TO_FILL ) );
430       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SHRINK_TO_FIT ) );
431       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_WIDTH ) );
432       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_HEIGHT ) );
433
434       mPopup.Add( fittingModes );
435       mPopup.Show();
436     }
437     else if( button.GetName() == SAMPLING_BUTTON_ID )
438     {
439       mPopup = CreatePopup();
440
441       // Table to hold buttons for each sampling mode:
442       Toolkit::TableView samplingModes = Toolkit::TableView::New( 6, 1 );
443       samplingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
444       samplingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
445       samplingModes.SetBackgroundColor( BACKGROUND_COLOUR );
446       samplingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
447       samplingModes.SetFitHeight( 0 );
448       samplingModes.SetFitHeight( 1 );
449       samplingModes.SetFitHeight( 2 );
450       samplingModes.SetFitHeight( 3 );
451       samplingModes.SetFitHeight( 4 );
452       samplingModes.SetFitHeight( 5 );
453
454       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NEAREST ) );
455       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::LINEAR ) );
456       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX ) );
457       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_NEAREST ) );
458       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_LINEAR ) );
459       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NO_FILTER ) );
460
461       mPopup.Add( samplingModes );
462       mPopup.Show();
463     }
464     else if( CheckFittingModeButton( button, FittingMode::SCALE_TO_FILL) ||
465              CheckFittingModeButton( button, FittingMode::SHRINK_TO_FIT) ||
466              CheckFittingModeButton( button, FittingMode::FIT_WIDTH) ||
467              CheckFittingModeButton( button, FittingMode::FIT_HEIGHT) )
468     {
469     }
470     else if( CheckSamplingModeButton( button, SamplingMode::NEAREST ) ||
471              CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
472              CheckSamplingModeButton( button, SamplingMode::BOX ) ||
473              CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
474              CheckSamplingModeButton( button, SamplingMode::BOX_THEN_NEAREST ) ||
475              CheckSamplingModeButton( button, SamplingMode::BOX_THEN_LINEAR ) ||
476              CheckSamplingModeButton( button, SamplingMode::NO_FILTER ) )
477     {
478     }
479     return true;
480   }
481
482   bool CheckFittingModeButton( Actor &button, FittingMode::Type mode )
483   {
484     const char * const modeName = StringFromScalingMode( mode );
485     if( button.GetName() == modeName )
486     {
487       mFittingMode = mode;
488       mFittingModeButton.SetLabelText( modeName );
489       ResizeImage();
490       mPopup.Hide();
491       mPopup.Reset();
492       return true;
493     }
494     return false;
495   }
496
497   bool CheckSamplingModeButton( Actor &button, SamplingMode::Type mode )
498   {
499     const char * const modeName = StringFromFilterMode( mode );
500     if( button.GetName() == modeName )
501     {
502       mSamplingMode = mode;
503       mSamplingModeButton.SetLabelText( modeName );
504       ResizeImage();
505       mPopup.Hide();
506       mPopup.Reset();
507       return true;
508     }
509     return false;
510   }
511
512   void OnPopupOutsideTouched()
513   {
514     if( mPopup )
515     {
516       mPopup.Hide();
517       mPopup.Reset();
518     }
519   }
520
521   void OnImageLoaded( ResourceImage image )
522   {
523       DALI_ASSERT_DEBUG( image == mNextImage );
524       mImageActor.SetImage( image );
525       mImageActor.SetSize( Size( image.GetWidth(), image.GetHeight() ) );
526   }
527
528   bool OnControlTouched( Actor actor, const TouchEvent& event )
529   {
530     if(event.GetPointCount() > 0)
531     {
532       const TouchPoint& point = event.GetPoint(0);
533       switch(point.state)
534       {
535         case TouchPoint::Up:
536         {
537           const std::string & name = actor.GetName();
538           if( name == NEXT_BUTTON_ID )
539           {
540             mCurrentPath = mCurrentPath + 1;
541             mCurrentPath = mCurrentPath <  NUM_IMAGE_PATHS ? mCurrentPath : 0;
542             ResizeImage();
543           }
544           else if( name == PREVIOUS_BUTTON_ID )
545           {
546             mCurrentPath = mCurrentPath - 1;
547             mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
548             ResizeImage();
549           }
550           break;
551         }
552         default:
553         {
554           break;
555         }
556       } // end switch
557     }
558
559     return false;
560   }
561
562   void OnPinch( Actor actor, const PinchGesture& pinch )
563   {
564     if( pinch.state == Gesture::Started )
565     {
566       mLastPinchScale = pinch.scale;
567     }
568     const float scale = pinch.scale;
569
570     if( scale != mLastPinchScale )
571     {
572       if ( scale < mLastPinchScale )
573       {
574         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
575         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
576       }
577       else
578       {
579         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
580         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
581       }
582       ResizeImage();
583     }
584     mLastPinchScale = scale;
585   }
586
587   void OnPan( Actor actor, const PanGesture& gesture )
588   {
589     Stage stage = Stage::GetCurrent();
590     mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x + (gesture.displacement.x * 2.0f / stage.GetSize().width ) ) );
591     mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y + (gesture.displacement.y * 2.0f / stage.GetSize().height ) ) );
592     ResizeImage();
593   }
594
595   void OnKeyEvent(const KeyEvent& event)
596   {
597     if( event.state == KeyEvent::Down )
598     {
599       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
600       {
601         if( mPopup && mPopup.IsVisible() )
602         {
603           mPopup.Hide();
604           mPopup.Reset();
605         }
606         else
607         {
608           mApplication.Quit();
609         }
610       }
611       else if ( event.keyPressedName == "Right" )
612       {
613         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
614       }
615       else if ( event.keyPressedName == "Left" )
616       {
617         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
618       }
619       else if ( event.keyPressedName == "Up" )
620       {
621         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
622       }
623       else if ( event.keyPressedName == "Down" )
624       {
625         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
626       }
627       else if ( event.keyPressedName == "o" )
628       {
629         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
630         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
631       }
632       else if ( event.keyPressedName == "p" )
633       {
634         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
635         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
636       }
637       else if ( event.keyPressedName == "n" )
638       {
639         mCurrentPath = mCurrentPath + 1;
640         mCurrentPath = mCurrentPath <  NUM_IMAGE_PATHS ? mCurrentPath : 0;
641       }
642       else if ( event.keyPressedName == "b" )
643       {
644         mCurrentPath = mCurrentPath - 1;
645         mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
646       }
647       // Cycle filter and scaling modes:
648       else if ( event.keyPressedName == "f" )
649       {
650         mSamplingMode = NextFilterMode( mSamplingMode );
651         mSamplingModeButton.SetLabelText( StringFromFilterMode( mSamplingMode ) );
652       }
653       // Cycle filter and scaling modes:
654       else if ( event.keyPressedName == "s" )
655       {
656         mFittingMode = NextScalingMode( mFittingMode );
657         mFittingModeButton.SetLabelText( StringFromScalingMode( mFittingMode ) );
658       }
659       else
660       {
661         return;
662       }
663
664       ResizeImage();
665     }
666   }
667
668 private:
669   void ResizeImage()
670   {
671     const char * const path = IMAGE_PATHS[mCurrentPath];
672
673     Stage stage = Stage::GetCurrent();
674     Size imageSize = stage.GetSize() * mImageStageScale;
675     const ImageDimensions imageSizeInt = ImageDimensions::FromFloatArray( &imageSize.x );
676
677     ResourceImage image = ResourceImage::New( path, imageSizeInt, mFittingMode, mSamplingMode );
678     image.LoadingFinishedSignal().Connect( this, &ImageScalingAndFilteringController::OnImageLoaded );
679
680     mNextImage = image;
681
682     mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
683     mHeightBox.SetSize( stage.GetSize().width,  (stage.GetSize() * mImageStageScale).height );
684     mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
685   }
686
687 private:
688   Application&  mApplication;
689   Toolkit::ImageView mDesiredBox; //< Background rectangle to show requested image size.
690   Toolkit::ImageView mHeightBox;  //< Background horizontal stripe to show requested image height.
691   Toolkit::ImageView mWidthBox;   //< Background vertical stripe to show requested image width.
692   Toolkit::PushButton mFittingModeButton;
693   Toolkit::PushButton mSamplingModeButton;
694   Toolkit::Popup mPopup;
695   PinchGestureDetector mPinchDetector;
696   float mLastPinchScale;
697   Toolkit::PushButton  mGrabCorner;
698   PanGestureDetector mPanGestureDetector;
699   ImageActor mImageActor;
700   ResourceImage mNextImage; //< Currently-loading image
701   Vector2 mImageStageScale;
702   int mCurrentPath;
703   FittingMode::Type mFittingMode;
704   SamplingMode::Type mSamplingMode;
705 };
706
707 void RunTest( Application& application )
708 {
709   ImageScalingAndFilteringController test( application );
710
711   application.MainLoop();
712 }
713
714 // Entry point for Linux & Tizen applications
715 int main( int argc, char **argv )
716 {
717   Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
718
719   RunTest( application );
720
721   return 0;
722 }