void MRCHeader::ToDensityHeader(DensityHeader &h)
{
  std::string empty;
  h.update_map_dimensions(nx,ny,nz);
  h.update_cell_dimensions();
  // mode
  if(mode==0)
    h.set_data_type(1);
  else if(mode==1)
    h.set_data_type(2);
  else if(mode==2)
    h.set_data_type(5);
  // number of first columns in map (default = 0)
  h.nxstart=nxstart ; h.nystart=nystart ; h.nzstart=nzstart;
  h.mx=mx ; h.my=my ; h.mz=mz; // Number of intervals along each dimension
  h.xlen=xlen;h.ylen=ylen;h.zlen=zlen;//Cell dimentions(amgstroms)
  h.alpha=alpha ; h.beta=beta ; h.gamma=gamma; // Cell angles (degrees)
  /* Axes corresponding to columns (mapc), rows (mapr) and sections (maps)
     (1,2,3 for x,y,z) */
  h.mapc=mapc;
  h.mapr=mapr;
  h.maps=maps;
  /* Minimum, maximum and mean density value */
  h.dmin=dmin;
  h.dmax=dmax;
  h.dmean=dmean;
  h.ispg=ispg; // Sapce group number 0 or 1 (default 0)
  h.nsymbt=nsymbt; // Number of bytes used for symmetry data (0 or 80)
  // extra space used for anything - 0 by default
  for(int i=0;i<IMP_MRC_USER;i++)
    h.user[i]=user[i];
  strcpy(h.map,"MAP\0"); // character string 'MAP ' to identify file type
  // Origin used for transforms
  h.set_xorigin(xorigin) ; h.set_yorigin(yorigin) ; h.set_zorigin(zorigin);
  // machine stamp (0x11110000 bigendian, 0x44440000 little)
  h.machinestamp=machinestamp;
  h.rms=rms; // RMS deviation of map from mean density
  h.nlabl=nlabl; // Number of labels being used
  // Copy comments
  for(int i=0;i<h.nlabl;i++) {
    //to make sure there is not memory leak
    std::string temp;
    temp.copy(labels[i],DensityHeader::COMMENT_FIELD_SINGLE_SIZE,0);
    strcpy(h.comments[i],temp.c_str());
  }
  // Fill empty coments with null character
  const char *c="\0";
  empty.resize(IMP_MRC_LABEL_SIZE,*c);
  for(int i=h.nlabl;i<IMP_MRC_NUM_LABELS;i++)
    strcpy(h.comments[i],empty.c_str());
}
DensityHeader create_density_header(const algebra::BoundingBoxD<3> &bb,
                                    float spacing) {
  DensityHeader header;
  algebra::Vector3D origin=bb.get_corner(0);
  algebra::Vector3D top=bb.get_corner(1);
  header.set_xorigin(origin[0]);
  header.set_yorigin(origin[1]);
  header.set_zorigin(origin[2]);
  header.Objectpixelsize_=spacing;
  //calculate the extent of the map
  int extent[3];
  for(int i=0;i<3;i++) {
    extent[i]=ceil((top[i]-origin[i])/spacing);
  }
  header.update_map_dimensions(extent[0],extent[1],extent[2]);
  header.compute_xyz_top(true);
  return header;
}
IMPEM_BEGIN_NAMESPACE

//! Function to transfer the (compatible) information content from ImageHeader
//! to DensityHeader
void ImageHeader_to_DensityHeader(const ImageHeader &h, DensityHeader &dh) {
  std::string empty;
  // map size and voxel size
  dh.update_map_dimensions(static_cast<int>(h.get_number_of_columns()),
                           static_cast<int>(h.get_number_of_rows()),
                           static_cast<int>(h.get_number_of_slices()));
  dh.Objectpixelsize_ = h.get_object_pixel_size();
  // mode
  switch ((int)h.get_fIform()) {
    case ImageHeader::IMG_BYTE:
      dh.set_data_type(1);
      break;
    case ImageHeader::IMG_IMPEM:
      dh.set_data_type(5);
      break;
    case ImageHeader::IMG_INT:
      dh.set_data_type(2);
      break;
    case ImageHeader::VOL_BYTE:
      dh.set_data_type(1);
      break;
    case ImageHeader::VOL_IMPEM:
      dh.set_data_type(5);
      break;
    case ImageHeader::VOL_INT:
      dh.set_data_type(2);
      break;
  }
  // number of first columns in map (default = 0)
  dh.nxstart = 0;
  dh.nystart = 0;
  dh.nzstart = 0;
  // Number of intervals along each dimension. In DensityHeader it is assumed
  // to be equal to the size of the map
  dh.mx = dh.get_nx();
  dh.my = dh.get_ny();
  dh.mz = dh.get_nz();
  // Cell angles (degrees)
  // Spider format does not have these fields. Filed with default (90 deg)
  dh.alpha = 90.;
  dh.beta = 90.;
  dh.gamma = 90.;
  /* Axes corresponding to columns (mapc), rows (mapr) and slices (maps)
     They are given the default values (1,2,3 for x,y,z) */
  dh.mapc = 1;
  dh.mapr = 2;
  dh.maps = 3;
  // Statistical values: Minimum, maximum, mean, standard deviation
  // Only set them if computed
  if ((int)h.get_fImami() == 1) {
    dh.dmin = h.get_fFmin();
    dh.dmax = h.get_fFmax();
    dh.dmean = h.get_fAv();
    dh.rms = h.get_fSig();
  } else {
    dh.dmin = 0;
    dh.dmax = 0;
    dh.dmean = 0;
    dh.rms = 0;
  }
  dh.ispg = 0;    // Sapce group number 0 or 1 (default 0)
  dh.nsymbt = 0;  // Number of bytes used for symmetry data (0 or 80, default 0)
  std::strcpy(dh.map, "MAP\0");  // string 'MAP ' to identify file type
  // Origin used for transforms
  algebra::Vector3D origin = h.get_origin();
  dh.set_xorigin(origin[0]);
  dh.set_yorigin(origin[1]);
  dh.set_zorigin(origin[2]);
  /* CCP4 convention machine stamp: 0x11110000 for big endian, or
    0x44440000 for little endian */
  unsigned char *ch;
  ch = (unsigned char *)&dh.machinestamp;
  if (algebra::get_is_big_endian()) {
    ch[0] = ch[1] = 0x11;
    ch[2] = ch[3] = 0;
  } else {
    ch[0] = ch[1] = 0x44;
    ch[2] = ch[3] = 0;
  }
  // Number of labels being used (default, 0)
  dh.nlabl = 0;
  // Reversed image or not
  if (h.get_reversed() == true) {
    if (algebra::get_is_big_endian()) {
      dh.lswap = 0;
    } else {
      dh.lswap = 1;
    }
  } else {
    if (algebra::get_is_big_endian()) {
      dh.lswap = 1;
    } else {
      dh.lswap = 0;
    }
  }
}