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

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $HeadURL$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or https://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "itkResampleImageFilter.h"
#include "itkImageFileWriter.h"
#include "itkPluginUtilities.h"

#include "itkCheckerBoardImageFilter.h"

#include "CheckerBoardFilterCLP.h"

// STD includes
#include <iostream>

// Use an anonymous namespace to keep class types and function names
// from colliding when module is used as shared object module.  Every
// thing should be in an anonymous namespace except for the module
// entry point, e.g. main()
//
namespace
{

template <class T>
int DoIt(int argc, char* argv[], T)
{
  typedef T InputPixelType;
  typedef T OutputPixelType;

  typedef itk::Image<InputPixelType, 3> InputImageType;
  typedef itk::Image<OutputPixelType, 3> OutputImageType;

  typedef itk::ImageFileReader<InputImageType> ReaderType;
  typedef itk::ResampleImageFilter<InputImageType, InputImageType> ResampleType;
  typedef itk::ImageFileWriter<OutputImageType> WriterType;

  PARSE_ARGS;

  typename ReaderType::Pointer reader1 = ReaderType::New();
  typename ReaderType::Pointer reader2 = ReaderType::New();
  typename ResampleType::Pointer resample = ResampleType::New();
  typename WriterType::Pointer writer = WriterType::New();

  reader1->SetFileName(inputVolume1.c_str());
  reader2->SetFileName(inputVolume2.c_str());

  writer->SetFileName(outputVolume.c_str());

  typedef itk::CheckerBoardImageFilter<InputImageType> FilterType;

  typename FilterType::Pointer filter = FilterType::New();
  itk::PluginFilterWatcher watcher(filter, "CheckerBoard Image Filter", CLPProcessInformation);

  typename FilterType::PatternArrayType pattern;

  pattern[0] = checkerPattern[0];
  pattern[1] = checkerPattern[1];
  pattern[2] = checkerPattern[2];

  resample->SetInput(reader2->GetOutput());
  resample->SetReferenceImage(reader1->GetOutput());
  resample->UseReferenceImageOn();

  filter->SetCheckerPattern(pattern);
  filter->SetInput1(reader1->GetOutput());
  filter->SetInput2(resample->GetOutput());

  writer->SetInput(filter->GetOutput());
  writer->Update();

  return EXIT_SUCCESS;
}

} // end of anonymous namespace

int main(int argc, char* argv[])
{

  PARSE_ARGS;

  itk::IOPixelEnum pixelType;
  itk::IOComponentEnum componentType;

  try
  {
    itk::GetImageType(inputVolume1, pixelType, componentType);

    // This filter handles all types

    switch (componentType)
    {
      case itk::IOComponentEnum::UCHAR: return DoIt(argc, argv, static_cast<unsigned char>(0)); break;
      case itk::IOComponentEnum::CHAR: return DoIt(argc, argv, static_cast<char>(0)); break;
      case itk::IOComponentEnum::USHORT: return DoIt(argc, argv, static_cast<unsigned short>(0)); break;
      case itk::IOComponentEnum::SHORT: return DoIt(argc, argv, static_cast<short>(0)); break;
      case itk::IOComponentEnum::UINT: return DoIt(argc, argv, static_cast<unsigned int>(0)); break;
      case itk::IOComponentEnum::INT: return DoIt(argc, argv, static_cast<int>(0)); break;
      case itk::IOComponentEnum::ULONG: return DoIt(argc, argv, static_cast<unsigned long>(0)); break;
      case itk::IOComponentEnum::LONG: return DoIt(argc, argv, static_cast<long>(0)); break;
      case itk::IOComponentEnum::FLOAT: return DoIt(argc, argv, static_cast<float>(0)); break;
      case itk::IOComponentEnum::DOUBLE: return DoIt(argc, argv, static_cast<double>(0)); break;
      case itk::IOComponentEnum::UNKNOWNCOMPONENTTYPE:
      default: std::cout << "unknown component type" << std::endl; break;
    }
  }
  catch (itk::ExceptionObject& excep)
  {
    std::cerr << argv[0] << ": exception caught !" << std::endl;
    std::cerr << excep << std::endl;
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}
