#include "RangeCUDABilateralImageFilter.h"

#include <itkRescaleIntensityImageFilter.h>


//----------------------------------------------------------------------------
template<class TRImage>
RangeCUDABilateralImageFilter<TRImage>
::RangeCUDABilateralImageFilter()
{
	m_RangeSigmaSpatial = 5;
	m_RangeSigmaRange = 100;
	
	// Set up range filter
	m_RangeFilter = RangeFilterType::New();
	m_RangeFilter->SetSpatialSigma(m_RangeSigmaSpatial);
	m_RangeFilter->SetRangeSigma(m_RangeSigmaRange);
}


//----------------------------------------------------------------------------
template<class TRImage>
RangeCUDABilateralImageFilter<TRImage>
::~RangeCUDABilateralImageFilter()
{
}


//----------------------------------------------------------------------------
template<class TRImage>
void
RangeCUDABilateralImageFilter<TRImage>
::SetRangeSigmaSpatial(float SigmaSpatial)
{
	if ( SigmaSpatial == m_RangeSigmaSpatial )
		return;

	m_RangeSigmaSpatial = SigmaSpatial;
	m_RangeFilter->SetSpatialSigma(m_RangeSigmaSpatial);
	this->Modified();
}


//----------------------------------------------------------------------------
template<class TRImage>
void
RangeCUDABilateralImageFilter<TRImage>
::SetRangeSigmaRange(float SigmaRange)
{
	if ( SigmaRange == m_RangeSigmaRange )
		return;

	m_RangeSigmaRange = SigmaRange;
	m_RangeFilter->SetRangeSigma(m_RangeSigmaRange);
	this->Modified();
}


//----------------------------------------------------------------------------
template<class TRImage>
void
RangeCUDABilateralImageFilter<TRImage>
::SetRangeKernelSize(int size)
{
	if ( size == m_RangeKernelSize )
		return;

	m_RangeKernelSize = size;
	m_RangeFilter->SetKernelSize(size);
	this->Modified();
}


//----------------------------------------------------------------------------
template<class TRImage>
float
RangeCUDABilateralImageFilter<TRImage>
::GetRangeSigmaSpatial()
{
	return m_RangeSigmaSpatial;
}


//----------------------------------------------------------------------------
template<class TRImage>
float
RangeCUDABilateralImageFilter<TRImage>
::GetRangeSigmaRange()
{
	return m_RangeSigmaRange;
}


//----------------------------------------------------------------------------
template<class TRImage>
void
RangeCUDABilateralImageFilter<TRImage>
::DoFiltering()
{
	// Filter range image if requested
	if ( (Superclass::GetFilterMode()&Superclass::RANGE) == Superclass::RANGE )
	{
		m_RangeFilter->SetInput(this->GetInput()->GetRangeImage());		
		//m_RangeFilter->GetOutput()->SetRequestedRegion(this->GetOutput()->GetRequestedRegion());
		m_RangeFilter->UpdateLargestPossibleRegion();
		/*this->GetOutput()->SetRangeImage(m_RangeFilter->GetOutput());
		this->GetOutput()->GetRangeImage()->DisconnectPipeline();*/
		this->GetOutput()->GetRangeImage()->Graft(m_RangeFilter->GetOutput());
	}

	// Filtering for RGBs NOT supported
	if ( (Superclass::GetFilterMode()&Superclass::RGB) == Superclass::RGB )
	{
		itkExceptionMacro(<<"Filtering for RGB images not supported atm!");
		/*m_RGBFilter->SetInput(this->GetInput()->GetRGBImage());		
		m_RGBFilter->GetOutput()->SetRequestedRegion(this->GetOutput()->GetRequestedRegion());
		m_RGBFilter->Update();
		this->GetOutput()->SetRGBImage(m_RGBFilter->GetOutput());
		this->GetOutput()->GetRGBImage()->DisconnectPipeline();*/
	}

	// Filtering for world coord images NOT supported
	if ( (Superclass::GetFilterMode()&Superclass::WORLDCOORD) == Superclass::WORLDCOORD ) 
	{
		itkExceptionMacro(<<"Filtering of world coords not supported!");
	}
}
