Created
October 27, 2010 17:24
-
-
Save thewtex/649517 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.... | |
void VTKImageIO2::StripSymmetricTensorBinaryBufferSymmetry( const void *fullBuffer, | |
void *strippedBuffer, | |
StreamingImageIOBase::SizeType num ) | |
{ | |
std::streamsize bytesRemaining = num; | |
const SizeType componentSize = this->GetComponentSize(); | |
SizeType pixelSize = componentSize * 6; | |
char zero[1024]; | |
std::memset( zero, 0, 1024 ); | |
if( this->GetNumberOfComponents() != 6 ) | |
{ | |
itkExceptionMacro(<< "Unsupported tensor dimension."); | |
} | |
while( bytesRemaining ) | |
{ | |
// row 1 | |
std::memcpy( strippedBuffer, fullBuffer, 3 * componentSize ); | |
strippedBuffer = static_cast< char * >(strippedBuffer) + 3 * componentSize; | |
fullBuffer = static_cast< char * >(fullBuffer) + 3 * componentSize; | |
// row 2 | |
fullBuffer = static_cast< char * >(fullBuffer) + componentSize; | |
std::memcpy( strippedBuffer, fullBuffer, 2 * componentSize ); | |
strippedBuffer = static_cast< char * >(strippedBuffer) + 2 * componentSize; | |
fullBuffer = static_cast< char * >(fullBuffer) + 2 * componentSize; | |
// row 3 | |
fullBuffer = static_cast< char * >(fullBuffer) + 2 * componentSize; | |
std::memcpy( strippedBuffer, fullBuffer, componentSize ); | |
strippedBuffer = static_cast< char * >(strippedBuffer) + componentSize; | |
fullBuffer = static_cast< char * >(fullBuffer) + componentSize; | |
bytesRemaining -= pixelSize; | |
} | |
} | |
void VTKImageIO2::Read(void *buffer) | |
{ | |
std::ifstream file; | |
if ( this->RequestedToStream() ) | |
{ | |
itkAssertOrThrowMacro(m_FileType != ASCII, "Can not stream with ASCII type files"); | |
itkExceptionMacro(<< "Cannot stream read binary second rank tensors."); | |
} | |
// open and stream read | |
this->OpenFileForReading( file, this->m_FileName.c_str() ); | |
itkAssertOrThrowMacro(this->GetHeaderSize() != 0, "Header size is unknown when it shouldn't be!"); | |
if( this->GetPixelType() == ImageIOBase::SYMMETRICSECONDRANKTENSOR ) | |
{ | |
SizeType tempBufferSize = ( this->GetImageSizeInBytes() * 2 * this->GetNumberOfDimensions() ) / | |
( this->GetNumberOfDimensions() + 1 ); | |
std::streamoff seekPos = 0; | |
size_t subDimensionQuantity = 1; | |
for( unsigned int i = 0; i < this->m_IORegion.GetImageDimension(); ++i ) | |
{ | |
seekPos += static_cast< std::streamoff >( subDimensionQuantity * m_IORegion.GetIndex()[i] ); | |
subDimensionQuantity *= this->GetDimensions( i ); | |
} | |
seekPos = ( seekPos * 2 * this->GetNumberOfDimensions() ) / ( this->GetNumberOfDimensions() + 1 ); | |
itk::Array< char > tempArray( tempBufferSize ); | |
char* tempBuffer = tempArray.data_block(); | |
this->StreamReadBufferAsBinary( file, tempBuffer, tempBufferSize, seekPos ); | |
this->StripSymmetricTensorBinaryBufferSymmetry( tempBuffer, buffer, tempBufferSize ); | |
} | |
else | |
{ | |
this->StreamReadBufferAsBinary( file, buffer ); | |
} | |
} | |
else | |
{ | |
// open the file | |
this->OpenFileForReading( file, this->m_FileName.c_str() ); | |
itkAssertOrThrowMacro(this->GetHeaderSize() != 0, "Header size is unknown when it shouldn't be!"); | |
if ( file.fail() ) | |
{ | |
itkExceptionMacro(<< "Failed seeking to data position"); | |
} | |
// seek pass the header | |
std::streampos dataPos = static_cast< std::streampos >( this->GetHeaderSize() ); | |
file.seekg(dataPos, std::ios::beg); | |
//We are positioned at the data. The data is read depending on whether | |
//it is ASCII or binary. | |
if ( m_FileType == ASCII ) | |
{ | |
this->ReadBufferAsASCII( file, buffer, this->GetComponentType(), | |
this->GetImageSizeInComponents() | |
); | |
} | |
else | |
{ | |
// read the image | |
if( this->GetPixelType() == ImageIOBase::SYMMETRICSECONDRANKTENSOR ) | |
{ | |
SizeType tempBufferSize = ( this->GetImageSizeInBytes() * 2 * this->GetNumberOfDimensions() ) / | |
( this->GetNumberOfDimensions() + 1 ); | |
itk::Array< char > tempArray( tempBufferSize ); | |
char* tempBuffer = tempArray.data_block(); | |
this->ReadBufferAsBinary( file, tempBuffer, tempBufferSize ); | |
this->StripSymmetricTensorBinaryBufferSymmetry( tempBuffer, buffer, tempBufferSize ); | |
} | |
else | |
{ | |
this->ReadBufferAsBinary( file, buffer, this->GetImageSizeInBytes() ); | |
} | |
int size = this->GetComponentSize(); | |
switch ( size ) | |
{ | |
case 1: | |
break; | |
case 2: | |
ByteSwapper< uint16_t >::SwapRangeFromSystemToBigEndian( (uint16_t *)buffer, this->GetImageSizeInComponents() ); | |
break; | |
case 4: | |
ByteSwapper< uint32_t >::SwapRangeFromSystemToBigEndian( (uint32_t *)buffer, this->GetImageSizeInComponents() ); | |
break; | |
case 8: | |
ByteSwapper< uint64_t >::SwapRangeFromSystemToBigEndian( (uint64_t *)buffer, this->GetImageSizeInComponents() ); | |
break; | |
default: | |
itkExceptionMacro(<< "Unknown component size" << size); | |
} | |
} | |
} | |
} | |
.... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment