emutil.h

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 // Defining EMDelete using templates
00047 // Use EMDelete instead of delete as this will be very clean.
00048 // This should perhaps be moved to somewhere else later to make it
00049 // more widely accessible. (C. Yang 04/27/06)
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                         IMAGE_SITUS
00139                 };
00140 
00141                 static EMData *vertical_acf(const EMData * image, int maxdy);
00142 
00143                 static EMData *make_image_median(const vector < EMData * >&image_list);
00144 
00149                 static ImageType get_image_ext_type(const string & file_ext);
00150 
00155                 static ImageType get_image_type(const string & filename);
00156 
00163                 static bool is_valid_filename(const string & filename);
00164 
00169                 static int get_image_count(const string & filename);
00170 
00178                 static ImageIO *get_imageio(const string & filename, int rw_mode,
00179                                                                         ImageType image_type = IMAGE_UNKNOWN);
00180 
00185                 static const char *get_imagetype_name(EMUtil::ImageType type);
00186 
00191                 static const char *get_datatype_string(EMDataType type);
00192 
00202                 static void get_region_dims(const Region * area, int nx, int *area_x, int ny,
00203                                                                         int *area_y, int nz = 1, int *area_z = 0);
00204 
00213                 static void get_region_origins(const Region * area, int *p_x0, int *p_y0,
00214                                                                            int *p_z0 = 0, int nz = 1, int image_index = 0);
00215 
00235                 static void process_region_io(void *cdata, FILE * file, int rw_mode,
00236                                                                           int image_index, size_t mode_size, int nx,
00237                                                                           int ny, int nz = 1, const Region * area = 0,
00238                                                                           bool need_flip = false, ImageType imgtype=IMAGE_UNKNOWN,
00239                                                                           int pre_row = 0, int post_row = 0);
00240 
00241 
00247                 static void process_ascii_region_io(float *data, FILE * file, int rw_mode,
00248                                                                                         int image_index, size_t mode_size,
00249                                                                                         int nx, int ny, int nz,
00250                                                                                         const Region * area, bool has_index_line,
00251                                                                                         int nitems_per_line, const char *outformat);
00252 
00253 
00257                 static void dump_dict(const Dict & dict);
00258 
00264                 static bool is_same_size(const EMData * image1, const EMData * image2);
00265 
00271                 static bool is_same_ctf(const EMData * image1, const EMData * image2);
00272 
00273 
00274                 static bool is_complex_type(EMDataType datatype);
00275 
00276                 static void jump_lines(FILE * file, int nlines);
00277 
00278         static vector<string> get_euler_names(const string & euler_type);
00279 
00287                 static vector<EMObject> get_all_attributes(const string & file_name, const string & attr_name);
00288 
00302                 static void getRenderMinMax(float * data, const int nx, const int ny, float& rendermin, float& rendermax, const int nz = 1);
00303                 
00304 #ifdef EM_HDF5
00305 
00312                 static EMObject read_hdf_attribute(const string & filename, const string & key, int image_index=0);
00313 
00322                 static int write_hdf_attribute(const string & filename, const string & key, EMObject value, int image_index=0);
00323 
00331                 static int delete_hdf_attribute(const string & filename, const string & key, int image_index=0);
00332 #endif  //EM_HDF5
00333 
00334                 static bool cuda_available() {
00335 //#ifdef EMAN2_USING_CUDA
00336 //                      return true;
00337 //#else
00338                         return false;
00339 //#endif
00340                 }
00341 
00342                 inline static void* em_malloc(const size_t size) {
00343                         return malloc(size);
00344                 }
00345 
00346                 inline static void* em_calloc(const size_t nmemb,const size_t size) {
00347                         return calloc(nmemb,size);
00348                 }
00349 
00350                 inline static void* em_realloc(void* data,const size_t new_size) {
00351                         return realloc(data, new_size);
00352                 }
00353                 inline static void em_memset(void* data, const int value, const size_t size) {
00354                         memset(data, value, size);
00355                 }
00356                 inline static void em_free(void*data) {
00357                         free(data);
00358                 }
00359 
00360                 inline static void em_memcpy(void* dst,const void* const src,const size_t size) {
00361                         memcpy(dst,src,size);
00362                 }
00363           private:
00364                 static ImageType fast_get_image_type(const string & filename,
00365                                                                                          const void *first_block,
00366                                                                                          off_t file_size);
00367 
00368                 static void jump_lines_by_items(FILE * file, int nitems, int nitems_per_line);
00369 
00370 
00371 
00372                 static void process_numbers_io(FILE * file, int rw_mode,
00373                                                                            int nitems_per_line,
00374                                                                            size_t mode_size, int start, int end,
00375                                                                            float *data, int *p_i,
00376                                                                            const char *outformat);
00377 
00378                 static void exclude_numbers_io(FILE * file, int rw_mode,
00379                                                                            int nitems_per_line,
00380                                                                            size_t mode_size, int start, int end,
00381                                                                            float * data, int *p_i,
00382                                                                            const char *outformat);
00383 
00384                 static void process_lines_io(FILE * file, int rw_mode,
00385                                                                          int nitems_per_line, size_t mode_size,
00386                                                                          int nitems, float *data, int *p_i,
00387                                                                          const char *outformat);
00388 
00389 
00390         };
00391 
00392         struct ImageScore {
00393                 int index;
00394                 float score;
00395                 ImageScore(int i=0, float s=0) : index(i), score(s) {}
00396         };
00397 
00398         class ImageSort {
00399         public:
00400                 ImageSort(int n);
00401                 ~ImageSort();
00402 
00403                 void sort();
00404 
00405                 void set(int i, float score);
00406                 int get_index(int i) const;
00407                 float get_score(int i) const;
00408 
00409                 int size() const;
00410         private:
00411                 ImageScore* image_scores;
00412                 int n;
00413 
00414         };
00415 }
00416 
00417 #endif  //eman__emutil__h__

Generated on Thu Nov 17 12:42:58 2011 for EMAN2 by  doxygen 1.4.7