TuttleOFX  1
TurboJpegReaderPlugin.cpp
Go to the documentation of this file.
00001 #include "TurboJpegReaderPlugin.hpp"
00002 #include "TurboJpegReaderProcess.hpp"
00003 #include "TurboJpegReaderDefinitions.hpp"
00004 
00005 #include <boost/gil/gil_all.hpp>
00006 #include <boost/filesystem.hpp>
00007 
00008 #include <cstdio>
00009 #include <turbojpeg.h>
00010 
00011 namespace tuttle {
00012 namespace plugin {
00013 namespace turboJpeg {
00014 namespace reader {
00015 
00016 TurboJpegReaderPlugin::TurboJpegReaderPlugin( OfxImageEffectHandle handle )
00017 : ReaderPlugin( handle )
00018 {
00019         _optimization   = fetchChoiceParam( kParamOptimization );
00020         _fastUpsampling = fetchBooleanParam( kParamFastUpsampling );
00021 }
00022 
00023 TurboJpegReaderProcessParams TurboJpegReaderPlugin::getProcessParams( const OfxTime time ) const
00024 {
00025         TurboJpegReaderProcessParams params;
00026         params.filepath = getAbsoluteFilenameAt( time );
00027         params.optimization = static_cast< ETurboJpegOptimization >( _optimization->getValue() );
00028         params.fastUpsampling = _fastUpsampling->getValue();
00029         return params;
00030 }
00031 
00032 void TurboJpegReaderPlugin::changedParam( const OFX::InstanceChangedArgs &args, const std::string &paramName )
00033 {
00034         ReaderPlugin::changedParam( args, paramName );
00035 }
00036 
00037 bool TurboJpegReaderPlugin::getRegionOfDefinition( const OFX::RegionOfDefinitionArguments& args, OfxRectD& rod )
00038 {
00039         try
00040         {
00041                 FILE *file = NULL;
00042                 unsigned char *jpegbuf = NULL;
00043                 unsigned long jpgbufsize = 0;
00044 
00045                 file = fopen( getAbsoluteFilenameAt( args.time ).c_str(), "rb" );
00046                 if( file == NULL )
00047                 {
00048                         BOOST_THROW_EXCEPTION( exception::File()
00049                                 << exception::user( "TurboJpeg: Unable to open file" )
00050                                 << exception::filename( getAbsoluteFilenameAt( args.time ) ) );
00051                 }
00052                 
00053                 fseek( file, 0, SEEK_END );
00054                 jpgbufsize = ftell( file );
00055                 jpegbuf = new unsigned char[ jpgbufsize ];
00056                 
00057                 fseek(file, 0, SEEK_SET);
00058                 fread( jpegbuf, jpgbufsize, 1, file );
00059 
00060                 const tjhandle jpeghandle = tjInitDecompress();
00061                 int width = 0;
00062                 int height = 0;
00063                 int jpegsubsamp = -1;
00064                 
00065                 int ret = tjDecompressHeader2( jpeghandle, jpegbuf, jpgbufsize, &width, &height, &jpegsubsamp );
00066                 
00067                 if( ret != 0 )
00068                 {
00069                         BOOST_THROW_EXCEPTION( exception::FileNotExist()
00070                                 << exception::user( tjGetErrorStr() )
00071                                 << exception::filename( getAbsoluteFilenameAt( args.time ) ) );
00072                 }
00073                 
00074                 tjDestroy( jpeghandle );
00075                 //free(jpegbuf);
00076                 delete[] jpegbuf;
00077                 jpegbuf = NULL;
00078                 
00079                 fclose(file);
00080                 file=NULL;
00081                 
00082                 rod.x1 = 0;
00083                 rod.x2 = width * this->_clipDst->getPixelAspectRatio();
00084                 rod.y1 = 0;
00085                 rod.y2 = height;
00086                 //TUTTLE_LOG_VAR( TUTTLE_INFO, rod );
00087         }
00088         catch( std::exception& e )
00089         {
00090                 BOOST_THROW_EXCEPTION( exception::FileInSequenceNotExist()
00091                         << exception::user( "TurboJpeg: Unable to open file" )
00092                         << exception::filename( getAbsoluteFilenameAt( args.time ) ) );
00093         }
00094         
00095         return true;
00096 }
00097 
00098 void TurboJpegReaderPlugin::getClipPreferences( OFX::ClipPreferencesSetter& clipPreferences )
00099 {
00100         ReaderPlugin::getClipPreferences( clipPreferences );
00101 
00102         if( getExplicitBitDepthConversion() == eParamReaderBitDepthAuto )
00103         {
00104                 clipPreferences.setClipBitDepth( *this->_clipDst, OFX::eBitDepthUByte );
00105         }
00106 
00107         if( getExplicitChannelConversion() == eParamReaderChannelAuto )
00108         {
00109                 if( OFX::getImageEffectHostDescription()->supportsPixelComponent( OFX::ePixelComponentRGB ) )
00110                 {
00111                         clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentRGB );
00112                 }
00113                 else
00114                 {
00115                         clipPreferences.setClipComponents( *this->_clipDst, OFX::ePixelComponentRGBA );
00116                 }
00117         }
00118         
00119         clipPreferences.setPixelAspectRatio( *this->_clipDst, 1.0 );
00120 }
00121 
00122 /**
00123  * @brief The overridden render function
00124  * @param[in]   args     Rendering parameters
00125  */
00126 void TurboJpegReaderPlugin::render( const OFX::RenderArguments &args )
00127 {
00128         ReaderPlugin::render(args);
00129         doGilRender<TurboJpegReaderProcess>( *this, args );
00130 }
00131 
00132 }
00133 }
00134 }
00135 }