[dali_1.1.5] 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     background.Add( mHeightBox );
198
199     mWidthBox = Toolkit::ImageView::New( widthBackground );
200     mWidthBox.SetOpacity( 0.2f );
201     background.Add( mWidthBox );
202
203     mDesiredBox = Toolkit::ImageView::New( desiredBackground );
204     background.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     background.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
388     popup.OutsideTouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnPopupOutsideTouched );
389
390     return popup;
391   }
392
393   //void CreatePopupButton( Toolkit::Popup popup, const char* id )
394   Toolkit::PushButton CreatePopupButton( Actor parent, const char* id )
395   {
396     Toolkit::PushButton button = Toolkit::PushButton::New();
397     button.SetName( id );
398     button.SetLabelText( id );
399
400     button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
401     button.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
402     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
403     button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
404
405     button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
406
407     parent.Add( button );
408     return button;
409   }
410
411   bool OnButtonClicked( Toolkit::Button button )
412   {
413     if( button.GetName() == FITTING_BUTTON_ID )
414     {
415       mPopup = CreatePopup();
416
417       // Four-row table to hold buttons:
418       Toolkit::TableView fittingModes = Toolkit::TableView::New( 4, 1 );
419       fittingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
420       fittingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
421       fittingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
422       fittingModes.SetFitHeight( 0 );
423       fittingModes.SetFitHeight( 1 );
424       fittingModes.SetFitHeight( 2 );
425       fittingModes.SetFitHeight( 3 );
426
427       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SCALE_TO_FILL ) );
428       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SHRINK_TO_FIT ) );
429       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_WIDTH ) );
430       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_HEIGHT ) );
431
432       mPopup.SetContent( fittingModes );
433       Stage::GetCurrent().Add( mPopup );
434       mPopup.SetDisplayState( Toolkit::Popup::SHOWN );
435     }
436     else if( button.GetName() == SAMPLING_BUTTON_ID )
437     {
438       mPopup = CreatePopup();
439
440       // Table to hold buttons for each sampling mode:
441       Toolkit::TableView samplingModes = Toolkit::TableView::New( 6, 1 );
442       samplingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
443       samplingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
444       samplingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
445       samplingModes.SetFitHeight( 0 );
446       samplingModes.SetFitHeight( 1 );
447       samplingModes.SetFitHeight( 2 );
448       samplingModes.SetFitHeight( 3 );
449       samplingModes.SetFitHeight( 4 );
450       samplingModes.SetFitHeight( 5 );
451
452       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NEAREST ) );
453       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::LINEAR ) );
454       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX ) );
455       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_NEAREST ) );
456       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_LINEAR ) );
457       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NO_FILTER ) );
458
459       mPopup.SetContent( samplingModes );
460       Stage::GetCurrent().Add( mPopup );
461       mPopup.SetDisplayState( Toolkit::Popup::SHOWN );
462     }
463     else if( CheckFittingModeButton( button, FittingMode::SCALE_TO_FILL) ||
464              CheckFittingModeButton( button, FittingMode::SHRINK_TO_FIT) ||
465              CheckFittingModeButton( button, FittingMode::FIT_WIDTH) ||
466              CheckFittingModeButton( button, FittingMode::FIT_HEIGHT) )
467     {
468     }
469     else if( CheckSamplingModeButton( button, SamplingMode::NEAREST ) ||
470              CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
471              CheckSamplingModeButton( button, SamplingMode::BOX ) ||
472              CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
473              CheckSamplingModeButton( button, SamplingMode::BOX_THEN_NEAREST ) ||
474              CheckSamplingModeButton( button, SamplingMode::BOX_THEN_LINEAR ) ||
475              CheckSamplingModeButton( button, SamplingMode::NO_FILTER ) )
476     {
477     }
478     return true;
479   }
480
481   bool CheckFittingModeButton( Actor &button, FittingMode::Type mode )
482   {
483     const char * const modeName = StringFromScalingMode( mode );
484     if( button.GetName() == modeName )
485     {
486       mFittingMode = mode;
487       mFittingModeButton.SetLabelText( modeName );
488       ResizeImage();
489       mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
490       mPopup.Reset();
491       return true;
492     }
493     return false;
494   }
495
496   bool CheckSamplingModeButton( Actor &button, SamplingMode::Type mode )
497   {
498     const char * const modeName = StringFromFilterMode( mode );
499     if( button.GetName() == modeName )
500     {
501       mSamplingMode = mode;
502       mSamplingModeButton.SetLabelText( modeName );
503       ResizeImage();
504       mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
505       mPopup.Reset();
506       return true;
507     }
508     return false;
509   }
510
511   void OnPopupOutsideTouched()
512   {
513     if( mPopup )
514     {
515       mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
516       mPopup.Reset();
517     }
518   }
519
520   void OnImageLoaded( ResourceImage image )
521   {
522       DALI_ASSERT_DEBUG( image == mNextImage );
523       mImageActor.SetImage( image );
524       mImageActor.SetSize( Size( image.GetWidth(), image.GetHeight() ) );
525   }
526
527   bool OnControlTouched( Actor actor, const TouchEvent& event )
528   {
529     if(event.GetPointCount() > 0)
530     {
531       const TouchPoint& point = event.GetPoint(0);
532       switch(point.state)
533       {
534         case TouchPoint::Up:
535         {
536           const std::string & name = actor.GetName();
537           if( name == NEXT_BUTTON_ID )
538           {
539             mCurrentPath = mCurrentPath + 1;
540             mCurrentPath = mCurrentPath <  NUM_IMAGE_PATHS ? mCurrentPath : 0;
541             ResizeImage();
542           }
543           else if( name == PREVIOUS_BUTTON_ID )
544           {
545             mCurrentPath = mCurrentPath - 1;
546             mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
547             ResizeImage();
548           }
549           break;
550         }
551         default:
552         {
553           break;
554         }
555       } // end switch
556     }
557
558     return false;
559   }
560
561   void OnPinch( Actor actor, const PinchGesture& pinch )
562   {
563     if( pinch.state == Gesture::Started )
564     {
565       mLastPinchScale = pinch.scale;
566     }
567     const float scale = pinch.scale;
568
569     if( scale != mLastPinchScale )
570     {
571       if ( scale < mLastPinchScale )
572       {
573         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
574         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
575       }
576       else
577       {
578         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
579         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
580       }
581       ResizeImage();
582     }
583     mLastPinchScale = scale;
584   }
585
586   void OnPan( Actor actor, const PanGesture& gesture )
587   {
588     Stage stage = Stage::GetCurrent();
589     mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x + (gesture.displacement.x * 2.0f / stage.GetSize().width ) ) );
590     mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y + (gesture.displacement.y * 2.0f / stage.GetSize().height ) ) );
591     ResizeImage();
592   }
593
594   void OnKeyEvent(const KeyEvent& event)
595   {
596     if( event.state == KeyEvent::Down )
597     {
598       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
599       {
600         if( mPopup && mPopup.IsVisible() )
601         {
602           mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
603           mPopup.Reset();
604         }
605         else
606         {
607           mApplication.Quit();
608         }
609       }
610       else if ( event.keyPressedName == "Right" )
611       {
612         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
613       }
614       else if ( event.keyPressedName == "Left" )
615       {
616         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
617       }
618       else if ( event.keyPressedName == "Up" )
619       {
620         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
621       }
622       else if ( event.keyPressedName == "Down" )
623       {
624         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
625       }
626       else if ( event.keyPressedName == "o" )
627       {
628         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
629         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
630       }
631       else if ( event.keyPressedName == "p" )
632       {
633         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
634         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
635       }
636       else if ( event.keyPressedName == "n" )
637       {
638         mCurrentPath = mCurrentPath + 1;
639         mCurrentPath = mCurrentPath <  NUM_IMAGE_PATHS ? mCurrentPath : 0;
640       }
641       else if ( event.keyPressedName == "b" )
642       {
643         mCurrentPath = mCurrentPath - 1;
644         mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
645       }
646       // Cycle filter and scaling modes:
647       else if ( event.keyPressedName == "f" )
648       {
649         mSamplingMode = NextFilterMode( mSamplingMode );
650         mSamplingModeButton.SetLabelText( StringFromFilterMode( mSamplingMode ) );
651       }
652       // Cycle filter and scaling modes:
653       else if ( event.keyPressedName == "s" )
654       {
655         mFittingMode = NextScalingMode( mFittingMode );
656         mFittingModeButton.SetLabelText( StringFromScalingMode( mFittingMode ) );
657       }
658       else
659       {
660         return;
661       }
662
663       ResizeImage();
664     }
665   }
666
667 private:
668   void ResizeImage()
669   {
670     const char * const path = IMAGE_PATHS[mCurrentPath];
671
672     Stage stage = Stage::GetCurrent();
673     Size imageSize = stage.GetSize() * mImageStageScale;
674     const ImageDimensions imageSizeInt = ImageDimensions::FromFloatArray( &imageSize.x );
675
676     ResourceImage image = ResourceImage::New( path, imageSizeInt, mFittingMode, mSamplingMode );
677     image.LoadingFinishedSignal().Connect( this, &ImageScalingAndFilteringController::OnImageLoaded );
678
679     mNextImage = image;
680
681     mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
682     mHeightBox.SetSize( stage.GetSize().width,  (stage.GetSize() * mImageStageScale).height );
683     mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
684   }
685
686 private:
687   Application&  mApplication;
688   Toolkit::ImageView mDesiredBox; //< Background rectangle to show requested image size.
689   Toolkit::ImageView mHeightBox;  //< Background horizontal stripe to show requested image height.
690   Toolkit::ImageView mWidthBox;   //< Background vertical stripe to show requested image width.
691   Toolkit::PushButton mFittingModeButton;
692   Toolkit::PushButton mSamplingModeButton;
693   Toolkit::Popup mPopup;
694   PinchGestureDetector mPinchDetector;
695   float mLastPinchScale;
696   Toolkit::PushButton  mGrabCorner;
697   PanGestureDetector mPanGestureDetector;
698   ImageActor mImageActor;
699   ResourceImage mNextImage; //< Currently-loading image
700   Vector2 mImageStageScale;
701   int mCurrentPath;
702   FittingMode::Type mFittingMode;
703   SamplingMode::Type mSamplingMode;
704 };
705
706 void RunTest( Application& application )
707 {
708   ImageScalingAndFilteringController test( application );
709
710   application.MainLoop();
711 }
712
713 // Entry point for Linux & Tizen applications
714 int main( int argc, char **argv )
715 {
716   Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
717
718   RunTest( application );
719
720   return 0;
721 }