EMAN2
Public Types | Static Public Member Functions | Static Private Member Functions
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_DM4,
  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_OMAP, IMAGE_SITUS,
  IMAGE_SER
}
 Image format types. More...

Static Public Member Functions

static EMDatavertical_acf (const EMData *image, int maxdy)
static EMDatamake_image_median (const vector< EMData * > &image_list)
static ImageType get_image_ext_type (const string &file_ext)
 Get an image's format type from its filename extension.
static ImageType get_image_type (const string &filename)
 Get an image's format type by processing the first 1K of the image.
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 whether or not the return value of EMUtil.get_image_ext_type is IMAGE_UNKNOWN.
static int get_image_count (const string &filename)
 Get the number of images in an image file.
static ImageIOget_imageio (const string &filename, int rw_mode, ImageType image_type=IMAGE_UNKNOWN)
 Get an ImageIO object.
static void close_imageio (const string &filename, const ImageIO *io)
 Ian: Close ImageIO object.
static const char * get_imagetype_name (EMUtil::ImageType type)
 Give each image type a meaningful name.
static const char * get_datatype_string (EMDataType type)
 Give each data type a meaningful name.
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.
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.
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.
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.
static void dump_dict (const Dict &dict)
 Dump a Dict object.
static bool is_same_size (const EMData *image1, const EMData *image2)
 Check whether two EMData images are of the same size.
static bool is_same_ctf (const EMData *image1, const EMData *image2)
 Check whether two EMData images have the same CTF parameters.
static bool is_complex_type (EMDataType datatype)
static void jump_lines (FILE *file, int nlines)
static vector< string > get_euler_names (const string &euler_type)
static vector< EMObjectget_all_attributes (const string &file_name, const string &attr_name)
 Get an attribute from a stack of image, returned as a vector.
static 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.
static bool cuda_available ()
static void * em_malloc (const size_t size)
static void * em_calloc (const size_t nmemb, const size_t size)
static void * em_realloc (void *data, const size_t new_size)
static void em_memset (void *data, const int value, const size_t size)
static void em_free (void *data)
static void em_memcpy (void *dst, const void *const src, const size_t size)

Static Private Member Functions

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

Detailed Description

Definition at line 85 of file emutil.h.


Member Enumeration Documentation

Image pixel data type used in EMAN.

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

Enumerator:
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.

Image format types.

Enumerator:
IMAGE_UNKNOWN 
IMAGE_MRC 
IMAGE_SPIDER 
IMAGE_SINGLE_SPIDER 
IMAGE_IMAGIC 
IMAGE_HDF 
IMAGE_DM3 
IMAGE_DM4 
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_OMAP 
IMAGE_SITUS 
IMAGE_SER 

Definition at line 110 of file emutil.h.


Member Function Documentation

void EMUtil::close_imageio ( const string &  filename,
const ImageIO io 
) [static]

Ian: Close ImageIO object.

Definition at line 700 of file emutil.cpp.

Referenced by get_image_count(), EMAN::EMData::read_binedimage(), EMAN::EMData::read_image(), and EMAN::EMData::write_image().

{
    //printf("EMUtil::close_imageio\n");
    #ifdef IMAGEIO_CACHE
    if (GlobalCache::instance()->contains(filename)) {
        GlobalCache::instance()->close_imageio(filename);
    } else {
        delete io;
    }
    #else
    delete io;
    #endif
}
static bool EMAN::EMUtil::cuda_available ( ) [inline, static]

Definition at line 339 of file emutil.h.

                                             {
//#ifdef EMAN2_USING_CUDA
//                      return true;
//#else
                        return false;
//#endif
                }
void EMUtil::dump_dict ( const Dict dict) [static]

Dump a Dict object.

Parameters:
dictA Dict object.

Definition at line 1086 of file emutil.cpp.

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

Referenced by main().

{
        vector < string > keys = dict.keys();
        vector < EMObject > values = dict.values();

        for (unsigned int i = 0; i < keys.size(); i++) {
                EMObject obj = values[i];

                if (! obj.is_null()) {
                        string val = obj.to_str();

                        if (keys[i] == "datatype") {
                                val = get_datatype_string((EMDataType) (int) obj);
                        }

                        fprintf(stdout, "%25s\t%s\n", keys[i].c_str(), val.c_str());
                }
        }
}
static void* EMAN::EMUtil::em_calloc ( const size_t  nmemb,
const size_t  size 
) [inline, static]

Definition at line 351 of file emutil.h.

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

                                                                                    {
                        return calloc(nmemb,size);
                }
static void EMAN::EMUtil::em_free ( void *  data) [inline, static]
static void* EMAN::EMUtil::em_malloc ( const size_t  size) [inline, static]

Definition at line 347 of file emutil.h.

Referenced by EMAN::EMData::EMData(), EMAN::EMData::rotate_x(), EMAN::EMData::set_size(), and EMAN::TransformProcessor::transform().

                                                                 {
                        return malloc(size);
                }
static void EMAN::EMUtil::em_memcpy ( void *  dst,
const void *const  src,
const size_t  size 
) [inline, static]
static void EMAN::EMUtil::em_memset ( void *  data,
const int  value,
const size_t  size 
) [inline, static]

Definition at line 358 of file emutil.h.

Referenced by EMAN::EMData::clip_inplace(), and EMAN::EMData::set_size().

                                                                                             {
                        memset(data, value, size);
                }
static void* EMAN::EMUtil::em_realloc ( void *  data,
const size_t  new_size 
) [inline, static]

Definition at line 355 of file emutil.h.

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

                                                                                 {
                        return realloc(data, new_size);
                }
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 1516 of file emutil.cpp.

References Assert, portable_fseek(), and EMAN::ImageIO::READ_ONLY.

Referenced by process_ascii_region_io().

{
        Assert(file);
        Assert(mode_size > 0);
        Assert(start >= 0);
        Assert(end <= nitems_per_line);
        Assert(data);
        Assert(p_i);
        Assert(outformat);

        char line[MAXPATHLEN];

        if (rw_mode == ImageIO::READ_ONLY) {

                if (!fgets(line, sizeof(line), file)) {
                        Assert("read xplor file failed");
                }

                int nitems_in_line =  (int) (strlen(line) / mode_size);
                Assert(end <= nitems_in_line);

                vector<float> d(nitems_in_line);
                char *pline = line;

                for (int i = 0; i < nitems_in_line; i++) {
                        sscanf(pline, "%f", &d[i]);
                        pline = pline + (int)mode_size;
                }


                for (int i = 0; i < start; i++) {
                        data[*p_i] = d[i];
                        (*p_i)++;
                }

                for (int i = end+1; i < nitems_in_line; i++) {
                        data[*p_i] = d[i];
                        (*p_i)++;
                }
        }
        else {
                for (int i = 0; i < start; i++) {
                        fprintf(file, outformat, data[*p_i]);
                        (*p_i)++;
                }

                portable_fseek(file, (end-start+1) * mode_size, SEEK_CUR);

                for (int i = end+1; i < nitems_per_line; i++) {
                        fprintf(file, outformat, data[*p_i]);
                        (*p_i)++;
                }
                portable_fseek(file, 1, SEEK_CUR);
        }
}
EMUtil::ImageType EMUtil::fast_get_image_type ( const string &  filename,
const void *  first_block,
off_t  file_size 
) [static, private]

Definition at line 227 of file emutil.cpp.

References Assert, ENTERFUNC, EXITFUNC, EMAN::Util::get_filename_ext(), get_image_ext_type(), IMAGE_AMIRA, IMAGE_DF3, IMAGE_DM3, IMAGE_DM4, IMAGE_EM, IMAGE_GATAN2, IMAGE_HDF, IMAGE_ICOS, IMAGE_IMAGIC, IMAGE_LST, IMAGE_LSTFAST, IMAGE_MRC, IMAGE_OMAP, IMAGE_PGM, IMAGE_PIF, IMAGE_PNG, IMAGE_SAL, IMAGE_SER, IMAGE_SINGLE_SPIDER, IMAGE_SITUS, IMAGE_SPIDER, IMAGE_TIFF, IMAGE_UNKNOWN, IMAGE_V4L, IMAGE_VTK, IMAGE_XPLOR, EMAN::ImagicIO::is_valid(), EMAN::SerIO::is_valid(), EMAN::SitusIO::is_valid(), EMAN::OmapIO::is_valid(), 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::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::DM4IO::is_valid(), EMAN::DM3IO::is_valid(), and EMAN::MrcIO::is_valid().

Referenced by get_image_type().

{
        ENTERFUNC;

        Assert(filename != "");
        Assert(first_block != 0);
        Assert(file_size > 0);

#ifdef ENABLE_V4L2
        if (filename.compare(0,5,"/dev/")==0) return IMAGE_V4L;
#endif

        string ext = Util::get_filename_ext(filename);

        if (ext == "") {
                return IMAGE_UNKNOWN;
        }

        ImageType image_type = get_image_ext_type(ext);

        switch (image_type) {
        case IMAGE_MRC:
                if (MrcIO::is_valid(first_block, file_size)) {
                        return IMAGE_MRC;
                }
                break;
        case IMAGE_DM3:
                if (DM3IO::is_valid(first_block)) {
                        return IMAGE_DM3;
                }
                break;
        case IMAGE_DM4:
                if (DM4IO::is_valid(first_block)) {
                        return IMAGE_DM4;
                }
                break;
#ifdef EM_HDF5
        case IMAGE_HDF:
                if (HdfIO2::is_valid(first_block)) {
                        return IMAGE_HDF;
                }
                break;
#endif
        case IMAGE_LST:
                if (LstIO::is_valid(first_block)) {
                        return IMAGE_LST;
                }
                break;
        case IMAGE_LSTFAST:
                if (LstFastIO::is_valid(first_block)) {
                        return IMAGE_LSTFAST;
                }
                break;
#ifdef EM_TIFF
        case IMAGE_TIFF:
                if (TiffIO::is_valid(first_block)) {
                        return IMAGE_TIFF;
                }
                break;
#endif
        case IMAGE_SPIDER:
                if (SpiderIO::is_valid(first_block)) {
                        return IMAGE_SPIDER;
                }
                break;
        case IMAGE_SINGLE_SPIDER:
                if (SingleSpiderIO::is_valid(first_block)) {
                        return IMAGE_SINGLE_SPIDER;
                }
                break;
        case IMAGE_PIF:
                if (PifIO::is_valid(first_block)) {
                        return IMAGE_PIF;
                }
                break;
#ifdef EM_PNG
        case IMAGE_PNG:
                if (PngIO::is_valid(first_block)) {
                        return IMAGE_PNG;
                }
                break;
#endif
        case IMAGE_VTK:
                if (VtkIO::is_valid(first_block)) {
                        return IMAGE_VTK;
                }
                break;
        case IMAGE_PGM:
                if (PgmIO::is_valid(first_block)) {
                        return IMAGE_PGM;
                }
                break;
        case IMAGE_ICOS:
                if (IcosIO::is_valid(first_block)) {
                        return IMAGE_ICOS;
                }
                break;
        case IMAGE_SAL:
                if (SalIO::is_valid(first_block)) {
                        return IMAGE_SAL;
                }
                break;
        case IMAGE_AMIRA:
                if (AmiraIO::is_valid(first_block)) {
                        return IMAGE_AMIRA;
                }
                break;
        case IMAGE_XPLOR:
                if (XplorIO::is_valid(first_block)) {
                        return IMAGE_XPLOR;
                }
                break;
        case IMAGE_GATAN2:
                if (Gatan2IO::is_valid(first_block)) {
                        return IMAGE_GATAN2;
                }
                break;
        case IMAGE_EM:
                if (EmIO::is_valid(first_block, file_size)) {
                        return IMAGE_EM;
                }
                break;
        case IMAGE_DF3:
                if (EmIO::is_valid(first_block, file_size)) {
                        return IMAGE_DF3;
                }
                break;
        case IMAGE_OMAP:
                if (OmapIO::is_valid(first_block, file_size)) {
                        return IMAGE_OMAP;
                }
                break;
        case IMAGE_SITUS:
                if (SitusIO::is_valid(first_block)) {
                        return IMAGE_SITUS;
                }
                break;
        case IMAGE_SER:
                if (SerIO::is_valid(first_block)) {
                        return IMAGE_SER;
                }
                break;
        case IMAGE_IMAGIC:
                if (ImagicIO::is_valid(first_block)) {
                        return IMAGE_IMAGIC;
                }
                break;
        default:
                return IMAGE_UNKNOWN;
        }

        EXITFUNC;

        return IMAGE_UNKNOWN;
}
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_namethe image file name
attr_nameThe header attribute name.
Returns:
the vector of attribute value
Exceptions:
NotExistingObjectExceptionwhen access an non-existing attribute
InvalidCallExceptionwhen call this function for a non-stack image

Definition at line 1636 of file emutil.cpp.

References Assert, read_images(), and v.

{
        vector<EMObject> v;

        Assert(file_name != "");
        Assert(attr_name != "");

        vector< shared_ptr<EMData> > vpImg = EMData::read_images(file_name, vector<int>(), true);
        vector< shared_ptr<EMData> >::const_iterator iter;

        for (iter = vpImg.begin(); iter!=vpImg.end(); ++iter) {
                v.push_back((*iter)->get_attr_default(attr_name));
        }

        return v;
}
const char * EMUtil::get_datatype_string ( EMDataType  type) [static]

Give each data type a meaningful name.

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

Definition at line 808 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(), and main().

{
        switch (type) {
        case EM_CHAR:
                return "CHAR";
        case EM_UCHAR:
                return "UNSIGNED CHAR";
        case EM_SHORT:
                return "SHORT";
        case EM_USHORT:
                return "UNSIGNED SHORT";
        case EM_INT:
                return "INT";
        case EM_UINT:
                return "UNSIGNED INT";
        case EM_FLOAT:
                return "FLOAT";
        case EM_DOUBLE:
                return "DOUBLE";
        case EM_SHORT_COMPLEX:
                return "SHORT_COMPLEX";
        case EM_USHORT_COMPLEX:
                return "USHORT_COMPLEX";
        case EM_FLOAT_COMPLEX:
                return "FLOAT_COMPLEX";
        case EM_UNKNOWN:
                return "UNKNOWN";
        }

        return "UNKNOWN";
}
vector< string > EMUtil::get_euler_names ( const string &  euler_type) [static]

Definition at line 1593 of file emutil.cpp.

References b, and v.

{
    vector<string> v;
    string b = "euler_";

    if (euler_type == "EMAN") {
        v.push_back(b + "alt");
        v.push_back(b + "az");
        v.push_back(b + "phi");
    }
    else if (euler_type == "MRC") {
        v.push_back(b + "theta");
        v.push_back(b + "phi");
        v.push_back(b + "omega");
    }
    else if (euler_type == "IMAGIC") {
        v.push_back(b + "alpha");
        v.push_back(b + "beta");
        v.push_back(b + "gamma");
    }
    else if (euler_type == "SPIDER") {
        v.push_back(b + "phi");
        v.push_back(b + "theta");
        v.push_back(b + "gamma");
    }
    else if (euler_type == "SPIN" ||
             euler_type == "SGIROT") {
        v.push_back(b + "q");
        v.push_back(b + "n1");
        v.push_back(b + "n2");
        v.push_back(b + "n3");
    }

    else if (euler_type == "QUATERNION") {
        v.push_back(b + "e0");
        v.push_back(b + "e1");
        v.push_back(b + "e2");
        v.push_back(b + "e3");
    }

    return v;
}
int EMUtil::get_image_count ( const string &  filename) [static]

Get the number of images in an image file.

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

Definition at line 524 of file emutil.cpp.

References Assert, close_imageio(), ENTERFUNC, EXITFUNC, get_imageio(), EMAN::ImageIO::get_nimg(), and EMAN::ImageIO::READ_ONLY.

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

{
        ENTERFUNC;

        Assert(filename != "");

        int nimg = 0;

        ImageIO *imageio = get_imageio(filename, ImageIO::READ_ONLY);

        if (imageio) {
                nimg = imageio->get_nimg();
        }

   EMUtil::close_imageio(filename, imageio);

        imageio = NULL;

        EXITFUNC;

        return nimg;
}
EMUtil::ImageType EMUtil::get_image_ext_type ( const string &  file_ext) [static]

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

Parameters:
file_extFile extension.
Returns:
image format type.

Definition at line 63 of file emutil.cpp.

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

Referenced by fast_get_image_type(), is_valid_filename(), and EMAN::EMData::write_image().

{
        ENTERFUNC;

        static bool initialized = false;
        static map < string, ImageType > imagetypes;

        if (!initialized) {
                imagetypes["rec"] = IMAGE_MRC;
                imagetypes["mrc"] = IMAGE_MRC;
                imagetypes["MRC"] = IMAGE_MRC;
                imagetypes["ali"] = IMAGE_MRC;
                imagetypes["st"] = IMAGE_MRC;           // IMOD stack file

                imagetypes["mrcs"] = IMAGE_MRC;
                imagetypes["MRCS"] = IMAGE_MRC;

                imagetypes["raw"] = IMAGE_MRC;
                imagetypes["RAW"] = IMAGE_MRC;

                imagetypes["tnf"] = IMAGE_MRC;
                imagetypes["TNF"] = IMAGE_MRC;

                imagetypes["ccp4"] = IMAGE_MRC;
                imagetypes["map"] = IMAGE_MRC;

                imagetypes["dm3"] = IMAGE_DM3;
                imagetypes["DM3"] = IMAGE_DM3;

                imagetypes["dm4"] = IMAGE_DM4;
                imagetypes["DM4"] = IMAGE_DM4;

                imagetypes["spi"] = IMAGE_SPIDER;
                imagetypes["SPI"] = IMAGE_SPIDER;

                imagetypes["spider"] = IMAGE_SPIDER;
                imagetypes["SPIDER"] = IMAGE_SPIDER;

                imagetypes["spidersingle"] = IMAGE_SINGLE_SPIDER;
                imagetypes["SPIDERSINGLE"] = IMAGE_SINGLE_SPIDER;

                imagetypes["singlespider"] = IMAGE_SINGLE_SPIDER;
                imagetypes["SINGLESPIDER"] = IMAGE_SINGLE_SPIDER;

                imagetypes["img"] = IMAGE_IMAGIC;
                imagetypes["IMG"] = IMAGE_IMAGIC;

                imagetypes["hed"] = IMAGE_IMAGIC;
                imagetypes["HED"] = IMAGE_IMAGIC;

                imagetypes["imagic"] = IMAGE_IMAGIC;
                imagetypes["IMAGIC"] = IMAGE_IMAGIC;

                imagetypes["pgm"] = IMAGE_PGM;
                imagetypes["PGM"] = IMAGE_PGM;

                imagetypes["lst"] = IMAGE_LST;
                imagetypes["LST"] = IMAGE_LST;

                imagetypes["lsx"] = IMAGE_LSTFAST;      // but .lst or another extension would also be ok
                imagetypes["LSX"] = IMAGE_LSTFAST;

                imagetypes["pif"] = IMAGE_PIF;
                imagetypes["PIF"] = IMAGE_PIF;

                imagetypes["png"] = IMAGE_PNG;
                imagetypes["PNG"] = IMAGE_PNG;

                imagetypes["h5"] = IMAGE_HDF;
                imagetypes["H5"] = IMAGE_HDF;

                imagetypes["hd5"] = IMAGE_HDF;
                imagetypes["HD5"] = IMAGE_HDF;

                imagetypes["hdf"] = IMAGE_HDF;
                imagetypes["HDF"] = IMAGE_HDF;

                imagetypes["tif"] = IMAGE_TIFF;
                imagetypes["TIF"] = IMAGE_TIFF;

                imagetypes["tiff"] = IMAGE_TIFF;
                imagetypes["TIFF"] = IMAGE_TIFF;

                imagetypes["fts"] = IMAGE_FITS;
                imagetypes["FTS"] = IMAGE_FITS;

                imagetypes["vtk"] = IMAGE_VTK;
                imagetypes["VTK"] = IMAGE_VTK;

                imagetypes["hdr"] = IMAGE_SAL;
                imagetypes["HDR"] = IMAGE_SAL;

                imagetypes["sal"] = IMAGE_SAL;
                imagetypes["SAL"] = IMAGE_SAL;

                imagetypes["map"] = IMAGE_ICOS;
                imagetypes["MAP"] = IMAGE_ICOS;

                imagetypes["icos"] = IMAGE_ICOS;
                imagetypes["ICOS"] = IMAGE_ICOS;

                imagetypes["am"] = IMAGE_AMIRA;
                imagetypes["AM"] = IMAGE_AMIRA;

                imagetypes["amira"] = IMAGE_AMIRA;
                imagetypes["AMIRA"] = IMAGE_AMIRA;

                imagetypes["emim"] = IMAGE_EMIM;
                imagetypes["EMIM"] = IMAGE_EMIM;

                imagetypes["xplor"] = IMAGE_XPLOR;
                imagetypes["XPLOR"] = IMAGE_XPLOR;

                imagetypes["em"] = IMAGE_EM;
                imagetypes["EM"] = IMAGE_EM;

                imagetypes["dm2"] = IMAGE_GATAN2;
                imagetypes["DM2"] = IMAGE_GATAN2;

                imagetypes["v4l"] = IMAGE_V4L;
                imagetypes["V4L"] = IMAGE_V4L;

                imagetypes["jpg"] = IMAGE_JPEG;
                imagetypes["JPG"] = IMAGE_JPEG;
                imagetypes["jpeg"] = IMAGE_JPEG;
                imagetypes["JPEG"] = IMAGE_JPEG;

                imagetypes["df3"] = IMAGE_DF3;
                imagetypes["DF3"] = IMAGE_DF3;

                imagetypes["Omap"] = IMAGE_OMAP;
                imagetypes["omap"] = IMAGE_OMAP;
                imagetypes["OMAP"] = IMAGE_OMAP;
                imagetypes["BRIX"] = IMAGE_OMAP;
                imagetypes["brix"] = IMAGE_OMAP;
                imagetypes["DSN6"] = IMAGE_OMAP;

                imagetypes["situs"] = IMAGE_SITUS;
                imagetypes["SITUS"] = IMAGE_SITUS;

                imagetypes["ser"] = IMAGE_SER;
                imagetypes["SER"] = IMAGE_SER;

                initialized = true;
        }

        ImageType result = IMAGE_UNKNOWN;

        if (imagetypes.find(file_ext) != imagetypes.end()) {
                result = imagetypes[file_ext];
        }

        EXITFUNC;

        return result;
}
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:
filenameImage file name.
Returns:
image format type.

Definition at line 385 of file emutil.cpp.

References Assert, EMAN::Util::change_filename_ext(), ENTERFUNC, EXITFUNC, fast_get_image_type(), FileAccessException, EMAN::Util::get_filename_ext(), EMAN::ImagicIO::HED_EXT, IMAGE_AMIRA, IMAGE_DF3, IMAGE_DM3, IMAGE_DM4, IMAGE_EM, IMAGE_FITS, IMAGE_GATAN2, IMAGE_HDF, IMAGE_ICOS, IMAGE_IMAGIC, IMAGE_LST, IMAGE_LSTFAST, IMAGE_MRC, IMAGE_OMAP, IMAGE_PGM, IMAGE_PIF, IMAGE_PNG, IMAGE_SAL, IMAGE_SER, IMAGE_SINGLE_SPIDER, IMAGE_SITUS, IMAGE_SPIDER, IMAGE_TIFF, IMAGE_UNKNOWN, IMAGE_V4L, IMAGE_VTK, IMAGE_XPLOR, ImageFormatException, EMAN::ImagicIO::IMG_EXT, in, EMAN::Df3IO::is_valid(), EMAN::ImagicIO::is_valid(), EMAN::SerIO::is_valid(), EMAN::SitusIO::is_valid(), EMAN::OmapIO::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::PgmIO::is_valid(), EMAN::VtkIO::is_valid(), EMAN::PifIO::is_valid(), EMAN::LstFastIO::is_valid(), EMAN::LstIO::is_valid(), EMAN::DM4IO::is_valid(), EMAN::DM3IO::is_valid(), EMAN::MrcIO::is_valid(), EMAN::SingleSpiderIO::is_valid(), EMAN::SpiderIO::is_valid(), portable_fseek(), and portable_ftell().

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

{
        ENTERFUNC;

        Assert(in_filename != "");

#ifdef ENABLE_V4L2
        if (in_filename.compare(0,5,"/dev/")==0) return IMAGE_V4L;
#endif

        string filename = in_filename;

        string old_ext = Util::get_filename_ext(filename);

        if (old_ext == ImagicIO::IMG_EXT) {
                filename = Util::change_filename_ext(filename, ImagicIO::HED_EXT);
        }
        else if (old_ext == "hdf") {
                return IMAGE_HDF;
        }

        FILE *in = fopen(filename.c_str(), "rb");

        if (! in) {
                throw FileAccessException(filename);
        }

        char first_block[1024];
        size_t n = fread(first_block, sizeof(char), sizeof(first_block), in);
        portable_fseek(in, 0, SEEK_END);
        off_t file_size = portable_ftell(in);

        if (n == 0) {
//              This produces annoying console messages         
//              LOGERR("file '%s' is an empty file", filename.c_str());
                fclose(in);
                return IMAGE_UNKNOWN;
        }

        fclose(in);

        ImageType image_type = fast_get_image_type(filename, first_block, file_size);

        if (image_type != IMAGE_UNKNOWN) {
                return image_type;
        }

        if (SpiderIO::is_valid(first_block)) {
                image_type = IMAGE_SPIDER;
        }
        else if (SingleSpiderIO::is_valid(first_block)) {
                image_type = IMAGE_SINGLE_SPIDER;
        }
        else if (MrcIO::is_valid(first_block, file_size)) {
                image_type = IMAGE_MRC;
        }
        else if (DM3IO::is_valid(first_block)) {
                image_type = IMAGE_DM3;
        }
        else if (DM4IO::is_valid(first_block)) {
                image_type = IMAGE_DM4;
        }
#ifdef EM_HDF5
        else if (HdfIO2::is_valid(first_block)) {
                image_type = IMAGE_HDF;
        }
#endif
        else if (LstIO::is_valid(first_block)) {
                image_type = IMAGE_LST;
        }
        else if (LstFastIO::is_valid(first_block)) {
                image_type = IMAGE_LSTFAST;
        }
#ifdef EM_TIFF
        else if (TiffIO::is_valid(first_block)) {
                image_type = IMAGE_TIFF;
        }
#endif
        else if (PifIO::is_valid(first_block)) {
                image_type = IMAGE_PIF;
        }
#ifdef EM_PNG
        else if (PngIO::is_valid(first_block)) {
                image_type = IMAGE_PNG;
        }
#endif
        else if (VtkIO::is_valid(first_block)) {
                image_type = IMAGE_VTK;
        }
        else if (PgmIO::is_valid(first_block)) {
                image_type = IMAGE_PGM;
        }
        else if (IcosIO::is_valid(first_block)) {
                image_type = IMAGE_ICOS;
        }
        else if (SalIO::is_valid(first_block)) {
                image_type = IMAGE_SAL;
        }
        else if (AmiraIO::is_valid(first_block)) {
                image_type = IMAGE_AMIRA;
        }
        else if (XplorIO::is_valid(first_block)) {
                image_type = IMAGE_XPLOR;
        }
        else if (Gatan2IO::is_valid(first_block)) {
                image_type = IMAGE_GATAN2;
        }
        else if (FitsIO::is_valid(first_block)) {
                image_type = IMAGE_FITS;
        }
        else if (EmIO::is_valid(first_block, file_size)) {
                image_type = IMAGE_EM;
        }
        else if(OmapIO::is_valid(first_block, file_size)) {
                image_type = IMAGE_OMAP;
        }
        else if(SitusIO::is_valid(first_block)) {
                image_type = IMAGE_SITUS;
        }
        else if(SerIO::is_valid(first_block)) {
                image_type = IMAGE_SER;
        }
        else if (ImagicIO::is_valid(first_block)) {
                image_type = IMAGE_IMAGIC;
        }
        else if (Df3IO::is_valid(first_block)) {
                image_type = IMAGE_DF3;
        }
        else {
                // LOGERR("I don't know this image's type: '%s'", filename.c_str());

                throw ImageFormatException("invalid image type");
        }

        EXITFUNC;

        return image_type;
}
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:
filenameImage file name.
rw_modeImageIO read/write mode.
image_typeImage format type.
Returns:
An ImageIO object.

Definition at line 547 of file emutil.cpp.

References Assert, ENTERFUNC, EXITFUNC, get_image_type(), IMAGE_AMIRA, IMAGE_DF3, IMAGE_DM3, IMAGE_DM4, IMAGE_EM, IMAGE_FITS, IMAGE_GATAN2, IMAGE_HDF, IMAGE_ICOS, IMAGE_IMAGIC, IMAGE_JPEG, IMAGE_LST, IMAGE_LSTFAST, IMAGE_MRC, IMAGE_OMAP, IMAGE_PGM, IMAGE_PIF, IMAGE_PNG, IMAGE_SAL, IMAGE_SER, IMAGE_SINGLE_SPIDER, IMAGE_SITUS, IMAGE_SPIDER, IMAGE_TIFF, IMAGE_UNKNOWN, IMAGE_V4L, IMAGE_VTK, IMAGE_XPLOR, ImageFormatException, EMAN::ImageIO::READ_ONLY, EMAN::ImageIO::READ_WRITE, and EMAN::ImageIO::WRITE_ONLY.

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

{
        ENTERFUNC;

   // printf("EMUtil::get_imageio\n");

        Assert(filename != "");
        Assert(rw == ImageIO::READ_ONLY ||
                   rw == ImageIO::READ_WRITE ||
                   rw == ImageIO::WRITE_ONLY);

        ImageIO *imageio = 0;
   int persist = 0;

   #ifdef IMAGEIO_CACHE
   imageio = GlobalCache::instance()->get_imageio(filename, rw);
   if (imageio) {
        return imageio;
   }
   #endif

        ImageIO::IOMode rw_mode = static_cast < ImageIO::IOMode > (rw);

        if (image_type == IMAGE_UNKNOWN) {
                image_type = get_image_type(filename);
        }

        if (image_type == IMAGE_UNKNOWN) {
                if(rw == ImageIO::WRITE_ONLY || rw == ImageIO::READ_WRITE) {
                        throw ImageFormatException("writing to this image format not supported.");
                }
        }

        switch (image_type) {
#ifdef ENABLE_V4L2
        case IMAGE_V4L:
                imageio = new V4L2IO(filename, rw_mode);
                break;
#endif
        case IMAGE_MRC:
                imageio = new MrcIO(filename, rw_mode);
                break;
        case IMAGE_IMAGIC:
                imageio = new ImagicIO2(filename, rw_mode);
                if (rw_mode==ImageIO::READ_ONLY && ((ImagicIO2 *)imageio)->init_test()==-1 ) {
                        delete imageio;
                        imageio = new ImagicIO(filename, rw_mode);
                }
                break;
        case IMAGE_DM3:
                imageio = new DM3IO(filename, rw_mode);
                break;
        case IMAGE_DM4:
                imageio = new DM4IO(filename, rw_mode);
                break;
#ifdef EM_TIFF
        case IMAGE_TIFF:
                imageio = new TiffIO(filename, rw_mode);
                break;
#endif
#ifdef EM_HDF5
        case IMAGE_HDF:
        persist = 30;
        if (rw_mode != ImageIO::READ_ONLY) {
            persist = 3;
        }
                imageio = new HdfIO2(filename, rw_mode);
                if (((HdfIO2 *)imageio)->init_test()==-1) {
                        delete imageio;
                        imageio = new HdfIO(filename, rw_mode);
                }
                break;
#endif  //EM_HDF5
        case IMAGE_LST:
                imageio = new LstIO(filename, rw_mode);
                break;
        case IMAGE_LSTFAST:
                imageio = new LstFastIO(filename, rw_mode);
                break;
        case IMAGE_PIF:
                imageio = new PifIO(filename, rw_mode);
                break;
        case IMAGE_VTK:
                imageio = new VtkIO(filename, rw_mode);
                break;
        case IMAGE_SPIDER:
                imageio = new SpiderIO(filename, rw_mode);
                break;
        case IMAGE_SINGLE_SPIDER:
                imageio = new SingleSpiderIO(filename, rw_mode);
                break;
        case IMAGE_PGM:
                imageio = new PgmIO(filename, rw_mode);
                break;
#ifdef EM_JPEG
        case IMAGE_JPEG:
                imageio = new JpegIO(filename,rw_mode);
                break;
#endif
        case IMAGE_ICOS:
                imageio = new IcosIO(filename, rw_mode);
                break;
#ifdef EM_PNG
        case IMAGE_PNG:
                imageio = new PngIO(filename, rw_mode);
                break;
#endif
        case IMAGE_SAL:
                imageio = new SalIO(filename, rw_mode);
                break;
        case IMAGE_AMIRA:
                imageio = new AmiraIO(filename, rw_mode);
                break;
        case IMAGE_GATAN2:
                imageio = new Gatan2IO(filename, rw_mode);
                break;
        case IMAGE_EM:
                imageio = new EmIO(filename, rw_mode);
                break;
        case IMAGE_XPLOR:
                imageio = new XplorIO(filename, rw_mode);
                break;
        case IMAGE_FITS:
                imageio = new FitsIO(filename, rw_mode);
                break;
        case IMAGE_DF3:
                imageio = new Df3IO(filename, rw_mode);
                break;
        case IMAGE_OMAP:
                imageio = new OmapIO(filename, rw_mode);
                break;
        case IMAGE_SITUS:
                imageio = new SitusIO(filename, rw_mode);
                break;
        case IMAGE_SER:
                imageio = new SerIO(filename, rw_mode);
                break;
        default:
                break;
        }

   #ifdef IMAGEIO_CACHE
        if (persist > 0) {
                GlobalCache::instance()->add_imageio(filename, rw, persist, imageio);
        }
   #endif

        EXITFUNC;

        return imageio;
}
const char * EMUtil::get_imagetype_name ( EMUtil::ImageType  type) [static]

Give each image type a meaningful name.

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

Definition at line 714 of file emutil.cpp.

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

Referenced by main().

{
        switch (t) {
        case IMAGE_V4L:
                return "V4L2";
                break;
        case IMAGE_MRC:
                return "MRC";
                break;
        case IMAGE_SPIDER:
                return "SPIDER";
                break;
        case IMAGE_SINGLE_SPIDER:
                return "Single-SPIDER";
                break;
        case IMAGE_IMAGIC:
                return "IMAGIC";
                break;
        case IMAGE_PGM:
                return "PGM";
                break;
        case IMAGE_LST:
                return "LST";
                break;
        case IMAGE_LSTFAST:
                return "Fast LST";
                break;
        case IMAGE_PIF:
                return "PIF";
                break;
        case IMAGE_PNG:
                return "PNG";
                break;
        case IMAGE_HDF:
                return "HDF5";
                break;
        case IMAGE_DM3:
                return "GatanDM3";
                break;
        case IMAGE_DM4:
                return "GatanDM4";
                break;
        case IMAGE_TIFF:
                return "TIFF";
                break;
        case IMAGE_VTK:
                return "VTK";
                break;
        case IMAGE_SAL:
                return "HDR";
                break;
        case IMAGE_ICOS:
                return "ICOS_MAP";
                break;
        case IMAGE_EMIM:
                return "EMIM";
                break;
        case IMAGE_GATAN2:
                return "GatanDM2";
                break;
        case IMAGE_JPEG:
                return "JPEG";
                break;
        case IMAGE_AMIRA:
                return "AmiraMesh";
                break;
        case IMAGE_XPLOR:
                return "XPLOR";
                break;
        case IMAGE_EM:
                return "EM";
                break;
        case IMAGE_FITS:
                return "FITS";
                break;
        case IMAGE_DF3:
                return "DF3";
                break;
        case IMAGE_OMAP:
                return "OMAP";
                break;
        case IMAGE_SITUS:
                return "SITUS";
                break;
        case IMAGE_SER:
                return "SER";
                break;
        case IMAGE_UNKNOWN:
                return "unknown";
        }

        return "unknown";
}
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:
areaThe region area.
nxImage x size.
area_xThe pointer used to return the region x size.
nyImage y size.
area_yThe pointer used to return the region y size.
nzImage z size.
area_zThe pointer used to return the region z size.

Definition at line 840 of file emutil.cpp.

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

Referenced by process_ascii_region_io(), process_region_io(), EMAN::MrcIO::read_fei_header(), and EMAN::MrcIO::read_mrc_header().

{
        Assert(area_x);
        Assert(area_y);

        if (! area) {
                *area_x = nx;
                *area_y = ny;

                if (area_z) {
                        *area_z = nz;
                }
        }
        else {
                Vec3i size = area->get_size();
                *area_x = size[0];
                *area_y = size[1];

                if (area_z) {
                        if (area->get_ndim() > 2 && nz > 1) {
                                *area_z = size[2];
                        }
                        else {
                                *area_z = 1;
                        }
                }

        }
}
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:
areaThe region area.
p_x0The pointer used to return the region x origin.
p_y0The pointer used to return the region y origin.
p_z0The pointer used to return the region z origin.
nzImage z size.
image_indexImage index.

Definition at line 871 of file emutil.cpp.

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

{
        Assert(p_x0);
        Assert(p_y0);

        if (area) {
                *p_x0 = static_cast < int >(area->origin[0]);
                *p_y0 = static_cast < int >(area->origin[1]);

                if (p_z0 && nz > 1 && area->get_ndim() > 2) {
                        *p_z0 = static_cast < int >(area->origin[2]);
                }
        }
        else {
                *p_x0 = 0;
                *p_y0 = 0;

                if (p_z0) {
                        *p_z0 = nz > 1 ? 0 : image_index;
                }
        }
}
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]data2D image's data array
[in]nxx dimension size
[in]nyy dimension size
[out]renderminthe minmal value for normalization
[out]rendermaxthe maximum value for normalization
[in]nzz dimension size

Definition at line 1653 of file emutil.cpp.

References get_image_type(), IMAGE_HDF, ImageFormatException, key, max, min, EMAN::ImageIO::READ_ONLY, EMAN::ImageIO::READ_WRITE, sqrt(), and EMAN::ImageIO::WRITE_ONLY.

{
#ifdef _WIN32
        if (rendermax<=rendermin || _isnan(rendermin) || _isnan(rendermax)) {
#else
        if (rendermax<=rendermin || std::isnan(rendermin) || std::isnan(rendermax)) {
#endif

                float m = 0.0f, s = 0.0f;

                size_t size = (size_t)nx*ny*nz;
                float min = data[0], max = data[0];

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

                m /= (float)(size);
                s = sqrt(s/(float)(size)-m*m);

#ifdef _WIN32
                if (s <= 0 || _isnan(s)) s = 1.0;       // this means all data values are the same
#else
                if (s <= 0 || std::isnan(s)) s = 1.0; // this means all data values are the same
#endif  //_WIN32

                rendermin = m - s * 5.0f;
                rendermax = m + s * 5.0f;

                if (rendermin <= min) rendermin = min;
                if (rendermax >= max) rendermax = max;

//      printf("rendermm %f %f %f %f\n",rendermin,rendermax,m,s);
        }
}
bool EMUtil::is_complex_type ( EMDataType  datatype) [static]

Definition at line 1117 of file emutil.cpp.

References EM_FLOAT_COMPLEX, EM_SHORT_COMPLEX, and EM_USHORT_COMPLEX.

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

{
        if (datatype == EM_SHORT_COMPLEX ||
                datatype == EM_USHORT_COMPLEX ||
                datatype == EM_FLOAT_COMPLEX) {

                return true;
        }

        return false;
}
bool EMUtil::is_same_ctf ( const EMData image1,
const EMData image2 
) [static]

Check whether two EMData images have the same CTF parameters.

Parameters:
image1The first EMData image.
image2The second EMData image.
Returns:
whether two EMData images have the same CTF.

Definition at line 1232 of file emutil.cpp.

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

Referenced by main().

{
        if (!image1) {
                throw NullPointerException("image1 is NULL");
        }

        if (!image2) {
                throw NullPointerException("image2 is NULL");
        }

        Ctf *ctf1 = image1->get_ctf();
        Ctf *ctf2 = image2->get_ctf();

        if ((!ctf1 && !ctf2) && (image1->has_ctff() == false && image2->has_ctff() == false)) {
                return true;
        }

        if (ctf1 && ctf2) {
                bool result = ctf1->equal(ctf2);
                delete ctf1;
                ctf1 = NULL;
                delete ctf2;
                ctf2 = NULL;

                return result;
        }

        return false;
}
bool EMUtil::is_same_size ( const EMData image1,
const EMData image2 
) [static]
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:
filenameImage file name.
Returns:
whether or not it is a valid filename

Definition at line 220 of file emutil.cpp.

References EMAN::Util::get_filename_ext(), get_image_ext_type(), and IMAGE_UNKNOWN.

{
        ImageType type = get_image_ext_type(Util::get_filename_ext(filename));

        return (type != IMAGE_UNKNOWN);
}
void EMUtil::jump_lines ( FILE *  file,
int  nlines 
) [static]

Definition at line 1455 of file emutil.cpp.

References Assert.

Referenced by jump_lines_by_items(), and process_ascii_region_io().

{
        Assert(file);

        if (nlines > 0) {
                char line[MAXPATHLEN];

                for (int l = 0; l < nlines; l++) {
                        if (!fgets(line, sizeof(line), file)) {
                                Assert("read xplor file failed");
                        }
                }
        }
}
void EMUtil::jump_lines_by_items ( FILE *  file,
int  nitems,
int  nitems_per_line 
) [static, private]

Definition at line 1435 of file emutil.cpp.

References Assert, and jump_lines().

Referenced by process_ascii_region_io().

{
        Assert(file);
        Assert(nitems_per_line > 0);

        if (nitems <= 0) {
                return;
        }

        int nlines = nitems / nitems_per_line;

        if ((nitems % nitems_per_line) != 0) {
                nlines++;
        }

        if (nlines > 0) {
                jump_lines(file, nlines);
        }
}
EMData * EMUtil::make_image_median ( const vector< EMData * > &  image_list) [static]

Definition at line 1166 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.

{
        if (image_list.size() == 0) {
                return 0;
        }

        EMData *image0 = image_list[0];

        int image0_nx = image0->get_xsize();
        int image0_ny = image0->get_ysize();
        int image0_nz = image0->get_zsize();
        size_t size = (size_t)image0_nx * image0_ny * image0_nz;

        EMData *result = new EMData();

        result->set_size(image0_nx, image0_ny, image0_nz);

        float *dest = result->get_data();
        int nitems = static_cast < int >(image_list.size());
        float *srt = new float[nitems];
        float **src = new float *[nitems];

        for (int i = 0; i < nitems; i++) {
                src[i] = image_list[i]->get_data();
        }

        for (size_t i = 0; i < size; ++i) {
                for (int j = 0; j < nitems; j++) {
                        srt[j] = src[j][i];
                }

                for (int j = 0; j < nitems; j++) {
                        for (int k = j + 1; k < nitems; k++) {
                                if (srt[j] < srt[k]) {
                                        float v = srt[j];
                                        srt[j] = srt[k];
                                        srt[k] = v;
                                }
                        }
                }

                int l = nitems / 2;

                if (nitems < 3) {
                        dest[i] = srt[l];
                }
                else {
                        dest[i] = (srt[l] + srt[l + 1] + srt[l - 1]) / 3.0f;
                }
        }

        if (srt) {
                delete [] srt;
                srt = NULL;
        }

        if (src) {
                delete [] src;
                src = NULL;
        }

        result->update();

        return result;
}
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 1327 of file emutil.cpp.

References Assert, exclude_numbers_io(), get_region_dims(), jump_lines(), jump_lines_by_items(), ny, EMAN::Region::origin, process_lines_io(), process_numbers_io(), EMAN::ImageIO::READ_ONLY, EMAN::ImageIO::READ_WRITE, and EMAN::ImageIO::WRITE_ONLY.

{
        Assert(data != 0);
        Assert(file != 0);
        Assert(rw_mode == ImageIO::READ_ONLY ||
                   rw_mode == ImageIO::READ_WRITE ||
                   rw_mode == ImageIO::WRITE_ONLY);

        int xlen = 0, ylen = 0, zlen = 0;
        get_region_dims(area, nx, &xlen, ny, &ylen, nz, &zlen);

        int x0 = 0;
        int y0 = 0;
        int z0 = 0;

        if (area) {
                x0 = (int)area->origin[0];
                y0 = (int)area->origin[1];
                z0 = (int)area->origin[2];
        }

        int nlines_per_sec = (nx *ny) / nitems_per_line;
        int nitems_last_line = (nx * ny) % nitems_per_line;

        if (nitems_last_line != 0) {
                nlines_per_sec++;
        }

        if (has_index_line) {
                nlines_per_sec++;
        }

        if (z0 > 0) {
                jump_lines(file, z0 * nlines_per_sec);
        }

        int nlines_pre_sec = (y0 * nx + x0) / nitems_per_line;
        int gap_nitems = nx - xlen;
        int ti = 0;
        int rlines = 0;

        for (int k = 0; k < zlen; k++) {
                EMUtil::jump_lines(file, nlines_pre_sec+1);

                int head_nitems = (y0 * nx + x0) % nitems_per_line;
                int tail_nitems = 0;
                bool is_head_read = false;

                for (int j = 0; j < ylen; j++) {
                        if (head_nitems > 0 && !is_head_read) {
                                EMUtil::process_numbers_io(file, rw_mode, nitems_per_line, mode_size,
                                                                                   nitems_per_line-head_nitems,
                                                                                   nitems_per_line-1, data, &ti, outformat);
                                rlines++;
                        }

                        EMUtil::process_lines_io(file, rw_mode, nitems_per_line,
                                                                         mode_size, (xlen - head_nitems),
                                                                         data, &ti, outformat);

                        rlines += ((xlen - head_nitems) / nitems_per_line);

                        tail_nitems = (xlen - head_nitems) % nitems_per_line;

                        if ((gap_nitems + tail_nitems) > 0) {
                                head_nitems = nitems_per_line -
                                        (gap_nitems + tail_nitems) % nitems_per_line;
                        }
                        else {
                                head_nitems = 0;
                        }

                        is_head_read = false;

                        if (tail_nitems > 0) {
                                if ((gap_nitems < (nitems_per_line-tail_nitems)) &&
                                        (j != (ylen-1))) {
                                        EMUtil::exclude_numbers_io(file, rw_mode, nitems_per_line,
                                                                                           mode_size, tail_nitems,
                                                                                           tail_nitems+gap_nitems-1, data, &ti, outformat);
                                        is_head_read = true;
                                        rlines++;
                                }
                                else {
                                        EMUtil::process_numbers_io(file, rw_mode, nitems_per_line, mode_size,
                                                                                           0, tail_nitems-1, data, &ti, outformat);
                                        rlines++;
                                }
                        }

                        if (gap_nitems > (nitems_per_line-tail_nitems)) {
                                int gap_nlines = (gap_nitems - (nitems_per_line-tail_nitems)) /
                                        nitems_per_line;

                                if (gap_nlines > 0 && j != (ylen-1)) {
                                        EMUtil::jump_lines(file, gap_nlines);
                                }
                        }
                }

                int ytail_nitems = (ny-ylen-y0) * nx + (nx-xlen-x0) - (nitems_per_line-tail_nitems);
                EMUtil::jump_lines_by_items(file, ytail_nitems, nitems_per_line);
        }
}
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 1574 of file emutil.cpp.

References Assert, and process_numbers_io().

Referenced by process_ascii_region_io().

{
        Assert(file);
        Assert(data);
        Assert(p_i);

        if (nitems > 0) {
                int nlines = nitems / nitems_per_line;

                for (int i = 0; i < nlines; i++) {
                        EMUtil::process_numbers_io(file, rw_mode, nitems_per_line, mode_size, 0,
                                                                           nitems_per_line-1, data, p_i, outformat);
                }
        }
}
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 1470 of file emutil.cpp.

References Assert, portable_fseek(), and EMAN::ImageIO::READ_ONLY.

Referenced by process_ascii_region_io(), and process_lines_io().

{
        Assert(file);
        Assert(start >= 0);
        Assert(start <= end);
        Assert(end <= nitems_per_line);
        Assert(data);
        Assert(p_i);
        Assert(outformat);

        char line[MAXPATHLEN];

        if (rw_mode == ImageIO::READ_ONLY) {
                if (!fgets(line, sizeof(line), file)) {
                        Assert("read xplor file failed");
                }

                int nitems_in_line = (int) (strlen(line) / mode_size);
                Assert(end <= nitems_in_line);
                vector<float> d(nitems_in_line);
                char * pline = line;

                for (int i = 0; i < nitems_in_line; i++) {
                        sscanf(pline, "%f", &d[i]);
                        pline += (int)mode_size;
                }

                for (int i = start; i <= end; i++) {
                        data[*p_i] = d[i];
                        (*p_i)++;
                }
        }
        else {
                portable_fseek(file, mode_size * start, SEEK_CUR);

                for (int i = start; i <= end; i++) {
                        fprintf(file, outformat, data[*p_i]);
                        (*p_i)++;
                }

                portable_fseek(file, mode_size * (nitems_per_line - end-1)+1, SEEK_CUR);
        }
}
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:
cdataData array.
fileThe image file pointer.
rw_modeRead/write mode. It is either READ_ONLY or WRITE_ONLY.
image_indexImage index.
mode_sizePixel size.
nxImage x size.
nyImage y size.
nzImage z size.
areaThe region to read/write.
need_flipDo we need flip the image?
imgtypeThe Image type of the processed file.
pre_rowFile size needed to be skipped before each row.
post_rowFile size needed to be skipped after each row.
Exceptions:
ImageReadExceptionIf the read has some error.
ImageWriteExceptionIf the write has some error.

Definition at line 895 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(), EMAN::ImageIO::READ_ONLY, EMAN::ImageIO::READ_WRITE, UnexpectedBehaviorException, and EMAN::ImageIO::WRITE_ONLY.

Referenced by EMAN::EMData::read_data(), EMAN::EMData::write_data(), and EMAN::SpiderIO::write_single_data().

{
        Assert(vdata != 0);
        Assert(file != 0);
        Assert(rw_mode == ImageIO::READ_ONLY ||
                   rw_mode == ImageIO::READ_WRITE ||
                   rw_mode == ImageIO::WRITE_ONLY);

        if (mode_size == 0) throw UnexpectedBehaviorException("The mode size was 0?");

        unsigned char * cdata = (unsigned char *)vdata;

        int dx0 = 0; // data x0
        int dy0 = 0; // data y0
        int dz0 = 0; // data z0

        int fx0 = 0; // file x0
        int fy0 = 0; // file y0
        int fz0 = nz > 1 ? 0 : image_index; // file z0

        int xlen = 0;
        int ylen = 0;
        int zlen = 0;

        get_region_dims(area, nx, &xlen, ny, &ylen, nz, &zlen);

        if (area) { // Accommodate for all boundary overlaps of the region

                Vec3i origin = area->get_origin();


                fx0 = origin[0]; dx0 = origin[0];
                fy0 = origin[1]; dy0 = origin[1];

                if (nz > 1 && area->get_ndim() > 2) {
                        fz0 = origin[2]; dz0 = origin[2];
                }

                if (need_flip) {
                        Vec3i size = area->get_size();
                        fy0 = ny-(origin[1]+size[1]);
                }

                if (fx0 < 0) {
                        dx0 *= -1;
                        xlen = xlen + fx0; // because there are less reads
                        fx0 = 0;
                } else {
                        dx0 = 0;
                        //fx0 *= -1;
                }

                if (fy0 < 0) {
                        dy0 *= -1;
                        ylen = ylen + fy0; // because there are less reads
                        fy0 = 0;
                } else {
                        if (need_flip){
                                dy0*=-1;
                        }
                        else dy0 = 0;
                        //fy0 *= -1;
                }

                if (fz0 < 0) {
                        dz0 *= -1;
                        zlen = zlen + fz0; // because there are less reads
                        fz0 = 0;
                } else {
                        dz0 = 0;
                        //fz0 *= -1;
                }

                if ((fx0 + xlen)> nx) xlen = nx-fx0;
                if ((fy0 + ylen)> ny) ylen = ny-fy0;
                if ((fz0 + zlen)> nz) zlen = nz-fz0;

                // This is fine - the region was entirely outside the image

                if ( xlen <= 0 || ylen <= 0 || zlen <= 0 ) return;
        }

        if (xlen <= 0) {
                cout << "Xlen was too small " << xlen << endl;

                return;
        }

        Vec3i size;

        if (area != 0) size = area->get_size();
        else size = Vec3d(nx,ny,nz);

        //size_t area_sec_size = xlen * ylen * mode_size;
        size_t memory_sec_size = size[0] * size[1] * mode_size;
        size_t img_row_size = nx * mode_size + pre_row + post_row;
        size_t area_row_size = xlen * mode_size;
        size_t memory_row_size = size[0] * mode_size;

        if ( area_row_size <= 0 ) {
                cout << "Xlen was too small " << xlen << " mode_size " << mode_size << endl;

                return;
        }

        size_t x_pre_gap = fx0 * mode_size;
        size_t x_post_gap = (nx - fx0 - xlen) * mode_size;

        size_t y_pre_gap = fy0 * img_row_size;
        size_t y_post_gap = (ny - fy0 - ylen) * img_row_size;

        portable_fseek(file, img_row_size * ny * fz0, SEEK_CUR);

        float nxlendata[1];
        int floatsize = (int) sizeof(float);
        nxlendata[0] = (float)(nx * floatsize);

        for (int k = dz0; k < (dz0+zlen); k++) {
                if (y_pre_gap > 0) {
                        portable_fseek(file, y_pre_gap, SEEK_CUR);
                }

                //long k2 = k * area_sec_size;
                long k2 = k*memory_sec_size;

                for (int j = dy0; j < (dy0+ylen); j++) {
                        if (pre_row > 0) {
                                if (imgtype == IMAGE_ICOS && rw_mode != ImageIO::READ_ONLY && !area) {
                                        fwrite(nxlendata, floatsize, 1, file);
                                }
                                else {
                                        portable_fseek(file, pre_row, SEEK_CUR);
                                }
                        }

                        if (x_pre_gap > 0) {
                                portable_fseek(file, x_pre_gap, SEEK_CUR);
                        }

                        int jj = j;

                        if (need_flip) {
                                jj = (dy0+ylen) - 1 - j;

                                // region considerations add complications in the flipping scenario (imagic format)

                                if (dy0 > 0 ) {
                                        jj += dy0;
                                }
                        }

                        if (rw_mode == ImageIO::READ_ONLY) {
                                if (fread(&cdata[k2 + jj * memory_row_size+dx0*mode_size],
                                                  area_row_size, 1, file) != 1) {
                                        cout << jj << " " << k2 << " " << memory_row_size << " " << dx0 << " "
                                                  << mode_size << " " << area_row_size << " "
                                                  << cdata << ftell(file) << "done" << endl;
                                        throw ImageReadException("Unknownfilename", "incomplete data read");
                                }
                        }
                        else {
                                if (fwrite(&cdata[k2 + jj * memory_row_size+dx0*mode_size],
                                                   area_row_size, 1, file) != 1) {
                                        throw ImageWriteException("", "incomplete data write");
                                }
                        }

                        if (x_post_gap > 0) {
                                portable_fseek(file, x_post_gap, SEEK_CUR);
                        }

                        if (post_row > 0) {
                                if (imgtype == IMAGE_ICOS && rw_mode != ImageIO::READ_ONLY && !area) {
                                        fwrite(nxlendata, floatsize, 1, file);
                                }
                                else {
                                        portable_fseek(file, post_row, SEEK_CUR);
                                }
                        }
                }

                if (y_post_gap > 0) {
                        portable_fseek(file, y_post_gap, SEEK_CUR);
                }
        }
}
EMData * EMUtil::vertical_acf ( const EMData image,
int  maxdy 
) [static]

Definition at line 1129 of file emutil.cpp.

References data, EMAN::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.

{
        if (!image) {
                throw NullPointerException("NULL Image");
        }

        EMData *ret = new EMData();

        int nx = image->get_xsize();
        int ny = image->get_ysize();

        if (maxdy <= 1) {
                maxdy = ny / 8;
        }

        ret->set_size(nx, maxdy, 1);

        float *data = image->get_data();
        float *ret_data = ret->get_data();

        for (int x = 0; x < nx; x++) {
                for (int y = 0; y < maxdy; y++) {
                        float dot = 0;

                        for (int yy = maxdy; yy < ny - maxdy; yy++) {
                                dot += data[x + (yy + y) * nx] * data[x + (yy - y) * nx];
                        }

                        ret_data[x + y * nx] = dot;
                }
        }

        ret->update();

        return ret;
}

The documentation for this class was generated from the following files: