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

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkMetaImageIO.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://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.

=========================================================================*/
#ifdef _MSC_VER
#pragma warning ( disable : 4786 )
#endif

#include <string>
#include "itkMetaImageIO.h"
#include "itkExceptionObject.h"

namespace itk
{

MetaImageIO::MetaImageIO()
{
  if(MET_SystemByteOrderMSB())
    m_ByteOrder = BigEndian;
  else
    m_ByteOrder = LittleEndian;
} 



MetaImageIO::~MetaImageIO()
{
}



void MetaImageIO::PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);
  m_MetaImage.PrintInfo();
}


void MetaImageIO::SetDataFileName( const char* filename ) 
{ 
  m_MetaImage.ElementDataFileName( filename );
}

 
// This method will only test if the header looks like a
// MetaImage.  Some code is redundant with ReadImageInformation
// a StateMachine could provide a better implementation
bool MetaImageIO::CanReadFile( const char* filename ) 
{ 

  // First check the extension
  std::string fname = filename;
  if(  fname == "" )
    {
    itkDebugMacro(<<"No filename specified.");
    return false;
    }

  bool extensionFound = false;
  std::string::size_type mhaPos = fname.rfind(".mha");
  if ((mhaPos != std::string::npos)
      && (mhaPos == fname.length() - 4))
    {
    extensionFound = true;
    }

  std::string::size_type mhdPos = fname.rfind(".mhd");
  if ((mhdPos != std::string::npos)
      && (mhdPos == fname.length() - 4))
    {
    extensionFound = true;
    }
  
  if( !extensionFound )
    {
    itkDebugMacro(<<"The filename extension is not recognized");
    return false;
    }

  // Now check the file content
  std::ifstream inputStream;

  inputStream.open( filename, std::ios::in | std::ios::binary );

  if( inputStream.fail() )
    {
    return false;
    }

  char key[8000];

  inputStream >> key;

  if( inputStream.eof() )
    {
    inputStream.close();
    return false;
    }

  if( strcmp(key,"NDims")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"ObjectType")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"TransformType")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"ID")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"ParentID")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"BinaryData")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"Comment")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"AcquisitionDate")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"Modality")==0 ) 
    {
    inputStream.close();
    return true;
    }

  inputStream.close();
  return false;

}
  

void MetaImageIO::ReadImageInformation()
{ 
  if(!m_MetaImage.Read(m_FileName.c_str()), false)
    {
    ExceptionObject exception(__FILE__, __LINE__);
    exception.SetDescription("File cannot be read");
    throw exception;
    }

  if(m_MetaImage.BinaryData())
    {
    this->SetFileType(Binary);
    }
  else
    {
    this->SetFileType(ASCII);
    }

  this->SetNumberOfComponents(m_MetaImage.ElementNumberOfChannels());

  switch(m_MetaImage.ElementType())
    {
    default:
    case MET_OTHER:
    case MET_NONE:
      this->SetPixelType( UNKNOWN );
      this->SetComponentType( UNKNOWN );
      break;
    case MET_CHAR:
    case MET_CHAR_ARRAY:
    case MET_STRING:
    case MET_ASCII_CHAR:<commit_msg>FIX: MetaImage was reading the data twice. Should now speed up the reading significantly.<commit_after>/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkMetaImageIO.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://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.

=========================================================================*/
#ifdef _MSC_VER
#pragma warning ( disable : 4786 )
#endif

#include <string>
#include "itkMetaImageIO.h"
#include "itkExceptionObject.h"

namespace itk
{

MetaImageIO::MetaImageIO()
{
  if(MET_SystemByteOrderMSB())
    m_ByteOrder = BigEndian;
  else
    m_ByteOrder = LittleEndian;
} 



MetaImageIO::~MetaImageIO()
{
}



void MetaImageIO::PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);
  m_MetaImage.PrintInfo();
}


void MetaImageIO::SetDataFileName( const char* filename ) 
{ 
  m_MetaImage.ElementDataFileName( filename );
}

 
// This method will only test if the header looks like a
// MetaImage.  Some code is redundant with ReadImageInformation
// a StateMachine could provide a better implementation
bool MetaImageIO::CanReadFile( const char* filename ) 
{ 

  // First check the extension
  std::string fname = filename;
  if(  fname == "" )
    {
    itkDebugMacro(<<"No filename specified.");
    return false;
    }

  bool extensionFound = false;
  std::string::size_type mhaPos = fname.rfind(".mha");
  if ((mhaPos != std::string::npos)
      && (mhaPos == fname.length() - 4))
    {
    extensionFound = true;
    }

  std::string::size_type mhdPos = fname.rfind(".mhd");
  if ((mhdPos != std::string::npos)
      && (mhdPos == fname.length() - 4))
    {
    extensionFound = true;
    }
  
  if( !extensionFound )
    {
    itkDebugMacro(<<"The filename extension is not recognized");
    return false;
    }

  // Now check the file content
  std::ifstream inputStream;

  inputStream.open( filename, std::ios::in | std::ios::binary );

  if( inputStream.fail() )
    {
    return false;
    }

  char key[8000];

  inputStream >> key;

  if( inputStream.eof() )
    {
    inputStream.close();
    return false;
    }

  if( strcmp(key,"NDims")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"ObjectType")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"TransformType")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"ID")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"ParentID")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"BinaryData")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"Comment")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"AcquisitionDate")==0 ) 
    {
    inputStream.close();
    return true;
    }
  if( strcmp(key,"Modality")==0 ) 
    {
    inputStream.close();
    return true;
    }

  inputStream.close();
  return false;

}
  

void MetaImageIO::ReadImageInformation()
{ 
  if(!m_MetaImage.Read(m_FileName.c_str(), false))
    {
    ExceptionObject exception(__FILE__, __LINE__);
    exception.SetDescription("File cannot be read");
    throw exception;
    }

  if(m_MetaImage.BinaryData())
    {
    this->SetFileType(Binary);
    }
  else
    {
    this->SetFileType(ASCII);
    }

  this->SetNumberOfComponents(m_MetaImage.ElementNumberOfChannels());

  switch(m_MetaImage.ElementType())
    {
    default:
    case MET_OTHER:
    case MET_NONE:
      this->SetPixelType( UNKNOWN );
      this->SetComponentType( UNKNOWN );
      break;
    case MET_CHAR:
    case MET_CHAR_ARRAY:
    case MET_STRING:
    case MET_ASCII_CHAR:
{'fraction_non_alphanumeric': 0.16034949544671426, 'fraction_numerical': 0.004676347526458282, 'mean_word_length': 3.18486096807415, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7455626', 'n_tokens_mistral': 2719, 'n_tokens_neox': 2574, 'n_words': 580}