EMAN2
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_DM4,
00120                         IMAGE_TIFF,
00121                         IMAGE_PGM,
00122                         IMAGE_LST,
00123                         IMAGE_PIF,
00124                         IMAGE_VTK,
00125                         IMAGE_PNG,
00126                         IMAGE_SAL,
00127                         IMAGE_ICOS,
00128                         IMAGE_EMIM,
00129                         IMAGE_GATAN2,
00130                         IMAGE_AMIRA,
00131                         IMAGE_XPLOR,
00132                         IMAGE_EM,
00133                         IMAGE_V4L,
00134                         IMAGE_JPEG,
00135                         IMAGE_FITS,
00136                         IMAGE_LSTFAST,
00137                         IMAGE_DF3,
00138                         IMAGE_OMAP,
00139                         IMAGE_SITUS,
00140                         IMAGE_SER
00141                 };
00142 
00143                 static EMData *vertical_acf(const EMData * image, int maxdy);
00144 
00145                 static EMData *make_image_median(const vector < EMData * >&image_list);
00146 
00151                 static ImageType get_image_ext_type(const string & file_ext);
00152 
00157                 static ImageType get_image_type(const string & filename);
00158 
00165                 static bool is_valid_filename(const string & filename);
00166 
00171                 static int get_image_count(const string & filename);
00172 
00180                 static ImageIO *get_imageio(const string & filename, int rw_mode,
00181                                                                         ImageType image_type = IMAGE_UNKNOWN);
00182 
00184       static void close_imageio(const string & filename, const ImageIO * io);
00185 
00190                 static const char *get_imagetype_name(EMUtil::ImageType type);
00191 
00196                 static const char *get_datatype_string(EMDataType type);
00197 
00207                 static void get_region_dims(const Region * area, int nx, int *area_x, int ny,
00208                                                                         int *area_y, int nz = 1, int *area_z = 0);
00209 
00218                 static void get_region_origins(const Region * area, int *p_x0, int *p_y0,
00219                                                                            int *p_z0 = 0, int nz = 1, int image_index = 0);
00220 
00240                 static void process_region_io(void *cdata, FILE * file, int rw_mode,
00241                                                                           int image_index, size_t mode_size, int nx,
00242                                                                           int ny, int nz = 1, const Region * area = 0,
00243                                                                           bool need_flip = false, ImageType imgtype=IMAGE_UNKNOWN,
00244                                                                           int pre_row = 0, int post_row = 0);
00245 
00246 
00252                 static void process_ascii_region_io(float *data, FILE * file, int rw_mode,
00253                                                                                         int image_index, size_t mode_size,
00254                                                                                         int nx, int ny, int nz,
00255                                                                                         const Region * area, bool has_index_line,
00256                                                                                         int nitems_per_line, const char *outformat);
00257 
00258 
00262                 static void dump_dict(const Dict & dict);
00263 
00269                 static bool is_same_size(const EMData * image1, const EMData * image2);
00270 
00276                 static bool is_same_ctf(const EMData * image1, const EMData * image2);
00277 
00278 
00279                 static bool is_complex_type(EMDataType datatype);
00280 
00281                 static void jump_lines(FILE * file, int nlines);
00282 
00283         static vector<string> get_euler_names(const string & euler_type);
00284 
00292                 static vector<EMObject> get_all_attributes(const string & file_name, const string & attr_name);
00293 
00307                 static void getRenderMinMax(float * data, const int nx, const int ny, float& rendermin, float& rendermax, const int nz = 1);
00308                 
00309 #ifdef EM_HDF5
00310 
00317                 static EMObject read_hdf_attribute(const string & filename, const string & key, int image_index=0);
00318 
00327                 static int write_hdf_attribute(const string & filename, const string & key, EMObject value, int image_index=0);
00328 
00336                 static int delete_hdf_attribute(const string & filename, const string & key, int image_index=0);
00337 #endif  //EM_HDF5
00338 
00339                 static bool cuda_available() {
00340 //#ifdef EMAN2_USING_CUDA
00341 //                      return true;
00342 //#else
00343                         return false;
00344 //#endif
00345                 }
00346 
00347                 inline static void* em_malloc(const size_t size) {
00348                         return malloc(size);
00349                 }
00350 
00351                 inline static void* em_calloc(const size_t nmemb,const size_t size) {
00352                         return calloc(nmemb,size);
00353                 }
00354 
00355                 inline static void* em_realloc(void* data,const size_t new_size) {
00356                         return realloc(data, new_size);
00357                 }
00358                 inline static void em_memset(void* data, const int value, const size_t size) {
00359                         memset(data, value, size);
00360                 }
00361                 inline static void em_free(void*data) {
00362                         free(data);
00363                 }
00364 
00365                 inline static void em_memcpy(void* dst,const void* const src,const size_t size) {
00366                         memcpy(dst,src,size);
00367                 }
00368           private:
00369                 static ImageType fast_get_image_type(const string & filename,
00370                                                                                          const void *first_block,
00371                                                                                          off_t file_size);
00372 
00373                 static void jump_lines_by_items(FILE * file, int nitems, int nitems_per_line);
00374 
00375 
00376 
00377                 static void process_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 exclude_numbers_io(FILE * file, int rw_mode,
00384                                                                            int nitems_per_line,
00385                                                                            size_t mode_size, int start, int end,
00386                                                                            float * data, int *p_i,
00387                                                                            const char *outformat);
00388 
00389                 static void process_lines_io(FILE * file, int rw_mode,
00390                                                                          int nitems_per_line, size_t mode_size,
00391                                                                          int nitems, float *data, int *p_i,
00392                                                                          const char *outformat);
00393 
00394 
00395         };
00396 
00397         struct ImageScore {
00398                 int index;
00399                 float score;
00400                 ImageScore(int i=0, float s=0) : index(i), score(s) {}
00401         };
00402 
00403         class ImageSort {
00404         public:
00405                 ImageSort(int n);
00406                 ~ImageSort();
00407 
00408                 void sort();
00409 
00410                 void set(int i, float score);
00411                 int get_index(int i) const;
00412                 float get_score(int i) const;
00413 
00414                 int size() const;
00415         private:
00416                 ImageScore* image_scores;
00417                 int n;
00418 
00419         };
00420 }
00421 
00422 #endif  //eman__emutil__h__