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                 };
00137 
00138                 static EMData *vertical_acf(const EMData * image, int maxdy);
00139 
00140                 static EMData *make_image_median(const vector < EMData * >&image_list);
00141 
00146                 static ImageType get_image_ext_type(const string & file_ext);
00147 
00152                 static ImageType get_image_type(const string & filename);
00153 
00160                 static bool is_valid_filename(const string & filename);
00161 
00166                 static int get_image_count(const string & filename);
00167 
00175                 static ImageIO *get_imageio(const string & filename, int rw_mode,
00176                                                                         ImageType image_type = IMAGE_UNKNOWN);
00177 
00182                 static const char *get_imagetype_name(EMUtil::ImageType type);
00183 
00188                 static const char *get_datatype_string(EMDataType type);
00189 
00199                 static void get_region_dims(const Region * area, int nx, int *area_x, int ny,
00200                                                                         int *area_y, int nz = 1, int *area_z = 0);
00201 
00210                 static void get_region_origins(const Region * area, int *p_x0, int *p_y0,
00211                                                                            int *p_z0 = 0, int nz = 1, int image_index = 0);
00212 
00232                 static void process_region_io(void *cdata, FILE * file, int rw_mode,
00233                                                                           int image_index, size_t mode_size, int nx,
00234                                                                           int ny, int nz = 1, const Region * area = 0,
00235                                                                           bool need_flip = false, ImageType imgtype=IMAGE_UNKNOWN,
00236                                                                           int pre_row = 0, int post_row = 0);
00237 
00238 
00244                 static void process_ascii_region_io(float *data, FILE * file, int rw_mode,
00245                                                                                         int image_index, size_t mode_size,
00246                                                                                         int nx, int ny, int nz,
00247                                                                                         const Region * area, bool has_index_line,
00248                                                                                         int nitems_per_line, const char *outformat);
00249 
00250 
00254                 static void dump_dict(const Dict & dict);
00255 
00261                 static bool is_same_size(const EMData * image1, const EMData * image2);
00262 
00268                 static bool is_same_ctf(const EMData * image1, const EMData * image2);
00269 
00270 
00271                 static bool is_complex_type(EMDataType datatype);
00272 
00273                 static void jump_lines(FILE * file, int nlines);
00274 
00275         static vector<string> get_euler_names(const string & euler_type);
00276 
00284                 static vector<EMObject> get_all_attributes(const string & file_name, const string & attr_name);
00285 
00299                 static void getRenderMinMax(float * data, const int nx, const int ny, float& rendermin, float& rendermax, const int nz = 1);
00300                 
00301 
00302                 static bool cuda_available() {
00303 #ifdef EMAN2_USING_CUDA
00304                         return true;
00305 #else
00306                         return false;
00307 #endif
00308                 }
00309 
00310                 inline static void* em_malloc(const size_t size) {
00311                         return malloc(size);
00312                 }
00313 
00314                 inline static void* em_calloc(const size_t nmemb,const size_t size) {
00315                         return calloc(nmemb,size);
00316                 }
00317 
00318                 inline static void* em_realloc(void* data,const size_t new_size) {
00319                         return realloc(data, new_size);
00320                 }
00321                 inline static void em_memset(void* data, const int value, const size_t size) {
00322                         memset(data, value, size);
00323                 }
00324                 inline static void em_free(void*data) {
00325                         free(data);
00326                 }
00327 
00328                 inline static void em_memcpy(void* dst,const void* const src,const size_t size) {
00329                         memcpy(dst,src,size);
00330                 }
00331           private:
00332                 static ImageType fast_get_image_type(const string & filename,
00333                                                                                          const void *first_block,
00334                                                                                          off_t file_size);
00335 
00336                 static void jump_lines_by_items(FILE * file, int nitems, int nitems_per_line);
00337 
00338 
00339 
00340                 static void process_numbers_io(FILE * file, int rw_mode,
00341                                                                            int nitems_per_line,
00342                                                                            size_t mode_size, int start, int end,
00343                                                                            float *data, int *p_i,
00344                                                                            const char *outformat);
00345 
00346                 static void exclude_numbers_io(FILE * file, int rw_mode,
00347                                                                            int nitems_per_line,
00348                                                                            size_t mode_size, int start, int end,
00349                                                                            float * data, int *p_i,
00350                                                                            const char *outformat);
00351 
00352                 static void process_lines_io(FILE * file, int rw_mode,
00353                                                                          int nitems_per_line, size_t mode_size,
00354                                                                          int nitems, float *data, int *p_i,
00355                                                                          const char *outformat);
00356 
00357 
00358         };
00359 
00360         struct ImageScore {
00361                 int index;
00362                 float score;
00363                 ImageScore(int i=0, float s=0) : index(i), score(s) {}
00364         };
00365 
00366         class ImageSort {
00367         public:
00368                 ImageSort(int n);
00369                 ~ImageSort();
00370 
00371                 void sort();
00372 
00373                 void set(int i, float score);
00374                 int get_index(int i) const;
00375                 float get_score(int i) const;
00376 
00377                 int size() const;
00378         private:
00379                 ImageScore* image_scores;
00380                 int n;
00381 
00382         };
00383 }
00384 
00385 #endif  //eman__emutil__h__

Generated on Tue May 25 17:13:32 2010 for EMAN2 by  doxygen 1.4.7