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

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

  Copyright (c) 2002 Insight 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.

=========================================================================*/
#include <iostream>

#include <itkImage.h>
#include <itkImageFileWriter.h>
#include <itkImageFileReader.h>
#include <itkMetaImageIO.h>

#include <itkImageRegionIterator.h>

int main(int argc, char **argv)
  {
  // Declare the image
  typedef itk::Image<short, 3> ImageType;

  ImageType::SizeType imSize = {{256, 256, 10}};
  ImageType::Pointer im = ImageType::New();
  im->SetRegions(imSize);
  im->Allocate();

  // Set some values in the image
  itk::ImageRegionIterator< ImageType > it(im, 
        im->GetLargestPossibleRegion());

  it.GoToBegin();
  int c = 0;
  while(!it.IsAtEnd())
    {
    it.Set( c++/1000 );
    ++it;
    }

  // Declare a Writer
  typedef itk::ImageFileWriter< ImageType > VolumeWriterType;
  VolumeWriterType::Pointer writer = VolumeWriterType::New();

  // Add the MetaImage format to the writer
  writer->SetImageIO( itk::MetaImageIO::New() );

  // Set the filename
  writer->SetFileName( "test.mha" );

  // Set the image to write
  writer->SetInput( im );

  // Write the image - NOTE: This does not use the itk standard
  //   Update function.   Writers are the exception to that rule
  writer->Write();

  // Make sure the image in memory hasn't been corrupted by writing
  it.GoToBegin();
  c = 0;
  while(!it.IsAtEnd())
    {
    if(it.Get() != c++/1000)
      {
      std::cout << "Memory is " << it.Get() 
                << " expected " << c++/1000 << std::endl;
      }
    ++it;
    }

  // Declare a reader
  typedef itk::ImageFileReader< ImageType > VolumeReaderType;
  VolumeReaderType::Pointer reader = VolumeReaderType::New();

  // Set the name of the file to read
  reader->SetFileName( "test.mha" );

  // Set the image into which the results should be stored
  ImageType::Pointer imOut;
  imOut = reader->GetOutput();
 
  // See if the file can be read - "try" otherwise program will 
  //   mysteriously exit on failure in the Object factory
  try
    {
    reader->Update();
    }
  catch( ... )
    {
    std::cout << "Problems reading file format" << std::endl;
    return 1;
    }

  // See if the data read is valid
  itk::ImageRegionIterator< ImageType > itOut(imOut, 
        imOut->GetLargestPossibleRegion());

  it.GoToBegin();
  itOut.GoToBegin();
  while(!it.IsAtEnd())
    {
    if(it.Get() != itOut.Get())
      {<commit_msg>ENH: Includes demo of writing header and data to different files<commit_after>/*=========================================================================

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

  Copyright (c) 2002 Insight 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.

=========================================================================*/
#include <iostream>

#include <itkImage.h>
#include <itkImageFileWriter.h>
#include <itkImageFileReader.h>
#include <itkMetaImageIO.h>

#include <itkImageRegionIterator.h>

int main(int argc, char **argv)
  {
  // Declare the image
  typedef itk::Image<short, 3> ImageType;

  ImageType::SizeType imSize = {{256, 256, 10}};
  ImageType::Pointer im = ImageType::New();
  im->SetRegions(imSize);
  im->Allocate();

  // Set some values in the image
  itk::ImageRegionIterator< ImageType > it(im, 
        im->GetLargestPossibleRegion());

  it.GoToBegin();
  int c = 0;
  while(!it.IsAtEnd())
    {
    it.Set( c++/1000 );
    ++it;
    }

  // Declare a Writer
  typedef itk::ImageFileWriter< ImageType > VolumeWriterType;
  VolumeWriterType::Pointer writer = VolumeWriterType::New();

  // Add the MetaImage format to the writer
  itk::MetaImageIO::Pointer metaWriter = itk::MetaImageIO::New();
  writer->SetImageIO( metaWriter );

  // Uncomment the next line to write the data to one file.
  // metaWriter->SetDataFileName( "LOCAL" );

  // Set the filename
  writer->SetFileName( "test.mhd" );

  // Set the image to write
  writer->SetInput( im );

  // Write the image - NOTE: This does not use the itk standard
  //   Update function.   Writers are the exception to that rule
  writer->Write();

  // Make sure the image in memory hasn't been corrupted by writing
  it.GoToBegin();
  c = 0;
  while(!it.IsAtEnd())
    {
    if(it.Get() != c++/1000)
      {
      std::cout << "Memory is " << it.Get() 
                << " expected " << c++/1000 << std::endl;
      }
    ++it;
    }

  // Declare a reader
  typedef itk::ImageFileReader< ImageType > VolumeReaderType;
  VolumeReaderType::Pointer reader = VolumeReaderType::New();

  // Set the name of the file to read
  reader->SetFileName( "test.mhd" );

  // Set the image into which the results should be stored
  ImageType::Pointer imOut;
  imOut = reader->GetOutput();
 
  // See if the file can be read - "try" otherwise program will 
  //   mysteriously exit on failure in the Object factory
  try
    {
    reader->Update();
    }
  catch( ... )
    {
    std::cout << "Problems reading file format" << std::endl;
    return 1;
    }

  // See if the data read is valid
  itk::ImageRegionIterator< ImageType > itOut(imOut, 
        imOut->GetLargestPossibleRegion());

  it.GoToBegin();
  itOut.GoToBegin();
  while(!it.IsAtEnd())
    {
    if(it.Get() != itOut.Get())
      {
{'fraction_non_alphanumeric': 0.1685082872928177, 'fraction_numerical': 0.009099772505687357, 'mean_word_length': 3.2448275862068967, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 48, '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': '2353712', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1842, 'n_words': 556}