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