18 #pragma warning ( disable : 4786 )
29 #include <itksys/SystemTools.hxx>
38 if(MET_SystemByteOrderMSB())
78 std::string fname = filename;
81 itkDebugMacro(<<
"No filename specified.");
93 itkExceptionMacro(
"File cannot be read: "
97 << itksys::SystemTools::GetLastSystemError());
133 case MET_UCHAR_ARRAY:
141 case MET_SHORT_ARRAY:
149 case MET_USHORT_ARRAY:
155 if(
sizeof(
int) == MET_ValueTypeSize[MET_INT])
159 else if(
sizeof(
long) == MET_ValueTypeSize[MET_INT])
166 if(
sizeof(
int) == MET_ValueTypeSize[MET_INT])
170 else if(
sizeof(
long) == MET_ValueTypeSize[MET_INT])
177 if(
sizeof(
unsigned int) == MET_ValueTypeSize[MET_UINT])
181 else if(
sizeof(
unsigned long) == MET_ValueTypeSize[MET_UINT])
188 if(
sizeof(
int) == MET_ValueTypeSize[MET_INT])
192 else if(
sizeof(
long) == MET_ValueTypeSize[MET_INT])
199 if(
sizeof(
long) == MET_ValueTypeSize[MET_LONG])
203 else if(
sizeof(
int) == MET_ValueTypeSize[MET_LONG])
210 if(
sizeof(
long) == MET_ValueTypeSize[MET_LONG])
214 else if(
sizeof(
int) == MET_ValueTypeSize[MET_LONG])
221 if(
sizeof(
unsigned long) == MET_ValueTypeSize[MET_ULONG])
225 else if(
sizeof(
unsigned int) == MET_ValueTypeSize[MET_ULONG])
230 case MET_ULONG_ARRAY:
232 if(
sizeof(
unsigned long) == MET_ValueTypeSize[MET_ULONG])
236 else if(
sizeof(
unsigned int) == MET_ValueTypeSize[MET_ULONG])
243 if(
sizeof(
long) == MET_ValueTypeSize[MET_LONG_LONG])
247 else if(
sizeof(
int) == MET_ValueTypeSize[MET_LONG_LONG])
256 case MET_LONG_LONG_ARRAY:
258 if(
sizeof(
long) == MET_ValueTypeSize[MET_LONG_LONG])
262 else if(
sizeof(
int) == MET_ValueTypeSize[MET_LONG_LONG])
273 if(
sizeof(
unsigned long) == MET_ValueTypeSize[MET_ULONG_LONG])
277 else if(
sizeof(
unsigned int) == MET_ValueTypeSize[MET_ULONG_LONG])
286 case MET_ULONG_LONG_ARRAY:
288 if(
sizeof(
unsigned long) == MET_ValueTypeSize[MET_ULONG_LONG])
292 else if(
sizeof(
unsigned int) == MET_ValueTypeSize[MET_ULONG_LONG])
303 if(
sizeof(
float) == MET_ValueTypeSize[MET_FLOAT])
307 else if(
sizeof(
double) == MET_ValueTypeSize[MET_FLOAT])
312 case MET_FLOAT_ARRAY:
314 if(
sizeof(
float) == MET_ValueTypeSize[MET_FLOAT])
318 else if(
sizeof(
double) == MET_ValueTypeSize[MET_FLOAT])
326 if(
sizeof(
double) == MET_ValueTypeSize[MET_DOUBLE])
330 else if(
sizeof(
float) == MET_ValueTypeSize[MET_DOUBLE])
335 case MET_DOUBLE_ARRAY:
337 if(
sizeof(
double) == MET_ValueTypeSize[MET_DOUBLE])
341 else if(
sizeof(
float) == MET_ValueTypeSize[MET_DOUBLE])
346 case MET_FLOAT_MATRIX:
348 if(
sizeof(
float) == MET_ValueTypeSize[MET_FLOAT])
352 else if(
sizeof(
double) == MET_ValueTypeSize[MET_FLOAT])
384 #if defined(ITKIO_DEPRECATED_METADATA_ORIENTATION)
393 if(m_NumberOfDimensions == 3)
397 bool coordUndefined =
false;
400 case MET_ORIENTATION_UNKNOWN:
403 coordUndefined =
true;
406 case MET_ORIENTATION_RL:
410 case MET_ORIENTATION_UNKNOWN:
413 coordUndefined =
true;
416 case MET_ORIENTATION_PA:
418 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_IS)
430 case MET_ORIENTATION_AP:
432 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_IS)
444 case MET_ORIENTATION_IS:
446 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_PA)
458 case MET_ORIENTATION_SI:
460 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_PA)
475 case MET_ORIENTATION_LR:
479 case MET_ORIENTATION_UNKNOWN:
482 coordUndefined =
true;
485 case MET_ORIENTATION_PA:
487 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_IS)
499 case MET_ORIENTATION_AP:
501 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_IS)
513 case MET_ORIENTATION_IS:
515 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_PA)
527 case MET_ORIENTATION_SI:
529 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_PA)
544 case MET_ORIENTATION_PA:
548 case MET_ORIENTATION_UNKNOWN:
551 coordUndefined =
true;
554 case MET_ORIENTATION_RL:
556 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_IS)
568 case MET_ORIENTATION_LR:
570 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_IS)
582 case MET_ORIENTATION_IS:
584 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_RL)
596 case MET_ORIENTATION_SI:
598 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_RL)
613 case MET_ORIENTATION_AP:
617 case MET_ORIENTATION_UNKNOWN:
620 coordUndefined =
true;
623 case MET_ORIENTATION_RL:
625 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_IS)
637 case MET_ORIENTATION_LR:
639 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_IS)
651 case MET_ORIENTATION_IS:
653 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_RL)
665 case MET_ORIENTATION_SI:
667 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_RL)
682 case MET_ORIENTATION_IS:
686 case MET_ORIENTATION_UNKNOWN:
689 coordUndefined =
true;
692 case MET_ORIENTATION_RL:
694 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_PA)
706 case MET_ORIENTATION_LR:
708 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_PA)
720 case MET_ORIENTATION_PA:
722 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_RL)
734 case MET_ORIENTATION_AP:
736 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_RL)
751 case MET_ORIENTATION_SI:
755 case MET_ORIENTATION_UNKNOWN:
758 coordUndefined =
true;
761 case MET_ORIENTATION_RL:
763 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_PA)
775 case MET_ORIENTATION_LR:
777 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_PA)
789 case MET_ORIENTATION_PA:
791 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_RL)
803 case MET_ORIENTATION_AP:
805 if(
m_MetaImage.AnatomicalOrientation(2) == MET_ORIENTATION_RL)
821 #if defined(ITKIO_DEPRECATED_METADATA_ORIENTATION)
824 EncapsulateMetaData<SpatialOrientation::ValidCoordinateOrientationFlags>(
833 const double *transformMatrix =
m_MetaImage.TransformMatrix();
851 if (
m_MetaImage.DistanceUnits() != MET_DISTANCE_UNITS_UNKNOWN )
853 EncapsulateMetaData<std::string>(
859 EncapsulateMetaData<std::string>(
874 for(
unsigned int i=0; i<nDims; i++)
883 int* indexMin =
new int[nDims];
884 int* indexMax =
new int[nDims];
885 for(
unsigned int i=0;i<nDims;i++)
906 itkExceptionMacro(
"File cannot be read: "
910 << itksys::SystemTools::GetLastSystemError());
924 itkExceptionMacro(
"File cannot be read: "
928 << itksys::SystemTools::GetLastSystemError());
945 std::string filename = name;
951 std::string::size_type mhaPos = filename.rfind(
".mha");
952 if ((mhaPos != std::string::npos)
953 && (mhaPos == filename.length() - 4))
958 std::string::size_type mhdPos = filename.rfind(
".mhd");
959 if ((mhdPos != std::string::npos)
960 && (mhdPos == filename.length() - 4))
974 std::string metaDataStr;
977 if ( ExposeMetaData<std::string>( metaDict,
ITK_VoxelUnits, metaDataStr ) )
980 if ( metaDataStr ==
"um. " )
982 m_MetaImage.DistanceUnits( MET_DISTANCE_UNITS_UM );
984 else if ( metaDataStr ==
"mm. " )
986 m_MetaImage.DistanceUnits( MET_DISTANCE_UNITS_MM );
988 else if ( metaDataStr ==
"cm. " )
990 m_MetaImage.DistanceUnits( MET_DISTANCE_UNITS_MM );
994 m_MetaImage.DistanceUnits( metaDataStr.c_str() );
1000 m_MetaImage.AcquisitionDate( metaDataStr.c_str() );
1012 unsigned int nDims = this->GetNumberOfDimensions();
1014 bool binaryData =
true;
1015 if(this->GetFileType() == ASCII)
1020 int nChannels = this->GetNumberOfComponents();
1022 MET_ValueEnumType eType = MET_OTHER;
1023 switch(m_ComponentType)
1026 case UNKNOWNCOMPONENTTYPE:
1042 if(
sizeof(
long) == MET_ValueTypeSize[MET_LONG])
1046 else if(
sizeof(
long) == MET_ValueTypeSize[MET_INT])
1050 else if(
sizeof(
long) == MET_ValueTypeSize[MET_LONG_LONG])
1052 eType = MET_LONG_LONG;
1056 if(
sizeof(
long) == MET_ValueTypeSize[MET_LONG])
1060 else if(
sizeof(
long) == MET_ValueTypeSize[MET_INT])
1064 else if(
sizeof(
long) == MET_ValueTypeSize[MET_LONG_LONG])
1066 eType = MET_ULONG_LONG;
1071 if(
sizeof(
int) == MET_ValueTypeSize[MET_INT])
1075 else if(
sizeof(
int) == MET_ValueTypeSize[MET_LONG])
1081 if(
sizeof(
int) == MET_ValueTypeSize[MET_INT])
1085 else if(
sizeof(
int) == MET_ValueTypeSize[MET_LONG])
1091 if(
sizeof(
float) == MET_ValueTypeSize[MET_FLOAT])
1095 else if(
sizeof(
float) == MET_ValueTypeSize[MET_DOUBLE])
1101 if(
sizeof(
double) == MET_ValueTypeSize[MET_DOUBLE])
1105 else if(
sizeof(
double) == MET_ValueTypeSize[MET_FLOAT])
1113 int * dSize =
new int[nDims];
1114 float * eSpacing =
new float[nDims];
1115 double * eOrigin =
new double[nDims];
1116 for(i=0; i<nDims; i++)
1118 dSize[i] = this->GetDimensions(i);
1119 eSpacing[i] =
static_cast<float>(this->GetSpacing(i));
1120 eOrigin[i] = this->GetOrigin(i);
1123 m_MetaImage.InitializeEssential(nDims, dSize, eSpacing, eType, nChannels,
1124 const_cast<void *>(buffer));
1125 m_MetaImage.Position(eOrigin);
1126 m_MetaImage.BinaryData(binaryData);
1129 this->WriteImageInformation();
1133 #if defined(ITKIO_DEPRECATED_METADATA_ORIENTATION)
1138 #if defined(ITKIO_DEPRECATED_METADATA_ORIENTATION)
1144 std::vector<double> dirx, diry, dirz;
1146 dirx = this->GetDirection(0);
1147 diry = this->GetDirection(1);
1148 dirz = this->GetDirection(2);
1149 for(
unsigned ii = 0; ii < 3; ii++)
1151 dir[ii][0] = dirx[ii];
1152 dir[ii][1] = diry[ii];
1153 dir[ii][2] = dirz[ii];
1156 #if defined(ITKIO_DEPRECATED_METADATA_ORIENTATION)
1172 m_MetaImage.AnatomicalOrientation(0, MET_ORIENTATION_RL);
1184 m_MetaImage.AnatomicalOrientation(0, MET_ORIENTATION_LR);
1196 m_MetaImage.AnatomicalOrientation(0, MET_ORIENTATION_AP);
1208 m_MetaImage.AnatomicalOrientation(0, MET_ORIENTATION_PA);
1220 m_MetaImage.AnatomicalOrientation(0, MET_ORIENTATION_IS);
1232 m_MetaImage.AnatomicalOrientation(0, MET_ORIENTATION_SI);
1247 m_MetaImage.AnatomicalOrientation(1, MET_ORIENTATION_RL);
1259 m_MetaImage.AnatomicalOrientation(1, MET_ORIENTATION_LR);
1271 m_MetaImage.AnatomicalOrientation(1, MET_ORIENTATION_AP);
1284 m_MetaImage.AnatomicalOrientation(1, MET_ORIENTATION_PA);
1296 m_MetaImage.AnatomicalOrientation(1, MET_ORIENTATION_IS);
1308 m_MetaImage.AnatomicalOrientation(1, MET_ORIENTATION_SI);
1323 m_MetaImage.AnatomicalOrientation(2, MET_ORIENTATION_RL);
1335 m_MetaImage.AnatomicalOrientation(2, MET_ORIENTATION_LR);
1347 m_MetaImage.AnatomicalOrientation(2, MET_ORIENTATION_AP);
1359 m_MetaImage.AnatomicalOrientation(2, MET_ORIENTATION_PA);
1372 m_MetaImage.AnatomicalOrientation(2, MET_ORIENTATION_IS);
1384 m_MetaImage.AnatomicalOrientation(2, MET_ORIENTATION_SI);
1392 double *transformMatrix =
1393 static_cast< double *
>(malloc(this->GetNumberOfDimensions() *
1394 this->GetNumberOfDimensions() *
sizeof(
double)));
1395 for(
unsigned int ii=0; ii < this->GetNumberOfDimensions(); ii++ )
1397 for(
unsigned int jj=0; jj < this->GetNumberOfDimensions(); jj++ )
1399 transformMatrix[ ii*this->GetNumberOfDimensions() + jj ] =
1400 this->GetDirection( ii )[ jj ];
1403 m_MetaImage.TransformMatrix( transformMatrix );
1404 free( transformMatrix );
1406 m_MetaImage.CompressedData( m_UseCompression );
1411 for(i=0; i<nDims; i++)
1414 largestRegion.
SetSize(i, this->GetDimensions(i));
1417 if( m_UseCompression && (largestRegion != m_IORegion) )
1419 std::cout <<
"Compression in use: cannot stream the file writing" << std::endl;
1421 else if( largestRegion != m_IORegion )
1423 int* indexMin =
new int[nDims];
1424 int* indexMax =
new int[nDims];
1425 for(
unsigned int k=0; k<nDims; k++ )
1430 indexMin[k] = m_IORegion.GetIndex()[k];
1431 indexMax[k] = m_IORegion.GetIndex()[k] + m_IORegion.GetSize()[k] - 1;
1434 if (!m_MetaImage.WriteROI( indexMin, indexMax, m_FileName.c_str() ))
1438 itkExceptionMacro(
"File ROI cannot be written: "
1439 << this->GetFileName()
1442 << itksys::SystemTools::GetLastSystemError());
1450 if ( !m_MetaImage.Write( m_FileName.c_str() ) )
1452 itkExceptionMacro(
"File cannot be written: "
1453 << this->GetFileName()
1456 << itksys::SystemTools::GetLastSystemError());
1479 if(!m_UseStreamedReading)
1481 for(
unsigned int i=0; i < this->m_NumberOfDimensions; i++ )
1483 streamableRegion.
SetSize( i, this->m_Dimensions[i] );
1489 streamableRegion = requestedRegion;
1492 return streamableRegion;
1503 if (pasteRegion != largestPossibleRegion)
1505 itkExceptionMacro(
"Pasting and compression is not supported! Can't write:" << this->
GetFileName());
1507 else if (numberOfRequestedSplits != 1)
1509 itkDebugMacro(
"Requested streaming and compression");
1510 itkDebugMacro(
"Meta IO is not streaming now!");
1515 if (!itksys::SystemTools::FileExists(
m_FileName.c_str() ))
1519 else if (pasteRegion != largestPossibleRegion)
1524 std::string errorMessage;
1529 headerImageIOReader->SetFileName(
m_FileName.c_str());
1530 headerImageIOReader->ReadImageInformation();
1534 errorMessage =
"Unable to read information from file: " +
m_FileName;
1547 if (errorMessage.size())
1552 else if (headerImageIOReader->m_MetaImage.CompressedData())
1554 errorMessage =
"File is compressed: " +
m_FileName;
1566 errorMessage =
"Component type does not match in file: " +
m_FileName;
1571 errorMessage =
"Dimensions does not match in file: " +
m_FileName;
1578 if (headerImageIOReader->GetDimensions(i) != this->
GetDimensions(i) ||
1579 headerImageIOReader->GetSpacing(i) != this->
GetSpacing(i) ||
1580 headerImageIOReader->GetOrigin(i) != this->
GetOrigin(i))
1582 errorMessage =
"Size, spacing or origin does not match in file: " +
m_FileName;
1586 if (headerImageIOReader->GetDirection(i) != this->
GetDirection(i))
1588 errorMessage =
"Direction cosines does not match in file: " +
m_FileName;
1594 if (errorMessage.size())
1596 itkExceptionMacro(
"Unable to paste because pasting file exists and is different. " << errorMessage);
1598 else if ( headerImageIOReader->GetPixelType() != this->
GetPixelType() )
1602 itkWarningMacro(
"Pixel types does not match file, but component type and number of components do.");
1605 else if (numberOfRequestedSplits != 1)
1611 if (!itksys::SystemTools::RemoveFile(
m_FileName.c_str()))
1612 itkExceptionMacro(
"Unable to remove file for streaming: " <<
m_FileName);
1621 unsigned int numberOfActualSplits,