00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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 attr_dict["source_path"] = filename;
00068 attr_dict["source_n"] = img_index;
00069 if (imageio->is_complex_mode()) {
00070 set_complex(true);
00071 set_fftpad(true);
00072 }
00073 if (attr_dict.has_key("is_fftodd") && (int)attr_dict["is_fftodd"] == 1) {
00074 set_fftodd(true);
00075 }
00076 if ((int) attr_dict["is_complex_ri"] == 1) {
00077 set_ri(true);
00078 }
00079 save_byteorder_to_dict(imageio);
00080
00081 nx = attr_dict["nx"];
00082 ny = attr_dict["ny"];
00083 nz = attr_dict["nz"];
00084
00085
00086
00087
00088
00089
00090
00091
00092 if (!nodata) {
00093
00094 if (region) {
00095 nx = (int)region->get_width();
00096 if (nx <= 0) nx = 1;
00097 ny = (int)region->get_height();
00098 if (ny <= 0) ny = 1;
00099 nz = (int)region->get_depth();
00100 if (nz <= 0) nz = 1;
00101 set_size(nx,ny,nz);
00102 to_zero();
00103 }
00104 else {
00105 set_size(nx, ny, nz);
00106 }
00107
00108
00109
00110
00111 int err = imageio->read_data(get_data(), img_index, region, is_3d);
00112 if (err) {
00113 throw ImageReadException(filename, "imageio read data failed");
00114 }
00115 else {
00116 update();
00117 }
00118 }
00119 }
00120 }
00121
00122 #ifndef IMAGEIO_CACHE
00123 if( imageio )
00124 {
00125 delete imageio;
00126 imageio = 0;
00127 }
00128 #endif
00129 EXITFUNC;
00130 }
00131
00132 #include <sys/stat.h>
00133
00134 void EMData::write_image(const string & filename, int img_index,
00135 EMUtil::ImageType imgtype,
00136 bool header_only, const Region * region,
00137 EMUtil::EMDataType filestoragetype,
00138 bool use_host_endian)
00139 {
00140 ENTERFUNC;
00141
00142 struct stat fileinfo;
00143 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");
00144
00145 if (is_complex() && is_shuffled())
00146 fft_shuffle();
00147
00148 if (imgtype == EMUtil::IMAGE_UNKNOWN) {
00149 const char *ext = strrchr(filename.c_str(), '.');
00150 if (ext) {
00151 ext++;
00152 imgtype = EMUtil::get_image_ext_type(ext);
00153 }
00154 }
00155 ImageIO::IOMode rwmode = ImageIO::READ_WRITE;
00156
00157
00158 attr_dict["nx"] = nx;
00159 attr_dict["ny"] = ny;
00160 attr_dict["nz"] = nz;
00161 attr_dict["changecount"] = changecount;
00162
00163 if (Util::is_file_exist(filename)) {
00164 LOGVAR("file exists");
00165 if (!header_only && region == 0) {
00166 ImageIO * tmp_imageio = EMUtil::get_imageio(filename, ImageIO::READ_ONLY,
00167 imgtype);
00168 if (tmp_imageio->is_single_image_format()) {
00169 rwmode = ImageIO::WRITE_ONLY;
00170 }
00171 #ifndef IMAGEIO_CACHE
00172 if( tmp_imageio )
00173 {
00174 delete tmp_imageio;
00175 tmp_imageio = 0;
00176 }
00177 #endif
00178 }
00179 }
00180 LOGVAR("getimageio %d",rwmode);
00181 ImageIO *imageio = EMUtil::get_imageio(filename, rwmode, imgtype);
00182 if (!imageio) {
00183 throw ImageFormatException("cannot create an image io");
00184 }
00185 else {
00186 update_stat();
00187
00188
00189
00190
00191 LOGVAR("header write %d",img_index);
00192
00193 switch(filestoragetype) {
00194 case EMUtil::EM_UINT:
00195 attr_dict["datatype"] = (int)EMUtil::EM_UINT;
00196 break;
00197 case EMUtil::EM_USHORT:
00198 attr_dict["datatype"] = (int)EMUtil::EM_USHORT;
00199 break;
00200 case EMUtil::EM_SHORT:
00201 attr_dict["datatype"] = (int)EMUtil::EM_SHORT;
00202 break;
00203 case EMUtil::EM_CHAR:
00204 attr_dict["datatype"] = (int)EMUtil::EM_CHAR;
00205 break;
00206 case EMUtil::EM_UCHAR:
00207 attr_dict["datatype"] = (int)EMUtil::EM_UCHAR;
00208 break;
00209 default:
00210 attr_dict["datatype"] = (int)EMUtil::EM_FLOAT;;
00211 }
00212
00213 int err = imageio->write_header(attr_dict, img_index, region, filestoragetype,
00214 use_host_endian);
00215 if (err) {
00216 throw ImageWriteException(filename, "imageio write header failed");
00217 }
00218 else {
00219 if (!header_only) {
00220 if (imgtype == EMUtil::IMAGE_LST) {
00221 const char *reffile = attr_dict["LST.reffile"];
00222 if (strcmp(reffile, "") == 0) {
00223 reffile = path.c_str();
00224 }
00225 int refn = attr_dict["LST.refn"];
00226 if (refn < 0) {
00227 refn = pathnum;
00228 }
00229
00230 const char *comment = attr_dict["LST.comment"];
00231 char *lstdata = new char[1024];
00232 sprintf(lstdata, "%d\t%s", refn, reffile);
00233 if(strcmp(comment, "") != 0) {
00234 sprintf(lstdata+strlen(lstdata), "\t%s\n", comment);
00235 }
00236 else {
00237 strcat(lstdata, "\n");
00238 }
00239 err = imageio->write_data((float*)lstdata, img_index,
00240 region, filestoragetype, use_host_endian);
00241 if( lstdata )
00242 {
00243 delete [] lstdata;
00244 lstdata = 0;
00245 }
00246 }
00247 if (imgtype == EMUtil::IMAGE_LSTFAST) {
00248 const char *reffile = attr_dict["LST.reffile"];
00249 if (strcmp(reffile, "") == 0) {
00250 reffile = path.c_str();
00251 }
00252 int refn = attr_dict["LST.refn"];
00253 if (refn < 0) {
00254 refn = pathnum;
00255 }
00256
00257 const char *comment = attr_dict["LST.comment"];
00258 char *lstdata = new char[1024];
00259 sprintf(lstdata, "%d\t%s", refn, reffile);
00260 if(strcmp(comment, "") != 0) {
00261 sprintf(lstdata+strlen(lstdata), "\t%s\n", comment);
00262 }
00263 else {
00264 strcat(lstdata, "\n");
00265 }
00266 err = imageio->write_data((float*)lstdata, img_index,
00267 region, filestoragetype, use_host_endian);
00268 if( lstdata )
00269 {
00270 delete [] lstdata;
00271 lstdata = 0;
00272 }
00273 }
00274 else {
00275 err = imageio->write_data(get_data(), img_index, region, filestoragetype,
00276 use_host_endian);
00277 }
00278 if (err) {
00279 imageio->flush();
00280 throw ImageWriteException(filename, "imageio write data failed");
00281 }
00282 }
00283 }
00284 }
00285
00286 if (!(imgtype == EMUtil::IMAGE_PNG)) {
00287 imageio->flush();
00288 }
00289
00290 #ifndef IMAGEIO_CACHE
00291 if( imageio )
00292 {
00293 delete imageio;
00294 imageio = 0;
00295 }
00296 #endif
00297
00298
00299
00300 EXITFUNC;
00301 }
00302
00303
00304 void EMData::append_image(const string & filename,
00305 EMUtil::ImageType imgtype, bool header_only)
00306 {
00307 ENTERFUNC;
00308 write_image(filename, -1, imgtype, header_only, 0);
00309 EXITFUNC;
00310 }
00311
00312
00313 void EMData::write_lst(const string & filename, const string & reffile,
00314 int refn, const string & comment)
00315 {
00316 ENTERFUNC;
00317 attr_dict["LST.reffile"] = reffile;
00318 attr_dict["LST.refn"] = refn;
00319 attr_dict["LST.comment"] = comment;
00320 write_image(filename, -1, EMUtil::IMAGE_LST, false);
00321 EXITFUNC;
00322 }
00323
00324
00325 void EMData::print_image(const string str, ostream& out) {
00326 out << "Printing EMData object: " << str << std::endl;
00327 int nx = get_xsize();
00328 int ny = get_ysize();
00329 int nz = get_zsize();
00330 for (int iz = 0; iz < nz; iz++) {
00331 out << "(z = " << iz << " slice)" << std::endl;
00332 for (int ix = 0; ix < nx; ix++) {
00333 for (int iy = 0; iy < ny; iy++) {
00334 out << setiosflags(std::ios::fixed)
00335 << setiosflags(std::ios_base::scientific)
00336 << std::setw(12)
00337 << std::setprecision(5) << (*this)(ix,iy,iz) << " ";
00338 if (((iy+1) % 6) == 0) {
00339 out << std::endl << " ";
00340 }
00341 }
00342 out << std::endl;
00343 }
00344 }
00345 }
00346
00347 vector < shared_ptr<EMData> > EMData::read_images(const string & filename, vector < int >img_indices,
00348 bool header_only)
00349 {
00350 ENTERFUNC;
00351
00352 int total_img = EMUtil::get_image_count(filename);
00353 size_t num_img = img_indices.size();
00354
00355 for (size_t i = 0; i < num_img; i++) {
00356 if (img_indices[i] < 0 && img_indices[i] >= total_img) {
00357 throw OutofRangeException(0, total_img, img_indices[i], "image index");
00358 }
00359 }
00360
00361 size_t n = (num_img == 0 ? total_img : num_img);
00362
00363 vector< shared_ptr<EMData> > v;
00364 for (size_t j = 0; j < n; j++) {
00365 shared_ptr<EMData> d(new EMData());
00366 size_t k = (num_img == 0 ? j : img_indices[j]);
00367 try {
00368 d->read_image(filename, (int)k, header_only);
00369 }
00370 catch(E2Exception &e) {
00371 throw(e);
00372 }
00373 if ( d != 0 )
00374 {
00375 v.push_back(d);
00376 }
00377 else
00378 throw ImageReadException(filename, "imageio read data failed");
00379 }
00380
00381 EXITFUNC;
00382 return v;
00383 }
00384
00385
00386 vector < shared_ptr<EMData> >EMData::read_images_ext(const string & filename, int img_index_start,
00387 int img_index_end, bool header_only,
00388 const string & ext)
00389 {
00390 ENTERFUNC;
00391
00392 if (img_index_end < img_index_start) {
00393 throw InvalidValueException(img_index_end, "image index end < image index start");
00394 }
00395 string new_filename = filename;
00396 new_filename = new_filename.insert(new_filename.rfind("."), ext);
00397 int num_img = EMUtil::get_image_count(new_filename);
00398
00399 if (img_index_start < 0 || img_index_start >= num_img) {
00400 throw OutofRangeException(0, num_img-1, img_index_start, "image index start");
00401 }
00402
00403 if (img_index_end >= num_img) {
00404 img_index_end = num_img - 1;
00405 }
00406
00407 vector < shared_ptr<EMData> >v;
00408
00409 for (int i = img_index_start; i < img_index_end; i++) {
00410 shared_ptr<EMData> d(new EMData());
00411 try {
00412 d->read_image(new_filename, i, header_only);
00413 }
00414 catch(E2Exception &e) {
00415 throw(e);
00416 }
00417 v.push_back(d);
00418 }
00419 EXITFUNC;
00420 return v;
00421 }
00422