TuttleOFX  1
ColorGradientPlugin.cpp
Go to the documentation of this file.
00001 #include "ColorGradientPlugin.hpp"
00002 #include "ColorGradientProcess.hpp"
00003 #include "ColorGradientAlgorithm.hpp"
00004 #include "ColorGradientDefinitions.hpp"
00005 
00006 #include <boost/gil/gil_all.hpp>
00007 #include <boost/algorithm/string/predicate.hpp>
00008 //#include <boost/algorithm/string/classification.hpp>
00009 
00010 namespace tuttle {
00011 namespace plugin {
00012 namespace colorGradient {
00013 
00014 using namespace boost::gil;
00015 
00016 ColorGradientPlugin::ColorGradientPlugin( OfxImageEffectHandle handle )
00017         : GeneratorPlugin( handle )
00018 {
00019         _gradientType = fetchChoiceParam( kGradientType );
00020         _nbPoints     = fetchIntParam( kNbPoints );
00021 
00022         _points.reserve( kMaxNbPoints );
00023         _colors.reserve( kMaxNbPoints );
00024         for( unsigned int i = 0; i < kMaxNbPoints; ++i )
00025         {
00026                 _points.push_back( fetchDouble2DParam( getPointParamName( i ) ) );
00027                 _colors.push_back( fetchRGBAParam( getColorParamName( i ) ) );
00028         }
00029         OFX::InstanceChangedArgs args;
00030         args.time          = 0;
00031         args.renderScale.x = 1;
00032         args.renderScale.y = 1;
00033         args.reason        = OFX::eChangePluginEdit;
00034 
00035         changedParam( args, kNbPoints ); // init IsSecret property for each point/color param
00036 }
00037 
00038 template<class View>
00039 ColorGradientProcessParams<View> ColorGradientPlugin::getProcessParams() const
00040 {
00041         ColorGradientProcessParams<View> params;
00042 
00043         Double2DParamVector::const_iterator it_point = _points.begin();
00044         RGBAParamVector::const_iterator it_color     = _colors.begin();
00045         unsigned int nbPoints                        = boost::numeric_cast<unsigned int>( _nbPoints->getValue() );
00046         for( unsigned int i = 0;
00047              i < nbPoints;
00048              ++i, ++it_point, ++it_color )
00049         {
00050                 OfxPointD p = ( *it_point )->getValue();
00051                 params._points.push_back( Point2( p.x, p.y ) );
00052                 OfxRGBAColourD c = ( *it_color )->getValue();
00053                 typename View::value_type pColor;
00054                 color_convert( rgba32f_pixel_t( c.r, c.g, c.b, c.a ), pColor );
00055                 params._colors.push_back( pColor );
00056         }
00057         return params;
00058 }
00059 
00060 template<template<typename> class Functor>
00061 void ColorGradientPlugin::renderFunctor( const OFX::RenderArguments& args )
00062 {
00063         using namespace boost::gil;
00064 
00065         // instantiate the render code based on the pixel depth of the dst clip
00066         OFX::EBitDepth bitDepth = _clipDst->getPixelDepth( );
00067         OFX::EPixelComponent components = _clipDst->getPixelComponents( );
00068 
00069         //doGilRender<ConstantProcess>( *this, args );
00070 
00071         switch( components )
00072         {
00073                 case OFX::ePixelComponentRGBA:
00074                 {
00075                         switch( bitDepth )
00076                         {
00077                                 case OFX::eBitDepthFloat:
00078                                 {
00079                                         ColorGradientProcess<rgba32f_view_t, Functor> p( *this );
00080                                         p.setupAndProcess( args );
00081                                         return;
00082                                 }
00083                                 case OFX::eBitDepthUByte:
00084                                 {
00085                                         ColorGradientProcess<rgba8_view_t, Functor> p( *this );
00086                                         p.setupAndProcess( args );
00087                                         return;
00088                                 }
00089                                 case OFX::eBitDepthUShort:
00090                                 {
00091                                         ColorGradientProcess<rgba16_view_t, Functor> p( *this );
00092                                         p.setupAndProcess( args );
00093                                         return;
00094                                 }
00095                                 case OFX::eBitDepthCustom:
00096                                 case OFX::eBitDepthNone:
00097                                 {
00098                                         BOOST_THROW_EXCEPTION( exception::Unsupported()
00099                                                 << exception::user() + "Bit depth (" + mapBitDepthEnumToString(bitDepth) + ") not recognized by the plugin." );
00100                                 }
00101                         }
00102                         break;
00103                 }
00104                 case OFX::ePixelComponentRGB:
00105                 {
00106                         switch( bitDepth )
00107                         {
00108                                 case OFX::eBitDepthFloat:
00109                                 {
00110                                         ColorGradientProcess<rgb32f_view_t, Functor> p( *this );
00111                                         p.setupAndProcess( args );
00112                                         return;
00113                                 }
00114                                 case OFX::eBitDepthUByte:
00115                                 {
00116                                         ColorGradientProcess<rgb8_view_t, Functor> p( *this );
00117                                         p.setupAndProcess( args );
00118                                         return;
00119                                 }
00120                                 case OFX::eBitDepthUShort:
00121                                 {
00122                                         ColorGradientProcess<rgb16_view_t, Functor> p( *this );
00123                                         p.setupAndProcess( args );
00124                                         return;
00125                                 }
00126                                 case OFX::eBitDepthCustom:
00127                                 case OFX::eBitDepthNone:
00128                                 {
00129                                         BOOST_THROW_EXCEPTION( exception::Unsupported()
00130                                                 << exception::user() + "Bit depth (" + mapBitDepthEnumToString(bitDepth) + ") not recognized by the plugin." );
00131                                 }
00132                         }
00133                         break;
00134                 }
00135                 case OFX::ePixelComponentAlpha:
00136                 {
00137                         switch( bitDepth )
00138                         {
00139                                 case OFX::eBitDepthFloat:
00140                                 {
00141                                         ColorGradientProcess<gray32f_view_t, Functor> p( *this );
00142                                         p.setupAndProcess( args );
00143                                         return;
00144                                 }
00145                                 case OFX::eBitDepthUByte:
00146                                 {
00147                                         ColorGradientProcess<gray8_view_t, Functor> p( *this );
00148                                         p.setupAndProcess( args );
00149                                         return;
00150                                 }
00151                                 case OFX::eBitDepthUShort:
00152                                 {
00153                                         ColorGradientProcess<gray16_view_t, Functor> p( *this );
00154                                         p.setupAndProcess( args );
00155                                         return;
00156                                 }
00157                                 case OFX::eBitDepthCustom:
00158                                 case OFX::eBitDepthNone:
00159                                 {
00160                                         BOOST_THROW_EXCEPTION( exception::Unsupported()
00161                                                 << exception::user() + "Bit depth (" + mapBitDepthEnumToString(bitDepth) + ") not recognized by the plugin." );
00162                                 }
00163                         }
00164                         break;
00165                 }
00166                 case OFX::ePixelComponentCustom:
00167                 case OFX::ePixelComponentNone:
00168                 {
00169                         BOOST_THROW_EXCEPTION( exception::Unsupported()
00170                                 << exception::user() + "Pixel components (" + mapPixelComponentEnumToString(components) + ") not supported by the plugin." );
00171                 }
00172         }
00173         BOOST_THROW_EXCEPTION( exception::Unknown() );
00174 }
00175 
00176 /**
00177  * @brief The overridden render function
00178  * @param[in]   args     Rendering parameters
00179  */
00180 void ColorGradientPlugin::render( const OFX::RenderArguments& args )
00181 {
00182         switch( static_cast<EGradientType>( _gradientType->getValue() ) )
00183         {
00184                 case eGradientType1DLinear:
00185                 {
00186                         renderFunctor<ColorGrandient1DLinearFunctor>( args );
00187                         return;
00188                 }
00189                 case eGradientType1DRadial:
00190                 {
00191                         renderFunctor<ColorGrandient1DLinearFunctor>( args ); /// @todo tuttle: not implemented yet
00192                         return;
00193                 }
00194                 case eGradientType2D:
00195                 {
00196                         renderFunctor<ColorGrandient2DLinearFunctor>( args );
00197                         return;
00198                 }
00199         }
00200         BOOST_THROW_EXCEPTION( exception::Unknown() );
00201 }
00202 
00203 void ColorGradientPlugin::getClipPreferences( OFX::ClipPreferencesSetter& clipPreferences )
00204 {
00205         GeneratorPlugin::getClipPreferences( clipPreferences );
00206 }
00207 
00208 void ColorGradientPlugin::changedParam( const OFX::InstanceChangedArgs& args, const std::string& paramName )
00209 {
00210         GeneratorPlugin::changedParam( args, paramName );
00211         if( paramName == kNbPoints )
00212         {
00213                 unsigned int nbPoints                  = boost::numeric_cast<unsigned int>( _nbPoints->getValue() );
00214                 Double2DParamVector::iterator it_point = _points.begin();
00215                 RGBAParamVector::iterator it_color     = _colors.begin();
00216                 for( unsigned int i = 0; i < nbPoints; ++i, ++it_point, ++it_color )
00217                 {
00218                         ( *it_point )->setIsSecret( false );
00219                         ( *it_color )->setIsSecret( false );
00220                 }
00221                 for( unsigned int i = nbPoints; i < kMaxNbPoints; ++i, ++it_point, ++it_color )
00222                 {
00223                         ( *it_point )->setIsSecret( true );
00224                         ( *it_color )->setIsSecret( true );
00225                 }
00226         }
00227         /*
00228         else if( boost::starts_with( paramName, kPoint ) )
00229         {
00230                 try
00231                 {
00232                         unsigned int n            = boost::lexical_cast<unsigned int>( paramName.c_str() + kPoint.size() );
00233                         OFX::Double2DParam* param = _points[n];
00234                         OfxPointD p               = param->getValue();
00235                         if( n < 2 )
00236                         {
00237                                 // A, B
00238                         }
00239                         else
00240                         {
00241                                 //
00242                         }
00243                 }
00244                 catch( boost::bad_lexical_cast& )
00245                 {}
00246         }
00247         */
00248 
00249 }
00250 
00251 }
00252 }
00253 }