[Insight-users] new ImageIO.cxx

Martijn van der Bom martijn at isi.uu.nl
Fri Aug 19 08:14:58 EDT 2005


Skipped content of type multipart/alternative-------------- next part --------------
/*=========================================================================

  
  Module:   itkV3DImageIO.h
  By:		ISI Image Registration Group, BOM
  

=========================================================================*/

#include "itkV3DImageIO.h"
#include "itkExceptionObject.h"
#include "itkByteSwapper.h"
#include <iostream>
#include <list>
#include <string>
#include <math.h>

namespace itk
{

	V3DImageIO::V3DImageIO() {
		this->SetNumberOfDimensions(3);
		m_PixelType = SCALAR;
		m_ComponentType = USHORT;
	}

	V3DImageIO::~V3DImageIO() {
	}

bool V3DImageIO::CanReadFile(const char* f) {
  std::string filename = f;
  m_FileName = f;
  m_ComponentType = USHORT;

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

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

void V3DImageIO::readLittleEndian(std::ifstream &is, unsigned int &i) {
	is.read( (char* ) &i, (sizeof(int)));
}

void V3DImageIO::readLittleEndian(std::ifstream &is, double &i) {
	is.read( (char* ) &i, (sizeof(double)));
}

void V3DImageIO::ReadImageInformation() {
  m_Spacing[0] = 1.0; 
  m_Spacing[1] = 1.0;
  m_Spacing[2] = 1.0;
  m_PixelType = SCALAR;
  m_ComponentType = USHORT;
  this->SetNumberOfDimensions(3);
  
  unsigned int nrOfVoxelsx;
  unsigned int nrOfVoxelsy;
  unsigned int nrOfVoxelsz;
  double voxelSizex;
  double voxelSizey;
  double voxelSizez;
  double centerx;
  double centery;
  double centerz;

  const int versionIdLength =40;
  char buffer[versionIdLength];
  char R3[versionIdLength] = "3D-RA R3.1";
  const char R4[versionIdLength] = "3D-RA R4.1";

  std::string filename = m_FileName;
  std::ifstream infile(filename.c_str());
  infile.read(buffer, versionIdLength);

  if(0 == strcmp(R4, buffer)){
	  readLittleEndian(infile,nrOfVoxelsx);
	  readLittleEndian(infile,nrOfVoxelsy);
	  readLittleEndian(infile,nrOfVoxelsz);
			  
	  readLittleEndian(infile,voxelSizex);
	  readLittleEndian(infile,voxelSizey);
	  readLittleEndian(infile,voxelSizez);

	  readLittleEndian(infile,centerx);
	  readLittleEndian(infile,centery);
	  readLittleEndian(infile,centerz);
  }

  m_Dimensions[0] = nrOfVoxelsx;
  m_Dimensions[1] = nrOfVoxelsy;
  m_Dimensions[2] = nrOfVoxelsz;
  m_Spacing[0] = voxelSizex;
  m_Spacing[1] = voxelSizey;
  m_Spacing[2] = voxelSizez;
  m_Origin[0] = centerx - (0.5*(nrOfVoxelsx * voxelSizex));
  m_Origin[1] = centery - (0.5*(nrOfVoxelsy * voxelSizey));
  m_Origin[2] = centerz - (0.5*(nrOfVoxelsz * voxelSizez));
  this->SetByteOrderToLittleEndian();
  this->SetFileTypeToBinary();
  this->ComputeStrides();
  
}

void V3DImageIO::Read(void* buffer) {

	long nrOfVoxels = this->m_Dimensions[0] * this->m_Dimensions[1] * this->m_Dimensions[2];
	long componentSize = sizeof(unsigned short);
	long dataSize = nrOfVoxels * componentSize;
	long headerSize = (6 * ( sizeof ( int ))) + (6 * ( sizeof ( double ))) + 40;
	std::string filename = m_FileName;
	std::ifstream infile(filename.c_str());
  
	bool acceptFile = false;
  
  // check filesize en headersize...
 
		if(infile == NULL){
	      itkExceptionMacro("Error reading image data.");
		}
	infile.seekg(0, std::ios::beg);
	long first = infile.tellg();
	infile.seekg(0,std::ios::end);
	long last = infile.tellg();
	long fileLength = last - first;

		if(fileLength != (dataSize + headerSize)) {
		  itkExceptionMacro("Error reading image data.");
		  
	}	


  /** Set to right position and do the reading. */
  infile.seekg (headerSize, std::ios::beg);

  char * newBuffer = static_cast<char *>(buffer);
  infile.read( newBuffer, this->GetImageSizeInBytes());
  
}

bool V3DImageIO::CanWriteFile( const char * name ) {
  std::string filename = name;

  if (filename == "")
    {
    return false;
    }
  
  std::string::size_type V3DPos = filename.rfind(".v3d");
  if ( (V3DPos != std::string::npos)
       && (V3DPos == filename.length() - 4) )
    {
    return true;
    }

  V3DPos = filename.rfind(".V3D");
  if ( (V3DPos != std::string::npos)
       && (V3DPos == filename.length() - 4) )
    {
    return true;
    }
  else {
	  return false;
  }
}

void V3DImageIO::WriteImageInformation() {
}

void V3DImageIO::Write(const void *buffer) {

  unsigned int nDims = this->GetNumberOfDimensions();

	if(nDims != 3) {
      itkExceptionMacro("V3DImageIO supports 3D only");
      //throw exception;
    }

	if(this->GetComponentType() != USHORT) {
      itkExceptionMacro(<<"V3DImageIO supports unsigned short only");
	  //throw exception;
    }

  std::ofstream outfile;
  outfile.open(m_FileName.c_str(), std::ios::binary | std::ios::out);

	if( m_Ofstream.fail() ) {
      itkExceptionMacro("File cannot be written");
      //throw exception;
	}
	unsigned int tmp = m_Dimensions[0];
	outfile.write( (char *) tmp,sizeof(int));
	tmp = m_Dimensions[1];
	outfile.write( (char *) tmp,sizeof(int));
	tmp = m_Dimensions[2];
	outfile.write( (char *) tmp,sizeof(int));

	double tmp_d = m_Spacing[0];
	outfile.write((char *) &tmp_d ,sizeof(double));
	tmp_d = m_Spacing[1];
	outfile.write((char *) &tmp_d,sizeof(double));
	tmp_d = m_Spacing[2];
	outfile.write( (char *) &tmp_d,sizeof(double));

	tmp_d = (m_Origin[0] + (0.5*(m_Dimensions[0] * m_Spacing[0])));
	outfile.write((char *) &tmp_d,sizeof(double));
	tmp_d = (m_Origin[1] + (0.5*(m_Dimensions[1] * m_Spacing[1])));
	outfile.write((char *) & tmp_d,sizeof(double));
	tmp_d = (m_Origin[2] + (0.5*(m_Dimensions[2] * m_Spacing[2])));
	outfile.write((char *) & tmp_d,sizeof(double));

	tmp = 0;
	outfile.write( (char *) tmp, sizeof(unsigned short) * 8);
	tmp = 100;
	outfile.write( (char *) tmp, sizeof(int));
	outfile.write( (char *) true, sizeof(int));

	

} // end namespace itk


}


More information about the Insight-users mailing list