/*
-----------------------------------------------------------------------------
This source file is part of OpenSpace3D
For the latest info, see http://www.openspace3d.com

Copyright (c) 2010 I-maginer

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt

You may alternatively use this source under the terms of a specific version of
the OpenSpace3D Unrestricted License provided you have obtained such a license from
I-maginer.
-----------------------------------------------------------------------------
*/

#include "CameraInput.h"

CameraInput::CameraInput(int index)
{
  mVI = new videoInput();
  mIndex = index;
  mNumMissed = 0; 

	mMirrorMode = false;
}

bool CameraInput::Initialize()
{
	bool ret = false;
  ret = mVI->setupDevice(mIndex); // crash when problem occured
	
	if(ret)
		mBuffer.resize(mVI->getSize(mIndex));

	return ret;
}

CameraInput::~CameraInput()
{
    mVI->stopDevice(mIndex);
		SAFE_DELETE(mVI);
}

bool CameraInput::IsOpened()
{
	return mVI->isDeviceSetup(mIndex);
}

cv::Mat CameraInput::UpdateImage()
{
	if (!IsOpened())
		throw std::logic_error("Device not initialised");

  //if (!mVI->isFrameNew(mIndex))
	//	return 0;

  // Capture the picture
  if(!mVI->getPixels(mIndex, &mBuffer[0], false, true))
	{
		 ++mNumMissed;
		 if (mNumMissed > 30)
		 {
				//std::cerr << numMissed << " number of bad pictures " << index << std::endl;
				mNumMissed = 0;
		 }
		 throw std::logic_error("Device has bad pixel buffer");
  }
  mNumMissed = 0;

  cv::Mat frame(mVI->getHeight(mIndex), mVI->getWidth(mIndex), CV_8UC3, &mBuffer[0]);
	
	//if (mMirrorMode)	cv::flip(frame,frame,1);

	return frame;
}

int CameraInput::GetWidth()
{
	if (IsOpened())
		return mVI->getWidth(mIndex);
	else
		return 0;
}

int CameraInput::GetHeight()
{
	if (IsOpened())
		return mVI->getHeight(mIndex);
	else
		return 0;
}

void CameraInput::SetSize(int width, int height)
{
	if (IsOpened())
	{
		mVI->stopDevice(mIndex);
		mVI->setupDevice(mIndex, width, height);

		//if the size is not supported we restart with the default size
		if (!IsOpened())
			mVI->setupDevice(mIndex);

    mBuffer.resize(mVI->getSize(mIndex));
	}
}

bool CameraInput::GetMirrorMode()
{
	return mMirrorMode;
}

void CameraInput::SetMirrorMode(bool mode)
{
	mMirrorMode = mode;
}
