# include "Ximage.h"
# define INFRONT 3

void DrawOverlay (Graphic *graphic, Layout *layout, int N, FILE *f, int extra) {

  int i;
  double X, Y, dX, dY;
  int Xmin, Ymin, Xmax, Ymax;
  double expand, X0, Y0;
 
  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);
  }
  /*  
  X0 =  (layout[0].X + 1 - 0.5*layout[0].matrix.Naxis[0])/expand + 0.5*layout[0].picture.dx;
  Y0 =  (layout[0].Y + 1 - 0.5*layout[0].matrix.Naxis[1])/expand + 0.5*layout[0].picture.dy;
  */
  Image_to_Screen (&X0, &Y0, 0.0, 0.0, layout);
  X0 -= layout[0].picture.x;
  Y0 -= layout[0].picture.y;

  Xmin = 0;
  Ymin = 0;
  Xmax = layout[0].picture.dx;
  Ymax = layout[0].picture.dy;

  for (i = 0; i < layout[0].overlay[N].Nobjects; i++) {
    if (N == INFRONT) {
      X  = layout[0].overlay[N].objects[i].x * Xmax;
      Y  = layout[0].overlay[N].objects[i].y * Ymax;
      dX = layout[0].overlay[N].objects[i].dx * Xmax;
      dY = layout[0].overlay[N].objects[i].dy * Ymax;
    } else {
      X  = (layout[0].overlay[N].objects[i].x)/expand + X0;
      Y =  Ymax - (layout[0].overlay[N].objects[i].y)/expand - Y0;
      dX = (layout[0].overlay[N].objects[i].dx)/expand;
      dY = (layout[0].overlay[N].objects[i].dy)/expand;
    }
    
    if (!strcmp (layout[0].overlay[N].objects[i].type, "LINE")) {
      if (((X < Xmin) && (X + dX < Xmin)) || ((X > Xmax) && (X + dX > Xmax)) ||
	  ((Y < Ymin) && (Y + dY < Ymin)) || ((Y > Ymax) && (Y + dY > Ymax))) {
	continue;
      }
      fprintf (f, " %6.1f %6.1f %6.1f %6.1f L\n", X + extra, Y + extra, (X+dX + extra), (Y-dY + extra));
      continue;
    }
    if (!strcmp (layout[0].overlay[N].objects[i].type, "TEXT")) {
      if (((X < Xmin) && (X + dX < Xmin)) || ((X > Xmax) && (X + dX > Xmax)) ||
	  ((Y < Ymin) && (Y + dY < Ymin)) || ((Y > Ymax) && (Y + dY > Ymax))) {
	continue;
      }
      fprintf (f, "(%s) %6.1f %6.1f T\n", layout[0].overlay[N].objects[i].text, X + extra, Y + extra); 
      continue;
    }
    if (!strcmp (layout[0].overlay[N].objects[i].type, "BOX")) {
      if (((X - 0.5*dX < Xmin) && (X + 0.5*dX < Xmin)) || ((X - 0.5*dX > Xmax) && (X + 0.5*dX > Xmax)) ||
	  ((Y - 0.5*dY < Ymin) && (Y + 0.5*dY < Ymin)) || ((Y - 0.5*dY > Ymax) && (Y + 0.5*dY > Ymax))) {
	continue;
      }
      fprintf (f, " %6.1f %6.1f %6.1f %6.1f B\n", (dX + 2*extra), (dY + 2*extra), (X - 0.5*dX - extra), (Y - 0.5*dY - extra));
      continue;
    }
    if (!strcmp (layout[0].overlay[N].objects[i].type, "CIRCLE")) {
      if (((X - dX < Xmin) && (X + dX < Xmin)) || ((X - dX > Xmax) && (X + dX > Xmax)) ||
	  ((Y - dY < Ymin) && (Y + dY < Ymin)) || ((Y - dY > Ymax) && (Y + dY > Ymax))) {
	continue;
      }
      fprintf (f, " %6.1f %6.1f %6.1f C\n", X, Y, fabs(dX + extra));
      continue;
    }
    fprintf (stderr, "don't know %s, skipping\n", layout[0].overlay[N].objects[i].type);
  }
  
}

/* this routine is independent of the number of overlays */

