18 #include <nifti1_io.h>
27 #include <itksys/SystemTools.hxx>
50 "UNKNOWN",
"BINARY",
"CHAR",
"SHORT",
"INT",
"FLOAT",
51 "COMPLEX",
"DOUBLE",
"RGB",
"ALL",
"USHORT",
"UINT"
56 const short int DataTypeSizes[12]={0,1,8,16,32,32,64,64,24,0,16,32};
63 ANALYZE_DT_UNSIGNED_CHAR,
64 ANALYZE_DT_SIGNED_SHORT,
65 ANALYZE_DT_SIGNED_INT,
71 SPMANALYZE_DT_UNSIGNED_SHORT,
72 SPMANALYZE_DT_UNSIGNED_INT
82 std::string fileExt(itksys::SystemTools::GetFilenameLastExtension(filename));
85 if(fileExt == std::string(
".gz"))
87 fileExt=itksys::SystemTools::GetFilenameLastExtension(itksys::SystemTools::GetFilenameWithoutLastExtension(filename) );
91 if(fileExt !=
".img.gz" && fileExt !=
".img" && fileExt !=
".hdr")
104 if( fileExt.length() > 0
105 && filename.length() > fileExt.length()
108 const std::string::size_type it = filename.find_last_of( fileExt );
109 const std::string baseName( filename, 0, it-(fileExt.length()-1) );
121 ImageFileName +=
".hdr";
122 return( ImageFileName );
128 std::string ImageFileName (filename);
130 if(fileExt ==
".hdr")
133 ImageFileName +=
".img";
135 return( ImageFileName );
144 size_t numberOfPixels =
static_cast<size_t>( _numberOfPixels );
155 ((
unsigned char*)buffer, numberOfPixels );
159 ((
short*)buffer, numberOfPixels );
163 ((
unsigned short*)buffer, numberOfPixels );
167 ((
int*)buffer, numberOfPixels );
171 ((
unsigned int*)buffer, numberOfPixels );
175 ((
long*)buffer, numberOfPixels );
179 ((
unsigned long*)buffer, numberOfPixels );
187 ((
double*)buffer, numberOfPixels );
190 itkExceptionMacro(<<
"Pixel Type Unknown");
203 ((
unsigned char *)buffer, numberOfPixels );
207 ((
short *)buffer, numberOfPixels );
211 ((
unsigned short *)buffer, numberOfPixels );
215 ((
int *)buffer, numberOfPixels );
219 ((
unsigned int *)buffer, numberOfPixels );
223 ((
long *)buffer, numberOfPixels );
227 ((
unsigned long *)buffer, numberOfPixels );
231 ((
float *)buffer, numberOfPixels );
235 ((
double *)buffer, numberOfPixels );
238 itkExceptionMacro(<<
"Pixel Type Unknown");
265 if((temphdr.hk.extents == 16384) || (temphdr.hk.sizeof_hdr == 348))
267 returnvalue = systemOrder;
286 &imageheader->hk.sizeof_hdr);
288 &imageheader->hk.extents );
290 &imageheader->hk.session_error );
292 &imageheader->dime.dim[0], 8 );
294 &imageheader->dime.unused1 );
296 &imageheader->dime.datatype );
298 &imageheader->dime.bitpix );
300 &imageheader->dime.dim_un0 );
303 &imageheader->dime.pixdim[0],8 );
305 &imageheader->dime.vox_offset );
307 &imageheader->dime.roi_scale );
309 &imageheader->dime.funused1 );
311 &imageheader->dime.funused2 );
313 &imageheader->dime.cal_max );
315 &imageheader->dime.cal_min );
317 &imageheader->dime.compressed );
319 &imageheader->dime.verified );
321 &imageheader->dime.glmax );
323 &imageheader->dime.glmin );
326 &imageheader->hist.views );
328 &imageheader->hist.vols_added );
330 &imageheader->hist.start_field );
332 &imageheader->hist.field_skip );
334 &imageheader->hist.omax );
336 &imageheader->hist.omin );
338 &imageheader->hist.smax );
340 &imageheader->hist.smin );
348 &imageheader->hk.sizeof_hdr );
350 &imageheader->hk.extents );
352 &imageheader->hk.session_error );
355 &imageheader->dime.dim[0], 8 );
357 &imageheader->dime.unused1 );
359 &imageheader->dime.datatype );
361 &imageheader->dime.bitpix );
363 &imageheader->dime.dim_un0 );
366 &imageheader->dime.pixdim[0],8 );
368 &imageheader->dime.vox_offset );
370 &imageheader->dime.roi_scale );
372 &imageheader->dime.funused1 );
374 &imageheader->dime.funused2 );
376 &imageheader->dime.cal_max );
378 &imageheader->dime.cal_min );
380 &imageheader->dime.compressed );
382 &imageheader->dime.verified );
384 &imageheader->dime.glmax );
386 &imageheader->dime.glmin );
389 &imageheader->hist.views );
391 &imageheader->hist.vols_added );
393 &imageheader->hist.start_field );
395 &imageheader->hist.field_skip );
397 &imageheader->hist.omax );
399 &imageheader->hist.omin );
401 &imageheader->hist.smax );
403 &imageheader->hist.smin );
407 itkExceptionMacro(<<
"Machine Endian Type Unknown");
438 memset(&(this->
m_Hdr),0,
sizeof(
struct dsr));
443 this->
m_Hdr.hk.sizeof_hdr =
static_cast< int >(
sizeof(
struct dsr) );
444 this->
m_Hdr.hk.db_name[0]=
'\0';
445 this->
m_Hdr.hk.extents=16384;
446 this->
m_Hdr.hk.session_error=0;
447 this->
m_Hdr.hk.regular=
'r';
448 this->
m_Hdr.hk.hkey_un0=
'\0';
451 this->
m_Hdr.dime.dim[0]=4;
452 this->
m_Hdr.dime.dim[1]=1;
453 this->
m_Hdr.dime.dim[2]=1;
454 this->
m_Hdr.dime.dim[3]=1;
455 this->
m_Hdr.dime.dim[4]=1;
456 this->
m_Hdr.dime.dim[5]=1;
457 this->
m_Hdr.dime.dim[6]=1;
458 this->
m_Hdr.dime.dim[7]=1;
461 this->
m_Hdr.dime.vox_units[0]=
'\0';
463 this->
m_Hdr.dime.cal_units[0]=
'\0';
465 this->
m_Hdr.dime.unused1=0;
473 this->
m_Hdr.dime.dim_un0=0;
479 this->
m_Hdr.dime.pixdim[0]=0.0f;
480 this->
m_Hdr.dime.pixdim[1]=1.0f;
481 this->
m_Hdr.dime.pixdim[2]=1.0f;
482 this->
m_Hdr.dime.pixdim[3]=1.0f;
483 this->
m_Hdr.dime.pixdim[4]=1.0f;
484 this->
m_Hdr.dime.pixdim[5]=1.0f;
485 this->
m_Hdr.dime.pixdim[6]=1.0f;
486 this->
m_Hdr.dime.pixdim[7]=1.0f;
490 this->
m_Hdr.dime.vox_offset=0.0f;
492 this->
m_Hdr.dime.roi_scale=0.0f;
493 this->
m_Hdr.dime.funused1=0.0f;
494 this->
m_Hdr.dime.funused2=0.0f;
495 this->
m_Hdr.dime.cal_max=0.0f;
496 this->
m_Hdr.dime.cal_min=0.0f;
497 this->
m_Hdr.dime.compressed=0;
499 this->
m_Hdr.dime.verified=0;
500 this->
m_Hdr.dime.glmax=0;
501 this->
m_Hdr.dime.glmin=0;
504 this->
m_Hdr.hist.descrip[0]=
'\0';
505 this->
m_Hdr.hist.aux_file[0]=
'\0';
514 this->
m_Hdr.hist.orient =
517 this->
m_Hdr.hist.originator[0]=
'\0';
518 this->
m_Hdr.hist.generated[0]=
'\0';
519 this->
m_Hdr.hist.scannum[0]=
'\0';
520 this->
m_Hdr.hist.patient_id[0]=
'\0';
521 this->
m_Hdr.hist.exp_date[0]=
'\0';
522 this->
m_Hdr.hist.exp_time[0]=
'\0';
523 this->
m_Hdr.hist.hist_un0[0]=
'\0';
524 this->
m_Hdr.hist.views=0;
525 this->
m_Hdr.hist.vols_added=0;
526 this->
m_Hdr.hist.start_field=0;
527 this->
m_Hdr.hist.field_skip=0;
528 this->
m_Hdr.hist.omax=0;
529 this->
m_Hdr.hist.omin=0;
530 this->
m_Hdr.hist.smax=0;
531 this->
m_Hdr.hist.smin=0;
552 std::string filename(FileNameToWrite);
554 std::string::size_type imgPos = filename.rfind(
".img");
555 if ((imgPos != std::string::npos)
556 && (imgPos == filename.length() - 4))
562 std::string::size_type hdrPos = filename.rfind(
".hdr");
563 if ((hdrPos != std::string::npos)
564 && (hdrPos == filename.length() - 4))
570 std::string::size_type imggzPos = filename.rfind(
".img.gz");
571 if ((imggzPos != std::string::npos)
572 && (imggzPos == filename.length() - 7))
598 enum DataTypeIndex eNewType;
605 eNewType = ANALYZE_DT_INDEX_RGB;
610 eNewType=ANALYZE_DT_INDEX_UNSIGNED_CHAR;
614 eNewType=ANALYZE_DT_INDEX_SIGNED_SHORT;
617 eNewType = SPMANALYZE_DT_INDEX_UNSIGNED_SHORT;
620 eNewType=ANALYZE_DT_INDEX_SIGNED_INT;
623 eNewType=SPMANALYZE_DT_INDEX_UNSIGNED_INT;
626 eNewType=ANALYZE_DT_INDEX_FLOAT;
629 eNewType=ANALYZE_DT_INDEX_DOUBLE;
645 eNewType=ANALYZE_DT_INDEX_UNKNOWN;
646 itkExceptionMacro(<<
"Pixel Type Unknown");
651 switch(
m_Hdr.dime.datatype)
653 case ANALYZE_DT_BINARY:
657 case ANALYZE_DT_UNSIGNED_CHAR:
658 m_Hdr.dime.glmax=255;
661 case ANALYZE_DT_SIGNED_SHORT:
665 case ANALYZE_DT_FLOAT:
669 case ANALYZE_DT_DOUBLE:
674 m_Hdr.dime.glmax=255;
706 gzFile file_p = ::gzopen( ImageFileName.c_str(),
"rb" );
712 ImageFileName +=
".gz";
713 file_p = ::gzopen( ImageFileName.c_str(),
"rb" );
716 itkExceptionMacro( <<
"Analyze Data File can not be read: "
717 <<
" The following files were attempted:\n "
719 << ImageFileName <<
"\n" );
728 z_off_t byteOffset =
static_cast<z_off_t
>(fabs(
m_Hdr.dime.vox_offset));
731 if ( ::gzseek(file_p, byteOffset, SEEK_SET) == -1 )
733 itkExceptionMacro ( <<
"Analyze Data File can not be read: "
734 <<
" Unable to seek to the vox_offset: "
735 << byteOffset <<
"\n" );
749 char * p =
static_cast<char *
>(buffer);
752 while ( bytesRemaining )
754 unsigned int bytesToRead = bytesRemaining >
static_cast<SizeType>(maxChunk)
755 ? maxChunk : static_cast<unsigned int>(bytesRemaining);
757 int retval = ::gzread( file_p, p, bytesToRead );
764 if( retval != static_cast<int>(bytesToRead) )
766 itkExceptionMacro( <<
"Analyze Data File : gzread returned bad value: "
772 bytesRemaining -= bytesToRead;
782 if ( file_p !=
NULL )
796 std::string filename(FileNameToRead);
799 if(filenameext != std::string(
".hdr")
800 && filenameext != std::string(
".img.gz")
801 && filenameext != std::string(
".img")
809 std::ifstream local_InputStream;
810 local_InputStream.open( HeaderFileName.c_str(),
811 std::ios::in | std::ios::binary );
812 if( local_InputStream.fail() )
817 (
void *)&(this->
m_Hdr),
818 sizeof(
struct dsr) ) )
820 local_InputStream.close();
823 local_InputStream.close();
829 #ifdef OMIT_THIS_CODE
832 if(this->
m_Hdr.dime.compressed==1)
844 const bool NotNiftiTaggedFile=(is_nifti_file(FileNameToRead) == 0);
845 return NotNiftiTaggedFile;
852 std::ifstream local_InputStream;
853 local_InputStream.open(HeaderFileName.c_str(),
854 std::ios::in | std::ios::binary);
855 if( local_InputStream.fail())
857 itkExceptionMacro(<<
"File cannot be read");
860 (
void *)&(this->
m_Hdr),
861 sizeof(
struct dsr) ) )
863 itkExceptionMacro(<<
"Unexpected end of file");
865 local_InputStream.close();
883 unsigned int numberOfDimensions = this->
m_Hdr.dime.dim[0];
884 if (numberOfDimensions == 0)
890 const unsigned int maxNumberOfDimensions = 4;
891 for (
unsigned int idx = 1;
892 (idx <= maxNumberOfDimensions) && (this->
m_Hdr.dime.dim[idx]);
895 if (this->
m_Hdr.dime.dim[idx] > 0)
897 numberOfDimensions++;
901 itkWarningMacro(
"AnalyzeImageIO is ignoring dimension " << idx <<
" with value " << this->
m_Hdr.dime.dim[idx] );
906 if (numberOfDimensions == 0)
908 itkExceptionMacro(
"AnalyzeImageIO cannot process file: "
910 <<
". Number of dimensions is 0." <<std::endl
911 <<
"hdr.dime[0] = " <<
m_Hdr.dime.dim[0] << std::endl
912 <<
"hdr.dime[1] = " <<
m_Hdr.dime.dim[1] << std::endl
913 <<
"hdr.dime[2] = " <<
m_Hdr.dime.dim[2] << std::endl
914 <<
"hdr.dime[3] = " <<
m_Hdr.dime.dim[3] << std::endl
915 <<
"hdr.dime[4] = " <<
m_Hdr.dime.dim[4] << std::endl);
918 while(this->
m_Hdr.dime.dim[numberOfDimensions] <=1 )
920 --numberOfDimensions;
925 switch( this->
m_Hdr.dime.datatype )
927 case ANALYZE_DT_BINARY:
931 case ANALYZE_DT_UNSIGNED_CHAR:
935 case ANALYZE_DT_SIGNED_SHORT:
939 case SPMANALYZE_DT_UNSIGNED_SHORT:
943 case ANALYZE_DT_SIGNED_INT:
947 case SPMANALYZE_DT_UNSIGNED_INT:
951 case ANALYZE_DT_FLOAT:
955 case ANALYZE_DT_DOUBLE:
988 itk::EncapsulateMetaData<std::string>
992 itk::EncapsulateMetaData<std::string>
994 itk::EncapsulateMetaData<std::string>
996 std::string(this->
m_Hdr.dime.cal_units,8));
997 itk::EncapsulateMetaData<short int>
999 itk::EncapsulateMetaData<float>
1002 this->
m_Hdr.dime.cal_max);
1004 this->
m_Hdr.dime.cal_min);
1010 if (
m_Hdr.dime.dim[dim] != 1)
1017 switch(this->
m_Hdr.dime.datatype)
1019 case ANALYZE_DT_BINARY:
1020 itk::EncapsulateMetaData<std::string>
1023 case ANALYZE_DT_UNSIGNED_CHAR:
1024 itk::EncapsulateMetaData<std::string>
1026 std::string(
typeid(
unsigned char).name()));
1028 case ANALYZE_DT_SIGNED_SHORT:
1029 itk::EncapsulateMetaData<std::string>
1031 std::string(
typeid(
short).name()));
1033 case SPMANALYZE_DT_UNSIGNED_SHORT:
1034 itk::EncapsulateMetaData<std::string>
1036 std::string(
typeid(
unsigned short).name()));
1038 case ANALYZE_DT_SIGNED_INT:
1039 itk::EncapsulateMetaData<std::string>
1041 std::string(
typeid(
long).name()));
1043 case SPMANALYZE_DT_UNSIGNED_INT:
1044 itk::EncapsulateMetaData<std::string>
1046 std::string(
typeid(
unsigned long).name()));
1048 case ANALYZE_DT_FLOAT:
1049 itk::EncapsulateMetaData<std::string>
1051 std::string(
typeid(
float).name()));
1053 case ANALYZE_DT_DOUBLE:
1054 itk::EncapsulateMetaData<std::string>
1056 std::string(
typeid(
double).name()));
1058 case ANALYZE_DT_RGB:
1061 itk::EncapsulateMetaData<std::string>
1070 itk::EncapsulateMetaData<std::string>
1072 std::string(this->
m_Hdr.hist.descrip,80));
1073 itk::EncapsulateMetaData<std::string>
1075 std::string(this->
m_Hdr.hist.aux_file,24));
1079 (this->
m_Hdr.hist.orient);
1094 itkWarningMacro(
"Unknown orientation in file " <<
m_FileName );
1100 OrientAdapterType().ToDirectionCosines(coord_orient);
1104 #define itkAnalzyeImageIO_MINDIMS_IS_THREE ( (dims < 3) ? 3 : dims)
1108 #undef itkAnalzyeImageIO_MINDIMS_IS_THREE
1110 dirx[0] = dir[0][0];
1111 dirx[1] = dir[1][0];
1112 dirx[2] = dir[2][0];
1113 diry[0] = dir[0][1];
1114 diry[1] = dir[1][1];
1115 diry[2] = dir[2][1];
1116 dirz[0] = dir[0][2];
1117 dirz[1] = dir[1][2];
1118 dirz[2] = dir[2][2];
1119 for(
unsigned i = 3; i < dims; i++)
1121 dirx[i] = diry[i] = dirz[i] = 0;
1125 if(numberOfDimensions > 2)
1139 if(dirx[0] == 0.0 && dirx[1] == 0.0)
1150 else if(diry[0] == 0.0 && diry[1] == 0.0)
1162 #if defined(ITKIO_DEPRECATED_METADATA_ORIENTATION)
1168 itk::EncapsulateMetaData<std::string>
1170 std::string(this->
m_Hdr.hist.originator,10));
1171 itk::EncapsulateMetaData<std::string>
1173 std::string(this->
m_Hdr.hist.generated,10));
1174 itk::EncapsulateMetaData<std::string>
1176 std::string(this->
m_Hdr.hist.scannum,10));
1177 itk::EncapsulateMetaData<std::string>
1179 std::string(this->
m_Hdr.hist.patient_id,10));
1180 itk::EncapsulateMetaData<std::string>
1182 std::string(this->
m_Hdr.hist.exp_date,10));
1183 itk::EncapsulateMetaData<std::string>
1185 std::string(this->
m_Hdr.hist.exp_time,10));
1187 itk::EncapsulateMetaData<int>
1189 this->
m_Hdr.hist.omax);
1190 itk::EncapsulateMetaData<int>
1192 this->
m_Hdr.hist.omin);
1193 itk::EncapsulateMetaData<int>
1195 this->
m_Hdr.hist.smax);
1196 itk::EncapsulateMetaData<int>
1198 this->
m_Hdr.hist.smin);
1211 for(
unsigned int i = 0; i < this->GetNumberOfDimensions(); i++)
1213 unsigned int curdim(this->GetDimensions(i));
1216 itkExceptionMacro( <<
"Dimension(" << i <<
") = " << curdim
1217 <<
" is greater than maximum possible dimension "
1223 if(this->GetPixelType() == RGB)
1225 if(this->GetComponentType() != UCHAR)
1227 itkExceptionMacro(<<
"Only unsigned char RGB files supported");
1232 if(this->GetNumberOfComponents() > 1)
1234 itkExceptionMacro(<<
"More than one component per pixel not supported");
1239 std::ofstream local_OutputStream;
1240 local_OutputStream.open( HeaderFileName.c_str(),
1241 std::ios::out | std::ios::binary );
1242 if( local_OutputStream.fail() )
1244 itkExceptionMacro(<<
"File cannot be written");
1256 strncpy(this->m_Hdr.hk.db_name,temp.c_str(),18);
1259 if(itk::ExposeMetaData<std::string>(thisDic,
ITK_VoxelUnits,temp))
1261 strncpy(this->m_Hdr.dime.vox_units,temp.c_str(),4);
1266 strncpy(this->m_Hdr.dime.cal_units,temp.c_str(),8);
1269 itk::ExposeMetaData<short int>
1271 this->m_Hdr.dime.bitpix);
1272 itk::ExposeMetaData<float>
1274 this->m_Hdr.dime.roi_scale);
1275 itk::ExposeMetaData<float>
1277 this->m_Hdr.dime.cal_max);
1278 itk::ExposeMetaData<float>
1280 this->m_Hdr.dime.cal_min);
1281 itk::ExposeMetaData<int>
1283 this->m_Hdr.dime.glmax);
1284 itk::ExposeMetaData<int>
1286 this->m_Hdr.dime.glmin);
1288 if(itk::ExposeMetaData<std::string>(thisDic,
ITK_FileNotes,temp))
1290 strncpy(this->m_Hdr.hist.descrip,temp.c_str(),80);
1295 strncpy(this->m_Hdr.hist.aux_file,temp.c_str(),24);
1300 #if defined(ITKIO_DEPRECATED_METADATA_ORIENTATION)
1308 unsigned int dims = this->GetNumberOfDimensions();
1309 std::vector<double> dirx = this->GetDirection(0);
1310 std::vector<double> diry = this->GetDirection(1);
1311 std::vector<double> dirz;
1314 dirz = this->GetDirection(2);
1318 for(
unsigned i = 0; i < 3; i++)
1320 dirz.push_back(0.0);
1324 for(i = 0; i < dims; i++)
1326 dir[i][0] = dirx[i];
1327 dir[i][1] = diry[i];
1328 dir[i][2] = dirz[i];
1338 #if defined(ITKIO_DEPRECATED_METADATA_ORIENTATION)
1341 switch (coord_orient)
1344 this->m_Hdr.hist.orient =
1348 this->m_Hdr.hist.orient =
1352 this->m_Hdr.hist.orient =
1356 this->m_Hdr.hist.orient =
1358 itkWarningMacro(
"ERROR: Analyze 7.5 File Format"
1359 " Only Allows RPI, PIR, and RIP Orientation " );
1364 strncpy(this->m_Hdr.hist.originator,temp.c_str(),10);
1369 strncpy(this->m_Hdr.hist.generated,temp.c_str(),10);
1374 strncpy(this->m_Hdr.hist.scannum,temp.c_str(),10);
1377 if(itk::ExposeMetaData<std::string>(thisDic,
ITK_PatientID,temp))
1379 strncpy(this->m_Hdr.hist.patient_id,temp.c_str(),10);
1384 strncpy(this->m_Hdr.hist.exp_date,temp.c_str(),10);
1389 strncpy(this->m_Hdr.hist.exp_time,temp.c_str(),10);
1392 itk::ExposeMetaData<int>(thisDic,
ANALYZE_O_MAX,this->m_Hdr.hist.omax);
1393 itk::ExposeMetaData<int>(thisDic,
ANALYZE_O_MIN,this->m_Hdr.hist.omin);
1394 itk::ExposeMetaData<int>(thisDic,
ANALYZE_S_MAX,this->m_Hdr.hist.smax);
1395 itk::ExposeMetaData<int>(thisDic,
ANALYZE_S_MIN,this->m_Hdr.hist.smin);
1399 const SizeValueType maximumNumberOfPixelsAllowedInOneDimension =
1400 itk::NumericTraits<short>::max();
1402 for( dim=0; dim< this->GetNumberOfDimensions(); dim++ )
1404 const SizeValueType numberOfPixelsAlongThisDimension = m_Dimensions[ dim ];
1406 if( numberOfPixelsAlongThisDimension > maximumNumberOfPixelsAllowedInOneDimension )
1408 itkExceptionMacro(
"Number of pixels along dimension " << dim
1409 <<
" is " << numberOfPixelsAlongThisDimension <<
1410 " which exceeds maximum allowable dimension of " <<
1411 maximumNumberOfPixelsAllowedInOneDimension );
1416 this->m_Hdr.dime.dim[dim+1] = numberOfPixelsAlongThisDimension;
1420 this->m_Hdr.dime.dim[0]= 4;
1421 for( dim=this->GetNumberOfDimensions();(int)dim < this->m_Hdr.dime.dim[0];
1426 this->m_Hdr.dime.dim[dim+1] = 1;
1428 for( dim=0; dim< this->GetNumberOfDimensions(); dim++ )
1432 this->m_Hdr.dime.pixdim[dim+1]=
static_cast< float >( m_Spacing[ dim ] );
1436 this->DefineHeaderObjectDataType();
1438 local_OutputStream.write( (
const char *)&(this->m_Hdr),
sizeof(
struct dsr) );
1439 if( local_OutputStream.eof() )
1441 itkExceptionMacro(<<
"Unexpected end of file");
1443 local_OutputStream.close();
1456 if( this->m_Dimensions.size() == correctedDirection.size() )
1458 return correctedDirection;
1467 if(direction0[0] == 0.0 && direction1[0] == 0)
1469 if(direction0[1] == 0.0)
1471 correctedDirection[0] = 1.0;
1478 if(direction0[0] == 0.0 && direction1[0] == 0)
1480 if(direction1[1] == 0.0)
1482 correctedDirection[0] = 1.0;
1485 else if(direction0[1] == 0.0 && direction1[1] == 0)
1487 if(direction0[0] == 0.0)
1489 correctedDirection[0] = 1.0;
1491 if(direction1[0] == 0.0)
1493 correctedDirection[1] = 1.0;
1498 return correctedDirection;
1512 std::vector<double> direction0 = this->GetDirection(0);
1513 std::vector<double> direction1 = this->GetDirection(1);
1517 if(direction0[0] == 0.0 && direction1[0] == 0)
1519 if(direction0[1] == 0.0)
1521 defaultDirection[0] = 1.0;
1528 if(direction0[0] == 0.0 && direction1[0] == 0)
1530 if(direction1[1] == 0.0)
1532 defaultDirection[0] = 1.0;
1535 else if(direction0[1] == 0.0 && direction1[1] == 0)
1537 if(direction0[0] == 0.0)
1539 defaultDirection[0] = 1.0;
1541 if(direction1[0] == 0.0)
1543 defaultDirection[1] = 1.0;
1548 return defaultDirection;
1560 this->WriteImageInformation();
1565 voidp p =
const_cast<voidp
>(buffer);
1567 const std::string fileExt =
GetExtension( m_FileName );
1570 if(!fileExt.compare(
".img.gz" ))
1573 gzFile file_p = ::gzopen( ImageFileName.c_str(),
"wb" );
1576 itkExceptionMacro( <<
"Error, Can not write compressed image file for " << m_FileName );
1588 SizeType bytesRemaining = this->GetImageSizeInBytes();
1589 while ( bytesRemaining )
1591 unsigned int bytesToWrite = bytesRemaining >
static_cast<SizeType>(maxChunk)
1592 ? maxChunk : static_cast<unsigned int>(bytesRemaining);
1594 if( ::gzwrite(file_p, p, bytesToWrite ) != static_cast<int>(bytesToWrite) )
1596 itkExceptionMacro( <<
"Error, Can not write compressed image file for "<< m_FileName );
1598 p =
static_cast<char *
>( p ) + bytesToWrite;
1599 bytesRemaining -= bytesToWrite;
1601 ::gzclose( file_p );
1607 if ( file_p !=
NULL )
1609 ::gzclose( file_p );
1622 unusedbaseimgname +=
".img";
1623 itksys::SystemTools::RemoveFile(unusedbaseimgname.c_str());
1628 std::ofstream local_OutputStream;
1629 local_OutputStream.open( ImageFileName.c_str(), std::ios::out | std::ios::binary );
1630 if( !local_OutputStream )
1632 itkExceptionMacro( <<
"Error opening image data file for writing."
1635 local_OutputStream.write((
const char *)p, static_cast< std::streamsize >( this->GetImageSizeInBytes() ) );
1636 bool success = !local_OutputStream.bad();
1637 local_OutputStream.close();
1640 itkExceptionMacro( <<
"Error writing image data."
1649 unusedbaseimgname +=
".img.gz";
1650 itksys::SystemTools::RemoveFile(unusedbaseimgname.c_str());