# include "Ximage.h"
# define INFRONT 4

static char name[4][16] = {"red", "green", "blue", "yellow"};

void bDrawOverlay (Layout *layout, int N) {

  int i;
  int dX, dY, dx, dy;
  int X, Y, Xmin, Ymin, Xmax, Ymax, Xrange, Yrange;
  double expand, X0, Y0;
  bDrawColor color;
 
  /* translate color to bDrawColors : layout[0].overlay[N].color */
  color = KapaColorByName (name[N]);
  bDrawSetStyle (color, 0, 0);
  
  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);
  X0 -= layout[0].picture.x;
  Y0 -= layout[0].picture.y;

  Xmin = 0;
  Ymin = 0;
  Xmax = layout[0].picture.dx;
  Ymax = 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;
      Y  = layout[0].overlay[N].objects[i].y * Yrange;
      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")) {
      bDrawLine (X, Y, (X+dX), (Y+dY));
      continue;
    }
    if (!strcasecmp (layout[0].overlay[N].objects[i].type, "TEXT")) {
      bDrawRotText (X, Y, layout[0].overlay[N].objects[i].text, 8, 0.0);
      continue;
    }
    if (!strcasecmp (layout[0].overlay[N].objects[i].type, "BOX")) {
      dx = MAX (abs(dX),2) / 2;
      dy = MAX (abs(dY),2) / 2;
      bDrawRectOpen ((X-dx), (Y-dy), (X+dx), (Y+dy));
      // bDrawRectOpen ((X-dx), (Y-dy), (X), (Y));
       continue;
    }
    if (!strcasecmp (layout[0].overlay[N].objects[i].type, "CIRCLE")) {
      dx = MAX (abs(dX),2);
      dy = MAX (abs(dY),2);
      bDrawArc (X, Y, dx, dy, 0, 360);
      continue;
    }
    fprintf (stderr, "don't know %s, skipping (PO)\n", layout[0].overlay[N].objects[i].type);
  }
  
  /* translate color to bDrawColors : layout[0].overlay[N].color */
  bDrawSetStyle (color, 0, 0);
}

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

