31 #include <itksys/SystemTools.hxx>
60 std::string fname = filename;
62 ( fname.find(
".mrc") < fname.length() ||
63 fname.find(
".rec") < fname.length() ) )
80 itkDebugMacro( <<
"Reading Magic numbers " << filename );
94 if ( strncmp(map,
magicMAP, 4) != 0 )
108 itkExceptionMacro(<<
"Must read info first");
132 switch ( header.
mode )
190 itkExceptionMacro(<<
"Unrecognized mode");
195 if ( header.
xlen == 0 &&
243 itkDebugMacro(<<
"Reading Information ");
250 itkExceptionMacro(<<
"Header Read failed: Wanted "
252 <<
" bytes, but read "
253 << file.gcount() <<
" bytes.");
257 if ( !
m_MRCHeader->SetHeader(reinterpret_cast<const MRCHeaderObject::Header*>(buffer) ) )
259 itkExceptionMacro(<<
"Unrecognized header");
265 buffer =
new char[
m_MRCHeader->GetExtendedHeaderSize()];
268 itkExceptionMacro(<<
"Extended Header Read failed.");
292 if( this->RequestedToStream( ) )
295 this->OpenFileForReading(file, this->m_FileName.c_str());
297 this->StreamReadBufferAsBinary(file, buffer);
305 this->OpenFileForReading(file, this->m_FileName.c_str());
308 std::streampos dataPos =
static_cast<std::streampos
>( this->GetHeaderSize() );
309 file.seekg( dataPos, std::ios::beg );
313 itkExceptionMacro(<<
"Failed seeking to data position");
318 this->ReadBufferAsBinary( file, buffer, this->GetImageSizeInBytes() );
321 int size = this->GetComponentSize();
327 this->GetByteOrder() == BigEndian ?
332 this->GetByteOrder() == BigEndian ?
337 itkExceptionMacro(<<
"Unknown component size");
344 std::string filename = fname;
345 if ( filename.length() > 4 &&
346 (filename.find(
".mrc") == filename.length() - 4 ||
347 filename.find(
".rec") == filename.length() - 4 ) )
363 itkExceptionMacro(<<
"MRC Writer can not write more than 3-dimensional images");
370 header.
stamp[0] = 17;
372 header.
stamp[0] = 68;
427 if (header.
mode == -1)
432 <<
"Supported pixel types include unsigned byte, unsigned short, signed short, float, rgb unsigned char, float complex"
453 itkExceptionMacro(<<
"Unexpected error setting header");
470 file.write(static_cast<const char*>((
void*)&(header)), 1024);
480 switch ( header.
mode )
485 this->UpdateHeaderWithMinMaxMean(static_cast<const unsigned char*>(bufferBegin));
491 this->UpdateHeaderWithMinMaxMean(static_cast<const short*>(bufferBegin));
497 this->UpdateHeaderWithMinMaxMean(static_cast<const float*>(bufferBegin));
506 m_MRCHeader->m_Header.amin = -1.0f;
507 m_MRCHeader->m_Header.amax = 1.0f;
508 m_MRCHeader->m_Header.amean = 0.0f;
517 m_MRCHeader->m_Header.amin = -1.0f;
518 m_MRCHeader->m_Header.amax = 1.0f;
519 m_MRCHeader->m_Header.amean = 0.0f;
525 this->UpdateHeaderWithMinMaxMean(static_cast<const unsigned short*>(bufferBegin));
533 m_MRCHeader->m_Header.amin = 0.0f;
534 m_MRCHeader->m_Header.amax = 255.0f;
535 m_MRCHeader->m_Header.amean = 127.5f;
540 itkExceptionMacro(<<
"Unrecognized mode");
549 if( this->RequestedToStream() )
556 if (!itksys::SystemTools::FileExists( m_FileName.c_str() ))
558 this->WriteImageInformation( buffer );
563 this->OpenFileForWriting(file, this->m_FileName.c_str(),
false);
568 std::streampos seekPos = this->GetImageSizeInBytes() + this->GetHeaderSize() - 1;
569 file.seekp( seekPos, std::ios::cur );
577 if ( m_MRCHeader.IsNull() )
588 this->InternalReadImageInformation( file );
600 this->OpenFileForWriting(file, this->m_FileName.c_str(),
false);
602 this->StreamWriteBufferAsBinary(file, buffer);
610 this->WriteImageInformation( buffer );
614 this->OpenFileForWriting(file, this->m_FileName.c_str(),
false);
617 std::streampos dataPos =
static_cast<std::streampos
>( this->GetHeaderSize() );
618 file.seekp( dataPos, std::ios::beg );
622 itkExceptionMacro(<<
"Failed seeking to data position");
626 if (!this->WriteBufferAsBinary( file, buffer, this->GetImageSizeInBytes() ))
628 itkExceptionMacro(<<
"Could not write file: " << m_FileName);