23 #if !defined(_MSC_VER) || (_MSC_VER > 1300)
29 #include <itksys/SystemTools.hxx>
34 #define Brains2_MASKFILE_WHITE 0
35 #define Brains2_MASKFILE_BLACK 1
36 #define Brains2_MASKFILE_GRAY 2
39 template <
class TPixel>
42 unsigned int Evaluate(
const TPixel *pixel);
44 template <
class TPixel>
49 return *pixel == 0 ? 0 : 1;
79 const std::string FileExtension=itksys::SystemTools::GetFilenameLastExtension(
m_FileName);
80 if( FileExtension == std::string(
".mask"))
94 unsigned short int colorCode;
95 octreestream.read((
char *)&colorCode,
sizeof (
unsigned short int));
96 if (machineByteOrder != fileByteOrder)
103 else if ( machineByteOrder ==
BigEndian )
114 for (
unsigned int i =
ZERO; i <=
SEVEN; i++)
117 CurrentNodeBranch->
GetLeaf(static_cast<enum LeafIdentifier>(i));
119 switch ((colorCode >> (i << 1)) & 3)
135 return CurrentNodeBranch;
141 std::ifstream local_InputStream;
142 local_InputStream.open( this->m_FileName.c_str(), std::ios::in | std::ios::binary );
143 if( local_InputStream.fail() )
145 itkExceptionMacro(<<
"File "<< this->m_FileName <<
"cannot be opened for reading");
148 this->m_B2MaskHeader.ReadBrains2Header(local_InputStream);
150 unsigned int octreeHdr[6];
153 #if !defined(__BORLANDC__)
154 local_InputStream.ignore();
156 local_InputStream.read((
char *)octreeHdr,6*
sizeof(
unsigned int));
157 if(this->m_ByteOrder != this->m_MachineByteOrder)
159 if ( m_MachineByteOrder == LittleEndian )
172 octree->
SetTrueDims(octreeHdr[2],octreeHdr[3],octreeHdr[4]);
173 this->m_Octree = octree;
174 switch (octreeHdr[5])
185 octree->
SetTree(readOctree(local_InputStream,this->m_MachineByteOrder,this->m_ByteOrder ));
187 local_InputStream.close();
190 unsigned char *
const p =
static_cast<unsigned char *
>(buffer);
191 for(
unsigned int k=0; k< this->m_Dimensions[2]; k++)
193 const unsigned int slice_offset=k*this->m_Dimensions[1]*this->m_Dimensions[0];
194 for(
unsigned int j=0; j< this->m_Dimensions[1]; j++)
196 const unsigned int sliceandrowoffset=slice_offset+j*this->m_Dimensions[0];
197 for(
unsigned int i=0; i< this->m_Dimensions[0]; i++)
200 unsigned int val = octree->
GetValue(i,j,k);
207 p[sliceandrowoffset+i] = 0;
220 const std::string FileName(FileNameToRead);
221 const std::string FileExtension=itksys::SystemTools::GetFilenameLastExtension(FileName);
222 if( FileExtension != std::string(
".mask"))
227 std::ifstream local_InputStream;
228 local_InputStream.open( FileNameToRead, std::ios::in | std::ios::binary );
229 if( local_InputStream.fail() )
245 std::vector<double> dirx(dims,0), diry(dims,0), dirz(dims,0);
266 if(acqVal ==
"SAGITTAL")
270 else if(acqVal ==
"AXIAL")
274 else if(acqVal ==
"CORONAL")
280 itkExceptionMacro(<<
"If MASK_ACQ_PLANE is specified, then it must be one of CORONAL, AXIAL, or SAGITAL flags.");
282 EncapsulateMetaData<SpatialOrientation::ValidCoordinateOrientationFlags>
284 #if !defined(_MSC_VER) || (_MSC_VER > 1300)
288 dir = OrientAdapterType().ToDirectionCosines(coord_orient);
299 for(
unsigned i = 3; i < dims; i++)
301 dirx[i] = diry[i] = dirz[i] = 0;
306 itkExceptionMacro(<<
"No orientation specified.");
322 local_InputStream.close();
366 "BYTE_ORDER: BIG_ENDIAN\n"
367 "MASK_HEADER_BEGIN\n"
368 "MASK_NUM_DIMS: %d\n"
370 "MASK_X_RESOLUTION: %f\n"
372 "MASK_Y_RESOLUTION: %f\n"
374 "MASK_Z_RESOLUTION: %f\n"
375 "MASK_THRESHOLD: %f\n"
377 #if defined(OBSOLETE)
378 "MASK_ACQ_PLANE: %s\n"
405 unsigned short colorCode = 0;
407 for (i = 0; i < 8; i++)
410 branch->
GetChild(static_cast<enum LeafIdentifier>(i));
428 output.write((
const char *)&colorCode,
sizeof(colorCode));
429 for (i = 0; i < 8; i++)
432 branch->
GetChild(static_cast<enum LeafIdentifier>(i));
444 for(
unsigned i = 0; i < s.size(); i++)
457 if(this->m_FileName ==
"")
459 itkExceptionMacro(<<
"Error in missing Filename");
461 std::ofstream output(this->m_FileName.c_str(), std::ios::out | std::ios::binary );
464 itkExceptionMacro(<<
"Error in opening file "<< this->m_FileName <<
" for writing");
466 const unsigned xsize = this->GetDimensions(0);
467 const unsigned ysize = this->GetDimensions(1);
468 const unsigned zsize = this->GetDimensions(2);
470 const double xres = this->GetSpacing(0);
471 const double yres = this->GetSpacing(1);
472 const double zres = this->GetSpacing(2);
476 std::string patient_id(
"00000");
487 timeinfo = localtime(&rawtime);
488 std::string timestr = asctime(timeinfo);
490 std::string::size_type newline = timestr.rfind(
'\n');
491 if(newline != std::string::npos)
493 timestr.erase(newline);
495 if(patient_id ==
"" || patient_id ==
" " )
497 patient_id =
"00000";
499 std::string fname = this->m_FileName;
501 std::string orientation =
"UNKNOWN";
502 std::vector<double> dirx = this->GetDirection(0);
503 std::vector<double> diry = this->GetDirection(1);
504 std::vector<double> dirz = this->GetDirection(2);
506 origin[0] = this->GetOrigin(0);
507 origin[1] = this->GetOrigin(1);
508 origin[2] = this->GetOrigin(2);
511 #if defined(OBSOLETE)
514 for(
unsigned int i = 0; i < 3; i++)
522 switch (coord_orient)
526 orientation =
"AXIAL";
530 orientation =
"SAGITTAL";
534 orientation =
"CORONAL";
540 itkExceptionMacro(<<
"Error: Invalid orientation specified for writing mask. \n"
541 <<
"\nGIVEN " << coord_orient <<
"\n" << dir
542 <<
"\n Only Axial, Sagital, and Coronal orietations are supported in this file format."
556 "itkBrains2MaskImageIO",
559 itksys::SystemTools::GetFilenameName(m_FileName).c_str(),
569 #if defined(OBSOLETE)
572 dirx[0],dirx[1],dirx[2],
573 diry[0],diry[1],diry[2],
574 dirz[0],dirz[1],dirz[2],
575 origin[0],origin[1],origin[2]
577 output.write(buf,strlen(buf));
578 unsigned octreeHdr[6];
581 if(m_ComponentType == CHAR)
585 else if(m_ComponentType == UCHAR)
589 else if(m_ComponentType == SHORT)
593 else if(m_ComponentType == USHORT)
597 else if(m_ComponentType == INT)
601 else if(m_ComponentType == UINT)
605 else if(m_ComponentType == LONG)
609 else if(m_ComponentType == ULONG)
613 else if(m_ComponentType == FLOAT)
617 else if(m_ComponentType == DOUBLE)
623 itkExceptionMacro(<<
"Pixel type unsupported in this file type.");
625 octBasePtr->BuildFromBuffer(buffer,xsize,ysize,zsize);
626 tree = octBasePtr->GetTree();
628 octreeHdr[0] = octBasePtr->GetDepth();
629 octreeHdr[1] = octBasePtr->GetWidth();
630 octreeHdr[2] = xsize;
631 octreeHdr[3] = ysize;
632 octreeHdr[4] = zsize;
643 output.write((
const char *)octreeHdr,
sizeof(
unsigned)*6);