/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
/*=========================================================================
 *
 *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
 *
 *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
 *
 *  For complete copyright, license and disclaimer of warranty information
 *  please refer to the NOTICE file at the top of the ITK source tree.
 *
 *=========================================================================*/
#ifndef __itkIntensityWindowingImageFilter_hxx
#define __itkIntensityWindowingImageFilter_hxx

#include "itkIntensityWindowingImageFilter.h"
#include "itkMinimumMaximumImageCalculator.h"

namespace itk
{
/**
 *
 */
template< typename TInputImage, typename TOutputImage >
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::IntensityWindowingImageFilter()
{
  m_OutputMaximum   = NumericTraits< OutputPixelType >::max();
  m_OutputMinimum   = NumericTraits< OutputPixelType >::NonpositiveMin();

  m_WindowMaximum   = NumericTraits< InputPixelType >::max();
  m_WindowMinimum   = NumericTraits< InputPixelType >::NonpositiveMin();

  m_Scale = 1.0;
  m_Shift = 0.0;
}

/**
 *
 */
template< typename TInputImage, typename TOutputImage >
void
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::SetWindowLevel(const InputPixelType & window, const InputPixelType & level)
{
  typedef typename NumericTraits< InputPixelType >::RealType InputRealType;
  InputRealType tmp1, tmp2;

  tmp1 = static_cast< InputRealType >( level )
         - ( static_cast< InputRealType >( window ) / 2.0 );
  if ( tmp1 < NumericTraits< InputPixelType >::NonpositiveMin() )
    {
    tmp1 = 0.0;
    }

  tmp2 = static_cast< InputRealType >( level )
         + ( static_cast< InputRealType >( window ) / 2.0 );
  if ( tmp2 > NumericTraits< InputPixelType >::max() )
    {
    tmp2 = NumericTraits< InputPixelType >::max();
    }

  m_WindowMinimum = static_cast< InputPixelType >( tmp1 );
  m_WindowMaximum = static_cast< InputPixelType >( tmp2 );
}

/**
 *
 */
template< typename TInputImage, typename TOutputImage >
typename IntensityWindowingImageFilter< TInputImage, TOutputImage >::InputPixelType
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::GetWindow() const
{
  return m_WindowMaximum - m_WindowMinimum;
}

/**
 *
 */
template< typename TInputImage, typename TOutputImage >
typename IntensityWindowingImageFilter< TInputImage, TOutputImage >::InputPixelType
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::GetLevel() const
{
  return ( m_WindowMaximum + m_WindowMinimum ) / 2;
}

/**
 *
 */
template< typename TInputImage, typename TOutputImage >
void
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);

  os << indent << "Output Minimum: "
     << static_cast< typename NumericTraits< OutputPixelType >::PrintType >( m_OutputMinimum )
     << std::endl;
  os << indent << "Output Maximum: "
     << static_cast< typename NumericTraits< OutputPixelType >::PrintType >( m_OutputMaximum )
     << std::endl;
  os << indent << "Window Minimum: "
     << static_cast< typename NumericTraits< InputPixelType >::PrintType >( m_WindowMinimum )
     << std::endl;
  os << indent << "Window Maximum: "
     << static_cast< typename NumericTraits< InputPixelType >::PrintType >( m_WindowMaximum )
     << std::endl;
  os << indent << "Scale Factor: "
     << static_cast< typename NumericTraits< RealType >::PrintType >( m_Scale )
     << std::endl;
  os << indent << "Shift offset: "
     << static_cast< typename NumericTraits< RealType >::PrintType >( m_Shift )
     << std::endl;
}

/**
 *
 */
template< typename TInputImage, typename TOutputImage >
void
IntensityWindowingImageFilter< TInputImage, TOutputImage >
::BeforeThreadedGenerateData()
{
  m_Scale =
    ( static_cast< RealType >( m_OutputMaximum )
      - static_cast< RealType >( m_OutputMinimum ) )
    / ( static_cast< RealType >( m_WindowMaximum )
        - static_cast< RealType >( m_WindowMinimum ) );

  m_Shift =
    static_cast< RealType >( m_OutputMinimum )
    - static_cast< RealType >( m_WindowMinimum ) * m_Scale;

  // set up the functor values
  this->GetFunctor().SetOutputMinimum(m_OutputMinimum);
  this->GetFunctor().SetOutputMaximum(m_OutputMaximum);

  this->GetFunctor().SetWindowMinimum(m_WindowMinimum);
  this->GetFunctor().SetWindowMaximum(m_WindowMaximum);

  this->GetFunctor().SetFactor(m_Scale);
  this->GetFunctor().SetOffset(m_Shift);
}
} // end namespace itk

#endif
