Guest User

Untitled

a guest
Jun 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. ....
  2.  
  3. void VTKImageIO2::StripSymmetricTensorBinaryBufferSymmetry( const void *fullBuffer,
  4. void *strippedBuffer,
  5. StreamingImageIOBase::SizeType num )
  6. {
  7. std::streamsize bytesRemaining = num;
  8. const SizeType componentSize = this->GetComponentSize();
  9. SizeType pixelSize = componentSize * 6;
  10. char zero[1024];
  11.  
  12. std::memset( zero, 0, 1024 );
  13.  
  14. if( this->GetNumberOfComponents() != 6 )
  15. {
  16. itkExceptionMacro(<< "Unsupported tensor dimension.");
  17. }
  18. while( bytesRemaining )
  19. {
  20. // row 1
  21. std::memcpy( strippedBuffer, fullBuffer, 3 * componentSize );
  22. strippedBuffer = static_cast< char * >(strippedBuffer) + 3 * componentSize;
  23. fullBuffer = static_cast< char * >(fullBuffer) + 3 * componentSize;
  24. // row 2
  25. fullBuffer = static_cast< char * >(fullBuffer) + componentSize;
  26. std::memcpy( strippedBuffer, fullBuffer, 2 * componentSize );
  27. strippedBuffer = static_cast< char * >(strippedBuffer) + 2 * componentSize;
  28. fullBuffer = static_cast< char * >(fullBuffer) + 2 * componentSize;
  29. // row 3
  30. fullBuffer = static_cast< char * >(fullBuffer) + 2 * componentSize;
  31. std::memcpy( strippedBuffer, fullBuffer, componentSize );
  32. strippedBuffer = static_cast< char * >(strippedBuffer) + componentSize;
  33. fullBuffer = static_cast< char * >(fullBuffer) + componentSize;
  34. bytesRemaining -= pixelSize;
  35. }
  36. }
  37.  
  38. void VTKImageIO2::Read(void *buffer)
  39. {
  40. std::ifstream file;
  41.  
  42. if ( this->RequestedToStream() )
  43. {
  44. itkAssertOrThrowMacro(m_FileType != ASCII, "Can not stream with ASCII type files");
  45.  
  46. itkExceptionMacro(<< "Cannot stream read binary second rank tensors.");
  47. }
  48.  
  49. // open and stream read
  50. this->OpenFileForReading( file, this->m_FileName.c_str() );
  51.  
  52. itkAssertOrThrowMacro(this->GetHeaderSize() != 0, "Header size is unknown when it shouldn't be!");
  53.  
  54. if( this->GetPixelType() == ImageIOBase::SYMMETRICSECONDRANKTENSOR )
  55. {
  56. SizeType tempBufferSize = ( this->GetImageSizeInBytes() * 2 * this->GetNumberOfDimensions() ) /
  57. ( this->GetNumberOfDimensions() + 1 );
  58. std::streamoff seekPos = 0;
  59. size_t subDimensionQuantity = 1;
  60. for( unsigned int i = 0; i < this->m_IORegion.GetImageDimension(); ++i )
  61. {
  62. seekPos += static_cast< std::streamoff >( subDimensionQuantity * m_IORegion.GetIndex()[i] );
  63. subDimensionQuantity *= this->GetDimensions( i );
  64. }
  65. seekPos = ( seekPos * 2 * this->GetNumberOfDimensions() ) / ( this->GetNumberOfDimensions() + 1 );
  66. itk::Array< char > tempArray( tempBufferSize );
  67. char* tempBuffer = tempArray.data_block();
  68. this->StreamReadBufferAsBinary( file, tempBuffer, tempBufferSize, seekPos );
  69. this->StripSymmetricTensorBinaryBufferSymmetry( tempBuffer, buffer, tempBufferSize );
  70. }
  71. else
  72. {
  73. this->StreamReadBufferAsBinary( file, buffer );
  74. }
  75. }
  76. else
  77. {
  78. // open the file
  79. this->OpenFileForReading( file, this->m_FileName.c_str() );
  80.  
  81. itkAssertOrThrowMacro(this->GetHeaderSize() != 0, "Header size is unknown when it shouldn't be!");
  82.  
  83. if ( file.fail() )
  84. {
  85. itkExceptionMacro(<< "Failed seeking to data position");
  86. }
  87.  
  88. // seek pass the header
  89. std::streampos dataPos = static_cast< std::streampos >( this->GetHeaderSize() );
  90. file.seekg(dataPos, std::ios::beg);
  91.  
  92. //We are positioned at the data. The data is read depending on whether
  93. //it is ASCII or binary.
  94. if ( m_FileType == ASCII )
  95. {
  96. this->ReadBufferAsASCII( file, buffer, this->GetComponentType(),
  97. this->GetImageSizeInComponents()
  98. );
  99. }
  100. else
  101. {
  102. // read the image
  103. if( this->GetPixelType() == ImageIOBase::SYMMETRICSECONDRANKTENSOR )
  104. {
  105. SizeType tempBufferSize = ( this->GetImageSizeInBytes() * 2 * this->GetNumberOfDimensions() ) /
  106. ( this->GetNumberOfDimensions() + 1 );
  107. itk::Array< char > tempArray( tempBufferSize );
  108. char* tempBuffer = tempArray.data_block();
  109. this->ReadBufferAsBinary( file, tempBuffer, tempBufferSize );
  110. this->StripSymmetricTensorBinaryBufferSymmetry( tempBuffer, buffer, tempBufferSize );
  111. }
  112. else
  113. {
  114. this->ReadBufferAsBinary( file, buffer, this->GetImageSizeInBytes() );
  115. }
  116.  
  117. int size = this->GetComponentSize();
  118. switch ( size )
  119. {
  120. case 1:
  121. break;
  122. case 2:
  123. ByteSwapper< uint16_t >::SwapRangeFromSystemToBigEndian( (uint16_t *)buffer, this->GetImageSizeInComponents() );
  124. break;
  125. case 4:
  126. ByteSwapper< uint32_t >::SwapRangeFromSystemToBigEndian( (uint32_t *)buffer, this->GetImageSizeInComponents() );
  127. break;
  128. case 8:
  129. ByteSwapper< uint64_t >::SwapRangeFromSystemToBigEndian( (uint64_t *)buffer, this->GetImageSizeInComponents() );
  130. break;
  131. default:
  132. itkExceptionMacro(<< "Unknown component size" << size);
  133. }
  134. }
  135. }
  136. }
  137.  
  138. ....
Add Comment
Please, Sign In to add comment