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