EMAN::MrcIO Class Reference

MRC file = header + data (nx x ny x nz). More...

#include <mrcio.h>

Inheritance diagram for EMAN::MrcIO:

Inheritance graph
[legend]
Collaboration diagram for EMAN::MrcIO:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 MrcIO (const string &filename, IOMode rw_mode=READ_ONLY)
 ~MrcIO ()
int read_ctf (Ctf &ctf, int image_index=0)
 Read CTF data from this image.
void write_ctf (const Ctf &ctf, int image_index=0)
 Write CTF data to this image.

Static Public Member Functions

static bool is_valid (const void *first_block, off_t file_size=0)
static int get_mode_size (int mm)
static int to_em_datatype (int mrcmode)
static int to_mrcmode (int em_datatype, int is_complex)

Public Attributes

 DEFINE_IMAGEIO_FUNC

Private Types

enum  MrcMode {
  MRC_UCHAR = 0, MRC_SHORT, MRC_FLOAT, MRC_SHORT_COMPLEX,
  MRC_FLOAT_COMPLEX, MRC_USHORT = 6, MRC_UCHAR3 = 16, MRC_UNKNOWN
}
enum  { MRC_NUM_LABELS = 10, MRC_LABEL_SIZE = 80, NUM_4BYTES_PRE_MAP = 52, NUM_4BYTES_AFTER_MAP = 3 }

Private Member Functions

void swap_header (MrcHeader &mrch)

Static Private Member Functions

static int generate_machine_stamp ()
 generate the machine stamp used in MRC image format.

Private Attributes

string filename
IOMode rw_mode
FILE * mrcfile
MrcHeader mrch
int mode_size
int is_ri
bool is_big_endian
bool is_new_file
bool initialized

Static Private Attributes

static const char * CTF_MAGIC = "!-"
static const char * SHORT_CTF_MAGIC = "!$"

Classes

struct  MrcHeader


Detailed Description

MRC file = header + data (nx x ny x nz).

A MRC image file stores 1D, 2D or 3D image. The image's dimensions and pixel type are defined in the header.

Definition at line 48 of file mrcio.h.


Member Enumeration Documentation

enum EMAN::MrcIO::MrcMode [private]

Enumerator:
MRC_UCHAR 
MRC_SHORT 
MRC_FLOAT 
MRC_SHORT_COMPLEX 
MRC_FLOAT_COMPLEX 
MRC_USHORT 
MRC_UCHAR3 
MRC_UNKNOWN 

Definition at line 65 of file mrcio.h.

00065                              {
00066                         MRC_UCHAR = 0,
00067                         MRC_SHORT,
00068                         MRC_FLOAT,
00069                         MRC_SHORT_COMPLEX,
00070                         MRC_FLOAT_COMPLEX,
00071                         MRC_USHORT = 6,         //non-standard
00072                         MRC_UCHAR3 = 16,        //unsigned char * 3, for rgb data, non-standard
00073                         MRC_UNKNOWN
00074                 };

anonymous enum [private]

Enumerator:
MRC_NUM_LABELS 
MRC_LABEL_SIZE 
NUM_4BYTES_PRE_MAP 
NUM_4BYTES_AFTER_MAP 

Definition at line 76 of file mrcio.h.

00076                      {
00077                         MRC_NUM_LABELS = 10,
00078                         MRC_LABEL_SIZE = 80,
00079                         NUM_4BYTES_PRE_MAP = 52,
00080                         NUM_4BYTES_AFTER_MAP = 3
00081                 };


Constructor & Destructor Documentation

EMAN::MrcIO::MrcIO ( const string &  filename,
IOMode  rw_mode = READ_ONLY 
) [explicit]

MrcIO::~MrcIO (  ) 

Definition at line 61 of file mrcio.cpp.

References mrcfile.

00062 {
00063         if (mrcfile) {
00064                 fclose(mrcfile);
00065                 mrcfile = 0;
00066         }
00067 }


Member Function Documentation

int MrcIO::read_ctf ( Ctf ctf,
int  image_index = 0 
) [virtual]

Read CTF data from this image.

Parameters:
ctf Used to store the CTF data.
image_index The index of the image to read.
Returns:
0 if OK; 1 if error.

Reimplemented from EMAN::ImageIO.

Definition at line 729 of file mrcio.cpp.

References CTF_MAGIC, ENTERFUNC, EXITFUNC, EMAN::Ctf::from_string(), EMAN::ImageIO::init(), EMAN::MrcIO::MrcHeader::labels, and mrch.

00730 {
00731         ENTERFUNC;
00732         init();
00733         size_t n = strlen(CTF_MAGIC);
00734 
00735         int err = 1;
00736         if (strncmp(&mrch.labels[0][0], CTF_MAGIC, n) == 0) {
00737                 err = ctf.from_string(string(&mrch.labels[0][n]));
00738         }
00739         EXITFUNC;
00740         return err;
00741 }

void MrcIO::write_ctf ( const Ctf ctf,
int  image_index = 0 
) [virtual]

Write CTF data to this image.

Parameters:
ctf Ctf instance storing the CTF data.
image_index The index of the image to write.
Returns:
0 if OK; 1 if error.

Reimplemented from EMAN::ImageIO.

Definition at line 743 of file mrcio.cpp.

References CTF_MAGIC, ENTERFUNC, EXITFUNC, filename, ImageWriteException, EMAN::ImageIO::init(), EMAN::MrcIO::MrcHeader::labels, mrcfile, mrch, and EMAN::Ctf::to_string().

00744 {
00745         ENTERFUNC;
00746         init();
00747 
00748         string ctf_str = ctf.to_string();
00749         sprintf(&mrch.labels[0][0], "%s%s", CTF_MAGIC, ctf_str.c_str());
00750         rewind(mrcfile);
00751 
00752         if (fwrite(&mrch, sizeof(MrcHeader), 1, mrcfile) != 1) {
00753                 throw ImageWriteException(filename, "write CTF info to header failed");
00754         }
00755         EXITFUNC;
00756 }

bool MrcIO::is_valid ( const void *  first_block,
off_t  file_size = 0 
) [static]

Definition at line 130 of file mrcio.cpp.

References data, ENTERFUNC, EXITFUNC, get_mode_size(), EMAN::ByteOrder::is_data_big_endian(), EMAN::ByteOrder::is_host_big_endian(), MRC_FLOAT_COMPLEX, MRC_SHORT_COMPLEX, MRC_UCHAR, MRC_UNKNOWN, nx, ny, and EMAN::ByteOrder::swap_bytes().

Referenced by EMAN::EMUtil::fast_get_image_type(), and EMAN::EMUtil::get_image_type().

00131 {
00132         ENTERFUNC;
00133 
00134         if (!first_block) {
00135                 return false;
00136         }
00137 
00138         const int *data = static_cast < const int *>(first_block);
00139         int nx = data[0];
00140         int ny = data[1];
00141         int nz = data[2];
00142         int mrcmode = data[3];
00143         int nsymbt = data[23];  //this field specify the extra bytes for symmetry information
00144 
00145         bool data_big_endian = ByteOrder::is_data_big_endian(&nz);
00146 
00147         if (data_big_endian != ByteOrder::is_host_big_endian()) {
00148                 ByteOrder::swap_bytes(&nx);
00149                 ByteOrder::swap_bytes(&ny);
00150                 ByteOrder::swap_bytes(&nz);
00151                 ByteOrder::swap_bytes(&mrcmode);
00152         }
00153 
00154         if (mrcmode == MRC_SHORT_COMPLEX || mrcmode == MRC_FLOAT_COMPLEX) {
00155                 nx *= 2;
00156         }
00157 
00158         const int max_dim = 1 << 20;
00159 
00160         if ((mrcmode >= MRC_UCHAR && mrcmode < MRC_UNKNOWN) &&
00161                 (nx > 1 && nx < max_dim) && (ny > 0 && ny < max_dim) && (nz > 0 && nz < max_dim)) {
00162 #ifndef SPIDERMRC // Spider MRC files don't satisfy the following test
00163                 if (file_size > 0) {
00164                         off_t file_size1 = (off_t)nx * (off_t)ny * (off_t)nz * (off_t)get_mode_size(mrcmode) + (off_t)sizeof(MrcHeader) + nsymbt;
00165                         if (file_size == file_size1) {
00166                                 return true;
00167                         }
00168                         return false;
00169                 }
00170                 else {
00171                         return true;
00172                 }
00173 #endif // SPIDERMRC
00174                 return true;
00175         }
00176         EXITFUNC;
00177         return false;
00178 }

int MrcIO::get_mode_size ( int  mm  )  [static]

Definition at line 764 of file mrcio.cpp.

References MRC_FLOAT, MRC_FLOAT_COMPLEX, MRC_SHORT, MRC_SHORT_COMPLEX, MRC_UCHAR, and MRC_USHORT.

Referenced by is_valid().

00765 {
00766         MrcIO::MrcMode m = static_cast < MrcMode > (mm);
00767 
00768         int msize = 0;
00769         switch (m) {
00770         case MRC_UCHAR:
00771                 msize = sizeof(char);
00772                 break;
00773         case MRC_SHORT:
00774         case MRC_USHORT:
00775         case MRC_SHORT_COMPLEX:
00776                 msize = sizeof(short);
00777                 break;
00778         case MRC_FLOAT:
00779         case MRC_FLOAT_COMPLEX:
00780                 msize = sizeof(float);
00781                 break;
00782         default:
00783                 msize = 0;
00784         }
00785 
00786         return msize;
00787 }

int MrcIO::to_em_datatype ( int  mrcmode  )  [static]

Definition at line 789 of file mrcio.cpp.

References EMAN::EMUtil::EM_FLOAT, EMAN::EMUtil::EM_FLOAT_COMPLEX, EMAN::EMUtil::EM_SHORT, EMAN::EMUtil::EM_SHORT_COMPLEX, EMAN::EMUtil::EM_UCHAR, EMAN::EMUtil::EM_UNKNOWN, EMAN::EMUtil::EM_USHORT, MRC_FLOAT, MRC_FLOAT_COMPLEX, MRC_SHORT, MRC_SHORT_COMPLEX, MRC_UCHAR, and MRC_USHORT.

00790 {
00791         EMUtil::EMDataType e = EMUtil::EM_UNKNOWN;
00792 
00793         switch (m) {
00794         case MRC_UCHAR:
00795                 e = EMUtil::EM_UCHAR;
00796                 break;
00797         case MRC_SHORT:
00798                 e = EMUtil::EM_SHORT;
00799                 break;
00800         case MRC_USHORT:
00801                 e = EMUtil::EM_USHORT;
00802                 break;
00803         case MRC_SHORT_COMPLEX:
00804                 e = EMUtil::EM_SHORT_COMPLEX;
00805                 break;
00806         case MRC_FLOAT:
00807                 e = EMUtil::EM_FLOAT;
00808                 break;
00809         case MRC_FLOAT_COMPLEX:
00810                 e = EMUtil::EM_FLOAT_COMPLEX;
00811                 break;
00812         default:
00813                 e = EMUtil::EM_UNKNOWN;
00814         }
00815         return e;
00816 }

int MrcIO::to_mrcmode ( int  em_datatype,
int  is_complex 
) [static]

Definition at line 819 of file mrcio.cpp.

References EMAN::EMUtil::EM_CHAR, EMAN::EMUtil::EM_FLOAT, EMAN::EMUtil::EM_FLOAT_COMPLEX, EMAN::EMUtil::EM_INT, EMAN::EMUtil::EM_SHORT, EMAN::EMUtil::EM_SHORT_COMPLEX, EMAN::EMUtil::EM_UCHAR, EMAN::EMUtil::EM_UINT, EMAN::EMUtil::EM_USHORT, EMAN::EMUtil::EM_USHORT_COMPLEX, MRC_FLOAT, MRC_FLOAT_COMPLEX, MRC_SHORT, MRC_SHORT_COMPLEX, MRC_UCHAR, MRC_UNKNOWN, and MRC_USHORT.

00820 {
00821         MrcMode m = MRC_UNKNOWN;
00822         EMUtil::EMDataType em_type = static_cast < EMUtil::EMDataType > (e);
00823 
00824         switch (em_type) {
00825         case EMUtil::EM_UCHAR:
00826                 m = MRC_UCHAR;
00827                 break;
00828         case EMUtil::EM_USHORT:
00829                 if (is_complex) {
00830                         m = MRC_SHORT_COMPLEX;
00831                 }
00832                 else {
00833                         m = MRC_USHORT;
00834                 }
00835                 break;
00836         case EMUtil::EM_SHORT:
00837                 if (is_complex) {
00838                         m = MRC_SHORT_COMPLEX;
00839                 }
00840                 else {
00841                         m = MRC_SHORT;
00842                 }
00843                 break;
00844         case EMUtil::EM_SHORT_COMPLEX:
00845         case EMUtil::EM_USHORT_COMPLEX:
00846                 m = MRC_SHORT_COMPLEX;
00847                 break;
00848         case EMUtil::EM_CHAR:
00849         case EMUtil::EM_INT:
00850         case EMUtil::EM_UINT:
00851         case EMUtil::EM_FLOAT:
00852                 if (is_complex) {
00853                         m = MRC_FLOAT_COMPLEX;
00854                 }
00855                 else {
00856                         m = MRC_FLOAT;
00857                 }
00858                 break;
00859         case EMUtil::EM_FLOAT_COMPLEX:
00860                 m = MRC_FLOAT_COMPLEX;
00861                 break;
00862         default:
00863                 m = MRC_FLOAT;
00864         }
00865 
00866         return m;
00867 }

int MrcIO::generate_machine_stamp (  )  [static, private]

generate the machine stamp used in MRC image format.

Definition at line 871 of file mrcio.cpp.

References EMAN::ByteOrder::is_host_big_endian().

00872 {
00873         int stamp = 0;
00874         char *p = (char *) (&stamp);
00875 
00876         if (ByteOrder::is_host_big_endian()) {
00877                 p[0] = 0x11;
00878                 p[1] = 0x11;
00879                 p[2] = 0;
00880                 p[3] = 0;
00881         }
00882         else {
00883                 p[0] = 0x44;
00884                 p[1] = 0x41;
00885                 p[2] = 0;
00886                 p[3] = 0;
00887         }
00888         return stamp;
00889 }

void MrcIO::swap_header ( MrcHeader mrch  )  [private]


Member Data Documentation

Definition at line 54 of file mrcio.h.

const char * MrcIO::CTF_MAGIC = "!-" [static, private]

Definition at line 139 of file mrcio.h.

Referenced by read_ctf(), and write_ctf().

const char * MrcIO::SHORT_CTF_MAGIC = "!$" [static, private]

Definition at line 140 of file mrcio.h.

string EMAN::MrcIO::filename [private]

Definition at line 144 of file mrcio.h.

Referenced by write_ctf().

Definition at line 145 of file mrcio.h.

FILE* EMAN::MrcIO::mrcfile [private]

Definition at line 146 of file mrcio.h.

Referenced by write_ctf(), and ~MrcIO().

Definition at line 147 of file mrcio.h.

Referenced by read_ctf(), and write_ctf().

int EMAN::MrcIO::mode_size [private]

Definition at line 148 of file mrcio.h.

int EMAN::MrcIO::is_ri [private]

Definition at line 150 of file mrcio.h.

Definition at line 151 of file mrcio.h.

bool EMAN::MrcIO::is_new_file [private]

Definition at line 152 of file mrcio.h.

bool EMAN::MrcIO::initialized [private]

Definition at line 153 of file mrcio.h.


The documentation for this class was generated from the following files:

Generated on Sat Nov 21 02:20:22 2009 for EMAN2 by  doxygen 1.5.6