/*======================================================================================

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkRImageFileWriter.txx
	Version:		$Revision: 1761 $
	Date:			$Date: 2011-09-23 17:25:41 +0200 (Fr, 23 Sep 2011) $

	Copyright (c) 2010-2011
	Pattern Recognition Lab, 
    University Erlangen-Nuremberg, Germany
    All rights reserved.

	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 RITKCopyright.txt for more information.

	http://www5.informatik.uni-erlangen.de/research/software/ritk

========================================================================================*/

#include "ritkRImageFileWriter.h"
#include <itkImageRegionConstIterator.h>

namespace ritk
{

template<class TRImage>
RImageFileWriter<TRImage>
::RImageFileWriter()
{
	// No default filename
	m_FileName = "";

	// By default true
	m_FileNameDirtyFlag = true;
}


template<class TRImage>
RImageFileWriter<TRImage>
::~RImageFileWriter()
{
	Save();
	m_IOPointer = NULL;
}


//---------------------------------------------------------
template<class TRImage>
void 
RImageFileWriter<TRImage>
::SetInput(const RImageType *input)
{
	// ProcessObject is not const_correct so this cast is required here.
	this->ProcessObject::SetNthInput(0, const_cast<RImageType*>(input ) );
}


//---------------------------------------------------------
template<class TRImage>
const typename RImageFileWriter<TRImage>::RImageType*
RImageFileWriter<TRImage>
::GetInput(void)
{
	if (this->GetNumberOfInputs() < 1)
	{
		return 0;
	}
  
	return static_cast<RImageType*>(this->ProcessObject::GetInput(0));
}


//---------------------------------------------------------
template<class TRImage>
const typename RImageFileWriter<TRImage>::RImageType*
RImageFileWriter<TRImage>
::GetInput(unsigned int idx)
{
	return static_cast<RImageType*>(this->ProcessObject::GetInput(idx));
}


//---------------------------------------------------------
template<class TRImage>
const char*
RImageFileWriter<TRImage>
::GetFileName() const
{
	return m_FileName.c_str();
}


//---------------------------------------------------------
template<class TRImage>
void
RImageFileWriter<TRImage>
::SetFileName(const char *FileName) 
{
	if ( FileName && (FileName == m_FileName ) )
		return;
	if ( !FileName )
	{
		m_FileName = "";
	}
	else
	{
		m_FileName = FileName;
	}

	// Set dirty flags
	m_FileNameDirtyFlag = true;
	this->Modified();
}


//---------------------------------------------------------
template<class TRImage>
void
RImageFileWriter<TRImage>
::Update()
{
	Superclass::Update();

	// Sanity checks
	if ( !GetInput() )
		itkExceptionMacro(<<"No input set!");
	if ( !GetFileName() )
		itkExceptionMacro(<<"No file name set!");

	// If no IO object is set we have to query
	if ( !m_IOPointer )
	{
		// Request the appropriate IO object
		m_IOPointer = RImageIOManager<TRImage>::GetInstance().RequestIOObject(this->GetInput()->GetNameOfClass());

		// Check if we can handle this image
		if ( !m_IOPointer )
		{
			itkExceptionMacro(<<"Could not determine IO mode for <" << this->GetInput()->GetNameOfClass() << ">");
		}

		// Set the filename
		m_IOPointer->SetFileName(m_FileName);
	}

	// Invoke the IO object
	m_IOPointer->WriteFrame(this->GetInput());
}


//---------------------------------------------------------
template<class TRImage>
void
RImageFileWriter<TRImage>
::Save()
{
	if ( m_IOPointer )
	{
		m_IOPointer->Close();
		m_IOPointer = NULL;
		m_FileNameDirtyFlag = true;
		this->Modified();
	}
}



} // end namespace ritk
