Пример #1
0
void EMData::read_image(const string & filename, int img_index, bool nodata,
						const Region * region, bool is_3d)
{
	ENTERFUNC;

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

	if (!imageio) {
		throw ImageFormatException("cannot create an image io");
	}
	else {
		int err = imageio->read_header(attr_dict, img_index, region, is_3d);
		if (err) {
			throw ImageReadException(filename, "imageio read header failed");
		}
		else {
			LstIO * myLstIO = dynamic_cast<LstIO *>(imageio);
			if(!myLstIO)	attr_dict["source_path"] = filename;	//"source_path" is set to full path of reference image for LstIO, so skip this statement
			attr_dict["source_n"] = img_index;
			if (imageio->is_complex_mode()) {
				set_complex(true);
				set_fftpad(true);
			}
			if (attr_dict.has_key("is_fftodd") && (int)attr_dict["is_fftodd"] == 1) {
				set_fftodd(true);
			}
			if ((int) attr_dict["is_complex_ri"] == 1) {
				set_ri(true);
			}
			save_byteorder_to_dict(imageio);

			nx = attr_dict["nx"];
			ny = attr_dict["ny"];
			nz = attr_dict["nz"];
			attr_dict.erase("nx");
			attr_dict.erase("ny");
			attr_dict.erase("nz");

			if (!nodata) {

				if (region) {
					nx = (int)region->get_width();
					if (nx <= 0) nx = 1;
					ny = (int)region->get_height();
					if (ny <= 0) ny = 1;
					nz = (int)region->get_depth();
					if (nz <= 0) nz = 1;
					set_size(nx,ny,nz);
					to_zero(); // This could be avoided in favor of setting only the regions that were not read to to zero... but tedious
				} // else the dimensions of the file being read match those of this
				else {
					set_size(nx, ny, nz);
				}

				// If GPU features are enabled there is  danger that rdata will
				// not be allocated, but set_size takes care of this, so this
				// should be safe.
				int err = imageio->read_data(get_data(), img_index, region, is_3d);
				if (err) {
					throw ImageReadException(filename, "imageio read data failed");
				}
				else {
					update();
				}
			}
			else {
				if (rdata!=0) EMUtil::em_free(rdata);
				rdata=0;
			}
				
		}
	}

#ifndef IMAGEIO_CACHE
	if( imageio )
	{
#ifdef HDFIO_CACHE
		if(dynamic_cast<HdfIO2*>(imageio)==NULL && dynamic_cast<HdfIO*>(imageio)==NULL) {
#endif	//HDFIO_CACHE
			delete imageio;
			imageio = 0;
#ifdef HDFIO_CACHE
		}
#endif	//HDFIO_CACHE
	}
#endif	//IMAGEIO_CACHE

	EXITFUNC;
}
Пример #2
0
void EMData::read_binedimage(const string & filename, int img_index, int binfactor, bool fast, bool is_3d)
{
	ENTERFUNC;
	
	ImageIO *imageio = EMUtil::get_imageio(filename, ImageIO::READ_ONLY);
	
	if (!imageio) {
		throw ImageFormatException("cannot create an image io");
	}
	else {
		int err = imageio->read_header(attr_dict, img_index, 0, is_3d);
		if (err) {
			throw ImageReadException(filename, "imageio read header failed");
		}
		else {
			attr_dict["source_path"] = filename;
			attr_dict["source_n"] = img_index;
			if (imageio->is_complex_mode()) {
				set_complex(true);
				set_fftpad(true);
			}
			if (attr_dict.has_key("is_fftodd") && (int)attr_dict["is_fftodd"] == 1) {
				set_fftodd(true);
			}
			if ((int) attr_dict["is_complex_ri"] == 1) {
				set_ri(true);
			}
			save_byteorder_to_dict(imageio);

			int ori_nx = nx = attr_dict["nx"];
			int ori_ny = ny = attr_dict["ny"];
			int ori_nz = nz = attr_dict["nz"];
			attr_dict.erase("nx");
			attr_dict.erase("ny");
			attr_dict.erase("nz");
			
			// At this point nx, ny and nz are all reduced by binfactor
			set_size(nx/binfactor, ny/binfactor, nz/binfactor);

			//here is where we read in the binned data
			EMData* tempdata = new EMData();
			size_t sizeofslice = nx*ny*sizeof(float);
			
			//zbin factor use 1 to speed binning(but don't benfit by averaging in Z)
			int zbin = binfactor;
			if(fast) zbin = 1;
			//verbose
			float percent = 0.1f;
			for(int k = 0; k < ori_nz; k+=binfactor){
				if(k > ori_nz*percent){	
					cout << float(k)/float(ori_nz) << "% Done!" << endl;
					percent+=0.1f;
				}
				// read in a slice region
				const Region* binregion = new Region(0,0,k,ori_nx,ori_ny,zbin);
				tempdata->read_image(filename, 0, false, binregion);
				// shrink the slice
				if (binfactor > 1) tempdata->process_inplace("math.meanshrink",Dict("n",binfactor));
				size_t offset = nx*ny*k/binfactor;
				//add slice to total
				EMUtil::em_memcpy(get_data()+offset,tempdata->get_data(),sizeofslice);
				delete binregion;
			}
			
			delete tempdata;
			update();
		}
	}

#ifndef IMAGEIO_CACHE
	if( imageio )
	{
#ifdef HDFIO_CACHE
		if(dynamic_cast<HdfIO2*>(imageio)==NULL && dynamic_cast<HdfIO*>(imageio)==NULL) {
#endif	//HDFIO_CACHE
			delete imageio;
			imageio = 0;
#ifdef HDFIO_CACHE
		}
#endif	//HDFIO_CACHE
	}
#endif	//IMAGEIO_CACHE

	EXITFUNC;
}