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

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkRImageFileWriter.h
	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

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


#ifndef RIMAGEFILEWRITER_H__
#define RIMAGEFILEWRITER_H__

#include "ritkCommon.h"

#include "ritkRImage.h"
#include "ritkRImageIOBase.h"
#include <itkProcessObject.h>

namespace ritk
{

/**	@ingroup	Common
 *	@class		RImageFileWriter
 *	@author		Jakob Wasza
 *	@brief		Class for writing a sequence of range images
 *
 *	@details
 *	This class allows to write a sequence of range images to a single file. Upon the first call of the Update() method, 
 *	the file will be created (overwriting an exisiting file) and the header followed by the amplitude and range image
 *	data will be written. Subsequent calls to Update() will just append the amplitude and range image at the end of the file 
 *	and update the frame counter until a new filename is set. After setting a new (different) filename the writer will behave 
 *	as before the first call to Update() (overwriting existing files, header,...). Internally, this class uses the RImageIOManager
 *	concept.
 *
 *	@sa	RImageIOManager
 *	@sa	RImageFileSource
 */
template<class TRImage>
class RImageFileWriter : public itk::ProcessObject
{
	public:
	/** @name Typedefs */
	//@{

	/** @name Standard typedefs */
	//@{
	typedef RImageFileWriter				Self;
	typedef itk::ProcessObject				Superclass;
	typedef itk::SmartPointer<Self>			Pointer;
	typedef itk::SmartPointer<const Self>	ConstPointer;
	//@}

	/**	@name The RImage types */
	//@{
	typedef TRImage							RImageType;
	typedef typename RImageType::Pointer		RImagePointer;
	typedef typename RImageType::ConstPointer RImageConstPointer;
	//@}

	//@}

	/// Object creation
	itkNewMacro(Self);

	/// RTTI
	itkTypeMacro(RImageFileWriter, itk::ProcessObject);

	/**	@name Set/get the input image */
	//@{
	void SetInput(const RImageType *input);
	const RImageType* GetInput();
	const RImageType* GetInput(unsigned int idx);
	//@}

	/** @name Set/get the filename */
	//@{
	const char* GetFileName() const;
	void SetFileName(const char* FileName);
	//@}

	/** @brief	Update the writer
	 *
	 *	@details
	 *	The actual work is done here. Will write the image set via SetInput() to the given file. 
	 *	If this is the first call (initial state), the data file will be created (overwriting an exisiting file) 
	 *	and the header is written. Range and amplitude data are appended to the data file.
	 */
	void Update();

	/**	@brief	Save file
	 *
	 *	@details
	 *	A call to this method will flush and close the internal file stream. After calling
	 *	this method, the writer behaves like in initial state.
	 */
	void Save();
	
protected:
	/// Constructor
	RImageFileWriter();
	/// Destructorthe
	~RImageFileWriter();

private:
	/// purposely not implemented
	RImageFileWriter(Self&); 
	/// purposely not implemented
	void operator=(const Self&); 

protected:
	/// The filename
	std::string m_FileName;
	/// Flag that indicates that the filename has changed
	bool m_FileNameDirtyFlag;

	/**	@name The IO object */
	//@{
	typedef RImageIOBase<RImageType>			RImageIOBaseType;
	typedef typename RImageIOBaseType::Pointer	RImageIOBasePointer;
	RImageIOBasePointer m_IOPointer;
	//@}

};


//#include "RImageFileWriter.txx"

/** @name Template instantiations */
//@{
typedef RImageFileWriter<RImageF2> RImageFileWriterF2;
//@}

} // end namespace ritk

#endif // RIMAGEFILEWRITER_H__