#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkTileImageFilter.h"

int main(int argc, char *argv[] )
{
  // Verify arguments
  if (argc < 5)
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << "input1 input2 ... inputn output" << std::endl; //input1 input2 input3 input4 input5 input6 output"
    return EXIT_FAILURE;
    }
 
  // Parse arguments
 /* std::string inputFileName = argv[1];
 std::string input2FileName = argv[2];
  std::string input3FileName = argv[3];
  std::string input4FileName = argv[4];
  std::string input5FileName = argv[5];
  std::string input6FileName = argv[6];
  std::string outputFileName = argv[2]; 
  std::string outputFileName = argv[2]; 

  // Output arguments
 /*std::cout << "inputFileName " << inputFileName << std::endl;
  std::cout << "input2FileName " << input2FileName << std::endl;
  std::cout << "input3FileName " << input3FileName << std::endl;
  std::cout << "input4FileName " << input4FileName << std::endl;
  std::cout << "input5FileName " << input5FileName << std::endl;
  std::cout << "input6FileName " << input6FileName << std::endl;
  std::cout << "outputFileName " << outputFileName << std::endl;
  */ 

  typedef itk::Image< unsigned char, 2>   ImageType;

  // Read images
 /* typedef itk::ImageFileReader< ImageType > ImageReaderType ;
  ImageReaderType::Pointer reader1 = ImageReaderType::New();
  reader1->SetFileName(input1FileName);
  reader1->Update();

  ImageReaderType::Pointer reader2 = ImageReaderType::New();
  reader2->SetFileName(input2FileName);
  reader2->Update();

  ImageReaderType::Pointer reader3 = ImageReaderType::New();
  reader3->SetFileName(input3FileName);
  reader3->Update();

  ImageReaderType::Pointer reader4 = ImageReaderType::New();
  reader4->SetFileName(input4FileName);
  reader4->Update();
  
  ImageReaderType::Pointer reader5 = ImageReaderType::New();
  reader5->SetFileName(input5FileName);
  reader5->Update();

  ImageReaderType::Pointer reader6 = ImageReaderType::New();
  reader6->SetFileName(input6FileName);
  reader6->Update();
  */
  
 

  // Tile the images side-by-side
  typedef itk::TileImageFilter< ImageType, ImageType > TileFilterType;

  TileFilterType::Pointer tileFilter = TileFilterType::New();

  itk::FixedArray< unsigned int, 2 > layout;

  layout[0] = 10;
  layout[1] = 0;
  layout[2] = 0;

  tileFilter->SetLayout( layout );


  unsigned int inputImageNumber = 0;
  
  typedef itk::ImageFileReader< ImageType> ImageReaderType;
  ImageReaderType::Pointer reader = ImageReaderType::New();
  ImageType::Pointer inputImage;

  for (int i = 1;i < argc - 1;  i++) 
    {
    reader->SetFileName( argv[i] );
	//reader->Update();
	inputImage = reader->GetOutput();
    //inputImage->DisconnectPipeline();
	tileFilter->SetInput( inputImageNumber++,inputImage);// inputImageTile   inputImageNumber++,inputImage

  }
  /* 
  tileFilter->SetInput(0, reader->GetOutput());
  tileFilter->SetInput(1, reader2->GetOutput());
  tileFilter->SetInput(2, reader3->GetOutput());
  tileFilter->SetInput(3, reader4->GetOutput());
  tileFilter->SetInput(4, reader5->GetOutput());
  tileFilter->SetInput(5, reader6->GetOutput());
  */
  
  // Set the value of output pixels which are created by mismatched size input images.
  // If the two images are the same height, this will not be used.
  
  unsigned char fillerValue = 128;
  tileFilter->SetDefaultPixelValue( fillerValue );

  tileFilter->Update();

  // Write the output image
  typedef itk::ImageFileWriter< ImageType > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetInput( tileFilter->GetOutput() );
  writer->SetFileName( argv[argc-1] ); //outputFileName argv[argc-1]
  writer->Update();

  return EXIT_SUCCESS;
}
