EMAN2
emutil.h
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifndef eman__emutil__h__
33#define eman__emutil__h__ 1
34
35#include <cstring>
36#include "emobject.h"
37#include "emassert.h"
38
39using std::string;
40using std::vector;
41
42// Defining EMDelete using templates
43// Use EMDelete instead of delete as this will be very clean.
44// This should perhaps be moved to somewhere else later to make it
45// more widely accessible. (C. Yang 04/27/06)
46template <class T>
47inline void EMDeletePtr(T & x)
48{
49#ifdef _WIN32
50 if(x != NULL) {
51 delete x;
52 x = NULL;
53 }
54#else
55 {Assert(x != NULL);}
56 delete x;
57 x = NULL;
58#endif
59}
60
61template <class T>
62inline void EMDeleteArray(T & x)
63{
64#ifdef _WIN32
65 if(x != NULL) {
66 delete x;
67 x = NULL;
68 }
69#else
70 {Assert(x != NULL);}
71 delete [] x;
72 x = NULL;
73#endif
74}
75
76namespace EMAN
77{
78 class Region;
79 class ImageIO;
80
81 // Was going to make this a static member of EMUtil since it is tied to EMDataType, but couldn't sort out the syntax
82 const int EMDataTypeBits[] = { 0,8,8,16,16,32,32,32,64,32,32,64,0 };
83
84 class EMUtil
85 {
86 public:
92 {
105 EM_COMPRESSED // compressed
106 };
107
108
112 {
145 };
146
147 static EMData *vertical_acf(const EMData * image, int maxdy);
148
153 static ImageType get_image_ext_type(const string & file_ext);
154
159 static ImageType get_image_type(const string & filename);
160
167 static bool is_valid_filename(const string & filename);
168
173 static int get_image_count(const string & filename);
174
182 static ImageIO *get_imageio(const string & filename, int rw_mode,
183 ImageType image_type = IMAGE_UNKNOWN);
184
186 static void close_imageio(const string & filename, const ImageIO * io);
187
192 static const char *get_imagetype_name(EMUtil::ImageType type);
193
198 static const char *get_datatype_string(EMDataType type);
199
209 static void get_region_dims(const Region * area, int nx, int *area_x, int ny,
210 int *area_y, int nz = 1, int *area_z = 0);
211
220 static void get_region_origins(const Region * area, int *p_x0, int *p_y0,
221 int *p_z0 = 0, int nz = 1, int image_index = 0);
222
227 static double mode_size_product(size_t factor, size_t mode_size);
228
248 static void process_region_io(void *cdata, FILE * file, int rw_mode,
249 int image_index, size_t mode_size, int nx,
250 int ny, int nz = 1, const Region * area = 0,
251 bool need_flip = false, ImageType imgtype=IMAGE_UNKNOWN,
252 int pre_row = 0, int post_row = 0);
253
254
260 static void process_ascii_region_io(float *data, FILE * file, int rw_mode,
261 int image_index, size_t mode_size,
262 int nx, int ny, int nz,
263 const Region * area, bool has_index_line,
264 int nitems_per_line, const char *outformat);
265
266
270 static void dump_dict(const Dict & dict);
271
277 static bool is_same_size(const EMData * image1, const EMData * image2);
278
284 static bool is_same_ctf(const EMData * image1, const EMData * image2);
285
286
287 static bool is_complex_type(EMDataType datatype);
288
289 static void jump_lines(FILE * file, int nlines);
290
291 static vector<string> get_euler_names(const string & euler_type);
292
300 static vector<EMObject> get_all_attributes(const string & file_name, const string & attr_name);
301
310 static void getRenderLimits(const Dict & dict, float & rendermin, float & rendermax, int & renderbits);
311
326 static void getRenderMinMax(float * data, const int nx, const int ny, float & rendermin, float & rendermax, int &renderbits, const int nz = 1);
327
328#ifdef USE_HDF5
336 static EMObject read_hdf_attribute(const string & filename, const string & key, int image_index=0);
337
346 static int write_hdf_attribute(const string & filename, const string & key, EMObject value, int image_index=0);
347
355 static int delete_hdf_attribute(const string & filename, const string & key, int image_index=0);
356#endif //USE_HDF5
357
358 static bool cuda_available() {
359//#ifdef EMAN2_USING_CUDA
360// return true;
361//#else
362 return false;
363//#endif
364 }
365
366 inline static void* em_malloc(const size_t size) {
367 return malloc(size);
368 }
369
370 inline static void* em_calloc(const size_t nmemb,const size_t size) {
371 return calloc(nmemb,size);
372 }
373
374 inline static void* em_realloc(void* data,const size_t new_size) {
375 return realloc(data, new_size);
376 }
377 inline static void em_memset(void* data, const int value, const size_t size) {
378 memset(data, value, size);
379 }
380 inline static void em_free(void*data) {
381 free(data);
382 }
383
384 inline static void em_memcpy(void* dst,const void* const src,const size_t size) {
385 memcpy(dst,src,size);
386 }
387 private:
388 static ImageType fast_get_image_type(const string & filename,
389 const void *first_block,
390 off_t file_size);
391
392 static void jump_lines_by_items(FILE * file, int nitems, int nitems_per_line);
393
394
395
396 static void process_numbers_io(FILE * file, int rw_mode,
397 int nitems_per_line,
398 size_t mode_size, int start, int end,
399 float *data, int *p_i,
400 const char *outformat);
401
402 static void exclude_numbers_io(FILE * file, int rw_mode,
403 int nitems_per_line,
404 size_t mode_size, int start, int end,
405 float * data, int *p_i,
406 const char *outformat);
407
408 static void process_lines_io(FILE * file, int rw_mode,
409 int nitems_per_line, size_t mode_size,
410 int nitems, float *data, int *p_i,
411 const char *outformat);
412
413
414 };
415
416 struct ImageScore {
417 int index;
418 float score;
419 ImageScore(int i=0, float s=0) : index(i), score(s) {}
420 };
421
422 class ImageSort {
423 public:
424 ImageSort(int n);
425 ~ImageSort();
426
427 void sort();
428
429 void set(int i, float score);
430 int get_index(int i) const;
431 float get_score(int i) const;
432
433 int size() const;
434 private:
436 int n;
437 };
438}
439
440#endif //eman__emutil__h__
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
EMData stores an image's data and defines core image processing routines.
Definition: emdata.h:82
EMObject is a wrapper class for types including int, float, double, etc as defined in ObjectType.
Definition: emobject.h:123
static void process_region_io(void *cdata, FILE *file, int rw_mode, int image_index, size_t mode_size, int nx, int ny, int nz=1, const Region *area=0, bool need_flip=false, ImageType imgtype=IMAGE_UNKNOWN, int pre_row=0, int post_row=0)
Process image region IO.
Definition: emutil.cpp:931
static void em_memset(void *data, const int value, const size_t size)
Definition: emutil.h:377
static void em_memcpy(void *dst, const void *const src, const size_t size)
Definition: emutil.h:384
static void dump_dict(const Dict &dict)
Dump a Dict object.
Definition: emutil.cpp:1204
static void close_imageio(const string &filename, const ImageIO *io)
Ian: Close ImageIO object.
Definition: emutil.cpp:720
static void jump_lines_by_items(FILE *file, int nitems, int nitems_per_line)
Definition: emutil.cpp:1473
static int get_image_count(const string &filename)
Get the number of images in an image file.
Definition: emutil.cpp:534
static vector< EMObject > get_all_attributes(const string &file_name, const string &attr_name)
Get an attribute from a stack of image, returned as a vector.
Definition: emutil.cpp:1674
EMDataType
Image pixel data type used in EMAN.
Definition: emutil.h:92
@ EM_UCHAR
Definition: emutil.h:95
@ EM_USHORT_COMPLEX
Definition: emutil.h:103
@ EM_COMPRESSED
Definition: emutil.h:105
@ EM_UNKNOWN
Definition: emutil.h:93
@ EM_USHORT
Definition: emutil.h:97
@ EM_FLOAT_COMPLEX
Definition: emutil.h:104
@ EM_SHORT_COMPLEX
Definition: emutil.h:102
@ EM_SHORT
Definition: emutil.h:96
static bool is_same_size(const EMData *image1, const EMData *image2)
Check whether two EMData images are of the same size.
Definition: emutil.cpp:1224
static ImageIO * get_imageio(const string &filename, int rw_mode, ImageType image_type=IMAGE_UNKNOWN)
Get an ImageIO object.
Definition: emutil.cpp:558
static void process_numbers_io(FILE *file, int rw_mode, int nitems_per_line, size_t mode_size, int start, int end, float *data, int *p_i, const char *outformat)
Definition: emutil.cpp:1508
static bool is_complex_type(EMDataType datatype)
Definition: emutil.cpp:1231
static EMData * vertical_acf(const EMData *image, int maxdy)
Definition: emutil.cpp:1238
static void getRenderMinMax(float *data, const int nx, const int ny, float &rendermin, float &rendermax, int &renderbits, const int nz=1)
Calculate the min and max pixel value accepted for image nomalization, if we did not get them from im...
Definition: emutil.cpp:1706
static void * em_calloc(const size_t nmemb, const size_t size)
Definition: emutil.h:370
ImageType
Image format types.
Definition: emutil.h:112
@ IMAGE_SINGLE_SPIDER
Definition: emutil.h:119
@ IMAGE_XPLOR
Definition: emutil.h:135
@ IMAGE_EMIM
Definition: emutil.h:132
@ IMAGE_IMAGIC
Definition: emutil.h:120
@ IMAGE_LSTFAST
Definition: emutil.h:140
@ IMAGE_UNKNOWN
Definition: emutil.h:113
@ IMAGE_SITUS
Definition: emutil.h:143
@ IMAGE_OMAP
Definition: emutil.h:142
@ IMAGE_EER4X
Definition: emutil.h:117
@ IMAGE_SPIDER
Definition: emutil.h:118
@ IMAGE_EER2X
Definition: emutil.h:116
@ IMAGE_GATAN2
Definition: emutil.h:133
@ IMAGE_JPEG
Definition: emutil.h:138
@ IMAGE_AMIRA
Definition: emutil.h:134
@ IMAGE_FITS
Definition: emutil.h:139
@ IMAGE_TIFF
Definition: emutil.h:124
@ IMAGE_ICOS
Definition: emutil.h:131
static ImageType get_image_ext_type(const string &file_ext)
Get an image's format type from its filename extension.
Definition: emutil.cpp:58
static void getRenderLimits(const Dict &dict, float &rendermin, float &rendermax, int &renderbits)
Get the min and max pixel value accepted for image nomalization from image attribute dictionary,...
Definition: emutil.cpp:1689
static bool is_valid_filename(const string &filename)
Ask whether or not the given filename is a valid EM image filename This is the same thing as checking...
Definition: emutil.cpp:222
static ImageType fast_get_image_type(const string &filename, const void *first_block, off_t file_size)
Definition: emutil.cpp:229
static void get_region_origins(const Region *area, int *p_x0, int *p_y0, int *p_z0=0, int nz=1, int image_index=0)
Get a region's original locations.
Definition: emutil.cpp:891
static void exclude_numbers_io(FILE *file, int rw_mode, int nitems_per_line, size_t mode_size, int start, int end, float *data, int *p_i, const char *outformat)
Definition: emutil.cpp:1554
static void process_ascii_region_io(float *data, FILE *file, int rw_mode, int image_index, size_t mode_size, int nx, int ny, int nz, const Region *area, bool has_index_line, int nitems_per_line, const char *outformat)
Works for regions that are outside the image data dimension area.
Definition: emutil.cpp:1365
static void em_free(void *data)
Definition: emutil.h:380
static vector< string > get_euler_names(const string &euler_type)
Definition: emutil.cpp:1631
static bool is_same_ctf(const EMData *image1, const EMData *image2)
Check whether two EMData images have the same CTF parameters.
Definition: emutil.cpp:1275
static void * em_realloc(void *data, const size_t new_size)
Definition: emutil.h:374
static const char * get_imagetype_name(EMUtil::ImageType type)
Give each image type a meaningful name.
Definition: emutil.cpp:734
static ImageType get_image_type(const string &filename)
Get an image's format type by processing the first 1K of the image.
Definition: emutil.cpp:396
static void get_region_dims(const Region *area, int nx, int *area_x, int ny, int *area_y, int nz=1, int *area_z=0)
Get a region's dimensions.
Definition: emutil.cpp:860
static void jump_lines(FILE *file, int nlines)
Definition: emutil.cpp:1493
static double mode_size_product(size_t factor, size_t mode_size)
Return a factor times the mode size, which may be a special value (11111111) meaning one half,...
Definition: emutil.cpp:915
static void process_lines_io(FILE *file, int rw_mode, int nitems_per_line, size_t mode_size, int nitems, float *data, int *p_i, const char *outformat)
Definition: emutil.cpp:1612
static const char * get_datatype_string(EMDataType type)
Give each data type a meaningful name.
Definition: emutil.cpp:828
static bool cuda_available()
Definition: emutil.h:358
static void * em_malloc(const size_t size)
Definition: emutil.h:366
ImageIO classes are designed for reading/writing various electron micrography image formats,...
Definition: imageio.h:127
ImageSort(int n)
Definition: emutil.cpp:1322
ImageScore * image_scores
Definition: emutil.h:435
float get_score(int i) const
Definition: emutil.cpp:1354
int size() const
Definition: emutil.cpp:1360
int get_index(int i) const
Definition: emutil.cpp:1348
void set(int i, float score)
Definition: emutil.cpp:1342
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
Definition: geometry.h:497
#define Assert(s)
Define Assert() function that is effective only when -DDEBUG is used.
Definition: emassert.h:42
void EMDeletePtr(T &x)
Definition: emutil.h:47
void EMDeleteArray(T &x)
Definition: emutil.h:62
E2Exception class.
Definition: aligner.h:40
const int EMDataTypeBits[]
Definition: emutil.h:82
#define x(i)
Definition: projector.cpp:1517
ImageScore(int i=0, float s=0)
Definition: emutil.h:419