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

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkRImageFileSource.h
	Version:		$Revision: 1761 $
	Date:			$Date: 2011-09-23 17:25:41 +0200 (Fri, 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 RIMAGEFILERSOURCE_H__
#define RIMAGEFILERSOURCE_H__

#include "ritkCommon.h"

#include "ritkRImage.h"
#include "ritkRImageSource.h"
#include "ritkRImageIOBase.h"


namespace ritk
{

/**	@ingroup	Common
  *	@class		RImageFileSource
  *	@author		Jakob Wasza
  * @brief		Class to read range images from a file
  *
  * @details
  *	Allows to read range images from a file. This class is intended to be used as the first element 
  *	in a filter pipeline, loading of a range image frame from the file is triggered by the Update() method, 
  *	the resulting range image can be obtained be the GetOutput() method. A frame index to be read 
  *	can be set by the SetFrameIndex() method, by default the index is set to the first frame in the 
  *	data set (index 0). The total number of frames available can be queried by GetNumberOfFrames(). \n
  *	Internally, this class uses the RImageIOManager concept: the header of a file to be read HAS TO
  *	contain a string identifier for the actual range image type (the base is simply RImage). This class
  *	the queries the corresponding IO object from the RImageIOManager. This IO object is then responsible
  *	for reading frames. This IO concept allows custom range image types that can be processed (as long as
  *	a valid IO object for this type is provided...) by the basic implementation of the range image pipeline.
  *	
  *	@sa RImageIOBase
  *	@sa	RImageIOManager
  */
template<class TRImage>
class RImageFileSource : public RImageSource<TRImage>
{
public:
	/** @name Typedefs */
	//@{

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

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

	//@}

	/// Object creation
	itkNewMacro(Self);

	/// RTTI
	itkTypeMacro(RImageFileSource, RImageSource);

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

	/** @name Set/get the frame index and available number of frames*/
	//@{
	void SetFrameIndex(unsigned int id);
	unsigned int GetFrameIndex() const;
	unsigned int GetNumberOfFrames();
	//@}

	/// Get the timestamps of all frames
	const std::vector<typename RImageType::TimeStampType> &GetTimeStamps() const;

	/// Get the image resolution
	void GetResolution(unsigned int *SX, unsigned int *SY);


protected:
	/// Constructor
	RImageFileSource();
	/// Destructorthe
	virtual ~RImageFileSource();

	/**	@brief	Create the range image from a file
	 *
	 *	The actual work is done here
	 */
	virtual void GenerateRImage();

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

protected:
	/// The file name
	std::string m_FileName;
	/// Flag that indicates that the filename has changed
	bool m_FileNameDirtyFlag;
	/// The ID of the frame
	unsigned int m_FrameID;

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


/** @name Template instantiations */
//@{
typedef RImageFileSource<RImageF2> RImageFileSourceF2;
//@}

//#include "RImageFileSource.txx"


} // end namespace ritk


#endif // RIMAGEFILERSOURCE_H__
