[dali_1.0.50] 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     //mDesiredBox.SetSortModifier(4.f);
211
212     mHeightBox.SetSize( stage.GetSize().width,  (stage.GetSize() * mImageStageScale).height );
213     mHeightBox.SetParentOrigin( ParentOrigin::CENTER );
214     mHeightBox.SetAnchorPoint( AnchorPoint::CENTER );
215     mHeightBox.SetPosition( 0, 0, -1 );
216     //mHeightBox.SetSortModifier(3.f);
217
218     mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
219     mWidthBox.SetParentOrigin( ParentOrigin::CENTER );
220     mWidthBox.SetAnchorPoint( AnchorPoint::CENTER );
221     mWidthBox.SetPosition( 0, 0, -1 );
222     //mWidthBox.SetSortModifier(2.f);
223
224     // Make a grab-handle for resizing the image:
225     mGrabCorner = Toolkit::PushButton::New();
226     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
227     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
228     mGrabCorner.SetName( "GrabCorner" );
229     mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
230     mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
231     mGrabCorner.SetSize( Vector2( stage.GetSize().width*0.08f, stage.GetSize().width*0.08f ) );
232     mGrabCorner.SetOpacity( 0.6f );
233
234     Layer grabCornerLayer = Layer::New();
235     grabCornerLayer.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
236     grabCornerLayer.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
237
238     grabCornerLayer.Add( mGrabCorner );
239     mDesiredBox.Add( grabCornerLayer );
240     mPanGestureDetector = PanGestureDetector::New();
241     mPanGestureDetector.Attach( mGrabCorner );
242     mPanGestureDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPan );
243
244     // Initialize the actor
245     mImageActor = ImageActor::New();
246
247     // Reposition the actor
248     mImageActor.SetParentOrigin( ParentOrigin::CENTER );
249     mImageActor.SetAnchorPoint( AnchorPoint::CENTER );
250     mImageActor.SetSortModifier(5.f);
251
252     // Display the actor on the stage
253     stage.Add( mImageActor );
254
255     mImageActor.SetSize( stage.GetSize() * mImageStageScale );
256
257     // Setup the pinch detector for scaling the desired image load dimensions:
258     mPinchDetector = PinchGestureDetector::New();
259     mPinchDetector.Attach( mImageActor );
260     mPinchDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPinch );
261
262     // Tie-in input event handlers:
263     stage.KeyEventSignal().Connect( this, &ImageScalingAndFilteringController::OnKeyEvent );
264
265     CreateControls();
266
267     ResizeImage();
268   }
269
270   /**
271    * Create the GUI controls which float above the scene
272    */
273   void CreateControls()
274   {
275     Stage stage = Stage::GetCurrent();
276
277     Dali::Layer controlsLayer = Dali::Layer::New();
278     controlsLayer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
279     controlsLayer.SetSizeModeFactor( Vector3( 1.0f, 1.0f, 1.0f ) );
280     controlsLayer.SetAnchorPoint( AnchorPoint::TOP_LEFT);
281     controlsLayer.SetParentOrigin( ParentOrigin::TOP_LEFT);
282     stage.Add( controlsLayer );
283
284     // Back and next image buttons in corners of stage:
285     unsigned int playWidth = std::min( stage.GetSize().x * (1 / 5.0f), 58.0f );
286     Image playImage = ResourceImage::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ), FittingMode::SHRINK_TO_FIT, SamplingMode::BOX_THEN_LINEAR );
287     Toolkit::ImageView imagePrevious = Toolkit::ImageView::New( playImage );
288
289     // Last image button:
290     imagePrevious.SetAnchorPoint( AnchorPoint::TOP_LEFT );
291     imagePrevious.RotateBy( Radian(3.14159265358979323846f), Vector3( 0, 1.0f, 0 ) );
292     imagePrevious.SetY( playWidth * 0.5f );
293     imagePrevious.SetX( playWidth + playWidth * 0.5f );
294     imagePrevious.SetOpacity( 0.6f );
295     controlsLayer.Add( imagePrevious );
296     imagePrevious.SetName( PREVIOUS_BUTTON_ID );
297     imagePrevious.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
298
299     // Next image button:
300     Toolkit::ImageView imageNext = Toolkit::ImageView::New( playImage );
301     imageNext.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
302     imageNext.SetY( playWidth * 0.5f );
303     imageNext.SetX( stage.GetSize().x - playWidth * 0.5f );
304     imageNext.SetOpacity( 0.6f );
305     controlsLayer.Add( imageNext );
306     imageNext.SetName( NEXT_BUTTON_ID );
307     imageNext.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
308
309     // Buttons to popup selectors for fitting and sampling modes:
310
311     // Wrapper table to hold two buttons side by side:
312     Toolkit::TableView modesGroupBackground = Toolkit::TableView::New( 1, 2 );
313     modesGroupBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
314     modesGroupBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
315     modesGroupBackground.SetBackgroundColor( BACKGROUND_COLOUR );
316     modesGroupBackground.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE ) );
317     modesGroupBackground.SetFitHeight( 0 );
318
319     modesGroupBackground.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
320     modesGroupBackground.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
321     modesGroupBackground.SetPosition( 0.0f, 0.0f );
322
323     controlsLayer.Add( modesGroupBackground );
324
325     {
326       // Vertical table to hold label and button:
327       Toolkit::TableView fittingModeGroup = Toolkit::TableView::New( 2, 1 );
328       fittingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
329       fittingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
330       fittingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
331       fittingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
332       fittingModeGroup.SetFitHeight( 0 );
333       fittingModeGroup.SetFitHeight( 1 );
334
335       TextLabel label = TextLabel::New( "Image fitting mode:" );
336       label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
337       fittingModeGroup.Add( label );
338
339       Toolkit::PushButton button = CreateButton( FITTING_BUTTON_ID, StringFromScalingMode( mFittingMode ) );
340       fittingModeGroup.Add( button );
341       mFittingModeButton = button;
342
343       modesGroupBackground.Add( fittingModeGroup );
344     }
345
346     {
347       // Vertical table to hold label and button:
348       Toolkit::TableView samplingModeGroup = Toolkit::TableView::New( 2, 1 );
349       samplingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
350       samplingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
351       samplingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
352       samplingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
353       samplingModeGroup.SetFitHeight( 0 );
354       samplingModeGroup.SetFitHeight( 1 );
355
356       TextLabel label = TextLabel::New( "Image sampling mode:" );
357       label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
358       samplingModeGroup.Add( label );
359
360       Toolkit::PushButton button = CreateButton( SAMPLING_BUTTON_ID, StringFromFilterMode( mSamplingMode ) );
361       samplingModeGroup.Add( button );
362       mSamplingModeButton = button;
363
364       modesGroupBackground.Add( samplingModeGroup );
365     }
366   }
367
368   Toolkit::PushButton CreateButton( const char * id, const char * label )
369   {
370     Toolkit::PushButton button = Toolkit::PushButton::New();
371     button.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
372     button.SetName( id );
373     button.SetLabelText( label );
374     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
375     button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
376     button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
377     return button;
378   }
379
380   Toolkit::Popup CreatePopup()
381   {
382     Stage stage = Stage::GetCurrent();
383     const float POPUP_WIDTH_DP = stage.GetSize().width * 0.75f;
384
385     Toolkit::Popup popup = Toolkit::Popup::New();
386     popup.SetName( "POPUP" );
387     popup.SetParentOrigin( ParentOrigin::CENTER );
388     popup.SetAnchorPoint( AnchorPoint::CENTER );
389     popup.SetSize( POPUP_WIDTH_DP, 0.0f );
390     popup.HideTail();
391
392     popup.OutsideTouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnPopupOutsideTouched );
393
394     return popup;
395   }
396
397   //void CreatePopupButton( Toolkit::Popup popup, const char* id )
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.SetBackgroundColor( BACKGROUND_COLOUR );
426       fittingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
427       fittingModes.SetFitHeight( 0 );
428       fittingModes.SetFitHeight( 1 );
429       fittingModes.SetFitHeight( 2 );
430       fittingModes.SetFitHeight( 3 );
431
432       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SCALE_TO_FILL ) );
433       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SHRINK_TO_FIT ) );
434       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_WIDTH ) );
435       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_HEIGHT ) );
436
437       mPopup.Add( fittingModes );
438       mPopup.Show();
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.SetBackgroundColor( BACKGROUND_COLOUR );
449       samplingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
450       samplingModes.SetFitHeight( 0 );
451       samplingModes.SetFitHeight( 1 );
452       samplingModes.SetFitHeight( 2 );
453       samplingModes.SetFitHeight( 3 );
454       samplingModes.SetFitHeight( 4 );
455       samplingModes.SetFitHeight( 5 );
456
457       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NEAREST ) );
458       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::LINEAR ) );
459       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX ) );
460       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_NEAREST ) );
461       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_LINEAR ) );
462       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NO_FILTER ) );
463
464       mPopup.Add( samplingModes );
465       mPopup.Show();
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.Hide();
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.Hide();
509       mPopup.Reset();
510       return true;
511     }
512     return false;
513   }
514
515   void OnPopupOutsideTouched()
516   {
517     if( mPopup )
518     {
519       mPopup.Hide();
520       mPopup.Reset();
521     }
522   }
523
524   void OnImageLoaded( ResourceImage image )
525   {
526       DALI_ASSERT_DEBUG( image == mNextImage );
527       mImageActor.SetImage( image );
528       mImageActor.SetSize( Size( image.GetWidth(), image.GetHeight() ) );
529   }
530
531   bool OnControlTouched( Actor actor, const TouchEvent& event )
532   {
533     if(event.GetPointCount() > 0)
534     {
535       const TouchPoint& point = event.GetPoint(0);
536       switch(point.state)
537       {
538         case TouchPoint::Up:
539         {
540           const std::string & name = actor.GetName();
541           if( name == NEXT_BUTTON_ID )
542           {
543             mCurrentPath = mCurrentPath + 1;
544             mCurrentPath = mCurrentPath <  NUM_IMAGE_PATHS ? mCurrentPath : 0;
545             ResizeImage();
546           }
547           else if( name == PREVIOUS_BUTTON_ID )
548           {
549             mCurrentPath = mCurrentPath - 1;
550             mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
551             ResizeImage();
552           }
553           break;
554         }
555         default:
556         {
557           break;
558         }
559       } // end switch
560     }
561
562     return false;
563   }
564
565   void OnPinch( Actor actor, const PinchGesture& pinch )
566   {
567     if( pinch.state == Gesture::Started )
568     {
569       mLastPinchScale = pinch.scale;
570     }
571     const float scale = pinch.scale;
572
573     if( scale != mLastPinchScale )
574     {
575       if ( scale < mLastPinchScale )
576       {
577         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
578         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
579       }
580       else
581       {
582         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
583         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
584       }
585       ResizeImage();
586     }
587     mLastPinchScale = scale;
588   }
589
590   void OnPan( Actor actor, const PanGesture& gesture )
591   {
592     Stage stage = Stage::GetCurrent();
593     mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x + (gesture.displacement.x * 2.0f / stage.GetSize().width ) ) );
594     mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y + (gesture.displacement.y * 2.0f / stage.GetSize().height ) ) );
595     ResizeImage();
596   }
597
598   void OnKeyEvent(const KeyEvent& event)
599   {
600     if( event.state == KeyEvent::Down )
601     {
602       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
603       {
604         if( mPopup && mPopup.IsVisible() )
605         {
606           mPopup.Hide();
607           mPopup.Reset();
608         }
609         else
610         {
611           mApplication.Quit();
612         }
613       }
614       else if ( event.keyPressedName == "Right" )
615       {
616         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
617       }
618       else if ( event.keyPressedName == "Left" )
619       {
620         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
621       }
622       else if ( event.keyPressedName == "Up" )
623       {
624         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
625       }
626       else if ( event.keyPressedName == "Down" )
627       {
628         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
629       }
630       else if ( event.keyPressedName == "o" )
631       {
632         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
633         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
634       }
635       else if ( event.keyPressedName == "p" )
636       {
637         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
638         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
639       }
640       else if ( event.keyPressedName == "n" )
641       {
642         mCurrentPath = mCurrentPath + 1;
643         mCurrentPath = mCurrentPath <  NUM_IMAGE_PATHS ? mCurrentPath : 0;
644       }
645       else if ( event.keyPressedName == "b" )
646       {
647         mCurrentPath = mCurrentPath - 1;
648         mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
649       }
650       // Cycle filter and scaling modes:
651       else if ( event.keyPressedName == "f" )
652       {
653         mSamplingMode = NextFilterMode( mSamplingMode );
654         mSamplingModeButton.SetLabelText( StringFromFilterMode( mSamplingMode ) );
655       }
656       // Cycle filter and scaling modes:
657       else if ( event.keyPressedName == "s" )
658       {
659         mFittingMode = NextScalingMode( mFittingMode );
660         mFittingModeButton.SetLabelText( StringFromScalingMode( mFittingMode ) );
661       }
662       else
663       {
664         return;
665       }
666
667       ResizeImage();
668     }
669   }
670
671 private:
672   void ResizeImage()
673   {
674     const char * const path = IMAGE_PATHS[mCurrentPath];
675
676     Stage stage = Stage::GetCurrent();
677     Size imageSize = stage.GetSize() * mImageStageScale;
678     const ImageDimensions imageSizeInt = ImageDimensions::FromFloatArray( &imageSize.x );
679
680     ResourceImage image = ResourceImage::New( path, imageSizeInt, mFittingMode, mSamplingMode );
681     image.LoadingFinishedSignal().Connect( this, &ImageScalingAndFilteringController::OnImageLoaded );
682
683     mNextImage = image;
684
685     mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
686     mHeightBox.SetSize( stage.GetSize().width,  (stage.GetSize() * mImageStageScale).height );
687     mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
688   }
689
690 private:
691   Application&  mApplication;
692   Toolkit::ImageView mDesiredBox; //< Background rectangle to show requested image size.
693   Toolkit::ImageView mHeightBox;  //< Background horizontal stripe to show requested image height.
694   Toolkit::ImageView mWidthBox;   //< Background vertical stripe to show requested image width.
695   Toolkit::PushButton mFittingModeButton;
696   Toolkit::PushButton mSamplingModeButton;
697   Toolkit::Popup mPopup;
698   PinchGestureDetector mPinchDetector;
699   float mLastPinchScale;
700   Toolkit::PushButton  mGrabCorner;
701   PanGestureDetector mPanGestureDetector;
702   ImageActor mImageActor;
703   ResourceImage mNextImage; //< Currently-loading image
704   Vector2 mImageStageScale;
705   int mCurrentPath;
706   FittingMode::Type mFittingMode;
707   SamplingMode::Type mSamplingMode;
708 };
709
710 void RunTest( Application& application )
711 {
712   ImageScalingAndFilteringController test( application );
713
714   application.MainLoop();
715 }
716
717 // Entry point for Linux & Tizen applications
718 int main( int argc, char **argv )
719 {
720   Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
721
722   RunTest( application );
723
724   return 0;
725 }