41const char *VtkIO::MAGIC =
"# vtk DataFile Version";
43VtkIO::VtkIO(
const string & fname,
IOMode rw)
71static int samestr(
const char *s1,
const char *s2)
73 return (strncmp(s1, s2, strlen(s2)) == 0);
89 int bufsz =
sizeof(buf);
90 if (fgets(buf, bufsz,
file) == 0) {
98 if (fgets(buf, bufsz,
file) == 0) {
102 if (fgets(buf, bufsz,
file)) {
106 else if (
samestr(buf,
"BINARY")) {
114 if (fgets(buf, bufsz,
file)) {
116 char dataset_name[128];
117 sscanf(buf,
"DATASET %s", dataset_name);
126 while (fgets(buf, bufsz,
file)) {
128 char datatypestr[32];
130 sscanf(buf,
"SCALARS %s %s", scalartype, datatypestr);
134 string desc =
"unknown data type: " + string(datatypestr);
138 else if (
samestr(buf,
"LOOKUP_TABLE")) {
140 sscanf(buf,
"LOOKUP_TABLE %s", tablename);
141 if (!
samestr(tablename,
"default")) {
176 if(image_index == -1) {
180 if(image_index != 0) {
181 throw ImageReadException(
filename,
"no stack allowed for MRC image. For take 2D slice out of 3D image, read the 3D image first, then use get_clip().");
187 int xlen = 0, ylen = 0, zlen = 0;
214 if(image_index == -1) {
217 if(image_index != 0) {
234 fprintf(
file,
"# vtk DataFile Version 2.0\n");
235 fprintf(
file,
"EMAN\n");
236 fprintf(
file,
"BINARY\n");
237 fprintf(
file,
"DATASET STRUCTURED_POINTS\n");
238 fprintf(
file,
"DIMENSIONS %0d %0d %0d\nORIGIN %f %f %f\nSPACING %f %f %f\n",
242 fprintf(
file,
"POINT_DATA %0lu\nSCALARS density float 1\nLOOKUP_TABLE default\n",
257 LOGWARN(
"read VTK region is not supported yet. Read whole image instead.");
262 int xlen = 0, ylen = 0, zlen = 0;
263 int x0 = 0, y0 = 0, z0 = 0;
270 char *buf =
new char[bufsz];
273 while (fgets(buf, bufsz,
file)) {
274 size_t bufslen = strlen(buf) - 1;
277 for (
size_t j = 0; j < bufslen; j++) {
278 if (!isspace(buf[j])) {
279 numstr[k++] = buf[j];
283 data[i++] = (float)atoi(numstr);
298 for (
int i = 0; i <
nz; i++) {
300 for (
int j = 0; j <
ny; j++) {
301 fread(&data[i2 + j *
nx], row_size, 1,
file);
323 bool swapped =
false;
329 fwrite(data,
nx *
nz,
ny *
sizeof(
float),
file);
376 return sizeof(short);
384 return sizeof(float);
386 return sizeof(double);
388 LOGERR(
"don't support this data type '%d'", d);
397 static map < string, VtkIO::DataType > datatypes;
400 datatypes[
"bit"] =
BIT;
403 datatypes[
"char"] =
CHAR;
406 datatypes[
"short"] =
SHORT;
409 datatypes[
"int"] =
INT;
412 datatypes[
"long"] =
LONG;
414 datatypes[
"float"] =
FLOAT;
415 datatypes[
"double"] =
DOUBLE;
421 if (datatypes.find(datatype_name) != datatypes.end()) {
422 result = datatypes[datatype_name];
431 static map < string, DatasetType > types;
442 if (types.find(dataset_name) != types.end()) {
443 result = types[dataset_name];
451 int bufsz =
sizeof(buf);
456 while (i < nlines && fgets(buf, bufsz,
file)) {
457 if (
samestr(buf,
"DIMENSIONS")) {
458 sscanf(buf,
"DIMENSIONS %d %d %d", &
nx, &
ny, &
nz);
460 else if (
samestr(buf,
"ORIGIN")) {
463 else if (
samestr(buf,
"SPACING") ||
samestr(buf,
"ASPECT_RATIO")) {
473 "not support non-uniform spacing VTK so far\n");
static bool is_host_big_endian()
static void swap_bytes(T *data, size_t n=1)
swap the byte order of data with 'n' T-type elements.
Dict is a dictionary to store <string, EMObject> pair.
EMDataType
Image pixel data type used in EMAN.
static void get_region_origins(const Region *area, int *p_x0, int *p_y0, int *p_z0=0, int nz=1, int image_index=0)
Get a region's original locations.
static void get_region_dims(const Region *area, int nx, int *area_x, int ny, int *area_y, int nz=1, int *area_z=0)
Get a region's dimensions.
ImageIO classes are designed for reading/writing various electron micrography image formats,...
void check_region(const Region *area, const FloatSize &max_size, bool is_new_file=false, bool inbounds_only=true)
Validate image I/O region.
virtual void flush()=0
Flush the IO buffer.
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.
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.
virtual bool is_complex_mode()=0
Is this an complex image or not.
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.
FILE * sfopen(const string &filename, IOMode mode, bool *is_new=0, bool overwrite=false)
Run fopen safely.
void check_read_access(int image_index)
Validate 'image_index' in file reading.
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.
virtual void init()=0
Do some initialization before doing the read/write.
void check_write_access(IOMode rw_mode, int image_index, int max_nimg=0)
Validate rw_mode and image_index in file writing.
virtual bool is_image_big_endian()=0
Is this image in big endian or not.
IntSize is used to describe a 1D, 2D or 3D rectangular size in integers.
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
static bool check_file_by_magic(const void *first_block, const char *magic)
check whether a file starts with certain magic string.
DataType get_datatype_from_name(const string &datatype_name)
DatasetType get_datasettype_from_name(const string &dataset_name)
int get_mode_size(DataType d)
static const char * MAGIC
void read_dataset(DatasetType dstype)
int to_em_datatype(int vtk_datatype)
static bool is_valid(const void *first_block)
#define ImageReadException(filename, desc)
#define ImageWriteException(imagename, desc)
off_t portable_ftell(FILE *fp)
int portable_fseek(FILE *fp, off_t offset, int whence)
static int samestr(const char *s1, const char *s2)