EMAN2
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
EMAN::SerIO Class Reference

SER (Series File Format) is a file format created by Dr. More...

#include <serio.h>

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

Classes

struct  SerHeader
 

Public Member Functions

 SerIO (const string &fname, IOMode rw_mode=READ_ONLY)
 
 ~SerIO ()
 
int get_nimg ()
 Return the number of images in this image file. More...
 
- Public Member Functions inherited from EMAN::ImageIO
 ImageIO (const string &fname, IOMode rw)
 
virtual ~ImageIO ()
 
virtual int read_header (Dict &dict, int image_index=0, const Region *area=0, bool is_3d=false)=0
 Read the header from an image. More...
 
virtual int write_header (const Dict &dict, int image_index=0, const Region *area=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)=0
 Write a header to an image. More...
 
virtual int read_data (float *data, int image_index=0, const Region *area=0, bool is_3d=false)=0
 Read the data from an image. More...
 
virtual int read_data_8bit (unsigned char *data, int image_index=0, const Region *area=0, bool is_3d=false, float minval=0.0f, float maxval=0.0f)
 Read the data from an image as an 8 bit array, regardless of format. More...
 
virtual int write_data (float *data, int image_index=0, const Region *area=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)=0
 Write data to an image. More...
 
virtual int read_ctf (Ctf &ctf, int image_index=0)
 Read CTF data from this image. More...
 
virtual void write_ctf (const Ctf &ctf, int image_index=0)
 Write CTF data to this image. More...
 
virtual void flush ()=0
 Flush the IO buffer. More...
 
virtual bool is_complex_mode ()=0
 Is this an complex image or not. More...
 
virtual bool is_image_big_endian ()=0
 Is this image in big endian or not. More...
 
virtual bool is_single_image_format () const
 Is this image format only storing 1 image or not. More...
 
template<class T >
void become_host_endian (T *data, size_t n=1)
 Convert data of this image into host endian format. More...
 
string get_filename () const
 

Static Public Member Functions

static bool is_valid (const void *first_block)
 

Public Attributes

 DEFINE_IMAGEIO_FUNC
 

Private Types

enum  SerImgMode { oneD = 0x4120 , twoD = 0x4122 }
 
enum  SerTagType { timeOnly = 0x4152 , posTime = 0x4122 }
 
enum  SerDataMode {
  SER_UCHAR = 1 , SER_USHORT , SER_UINT , SER_CHAR ,
  SER_SHORT , SER_INT , SER_FLOAT , SER_DOUBLE ,
  SER_COMPLEX8 , SER_COMPLEX16 , UNKNOWN
}
 

Private Member Functions

void read_dim_arr (Dict &dict, int idx)
 helper function to read attributes in dimension array More...
 
void read_data_element (Dict &dict)
 helper function to read header attributes in data element More...
 
void read_data_tag (Dict &dict)
 helper function to read header attributes in data tag More...
 

Private Attributes

bool is_new_file
 
SerHeader serh
 
uint64_t * data_offset_array
 
uint64_t * tag_offset_array
 
int nimg
 
int nx
 
int ny
 
int nz
 
int datatypeid
 
int datamode
 

Additional Inherited Members

- Public Types inherited from EMAN::ImageIO
enum  IOMode { READ_ONLY = 1 , READ_WRITE = 2 , WRITE_ONLY = 3 }
 
- Protected Member Functions inherited from EMAN::ImageIO
virtual void init ()=0
 Do some initialization before doing the read/write. More...
 
void check_read_access (int image_index)
 Validate 'image_index' in file reading. More...
 
void check_read_access (int image_index, const float *data)
 Validate 'image_index' and 'data' in file reading. More...
 
void check_write_access (IOMode rw_mode, int image_index, int max_nimg=0)
 Validate rw_mode and image_index in file writing. More...
 
void check_write_access (IOMode rw_mode, int image_index, int max_nimg, const float *data)
 Validate rw_mode, image_index, and data pointer in file writing. More...
 
void check_region (const Region *area, const FloatSize &max_size, bool is_new_file=false, bool inbounds_only=true)
 Validate image I/O region. More...
 
void check_region (const Region *area, const IntSize &max_size, bool is_new_file=false, bool inbounds_only=true)
 Validate image I/O region. More...
 
FILE * sfopen (const string &filename, IOMode mode, bool *is_new=0, bool overwrite=false)
 Run fopen safely. More...
 
- Protected Attributes inherited from EMAN::ImageIO
string filename
 
IOMode rw_mode
 
FILE * file = nullptr
 
bool initialized = false
 

Detailed Description

SER (Series File Format) is a file format created by Dr.

Chris Boothroyd. The file format is used by TIA (Tecnai imaging and analysis), which is the program used on FEI Tecnai and Titan microscopes for acquiring and displaying scanned images and spectra.

Each .ser file stores a number of 1D or 2D images. We do not support complex SER image for now.

http://www.microscopy.cen.dtu.dk/~cbb/info/TIAformat/index.html

Definition at line 55 of file serio.h.

Member Enumeration Documentation

◆ SerDataMode

Enumerator
SER_UCHAR 
SER_USHORT 
SER_UINT 
SER_CHAR 
SER_SHORT 
SER_INT 
SER_FLOAT 
SER_DOUBLE 
SER_COMPLEX8 
SER_COMPLEX16 
UNKNOWN 

Definition at line 76 of file serio.h.

76 {
77 SER_UCHAR = 1,
82 SER_INT,
88 };
@ SER_COMPLEX16
Definition: serio.h:86
@ SER_USHORT
Definition: serio.h:78
@ SER_INT
Definition: serio.h:82
@ SER_CHAR
Definition: serio.h:80
@ SER_UCHAR
Definition: serio.h:77
@ UNKNOWN
Definition: serio.h:87
@ SER_UINT
Definition: serio.h:79
@ SER_DOUBLE
Definition: serio.h:84
@ SER_FLOAT
Definition: serio.h:83
@ SER_SHORT
Definition: serio.h:81
@ SER_COMPLEX8
Definition: serio.h:85

◆ SerImgMode

Enumerator
oneD 
twoD 

Definition at line 66 of file serio.h.

66 {
67 oneD = 0x4120,
68 twoD = 0x4122
69 };
@ twoD
Definition: serio.h:68
@ oneD
Definition: serio.h:67

◆ SerTagType

Enumerator
timeOnly 
posTime 

Definition at line 71 of file serio.h.

71 {
72 timeOnly = 0x4152,
73 posTime = 0x4122
74 };
@ posTime
Definition: serio.h:73
@ timeOnly
Definition: serio.h:72

Constructor & Destructor Documentation

◆ SerIO()

SerIO::SerIO ( const string &  fname,
IOMode  rw_mode = READ_ONLY 
)
explicit

Definition at line 45 of file serio.cpp.

45 :
46 ImageIO(fname, rw),
48 nimg(0), nx(0), ny(0), nz(0), datatypeid(0), datamode(0)
49{
50}
ImageIO(const string &fname, IOMode rw)
Definition: imageio.cpp:40
bool is_new_file
Definition: serio.h:104
int datamode
Definition: serio.h:116
int datatypeid
Definition: serio.h:115
int nx
Definition: serio.h:112
uint64_t * data_offset_array
Definition: serio.h:108
int ny
Definition: serio.h:113
int nimg
Definition: serio.h:111
int nz
Definition: serio.h:114
uint64_t * tag_offset_array
Definition: serio.h:109

◆ ~SerIO()

SerIO::~SerIO ( )

Definition at line 52 of file serio.cpp.

53{
54 if (file) {
55 fclose(file);
56 file = 0;
57 }
58
60 delete [] data_offset_array;
62 }
63
64 if (tag_offset_array) {
65 delete [] tag_offset_array;
67 }
68}
FILE * file
Definition: imageio.h:354

References data_offset_array, EMAN::ImageIO::file, and tag_offset_array.

Member Function Documentation

◆ get_nimg()

int SerIO::get_nimg ( )
virtual

Return the number of images in this image file.

Reimplemented from EMAN::ImageIO.

Definition at line 368 of file serio.cpp.

368 {
369 init();
370
372 int nimg;
373 if (fread(&nimg, sizeof(int), 1, file) != 1) {
374 throw ImageReadException(filename, "SER header");
375 }
376
377 return nimg;
378}
string filename
Definition: imageio.h:352
virtual void init()=0
Do some initialization before doing the read/write.
#define ImageReadException(filename, desc)
Definition: exception.h:204
int portable_fseek(FILE *fp, off_t offset, int whence)
static const int ValidNumberElementsOffset
Definition: serio.cpp:43

References EMAN::ImageIO::file, EMAN::ImageIO::filename, ImageReadException, EMAN::ImageIO::init(), nimg, portable_fseek(), and ValidNumberElementsOffset.

◆ is_valid()

bool SerIO::is_valid ( const void *  first_block)
static

Definition at line 94 of file serio.cpp.

95{
97
98 if (!first_block) {
99 return false;
100 }
101
102 const short *data = static_cast < const short *>(first_block);
103 short ByteOrder = data[0];
104 short SeriesID = data[1];
105
106 if(ByteOrder != 0x4949 || SeriesID != 0x0197) {
107 return false;
108 }
109
110 EXITFUNC;
111 return true;
112}
ByteOrder defines functions to work on big/little endian byte orders.
Definition: byteorder.h:59
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49

References ENTERFUNC, and EXITFUNC.

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

◆ read_data_element()

void SerIO::read_data_element ( Dict dict)
private

helper function to read header attributes in data element

Definition at line 441 of file serio.cpp.

442{
443 if(this->datatypeid == oneD) { //1D image
444 double hitem4[2];
445 if (fread(hitem4, sizeof(double), 2, file) != 2) {
446 throw ImageReadException(filename, "SER header");
447 }
448 dict["SER.CalibrationOffset"] = hitem4[0];
449 dict["SER.CalibrationDelta"] = hitem4[1];
450
451 int cali;
452 if (fread(&cali, sizeof(int), 1, file) != 1) {
453 throw ImageReadException(filename, "SER header");
454 }
455 dict["SER.CalibrationElement"] = cali;
456
457 short datatype;
458 if (fread(&datatype, sizeof(short), 1, file) != 1) {
459 throw ImageReadException(filename, "SER header");
460 }
461 dict["SER.DataType"] = datatype;
462
463 int arrlen;
464 if (fread(&arrlen, sizeof(int), 1, file) != 1) {
465 throw ImageReadException(filename, "SER header");
466 }
467 dict["nx"] = arrlen;
468 dict["ny"] = 1;
469 dict["nz"] = 1;
470
471 nx = arrlen;
472 ny = 1;
473 nz = 1;
474 }
475 else if(this->datatypeid == twoD) { //2D image
476 double hitem4[2];
477 if (fread(hitem4, sizeof(double), 2, file) != 2) {
478 throw ImageReadException(filename, "SER header");
479 }
480 dict["SER.CalibrationOffsetX"] = hitem4[0];
481 dict["SER.CalibrationDeltaX"] = hitem4[1];
482
483 int calix;
484 if (fread(&calix, sizeof(int), 1, file) != 1) {
485 throw ImageReadException(filename, "SER header");
486 }
487 dict["SER.CalibrationElementX"] = calix;
488
489 double hitem5[2];
490 if (fread(hitem5, sizeof(double), 2, file) != 2) {
491 throw ImageReadException(filename, "SER header");
492 }
493 dict["SER.CalibrationOffsetX"] = hitem5[0];
494 dict["SER.CalibrationDeltaX"] = hitem5[1];
495
496 int caliy;
497 if (fread(&caliy, sizeof(int), 1, file) != 1) {
498 throw ImageReadException(filename, "SER header");
499 }
500 dict["SER.CalibrationElementY"] = caliy;
501
502 short datatype;
503 if (fread(&datatype, sizeof(short), 1, file) != 1) {
504 throw ImageReadException(filename, "SER header");
505 }
506 dict["SER.DataType"] = datatype;
507 this->datamode = datatype;
508
509 int arrsize[2];
510 if (fread(&arrsize, sizeof(int), 2, file) != 2) {
511 throw ImageReadException(filename, "SER header");
512 }
513 dict["nx"] = arrsize[0];
514 dict["ny"] = arrsize[1];
515 dict["nz"] = 1;
516
517 nx = arrsize[0];
518 ny = arrsize[1];
519 nz = 1;
520 }
521}

References datamode, datatypeid, EMAN::ImageIO::file, EMAN::ImageIO::filename, ImageReadException, nx, ny, nz, oneD, and twoD.

◆ read_data_tag()

void SerIO::read_data_tag ( Dict dict)
private

helper function to read header attributes in data tag

Definition at line 523 of file serio.cpp.

524{
525 int tag_type = (int)dict["SER.TagTypeID"];
526 if( tag_type == timeOnly ) {
527 short tagtype;
528 if (fread(&tagtype, sizeof(short), 1, file) != 1) {
529 throw ImageReadException(filename, "SER header");
530 }
531 assert((int)tagtype == tag_type);
532
533 int sertime;
534 if (fread(&sertime, sizeof(int), 1, file) != 1) {
535 throw ImageReadException(filename, "SER header");
536 }
537 dict["SER.Time"] = sertime;
538 }
539 else if( tag_type == posTime ) {
540 short tagtype;
541 if (fread(&tagtype, sizeof(short), 1, file) != 1) {
542 throw ImageReadException(filename, "SER header");
543 }
544 assert((int)tagtype == tag_type);
545
546 int sertime;
547 if (fread(&sertime, sizeof(int), 1, file) != 1) {
548 throw ImageReadException(filename, "SER header");
549 }
550 dict["SER.Time"] = sertime;
551
552 double pos[2];
553 if (fread(&pos, sizeof(double), 2, file) != 2) {
554 throw ImageReadException(filename, "SER header");
555 }
556 dict["SER.PosionX"] = pos[0];
557 dict["SER.PosionY"] = pos[1];
558 }
559 else {
560 throw ImageReadException(filename, "SER header, wrong TagTypeID");
561 }
562}

References EMAN::ImageIO::file, EMAN::ImageIO::filename, ImageReadException, posTime, and timeOnly.

◆ read_dim_arr()

void SerIO::read_dim_arr ( Dict dict,
int  idx 
)
private

helper function to read attributes in dimension array

Definition at line 380 of file serio.cpp.

381{
382 int dimsize;
383 if (fread(&dimsize, sizeof(int), 1, file) != 1) {
384 throw ImageReadException(filename, "SER header");
385 }
386
387 string sidx = Util::int2str(idx);
388 dict["SER.DimensionSize"+sidx] = dimsize;
389
390 double hitem3[2];
391 if (fread(hitem3, sizeof(double), 2, file) != 2) {
392 throw ImageReadException(filename, "SER header");
393 }
394 dict["SER.CalibrationOffset"+sidx] = hitem3[0];
395 dict["SER.CalibrationDelta"+sidx] = hitem3[1];
396
397 int celement;
398 if (fread(&celement, sizeof(int), 1, file) != 1) {
399 throw ImageReadException(filename, "SER header");
400 }
401 dict["SER.CalibrationElement"+sidx] = celement;
402
403 int desclen;
404 if (fread(&desclen, sizeof(int), 1, file) != 1) {
405 throw ImageReadException(filename, "SER header");
406 }
407 dict["SER.DescriptionLength"+sidx] = desclen;
408
409 if(desclen != 0) {
410 char * descr = new char[desclen+1];
411 //char descr[desclen+1];
412 if (fread(descr, sizeof(char), desclen, file) != (unsigned int)desclen) {
413 throw ImageReadException(filename, "SER header");
414 }
415 descr[desclen] = '\0';
416 string sdescr(descr);
417 dict["SER.Description"+sidx] = sdescr;
418 delete [] descr;
419 }
420
421 int unitslen;
422 if (fread(&unitslen, sizeof(int), 1, file) != 1) {
423 throw ImageReadException(filename, "SER header");
424 }
425 dict["SER.UnitsLength"+sidx] = unitslen;
426
427 if(unitslen != 0) {
428 char * units = new char[unitslen+1];
429 //char units[unitslen+1];
430 if (fread(units, sizeof(int), unitslen, file) != (unsigned int)unitslen) {
431 throw ImageReadException(filename, "SER header");
432 }
433 units[unitslen] = '\0';
434 string sunits(units);
435 dict["SER.Units"+sidx] = sunits;
436 delete [] units;
437 }
438
439}
static string int2str(int n)
Get a string format of an integer, e.g.
Definition: util.cpp:315

References EMAN::ImageIO::file, EMAN::ImageIO::filename, ImageReadException, and EMAN::Util::int2str().

Member Data Documentation

◆ data_offset_array

uint64_t* EMAN::SerIO::data_offset_array
private

Definition at line 108 of file serio.h.

Referenced by ~SerIO().

◆ datamode

int EMAN::SerIO::datamode
private

Definition at line 116 of file serio.h.

Referenced by read_data_element().

◆ datatypeid

int EMAN::SerIO::datatypeid
private

Definition at line 115 of file serio.h.

Referenced by read_data_element().

◆ DEFINE_IMAGEIO_FUNC

EMAN::SerIO::DEFINE_IMAGEIO_FUNC

Definition at line 61 of file serio.h.

◆ is_new_file

bool EMAN::SerIO::is_new_file
private

Definition at line 104 of file serio.h.

◆ nimg

int EMAN::SerIO::nimg
private

Definition at line 111 of file serio.h.

Referenced by get_nimg().

◆ nx

int EMAN::SerIO::nx
private

Definition at line 112 of file serio.h.

Referenced by read_data_element().

◆ ny

int EMAN::SerIO::ny
private

Definition at line 113 of file serio.h.

Referenced by read_data_element().

◆ nz

int EMAN::SerIO::nz
private

Definition at line 114 of file serio.h.

Referenced by read_data_element().

◆ serh

SerHeader EMAN::SerIO::serh
private

Definition at line 106 of file serio.h.

◆ tag_offset_array

uint64_t* EMAN::SerIO::tag_offset_array
private

Definition at line 109 of file serio.h.

Referenced by ~SerIO().


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