TuttleOFX  1
EXRReaderPlugin.cpp
Go to the documentation of this file.
00001 #include "EXRReaderDefinitions.hpp"
00002 #include "EXRReaderPlugin.hpp"
00003 #include "EXRReaderProcess.hpp"
00004 
00005 #include <tuttle/plugin/context/ReaderPlugin.hpp>
00006 
00007 #include <ImfInputFile.h>
00008 #include <ImathBox.h>
00009 #include <ImfChannelList.h>
00010 
00011 #include <boost/gil/gil_all.hpp>
00012 #include <boost/filesystem.hpp>
00013 
00014 namespace tuttle {
00015 namespace plugin {
00016 namespace exr {
00017 namespace reader {
00018 
00019 namespace bfs = boost::filesystem;
00020 using namespace Imf;
00021 using namespace boost::gil;
00022 
00023 EXRReaderPlugin::EXRReaderPlugin( OfxImageEffectHandle handle )
00024         : ReaderPlugin( handle )
00025         , _channels ( 0 )
00026         , _par      ( 1.0 )
00027 {
00028         _outComponents   = fetchChoiceParam( kTuttlePluginChannel );
00029         _redComponents   = fetchChoiceParam( kParamOutputRedIs );
00030         _greenComponents = fetchChoiceParam( kParamOutputGreenIs );
00031         _blueComponents  = fetchChoiceParam( kParamOutputBlueIs );
00032         _alphaComponents = fetchChoiceParam( kParamOutputAlphaIs );
00033         
00034         _outputData      = fetchChoiceParam( kParamOutputData );
00035         
00036         _vChannelChoice.push_back( fetchChoiceParam( kParamOutputRedIs ) );
00037         _vChannelChoice.push_back( fetchChoiceParam( kParamOutputGreenIs ) );
00038         _vChannelChoice.push_back( fetchChoiceParam( kParamOutputBlueIs ) );
00039         _vChannelChoice.push_back( fetchChoiceParam( kParamOutputAlphaIs ) );
00040 
00041         updateCombos();
00042 }
00043 
00044 EXRReaderProcessParams EXRReaderPlugin::getProcessParams( const OfxTime time )
00045 {
00046         EXRReaderProcessParams params;
00047 
00048         params._filepath       = getAbsoluteFilenameAt( time );
00049         params._outComponents  = _outComponents->getValue();
00050         params._fileComponents = _channels;
00051         
00052         
00053         params._redChannelIndex   = _redComponents->getValue();
00054         params._greenChannelIndex = _greenComponents->getValue();
00055         params._blueChannelIndex  = _blueComponents->getValue();
00056         params._alphaChannelIndex = _alphaComponents->getValue();
00057         
00058         params._displayWindow     = ( _outputData->getValue() == 0 );
00059         
00060         return params;
00061 }
00062 
00063 void EXRReaderPlugin::changedParam( const OFX::InstanceChangedArgs& args, const std::string& paramName )
00064 {
00065         if( paramName == kTuttlePluginFilename )
00066         {
00067                 ReaderPlugin::changedParam( args, paramName );
00068                 updateCombos();
00069         }
00070         else if( paramName == kTuttlePluginChannel )
00071         {
00072                 switch( _outComponents->getValue() )
00073                 {
00074                         case eParamReaderChannelGray:
00075                         {
00076                                 for( std::size_t j = 0; j < _vChannelChoice.size() - 1; ++j )
00077                                 {
00078                                         _vChannelChoice[j]->setIsSecret( true );
00079                                 }
00080                                 _vChannelChoice[3]->setIsSecret( false );
00081                                 break;
00082                         }
00083                         case eParamReaderChannelRGB:
00084                         {
00085                                 for( std::size_t j = 0; j < _vChannelChoice.size() - 1; ++j )
00086                                 {
00087                                         _vChannelChoice[j]->setIsSecret( false );
00088                                 }
00089                                 _vChannelChoice[3]->setIsSecret( true );
00090                                 break;
00091                         }
00092                         case eParamReaderChannelAuto:
00093                         case eParamReaderChannelRGBA:
00094                         {       
00095                                 for( std::size_t j = 0; j < _vChannelChoice.size(); ++j )
00096                                 {
00097                                         _vChannelChoice[j]->setIsSecret( false );
00098                                 }
00099                                 break;
00100                         }
00101                 }
00102         }
00103         else
00104         {
00105                 ReaderPlugin::changedParam( args, paramName );
00106         }
00107 }
00108 
00109 void EXRReaderPlugin::updateCombos()
00110 {
00111         const std::string filename( getAbsoluteFirstFilename() );
00112         //TUTTLE_LOG_VAR("update Combo");
00113         if( bfs::exists( filename ) )
00114         {
00115                 // read dims
00116                 InputFile in( filename.c_str() );
00117                 const Header& h  = in.header();
00118                 const ChannelList& cl = h.channels();
00119                 
00120                 _par = h.pixelAspectRatio();
00121                 
00122                 // Hide output channel selection till we don't select a channel.
00123                 for( std::size_t i = 0; i < _vChannelChoice.size(); ++i )
00124                 {
00125                         //_vChannelChoice[i]->setIsSecret( true );
00126                         _vChannelChoice[i]->resetOptions();
00127                 }
00128                 _vChannelNames.clear();
00129                 for( ChannelList::ConstIterator it = cl.begin(); it != cl.end(); ++it )
00130                 {
00131                         _vChannelNames.push_back( it.name() );
00132                         //TUTTLE_LOG_VAR( it.name() );
00133                         for( std::size_t j = 0; j < _vChannelChoice.size(); ++j )
00134                         {
00135                                 _vChannelChoice[j]->appendOption( it.name() );
00136                         }
00137                         ++_channels;
00138                         switch( _channels )
00139                         {
00140                                 case 1:
00141                                 {
00142                                         for( std::size_t j = 0; j < _vChannelChoice.size(); j++ )
00143                                                 _vChannelChoice.at(j)->setValue( 0 );
00144                                         break;
00145                                 }
00146                                 case 3:
00147                                 {
00148                                         for( std::size_t j = 0; j < _vChannelChoice.size() - 1; j++ )
00149                                                 _vChannelChoice.at(j)->setValue( 2 - j );
00150                                         _vChannelChoice.at(3)->setValue( 0 );
00151                                         break;
00152                                 }
00153                                 case 4:
00154                                 {
00155                                         for( std::size_t j = 0; j < _vChannelChoice.size(); j++ )
00156                                                 _vChannelChoice.at(j)->setValue( 3 - j );
00157                                         break;
00158                                 }
00159                         }
00160                 }
00161         }
00162 }
00163 
00164 void EXRReaderPlugin::getClipPreferences( OFX::ClipPreferencesSetter& clipPreferences )
00165 {
00166         ReaderPlugin::getClipPreferences( clipPreferences );
00167         
00168         if( getExplicitChannelConversion() == eParamReaderChannelAuto )
00169         {
00170                 switch( _channels )
00171                 {
00172                         case 1:
00173                         {
00174                                 clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentAlpha );
00175                                 break;
00176                         }
00177                         case 3:
00178                         {
00179                                 if( OFX::getImageEffectHostDescription()->supportsPixelComponent( OFX::ePixelComponentRGB ) )
00180                                         clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentRGB );
00181                                 else
00182                                         clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentRGBA );
00183                                 break;
00184                         }
00185                         case 4:
00186                         {
00187                                 clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentRGBA );
00188                                 break;
00189                         }
00190                         default:
00191                         {
00192                                 std::string msg = "EXR: not support ";
00193                                 msg += _channels;
00194                                 msg += " channels.";
00195                                 BOOST_THROW_EXCEPTION( exception::FileNotExist()
00196                                                                            << exception::user( msg ) );
00197                                 break;
00198                         }
00199                 }
00200         }
00201 
00202         clipPreferences.setPixelAspectRatio( *this->_clipDst, _par );
00203 }
00204 
00205 bool EXRReaderPlugin::getRegionOfDefinition( const OFX::RegionOfDefinitionArguments& args, OfxRectD& rod )
00206 {
00207         try
00208         {
00209                 InputFile in( getAbsoluteFilenameAt( args.time ).c_str() );
00210                 const Header&      h      = in.header();
00211                 const Imath::Box2i window( ( _outputData->getValue() == 0 ) ? h.displayWindow() : h.dataWindow() );
00212 
00213                 std::cout << "getRegionOfDefinition " << ( window.size().x + 1 ) * _par << " x " << window.size().y + 1 << std::endl;
00214                 
00215                 rod.x1 = 0;
00216                 rod.x2 = window.size().x + 1;
00217                 rod.y1 = 0;
00218                 rod.y2 = window.size().y + 1;
00219 
00220         }
00221         catch( ... )
00222         {
00223                 BOOST_THROW_EXCEPTION( exception::FileInSequenceNotExist()
00224                 << exception::user( "EXR: Unable to open file." )
00225                 << exception::filename( getAbsoluteFilenameAt( args.time ) ) );
00226         }
00227         return true;
00228 }
00229 
00230 /**
00231  * @brief The overridden render function
00232  * @param[in]   args     Rendering parameters
00233  */
00234 void EXRReaderPlugin::render( const OFX::RenderArguments& args )
00235 {
00236         ReaderPlugin::render( args );
00237         doGilRender<EXRReaderProcess>( *this, args );
00238 }
00239 
00240 
00241 }
00242 }
00243 }
00244 }