<commit_before>/*=========================================================================

  Program:   Advanced Normalization Tools
  Module:    $RCSfile: itkANTSImageTransformation.cxx,v $
  Language:  C++
  Date:      $Date: 2008/11/15 23:46:06 $
  Version:   $Revision: 1.18 $

  Copyright (c) ConsortiumOfANTS. All rights reserved.
  See accompanying COPYING.txt or
 http://sourceforge.net/projects/advants/files/ANTS/ANTSCopyright.txt 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.

=========================================================================*/
#ifndef _itkANTSImageTransformation_txx_
#define _itkANTSImageTransformation_txx_

// disable debug warnings in MS compiler
#ifdef _MSC_VER
#pragma warning(disable: 4786)
#endif

#include "ANTS_affine_registration2.h"
#include "itkANTSImageTransformation.h"

#include "itkIdentityTransform.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkRecursiveGaussianImageFilter.h"
#include "itkResampleImageFilter.h"
// #include "itkVectorImageFileWriter.h"

#include "vnl/vnl_math.h"

namespace itk
{
template <unsigned int TDimension, class TReal>
ANTSImageTransformation<TDimension, TReal>
::ANTSImageTransformation()
{
  this->m_DeformationField = NULL;
  this->m_InverseDeformationField = NULL;
  this->m_AffineTransform = NULL;
  this->m_WriteComponentImages = false;
  m_DeformationRegionOfInterestSize.Fill(0);
  m_DeformationRegionSpacing.Fill(1);
  m_DeformationRegionOfInterestCenter.Fill(0);
}

template <unsigned int TDimension, class TReal>
void
ANTSImageTransformation<TDimension, TReal>
::Compose()
{
}

template <unsigned int TDimension, class TReal>
void
ANTSImageTransformation<TDimension, TReal>
::Write()
{
  std::cout << " begin writing " << m_NamingConvention << std::endl;

  std::string            filePrefix = this->m_NamingConvention;
  std::string::size_type pos = filePrefix.rfind( "." );
  std::string            extension;
  std::string            gzExtension( "" );

  if( pos != std::string::npos
      && std::string( filePrefix, pos, pos + 2 ) != std::string( "./" ) )
    {
    filePrefix = std::string( filePrefix, 0, pos );
    extension = std::string( this->m_NamingConvention, pos,
                             this->m_NamingConvention.length() - 1 );
    if( extension == std::string( ".gz" ) )
      {
      gzExtension = std::string( ".gz" );
      pos = filePrefix.rfind( "." );
      extension = std::string( filePrefix, pos,
                               filePrefix.length() - 1 );
      filePrefix = std::string( filePrefix, 0, pos );
      }
    }
  else
    {
    extension = std::string( ".nii.gz" );
    }

  // Added by songgang
  if( this->m_AffineTransform )
    {
    std::cout << " writing " << filePrefix << " affine " << std::endl;
    std::string filename = filePrefix + std::string( "Affine.txt" );
    WriteAffineTransformFile(this->m_AffineTransform, filename);
    }

  if( this->m_DeformationField )
    {
    std::cout << " writing " << filePrefix << " def " <<  std::endl;
    if( extension != std::string( ".mha" ) )
      {
      std::string filename = filePrefix + std::string( "Warp" )
        + extension + gzExtension;

      typedef ImageFileWriter<DeformationFieldType> WriterType;
      typename WriterType::Pointer writer = WriterType::New();
      std::cout << "filename " << filename << std::endl;
      writer->SetFileName( filename );<commit_msg>ENH: adding a fix to check relevant file extensions for output types even when multiple .s are in the path <commit_after>/*=========================================================================

  Program:   Advanced Normalization Tools
  Module:    $RCSfile: itkANTSImageTransformation.cxx,v $
  Language:  C++
  Date:      $Date: 2008/11/15 23:46:06 $
  Version:   $Revision: 1.18 $

  Copyright (c) ConsortiumOfANTS. All rights reserved.
  See accompanying COPYING.txt or
 http://sourceforge.net/projects/advants/files/ANTS/ANTSCopyright.txt 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.

=========================================================================*/
#ifndef _itkANTSImageTransformation_txx_
#define _itkANTSImageTransformation_txx_

// disable debug warnings in MS compiler
#ifdef _MSC_VER
#pragma warning(disable: 4786)
#endif

#include "ANTS_affine_registration2.h"
#include "itkANTSImageTransformation.h"

#include "itkIdentityTransform.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkRecursiveGaussianImageFilter.h"
#include "itkResampleImageFilter.h"
// #include "itkVectorImageFileWriter.h"

#include "vnl/vnl_math.h"

namespace itk
{
template <unsigned int TDimension, class TReal>
ANTSImageTransformation<TDimension, TReal>
::ANTSImageTransformation()
{
  this->m_DeformationField = NULL;
  this->m_InverseDeformationField = NULL;
  this->m_AffineTransform = NULL;
  this->m_WriteComponentImages = false;
  m_DeformationRegionOfInterestSize.Fill(0);
  m_DeformationRegionSpacing.Fill(1);
  m_DeformationRegionOfInterestCenter.Fill(0);
}

template <unsigned int TDimension, class TReal>
void
ANTSImageTransformation<TDimension, TReal>
::Compose()
{
}

template <unsigned int TDimension, class TReal>
void
ANTSImageTransformation<TDimension, TReal>
::Write()
{
  std::cout << " begin writing " << m_NamingConvention << std::endl;

  bool                   is_supported = false;
  std::string            filePrefix = this->m_NamingConvention;
  std::string::size_type pos = filePrefix.rfind( "." );
  std::string            extension;
  std::string            gzExtension( "" );

  if( pos != std::string::npos
      && std::string( filePrefix, pos, pos + 2 ) != std::string( "./" ) )
    {
    filePrefix = std::string( filePrefix, 0, pos );
    extension = std::string( this->m_NamingConvention, pos,
                             this->m_NamingConvention.length() - 1 );
    if( extension == std::string( ".gz" ) )
      {
      gzExtension = std::string( ".gz" );
      pos = filePrefix.rfind( "." );
      extension = std::string( filePrefix, pos,
                               filePrefix.length() - 1 );
      filePrefix = std::string( filePrefix, 0, pos );
      }

    // GetSupportedWriteExtensions
    typedef itk::ImageIOBase                           IOBaseType;
    typedef std::list<itk::LightObject::Pointer>       ArrayOfImageIOType;
    typedef typename IOBaseType::ArrayOfExtensionsType ArrayOfExtensionsType;
    ArrayOfImageIOType           allobjects = itk::ObjectFactoryBase::CreateAllInstance("itkImageIOBase");
    ArrayOfImageIOType::iterator itr = allobjects.begin();
    while( itr != allobjects.end() )
      {
      IOBaseType * io = dynamic_cast<IOBaseType *>( itr->GetPointer() );
      if( !io )
        {
        std::cerr << "Got a null pointer in the array" << std::endl;
        }
      else
        {
        const ArrayOfExtensionsType &         writeExtensions = io->GetSupportedWriteExtensions();
        ArrayOfExtensionsType::const_iterator writeItr = writeExtensions.begin();
        while( writeItr != writeExtensions.end() )
          {
          std::string test_ext = *writeItr;
          //	    std::cout <<" compare " << extension << " to " << test_ext << std::endl;
          if( extension == test_ext )
            {
            is_supported = true;
            }
          //	    else std::cout <<" not the same " << std::endl;
          ++writeItr;
          }
        }
      ++itr;
      }

    if( !is_supported )
      {
      std::cout << " WARNING! we are guessing you want nii.gz for your image output instead of: " << extension
                << std::endl;
      filePrefix = this->m_NamingConvention;
      extension = std::string(".nii.gz");
      }
    }
  else
    {
    extension = std::string( ".nii.gz" );
    }
  // Added by songgang
  if( this->m_AffineTransform )
    {
    std::cout << " writing " << filePrefix << " affine " << std::endl;
    std::string filename = filePrefix + std::string( "Affine.txt" );
    WriteAffineTransformFile(this->m_AffineTransform, filename);
    }

  if( this->m_DeformationField )
    {
    std::cout << " writing " << filePrefix << " def " <<  std::endl;
    if( extension != std::string( ".mha" ) )
      {
      std::string filename = filePrefix + std::string( "Warp" )
        + extension + gzExtension;

      typedef ImageFileWriter<DeformationFieldType> WriterType;
      typename WriterType::Pointer writer = WriterType::New();
      std::cout << "filename " << filename << std::endl;
      writer->SetFileName( filename );
{'fraction_non_alphanumeric': 0.15200892857142856, 'fraction_numerical': 0.006696428571428571, 'mean_word_length': 3.3776257938446506, 'pattern_counts': {'":': 0, '<': 99, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4325213', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2643, 'n_words': 599}