/*
 * Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * 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
 *
 * 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.
 */


/* Example usage:
./NeighborhoodIterators4 Input/QB_Suburb.png Output/NeighborhoodIterators4a.png 0
*/

/* Example usage:
./NeighborhoodIterators4 Input/QB_Suburb.png Output/NeighborhoodIterators4b.png 1
*/

/* Example usage:
./NeighborhoodIterators4 Input/QB_Suburb.png Output/NeighborhoodIterators4c.png 2
*/

/* Example usage:
./NeighborhoodIterators4 Input/QB_Suburb.png Output/NeighborhoodIterators4d.png 5
*/


#include "otbImage.h"
#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
#include "itkUnaryFunctorImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkConstNeighborhoodIterator.h"
#include "itkImageRegionIterator.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkNeighborhoodInnerProduct.h"

// We now introduce a variation on convolution filtering that is useful when a
// convolution kernel is separable.  In this example, we create a different
// neighborhood iterator for each axial direction of the image and then take
// separate inner products with a 1D discrete Gaussian kernel.
// The idea of using several neighborhood iterators at once has applications
// beyond convolution filtering and may improve efficiency when the size of
// the whole neighborhood relative to the portion of the neighborhood used
// in calculations becomes large.
//
// The only new class necessary for this example is the Gaussian operator.

#include "itkGaussianOperator.h"

int main(int argc, char* argv[])
{
  if (argc < 4)
  {
    std::cerr << "Missing parameters. " << std::endl;
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " inputImageFile outputImageFile sigma" << std::endl;
    return -1;
  }

  using PixelType  = float;
  using ImageType  = otb::Image<PixelType, 2>;
  using ReaderType = otb::ImageFileReader<ImageType>;

  using NeighborhoodIteratorType = itk::ConstNeighborhoodIterator<ImageType>;
  using IteratorType             = itk::ImageRegionIterator<ImageType>;

  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(argv[1]);
  try
  {
    reader->Update();
  }
  catch (itk::ExceptionObject& err)
  {
    std::cout << "ExceptionObject caught !" << std::endl;
    std::cout << err << std::endl;
    return -1;
  }

  ImageType::Pointer output = ImageType::New();
  output->SetRegions(reader->GetOutput()->GetRequestedRegion());
  output->Allocate();

  itk::NeighborhoodInnerProduct<ImageType> innerProduct;

  using FaceCalculatorType = itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<ImageType>;

  FaceCalculatorType                         faceCalculator;
  FaceCalculatorType::FaceListType           faceList;
  FaceCalculatorType::FaceListType::iterator fit;

  IteratorType             out;
  NeighborhoodIteratorType it;

  // The Gaussian operator, like the Sobel operator, is instantiated with a pixel
  // type and a dimensionality.  Additionally, we set the variance of the
  // Gaussian, which has been read from the command line as standard deviation.

  itk::GaussianOperator<PixelType, 2> gaussianOperator;
  gaussianOperator.SetVariance(::atof(argv[3]) * ::atof(argv[3]));

  // The only further changes from the previous example are in the main loop.
  // Once again we use the results from face calculator to construct a loop that
  // processes boundary and non-boundary image regions separately.  Separable
  // convolution, however, requires an additional, outer loop over all the image
  // dimensions.  The direction of the Gaussian operator is reset at each
  // iteration of the outer loop using the new dimension.  The iterators change
  // direction to match because they are initialized with the radius of the
  // Gaussian operator.
  //
  // Input and output buffers are swapped at each iteration so that the output of
  // the previous iteration becomes the input for the current iteration. The swap
  // is not performed on the last iteration.

  ImageType::Pointer input = reader->GetOutput();
  for (unsigned int i = 0; i < ImageType::ImageDimension; ++i)
  {
    gaussianOperator.SetDirection(i);
    gaussianOperator.CreateDirectional();

    faceList = faceCalculator(input, output->GetRequestedRegion(), gaussianOperator.GetRadius());

    for (fit = faceList.begin(); fit != faceList.end(); ++fit)
    {
      it = NeighborhoodIteratorType(gaussianOperator.GetRadius(), input, *fit);

      out = IteratorType(output, *fit);

      for (it.GoToBegin(), out.GoToBegin(); !it.IsAtEnd(); ++it, ++out)
      {
        out.Set(innerProduct(it, gaussianOperator));
      }
    }

    // Swap the input and output buffers
    if (i != ImageType::ImageDimension - 1)
    {
      ImageType::Pointer tmp = input;
      input                  = output;
      output                 = tmp;
    }
  }

  // The output is rescaled and written as in the previous examples.
  // Figure~\ref{fig:NeighborhoodExample4} shows the results of Gaussian blurring
  // the image \code{Examples/Data/QB\_Suburb.png} using increasing
  // kernel widths.
  //
  // \begin{figure}
  // \centering
  // \includegraphics[width=0.23\textwidth]{NeighborhoodIterators4a.eps}
  // \includegraphics[width=0.23\textwidth]{NeighborhoodIterators4b.eps}
  // \includegraphics[width=0.23\textwidth]{NeighborhoodIterators4c.eps}
  // \includegraphics[width=0.23\textwidth]{NeighborhoodIterators4d.eps}
  // \itkcaption[Gaussian blurring by convolution filtering]{Results of
  // convolution filtering with a Gaussian kernel of increasing standard
  // deviation $\sigma$ (from left to right, $\sigma = 0$, $\sigma = 1$, $\sigma
  // = 2$, $\sigma = 5$).  Increased blurring reduces contrast and changes the
  // average intensity value of the image, which causes the image to appear
  // brighter when rescaled.}
  // \protect\label{fig:NeighborhoodExample4}
  // \end{figure}

  using WritePixelType = unsigned char;
  using WriteImageType = otb::Image<WritePixelType, 2>;
  using WriterType     = otb::ImageFileWriter<WriteImageType>;

  using RescaleFilterType = itk::RescaleIntensityImageFilter<ImageType, WriteImageType>;

  RescaleFilterType::Pointer rescaler = RescaleFilterType::New();

  rescaler->SetOutputMinimum(0);
  rescaler->SetOutputMaximum(255);
  rescaler->SetInput(output);

  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(argv[2]);
  writer->SetInput(rescaler->GetOutput());
  try
  {
    writer->Update();
  }
  catch (itk::ExceptionObject& err)
  {
    std::cout << "ExceptionObject caught !" << std::endl;
    std::cout << err << std::endl;
    return -1;
  }

  return EXIT_SUCCESS;
}
