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

VtkIO reads/writes VTK image file. More...

#include <vtkio.h>

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

Public Member Functions

 VtkIO (const string &fname, IOMode rw_mode=READ_ONLY)
 
 ~VtkIO ()
 
- 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 int get_nimg ()
 Return the number of images in this image file. 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  VtkType { VTK_UNKNOWN , VTK_ASCII , VTK_BINARY }
 
enum  DataType {
  DATATYPE_UNKNOWN , BIT , UNSIGNED_CHAR , CHAR ,
  UNSIGNED_SHORT , SHORT , UNSIGNED_INT , INT ,
  UNSIGNED_LONG , LONG , FLOAT , DOUBLE
}
 
enum  DatasetType {
  DATASET_UNKNOWN , STRUCTURED_POINTS , STRUCTURED_GRID , RECTILINEAR_GRID ,
  UNSTRUCTURED_GRID , POLYDATA
}
 

Private Member Functions

int to_em_datatype (int vtk_datatype)
 
int get_mode_size (DataType d)
 
void read_dataset (DatasetType dstype)
 
DataType get_datatype_from_name (const string &datatype_name)
 
DatasetType get_datasettype_from_name (const string &dataset_name)
 

Private Attributes

bool is_big_endian
 
bool is_new_file
 
DataType datatype
 
VtkType filetype
 
int nx
 
int ny
 
int nz
 
float originx
 
float originy
 
float originz
 
float spacingx
 
float spacingy
 
float spacingz
 
off_t file_offset
 

Static Private Attributes

static const char * MAGIC = "# vtk DataFile Version"
 

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

VtkIO reads/writes VTK image file.

VTK is a file format used by the Visual Toolkit. (http://public.kitware.com/VTK/)

There are 2 VTK formats: ASCII or Binary.

ASCII format It has 5 parts:

Binary format It has the same 5 parts like ASCII format,followed by data in binary format. The data are stored in big endian by default..

A VTK file contains 1 2D or 3D image.

Definition at line 72 of file vtkio.h.

Member Enumeration Documentation

◆ DatasetType

Enumerator
DATASET_UNKNOWN 
STRUCTURED_POINTS 
STRUCTURED_GRID 
RECTILINEAR_GRID 
UNSTRUCTURED_GRID 
POLYDATA 

Definition at line 107 of file vtkio.h.

108 {
115 };
@ STRUCTURED_GRID
Definition: vtkio.h:111
@ STRUCTURED_POINTS
Definition: vtkio.h:110
@ UNSTRUCTURED_GRID
Definition: vtkio.h:113
@ POLYDATA
Definition: vtkio.h:114
@ RECTILINEAR_GRID
Definition: vtkio.h:112
@ DATASET_UNKNOWN
Definition: vtkio.h:109

◆ DataType

enum EMAN::VtkIO::DataType
private
Enumerator
DATATYPE_UNKNOWN 
BIT 
UNSIGNED_CHAR 
CHAR 
UNSIGNED_SHORT 
SHORT 
UNSIGNED_INT 
INT 
UNSIGNED_LONG 
LONG 
FLOAT 
DOUBLE 

Definition at line 91 of file vtkio.h.

92 {
94 BIT,
96 CHAR,
98 SHORT,
100 INT,
102 LONG,
103 FLOAT,
104 DOUBLE
105 };
@ DOUBLE
Definition: vtkio.h:104
@ DATATYPE_UNKNOWN
Definition: vtkio.h:93
@ SHORT
Definition: vtkio.h:98
@ UNSIGNED_SHORT
Definition: vtkio.h:97
@ CHAR
Definition: vtkio.h:96
@ UNSIGNED_LONG
Definition: vtkio.h:101
@ UNSIGNED_CHAR
Definition: vtkio.h:95
@ UNSIGNED_INT
Definition: vtkio.h:99

◆ VtkType

enum EMAN::VtkIO::VtkType
private
Enumerator
VTK_UNKNOWN 
VTK_ASCII 
VTK_BINARY 

Definition at line 84 of file vtkio.h.

85 {
89 };
@ VTK_UNKNOWN
Definition: vtkio.h:86
@ VTK_BINARY
Definition: vtkio.h:88
@ VTK_ASCII
Definition: vtkio.h:87

Constructor & Destructor Documentation

◆ VtkIO()

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

Definition at line 43 of file vtkio.cpp.

44: ImageIO(fname, rw)
45{
47 is_new_file = false;
48
51 nx = 0;
52 ny = 0;
53 nz = 0;
54 originx = 0;
55 originy = 0;
56 originz = 0;
57 spacingx = 0;
58 spacingy = 0;
59 spacingz = 0;
60 file_offset = 0;
61}
static bool is_host_big_endian()
Definition: byteorder.cpp:40
ImageIO(const string &fname, IOMode rw)
Definition: imageio.cpp:40
off_t file_offset
Definition: vtkio.h:138
float originx
Definition: vtkio.h:132
float spacingx
Definition: vtkio.h:135
bool is_new_file
Definition: vtkio.h:125
float spacingy
Definition: vtkio.h:136
float originz
Definition: vtkio.h:134
DataType datatype
Definition: vtkio.h:127
float originy
Definition: vtkio.h:133
int nx
Definition: vtkio.h:129
VtkType filetype
Definition: vtkio.h:128
int ny
Definition: vtkio.h:130
bool is_big_endian
Definition: vtkio.h:124
float spacingz
Definition: vtkio.h:137
int nz
Definition: vtkio.h:131

References datatype, DATATYPE_UNKNOWN, file_offset, filetype, is_big_endian, EMAN::ByteOrder::is_host_big_endian(), is_new_file, nx, ny, nz, originx, originy, originz, spacingx, spacingy, spacingz, and VTK_UNKNOWN.

◆ ~VtkIO()

VtkIO::~VtkIO ( )

Definition at line 63 of file vtkio.cpp.

64{
65 if (file) {
66 fclose(file);
67 file = 0;
68 }
69}
FILE * file
Definition: imageio.h:354

References EMAN::ImageIO::file.

Member Function Documentation

◆ get_datasettype_from_name()

VtkIO::DatasetType VtkIO::get_datasettype_from_name ( const string &  dataset_name)
private

Definition at line 427 of file vtkio.cpp.

428{
429
430 static bool initialized = false;
431 static map < string, DatasetType > types;
432
433 if (!initialized) {
434 types["STRUCTURED_POINTS"] = STRUCTURED_POINTS;
435 types["STRUCTURED_GRID"] = STRUCTURED_GRID;
436 types["RECTILINEAR_GRID"] = RECTILINEAR_GRID;
437 types["UNSTRUCTURED_GRID"] = UNSTRUCTURED_GRID;
438 types["POLYDATA"] = POLYDATA;
439 }
440
442 if (types.find(dataset_name) != types.end()) {
443 result = types[dataset_name];
444 }
445 return result;
446}
bool initialized
Definition: imageio.h:355

References DATASET_UNKNOWN, EMAN::ImageIO::initialized, POLYDATA, RECTILINEAR_GRID, STRUCTURED_GRID, STRUCTURED_POINTS, and UNSTRUCTURED_GRID.

◆ get_datatype_from_name()

VtkIO::DataType VtkIO::get_datatype_from_name ( const string &  datatype_name)
private

Definition at line 394 of file vtkio.cpp.

395{
396 static bool initialized = false;
397 static map < string, VtkIO::DataType > datatypes;
398
399 if (!initialized) {
400 datatypes["bit"] = BIT;
401
402 datatypes["unsigned_char"] = UNSIGNED_CHAR;
403 datatypes["char"] = CHAR;
404
405 datatypes["unsigned_short"] = UNSIGNED_SHORT;
406 datatypes["short"] = SHORT;
407
408 datatypes["unsigned_int"] = UNSIGNED_INT;
409 datatypes["int"] = INT;
410
411 datatypes["unsigned_long"] = UNSIGNED_LONG;
412 datatypes["long"] = LONG;
413
414 datatypes["float"] = FLOAT;
415 datatypes["double"] = DOUBLE;
416 initialized = true;
417 }
418
419 DataType result = DATATYPE_UNKNOWN;
420
421 if (datatypes.find(datatype_name) != datatypes.end()) {
422 result = datatypes[datatype_name];
423 }
424 return result;
425}

References BIT, CHAR, DATATYPE_UNKNOWN, DOUBLE, FLOAT, EMAN::ImageIO::initialized, INT, LONG, SHORT, UNSIGNED_CHAR, UNSIGNED_INT, UNSIGNED_LONG, and UNSIGNED_SHORT.

◆ get_mode_size()

int VtkIO::get_mode_size ( DataType  d)
private

Definition at line 368 of file vtkio.cpp.

369{
370 switch (d) {
371 case UNSIGNED_CHAR:
372 case CHAR:
373 return sizeof(char);
374 case UNSIGNED_SHORT:
375 case SHORT:
376 return sizeof(short);
377 case UNSIGNED_INT:
378 case INT:
379 return sizeof(int);
380 case UNSIGNED_LONG:
381 case LONG:
382 return sizeof(long);
383 case FLOAT:
384 return sizeof(float);
385 case DOUBLE:
386 return sizeof(double);
387 default:
388 LOGERR("don't support this data type '%d'", d);
389 break;
390 }
391 return 0;
392}
#define LOGERR
Definition: log.h:51

References CHAR, DOUBLE, FLOAT, INT, LOGERR, LONG, SHORT, UNSIGNED_CHAR, UNSIGNED_INT, UNSIGNED_LONG, and UNSIGNED_SHORT.

◆ is_valid()

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

Definition at line 160 of file vtkio.cpp.

161{
162 ENTERFUNC;
163 bool result = false;
164 if (first_block) {
165 result = Util::check_file_by_magic(first_block, MAGIC);
166 }
167 EXITFUNC;
168 return result;
169}
static bool check_file_by_magic(const void *first_block, const char *magic)
check whether a file starts with certain magic string.
Definition: util.cpp:239
static const char * MAGIC
Definition: vtkio.h:82
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49

References EMAN::Util::check_file_by_magic(), ENTERFUNC, EXITFUNC, and MAGIC.

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

◆ read_dataset()

void VtkIO::read_dataset ( DatasetType  dstype)
private

Definition at line 448 of file vtkio.cpp.

449{
450 char buf[1024];
451 int bufsz = sizeof(buf);
452
453 if (dstype == STRUCTURED_POINTS) {
454 int nlines = 3;
455 int i = 0;
456 while (i < nlines && fgets(buf, bufsz, file)) {
457 if (samestr(buf, "DIMENSIONS")) {
458 sscanf(buf, "DIMENSIONS %d %d %d", &nx, &ny, &nz);
459 }
460 else if (samestr(buf, "ORIGIN")) {
461 sscanf(buf, "ORIGIN %f %f %f", &originx, &originy, &originz);
462 }
463 else if (samestr(buf, "SPACING") || samestr(buf, "ASPECT_RATIO")) {
464 if (samestr(buf, "SPACING")) {
465 sscanf(buf, "SPACING %f %f %f", &spacingx, &spacingy, &spacingz);
466 }
467 else {
468 sscanf(buf, "ASPECT_RATIO %f %f %f", &spacingx, &spacingy, &spacingz);
469 }
470
471 if (spacingx != spacingy || spacingx != spacingz || spacingy != spacingz) {
473 "not support non-uniform spacing VTK so far\n");
474 }
475 }
476 i++;
477 }
478
479 if (i != nlines) {
480 throw ImageReadException(filename, "read VTK file failed");
481 }
482 }
483 else {
484 throw ImageReadException(filename, "only STRUCTURED_POINTS is supported so far");
485 }
486}
string filename
Definition: imageio.h:352
#define ImageReadException(filename, desc)
Definition: exception.h:204
static int samestr(const char *s1, const char *s2)
Definition: vtkio.cpp:71

References EMAN::ImageIO::file, EMAN::ImageIO::filename, ImageReadException, nx, ny, nz, originx, originy, originz, samestr(), spacingx, spacingy, spacingz, and STRUCTURED_POINTS.

◆ to_em_datatype()

int VtkIO::to_em_datatype ( int  vtk_datatype)
private

Definition at line 353 of file vtkio.cpp.

354{
355 DataType d = static_cast < DataType > (vtk_datatype);
356 switch (d) {
357 case UNSIGNED_SHORT:
358 return EMUtil::EM_USHORT;
359 case FLOAT:
360 return EMUtil::EM_FLOAT;
361 default:
362 break;
363 }
364 return EMUtil::EM_UNKNOWN;
365}
@ EM_UNKNOWN
Definition: emutil.h:93
@ EM_USHORT
Definition: emutil.h:97

References EMAN::EMUtil::EM_FLOAT, EMAN::EMUtil::EM_UNKNOWN, EMAN::EMUtil::EM_USHORT, FLOAT, and UNSIGNED_SHORT.

Member Data Documentation

◆ datatype

DataType EMAN::VtkIO::datatype
private

Definition at line 127 of file vtkio.h.

Referenced by VtkIO().

◆ DEFINE_IMAGEIO_FUNC

EMAN::VtkIO::DEFINE_IMAGEIO_FUNC

Definition at line 78 of file vtkio.h.

◆ file_offset

off_t EMAN::VtkIO::file_offset
private

Definition at line 138 of file vtkio.h.

Referenced by VtkIO().

◆ filetype

VtkType EMAN::VtkIO::filetype
private

Definition at line 128 of file vtkio.h.

Referenced by VtkIO().

◆ is_big_endian

bool EMAN::VtkIO::is_big_endian
private

Definition at line 124 of file vtkio.h.

Referenced by VtkIO().

◆ is_new_file

bool EMAN::VtkIO::is_new_file
private

Definition at line 125 of file vtkio.h.

Referenced by VtkIO().

◆ MAGIC

const char * VtkIO::MAGIC = "# vtk DataFile Version"
staticprivate

Definition at line 82 of file vtkio.h.

Referenced by is_valid().

◆ nx

int EMAN::VtkIO::nx
private

Definition at line 129 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

◆ ny

int EMAN::VtkIO::ny
private

Definition at line 130 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

◆ nz

int EMAN::VtkIO::nz
private

Definition at line 131 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

◆ originx

float EMAN::VtkIO::originx
private

Definition at line 132 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

◆ originy

float EMAN::VtkIO::originy
private

Definition at line 133 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

◆ originz

float EMAN::VtkIO::originz
private

Definition at line 134 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

◆ spacingx

float EMAN::VtkIO::spacingx
private

Definition at line 135 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

◆ spacingy

float EMAN::VtkIO::spacingy
private

Definition at line 136 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().

◆ spacingz

float EMAN::VtkIO::spacingz
private

Definition at line 137 of file vtkio.h.

Referenced by read_dataset(), and VtkIO().


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