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