1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_SLIDESHOW_INTERPOLATION_HXX
29 #define INCLUDED_SLIDESHOW_INTERPOLATION_HXX
30 
31 #include <basegfx/tools/lerp.hxx>
32 
33 namespace basegfx
34 {
35     namespace tools
36     {
37         // Interpolator specializations
38         // ============================
39 
40         // NOTE: generic lerp is included from lerp.hxx. Following
41         // are some specializations for various
42         // not-straight-forward-interpolatable types
43 
44         /// Specialization for RGBColor, to employ color-specific interpolator
45 		template<> ::slideshow::internal::RGBColor lerp< ::slideshow::internal::RGBColor >(
46             const ::slideshow::internal::RGBColor& rFrom,
47             const ::slideshow::internal::RGBColor& rTo,
48             double			                       t	 )
49         {
50             return interpolate( rFrom, rTo, t );
51         }
52 
53         /// Specialization also for sal_Int16, although this code should not be called
54         template<> sal_Int16 lerp< sal_Int16 >( const sal_Int16&,
55                                                 const sal_Int16& 	rTo,
56                                                 double					   )
57         {
58             OSL_ENSURE( false,
59                         "lerp<sal_Int16> called" );
60             return rTo;
61         }
62 
63 		/// Specialization also for string, although this code should not be called
64         template<> ::rtl::OUString lerp< ::rtl::OUString >( const ::rtl::OUString&,
65                                                             const ::rtl::OUString& 	rTo,
66                                                             double					     )
67         {
68             OSL_ENSURE( false,
69                         "lerp<::rtl::OUString> called" );
70             return rTo;
71         }
72 
73 		/// Specialization also for bool, although this code should not be called
74         template<> bool lerp< bool >( const bool&,
75                                       const bool& 	rTo,
76                                       double		     )
77         {
78             OSL_ENSURE( false,
79                         "lerp<bool> called" );
80             return rTo;
81         }
82     }
83 }
84 
85 namespace slideshow
86 {
87     namespace internal
88     {
89         template< typename ValueType > struct Interpolator
90         {
91             ValueType operator()( const ValueType& 	rFrom,
92                                   const ValueType& 	rTo,
93                                   double			t ) const
94             {
95                 return basegfx::tools::lerp( rFrom, rTo, t );
96             }
97         };
98 
99 		/// Specialization for HSLColor, to employ color-specific interpolator
100         template<> struct Interpolator< HSLColor >
101         {
102             Interpolator( bool bCCW ) :
103                 mbCCW( bCCW )
104             {
105             }
106 
107             HSLColor operator()( const HSLColor&	rFrom,
108                                  const HSLColor&	rTo,
109                                  double				t ) const
110             {
111                 return interpolate( rFrom, rTo, t, mbCCW );
112             }
113 
114         private:
115             /// When true: interpolate counter-clockwise
116             const bool mbCCW;
117         };
118 
119 
120 		/** Generic linear interpolator
121 
122 			@tpl ValueType
123             Must have operator+ and operator* defined, and should
124             have value semantics.
125 
126             @param rInterpolator
127             Interpolator to use for lerp
128 
129             @param nFrame
130             Must be in the [0,nTotalFrames) range
131 
132             @param nTotalFrames
133             Total number of frames. Should be greater than zero.
134         */
135         template< typename ValueType > ValueType lerp( const Interpolator< ValueType >& rInterpolator,
136                                                        const ValueType& 				rFrom,
137                                                        const ValueType& 				rTo,
138                                                        sal_uInt32						nFrame,
139                                                        ::std::size_t					nTotalFrames )
140         {
141             // TODO(P1): There's a nice HAKMEM trick for that
142             // nTotalFrames > 1 condition below
143 
144             // for 1 and 0 frame animations, always take end value
145             const double nFraction( nTotalFrames > 1 ? double(nFrame)/(nTotalFrames-1) : 1.0 );
146 
147             return rInterpolator( rFrom, rTo, nFraction );
148         }
149 
150 		/// Specialization for non-interpolatable constants/enums
151         template<> sal_Int16 lerp< sal_Int16 >( const Interpolator< sal_Int16 >& 	/*rInterpolator*/,
152                                                 const sal_Int16& 					rFrom,
153                                                 const sal_Int16& 					rTo,
154                                                 sal_uInt32							nFrame,
155                                                 ::std::size_t						nTotalFrames )
156         {
157             // until one half of the total frames are over, take from value.
158             // after that, take to value.
159             // For nFrames not divisable by 2, we prefer to over from, which
160             // also neatly yields to for 1 frame activities
161             return nFrame < nTotalFrames/2 ? rFrom : rTo;
162         }
163 
164 		/// Specialization for non-interpolatable strings
165         template<> ::rtl::OUString lerp< ::rtl::OUString >( const Interpolator< ::rtl::OUString >& 	/*rInterpolator*/,
166                                                             const ::rtl::OUString& 					rFrom,
167                                                             const ::rtl::OUString& 					rTo,
168                                                             sal_uInt32								nFrame,
169                                                             ::std::size_t							nTotalFrames )
170         {
171             // until one half of the total frames are over, take from value.
172             // after that, take to value.
173             // For nFrames not divisable by 2, we prefer to over from, which
174             // also neatly yields to for 1 frame activities
175             return nFrame < nTotalFrames/2 ? rFrom : rTo;
176         }
177 
178 		/// Specialization for non-interpolatable bools
179         template<> bool lerp< bool >( const Interpolator< bool >& 	/*rInterpolator*/,
180                                       const bool&					bFrom,
181                                       const bool&					bTo,
182                                       sal_uInt32					nFrame,
183                                       ::std::size_t					nTotalFrames )
184         {
185             // until one half of the total frames are over, take from value.
186             // after that, take to value.
187             // For nFrames not divisable by 2, we prefer to over from, which
188             // also neatly yields to for 1 frame activities
189             return nFrame < nTotalFrames/2 ? bFrom : bTo;
190         }
191     }
192 }
193 
194 #endif /* INCLUDED_SLIDESHOW_INTERPOLATION_HXX */
195