#include "InputFrameListModule.h"
#include "utils.hpp"

#include <fstream>
#include <ostream>
#include <iostream>

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"

InputFrameListModule::InputFrameListModule(const std::string& basePath, const std::string& pictureList)
  : mCurrentIdx(0), mInit(false), mBasePath(basePath+"/") {
  loadFileList(pictureList);
}
InputFrameListModule::~InputFrameListModule()
{

}



bool InputFrameListModule::getNextFrame(cv::Mat& mat)
{
  bool success = false;
  if(mCurrentIdx < mFileList.size())
    {
      const std::string& fileName = mBasePath+mFileList[mCurrentIdx++];
      mCurrentFrame = cv::imread(fileName);

      if(!mCurrentFrame.empty())
	success = true;
      mat = mCurrentFrame;
    }

	
  return success;
}

unsigned int InputFrameListModule::getNbFrames()
{
  return mFileList.size();
}

void InputFrameListModule::loadFileList(const std::string& path)
{
  std::ifstream inputFile((mBasePath+path).c_str());
  ::openCheck(inputFile, mBasePath+path, "InputFrameListModule::loadFileList");
  std::string str;					
  while( !inputFile.eof() )
    {
      std::getline(inputFile, str);
      if (str.empty()) 
	break;
      if (str.at(0) == '#' ) 
	continue; /* comment */
      mFileList.push_back(str);
    }
		
  if(!mFileList.empty())
    {
      cv::Mat tmpImg = cv::imread(mBasePath+mFileList[0]);
      mSize = cv::Size(tmpImg.cols, tmpImg.rows);
      mInit = true;
    }
}
