int ImagicIO::write_header(const Dict & dict, int image_index, const Region * area, EMUtil::EMDataType, bool use_host_endian) { ENTERFUNC; if(image_index<0) { image_index = get_nimg(); } check_write_access(rw_mode, image_index); nz = dict["nz"]; if (nz > 1 && image_index != 0) { throw ImageWriteException(filename, "to write 3D IMAGIC image, image index must be 0"); } if (area) { check_region(area, FloatSize(imagich.nx, imagich.ny, imagich.count+1), is_new_hed); EXITFUNC; return 0; } int nx = dict["nx"]; int ny = dict["ny"]; int nimg=0; //# images currently in file if (!is_new_hed) { datatype = get_datatype_from_name(imagich.type); if (imagich.nx != nx || imagich.ny != ny) { char desc[256]; sprintf(desc, "new IMAGIC size %dx%d is not equal to existing size %dx%d", nx, ny, imagich.nx, imagich.ny); throw ImageWriteException(filename, desc); } if (datatype!=IMAGIC_FLOAT) { throw ImageWriteException(filename, "Attempted write to non REAL Imagic file"); } rewind(hed_file); nimg=imagich.count+1; } ImagicHeader new_hed; memset(&new_hed, 0, sizeof(ImagicHeader)); time_t cur_time = time(0); struct tm *tm = localtime(&cur_time); new_hed.error = 0; new_hed.headrec = 1; new_hed.mday = tm->tm_mday; new_hed.month = tm->tm_mon; new_hed.year = tm->tm_year + 1900; new_hed.hour = tm->tm_hour; new_hed.minute = tm->tm_min; new_hed.sec = tm->tm_sec; new_hed.reals = nx * ny; new_hed.pixels = nx * ny; new_hed.ny = ny; new_hed.nx = nx; new_hed.ixold = 0; new_hed.iyold = 0; new_hed.oldav = 0; new_hed.min = (float)dict["minimum"]; new_hed.max = (float)dict["maximum"]; new_hed.avdens = (float)dict["mean"]; new_hed.sigma = (float)dict["sigma"]; if(nz<=1 && dict.has_key("xform.projection")) { Transform * t = dict["xform.projection"]; Dict d = t->get_rotation("eman"); new_hed.mrc1[1] = (float)d["alt"]*M_PI/180.0f; new_hed.mrc1[2] = (float)d["az"]*M_PI/180.0f; new_hed.mrc1[0] = (float)d["phi"]*M_PI/180.0f; if(t) {delete t; t=0;} } else if(nz>1 && dict.has_key("xform.align3d")) { Transform * t = dict["xform.align3d"]; Dict d = t->get_rotation("eman"); new_hed.mrc1[1] = (float)d["alt"]*M_PI/180.0f; new_hed.mrc1[2] = (float)d["az"]*M_PI/180.0f; new_hed.mrc1[0] = (float)d["phi"]*M_PI/180.0f; if(t) {delete t; t=0;} } else { if(dict.has_key("euler_alt")) new_hed.mrc1[1] = (float)dict["euler_alt"]*M_PI/180.0f; if(dict.has_key("euler_az")) new_hed.mrc1[2] = (float)dict["euler_az"]*M_PI/180.0f; if(dict.has_key("euler_phi")) new_hed.mrc1[0] = (float)dict["euler_phi"]*M_PI/180.0f; } if(dict.has_key("ptcl_repr")) new_hed.mrc2 = (int)dict["ptcl_repr"]; string new_label = dict.has_key("IMAGIC.label") ? (string) dict["IMAGIC.label"] : ""; sprintf(new_hed.label, "%s", new_label.c_str() ); new_hed.lbuf = nx; new_hed.inn = 1; new_hed.iblp = ny; new_hed.ifb = 0; new_hed.lbw = 0; new_hed.lbr = -1; new_hed.lastlr = -1; new_hed.lastlw = 1; new_hed.num = 8; new_hed.nhalf = nx / 2; new_hed.ibsd = nx * 2; new_hed.ihfl = 7; new_hed.lcbr = -1; new_hed.lcbw = 1; new_hed.imstr = -1; new_hed.imstw = -1; new_hed.istart = 1; new_hed.iend = nx; new_hed.leff = nx; new_hed.linbuf = nx * 2; new_hed.ntotbuf = -1; new_hed.icstart = 1; new_hed.icend = nx / 2; strncpy(new_hed.type, REAL_TYPE_MAGIC,4); // header in file order if ( (is_big_endian != ByteOrder::is_host_big_endian()) || !use_host_endian) swap_header(new_hed); // overwrite existing header if necessary if (image_index>=0 && image_index<nimg) { portable_fseek(hed_file, sizeof(ImagicHeader)*image_index, SEEK_SET); new_hed.imgnum=image_index+1; if (is_big_endian != ByteOrder::is_host_big_endian()) ByteOrder::swap_bytes((int *) &new_hed.imgnum,1); fwrite(&new_hed, sizeof(ImagicHeader),1,hed_file); } // How many images does the file need when we're done ? int required_len; if (nz>1) required_len=nz; else { if (image_index<0) required_len=nimg+1; else if (image_index+1>nimg) required_len=image_index+1; else required_len=nimg; } // Extend the file to the necessary length portable_fseek(hed_file, 0, SEEK_END); while (nimg<required_len) { nimg++; new_hed.imgnum=nimg; if (is_big_endian != ByteOrder::is_host_big_endian()) ByteOrder::swap_bytes((int *) &new_hed.imgnum,1); fwrite(&new_hed, sizeof(ImagicHeader),1,hed_file); } // update the 1st header with total # images portable_fseek(hed_file, sizeof(int), SEEK_SET); nimg--; if (is_big_endian != ByteOrder::is_host_big_endian()) ByteOrder::swap_bytes((int *) &nimg,1); fwrite(&nimg, sizeof(int), 1, hed_file); // header in machine order if ( (is_big_endian != ByteOrder::is_host_big_endian()) || !use_host_endian) swap_header(new_hed); imagich=new_hed; imagich.count=nimg; is_new_hed = false; if( dict.has_key("ctf") ) { Ctf * ctf_ = dict["ctf"]; write_ctf(ctf_); if(ctf_) {delete ctf_; ctf_=0;} } EXITFUNC; return 0; }
void EMUtil::getRenderLimits(const Dict & dict, float & rendermin, float & rendermax) { const char min_or_max_flag = 'm'; const char std_devs_flag = 's'; const float flag_base = 1.0e30; const float use_data_min_or_max = -flag_base; string svalue; const char * str; char flag; float num_std_devs; bool debug = (getenv("DEBUG_RENDER_LIMITS") != NULL); rendermin = 0.0; rendermax = 0.0; if (debug) { printf ("into RenderLimits, rmin = %g, rmax = %g\n", rendermin, rendermax); } if (dict.has_key("render_min")) { svalue = static_cast<string>(dict["render_min"]); str = svalue.c_str(); flag = str[0]; if (debug) printf ("render_min = %s\n", str); if (flag == min_or_max_flag) { rendermin = use_data_min_or_max; } else if (flag == std_devs_flag) { num_std_devs = 4.0; sscanf (str+1, "%g", & num_std_devs); rendermin = flag_base * num_std_devs; } else { rendermin = (float) dict["render_min"]; } } if (dict.has_key("render_max")) { svalue = static_cast<string>(dict["render_max"]); str = svalue.c_str(); flag = str[0]; if (debug) printf ("render_max = %s\n", str); if (flag == min_or_max_flag) { rendermax = use_data_min_or_max; } else if (flag == std_devs_flag) { num_std_devs = 4.0; sscanf (str+1, "%g", & num_std_devs); rendermax = flag_base * num_std_devs; } else { rendermax = (float) dict["render_max"]; } } if (debug) { printf ("out of RenderLimits, rmin = %g, rmax = %g\n", rendermin, rendermax); } }
int PgmIO::write_header(const Dict & dict, int image_index, const Region*, EMUtil::EMDataType, bool) { ENTERFUNC; int err = 0; //single image format, index can only be zero if(image_index == -1) { image_index = 0; } if(image_index != 0) { throw ImageWriteException(filename, "PGM file does not support stack."); } check_write_access(rw_mode, image_index); int nz = dict["nz"]; if ((int)nz != 1) { LOGERR("Cannot write 3D image as PGM. Your image nz = %d", nz); err = 1; throw ImageWriteException("N/A", "Cannot write 3D image as PGM."); } else { nx = dict["nx"]; ny = dict["ny"]; if(dict.has_key("min_grey")) minval = dict["min_gray"]; if(dict.has_key("max_grey")) maxval = dict["max_gray"]; //if we didn't get any good values from attributes, assign to 255 by default #ifdef _WIN32 if (maxval<=minval || _isnan(minval) || _isnan(maxval)) { #else if (maxval<=minval || std::isnan(minval) || std::isnan(maxval)) { #endif //_WIN32 maxval = 255; } if(dict.has_key("render_min")) rendermin=(float)dict["render_min"]; // float value representing black in the output if(dict.has_key("render_max")) rendermax=(float)dict["render_max"]; // float value representign white in the output fprintf(pgm_file, "%s\n%d %d\n%d\n", MAGIC_BINARY, nx, ny, maxval); } EXITFUNC; return err; } int PgmIO::read_data(float *data, int image_index, const Region * area, bool) { ENTERFUNC; //single image format, index can only be zero image_index = 0; check_read_access(image_index, data); check_region(area, IntSize(nx, ny)); portable_fseek(pgm_file, file_offset, SEEK_SET); unsigned char *cdata = (unsigned char *) (data); size_t mode_size = sizeof(unsigned char); EMUtil::process_region_io(cdata, pgm_file, READ_ONLY, image_index, mode_size, nx, ny, 1, area, true); int xlen = 0, ylen = 0; EMUtil::get_region_dims(area, nx, &xlen, ny, &ylen); for (int k = xlen * ylen - 1; k >= 0; k--) { data[k] = static_cast < float >(cdata[k]); } EXITFUNC; return 0; }