EMAN2
emdata_io.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 <iomanip>
00037 
00038 #include "emdata.h"
00039 #include "all_imageio.h"
00040 #include "ctf.h"
00041 
00042 #include <iostream>
00043 using std::cout;
00044 using std::endl;
00045 
00046 #include <boost/shared_ptr.hpp>
00047 using boost::shared_ptr;
00048 
00049 using namespace EMAN;
00050 
00051 void EMData::read_image(const string & filename, int img_index, bool nodata,
00052                                                 const Region * region, bool is_3d)
00053 {
00054         ENTERFUNC;
00055 
00056         ImageIO *imageio = EMUtil::get_imageio(filename, ImageIO::READ_ONLY);
00057 
00058         if (!imageio) {
00059                 throw ImageFormatException("cannot create an image io");
00060         }
00061         else {
00062                 int err = imageio->read_header(attr_dict, img_index, region, is_3d);
00063                 if (err) {
00064                         throw ImageReadException(filename, "imageio read header failed");
00065                 }
00066                 else {
00067                         LstIO * myLstIO = dynamic_cast<LstIO *>(imageio);
00068                         if(!myLstIO)    attr_dict["source_path"] = filename;    //"source_path" is set to full path of reference image for LstIO, so skip this statement
00069                         attr_dict["source_n"] = img_index;
00070                         if (imageio->is_complex_mode()) {
00071                                 set_complex(true);
00072                                 set_fftpad(true);
00073                         }
00074                         if (attr_dict.has_key("is_fftodd") && (int)attr_dict["is_fftodd"] == 1) {
00075                                 set_fftodd(true);
00076                         }
00077                         if ((int) attr_dict["is_complex_ri"] == 1) {
00078                                 set_ri(true);
00079                         }
00080                         save_byteorder_to_dict(imageio);
00081 
00082                         nx = attr_dict["nx"];
00083                         ny = attr_dict["ny"];
00084                         nz = attr_dict["nz"];
00085                         attr_dict.erase("nx");
00086                         attr_dict.erase("ny");
00087                         attr_dict.erase("nz");
00088 
00089                         if (!nodata) {
00090 
00091                                 if (region) {
00092                                         nx = (int)region->get_width();
00093                                         if (nx <= 0) nx = 1;
00094                                         ny = (int)region->get_height();
00095                                         if (ny <= 0) ny = 1;
00096                                         nz = (int)region->get_depth();
00097                                         if (nz <= 0) nz = 1;
00098                                         set_size(nx,ny,nz);
00099                                         to_zero(); // This could be avoided in favor of setting only the regions that were not read to to zero... but tedious
00100                                 } // else the dimensions of the file being read match those of this
00101                                 else {
00102                                         set_size(nx, ny, nz);
00103                                 }
00104 
00105                                 // If GPU features are enabled there is  danger that rdata will
00106                                 // not be allocated, but set_size takes care of this, so this
00107                                 // should be safe.
00108                                 int err = imageio->read_data(get_data(), img_index, region, is_3d);
00109                                 if (err) {
00110                                         throw ImageReadException(filename, "imageio read data failed");
00111                                 }
00112                                 else {
00113                                         update();
00114                                 }
00115                         }
00116                         else {
00117                                 if (rdata!=0) EMUtil::em_free(rdata);
00118                                 rdata=0;
00119                         }
00120 
00121                 }
00122         }
00123     
00124         EMUtil::close_imageio(filename, imageio);
00125         imageio = 0;
00126         EXITFUNC;
00127 }
00128 
00129 void EMData::read_binedimage(const string & filename, int img_index, int binfactor, bool fast, bool is_3d)
00130 {
00131         ENTERFUNC;
00132 
00133         ImageIO *imageio = EMUtil::get_imageio(filename, ImageIO::READ_ONLY);
00134 
00135         if (!imageio) {
00136                 throw ImageFormatException("cannot create an image io");
00137         }
00138         else {
00139                 int err = imageio->read_header(attr_dict, img_index, 0, is_3d);
00140                 if (err) {
00141                         throw ImageReadException(filename, "imageio read header failed");
00142                 }
00143                 else {
00144                         attr_dict["source_path"] = filename;
00145                         attr_dict["source_n"] = img_index;
00146                         if (imageio->is_complex_mode()) {
00147                                 set_complex(true);
00148                                 set_fftpad(true);
00149                         }
00150                         if (attr_dict.has_key("is_fftodd") && (int)attr_dict["is_fftodd"] == 1) {
00151                                 set_fftodd(true);
00152                         }
00153                         if ((int) attr_dict["is_complex_ri"] == 1) {
00154                                 set_ri(true);
00155                         }
00156                         save_byteorder_to_dict(imageio);
00157 
00158                         int ori_nx = nx = attr_dict["nx"];
00159                         int ori_ny = ny = attr_dict["ny"];
00160                         int ori_nz = nz = attr_dict["nz"];
00161                         attr_dict.erase("nx");
00162                         attr_dict.erase("ny");
00163                         attr_dict.erase("nz");
00164 
00165                         // At this point nx, ny and nz are all reduced by binfactor
00166                         set_size(nx/binfactor, ny/binfactor, nz/binfactor);
00167 
00168                         //here is where we read in the binned data
00169                         EMData* tempdata = new EMData();
00170                         size_t sizeofslice = nx*ny*sizeof(float);
00171 
00172                         //zbin factor use 1 to speed binning(but don't benfit by averaging in Z)
00173                         int zbin = binfactor;
00174                         if(fast) zbin = 1;
00175                         //verbose
00176                         float percent = 0.1f;
00177                         for(int k = 0; k < ori_nz; k+=binfactor){
00178                                 if(k > ori_nz*percent){
00179                                         cout << float(k)/float(ori_nz) << "% Done!" << endl;
00180                                         percent+=0.1f;
00181                                 }
00182                                 // read in a slice region
00183                                 const Region* binregion = new Region(0,0,k,ori_nx,ori_ny,zbin);
00184                                 tempdata->read_image(filename, 0, false, binregion);
00185                                 // shrink the slice
00186                                 if (binfactor > 1) tempdata->process_inplace("math.meanshrink",Dict("n",binfactor));
00187                                 size_t offset = nx*ny*k/binfactor;
00188                                 //add slice to total
00189                                 EMUtil::em_memcpy(get_data()+offset,tempdata->get_data(),sizeofslice);
00190                                 delete binregion;
00191                         }
00192 
00193                         delete tempdata;
00194                         update();
00195                 }
00196         }
00197 
00198     EMUtil::close_imageio(filename, imageio);
00199         imageio = 0;
00200         EXITFUNC;
00201 }
00202 
00203 #include <sys/stat.h>
00204 
00205 void EMData::write_image(const string & filename, int img_index,
00206                                                  EMUtil::ImageType imgtype,
00207                                                  bool header_only, const Region * region,
00208                                                  EMUtil::EMDataType filestoragetype,
00209                                                  bool use_host_endian)
00210 {
00211         ENTERFUNC;
00212 
00213         struct stat fileinfo;
00214         if ( region && stat(filename.c_str(),&fileinfo) != 0 ) throw UnexpectedBehaviorException("To write an image using a region the file must already exist and be the correct dimensions");
00215 
00216         if (is_complex() && is_shuffled())
00217                 fft_shuffle();
00218 
00219         if (imgtype == EMUtil::IMAGE_UNKNOWN) {
00220                 const char *ext = strrchr(filename.c_str(), '.');
00221                 if (ext) {
00222                         ext++;
00223                         imgtype = EMUtil::get_image_ext_type(ext);
00224                 }
00225         }
00226         ImageIO::IOMode rwmode = ImageIO::READ_WRITE;
00227 
00228         //set "nx", "ny", "nz" and "changecount" in attr_dict, since they are taken out of attribute dictionary
00229 
00230         attr_dict["nx"] = nx;
00231         attr_dict["ny"] = ny;
00232         attr_dict["nz"] = nz;
00233         attr_dict["changecount"] = changecount;
00234 
00235     // Check if this is a write only format.
00236     if (Util::is_file_exist(filename)) {
00237         if (!header_only && region == 0) {
00238             ImageIO * tmp_imageio = EMUtil::get_imageio(filename, ImageIO::READ_ONLY, imgtype);
00239             if (tmp_imageio->is_single_image_format()) {
00240                 rwmode = ImageIO::WRITE_ONLY;
00241             }
00242             EMUtil::close_imageio(filename, tmp_imageio);
00243             tmp_imageio = 0;
00244         }
00245     }
00246 
00247         LOGVAR("getimageio %d",rwmode);
00248         ImageIO *imageio = EMUtil::get_imageio(filename, rwmode, imgtype);
00249         if (!imageio) {
00250                 throw ImageFormatException("cannot create an image io");
00251         }
00252         else {
00253                 update_stat();
00254                 /* Let each image format decide how to deal with negative image_index*/
00255 //              if (img_index < 0) {
00256 //                      img_index = imageio->get_nimg();
00257 //              }
00258                 LOGVAR("header write %d",img_index);
00259 
00260                 switch(filestoragetype) {
00261                 case EMUtil::EM_UINT:
00262                         attr_dict["datatype"] = (int)EMUtil::EM_UINT;
00263                         break;
00264                 case EMUtil::EM_USHORT:
00265                         attr_dict["datatype"] = (int)EMUtil::EM_USHORT;
00266                         break;
00267                 case EMUtil::EM_SHORT:
00268                         attr_dict["datatype"] = (int)EMUtil::EM_SHORT;
00269                         break;
00270                 case EMUtil::EM_CHAR:
00271                         attr_dict["datatype"] = (int)EMUtil::EM_CHAR;
00272                         break;
00273                 case EMUtil::EM_UCHAR:
00274                         attr_dict["datatype"] = (int)EMUtil::EM_UCHAR;
00275                         break;
00276                 default:
00277                         attr_dict["datatype"] = (int)EMUtil::EM_FLOAT;; //default float
00278                 }
00279 
00280                 int err = imageio->write_header(attr_dict, img_index, region, filestoragetype,
00281                                                                                 use_host_endian);
00282                 if (err) {
00283                         throw ImageWriteException(filename, "imageio write header failed");
00284                 }
00285                 else {
00286                         if (!header_only) {
00287                                 if (imgtype == EMUtil::IMAGE_LST) {
00288                                         const char *reffile = attr_dict["LST.reffile"];
00289                                         if (strcmp(reffile, "") == 0) {
00290                                                 reffile = path.c_str();
00291                                         }
00292                                         int refn = attr_dict["LST.refn"];
00293                                         if (refn < 0) {
00294                                                 refn = pathnum;
00295                                         }
00296 
00297                                         const char *comment = attr_dict["LST.comment"];
00298                                         char *lstdata = new char[1024];
00299                                         sprintf(lstdata, "%d\t%s", refn, reffile);
00300                                         if(strcmp(comment, "") != 0) {
00301                                                 sprintf(lstdata+strlen(lstdata), "\t%s\n", comment);
00302                                         }
00303                                         else {
00304                                                 strcat(lstdata, "\n");
00305                                         }
00306                                         err = imageio->write_data((float*)lstdata, img_index,
00307                                                                                           region, filestoragetype, use_host_endian);
00308                                         if( lstdata )
00309                                         {
00310                                                 delete [] lstdata;
00311                                                 lstdata = 0;
00312                                         }
00313                                 }
00314                                 if (imgtype == EMUtil::IMAGE_LSTFAST) {
00315                                         const char *reffile = attr_dict["LST.reffile"];
00316                                         if (strcmp(reffile, "") == 0) {
00317                                                 reffile = path.c_str();
00318                                         }
00319                                         int refn = attr_dict["LST.refn"];
00320                                         if (refn < 0) {
00321                                                 refn = pathnum;
00322                                         }
00323 
00324                                         const char *comment = attr_dict["LST.comment"];
00325                                         char *lstdata = new char[1024];
00326                                         sprintf(lstdata, "%d\t%s", refn, reffile);
00327                                         if(strcmp(comment, "") != 0) {
00328                                                 sprintf(lstdata+strlen(lstdata), "\t%s\n", comment);
00329                                         }
00330                                         else {
00331                                                 strcat(lstdata, "\n");
00332                                         }
00333                                         err = imageio->write_data((float*)lstdata, img_index,
00334                                                                                           region, filestoragetype, use_host_endian);
00335                                         if( lstdata )
00336                                         {
00337                                                 delete [] lstdata;
00338                                                 lstdata = 0;
00339                                         }
00340                                 }
00341                                 else {
00342                                         err = imageio->write_data(get_data(), img_index, region, filestoragetype,
00343                                                                                           use_host_endian);
00344                                 }
00345                                 if (err) {
00346                                         imageio->flush();
00347                                         throw ImageWriteException(filename, "imageio write data failed");
00348                                 }
00349                         }
00350                 }
00351         }
00352         //PNG image already do cleaning in write_data function.
00353         if (!(imgtype == EMUtil::IMAGE_PNG)) {
00354                 imageio->flush();
00355         }
00356 
00357     EMUtil::close_imageio(filename, imageio);
00358         imageio = 0;
00359         EXITFUNC;
00360 }
00361 
00362 
00363 void EMData::append_image(const string & filename,
00364                                                   EMUtil::ImageType imgtype, bool header_only)
00365 {
00366         ENTERFUNC;
00367         write_image(filename, -1, imgtype, header_only, 0);
00368         EXITFUNC;
00369 }
00370 
00371 
00372 void EMData::write_lst(const string & filename, const string & reffile,
00373                                            int refn, const string & comment)
00374 {
00375         ENTERFUNC;
00376         attr_dict["LST.reffile"] = reffile;
00377         attr_dict["LST.refn"] = refn;
00378         attr_dict["LST.comment"] = comment;
00379         write_image(filename, -1, EMUtil::IMAGE_LST, false);
00380         EXITFUNC;
00381 }
00382 
00383 
00384 void EMData::print_image(const string str, ostream& out) {
00385         out << "Printing EMData object: " << str << std::endl;
00386         int nx = get_xsize();
00387         int ny = get_ysize();
00388         int nz = get_zsize();
00389         for (int iz = 0; iz < nz; iz++) {
00390                 out << "(z = " << iz << " slice)" << std::endl;
00391                 for (int ix = 0; ix < nx; ix++) {
00392                         for (int iy = 0; iy < ny; iy++) {
00393                                 out << std::setiosflags(std::ios::fixed)
00394                                         << std::setiosflags(std::ios_base::scientific)
00395                                         << std::setw(12)
00396                                          << std::setprecision(5) << (*this)(ix,iy,iz) << "  ";
00397                                 if (((iy+1) % 6) == 0) {
00398                                         out << std::endl << "   ";
00399                                 }
00400                         }
00401                         out << std::endl;
00402                 }
00403         }
00404 }
00405 
00406 vector < shared_ptr<EMData> > EMData::read_images(const string & filename, vector < int >img_indices,
00407                                                                            bool header_only)
00408 {
00409         ENTERFUNC;
00410 
00411         int total_img = EMUtil::get_image_count(filename);
00412         size_t num_img = img_indices.size();
00413 
00414         for (size_t i = 0; i < num_img; i++) {
00415                 if (img_indices[i] < 0 || img_indices[i] >= total_img) {
00416                         throw OutofRangeException(0, total_img, img_indices[i], "image index");
00417                 }
00418         }
00419 
00420         size_t n = (num_img == 0 ? total_img : num_img);
00421 
00422         vector< shared_ptr<EMData> > v;
00423         for (size_t j = 0; j < n; j++) {
00424                 shared_ptr<EMData> d(new EMData());
00425                 size_t k = (num_img == 0 ? j : img_indices[j]);
00426                 try {
00427                         d->read_image(filename, (int)k, header_only);
00428                 }
00429                 catch(E2Exception &e) {
00430                         throw(e);
00431                 }
00432                 if ( d != 0 )
00433                 {
00434                         v.push_back(d);
00435                 }
00436                 else
00437                         throw ImageReadException(filename, "imageio read data failed");
00438         }
00439 
00440         EXITFUNC;
00441         return v;
00442 }
00443 
00444 
00445 vector < shared_ptr<EMData> >EMData::read_images_ext(const string & filename, int img_index_start,
00446                                                                                    int img_index_end, bool header_only,
00447                                                                                    const string & ext)
00448 {
00449         ENTERFUNC;
00450 
00451         if (img_index_end < img_index_start) {
00452                 throw InvalidValueException(img_index_end, "image index end < image index start");
00453         }
00454         string new_filename = filename;
00455         new_filename = new_filename.insert(new_filename.rfind("."), ext);
00456         int num_img = EMUtil::get_image_count(new_filename);
00457 
00458         if (img_index_start < 0 || img_index_start >= num_img) {
00459                 throw OutofRangeException(0, num_img-1, img_index_start, "image index start");
00460         }
00461 
00462         if (img_index_end >= num_img) {
00463                 img_index_end = num_img - 1;
00464         }
00465 
00466         vector < shared_ptr<EMData> >v;
00467 
00468         for (int i = img_index_start; i < img_index_end; i++) {
00469                 shared_ptr<EMData> d(new EMData());
00470                 try {
00471                         d->read_image(new_filename, i, header_only);
00472                 }
00473                 catch(E2Exception &e) {
00474                         throw(e);
00475                 }
00476                 v.push_back(d);
00477         }
00478         EXITFUNC;
00479         return v;
00480 }
00481