Stop using ImageActor in EffectsView and ImageFilters
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / filters / image-filter.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 // CLASS HEADER
19 #include "image-filter.h"
20
21 // INTERNAL INCLUDES
22
23 namespace Dali
24 {
25
26 namespace Toolkit
27 {
28
29 namespace Internal
30 {
31
32 namespace
33 {
34 const float ARBITRARY_FIELD_OF_VIEW = Math::PI / 4.0f;
35 } // namespace
36
37 ImageFilter::ImageFilter()
38 : mBackgroundColor( Vector4( 1.0f, 1.0f, 1.0f, 0.0f ) ),
39   mTargetSize( Vector2::ZERO ),
40   mPixelFormat( Pixel::RGBA8888 ),
41   mRefreshOnDemand( false )
42 {
43 }
44
45 ImageFilter::~ImageFilter()
46 {
47 }
48
49 void ImageFilter::SetRefreshOnDemand( bool onDemand )
50 {
51   mRefreshOnDemand = onDemand;
52 }
53
54 void ImageFilter::SetInputImage( Image image )
55 {
56   mInputImage = image;
57 }
58
59 void ImageFilter::SetOutputImage( FrameBufferImage image )
60 {
61   mOutputImage = image;
62 }
63
64 void ImageFilter::SetSize( const Vector2& size )
65 {
66   mTargetSize = size;
67 }
68
69 void ImageFilter::SetPixelFormat( Pixel::Format pixelFormat )
70 {
71   mPixelFormat = pixelFormat;
72 }
73
74 void ImageFilter::SetKernel( const FilterKernel& kernel )
75 {
76   mKernel = kernel;
77 }
78
79 const ImageFilter::FilterKernel& ImageFilter::GetKernel() const
80 {
81   return mKernel;
82 }
83
84 size_t ImageFilter::GetKernelSize() const
85 {
86   return mKernel.size();
87 }
88
89 void ImageFilter::CreateKernel( const float* weights, size_t count )
90 {
91   if( (mTargetSize.width * mTargetSize.height ) > 0.0f )
92   {
93     Vector2 pixelsToUV( 1.0f / mTargetSize.width, 1.0f / mTargetSize.height );
94
95     mKernel.clear();
96
97     mKernel.push_back( Vector3( 0.0f, 0.0f, weights[0] ) );
98     for( size_t i = 0; i < count >> 1; ++i )
99     {
100       float offset = 1.5f + (i << 1);
101
102       mKernel.push_back( Vector3( pixelsToUV.x * offset, pixelsToUV.y * offset, weights[(i << 1) + 1] ) );
103       mKernel.push_back( Vector3( -pixelsToUV.x * offset, -pixelsToUV.y * offset, weights[(i << 1) + 2] ) );
104     }
105   }
106 }
107
108 void ImageFilter::SetRootActor( Actor rootActor )
109 {
110   mRootActor = rootActor;
111 }
112
113 void ImageFilter::SetBackgroundColor( const Vector4& color )
114 {
115   mBackgroundColor = color;
116 }
117
118 void ImageFilter::SetupCamera()
119 {
120   if( !mCameraActor )
121   {
122     // create a camera for the render task, corresponding to its render target size
123     mCameraActor = CameraActor::New(mTargetSize);
124     mCameraActor.SetParentOrigin(ParentOrigin::CENTER);
125     mCameraActor.SetInvertYAxis( true );
126     mRootActor.Add( mCameraActor );
127   }
128   else
129   {
130     // place the camera for the render task, corresponding to its render target size
131     mCameraActor.SetFieldOfView(ARBITRARY_FIELD_OF_VIEW);
132     mCameraActor.SetNearClippingPlane(1.0f);
133     mCameraActor.SetAspectRatio(mTargetSize.width / mTargetSize.height);
134     mCameraActor.SetType(Dali::Camera::FREE_LOOK); // camera orientation based solely on actor
135     mCameraActor.SetPosition(0.0f, 0.0f, ((mTargetSize.height * 0.5f) / tanf(ARBITRARY_FIELD_OF_VIEW * 0.5f)));
136   }
137 }
138
139
140 } // namespace Internal
141
142 } // namespace Toolkit
143
144 } // namespace Dali