# include "Ximage.h"

void Screen_to_Image (double *x1, double *y1, double x2, double y2, Layout *layout) {

  double expand;

  expand = 1.0;
  if (layout[0].expand > 0) {
    expand = 1 / (1.0*layout[0].expand);
  }
  if (layout[0].expand < 0) {
    expand = fabs((double)layout[0].expand);
  }
  
  *x1 = expand*(x2 - layout[0].picture.x - 0.5*layout[0].picture.dx) + 0.5*layout[0].matrix.Naxis[0] - layout[0].X;
  *y1 = expand*(y2 - layout[0].picture.y - 0.5*layout[0].picture.dy) + 0.5*layout[0].matrix.Naxis[1] - layout[0].Y;
  
}

void Image_to_Screen (double *x1, double *y1, double x2, double y2, Layout *layout) {

  double expand;

  /* notice that here, expand is the reciprocal of the expand above */
  expand = 1.0;
  if (layout[0].expand > 0) {
    expand = abs(layout[0].expand);
  }
  if (layout[0].expand < 0) {
    expand = 1 / ((double)abs(layout[0].expand));
  }
  
  *x1 = (x2 - 0.5*layout[0].matrix.Naxis[0] + layout[0].X) * expand + layout[0].picture.x + 0.5*layout[0].picture.dx;
  *y1 = (y2 - 0.5*layout[0].matrix.Naxis[1] + layout[0].Y) * expand + layout[0].picture.y + 0.5*layout[0].picture.dy;
  
}

