imagicio.cpp

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033  *
00034  * */
00035 
00036 #include <cstring>
00037 #include "imagicio.h"
00038 #include "portable_fileio.h"
00039 #include "util.h"
00040 #include "geometry.h"
00041 #include "ctf.h"
00042 #include "emassert.h"
00043 #include "transform.h"
00044 
00045 #ifdef _WIN32
00046         #include <ctime>
00047 #endif  //_WIN32
00048 
00049 using namespace EMAN;
00050 
00051 const char *ImagicIO::HED_EXT = "hed";
00052 const char *ImagicIO::IMG_EXT = "img";
00053 const char *ImagicIO::REAL_TYPE_MAGIC = "REAL";
00054 const char *ImagicIO::CTF_MAGIC = "!-";
00055 
00056 ImagicIO::ImagicIO(string file, IOMode rw)
00057 :       filename(file), rw_mode(rw), hed_file(0), img_file(0), initialized(false)
00058 {
00059         hed_filename = Util::change_filename_ext(filename, HED_EXT);
00060         img_filename = Util::change_filename_ext(filename, IMG_EXT);
00061 
00062         is_big_endian = ByteOrder::is_host_big_endian();
00063         is_new_hed = false;
00064         is_new_img = false;
00065         memset(&imagich, 0, sizeof(ImagicHeader));
00066         imagich.count = -1;
00067         datatype = IMAGIC_UNKNOWN_TYPE;
00068         nz = 0;
00069 }
00070 
00071 ImagicIO::~ImagicIO()
00072 {
00073         if (hed_file) {
00074                 fclose(hed_file);
00075                 hed_file = 0;
00076         }
00077 
00078         if (img_file) {
00079                 fclose(img_file);
00080                 img_file = 0;
00081         }
00082 }
00083 
00084 void ImagicIO::init()
00085 {
00086         ENTERFUNC;
00087 
00088         if (initialized) {
00089                 return;
00090         }
00091 
00092         initialized = true;
00093 
00094         is_new_hed = false;
00095         is_new_img = false;
00096 
00097         hed_file = sfopen(hed_filename, rw_mode, &is_new_hed);
00098         img_file = sfopen(img_filename, rw_mode, &is_new_img);
00099 
00100         if (is_new_hed != is_new_img) {
00101                 LOGWARN("IMAGIC header file and data file should both exist or both not exist");
00102         }
00103 
00104         if (!is_new_hed) {
00105                 if (fread(&imagich, sizeof(ImagicHeader), 1, hed_file) != 1) {
00106                         throw ImageReadException(hed_filename, "IMAGIC header");
00107                 }
00108 
00109                 if (!is_valid(&imagich)) {
00110                         throw ImageReadException(hed_filename, "invalid IMAGIC file");
00111                 }
00112 
00113                 datatype = get_datatype_from_name(imagich.type);
00114 
00115                 if (datatype != IMAGIC_USHORT && datatype != IMAGIC_FLOAT) {
00116                         LOGERR("unsupported imagic data type: %s", imagich.type);
00117                         throw ImageReadException(hed_filename, "unsupported imagic data type");
00118                 }
00119 
00120                 is_big_endian = ByteOrder::is_data_big_endian(&imagich.ny);
00121                 make_header_host_endian(imagich);
00122                 rewind(hed_file);
00123         }
00124         EXITFUNC;
00125 }
00126 
00127 bool ImagicIO::is_valid(const void *first_block)
00128 {
00129         ENTERFUNC;
00130 
00131         if (!first_block) {
00132                 return false;
00133         }
00134 
00135         const int *data = static_cast < const int *>(first_block);
00136         int count = data[1];
00137         int headrec = data[3];
00138         int month = data[5];
00139         int hour = data[7];
00140         int nx = data[13];
00141         int ny = data[12];
00142 
00143         bool data_big_endian = ByteOrder::is_data_big_endian(&headrec);
00144 
00145         if (data_big_endian != ByteOrder::is_host_big_endian()) {
00146                 ByteOrder::swap_bytes(&count);
00147                 ByteOrder::swap_bytes(&headrec);
00148                 ByteOrder::swap_bytes(&month);
00149                 ByteOrder::swap_bytes(&hour);
00150                 ByteOrder::swap_bytes(&nx);
00151                 ByteOrder::swap_bytes(&ny);
00152         }
00153 
00154         const int max_dim = 1 << 20;
00155         bool result = false;
00156 
00157         if (headrec == 1 &&
00158                 count >= 0 && count < max_dim &&
00159                 nx > 0 && nx < max_dim &&
00160                 ny > 0 && ny < max_dim && month >= 0 && month <= 12 && hour >= 0 && hour <= 24) {
00161                 result = true;
00162         }
00163 
00164         EXITFUNC;
00165         return result;
00166 }
00167 
00168 int ImagicIO::read_header(Dict & dict, int image_index, const Region * area, bool is_3d)
00169 {
00170         ENTERFUNC;
00171 
00172         check_read_access(image_index);
00173 
00174         int nimg = 1;
00175 
00176         if (is_3d) {
00177                 nimg = imagich.count + 1;
00178 
00179                 if (nimg <= 1) {
00180                         LOGWARN("this is not a 3D IMAGIC. Read as a 2D");
00181                 }
00182         }
00183 
00184         ImagicHeader hed;
00185         if (image_index == 0) {
00186                 hed = imagich;
00187         }
00188         else {
00189                 memset(&hed, 0, sizeof(ImagicHeader));
00190                 portable_fseek(hed_file, sizeof(ImagicHeader) * image_index, SEEK_SET);
00191                 fread(&hed, sizeof(ImagicHeader), 1, hed_file);
00192                 make_header_host_endian(hed);
00193         }
00194         check_region(area, FloatSize(hed.nx, hed.ny, nimg), is_new_hed,false);
00195 
00196     datatype = get_datatype_from_name(imagich.type);
00197 
00198         int xlen = 0, ylen = 0, zlen = 0;
00199         EMUtil::get_region_dims(area, hed.nx, &xlen, hed.ny, &ylen, nimg, &zlen);
00200 
00201         dict["nx"] = xlen;
00202         dict["ny"] = ylen;
00203         dict["nz"] = zlen;
00204 
00205         dict["datatype"] = to_em_datatype(datatype);
00206 
00207         dict["minimum"] = hed.min;
00208         dict["maximum"] = hed.max;
00209         dict["mean"] = hed.avdens;
00210         dict["sigma"] = hed.sigma;
00211 
00212         dict["IMAGIC.imgnum"] = hed.imgnum;
00213         dict["IMAGIC.count"] = hed.count;
00214         dict["IMAGIC.error"] = hed.error;
00215 
00216         dict["IMAGIC.headrec"] = hed.headrec;
00217         dict["IMAGIC.mday"] = hed.mday;
00218         dict["IMAGIC.month"] = hed.month;
00219 
00220         dict["IMAGIC.year"] = hed.year;
00221         dict["IMAGIC.hour"] = hed.hour;
00222         dict["IMAGIC.minute"] = hed.minute;
00223 
00224         dict["IMAGIC.sec"] = hed.sec;
00225         dict["IMAGIC.reals"] = hed.reals;
00226         dict["IMAGIC.pixels"] = hed.pixels;
00227 
00228         char tmp[5] = { hed.type[0],hed.type[1],hed.type[2],hed.type[3],0 };
00229         dict["IMAGIC.type"] = tmp;
00230         dict["IMAGIC.ixold"] = hed.ixold;
00231         dict["IMAGIC.iyold"] = hed.iyold;
00232 
00233         dict["IMAGIC.oldav"] = hed.oldav;
00234         dict["IMAGIC.label"] = hed.label;
00235         dict["ptcl_repr"] = hed.mrc2;                   // raw images represented by this image
00236 
00237         dict["orientation_convention"] = "EMAN";
00238     const float alt = hed.mrc1[1]*180.0f/M_PI;
00239     const float az = hed.mrc1[2]*180.0f/M_PI;
00240     const float phi = hed.mrc1[0]*180.0f/M_PI;
00241         dict["euler_alt"] = alt;
00242         dict["euler_az"] = az;
00243         dict["euler_phi"] = phi;
00244         Transform *trans = new Transform();
00245         trans->set_rotation(Dict("type", "eman", "alt", alt, "az", az, "phi", phi));
00246         if( hed.count==0 ) {
00247                 dict["xform.projection"] = trans;
00248         }
00249         else {
00250                 dict["xform.projection"] = trans;
00251                 dict["xform.align3d"] = trans;
00252         }
00253         Ctf * ctf_ = read_ctf(hed);
00254         if( ctf_ != 0) {
00255                 dict["ctf"] = ctf_;
00256         }
00257         if(trans) {delete trans; trans=0;}
00258         if(ctf_) {delete ctf_; ctf_=0;}
00259         EXITFUNC;
00260         return 0;
00261 }
00262 
00263 int ImagicIO::write_header(const Dict & dict, int image_index,
00264                                                    const Region * area, EMUtil::EMDataType, bool use_host_endian)
00265 {
00266         ENTERFUNC;
00267 
00268         check_write_access(rw_mode, image_index);
00269         nz = dict["nz"];
00270         if (nz > 1 && image_index != 0) {
00271                 throw ImageWriteException(filename, "to write 3D IMAGIC image, image index must be 0");
00272         }
00273 
00274         if (area) {
00275                 check_region(area, FloatSize(imagich.nx, imagich.ny, imagich.count+1),
00276                                          is_new_hed);
00277                 EXITFUNC;
00278                 return 0;
00279         }
00280 
00281         int nx = dict["nx"];
00282         int ny = dict["ny"];
00283         int nimg=0;             //# images currently in file
00284 
00285 
00286         if (!is_new_hed) {
00287         datatype = get_datatype_from_name(imagich.type);
00288 
00289                 if (imagich.nx != nx || imagich.ny != ny) {
00290                         char desc[256];
00291                         sprintf(desc, "new IMAGIC size %dx%d is not equal to existing size %dx%d",
00292                                         nx, ny, imagich.nx, imagich.ny);
00293                         throw ImageWriteException(filename, desc);
00294                 }
00295 
00296         if (datatype!=IMAGIC_FLOAT) {
00297                         throw ImageWriteException(filename, "Attempted write to non REAL Imagic file");
00298                 }
00299 
00300         rewind(hed_file);
00301                 nimg=imagich.count+1;
00302         }
00303 
00304         ImagicHeader new_hed;
00305         memset(&new_hed, 0, sizeof(ImagicHeader));
00306 
00307         time_t cur_time = time(0);
00308         struct tm *tm = localtime(&cur_time);
00309 
00310         new_hed.error = 0;
00311         new_hed.headrec = 1;
00312 
00313         new_hed.mday = tm->tm_mday;
00314         new_hed.month = tm->tm_mon;
00315         new_hed.year = tm->tm_year + 1900;
00316         new_hed.hour = tm->tm_hour;
00317         new_hed.minute = tm->tm_min;
00318         new_hed.sec = tm->tm_sec;
00319 
00320         new_hed.reals = nx * ny;
00321         new_hed.pixels = nx * ny;
00322         new_hed.ny = ny;
00323         new_hed.nx = nx;
00324 
00325         new_hed.ixold = 0;
00326         new_hed.iyold = 0;
00327         new_hed.oldav = 0;
00328 
00329         new_hed.min = (float)dict["minimum"];
00330         new_hed.max = (float)dict["maximum"];
00331         new_hed.avdens = (float)dict["mean"];
00332         new_hed.sigma = (float)dict["sigma"];
00333 
00334         if(nz<=1 && dict.has_key("xform.projection")) {
00335                 Transform * t = dict["xform.projection"];
00336                 Dict d = t->get_rotation("eman");
00337                 new_hed.mrc1[1] = (float)d["alt"]*M_PI/180.0f;
00338                 new_hed.mrc1[2] = (float)d["az"]*M_PI/180.0f;
00339                 new_hed.mrc1[0] = (float)d["phi"]*M_PI/180.0f;
00340                 if(t) {delete t; t=0;}
00341         }
00342         else if(nz>1 && dict.has_key("xform.align3d")) {
00343                 Transform * t = dict["xform.align3d"];
00344                 Dict d = t->get_rotation("eman");
00345                 new_hed.mrc1[1] = (float)d["alt"]*M_PI/180.0f;
00346                 new_hed.mrc1[2] = (float)d["az"]*M_PI/180.0f;
00347                 new_hed.mrc1[0] = (float)d["phi"]*M_PI/180.0f;
00348                 if(t) {delete t; t=0;}
00349         }
00350         else {
00351                 if(dict.has_key("euler_alt")) new_hed.mrc1[1] = (float)dict["euler_alt"]*M_PI/180.0f;
00352                 if(dict.has_key("euler_az")) new_hed.mrc1[2] = (float)dict["euler_az"]*M_PI/180.0f;
00353                 if(dict.has_key("euler_phi")) new_hed.mrc1[0] = (float)dict["euler_phi"]*M_PI/180.0f;
00354         }
00355 
00356         if(dict.has_key("ptcl_repr")) new_hed.mrc2 = (int)dict["ptcl_repr"];
00357 
00358         string new_label = dict.has_key("IMAGIC.label") ? (string) dict["IMAGIC.label"] : "";
00359         sprintf(new_hed.label, new_label.c_str() );
00360 
00361         new_hed.lbuf = nx;
00362         new_hed.inn = 1;
00363         new_hed.iblp = ny;
00364         new_hed.ifb = 0;
00365         new_hed.lbw = 0;
00366         new_hed.lbr = -1;
00367         new_hed.lastlr = -1;
00368         new_hed.lastlw = 1;
00369         new_hed.num = 8;
00370         new_hed.nhalf = nx / 2;
00371         new_hed.ibsd = nx * 2;
00372         new_hed.ihfl = 7;
00373         new_hed.lcbr = -1;
00374         new_hed.lcbw = 1;
00375         new_hed.imstr = -1;
00376         new_hed.imstw = -1;
00377         new_hed.istart = 1;
00378         new_hed.iend = nx;
00379         new_hed.leff = nx;
00380         new_hed.linbuf = nx * 2;
00381         new_hed.ntotbuf = -1;
00382         new_hed.icstart = 1;
00383         new_hed.icend = nx / 2;
00384         strncpy(new_hed.type, REAL_TYPE_MAGIC,4);
00385 
00386 
00387         // header in file order
00388         if ( (is_big_endian != ByteOrder::is_host_big_endian()) || !use_host_endian)  swap_header(new_hed);
00389 
00390         // overwrite existing header if necessary
00391         if (image_index>=0 && image_index<nimg) {
00392                 portable_fseek(hed_file, sizeof(ImagicHeader)*image_index, SEEK_SET);
00393                 new_hed.imgnum=image_index+1;
00394                 if (is_big_endian != ByteOrder::is_host_big_endian())
00395                                 ByteOrder::swap_bytes((int *) &new_hed.imgnum,1);
00396                 fwrite(&new_hed, sizeof(ImagicHeader),1,hed_file);
00397         }
00398 
00399         // How many images does the file need when we're done ?
00400         int required_len;
00401         if (nz>1) required_len=nz;
00402         else {
00403                 if (image_index<0) required_len=nimg+1;
00404                 else if (image_index+1>nimg) required_len=image_index+1;
00405                 else required_len=nimg;
00406         }
00407 
00408         // Extend the file to the necessary length
00409         portable_fseek(hed_file, 0, SEEK_END);
00410         while (nimg<required_len) {
00411                 nimg++;
00412                 new_hed.imgnum=nimg;
00413                 if (is_big_endian != ByteOrder::is_host_big_endian())
00414                                 ByteOrder::swap_bytes((int *) &new_hed.imgnum,1);
00415                 fwrite(&new_hed, sizeof(ImagicHeader),1,hed_file);
00416         }
00417 
00418         // update the 1st header with total # images
00419         portable_fseek(hed_file, sizeof(int), SEEK_SET);
00420         nimg--;
00421         if (is_big_endian != ByteOrder::is_host_big_endian())
00422                         ByteOrder::swap_bytes((int *) &nimg,1);
00423         fwrite(&nimg, sizeof(int), 1, hed_file);
00424 
00425         // header in machine order
00426         if ( (is_big_endian != ByteOrder::is_host_big_endian()) || !use_host_endian)  swap_header(new_hed);
00427         imagich=new_hed;
00428         imagich.count=nimg;
00429         is_new_hed = false;
00430 
00431         if( dict.has_key("ctf") ) {
00432                 Ctf * ctf_ = dict["ctf"];
00433                 write_ctf(ctf_);
00434                 if(ctf_) {delete ctf_; ctf_=0;}
00435         }
00436 
00437         EXITFUNC;
00438         return 0;
00439 }
00440 
00441 int ImagicIO::read_data(float *data, int image_index, const Region * area, bool is_3d)
00442 {
00443         ENTERFUNC;
00444 
00445         check_read_access(image_index, data);
00446     Assert(datatype != IMAGIC_UNKNOWN_TYPE);
00447         int nimg = 1;
00448         if (is_3d) {
00449                 nimg = imagich.count + 1;
00450         }
00451 
00452         if (is_3d && imagich.count < 1) {
00453                 LOGWARN("this is not a 3D IMAGIC. Read as a 2D");
00454                 is_3d = false;
00455         }
00456 
00457         check_region(area, FloatSize(imagich.nx, imagich.ny, nimg), is_new_hed,false);
00458 
00459         rewind(img_file);
00460 
00461         unsigned short *sdata = (unsigned short *) data;
00462         unsigned char *cdata = (unsigned char *) data;
00463         size_t mode_size = get_datatype_size(datatype);
00464         EMUtil::process_region_io(cdata, img_file, READ_ONLY, image_index, mode_size,
00465                                                           imagich.nx, imagich.ny, nimg, area, true);
00466 
00467 #if 0
00468         int row_size = imagich.nx * mode_size;
00469         int sec_size = imagich.nx * imagich.ny * mode_size;
00470 
00471         for (int k = 0; k < nimg; k++) {
00472                 for (int i = imagich.ny - 1; i >= 0; i--) {
00473                         if (fread(&cdata[k * sec_size + i * row_size], row_size, 1, img_file) != 1) {
00474                                 LOGERR("incomplete data read: %d/%d blocks on file '%s'",
00475                                                                          i, imagich.ny, filename.c_str());
00476                                 return 1;
00477                         }
00478                 }
00479         }
00480 #endif
00481 
00482         int img_size = imagich.nx * imagich.ny * nimg;
00483 
00484         if (datatype == IMAGIC_FLOAT) {
00485                 become_host_endian(data, img_size);
00486         }
00487         else if (datatype == IMAGIC_USHORT) {
00488                 become_host_endian((unsigned short *) cdata, img_size);
00489 
00490                 for (int j = img_size - 1; j >= 0; j--) {
00491                         data[j] = static_cast < float >(sdata[j]);
00492                 }
00493         }
00494         else {
00495                 throw ImageReadException(filename, "unknown imagic data type");
00496         }
00497 
00498         EXITFUNC;
00499         return 0;
00500 }
00501 
00502 int ImagicIO::write_data(float *data, int image_index, const Region* area,
00503                                                  EMUtil::EMDataType, bool use_host_endian)
00504 {
00505         ENTERFUNC;
00506 
00507         check_write_access(rw_mode, image_index, 0, data);
00508         check_region(area, FloatSize(imagich.nx, imagich.ny, imagich.count+1),
00509                                  is_new_hed);
00510 
00511         if (nz == 1) {
00512                 if (image_index == -1) {
00513                         portable_fseek(img_file, 0, SEEK_END);
00514                 }
00515                 else {
00516                         size_t sec_size = imagich.nx * imagich.ny * sizeof(float);
00517                         portable_fseek(img_file, ((off_t) sec_size) * image_index, SEEK_SET);
00518                 }
00519         }
00520 
00521         if(is_new_img) {
00522                 if(!use_host_endian) {
00523                         ByteOrder::swap_bytes(data, imagich.nx * imagich.ny * nz);
00524                 }
00525         }
00526         else if (is_big_endian != ByteOrder::is_host_big_endian()) {
00527                 ByteOrder::swap_bytes(data, imagich.nx * imagich.ny * nz);
00528         }
00529 #if 0
00530         int n_pad_imgs = 0;
00531         int old_num_imgs = imagich.count + 1;
00532         if (image_index > old_num_imgs) {
00533                 n_pad_imgs = image_index - old_num_imgs;
00534         }
00535 #endif
00536 
00537         // New way to write data which includes region writing.
00538         // If it is tested to be OK, remove the old code in the
00539         // #if 0  ... #endif block.
00540         EMUtil::process_region_io(data, img_file, WRITE_ONLY, 0,
00541                                                           sizeof(float), imagich.nx, imagich.ny,
00542                                                           nz, area, true);
00543 
00544 
00545 #if 0
00546         size_t row_size = imagich.nx * sizeof(float);
00547         int nxy = imagich.nx * imagich.ny;
00548 
00549         for (int i = 0; i < nz; i++) {
00550                 for (int j = imagich.ny - 1; j >= 0; j--) {
00551                         fwrite(&data[i * nxy + j * imagich.nx], row_size, 1, img_file);
00552                 }
00553 
00554                 if (!is_new_img && (is_big_endian != ByteOrder::is_host_big_endian())) {
00555                         ByteOrder::swap_bytes(data, imagich.nx * imagich.ny);
00556                 }
00557         }
00558 #endif
00559         EXITFUNC;
00560         return 0;
00561 }
00562 
00563 void ImagicIO::flush()
00564 {
00565         fflush(img_file);
00566         fflush(hed_file);
00567 }
00568 
00569 Ctf * ImagicIO::read_ctf(const ImagicHeader& hed) const
00570 {
00571         ENTERFUNC;
00572 
00573         Ctf * ctf_ = 0;
00574         size_t n = strlen(CTF_MAGIC);
00575 
00576         if (strncmp(imagich.label, CTF_MAGIC, n) == 0) {
00577                 ctf_ = new EMAN1Ctf();
00578                 string header_label(hed.label);
00579                 // Note: this block was making things crash because it assumed the following if statement
00580                 // was true - I added the if statement (d.woolford)
00581                 if (header_label.size() > 2) {
00582                         string sctf = "O" + header_label.substr(2);
00583                         ctf_->from_string(sctf);
00584                 }
00585         }
00586 
00587         EXITFUNC;
00588         return ctf_;
00589 }
00590 
00591 void ImagicIO::write_ctf(const Ctf * const ctf, int image_index)
00592 {
00593         ENTERFUNC;
00594         init();
00595 
00596         size_t n = strlen(CTF_MAGIC);
00597         strcpy(imagich.label, CTF_MAGIC);
00598         string ctf_ = ctf->to_string().substr(1);
00599         strncpy(&imagich.label[n], ctf_.c_str(), sizeof(imagich.label) - n);
00600 
00601         rewind(hed_file);
00602         if (fwrite(&imagich, sizeof(ImagicHeader), 1, hed_file) != 1) {
00603                 throw ImageWriteException(hed_filename, "Imagic Header");
00604         }
00605 
00606         EXITFUNC;
00607 }
00608 
00609 bool ImagicIO::is_complex_mode()
00610 {
00611         init();
00612         if (datatype == IMAGIC_FLOAT_COMPLEX || datatype == IMAGIC_FFT_FLOAT_COMPLEX) {
00613                 return true;
00614         }
00615         return false;
00616 }
00617 
00618 bool ImagicIO::is_image_big_endian()
00619 {
00620         init();
00621         return is_big_endian;
00622 }
00623 
00624 int ImagicIO::get_nimg()
00625 {
00626         init();
00627         return (imagich.count + 1);
00628 }
00629 
00630 ImagicIO::DataType ImagicIO::get_datatype_from_name(const char *name)
00631 {
00632         DataType t = IMAGIC_UNKNOWN_TYPE;
00633 
00634         if (strncmp(name, "PACK",4) == 0) {
00635                 t = IMAGIC_UCHAR;
00636         }
00637         else if (strncmp(name, "INTG",4) == 0) {
00638                 t = IMAGIC_USHORT;
00639         }
00640         else if (strncmp(name, REAL_TYPE_MAGIC,4) == 0) {
00641                 t = IMAGIC_FLOAT;
00642         }
00643         else if (strncmp(name, "COMP",4) == 0) {
00644                 t = IMAGIC_FLOAT_COMPLEX;
00645         }
00646         else if (strncmp(name, "RECO",4) == 0) {
00647                 t = IMAGIC_FFT_FLOAT_COMPLEX;
00648         }
00649         return t;
00650 }
00651 
00652 size_t ImagicIO::get_datatype_size(DataType t)
00653 {
00654         size_t s = 0;
00655         switch (t) {
00656         case IMAGIC_UCHAR:
00657                 s = sizeof(unsigned char);
00658                 break;
00659         case IMAGIC_USHORT:
00660                 s = sizeof(unsigned short);
00661                 break;
00662         case IMAGIC_FLOAT:
00663         case IMAGIC_FLOAT_COMPLEX:
00664         case IMAGIC_FFT_FLOAT_COMPLEX:
00665                 s = sizeof(float);
00666                 break;
00667         default:
00668                 s = 0;
00669         }
00670 
00671         return s;
00672 }
00673 
00674 int ImagicIO::to_em_datatype(DataType t)
00675 {
00676         switch (t) {
00677         case IMAGIC_UCHAR:
00678                 return EMUtil::EM_UCHAR;
00679         case IMAGIC_USHORT:
00680                 return EMUtil::EM_USHORT;
00681         case IMAGIC_FLOAT:
00682                 return EMUtil::EM_FLOAT;
00683         case IMAGIC_FLOAT_COMPLEX:
00684                 return EMUtil::EM_FLOAT_COMPLEX;
00685         default:
00686                 break;
00687         }
00688 
00689         return EMUtil::EM_UNKNOWN;
00690 }
00691 
00692 void ImagicIO::make_header_host_endian(ImagicHeader & hed)
00693 {
00694         if (is_big_endian != ByteOrder::is_host_big_endian()) {
00695                 swap_header(hed);
00696         }
00697 }
00698 
00699 
00700 void ImagicIO::swap_header(ImagicHeader & hed)
00701 {
00702         ByteOrder::swap_bytes((int *) &hed, NUM_4BYTES_PRE_IXOLD);
00703         ByteOrder::swap_bytes(&hed.ixold, NUM_4BYTES_AFTER_IXOLD);
00704         ByteOrder::swap_bytes((int *) &hed.space, NUM_4BYTES_AFTER_SPACE);
00705 }

Generated on Sat Nov 21 02:19:15 2009 for EMAN2 by  doxygen 1.5.6