# include "Ximage.h"
# define INFRONT 4

void PaintOverlay (Graphic *graphic, Layout *layout, int N) {

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

  Image_to_Screen (&X0, &Y0, 0.0, 0.0, layout);

  Xmin = layout[0].picture.x;
  Ymin = layout[0].picture.y;
  Xmax = layout[0].picture.x + layout[0].picture.dx;
  Ymax = layout[0].picture.y + layout[0].picture.dy;
  Xrange = layout[0].picture.dx;
  Yrange = 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 * Xrange + Xmin;
      Y  = layout[0].overlay[N].objects[i].y * Yrange + Ymin;
      dX = layout[0].overlay[N].objects[i].dx * Xrange;
      dY = layout[0].overlay[N].objects[i].dy * Yrange;
    } else {
      X  = layout[0].overlay[N].objects[i].x/expand + X0;
      Y =  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 ((X + dX < Xmin) || (X - dX > Xmax) ||
	(Y + dY < Ymin) || (Y - dY > Ymax)) {
      continue;
    }

    /* for a LINE, (x, y) is the start, (dx, dy) is the distance to end
       for a CIRCLE (x, y) is the center, (dx, dy) is the radius 
       for a BOX (x, y) is the center, (dx, dy) is the width */

    if (!strcasecmp (layout[0].overlay[N].objects[i].type, "LINE")) {
      XDrawLine (graphic[0].display, graphic[0].window, graphic[0].gc, X, Y, (X+dX), (Y+dY));
      continue;
    }
    if (!strcasecmp (layout[0].overlay[N].objects[i].type, "TEXT")) {
      XDrawString (graphic[0].display, graphic[0].window, graphic[0].gc, X, Y, layout[0].overlay[N].objects[i].text, strlen(layout[0].overlay[N].objects[i].text));
      continue;
    }
    if (!strcasecmp (layout[0].overlay[N].objects[i].type, "BOX")) {
      dx = MAX (abs(dX),2) / 2;
      dy = MAX (abs(dY),2) / 2;
      XDrawRectangle (graphic[0].display, graphic[0].window, graphic[0].gc, (X - dx), (Y - dy), 2*dx, 2*dy);
       continue;
    }
    if (!strcasecmp (layout[0].overlay[N].objects[i].type, "CIRCLE")) {
      dx = MAX (abs(dX),2);
      dy = MAX (abs(dY),2);
      XDrawArc (graphic[0].display, graphic[0].window, graphic[0].gc, (X - dx), (Y - dy), 2*dx, 2*dy, 0, 23040);
      continue;
    }
    fprintf (stderr, "don't know %s, skipping (PO)\n", layout[0].overlay[N].objects[i].type);
  }
  
  XSetForeground (graphic[0].display, graphic[0].gc, graphic[0].black);

}

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

