libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/sound/dynamic_algorithm.hpp
Go to the documentation of this file.
00001 
00012 /*
00013  *  Copyright (C) 2010 Juan Pedro Bolivar Puente
00014  *
00015  *  This file is part of Psychosynth.
00016  *   
00017  *  Psychosynth is free software: you can redistribute it and/or modify
00018  *  it under the terms of the GNU General Public License as published by
00019  *  the Free Software Foundation, either version 3 of the License, or
00020  *  (at your option) any later version.
00021  *
00022  *  Psychosynth is distributed in the hope that it will be useful,
00023  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025  *  GNU General Public License for more details.
00026  *
00027  *  You should have received a copy of the GNU General Public License
00028  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00029  *
00030  */
00031 
00032 /*
00033  *  Copyright 2005-2007 Adobe Systems Incorporated
00034  * 
00035  *  Use, modification and distribution are subject to the Boost
00036  *  Software License, Version 1.0. (See accompanying file
00037  *  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
00038  */
00039 
00040 #ifndef PSYNTH_SOUND_DYNAMIC_ALGORITHM_HPP
00041 #define PSYNTH_SOUND_DYNAMIC_ALGORITHM_HPP
00042 
00043 #include <psynth/sound/algorithm.hpp>
00044 #include <psynth/sound/dynamic_buffer.hpp>
00045 #include <boost/bind.hpp>
00046 
00047 namespace psynth
00048 {
00049 namespace sound
00050 {
00051 
00052 /*
00053  *
00054  *      equal_frames
00055  *
00056  */
00057 
00058 namespace detail
00059 {
00060 
00061 struct equal_frames_fn : public binary_operation_obj<equal_frames_fn, bool>
00062 {
00063     template <typename V1, typename V2> PSYNTH_FORCEINLINE
00064     bool apply_compatible (const V1& v1, const V2& v2) const
00065     {
00066         return equal_frames (v1, v2);
00067     }
00068 };
00069 
00070 } /* namespace detail */
00071 
00073 template <typename Types1,  // Model MPL Random Access Container of
00074                             // models of BufferRangeConcept
00075           typename Range2>   // Model MutableBufferRangeConcept
00076 bool equal_frames (const dynamic_buffer_range<Types1>& src, const Range2& dst)
00077 {
00078     return apply_operation (
00079         src, boost::bind (detail::equal_frames_fn(), _1, dst));
00080 }
00081 
00083 template <typename Types1,  // Model MPL Random Access Container of
00084                             // models of BufferRangeConcept
00085           typename Range2>   // Model MutableBufferRangeConcept
00086 bool equal_frames (const dynamic_buffer<Types1>& src, const Range2& dst)
00087 {
00088     return apply_operation (
00089         src, boost::bind (detail::equal_frames_fn (), _1, dst));
00090 }
00091 
00093 template <typename Range1,   // Model BufferRangeConcept
00094           typename Types2>  // Model MPL Random Access Container of
00095                             // models of MutableBufferRangeConcept
00096 bool equal_frames (const Range1& src, const dynamic_buffer_range<Types2>& dst)
00097 {
00098     return apply_operation (
00099         dst, boost::bind (detail::equal_frames_fn(), src, _1));
00100 }
00101 
00103 template <typename Types1,  // Model MPL Random Access Container of
00104                             // models of BufferRangeConcept
00105           typename Types2>  // Model MPL Random Access Container of
00106                             // models of MutableBufferRangeConcept
00107 bool equal_frames(const dynamic_buffer_range<Types1>& src,
00108                   const dynamic_buffer_range<Types2>& dst)
00109 {
00110     return apply_operation (src, dst, detail::equal_frames_fn());
00111 }
00112 
00113 
00114 /*
00115  *
00116  *      copy_frames
00117  *
00118  */
00119 
00120 namespace detail
00121 {
00122 
00123 struct copy_frames_fn : public binary_operation_obj<copy_frames_fn>
00124 {
00125     template <typename Range1, typename Range2> PSYNTH_FORCEINLINE
00126     void apply_compatible (const Range1& src, const Range2& dst) const
00127     {
00128         copy_frames(src,dst);
00129     }
00130 };
00131 
00132 } /* namespace detail */
00133 
00135 template <typename Types1,  // Model MPL Random Access Container of
00136                             // models of BufferRangeConcept
00137           typename Range2>   // Model MutableBufferRangeConcept
00138 void copy_frames (const dynamic_buffer_range<Types1>& src, const Range2& dst)
00139 {
00140     apply_operation (src, boost::bind (detail::copy_frames_fn(), _1, dst));
00141 }
00142 
00144 template <typename Range1,   // Model BufferRangeConcept
00145           typename Types2>  // Model MPL Random Access Container of
00146                             // models of MutableBufferRangeConcept
00147 void copy_frames (const Range1& src, const dynamic_buffer_range<Types2>& dst)
00148 {
00149     apply_operation (dst, boost::bind (detail::copy_frames_fn(), src, _1));
00150 }
00151 
00153 template <typename Types1,  // Model MPL Random Access Container of
00154                             // models of BufferRangeConcept
00155           typename Types2>  // Model MPL Random Access Container of
00156                             // models of MutableBufferRangeConcept
00157 void copy_frames (const dynamic_buffer_range<Types1>& src,
00158                   const dynamic_buffer_range<Types2>& dst)
00159 {
00160     apply_operation (src, dst, detail::copy_frames_fn());
00161 }
00162 
00163 
00164 /*
00165  *
00166  *      copy_and_convert_frames
00167  *
00168  */
00169 
00170 // forward declaration for default_channel_converter (see full
00171 // definition in channel_convert.hpp)
00172 struct default_channel_converter;
00173 
00175 template <typename Types1,  // Model MPL Random Access Container of
00176                             // models of BufferRangeConcept
00177           typename Range2,   // Model MutableBufferRangeConcept
00178           typename CC>      // Model ColorConverterConcept
00179 void copy_and_convert_frames (const dynamic_buffer_range<Types1>& src,
00180                               const Range2& dst, CC cc)
00181 {
00182     apply_operation (src, boost::bind (
00183                          detail::copy_and_convert_frames_fn<CC>(cc), _1, dst));
00184 }
00185 
00187 template <typename Types1,  // Model MPL Random Access Container of
00188                             // models of BufferRangeConcept
00189           typename Range2>   // Model MutableBufferRangeConcept
00190 void copy_and_convert_frames (const dynamic_buffer_range<Types1>& src,
00191                               const Range2& dst)
00192 {
00193     apply_operation (
00194         src, boost::bind (
00195             detail::copy_and_convert_frames_fn<default_channel_converter>(),
00196             _1, dst));
00197 }
00198 
00200 template <typename Range1,   // Model BufferRangeConcept
00201           typename Types2,  // Model MPL Random Access Container of
00202                             // models of MutableBufferRangeConcept
00203           typename CC>      // Model ColorConverterConcept
00204 void copy_and_convert_frames (const Range1& src,
00205                               const dynamic_buffer_range<Types2>& dst,
00206                               CC cc)
00207 {
00208     apply_operation (
00209         dst, boost::bind (detail::copy_and_convert_frames_fn<CC> (cc), src, _1));
00210 }
00211 
00213 template <typename Range1,   // Model BufferRangeConcept
00214           typename Types2>  // Model MPL Random Access Container of
00215                             // models of MutableBufferRangeConcept
00216 void copy_and_convert_frames (const Range1& src,
00217                               const dynamic_buffer_range<Types2>& dst)
00218 {
00219     apply_operation (
00220         dst, boost::bind (
00221             detail::copy_and_convert_frames_fn<default_channel_converter>(),
00222             src, _1));
00223 }
00224 
00226 template <typename Types1,  // Model MPL Random Access Container of
00227                             // models of BufferRangeConcept
00228           typename Types2,  // Model MPL Random Access Container of
00229                             // models of MutableBufferRangeConcept
00230           typename CC>      // Model ColorConverterConcept
00231 void copy_and_convert_frames (const dynamic_buffer_range<Types1>& src,
00232                               const dynamic_buffer_range<Types2>& dst,
00233                               CC cc)
00234 {
00235     apply_operation (src, dst, detail::copy_and_convert_frames_fn<CC>(cc));
00236 }
00237 
00239 template <typename Types1,  // Model MPL Random Access Container of
00240                             // models of BufferRangeConcept
00241           typename Types2>  // Model MPL Random Access Container of
00242                             // models of MutableBufferRangeConcept
00243 void copy_and_convert_frames (const dynamic_buffer_range<Types1>& src,
00244                               const dynamic_buffer_range<Types2>& dst)
00245 {
00246     apply_operation (
00247         src, dst,
00248         detail::copy_and_convert_frames_fn<default_channel_converter>());
00249 }
00250 
00251 
00252 /*
00253  *
00254  *      fill_frames
00255  *
00256  */
00257 
00258 namespace detail
00259 {
00260 
00261 template <bool Compatible> struct fill_frames_fn1
00262 {
00263     template <typename V, typename Value>
00264     static void apply (const V& src, const Value& val)
00265     {
00266         fill_frames (src, val);
00267     }
00268 };
00269 
00270 // copy_frames invoked on incompatible buffers
00271 template <>
00272 struct fill_frames_fn1<false>
00273 {
00274     template <typename V, typename Value>
00275     static void apply (const V& src, const Value& val)
00276     {
00277         throw std::bad_cast();
00278     }
00279 };
00280 
00281 template <typename Value>
00282 struct fill_frames_fn
00283 {
00284     fill_frames_fn (const Value& val)
00285         : _val (val) {}
00286 
00287     typedef void result_type;
00288     template <typename V>
00289     result_type operator ()(const V& img_range) const
00290     {
00291         fill_frames_fn1 <
00292             frames_are_compatible<
00293                 typename V::value_type, Value>::value>::apply (img_range,_val);
00294     }
00295     Value _val;
00296 };
00297 
00298 } /* namespace detail */
00299 
00305 template <typename Types, // Model MPL Random Access Container of
00306                           // models of MutableBufferRangeConcept
00307           typename Value>
00308 void fill_frames (const dynamic_buffer_range<Types>& range, const Value& val)
00309 {
00310     apply_operation (range, detail::fill_frames_fn<Value>(val));
00311 }
00312 
00318 template <typename Types, // Model MPL Random Access Container of
00319                           // models of MutableBufferRangeConcept
00320           typename Value>
00321 void fill_frames (dynamic_buffer<Types>& range, const Value& val)
00322 {
00323     apply_operation (range, detail::fill_frames_fn<Value>(val));
00324 }
00325 
00326 } /* namespace sound */
00327 } /* namespace psynth */
00328 
00329 #endif /* PSYNTH_SOUND_DYNAMIC_ALGORITHM */