00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef eman__emutil__h__
00037 #define eman__emutil__h__ 1
00038
00039 #include "emobject.h"
00040 #include "emassert.h"
00041 #include <string.h>
00042
00043 using std::string;
00044 using std::vector;
00045
00046
00047
00048
00049
00050 template <class T>
00051 inline void EMDeletePtr(T & x)
00052 {
00053 #ifdef _WIN32
00054 if(x != NULL) {
00055 delete x;
00056 x = NULL;
00057 }
00058 #else
00059 {Assert(x != NULL);}
00060 delete x;
00061 x = NULL;
00062 #endif
00063 }
00064
00065 template <class T>
00066 inline void EMDeleteArray(T & x)
00067 {
00068 #ifdef _WIN32
00069 if(x != NULL) {
00070 delete x;
00071 x = NULL;
00072 }
00073 #else
00074 {Assert(x != NULL);}
00075 delete [] x;
00076 x = NULL;
00077 #endif
00078 }
00079
00080 namespace EMAN
00081 {
00082 class Region;
00083 class ImageIO;
00084
00085 class EMUtil
00086 {
00087 public:
00092 enum EMDataType
00093 {
00094 EM_UNKNOWN,
00095 EM_CHAR,
00096 EM_UCHAR,
00097 EM_SHORT,
00098 EM_USHORT,
00099 EM_INT,
00100 EM_UINT,
00101 EM_FLOAT,
00102 EM_DOUBLE,
00103 EM_SHORT_COMPLEX,
00104 EM_USHORT_COMPLEX,
00105 EM_FLOAT_COMPLEX
00106 };
00107
00110 enum ImageType
00111 {
00112 IMAGE_UNKNOWN,
00113 IMAGE_MRC,
00114 IMAGE_SPIDER,
00115 IMAGE_SINGLE_SPIDER,
00116 IMAGE_IMAGIC,
00117 IMAGE_HDF,
00118 IMAGE_DM3,
00119 IMAGE_TIFF,
00120 IMAGE_PGM,
00121 IMAGE_LST,
00122 IMAGE_PIF,
00123 IMAGE_VTK,
00124 IMAGE_PNG,
00125 IMAGE_SAL,
00126 IMAGE_ICOS,
00127 IMAGE_EMIM,
00128 IMAGE_GATAN2,
00129 IMAGE_AMIRA,
00130 IMAGE_XPLOR,
00131 IMAGE_EM,
00132 IMAGE_V4L,
00133 IMAGE_JPEG,
00134 IMAGE_FITS,
00135 IMAGE_LSTFAST,
00136 IMAGE_DF3,
00137 IMAGE_OMAP
00138 };
00139
00140 static EMData *vertical_acf(const EMData * image, int maxdy);
00141
00142 static EMData *make_image_median(const vector < EMData * >&image_list);
00143
00148 static ImageType get_image_ext_type(const string & file_ext);
00149
00154 static ImageType get_image_type(const string & filename);
00155
00162 static bool is_valid_filename(const string & filename);
00163
00168 static int get_image_count(const string & filename);
00169
00177 static ImageIO *get_imageio(const string & filename, int rw_mode,
00178 ImageType image_type = IMAGE_UNKNOWN);
00179
00184 static const char *get_imagetype_name(EMUtil::ImageType type);
00185
00190 static const char *get_datatype_string(EMDataType type);
00191
00201 static void get_region_dims(const Region * area, int nx, int *area_x, int ny,
00202 int *area_y, int nz = 1, int *area_z = 0);
00203
00212 static void get_region_origins(const Region * area, int *p_x0, int *p_y0,
00213 int *p_z0 = 0, int nz = 1, int image_index = 0);
00214
00234 static void process_region_io(void *cdata, FILE * file, int rw_mode,
00235 int image_index, size_t mode_size, int nx,
00236 int ny, int nz = 1, const Region * area = 0,
00237 bool need_flip = false, ImageType imgtype=IMAGE_UNKNOWN,
00238 int pre_row = 0, int post_row = 0);
00239
00240
00246 static void process_ascii_region_io(float *data, FILE * file, int rw_mode,
00247 int image_index, size_t mode_size,
00248 int nx, int ny, int nz,
00249 const Region * area, bool has_index_line,
00250 int nitems_per_line, const char *outformat);
00251
00252
00256 static void dump_dict(const Dict & dict);
00257
00263 static bool is_same_size(const EMData * image1, const EMData * image2);
00264
00270 static bool is_same_ctf(const EMData * image1, const EMData * image2);
00271
00272
00273 static bool is_complex_type(EMDataType datatype);
00274
00275 static void jump_lines(FILE * file, int nlines);
00276
00277 static vector<string> get_euler_names(const string & euler_type);
00278
00286 static vector<EMObject> get_all_attributes(const string & file_name, const string & attr_name);
00287
00301 static void getRenderMinMax(float * data, const int nx, const int ny, float& rendermin, float& rendermax, const int nz = 1);
00302
00303 #ifdef EM_HDF5
00304
00311 static EMObject read_hdf_attribute(const string & filename, const string & key, int image_index=0);
00312
00321 static int write_hdf_attribute(const string & filename, const string & key, EMObject value, int image_index=0);
00322
00330 static int delete_hdf_attribute(const string & filename, const string & key, int image_index=0);
00331 #endif //EM_HDF5
00332
00333 static bool cuda_available() {
00334
00335
00336
00337 return false;
00338
00339 }
00340
00341 inline static void* em_malloc(const size_t size) {
00342 return malloc(size);
00343 }
00344
00345 inline static void* em_calloc(const size_t nmemb,const size_t size) {
00346 return calloc(nmemb,size);
00347 }
00348
00349 inline static void* em_realloc(void* data,const size_t new_size) {
00350 return realloc(data, new_size);
00351 }
00352 inline static void em_memset(void* data, const int value, const size_t size) {
00353 memset(data, value, size);
00354 }
00355 inline static void em_free(void*data) {
00356 free(data);
00357 }
00358
00359 inline static void em_memcpy(void* dst,const void* const src,const size_t size) {
00360 memcpy(dst,src,size);
00361 }
00362 private:
00363 static ImageType fast_get_image_type(const string & filename,
00364 const void *first_block,
00365 off_t file_size);
00366
00367 static void jump_lines_by_items(FILE * file, int nitems, int nitems_per_line);
00368
00369
00370
00371 static void process_numbers_io(FILE * file, int rw_mode,
00372 int nitems_per_line,
00373 size_t mode_size, int start, int end,
00374 float *data, int *p_i,
00375 const char *outformat);
00376
00377 static void exclude_numbers_io(FILE * file, int rw_mode,
00378 int nitems_per_line,
00379 size_t mode_size, int start, int end,
00380 float * data, int *p_i,
00381 const char *outformat);
00382
00383 static void process_lines_io(FILE * file, int rw_mode,
00384 int nitems_per_line, size_t mode_size,
00385 int nitems, float *data, int *p_i,
00386 const char *outformat);
00387
00388
00389 };
00390
00391 struct ImageScore {
00392 int index;
00393 float score;
00394 ImageScore(int i=0, float s=0) : index(i), score(s) {}
00395 };
00396
00397 class ImageSort {
00398 public:
00399 ImageSort(int n);
00400 ~ImageSort();
00401
00402 void sort();
00403
00404 void set(int i, float score);
00405 int get_index(int i) const;
00406 float get_score(int i) const;
00407
00408 int size() const;
00409 private:
00410 ImageScore* image_scores;
00411 int n;
00412
00413 };
00414 }
00415
00416 #endif //eman__emutil__h__