/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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
 *
 *         https://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.
 *
 *=========================================================================*/

#include <fstream>
#include "itkImageFileWriter.h"
#include "itkImageFileReader.h"
#include "itkRawImageIO.h"
#include "itkTestingMacros.h"


// Specific ImageIO test

int
itkRawImageIOTest3(int argc, char * argv[])
{
  if (argc < 3)
  {
    std::cerr << "Missing Parameters." << std::endl;
    std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " Output1 Output2" << std::endl;
    return EXIT_FAILURE;
  }

  using ImageType = itk::Image<unsigned short, 2>;
  using PixelType = ImageType::PixelType;

  using ImageIteratorType = itk::ImageRegionIterator<ImageType>;

  using ImageConstIteratorType = itk::ImageRegionConstIterator<ImageType>;

  using RawImageIOType = itk::RawImageIO<PixelType, ImageType::ImageDimension>;


  // Create a source object (in this case a random image generator).
  // The source object is templated on the output type.
  //
  constexpr ImageType::SizeType size{ 517, 293 }; // prime numbers are good bug testers...
  ImageType::RegionType         region{ size };

  auto image = ImageType::New();
  image->SetRegions(region);
  image->Allocate();

  ImageIteratorType ii(image, region);

  PixelType value{};
  ii.GoToBegin();
  while (!ii.IsAtEnd())
  {
    ii.Set(value);
    ++value;
    ++ii;
  }

  auto io = RawImageIOType::New();
  io->SetByteOrderToBigEndian();

  // Write out the image
  const itk::ImageFileWriter<ImageType>::Pointer writer = itk::ImageFileWriter<ImageType>::New();
  writer->SetInput(image);
  writer->SetFileName(argv[1]);
  writer->SetImageIO(io);
  writer->Write();

  // Create a source object (in this case a reader)
  const itk::ImageFileReader<ImageType>::Pointer reader = itk::ImageFileReader<ImageType>::New();
  reader->SetImageIO(io);
  reader->SetFileName(argv[1]);
  reader->Update();

  // Compare pixel by pixel in memory


  ImageConstIteratorType it(reader->GetOutput(), reader->GetOutput()->GetBufferedRegion());

  ImageConstIteratorType ot(image, image->GetBufferedRegion());

  it.GoToBegin();
  ot.GoToBegin();
  while (!it.IsAtEnd())
  {
    const PixelType iv = it.Get();
    const PixelType ov = ot.Get();
    if (iv != ov)
    {
      std::cerr << "Error in read/write of pixel " << it.ComputeIndex() << std::endl;
      std::cerr << "Read value  is : " << iv << std::endl;
      std::cerr << "it should be   : " << ov << std::endl;
      std::cerr << "Test FAILED ! " << std::endl;
      return EXIT_FAILURE;
    }
    ++it;
    ++ot;
  }

  writer->SetInput(reader->GetOutput());
  writer->SetFileName(argv[2]);
  writer->SetInput(reader->GetOutput());
  writer->Write();

  std::cout << "Test PASSED ! " << std::endl;
  return EXIT_SUCCESS;
}
