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

EMAN::EMUtil Class Reference

#include <emutil.h>

List of all members.

Public Types

enum  EMDataType {
  EM_UNKNOWN, EM_CHAR, EM_UCHAR, EM_SHORT,
  EM_USHORT, EM_INT, EM_UINT, EM_FLOAT,
  EM_DOUBLE, EM_SHORT_COMPLEX, EM_USHORT_COMPLEX, EM_FLOAT_COMPLEX
}
 Image pixel data type used in EMAN. More...
enum  ImageType {
  IMAGE_UNKNOWN, IMAGE_MRC, IMAGE_SPIDER, IMAGE_SINGLE_SPIDER,
  IMAGE_IMAGIC, IMAGE_HDF, IMAGE_DM3, IMAGE_TIFF,
  IMAGE_PGM, IMAGE_LST, IMAGE_PIF, IMAGE_VTK,
  IMAGE_PNG, IMAGE_SAL, IMAGE_ICOS, IMAGE_EMIM,
  IMAGE_GATAN2, IMAGE_AMIRA, IMAGE_XPLOR, IMAGE_EM,
  IMAGE_V4L, IMAGE_JPEG, IMAGE_FITS, IMAGE_LSTFAST,
  IMAGE_DF3
}
 Image format types. More...

Static Public Member Functions

EMDatavertical_acf (const EMData *image, int maxdy)
EMDatamake_image_median (const vector< EMData * > &image_list)
ImageType get_image_ext_type (const string &file_ext)
 Get an image's format type from its filename extension.
ImageType get_image_type (const string &filename)
 Get an image's format type by processing the first 1K of the image.
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 whether or not the return value of EMUtil.get_image_ext_type is IMAGE_UNKNOWN.
int get_image_count (const string &filename)
 Get the number of images in an image file.
ImageIOget_imageio (const string &filename, int rw_mode, ImageType image_type=IMAGE_UNKNOWN)
 Get an ImageIO object.
const char * get_imagetype_name (EMUtil::ImageType type)
 Give each image type a meaningful name.
const char * get_datatype_string (EMDataType type)
 Give each data type a meaningful name.
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.
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.
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.
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.
void dump_dict (const Dict &dict)
 Dump a Dict object.
bool is_same_size (const EMData *image1, const EMData *image2)
 Check whether two EMData images are of the same size.
bool is_same_ctf (const EMData *image1, const EMData *image2)
 Check whether two EMData images have the same CTF parameters.
bool is_complex_type (EMDataType datatype)
void jump_lines (FILE *file, int nlines)
vector< string > get_euler_names (const string &euler_type)
vector< EMObjectget_all_attributes (const string &file_name, const string &attr_name)
 Get an attribute from a stack of image, returned as a vector.
void getRenderMinMax (float *data, const int nx, const int ny, float &rendermin, float &rendermax, const int nz=1)
 Calculate the min and max pixel value acceptedfor image nomalization, if we did not get them from image attribute dictionary, or they are not valid values rendermin = mean - 3*sigma rendermax = mean + 3*sigma.
bool cuda_available ()
void * em_malloc (const size_t size)
void * em_calloc (const size_t nmemb, const size_t size)
void * em_realloc (void *data, const size_t new_size)
void em_memset (void *data, const int value, const size_t size)
void em_free (void *data)
void em_memcpy (void *dst, const void *const src, const size_t size)

Static Private Member Functions

ImageType fast_get_image_type (const string &filename, const void *first_block, off_t file_size)
void jump_lines_by_items (FILE *file, int nitems, int nitems_per_line)
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)
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)
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)


Member Enumeration Documentation

enum EMAN::EMUtil::EMDataType
 

Image pixel data type used in EMAN.

EM_U means "EM unsigned". for example, EM_USHORT means EM unsigned short.

Enumeration values:
EM_UNKNOWN 
EM_CHAR 
EM_UCHAR 
EM_SHORT 
EM_USHORT 
EM_INT 
EM_UINT 
EM_FLOAT 
EM_DOUBLE 
EM_SHORT_COMPLEX 
EM_USHORT_COMPLEX 
EM_FLOAT_COMPLEX 

Definition at line 92 of file emutil.h.

Referenced by dump_dict().

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                 };

enum EMAN::EMUtil::ImageType
 

Image format types.

Enumeration values:
IMAGE_UNKNOWN 
IMAGE_MRC 
IMAGE_SPIDER 
IMAGE_SINGLE_SPIDER 
IMAGE_IMAGIC 
IMAGE_HDF 
IMAGE_DM3 
IMAGE_TIFF 
IMAGE_PGM 
IMAGE_LST 
IMAGE_PIF 
IMAGE_VTK 
IMAGE_PNG 
IMAGE_SAL 
IMAGE_ICOS 
IMAGE_EMIM 
IMAGE_GATAN2 
IMAGE_AMIRA 
IMAGE_XPLOR 
IMAGE_EM 
IMAGE_V4L 
IMAGE_JPEG 
IMAGE_FITS 
IMAGE_LSTFAST 
IMAGE_DF3 

Definition at line 110 of file emutil.h.

Referenced by fast_get_image_type(), get_image_ext_type(), get_image_type(), getRenderMinMax(), and is_valid_filename().

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                 };


Member Function Documentation

bool EMAN::EMUtil::cuda_available  )  [inline, static]
 

Definition at line 332 of file emutil.h.

00332                                              {
00333 #ifdef EMAN2_USING_CUDA
00334                         return true;
00335 #else
00336                         return false;
00337 #endif
00338                 }

void EMUtil::dump_dict const Dict dict  )  [static]
 

Dump a Dict object.

Parameters:
dict A Dict object.

Definition at line 947 of file emutil.cpp.

References EMDataType, get_datatype_string(), EMAN::EMObject::is_null(), EMAN::Dict::keys(), EMAN::EMObject::to_str(), and EMAN::Dict::values().

Referenced by main().

00948 {
00949         vector < string > keys = dict.keys();
00950         vector < EMObject > values = dict.values();
00951 
00952         for (unsigned int i = 0; i < keys.size(); i++) {
00953                 EMObject obj = values[i];
00954                 if( !obj.is_null() ) {
00955                         string val = obj.to_str();
00956 
00957                         if (keys[i] == "datatype") {
00958                                 val = get_datatype_string((EMDataType) (int) obj);
00959                         }
00960 
00961                         fprintf(stdout, "%25s\t%s\n", keys[i].c_str(), val.c_str());
00962                 }
00963         }
00964 }

void* EMAN::EMUtil::em_calloc const size_t  nmemb,
const size_t  size
[inline, static]
 

Definition at line 344 of file emutil.h.

00344                                                                                     {
00345                         return calloc(nmemb,size);
00346                 }

void EMAN::EMUtil::em_free void *  data  )  [inline, static]
 

Definition at line 354 of file emutil.h.

References data.

Referenced by EMAN::EMData::free_memory().

00354                                                       {
00355                         free(data);
00356                 }

void* EMAN::EMUtil::em_malloc const size_t  size  )  [inline, static]
 

Definition at line 340 of file emutil.h.

Referenced by EMAN::TransformProcessor::transform().

00340                                                                  {
00341                         return malloc(size);
00342                 }

void EMAN::EMUtil::em_memcpy void *  dst,
const void *const   src,
const size_t  size
[inline, static]
 

Definition at line 358 of file emutil.h.

00358                                                                                                 {
00359                         memcpy(dst,src,size);
00360                 }

void EMAN::EMUtil::em_memset void *  data,
const int  value,
const size_t  size
[inline, static]
 

Definition at line 351 of file emutil.h.

References data.

Referenced by EMAN::EMData::to_value().

00351                                                                                              {
00352                         memset(data, value, size);
00353                 }

void* EMAN::EMUtil::em_realloc void *  data,
const size_t  new_size
[inline, static]
 

Definition at line 348 of file emutil.h.

References data.

00348                                                                                  {
00349                         return realloc(data, new_size);
00350                 }

void EMUtil::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
[static, private]
 

Definition at line 1369 of file emutil.cpp.

References Assert, data, and portable_fseek().

Referenced by process_ascii_region_io().

01372 {
01373         Assert(file);
01374         Assert(mode_size > 0);
01375         Assert(start >= 0);
01376         Assert(end <= nitems_per_line);
01377         Assert(data);
01378         Assert(p_i);
01379         Assert(outformat);
01380 
01381         char line[MAXPATHLEN];
01382 
01383         if (rw_mode == ImageIO::READ_ONLY) {
01384 
01385                 if (!fgets(line, sizeof(line), file)) {
01386                         Assert("read xplor file failed");
01387                 }
01388 
01389                 int nitems_in_line =  (int) (strlen(line) / mode_size);
01390                 Assert(end <= nitems_in_line);
01391 
01392                 vector<float> d(nitems_in_line);
01393                 char *pline = line;
01394 
01395                 for (int i = 0; i < nitems_in_line; i++) {
01396                         sscanf(pline, "%f", &d[i]);
01397                         pline = pline + (int)mode_size;
01398                 }
01399 
01400 
01401                 for (int i = 0; i < start; i++) {
01402                         data[*p_i] = d[i];
01403                         (*p_i)++;
01404                 }
01405 
01406                 for (int i = end+1; i < nitems_in_line; i++) {
01407                         data[*p_i] = d[i];
01408                         (*p_i)++;
01409                 }
01410         }
01411         else {
01412                 for (int i = 0; i < start; i++) {
01413                         fprintf(file, outformat, data[*p_i]);
01414                         (*p_i)++;
01415                 }
01416 
01417                 portable_fseek(file, (end-start+1) * mode_size, SEEK_CUR);
01418 
01419                 for (int i = end+1; i < nitems_per_line; i++) {
01420                         fprintf(file, outformat, data[*p_i]);
01421                         (*p_i)++;
01422                 }
01423                 portable_fseek(file, 1, SEEK_CUR);
01424         }
01425 }

EMUtil::ImageType EMUtil::fast_get_image_type const string &  filename,
const void *  first_block,
off_t  file_size
[static, private]
 

Definition at line 197 of file emutil.cpp.

References Assert, EMAN::Util::get_filename_ext(), get_image_ext_type(), IMAGE_AMIRA, IMAGE_DF3, IMAGE_DM3, IMAGE_EM, IMAGE_EMIM, IMAGE_GATAN2, IMAGE_HDF, IMAGE_ICOS, IMAGE_IMAGIC, IMAGE_LST, IMAGE_LSTFAST, IMAGE_MRC, IMAGE_PGM, IMAGE_PIF, IMAGE_PNG, IMAGE_SAL, IMAGE_SINGLE_SPIDER, IMAGE_SPIDER, IMAGE_TIFF, IMAGE_VTK, IMAGE_XPLOR, ImageType, EMAN::EmIO::is_valid(), EMAN::Gatan2IO::is_valid(), EMAN::XplorIO::is_valid(), EMAN::AmiraIO::is_valid(), EMAN::SalIO::is_valid(), EMAN::IcosIO::is_valid(), EMAN::EmimIO::is_valid(), EMAN::PgmIO::is_valid(), EMAN::VtkIO::is_valid(), EMAN::PifIO::is_valid(), EMAN::SingleSpiderIO::is_valid(), EMAN::SpiderIO::is_valid(), EMAN::LstFastIO::is_valid(), EMAN::LstIO::is_valid(), EMAN::DM3IO::is_valid(), EMAN::ImagicIO::is_valid(), and EMAN::MrcIO::is_valid().

Referenced by get_image_type().

00200 {
00201         ENTERFUNC;
00202         Assert(filename != "");
00203         Assert(first_block != 0);
00204         Assert(file_size > 0);
00205 
00206 #ifdef ENABLE_V4L2
00207         if (filename.compare(0,5,"/dev/")==0) return IMAGE_V4L;
00208 #endif
00209 
00210         string ext = Util::get_filename_ext(filename);
00211         if (ext == "") {
00212                 return IMAGE_UNKNOWN;
00213         }
00214         ImageType image_type = get_image_ext_type(ext);
00215 
00216         switch (image_type) {
00217         case IMAGE_MRC:
00218                 if (MrcIO::is_valid(first_block, file_size)) {
00219                         return IMAGE_MRC;
00220                 }
00221                 break;
00222         case IMAGE_IMAGIC:
00223                 if (ImagicIO::is_valid(first_block)) {
00224                         return IMAGE_IMAGIC;
00225                 }
00226                 break;
00227         case IMAGE_DM3:
00228                 if (DM3IO::is_valid(first_block)) {
00229                         return IMAGE_DM3;
00230                 }
00231                 break;
00232 #ifdef EM_HDF5
00233         case IMAGE_HDF:
00234                 if (HdfIO2::is_valid(first_block)) {
00235                         return IMAGE_HDF;
00236                 }
00237                 break;
00238 #endif
00239         case IMAGE_LST:
00240                 if (LstIO::is_valid(first_block)) {
00241                         return IMAGE_LST;
00242                 }
00243                 break;
00244         case IMAGE_LSTFAST:
00245                 if (LstFastIO::is_valid(first_block)) {
00246                         return IMAGE_LSTFAST;
00247                 }
00248                 break;
00249 #ifdef EM_TIFF
00250         case IMAGE_TIFF:
00251                 if (TiffIO::is_valid(first_block)) {
00252                         return IMAGE_TIFF;
00253                 }
00254                 break;
00255 #endif
00256         case IMAGE_SPIDER:
00257                 if (SpiderIO::is_valid(first_block)) {
00258                         return IMAGE_SPIDER;
00259                 }
00260                 break;
00261         case IMAGE_SINGLE_SPIDER:
00262                 if (SingleSpiderIO::is_valid(first_block)) {
00263                         return IMAGE_SINGLE_SPIDER;
00264                 }
00265                 break;
00266         case IMAGE_PIF:
00267                 if (PifIO::is_valid(first_block)) {
00268                         return IMAGE_PIF;
00269                 }
00270                 break;
00271 #ifdef EM_PNG
00272         case IMAGE_PNG:
00273                 if (PngIO::is_valid(first_block)) {
00274                         return IMAGE_PNG;
00275                 }
00276                 break;
00277 #endif
00278         case IMAGE_VTK:
00279                 if (VtkIO::is_valid(first_block)) {
00280                         return IMAGE_VTK;
00281                 }
00282                 break;
00283         case IMAGE_PGM:
00284                 if (PgmIO::is_valid(first_block)) {
00285                         return IMAGE_PGM;
00286                 }
00287                 break;
00288         case IMAGE_EMIM:
00289                 if (EmimIO::is_valid(first_block)) {
00290                         return IMAGE_EMIM;
00291                 }
00292                 break;
00293         case IMAGE_ICOS:
00294                 if (IcosIO::is_valid(first_block)) {
00295                         return IMAGE_ICOS;
00296                 }
00297                 break;
00298         case IMAGE_SAL:
00299                 if (SalIO::is_valid(first_block)) {
00300                         return IMAGE_SAL;
00301                 }
00302                 break;
00303         case IMAGE_AMIRA:
00304                 if (AmiraIO::is_valid(first_block)) {
00305                         return IMAGE_AMIRA;
00306                 }
00307                 break;
00308         case IMAGE_XPLOR:
00309                 if (XplorIO::is_valid(first_block)) {
00310                         return IMAGE_XPLOR;
00311                 }
00312                 break;
00313         case IMAGE_GATAN2:
00314                 if (Gatan2IO::is_valid(first_block)) {
00315                         return IMAGE_GATAN2;
00316                 }
00317                 break;
00318         case IMAGE_EM:
00319                 if (EmIO::is_valid(first_block, file_size)) {
00320                         return IMAGE_EM;
00321                 }
00322                 break;
00323         case IMAGE_DF3:
00324                 if (EmIO::is_valid(first_block, file_size)) {
00325                         return IMAGE_DF3;
00326                 }
00327                 break;
00328         default:
00329                 return IMAGE_UNKNOWN;
00330         }
00331         EXITFUNC;
00332         return IMAGE_UNKNOWN;
00333 }

vector< EMObject > EMUtil::get_all_attributes const string &  file_name,
const string &  attr_name
[static]
 

Get an attribute from a stack of image, returned as a vector.

Parameters:
file_name the image file name
attr_name The header attribute name.
Returns:
the vector of attribute value
Exceptions:
NotExistingObjectException when access an non-existing attribute
InvalidCallException when call this function for a non-stack image

Definition at line 1489 of file emutil.cpp.

References Assert, read_images(), and v.

01490 {
01491         vector<EMObject> v;
01492 
01493         Assert(file_name != "");
01494         Assert(attr_name != "");
01495 
01496         vector<EMData *> vpImg = EMData::read_images(file_name, vector<int>(), true);
01497         vector<EMData *>::iterator iter;
01498         for(iter = vpImg.begin(); iter!=vpImg.end(); ++iter) {
01499                 v.push_back((*iter)->get_attr_default(attr_name));
01500         }
01501 
01502         return v;
01503 }

const char * EMUtil::get_datatype_string EMDataType  type  )  [static]
 

Give each data type a meaningful name.

Parameters:
type the EMDataType
Returns:
a name for that data type

Definition at line 686 of file emutil.cpp.

References EM_CHAR, EM_DOUBLE, EM_FLOAT, EM_FLOAT_COMPLEX, EM_INT, EM_SHORT, EM_SHORT_COMPLEX, EM_UCHAR, EM_UINT, EM_UNKNOWN, EM_USHORT, and EM_USHORT_COMPLEX.

Referenced by dump_dict().

00687 {
00688         switch (type) {
00689         case EM_CHAR:
00690                 return "CHAR";
00691         case EM_UCHAR:
00692                 return "UNSIGNED CHAR";
00693         case EM_SHORT:
00694                 return "SHORT";
00695         case EM_USHORT:
00696                 return "UNSIGNED SHORT";
00697         case EM_INT:
00698                 return "INT";
00699         case EM_UINT:
00700                 return "UNSIGNED INT";
00701         case EM_FLOAT:
00702                 return "FLOAT";
00703         case EM_DOUBLE:
00704                 return "DOUBLE";
00705         case EM_SHORT_COMPLEX:
00706                 return "SHORT_COMPLEX";
00707         case EM_USHORT_COMPLEX:
00708                 return "USHORT_COMPLEX";
00709         case EM_FLOAT_COMPLEX:
00710                 return "FLOAT_COMPLEX";
00711         case EM_UNKNOWN:
00712                 return "UNKNOWN";
00713         }
00714         return "UNKNOWN";
00715 }

vector< string > EMUtil::get_euler_names const string &  euler_type  )  [static]
 

Definition at line 1445 of file emutil.cpp.

References b, and v.

01446 {
01447     vector<string> v;
01448     string b = "euler_";
01449 
01450     if (euler_type == "EMAN") {
01451         v.push_back(b + "alt");
01452         v.push_back(b + "az");
01453         v.push_back(b + "phi");
01454     }
01455     else if (euler_type == "MRC") {
01456         v.push_back(b + "theta");
01457         v.push_back(b + "phi");
01458         v.push_back(b + "omega");
01459     }
01460     else if (euler_type == "IMAGIC") {
01461         v.push_back(b + "alpha");
01462         v.push_back(b + "beta");
01463         v.push_back(b + "gamma");
01464     }
01465     else if (euler_type == "SPIDER") {
01466         v.push_back(b + "phi");
01467         v.push_back(b + "theta");
01468         v.push_back(b + "gamma");
01469     }
01470     else if (euler_type == "SPIN" ||
01471              euler_type == "SGIROT") {
01472         v.push_back(b + "q");
01473         v.push_back(b + "n1");
01474         v.push_back(b + "n2");
01475         v.push_back(b + "n3");
01476     }
01477 
01478     else if (euler_type == "QUATERNION") {
01479         v.push_back(b + "e0");
01480         v.push_back(b + "e1");
01481         v.push_back(b + "e2");
01482         v.push_back(b + "e3");
01483     }
01484 
01485     return v;
01486 }

int EMUtil::get_image_count const string &  filename  )  [static]
 

Get the number of images in an image file.

Parameters:
filename Image file name.
Returns:
Number of images in the given file.

Definition at line 456 of file emutil.cpp.

References Assert, get_imageio(), and EMAN::ImageIO::get_nimg().

Referenced by EMAN::PCA::dopca_ooc(), EMAN::PCA::Lanczos_ooc(), main(), ReadStackandDist(), and ReadStackandDist_Cart().

00457 {
00458         ENTERFUNC;
00459         Assert(filename != "");
00460 
00461         int nimg = 0;
00462         ImageIO *imageio = get_imageio(filename, ImageIO::READ_ONLY);
00463 
00464         if (imageio) {
00465                 nimg = imageio->get_nimg();
00466         }
00467 #ifndef IMAGEIO_CACHE
00468         if( imageio )
00469         {
00470                 delete imageio;
00471                 imageio = 0;
00472         }
00473 #endif
00474         EXITFUNC;
00475         return nimg;
00476 }

EMUtil::ImageType EMUtil::get_image_ext_type const string &  file_ext  )  [static]
 

Get an image's format type from its filename extension.

Parameters:
file_ext File extension.
Returns:
image format type.

Definition at line 58 of file emutil.cpp.

References ImageType.

Referenced by fast_get_image_type(), and is_valid_filename().

00059 {
00060         ENTERFUNC;
00061         static bool initialized = false;
00062         static map < string, ImageType > imagetypes;
00063 
00064         if (!initialized) {
00065                 imagetypes["rec"] = IMAGE_MRC;
00066                 imagetypes["mrc"] = IMAGE_MRC;
00067                 imagetypes["MRC"] = IMAGE_MRC;
00068                 imagetypes["ali"] = IMAGE_MRC;
00069 
00070                 imagetypes["tnf"] = IMAGE_MRC;
00071                 imagetypes["TNF"] = IMAGE_MRC;
00072 
00073                 imagetypes["ccp4"] = IMAGE_MRC;
00074                 imagetypes["map"] = IMAGE_MRC;
00075 
00076                 imagetypes["dm3"] = IMAGE_DM3;
00077                 imagetypes["DM3"] = IMAGE_DM3;
00078 
00079                 imagetypes["spi"] = IMAGE_SPIDER;
00080                 imagetypes["SPI"] = IMAGE_SPIDER;
00081 
00082                 imagetypes["spider"] = IMAGE_SPIDER;
00083                 imagetypes["SPIDER"] = IMAGE_SPIDER;
00084 
00085                 imagetypes["spidersingle"] = IMAGE_SINGLE_SPIDER;
00086                 imagetypes["SPIDERSINGLE"] = IMAGE_SINGLE_SPIDER;
00087 
00088                 imagetypes["singlespider"] = IMAGE_SINGLE_SPIDER;
00089                 imagetypes["SINGLESPIDER"] = IMAGE_SINGLE_SPIDER;
00090 
00091                 imagetypes["img"] = IMAGE_IMAGIC;
00092                 imagetypes["IMG"] = IMAGE_IMAGIC;
00093 
00094                 imagetypes["hed"] = IMAGE_IMAGIC;
00095                 imagetypes["HED"] = IMAGE_IMAGIC;
00096 
00097                 imagetypes["imagic"] = IMAGE_IMAGIC;
00098                 imagetypes["IMAGIC"] = IMAGE_IMAGIC;
00099 
00100                 imagetypes["pgm"] = IMAGE_PGM;
00101                 imagetypes["PGM"] = IMAGE_PGM;
00102 
00103                 imagetypes["lst"] = IMAGE_LST;
00104                 imagetypes["LST"] = IMAGE_LST;
00105 
00106                 imagetypes["lsx"] = IMAGE_LSTFAST;      // but .lst or another extension would also be ok
00107                 imagetypes["LSX"] = IMAGE_LSTFAST;
00108 
00109                 imagetypes["pif"] = IMAGE_PIF;
00110                 imagetypes["PIF"] = IMAGE_PIF;
00111 
00112                 imagetypes["png"] = IMAGE_PNG;
00113                 imagetypes["PNG"] = IMAGE_PNG;
00114 
00115                 imagetypes["h5"] = IMAGE_HDF;
00116                 imagetypes["H5"] = IMAGE_HDF;
00117 
00118                 imagetypes["hd5"] = IMAGE_HDF;
00119                 imagetypes["HD5"] = IMAGE_HDF;
00120 
00121                 imagetypes["hdf"] = IMAGE_HDF;
00122                 imagetypes["HDF"] = IMAGE_HDF;
00123 
00124                 imagetypes["tif"] = IMAGE_TIFF;
00125                 imagetypes["TIF"] = IMAGE_TIFF;
00126 
00127                 imagetypes["tiff"] = IMAGE_TIFF;
00128                 imagetypes["TIFF"] = IMAGE_TIFF;
00129 
00130                 imagetypes["fts"] = IMAGE_FITS;
00131                 imagetypes["FTS"] = IMAGE_FITS;
00132 
00133                 imagetypes["vtk"] = IMAGE_VTK;
00134                 imagetypes["VTK"] = IMAGE_VTK;
00135 
00136                 imagetypes["hdr"] = IMAGE_SAL;
00137                 imagetypes["HDR"] = IMAGE_SAL;
00138 
00139                 imagetypes["sal"] = IMAGE_SAL;
00140                 imagetypes["SAL"] = IMAGE_SAL;
00141 
00142                 imagetypes["map"] = IMAGE_ICOS;
00143                 imagetypes["MAP"] = IMAGE_ICOS;
00144 
00145                 imagetypes["icos"] = IMAGE_ICOS;
00146                 imagetypes["ICOS"] = IMAGE_ICOS;
00147 
00148                 imagetypes["am"] = IMAGE_AMIRA;
00149                 imagetypes["AM"] = IMAGE_AMIRA;
00150 
00151                 imagetypes["amira"] = IMAGE_AMIRA;
00152                 imagetypes["AMIRA"] = IMAGE_AMIRA;
00153 
00154                 imagetypes["emim"] = IMAGE_EMIM;
00155                 imagetypes["EMIM"] = IMAGE_EMIM;
00156 
00157                 imagetypes["xplor"] = IMAGE_XPLOR;
00158                 imagetypes["XPLOR"] = IMAGE_XPLOR;
00159 
00160                 imagetypes["em"] = IMAGE_EM;
00161                 imagetypes["EM"] = IMAGE_EM;
00162 
00163                 imagetypes["dm2"] = IMAGE_GATAN2;
00164                 imagetypes["DM2"] = IMAGE_GATAN2;
00165 
00166                 imagetypes["v4l"] = IMAGE_V4L;
00167                 imagetypes["V4L"] = IMAGE_V4L;
00168 
00169                 imagetypes["jpg"] = IMAGE_JPEG;
00170                 imagetypes["JPG"] = IMAGE_JPEG;
00171                 imagetypes["jpeg"] = IMAGE_JPEG;
00172                 imagetypes["JPEG"] = IMAGE_JPEG;
00173 
00174                 imagetypes["df3"] = IMAGE_DF3;
00175                 imagetypes["DF3"] = IMAGE_DF3;
00176 
00177                 initialized = true;
00178         }
00179 
00180         ImageType result = IMAGE_UNKNOWN;
00181 
00182         if (imagetypes.find(file_ext) != imagetypes.end()) {
00183                 result = imagetypes[file_ext];
00184         }
00185 
00186         EXITFUNC;
00187         return result;
00188 }

EMUtil::ImageType EMUtil::get_image_type const string &  filename  )  [static]
 

Get an image's format type by processing the first 1K of the image.

Parameters:
filename Image file name.
Returns:
image format type.

Definition at line 336 of file emutil.cpp.

References Assert, EMAN::Util::change_filename_ext(), fast_get_image_type(), FileAccessException, EMAN::Util::get_filename_ext(), ImageFormatException, ImageType, in, EMAN::MrcIO::is_valid(), EMAN::Df3IO::is_valid(), EMAN::ImagicIO::is_valid(), EMAN::EmIO::is_valid(), EMAN::FitsIO::is_valid(), EMAN::Gatan2IO::is_valid(), EMAN::XplorIO::is_valid(), EMAN::AmiraIO::is_valid(), EMAN::SalIO::is_valid(), EMAN::IcosIO::is_valid(), EMAN::EmimIO::is_valid(), EMAN::PgmIO::is_valid(), EMAN::VtkIO::is_valid(), EMAN::PifIO::is_valid(), EMAN::SingleSpiderIO::is_valid(), EMAN::SpiderIO::is_valid(), EMAN::LstFastIO::is_valid(), EMAN::LstIO::is_valid(), EMAN::DM3IO::is_valid(), LOGERR, portable_fseek(), and portable_ftell().

Referenced by get_imageio(), getRenderMinMax(), and main().

00337 {
00338         ENTERFUNC;
00339         Assert(in_filename != "");
00340 
00341 #ifdef ENABLE_V4L2
00342         if (in_filename.compare(0,5,"/dev/")==0) return IMAGE_V4L;
00343 #endif
00344 
00345         string filename = in_filename;
00346 
00347         string old_ext = Util::get_filename_ext(filename);
00348         if (old_ext == ImagicIO::IMG_EXT) {
00349                 filename = Util::change_filename_ext(filename, ImagicIO::HED_EXT);
00350         }
00351 
00352         FILE *in = fopen(filename.c_str(), "rb");
00353         if (!in) {
00354                 throw FileAccessException(filename);
00355         }
00356 
00357         char first_block[1024];
00358         size_t n = fread(first_block, sizeof(char), sizeof(first_block), in);
00359         portable_fseek(in, 0, SEEK_END);
00360         off_t file_size = portable_ftell(in);
00361 
00362         if (n == 0) {
00363                 LOGERR("file '%s' is an empty file", filename.c_str());
00364                 fclose(in);
00365                 return IMAGE_UNKNOWN;
00366         }
00367         fclose(in);
00368 
00369         ImageType image_type = fast_get_image_type(filename, first_block, file_size);
00370         if (image_type != IMAGE_UNKNOWN) {
00371                 return image_type;
00372         }
00373 
00374         if (DM3IO::is_valid(first_block)) {
00375                 image_type = IMAGE_DM3;
00376         }
00377 #ifdef EM_HDF5
00378         else if (HdfIO2::is_valid(first_block)) {
00379                 image_type = IMAGE_HDF;
00380         }
00381 #endif
00382         else if (LstIO::is_valid(first_block)) {
00383                 image_type = IMAGE_LST;
00384         }
00385         else if (LstFastIO::is_valid(first_block)) {
00386                 image_type = IMAGE_LSTFAST;
00387         }
00388 #ifdef EM_TIFF
00389         else if (TiffIO::is_valid(first_block)) {
00390                 image_type = IMAGE_TIFF;
00391         }
00392 #endif
00393         else if (SpiderIO::is_valid(first_block)) {
00394                 image_type = IMAGE_SPIDER;
00395         }
00396         else if (SingleSpiderIO::is_valid(first_block)) {
00397                 image_type = IMAGE_SINGLE_SPIDER;
00398         }
00399         else if (PifIO::is_valid(first_block)) {
00400                 image_type = IMAGE_PIF;
00401         }
00402 #ifdef EM_PNG
00403         else if (PngIO::is_valid(first_block)) {
00404                 image_type = IMAGE_PNG;
00405         }
00406 #endif
00407         else if (VtkIO::is_valid(first_block)) {
00408                 image_type = IMAGE_VTK;
00409         }
00410         else if (PgmIO::is_valid(first_block)) {
00411                 image_type = IMAGE_PGM;
00412         }
00413         else if (EmimIO::is_valid(first_block)) {
00414                 image_type = IMAGE_EMIM;
00415         }
00416         else if (IcosIO::is_valid(first_block)) {
00417                 image_type = IMAGE_ICOS;
00418         }
00419         else if (SalIO::is_valid(first_block)) {
00420                 image_type = IMAGE_SAL;
00421         }
00422         else if (AmiraIO::is_valid(first_block)) {
00423                 image_type = IMAGE_AMIRA;
00424         }
00425         else if (XplorIO::is_valid(first_block)) {
00426                 image_type = IMAGE_XPLOR;
00427         }
00428         else if (Gatan2IO::is_valid(first_block)) {
00429                 image_type = IMAGE_GATAN2;
00430         }
00431         else if (FitsIO::is_valid(first_block)) {
00432                 image_type = IMAGE_FITS;
00433         }
00434         else if (EmIO::is_valid(first_block, file_size)) {
00435                 image_type = IMAGE_EM;
00436         }
00437         else if (ImagicIO::is_valid(first_block)) {
00438                 image_type = IMAGE_IMAGIC;
00439         }
00440         else if (Df3IO::is_valid(first_block)) {
00441                 image_type = IMAGE_DF3;
00442         }
00443         else if (MrcIO::is_valid(first_block, file_size)) {
00444                 image_type = IMAGE_MRC;
00445         }
00446         else {
00447                 //LOGERR("I don't know this image's type: '%s'", filename.c_str());
00448                 throw ImageFormatException("invalid image type");
00449         }
00450 
00451         EXITFUNC;
00452         return image_type;
00453 }

ImageIO * EMUtil::get_imageio const string &  filename,
int  rw_mode,
ImageType  image_type = IMAGE_UNKNOWN
[static]
 

Get an ImageIO object.

It may be a newly created object. Or an object stored in the cache.

Parameters:
filename Image file name.
rw_mode ImageIO read/write mode.
image_type Image format type.
Returns:
An ImageIO object.

Definition at line 479 of file emutil.cpp.

References EMAN::GlobalCache::add_imageio(), Assert, get_image_type(), EMAN::GlobalCache::get_imageio(), IMAGE_AMIRA, IMAGE_DF3, IMAGE_DM3, IMAGE_EM, IMAGE_EMIM, IMAGE_FITS, IMAGE_GATAN2, IMAGE_HDF, IMAGE_ICOS, IMAGE_IMAGIC, IMAGE_JPEG, IMAGE_LST, IMAGE_LSTFAST, IMAGE_MRC, IMAGE_PGM, IMAGE_PIF, IMAGE_PNG, IMAGE_SAL, IMAGE_SINGLE_SPIDER, IMAGE_SPIDER, IMAGE_TIFF, IMAGE_V4L, IMAGE_VTK, IMAGE_XPLOR, ImageFormatException, and EMAN::GlobalCache::instance().

Referenced by EMAN::LstIO::calc_ref_image_index(), EMAN::LstFastIO::calc_ref_image_index(), get_image_count(), and EMAN::EMData::read_image().

00481 {
00482         ENTERFUNC;
00483         Assert(filename != "");
00484         Assert(rw == ImageIO::READ_ONLY ||
00485                    rw == ImageIO::READ_WRITE ||
00486                    rw == ImageIO::WRITE_ONLY);
00487 
00488         ImageIO *imageio = 0;
00489 #ifdef IMAGEIO_CACHE
00490         imageio = GlobalCache::instance()->get_imageio(filename, rw);
00491         if (imageio) {
00492                 return imageio;
00493         }
00494 #endif
00495 
00496         ImageIO::IOMode rw_mode = static_cast < ImageIO::IOMode > (rw);
00497 
00498         if (image_type == IMAGE_UNKNOWN) {
00499                 if(rw == ImageIO::WRITE_ONLY || rw == ImageIO::READ_WRITE) {
00500                         throw ImageFormatException("writing to this image format not supported.");
00501                 }
00502 
00503                 image_type = get_image_type(filename);
00504         }
00505 
00506         switch (image_type) {
00507 #ifdef ENABLE_V4L2
00508         case IMAGE_V4L:
00509                 imageio = new V4L2IO(filename, rw_mode);
00510                 break;
00511 #endif
00512         case IMAGE_MRC:
00513                 imageio = new MrcIO(filename, rw_mode);
00514                 break;
00515         case IMAGE_IMAGIC:
00516                 imageio = new ImagicIO(filename, rw_mode);
00517                 break;
00518         case IMAGE_DM3:
00519                 imageio = new DM3IO(filename, rw_mode);
00520                 break;
00521 #ifdef EM_TIFF
00522         case IMAGE_TIFF:
00523                 imageio = new TiffIO(filename, rw_mode);
00524                 break;
00525 #endif
00526 #ifdef EM_HDF5
00527         case IMAGE_HDF:
00528                 imageio = new HdfIO2(filename, rw_mode);
00529                 if (((HdfIO2 *)imageio)->init_test()==-1) {
00530                         delete imageio;
00531                         imageio = new HdfIO(filename, rw_mode);
00532                 }
00533                 break;
00534 #endif
00535         case IMAGE_LST:
00536                 imageio = new LstIO(filename, rw_mode);
00537                 break;
00538         case IMAGE_LSTFAST:
00539                 imageio = new LstFastIO(filename, rw_mode);
00540                 break;
00541         case IMAGE_PIF:
00542                 imageio = new PifIO(filename, rw_mode);
00543                 break;
00544         case IMAGE_VTK:
00545                 imageio = new VtkIO(filename, rw_mode);
00546                 break;
00547         case IMAGE_SPIDER:
00548                 imageio = new SpiderIO(filename, rw_mode);
00549                 break;
00550         case IMAGE_SINGLE_SPIDER:
00551                 imageio = new SingleSpiderIO(filename, rw_mode);
00552                 break;
00553         case IMAGE_PGM:
00554                 imageio = new PgmIO(filename, rw_mode);
00555                 break;
00556 #ifdef EM_JPEG
00557         case IMAGE_JPEG:
00558                 imageio = new JpegIO(filename,rw_mode);
00559                 break;
00560 #endif
00561         case IMAGE_EMIM:
00562                 imageio = new EmimIO(filename, rw_mode);
00563                 break;
00564         case IMAGE_ICOS:
00565                 imageio = new IcosIO(filename, rw_mode);
00566                 break;
00567 #ifdef EM_PNG
00568         case IMAGE_PNG:
00569                 imageio = new PngIO(filename, rw_mode);
00570                 break;
00571 #endif
00572         case IMAGE_SAL:
00573                 imageio = new SalIO(filename, rw_mode);
00574                 break;
00575         case IMAGE_AMIRA:
00576                 imageio = new AmiraIO(filename, rw_mode);
00577                 break;
00578         case IMAGE_GATAN2:
00579                 imageio = new Gatan2IO(filename, rw_mode);
00580                 break;
00581         case IMAGE_EM:
00582                 imageio = new EmIO(filename, rw_mode);
00583                 break;
00584         case IMAGE_XPLOR:
00585                 imageio = new XplorIO(filename, rw_mode);
00586                 break;
00587         case IMAGE_FITS:
00588                 imageio = new FitsIO(filename, rw_mode);
00589                 break;
00590         case IMAGE_DF3:
00591                 imageio = new Df3IO(filename, rw_mode);
00592                 break;
00593         default:
00594                 break;
00595         }
00596 #ifdef IMAGEIO_CACHE
00597         GlobalCache::instance()->add_imageio(filename, rw, imageio);
00598 #endif
00599         EXITFUNC;
00600         return imageio;
00601 }

const char * EMUtil::get_imagetype_name EMUtil::ImageType  type  )  [static]
 

Give each image type a meaningful name.

Parameters:
type Image format type.
Returns:
A name for that type.

Definition at line 605 of file emutil.cpp.

References IMAGE_AMIRA, IMAGE_DF3, IMAGE_DM3, IMAGE_EM, IMAGE_EMIM, IMAGE_FITS, IMAGE_GATAN2, IMAGE_HDF, IMAGE_ICOS, IMAGE_IMAGIC, IMAGE_JPEG, IMAGE_LST, IMAGE_LSTFAST, IMAGE_MRC, IMAGE_PGM, IMAGE_PIF, IMAGE_PNG, IMAGE_SAL, IMAGE_SINGLE_SPIDER, IMAGE_SPIDER, IMAGE_TIFF, IMAGE_UNKNOWN, IMAGE_V4L, IMAGE_VTK, and IMAGE_XPLOR.

Referenced by main().

00606 {
00607         switch (t) {
00608         case IMAGE_V4L:
00609                 return "V4L2";
00610                 break;
00611         case IMAGE_MRC:
00612                 return "MRC";
00613                 break;
00614         case IMAGE_SPIDER:
00615                 return "SPIDER";
00616                 break;
00617         case IMAGE_SINGLE_SPIDER:
00618                 return "Single-SPIDER";
00619                 break;
00620         case IMAGE_IMAGIC:
00621                 return "IMAGIC";
00622                 break;
00623         case IMAGE_PGM:
00624                 return "PGM";
00625                 break;
00626         case IMAGE_LST:
00627                 return "LST";
00628                 break;
00629         case IMAGE_LSTFAST:
00630                 return "Fast LST";
00631                 break;
00632         case IMAGE_PIF:
00633                 return "PIF";
00634                 break;
00635         case IMAGE_PNG:
00636                 return "PNG";
00637                 break;
00638         case IMAGE_HDF:
00639                 return "HDF5";
00640                 break;
00641         case IMAGE_DM3:
00642                 return "GatanDM3";
00643                 break;
00644         case IMAGE_TIFF:
00645                 return "TIFF";
00646                 break;
00647         case IMAGE_VTK:
00648                 return "VTK";
00649                 break;
00650         case IMAGE_SAL:
00651                 return "HDR";
00652                 break;
00653         case IMAGE_ICOS:
00654                 return "ICOS_MAP";
00655                 break;
00656         case IMAGE_EMIM:
00657                 return "EMIM";
00658                 break;
00659         case IMAGE_GATAN2:
00660                 return "GatanDM2";
00661                 break;
00662         case IMAGE_JPEG:
00663                 return "JPEG";
00664                 break;
00665         case IMAGE_AMIRA:
00666                 return "AmiraMesh";
00667                 break;
00668         case IMAGE_XPLOR:
00669                 return "XPLOR";
00670                 break;
00671         case IMAGE_EM:
00672                 return "EM";
00673                 break;
00674         case IMAGE_FITS:
00675                 return "FITS";
00676                 break;
00677         case IMAGE_DF3:
00678                 return "DF3";
00679                 break;
00680         case IMAGE_UNKNOWN:
00681                 return "unknown";
00682         }
00683         return "unknown";
00684 }

void EMUtil::get_region_dims const Region area,
int  nx,
int *  area_x,
int  ny,
int *  area_y,
int  nz = 1,
int *  area_z = 0
[static]
 

Get a region's dimensions.

Parameters:
area The region area.
nx Image x size.
area_x The pointer used to return the region x size.
ny Image y size.
area_y The pointer used to return the region y size.
nz Image z size.
area_z The pointer used to return the region z size.

Definition at line 717 of file emutil.cpp.

References Assert, EMAN::Region::get_ndim(), EMAN::Region::get_size(), and EMAN::Vec3i.

Referenced by process_ascii_region_io(), and process_region_io().

00719 {
00720         Assert(area_x);
00721         Assert(area_y);
00722 
00723         if (!area) {
00724                 *area_x = nx;
00725                 *area_y = ny;
00726                 if (area_z) {
00727                         *area_z = nz;
00728                 }
00729         }
00730         else {
00731                 Vec3i size = area->get_size();
00732                 *area_x = size[0];
00733                 *area_y = size[1];
00734 
00735                 if (area_z) {
00736                         if (area->get_ndim() > 2 && nz > 1) {
00737                                 *area_z = size[2];
00738                         }
00739                         else {
00740                                 *area_z = 1;
00741                         }
00742                 }
00743 
00744         }
00745 }

void EMUtil::get_region_origins const Region area,
int *  p_x0,
int *  p_y0,
int *  p_z0 = 0,
int  nz = 1,
int  image_index = 0
[static]
 

Get a region's original locations.

Parameters:
area The region area.
p_x0 The pointer used to return the region x origin.
p_y0 The pointer used to return the region y origin.
p_z0 The pointer used to return the region z origin.
nz Image z size.
image_index Image index.

Definition at line 747 of file emutil.cpp.

References Assert, EMAN::Region::get_ndim(), and EMAN::Region::origin.

00749 {
00750         Assert(p_x0);
00751         Assert(p_y0);
00752 
00753         if (area) {
00754                 *p_x0 = static_cast < int >(area->origin[0]);
00755                 *p_y0 = static_cast < int >(area->origin[1]);
00756 
00757                 if (p_z0 && nz > 1 && area->get_ndim() > 2) {
00758                         *p_z0 = static_cast < int >(area->origin[2]);
00759                 }
00760         }
00761         else {
00762                 *p_x0 = 0;
00763                 *p_y0 = 0;
00764                 if (p_z0) {
00765                         *p_z0 = nz > 1 ? 0 : image_index;
00766                 }
00767         }
00768 }

void EMUtil::getRenderMinMax float *  data,
const int  nx,
const int  ny,
float &  rendermin,
float &  rendermax,
const int  nz = 1
[static]
 

Calculate the min and max pixel value acceptedfor image nomalization, if we did not get them from image attribute dictionary, or they are not valid values rendermin = mean - 3*sigma rendermax = mean + 3*sigma.

Parameters:
[in] data 2D image's data array
[in] nx x dimension size
[in] ny y dimension size
[out] rendermin the minmal value for normalization
[out] rendermax the maximum value for normalization
[in] nz z dimension size

Definition at line 1505 of file emutil.cpp.

References data, get_image_type(), ImageFormatException, ImageType, EMAN::EMObject::init(), key, max, min, nx, ny, and sqrt().

01506 {
01507 #ifdef _WIN32
01508         if (rendermax<=rendermin || _isnan(rendermin) || _isnan(rendermax)) {
01509 #else
01510         if (rendermax<=rendermin || std::isnan(rendermin) || std::isnan(rendermax)) {
01511 #endif
01512                 float m=0.0f,s=0.0f;
01513 
01514                 size_t size = nx*ny*nz;
01515                 float min=data[0],max=data[0];
01516 
01517                 for (size_t i=0; i<size; ++i) { m+=data[i]; s+=data[i]*data[i]; min=data[i]<min?data[i]:min; max=data[i]>max?data[i]:max; }
01518                 m/=(float)(size);
01519                 s=sqrt(s/(float)(size)-m*m);
01520 #ifdef _WIN32
01521                 if (s<=0 || _isnan(s)) s=1.0;   // this means all data values are the same
01522 #else
01523                 if (s<=0 || std::isnan(s)) s=1.0;       // this means all data values are the same
01524 #endif  //_WIN32
01525                 rendermin=m-s*5.0f;
01526                 rendermax=m+s*5.0f;
01527                 if (rendermin<=min) rendermin=min;
01528                 if (rendermax>=max) rendermax=max;
01529         }
01530 }

bool EMUtil::is_complex_type EMDataType  datatype  )  [static]
 

Definition at line 977 of file emutil.cpp.

References EM_SHORT_COMPLEX, and EM_USHORT_COMPLEX.

Referenced by EMAN::TestUtil::make_image_file_by_mode(), and EMAN::TestUtil::verify_image_file_by_mode().

00978 {
00979         if (datatype == EM_SHORT_COMPLEX ||
00980                 datatype == EM_USHORT_COMPLEX ||
00981                 datatype == EM_FLOAT_COMPLEX) {
00982                 return true;
00983         }
00984         return false;
00985 }

bool EMUtil::is_same_ctf const EMData image1,
const EMData image2
[static]
 

Check whether two EMData images have the same CTF parameters.

Parameters:
image1 The first EMData image.
image2 The second EMData image.
Returns:
whether two EMData images have the same CTF.

Definition at line 1089 of file emutil.cpp.

References EMAN::Ctf::equal(), EMAN::EMData::get_ctf(), EMAN::EMData::has_ctff(), and NullPointerException.

Referenced by main().

01090 {
01091         if (!image1) {
01092                 throw NullPointerException("image1 is NULL");
01093         }
01094         if (!image2) {
01095                 throw NullPointerException("image2 is NULL");
01096         }
01097 
01098         Ctf *ctf1 = image1->get_ctf();
01099         Ctf *ctf2 = image2->get_ctf();
01100 
01101         if ((!ctf1 && !ctf2) && (image1->has_ctff() == false && image2->has_ctff() == false)) {
01102                 return true;
01103         }
01104 
01105         if (ctf1 && ctf2) {
01106                 bool result = ctf1->equal(ctf2);
01107                 delete ctf1;
01108                 ctf1 = 0;
01109                 delete ctf2;
01110                 ctf2 = 0;
01111 
01112                 return result;
01113         }
01114         return false;
01115 }

bool EMUtil::is_same_size const EMData image1,
const EMData image2
[static]
 

Check whether two EMData images are of the same size.

Parameters:
image1 The first EMData image.
image2 The second EMData image.
Returns:
Whether two EMData images are of the same size.

Definition at line 967 of file emutil.cpp.

References EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), and EMAN::EMData::get_zsize().

Referenced by EMAN::CtfAverager::add_image(), EMAN::CtfCAutoAverager::add_image(), EMAN::CtfCWautoAverager::add_image(), EMAN::IterationAverager::add_image(), EMAN::MinMaxAverager::add_image(), EMAN::ImageAverager::add_image(), EMAN::TranslationalAligner::align(), EMAN::NormalizeMaskProcessor::calc_mean(), EMAN::NormalizeMaskProcessor::calc_sigma(), EMAN::IndexMaskFileProcessor::process_inplace(), and EMAN::Cmp::validate_input_args().

00968 {
00969         if (em1->get_xsize() == em2->get_xsize() &&
00970                 em1->get_ysize() == em2->get_ysize() &&
00971                 em1->get_zsize() == em2->get_zsize()) {
00972                 return true;
00973         }
00974         return false;
00975 }

bool EMUtil::is_valid_filename const string &  filename  )  [static]
 

Ask whether or not the given filename is a valid EM image filename This is the same thing as checking whether or not the return value of EMUtil.get_image_ext_type is IMAGE_UNKNOWN.

Parameters:
filename Image file name.
Returns:
whether or not it is a valid filename

Definition at line 192 of file emutil.cpp.

References get_image_ext_type(), and ImageType.

00192                                                       {
00193         ImageType type = get_image_ext_type(Util::get_filename_ext(filename));
00194         return (type != IMAGE_UNKNOWN);
00195 }

void EMUtil::jump_lines FILE *  file,
int  nlines
[static]
 

Definition at line 1308 of file emutil.cpp.

References Assert.

Referenced by jump_lines_by_items(), and process_ascii_region_io().

01309 {
01310         Assert(file);
01311 
01312         if (nlines > 0) {
01313                 char line[MAXPATHLEN];
01314                 for (int l = 0; l < nlines; l++) {
01315                         if (!fgets(line, sizeof(line), file)) {
01316                                 Assert("read xplor file failed");
01317                         }
01318                 }
01319         }
01320 }

void EMUtil::jump_lines_by_items FILE *  file,
int  nitems,
int  nitems_per_line
[static, private]
 

Definition at line 1289 of file emutil.cpp.

References Assert, and jump_lines().

Referenced by process_ascii_region_io().

01290 {
01291         Assert(file);
01292         Assert(nitems_per_line > 0);
01293 
01294         if (nitems <= 0) {
01295                 return;
01296         }
01297 
01298         int nlines = nitems / nitems_per_line;
01299         if ((nitems % nitems_per_line) != 0) {
01300                 nlines++;
01301         }
01302         if (nlines > 0) {
01303                 jump_lines(file, nlines);
01304         }
01305 }

EMData * EMUtil::make_image_median const vector< EMData * > &  image_list  )  [static]
 

Definition at line 1024 of file emutil.cpp.

References EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), EMAN::EMData::set_size(), EMAN::EMData::update(), and v.

01025 {
01026         if (image_list.size() == 0) {
01027                 return 0;
01028         }
01029 
01030         EMData *image0 = image_list[0];
01031         int image0_nx = image0->get_xsize();
01032         int image0_ny = image0->get_ysize();
01033         int image0_nz = image0->get_zsize();
01034         size_t size = image0_nx * image0_ny * image0_nz;
01035 
01036         EMData *result = new EMData();
01037 
01038         result->set_size(image0_nx, image0_ny, image0_nz);
01039 
01040         float *dest = result->get_data();
01041         int nitems = static_cast < int >(image_list.size());
01042         float *srt = new float[nitems];
01043         float **src = new float *[nitems];
01044 
01045         for (int i = 0; i < nitems; i++) {
01046                 src[i] = image_list[i]->get_data();
01047         }
01048 
01049         for (size_t i = 0; i < size; i++) {
01050                 for (int j = 0; j < nitems; j++) {
01051                         srt[j] = src[j][i];
01052                 }
01053 
01054                 for (int j = 0; j < nitems; j++) {
01055                         for (int k = j + 1; k < nitems; k++) {
01056                                 if (srt[j] < srt[k]) {
01057                                         float v = srt[j];
01058                                         srt[j] = srt[k];
01059                                         srt[k] = v;
01060                                 }
01061                         }
01062                 }
01063 
01064                 int l = nitems / 2;
01065                 if (nitems < 3) {
01066                         dest[i] = srt[l];
01067                 }
01068                 else {
01069                         dest[i] = (srt[l] + srt[l + 1] + srt[l - 1]) / 3.0f;
01070                 }
01071         }
01072 
01073         if( srt )
01074         {
01075                 delete[]srt;
01076                 srt = 0;
01077         }
01078         if( src )
01079         {
01080                 delete[]src;
01081                 src = 0;
01082         }
01083 
01084         result->update();
01085 
01086         return result;
01087 }

void EMUtil::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
[static]
 

Works for regions that are outside the image data dimension area.

The only function that calls this is in xplorio.cpp - that function throws if the region is invalid.

Definition at line 1180 of file emutil.cpp.

References Assert, data, exclude_numbers_io(), get_region_dims(), jump_lines(), jump_lines_by_items(), nx, ny, EMAN::Region::origin, process_lines_io(), and process_numbers_io().

01184 {
01185         Assert(data != 0);
01186         Assert(file != 0);
01187         Assert(rw_mode == ImageIO::READ_ONLY ||
01188                    rw_mode == ImageIO::READ_WRITE ||
01189                    rw_mode == ImageIO::WRITE_ONLY);
01190 
01191         int xlen = 0, ylen = 0, zlen = 0;
01192         get_region_dims(area, nx, &xlen, ny, &ylen, nz, &zlen);
01193 
01194         int x0 = 0;
01195         int y0 = 0;
01196         int z0 = 0;
01197 
01198         if (area) {
01199                 x0 = (int)area->origin[0];
01200                 y0 = (int)area->origin[1];
01201                 z0 = (int)area->origin[2];
01202         }
01203 
01204         int nlines_per_sec = (nx *ny) / nitems_per_line;
01205         int nitems_last_line = (nx * ny) % nitems_per_line;
01206         if (nitems_last_line != 0) {
01207                 nlines_per_sec++;
01208         }
01209 
01210         if (has_index_line) {
01211                 nlines_per_sec++;
01212         }
01213 
01214         if (z0 > 0) {
01215                 jump_lines(file, z0 * nlines_per_sec);
01216         }
01217 
01218 
01219         int nlines_pre_sec = (y0 * nx + x0) / nitems_per_line;
01220         int gap_nitems = nx - xlen;
01221         int ti = 0;
01222         int rlines = 0;
01223 
01224         for (int k = 0; k < zlen; k++) {
01225                 EMUtil::jump_lines(file, nlines_pre_sec+1);
01226 
01227                 int head_nitems = (y0 * nx + x0) % nitems_per_line;
01228                 int tail_nitems = 0;
01229                 bool is_head_read = false;
01230 
01231                 for (int j = 0; j < ylen; j++) {
01232 
01233                         if (head_nitems > 0 && !is_head_read) {
01234                                 EMUtil::process_numbers_io(file, rw_mode, nitems_per_line, mode_size,
01235                                                                                    nitems_per_line-head_nitems,
01236                                                                                    nitems_per_line-1, data, &ti, outformat);
01237                                 rlines++;
01238                         }
01239 
01240                         EMUtil::process_lines_io(file, rw_mode, nitems_per_line,
01241                                                                          mode_size, (xlen - head_nitems),
01242                                                                          data, &ti, outformat);
01243 
01244                         rlines += ((xlen - head_nitems)/nitems_per_line);
01245 
01246                         tail_nitems = (xlen - head_nitems) % nitems_per_line;
01247 
01248                         if ((gap_nitems + tail_nitems) > 0) {
01249                                 head_nitems = nitems_per_line -
01250                                         (gap_nitems + tail_nitems) % nitems_per_line;
01251                         }
01252                         else {
01253                                 head_nitems = 0;
01254                         }
01255 
01256                         is_head_read = false;
01257 
01258                         if (tail_nitems > 0) {
01259                                 if ((gap_nitems < (nitems_per_line-tail_nitems)) &&
01260                                         (j != (ylen-1))) {
01261                                         EMUtil::exclude_numbers_io(file, rw_mode, nitems_per_line,
01262                                                                                            mode_size, tail_nitems,
01263                                                                                            tail_nitems+gap_nitems-1, data, &ti, outformat);
01264                                         is_head_read = true;
01265                                         rlines++;
01266                                 }
01267                                 else {
01268                                         EMUtil::process_numbers_io(file, rw_mode, nitems_per_line, mode_size,
01269                                                                                            0, tail_nitems-1, data, &ti, outformat);
01270                                         rlines++;
01271                                 }
01272                         }
01273 
01274                         if (gap_nitems > (nitems_per_line-tail_nitems)) {
01275                                 int gap_nlines = (gap_nitems - (nitems_per_line-tail_nitems)) /
01276                                         nitems_per_line;
01277                                 if (gap_nlines > 0 && j != (ylen-1)) {
01278                                         EMUtil::jump_lines(file, gap_nlines);
01279                                 }
01280                         }
01281                 }
01282 
01283                 int ytail_nitems = (ny-ylen-y0) * nx + (nx-xlen-x0) - (nitems_per_line-tail_nitems);
01284                 EMUtil::jump_lines_by_items(file, ytail_nitems, nitems_per_line);
01285         }
01286 }

void EMUtil::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
[static, private]
 

Definition at line 1427 of file emutil.cpp.

References Assert, data, and process_numbers_io().

Referenced by process_ascii_region_io().

01431 {
01432         Assert(file);
01433         Assert(data);
01434         Assert(p_i);
01435 
01436         if (nitems > 0) {
01437                 int nlines = nitems / nitems_per_line;
01438                 for (int i = 0; i < nlines; i++) {
01439                         EMUtil::process_numbers_io(file, rw_mode, nitems_per_line, mode_size, 0,
01440                                                                            nitems_per_line-1, data, p_i, outformat);
01441                 }
01442         }
01443 }

void EMUtil::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
[static, private]
 

Definition at line 1322 of file emutil.cpp.

References Assert, data, and portable_fseek().

Referenced by process_ascii_region_io(), and process_lines_io().

01325 {
01326         Assert(file);
01327         Assert(start >= 0);
01328         Assert(start <= end);
01329         Assert(end <= nitems_per_line);
01330         Assert(data);
01331         Assert(p_i);
01332         Assert(outformat);
01333 
01334         char line[MAXPATHLEN];
01335 
01336         if (rw_mode == ImageIO::READ_ONLY) {
01337                 if (!fgets(line, sizeof(line), file)) {
01338                         Assert("read xplor file failed");
01339                 }
01340 
01341                 int nitems_in_line = (int) (strlen(line) / mode_size);
01342                 Assert(end <= nitems_in_line);
01343                 vector<float> d(nitems_in_line);
01344                 char * pline = line;
01345 
01346                 for (int i = 0; i < nitems_in_line; i++) {
01347                         sscanf(pline, "%f", &d[i]);
01348                         pline += (int)mode_size;
01349                 }
01350 
01351 
01352                 for (int i = start; i <= end; i++) {
01353                         data[*p_i] = d[i];
01354                         (*p_i)++;
01355                 }
01356         }
01357         else {
01358                 portable_fseek(file, mode_size * start, SEEK_CUR);
01359                 for (int i = start; i <= end; i++) {
01360                         fprintf(file, outformat, data[*p_i]);
01361                         (*p_i)++;
01362                 }
01363 
01364                 portable_fseek(file, mode_size * (nitems_per_line - end-1)+1, SEEK_CUR);
01365         }
01366 }

void EMUtil::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
[static]
 

Process image region IO.

It eithers read a region from an image file. Or write a region to an image file. Works for regions that are outside the image data dimension area.(David Woolford, April 23 2009)

Parameters:
cdata Data array.
file The image file pointer.
rw_mode Read/write mode. It is either READ_ONLY or WRITE_ONLY.
image_index Image index.
mode_size Pixel size.
nx Image x size.
ny Image y size.
nz Image z size.
area The region to read/write.
need_flip Do we need flip the image?
imgtype The Image type of the processed file.
pre_row File size needed to be skipped before each row.
post_row File size needed to be skipped after each row.
Exceptions:
ImageReadException If the read has some error.
ImageWriteException If the write has some error.

Definition at line 771 of file emutil.cpp.

References Assert, EMAN::Region::get_ndim(), EMAN::Region::get_origin(), get_region_dims(), EMAN::Region::get_size(), IMAGE_ICOS, ImageReadException, ImageWriteException, nx, ny, portable_fseek(), UnexpectedBehaviorException, EMAN::Vec3d, and EMAN::Vec3i.

00776 {
00777         Assert(vdata != 0);
00778         Assert(file != 0);
00779         Assert(rw_mode == ImageIO::READ_ONLY ||
00780                    rw_mode == ImageIO::READ_WRITE ||
00781                    rw_mode == ImageIO::WRITE_ONLY);
00782 
00783         if (mode_size == 0) throw UnexpectedBehaviorException("The mode size was 0?");
00784 
00785         unsigned char * cdata = (unsigned char *)vdata;
00786 
00787         int dx0 = 0; // data x0
00788         int dy0 = 0; // data y0
00789         int dz0 = 0; // data z0
00790 
00791         int fx0 = 0; // file x0
00792         int fy0 = 0; // file y0
00793         int fz0 = nz > 1 ? 0 : image_index; // file z0
00794 
00795 
00796         int xlen = 0;
00797         int ylen = 0;
00798         int zlen = 0;
00799         get_region_dims(area, nx, &xlen, ny, &ylen, nz, &zlen);
00800 
00801         if (area) { // Accommodate for all boundary overlaps of the region
00802 
00803                 Vec3i origin = area->get_origin();
00804 
00805 
00806                 fx0 = origin[0]; dx0 = origin[0];
00807                 fy0 = origin[1]; dy0 = origin[1];
00808                 if (nz > 1 && area->get_ndim() > 2) {
00809                         fz0 = origin[2]; dz0 = origin[2];
00810                 }
00811 
00812                 if (need_flip) {
00813                         Vec3i size = area->get_size();
00814                         fy0 = ny-(origin[1]+size[1]);
00815                 }
00816 
00817                 if (fx0 < 0) {
00818                         dx0 *= -1;
00819                         xlen = xlen + fx0; // because there are less reads
00820                         fx0 = 0;
00821                 }else {
00822                         dx0 = 0;
00823                         //fx0 *= -1;
00824                 }
00825                 if (fy0 < 0) {
00826                         dy0 *= -1;
00827                         ylen = ylen + fy0; // because there are less reads
00828                         fy0 = 0;
00829                 }else {
00830                         if (need_flip){
00831                                 dy0*=-1;
00832                         }
00833                         else dy0 = 0;
00834                         //fy0 *= -1;
00835                 }
00836                 if (fz0 < 0) {
00837                         dz0 *= -1;
00838                         zlen = zlen + fz0; // because there are less reads
00839                         fz0 = 0;
00840                 }else {
00841                         dz0 = 0;
00842                         //fz0 *= -1;
00843                 }
00844 
00845                 if ((fx0 + xlen)> nx) xlen = nx-fx0;
00846                 if ((fy0 + ylen)> ny) ylen = ny-fy0;
00847                 if ((fz0 + zlen)> nz) zlen = nz-fz0;
00848                 if ( xlen <= 0 || ylen <= 0 || zlen <= 0 ) return; // This is fine the region was entirely outside the image
00849         }
00850 
00851         if ( xlen <= 0 ) {
00852                 cout << "Xlen was too small " << xlen << endl;
00853                 return;
00854         }
00855 
00856         Vec3i size;
00857         if (area != 0) size = area->get_size();
00858         else size = Vec3d(nx,ny,nz);
00859 
00860         //size_t area_sec_size = xlen * ylen * mode_size;
00861         size_t memory_sec_size = size[0] * size[1] * mode_size;
00862         size_t img_row_size = nx * mode_size + pre_row + post_row;
00863         size_t area_row_size = xlen * mode_size;
00864         size_t memory_row_size = size[0] * mode_size;
00865 
00866         if ( area_row_size <= 0 ) {
00867                 cout << "Xlen was too small " << xlen << " mode_size " << mode_size << endl;
00868                 return;
00869         }
00870 
00871         size_t x_pre_gap = fx0 * mode_size;
00872         size_t x_post_gap = (nx - fx0 - xlen) * mode_size;
00873 
00874         size_t y_pre_gap = fy0 * img_row_size;
00875         size_t y_post_gap = (ny - fy0 - ylen) * img_row_size;
00876 
00877         portable_fseek(file, img_row_size * ny * fz0, SEEK_CUR);
00878 
00879         float nxlendata[1];
00880         int floatsize = (int) sizeof(float);
00881         nxlendata[0] = (float)(nx * floatsize);
00882 
00883         for (int k = dz0; k < (dz0+zlen); k++) {
00884                 if (y_pre_gap > 0) {
00885                         portable_fseek(file, y_pre_gap, SEEK_CUR);
00886                 }
00887                 //long k2 = k * area_sec_size;
00888                 long k2 = k*memory_sec_size;
00889 
00890                 for (int j = dy0; j < (dy0+ylen); j++) {
00891                         if (pre_row > 0) {
00892                                 if (imgtype == IMAGE_ICOS && rw_mode != ImageIO::READ_ONLY && !area) {
00893                                         fwrite(nxlendata, floatsize, 1, file);
00894                                 }
00895                                 else {
00896                                         portable_fseek(file, pre_row, SEEK_CUR);
00897                                 }
00898                         }
00899 
00900                         if (x_pre_gap > 0) {
00901                                 portable_fseek(file, x_pre_gap, SEEK_CUR);
00902                         }
00903 
00904                         int jj = j;
00905                         if (need_flip) {
00906                                 jj = (dy0+ylen) - 1 - j;
00907                                 if (dy0 > 0 ) { // region considerations add complications in the flipping scenario (imagic format)
00908                                         jj += dy0;
00909                                 }
00910                         }
00911 
00912                         if (rw_mode == ImageIO::READ_ONLY) {
00913                                 if (fread(&cdata[k2 + jj * memory_row_size+dx0*mode_size],
00914                                                   area_row_size, 1, file) != 1) {
00915                                         cout << jj << " " << k2 << " " << memory_row_size << " " << dx0 << " " << mode_size << " " << area_row_size << " " << cdata << "done" << endl;
00916                                         throw ImageReadException("", "incomplete data read");
00917                                 }
00918                         }
00919                         else {
00920                                 if (fwrite(&cdata[k2 + jj * memory_row_size+dx0*mode_size],
00921                                                    area_row_size, 1, file) != 1) {
00922                                         throw ImageWriteException("", "incomplete data write");
00923                                 }
00924                         }
00925 
00926                         if (x_post_gap > 0) {
00927                                 portable_fseek(file, x_post_gap, SEEK_CUR);
00928                         }
00929 
00930                         if (post_row > 0) {
00931                                 if (imgtype == IMAGE_ICOS && rw_mode != ImageIO::READ_ONLY && !area) {
00932                                         fwrite(nxlendata, floatsize, 1, file);
00933                                 }
00934                                 else {
00935                                         portable_fseek(file, post_row, SEEK_CUR);
00936                                 }
00937                         }
00938                 }
00939 
00940                 if (y_post_gap > 0) {
00941                         portable_fseek(file, y_post_gap, SEEK_CUR);
00942                 }
00943         }
00944 }

EMData * EMUtil::vertical_acf const EMData image,
int  maxdy
[static]
 

Definition at line 988 of file emutil.cpp.

References data, dot(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), NullPointerException, nx, ny, EMAN::EMData::set_size(), EMAN::EMData::update(), x, and y.

00989 {
00990         if (!image) {
00991                 throw NullPointerException("NULL Image");
00992         }
00993 
00994         EMData *ret = new EMData();
00995         int nx = image->get_xsize();
00996         int ny = image->get_ysize();
00997 
00998         if (maxdy <= 1) {
00999                 maxdy = ny / 8;
01000         }
01001 
01002         ret->set_size(nx, maxdy, 1);
01003 
01004         float *data = image->get_data();
01005         float *ret_data = ret->get_data();
01006 
01007         for (int x = 0; x < nx; x++) {
01008                 for (int y = 0; y < maxdy; y++) {
01009                         float dot = 0;
01010                         for (int yy = maxdy; yy < ny - maxdy; yy++) {
01011                                 dot += data[x + (yy + y) * nx] * data[x + (yy - y) * nx];
01012                         }
01013                         ret_data[x + y * nx] = dot;
01014                 }
01015         }
01016 
01017         ret->update();
01018 
01019         return ret;
01020 }


The documentation for this class was generated from the following files:
Generated on Thu Dec 9 13:47:24 2010 for EMAN2 by  doxygen 1.3.9.1