123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- #include "sys.hpp" // for libcwd
- #include "debug.hpp" // for libcwd
- #include "objmovie.hpp"
- #include <external/tinyxml/tinyxml.h>
- #include <iostream>
- #include <math.h>
- #include <stdexcept>
- /////////////////////////////////////////
- using namespace std;
- class FileOpenError: public runtime_error
- {
- public:
- FileOpenError(const std::string& __arg="FATAl ERROR: failed open file\n"): runtime_error(__arg)
- {
- cerr << "FATAL ERROR: failed to open file " << __arg << "\n";
- }
- };
- MovieMetaFile::MovieMetaFile(const char* FileName): MovieMetaFileName(FileName)
- {
- Loaded = Load();
- }
- MovieMetaFile::~MovieMetaFile()
- {
- }
- bool MovieMetaFile::Load()
- {
- cout << "Load MovieMetaFile()\n"; fflush(stdout);
- int ErrorCode;
- TiXmlDocument doc(MovieMetaFileName.c_str());
- cout << "Loaded xml file\n"; fflush(stdout);
- if (doc.LoadFile()) {
- TiXmlElement *root=doc.RootElement();
- TiXmlElement *StimParasElement=(root->FirstChild("StimParas"))->ToElement();
- if (StimParasElement) {
- TiXmlElement *element=(StimParasElement->FirstChild("NXParas"))->ToElement();
- if (element) {
- ErrorCode = element->QueryIntAttribute("value", &NXParas);
- }
- element=(StimParasElement->FirstChild("NYParas"))->ToElement();
- if (element) {
- ErrorCode = element->QueryIntAttribute("value", &NYParas);
- }
- element=(StimParasElement->FirstChild("MovieFileName"))->ToElement();
- if (element) {
- MovieFileName = element->Attribute("value");
- }
- }
- Print();
- return true;
- } else {
- cerr << "ERROR: Failed Loading XML-MovieMetaFile " << MovieMetaFileName << "\n";
- return false;
- }
- }
- void MovieMetaFile::Print()
- {
- cout << "MovieMetaFile::Print()\n";
- cout << "MovieFileName=" << MovieFileName << "\n";
- cout << "XNPara=" << NXParas << "\n";
- cout << "YNPara=" << NYParas << "\n";
- }
- ////////////////////////////////////////
- MovieFilter2d::MovieFilter2d(): LastShiftedFrame(0), LastFrame(0)
- {
- }
- MovieFilter2d::MovieFilter2d(FILE *fw): LastShiftedFrame(0), LastFrame(0)
- {
- LoadFromFile(fw);
- }
- MovieFilter2d::~MovieFilter2d()
- {
- if (LastShiftedFrame != 0) delete [] LastShiftedFrame;
- if (LastFrame != 0) delete [] LastFrame;
- }
- bool MovieFilter2d::LoadFromFile(FILE *fw)
- {
- fread(&Header, sizeof(Header), 1, fw);
- Dout(dc::input, "OutputWidth=" << Header.outputwidth << " OutputHeight=" << Header.outputheight);
- FrameSize = Header.outputwidth*Header.outputheight;
- LastShiftedFrame = new float[FrameSize];
- LastFrame = new float[FrameSize];
- int ReadCount = fread(LastFrame, Header.width*Header.height*sizeof(*LastFrame), 1, fw);
- // if (ReadCount != 1) cout << "ReadCount = " << ReadCount << "****************\n\n\n";
- return (ReadCount == 1);
- }
- bool MovieFilter2d::LoadFilterFrame(FILE *fw)
- {
- return (fread(LastFrame, FrameSize*sizeof(*LastFrame), 1, fw) == 1);
- }
- int MovieFilter2d::GetFrameSize()
- {
- return FrameSize;
- }
- int MovieFilter2d::GetOutputWidth()
- {
- return Header.outputwidth;
- }
- int MovieFilter2d::GetOutputHeight()
- {
- return Header.outputheight;
- }
- float* MovieFilter2d::GetFramePointer()
- {
- return LastFrame;
- }
- float* MovieFilter2d::GetShiftedFramePointer()
- {
- return LastShiftedFrame;
- }
- bool MovieFilter2d::GetMinMaxValue(float &MinValue, float &MaxValue)
- {
- if (FrameSize > 0 || LastFrame!=0) {
- MinValue=*LastFrame;
- MaxValue=*LastFrame;
- for (int i=1;i<FrameSize;++i) {
- if (LastFrame[i]<MinValue) {
- MinValue=LastFrame[i];
- }
- if (LastFrame[i]>MaxValue) {
- MaxValue=LastFrame[i];
- }
- }
- } else {
- return false;
- }
- return true;
- }
- /**
- @brief shifts the original picture in x and y direction
- @param RelXShift relative shift of picture in x direction [0..1]
- @param RelYShift relative shift of picture in y direction [0..1]
- @author fm
- @since 29.10.2008
- */
- void MovieFilter2d::SetFrameShift(float RelXShift, float RelYShift)
- {
- // calculate absolute shift
- int owidth=Header.outputwidth;
- int oheight=Header.outputheight;
- int XShift=int(round(RelXShift*owidth)) % owidth;
- int YShift=int(round(RelYShift*oheight)) % oheight;
- // cout << "xshift=" << XShift << " yshift=" << YShift << "\n";
- // copy original picture to shifted picture, according to XShift and YShift
- int NewX, NewY;
- for (int y=0;y<oheight;++y) {
- NewY = (y + YShift) % oheight ;
- // cout << "y=" << y <<" NewY=" << NewY << "\n";
- for (int x=0;x<owidth;++x) {
- NewX = (x + XShift) % owidth;
- LastShiftedFrame[NewX+NewY*owidth] = LastFrame[x+y*owidth];
- }
- }
-
- }
- /////////////////
- ObjMovie::ObjMovie()
- : MovieFileName(), SwitchMaster(true), FrameNumber(0), fw(0)
- {
- gslr=GslSingleton::GetGslSingleton().GetGslRng();
- }
- ObjMovie::ObjMovie(
- const char* FileName)
- : MovieFileName(FileName), SwitchMaster(true), FrameNumber(0), fw(0)
- {
- LoadMovieFile(MovieFileName.c_str());
- gslr=GslSingleton::GetGslSingleton().GetGslRng();
- }
- ObjMovie::~ObjMovie()
- {
- for (vector<MovieFilter2d*>::iterator it=FilterList.begin(); it!=FilterList.end();++it)
- {
- delete (*it);
- }
- if (fw) {
- fclose(fw);
- }
- }
- int ObjMovie::LoadMovieFile(const char* FileName)
- {
- Dout(dc::input, "ObjMovie::LoadFile " << MovieFileName);
- Debug( libcw_do.inc_indent(2) );
- fw = fopen(FileName, "r");
- if (!fw) {
- throw FileOpenError(FileName);
- }
- fread(&MHeader, sizeof(MHeader), 1, fw);
-
- Dout(dc::input, "finfo=" << MHeader.finfo);
- Dout(dc::input, "version=" << MHeader.version);
- Dout(dc::input, "width= " << MHeader.width << " height=" << MHeader.height);
- //! \todo throw exception if no valid movie file (fm)
-
- fread(&NFilters, sizeof(NFilters), 1, fw);
- Dout(dc::input, "NFilters= " << NFilters);
- // read filters
- for (int i=0;i<NFilters;++i) FilterList.push_back(new MovieFilter2d(fw));
- fgetpos(fw, &FilePos_FirstFrame);
- // read frames
- InitializeFrameIndex();
- for (vector<MovieFilter2d*>::iterator it=FilterList.begin(); it!=FilterList.end();++it)
- {
- (*it)->LoadFilterFrame(fw);
- }
- // float MinVal = GetMinValue(0);
- Debug( libcw_do.dec_indent(2) );
- }
- float* ObjMovie::GetFramePointer(int FilterNr)
- {
- return FilterList[FilterNr]->GetFramePointer();
- }
- float* ObjMovie::GetShiftedFramePointer(int FilterNr)
- {
- return FilterList[FilterNr]->GetShiftedFramePointer();
- }
- int ObjMovie::GetFrameSize(int FilterNr)
- {
- return FilterList[FilterNr]->GetFrameSize();
- }
- int ObjMovie::GetOutputWidth(int FilterNr)
- {
- return FilterList[FilterNr]->GetOutputWidth();
- }
- int ObjMovie::GetOutputHeight(int FilterNr)
- {
- return FilterList[FilterNr]->GetOutputHeight();
- }
- void ObjMovie::ResetSwitchMaster()
- {
- SwitchMaster = true;
- SwitchMasterObj = 0;
- }
- bool ObjMovie::GetSwitchMaster()
- {
- // tell only one (the first) requester to be the SwitchMaster
- if (SwitchMaster)
- {
- SwitchMaster = false;
- return true;
- } else return false;
- }
- input* ObjMovie::GetSwitchMaster(input* Requester)
- {
- // tell only one (the first) requester to be the SwitchMaster
- if (SwitchMaster)
- {
- SwitchMaster = false;
- SwitchMasterObj = Requester;
- return 0;
- } else return SwitchMasterObj;
- }
- int ObjMovie::NextFrame()
- {
- // read frames
- ++FrameNumber;
- if ((FrameNumber >= NFrames) || feof(fw))
- {
- cout << "*************************feof(fw)==true \n ";
- fsetpos(fw, &FilePos_FirstFrame);
- FrameNumber=0;
- cout << "*************************RestartObjectMovieFile "
- << FrameNumber << "\n\n";
- }
- for (vector<MovieFilter2d*>::iterator it=FilterList.begin(); it!=FilterList.end();++it)
- {
- (*it)->LoadFilterFrame(fw);
- }
- return FrameNumber;
- }
- int ObjMovie::SetFrameShift(float RelXShift, float RelYShift)
- {
- for (vector<MovieFilter2d*>::iterator it=FilterList.begin(); it!=FilterList.end();++it)
- {
- (*it)->SetFrameShift(RelXShift,RelYShift);
- }
- }
- int ObjMovie::SetRandomShift()
- {
- float xshift=gsl_rng_uniform(gslr);
- float yshift=gsl_rng_uniform(gslr);
- SetFrameShift(xshift, yshift);
- }
- int ObjMovie::NextRandomFrame()
- {
- // read frames
- FrameNumber = gsl_rng_uniform_int(gslr, NFrames);
- CurFramePos = FrameIndex[FrameNumber];
- fsetpos(fw, &CurFramePos);
- for (vector<MovieFilter2d*>::iterator it=FilterList.begin(); it!=FilterList.end();++it)
- {
- (*it)->LoadFilterFrame(fw);
- }
- }
- int ObjMovie::GotoFrameNr(int _FrameNr)
- {
- if (_FrameNr < NFrames)
- {
- FrameNumber = _FrameNr;
- CurFramePos = FrameIndex[FrameNumber];
- fsetpos(fw, &CurFramePos);
-
- for (vector<MovieFilter2d*>::iterator it=FilterList.begin();
- it!=FilterList.end();++it)
- {
- (*it)->LoadFilterFrame(fw);
- }
- } else {
- cerr << "ERROR ObjMovie::GotoFrameNr: FrameNumber =" << _FrameNr << " too high (NFrames=" << NFrames << "\n";
- return 0;
- }
- //! \todo introduce exceptions for error handling
- return 1;
- }
- int ObjMovie::GetFrameNumber()
- {
- return FrameNumber;
- }
- int ObjMovie::GetNFrames()
- {
- return NFrames;
- }
- int ObjMovie::GetNFilters()
- {
- return NFilters;
- }
- int ObjMovie::InitializeFrameIndex()
- {
- fsetpos(fw, &FilePos_FirstFrame);
- NFrames=0;
- FrameNumber=0;
- Dout(dc::input, "InitializeFrameIndex");
- bool NoError=true;
- while (NoError) {
- fgetpos(fw, &CurFramePos);
- for (vector<MovieFilter2d*>::iterator it=FilterList.begin();
- it!=FilterList.end();++it)
- {
- NoError = (*it)->LoadFilterFrame(fw);
- }
- if (NoError) {
- FrameIndex.push_back(CurFramePos);
- ++NFrames;
- }
- }
- fsetpos(fw, &FilePos_FirstFrame);
- Dout(dc::input, "ObjMovieFile has " << NFrames << " Frames");
- }
- /**
- get the minimum value in the ObjMovie file
- noch nicht fertig!
- could be used to decide if on and off input layers should be used with the MovieFilter
- */
- float ObjMovie::GetMinValue(int FilterNr)
- {
- // store current frame number
- int OldFrameNumber = GetFrameNumber();
-
- // rewind movie to start frame
- int CurFrameNr=0;
- GotoFrameNr(0);
- float MinValue, MaxValue, GlobalMinValue, GlobalMaxValue;
-
- cout << "ObjMovie::GetMinValue()\n";
- // loop through every frame from start to end
- do {
- cout <<"FrameNr=" << FrameNumber <<"\n";
- // compare every pixel value of current frame (and selected filter) with global minimum
-
- } while (NextFrame()!=0);
-
- // restore old frame number
- GotoFrameNr(OldFrameNumber);
- }
- std::string GetDefaultMovieDir()
- {
- string MovieDir;
- if (getenv("OBJSIM_MOVIE_DIR")) {
- MovieDir = getenv("OBJSIM_MOVIE_DIR");
- cout << "set MovieDir from environment to " << MovieDir << std::endl;
- } else {
- cout << "environment variable OBJSIM_MOVIE_DIR is not set" << std::endl;
- }
- if (MovieDir.empty()) {
- MovieDir = string(getenv("HOME")) + "/prog/objsim/data/movies/";
- }
- if (MovieDir[MovieDir.length()-1] != '/')
- {
- MovieDir += "/";
- }
- cout << "DefaultMovieDir: " << MovieDir << "\n";
- return MovieDir;
- }
- /**
- @para FileName without .idlmov
- */
- ObjMovie* LoadObjMovie(const string& StimFileName, int & Width, int & Height, int & NFilters, int & NFrames, string MovieDir)
- {
- cout << "StimFileName=" << StimFileName << "\n";
- if (MovieDir.empty()) {
- MovieDir = GetDefaultMovieDir();
- }
- cout << "MovieDir=" << MovieDir << "\n";
- std::string MetaFileName = MovieDir + StimFileName + ".meta.xml";
- MovieMetaFile MetaFile(MetaFileName.c_str());
- std::string MovieFileName;
- if (MetaFile.Loaded) {
- MovieFileName = MetaFile.MovieFileName;
- } else {
- cout << "MovieMetaFile not loaded\n";
- MovieFileName = StimFileName + ".idlmov";
- cout << "using movie file name: MovieFileName\n";
- }
- std::string StimName;
- {
- StimName = MovieDir + MovieFileName;
- cout << "Load movie: " << StimName << "\n";
- }
-
- ObjMovie* MyMovie = new ObjMovie(StimName.c_str());
- Width = MyMovie->GetOutputWidth(0);
- Height = MyMovie->GetOutputHeight(0);
- NFrames = MyMovie->GetNFrames();
- NFilters = MyMovie->GetNFilters();
- return MyMovie;
- }
|