# include "Ximage.h"

void Screen_to_Image (double *x1, double *y1, double x2, double y2, KapaImageWidget *image) {

  double expand;

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

void Image_to_Screen (double *x1, double *y1, double x2, double y2, KapaImageWidget *image) {

  double expand;

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

