Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

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                         IMAGE_SER
00140                 };
00141 
00142                 static EMData *vertical_acf(const EMData * image, int maxdy);
00143 
00144                 static EMData *make_image_median(const vector < EMData * >&image_list);
00145 
00150                 static ImageType get_image_ext_type(const string & file_ext);
00151 
00156                 static ImageType get_image_type(const string & filename);
00157 
00164                 static bool is_valid_filename(const string & filename);
00165 
00170                 static int get_image_count(const string & filename);
00171 
00179                 static ImageIO *get_imageio(const string & filename, int rw_mode,
00180                                                                         ImageType image_type = IMAGE_UNKNOWN);
00181 
00186                 static const char *get_imagetype_name(EMUtil::ImageType type);
00187 
00192                 static const char *get_datatype_string(EMDataType type);
00193 
00203                 static void get_region_dims(const Region * area, int nx, int *area_x, int ny,
00204                                                                         int *area_y, int nz = 1, int *area_z = 0);
00205 
00214                 static void get_region_origins(const Region * area, int *p_x0, int *p_y0,
00215                                                                            int *p_z0 = 0, int nz = 1, int image_index = 0);
00216 
00236                 static void process_region_io(void *cdata, FILE * file, int rw_mode,
00237                                                                           int image_index, size_t mode_size, int nx,
00238                                                                           int ny, int nz = 1, const Region * area = 0,
00239                                                                           bool need_flip = false, ImageType imgtype=IMAGE_UNKNOWN,
00240                                                                           int pre_row = 0, int post_row = 0);
00241 
00242 
00248                 static void process_ascii_region_io(float *data, FILE * file, int rw_mode,
00249                                                                                         int image_index, size_t mode_size,
00250                                                                                         int nx, int ny, int nz,
00251                                                                                         const Region * area, bool has_index_line,
00252                                                                                         int nitems_per_line, const char *outformat);
00253 
00254 
00258                 static void dump_dict(const Dict & dict);
00259 
00265                 static bool is_same_size(const EMData * image1, const EMData * image2);
00266 
00272                 static bool is_same_ctf(const EMData * image1, const EMData * image2);
00273 
00274 
00275                 static bool is_complex_type(EMDataType datatype);
00276 
00277                 static void jump_lines(FILE * file, int nlines);
00278 
00279         static vector<string> get_euler_names(const string & euler_type);
00280 
00288                 static vector<EMObject> get_all_attributes(const string & file_name, const string & attr_name);
00289 
00303                 static void getRenderMinMax(float * data, const int nx, const int ny, float& rendermin, float& rendermax, const int nz = 1);
00304                 
00305 #ifdef EM_HDF5
00306 
00313                 static EMObject read_hdf_attribute(const string & filename, const string & key, int image_index=0);
00314 
00323                 static int write_hdf_attribute(const string & filename, const string & key, EMObject value, int image_index=0);
00324 
00332                 static int delete_hdf_attribute(const string & filename, const string & key, int image_index=0);
00333 #endif  //EM_HDF5
00334 
00335                 static bool cuda_available() {
00336 //#ifdef EMAN2_USING_CUDA
00337 //                      return true;
00338 //#else
00339                         return false;
00340 //#endif
00341                 }
00342 
00343                 inline static void* em_malloc(const size_t size) {
00344                         return malloc(size);
00345                 }
00346 
00347                 inline static void* em_calloc(const size_t nmemb,const size_t size) {
00348                         return calloc(nmemb,size);
00349                 }
00350 
00351                 inline static void* em_realloc(void* data,const size_t new_size) {
00352                         return realloc(data, new_size);
00353                 }
00354                 inline static void em_memset(void* data, const int value, const size_t size) {
00355                         memset(data, value, size);
00356                 }
00357                 inline static void em_free(void*data) {
00358                         free(data);
00359                 }
00360 
00361                 inline static void em_memcpy(void* dst,const void* const src,const size_t size) {
00362                         memcpy(dst,src,size);
00363                 }
00364           private:
00365                 static ImageType fast_get_image_type(const string & filename,
00366                                                                                          const void *first_block,
00367                                                                                          off_t file_size);
00368 
00369                 static void jump_lines_by_items(FILE * file, int nitems, int nitems_per_line);
00370 
00371 
00372 
00373                 static void process_numbers_io(FILE * file, int rw_mode,
00374                                                                            int nitems_per_line,
00375                                                                            size_t mode_size, int start, int end,
00376                                                                            float *data, int *p_i,
00377                                                                            const char *outformat);
00378 
00379                 static void exclude_numbers_io(FILE * file, int rw_mode,
00380                                                                            int nitems_per_line,
00381                                                                            size_t mode_size, int start, int end,
00382                                                                            float * data, int *p_i,
00383                                                                            const char *outformat);
00384 
00385                 static void process_lines_io(FILE * file, int rw_mode,
00386                                                                          int nitems_per_line, size_t mode_size,
00387                                                                          int nitems, float *data, int *p_i,
00388                                                                          const char *outformat);
00389 
00390 
00391         };
00392 
00393         struct ImageScore {
00394                 int index;
00395                 float score;
00396                 ImageScore(int i=0, float s=0) : index(i), score(s) {}
00397         };
00398 
00399         class ImageSort {
00400         public:
00401                 ImageSort(int n);
00402                 ~ImageSort();
00403 
00404                 void sort();
00405 
00406                 void set(int i, float score);
00407                 int get_index(int i) const;
00408                 float get_score(int i) const;
00409 
00410                 int size() const;
00411         private:
00412                 ImageScore* image_scores;
00413                 int n;
00414 
00415         };
00416 }
00417 
00418 #endif  //eman__emutil__h__

Generated on Mon Aug 13 13:40:19 2012 for EMAN2 by  doxygen 1.3.9.1