#include <iostream>
#include <fstream>
#include <cassert>
#include "ofxCore.h"
#include "ofxImageEffect.h"
#include "ofxPixels.h"
#include "ofxhBinary.h"
#include "ofxhPropertySuite.h"
#include "ofxhClip.h"
#include "ofxhParam.h"
#include "ofxhMemory.h"
#include "ofxhImageEffect.h"
#include "ofxhPluginAPICache.h"
#include "ofxhPluginCache.h"
#include "ofxhHost.h"
#include "ofxhImageEffectAPI.h"
#include "hostDemoHostDescriptor.h"
#include "hostDemoEffectInstance.h"
#include "hostDemoClipInstance.h"
void exportToPPM(const std::string& fname, MyHost::MyImage* im)
{
std::ofstream op(fname.c_str());
OfxRectI rod = im->getROD();
op << "P3" << "\t# FORMAT" << std::endl;
op << rod.x2 - rod.x1 << "\t#WIDTH" << std::endl;
op << rod.y2 - rod.y1 << "\t#HEIGHT" <<std::endl;
op << "255" << std::endl;
for (int y = rod.y1; y< rod.y2; ++y)
{
for (int x = rod.x1; x < rod.x2; ++x)
{
OfxRGBAColourB* pix = im->pixel(x,y);
if(pix)
op << (int)pix->r << " " << (int)pix->g << " " << (int)pix->b << " " << std::endl;
else
op << "0 0 0" << std::endl;
}
}
}
int main(int argc, char **argv)
{
#ifdef _WIN32
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
OFX::Host::PluginCache::getPluginCache()->setCacheVersion("hostDemoV1");
MyHost::Host myHost;
OFX::Host::ImageEffect::PluginCache imageEffectPluginCache(myHost);
imageEffectPluginCache.registerInCache(*OFX::Host::PluginCache::getPluginCache());
std::ifstream ifs("hostDemoPluginCache.xml");
OFX::Host::PluginCache::getPluginCache()->readCache(ifs);
OFX::Host::PluginCache::getPluginCache()->scanPluginFiles();
ifs.close();
std::ofstream of("hostDemoPluginCache.xml");
OFX::Host::PluginCache::getPluginCache()->writePluginCache(of);
of.close();
OFX::Host::ImageEffect::ImageEffectPlugin* plugin = imageEffectPluginCache.getPluginById("net.sf.openfx:invertPlugin");
imageEffectPluginCache.dumpToStdOut();
if(plugin) {
std::auto_ptr<OFX::Host::ImageEffect::Instance> instance(plugin->createInstance(kOfxImageEffectContextFilter, NULL));
if(instance.get())
{
OfxStatus stat;
stat = instance->createInstanceAction();
assert(stat == kOfxStatOK || stat == kOfxStatReplyDefault);
bool ok = instance->getClipPreferences();
assert(ok);
OfxPointD renderScale;
renderScale.x = renderScale.y = 1.0;
OfxRectI renderWindow;
renderWindow.x1 = renderWindow.y1 = 0;
renderWindow.x2 = 720;
renderWindow.y2 = 576;
OfxRectD regionOfInterest;
regionOfInterest.x1 = regionOfInterest.y1 = 0;
regionOfInterest.x2 = renderWindow.x2 * instance->getProjectPixelAspectRatio();
regionOfInterest.y2 = 576;
int numFramesToRender = OFXHOSTDEMOCLIPLENGTH;
stat = instance->beginRenderAction(0, numFramesToRender, 1.0, false, renderScale, true, false
);
assert(stat == kOfxStatOK || stat == kOfxStatReplyDefault);
MyHost::MyClipInstance* outputClip = dynamic_cast<MyHost::MyClipInstance*>(instance->getClip("Output"));
assert(outputClip);
for(int t = 0; t <= numFramesToRender; ++t)
{
OfxTime frame = t;
std::map<OFX::Host::ImageEffect::ClipInstance *, OfxRectD> rois;
stat = instance->getRegionOfInterestAction(frame, renderScale,
regionOfInterest, rois);
assert(stat == kOfxStatOK || stat == kOfxStatReplyDefault);
stat = instance->renderAction(t,kOfxImageFieldBoth,renderWindow, renderScale, true, false, false);
assert(stat == kOfxStatOK);
MyHost::MyImage *outputImage = outputClip->getOutputImage();
std::ostringstream ss;
ss << "Output." << t << ".ppm";
exportToPPM(ss.str(), outputImage);
}
instance->endRenderAction(0, numFramesToRender, 1.0, false, renderScale, true, false
);
}
}
OFX::Host::PluginCache::clearPluginCache();
return 0;
}