# include "data.h"

int vload (int argc, char **argv) {
  
  int i, N, Noverlay;
  int kapa, type;
  char *name;
  double dx, dy, size;
  KiiOverlay *overlay;
  Vector *vecx, *vecy;
  
  name = NULL;
  if ((N = get_argument (argc, argv, "-n"))) {
    remove_argument (N, &argc, argv);
    name = strcreate (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (!GetImage (NULL, &kapa, name)) return (FALSE);
  FREE (name);

  type = KII_OVERLAY_BOX;
  if ((N = get_argument (argc, argv, "-type"))) {
    remove_argument (N, &argc, argv);
    type = KiiOverlayTypeByName (argv[N]);
    remove_argument (N, &argc, argv);
    if (!type) {
      gprint (GP_ERR, "unknown Kii point type %s\n", argv[N]);
      return (FALSE);
    }
  }

  size = 1.0;
  if ((N = get_argument (argc, argv, "-size"))) {
    remove_argument (N, &argc, argv);
    size = fabs(atof (argv[N]));
    remove_argument (N, &argc, argv);
  }

  if (argc != 4) {
    gprint (GP_ERR, "USAGE: vload (overlay) (xvec) (yvec) [-n]\n");
    return (FALSE);
  }
  
  if (type == KII_OVERLAY_CIRCLE) {
    dx = dy = size / 2;
  } else {
    dx = dy = size;
  }    

  if ((vecx = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if ((vecy = SelectVector (argv[3], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if (vecx[0].Nelements != vecy[0].Nelements) {
    gprint (GP_ERR, "mismatched vector lengths\n");
    return (FALSE);
  }

  Noverlay = vecx[0].Nelements;
  ALLOCATE (overlay, KiiOverlay, Noverlay);

  for (i = 0; i < Noverlay; i++) {
    overlay[i].type = type;
    overlay[i].text = NULL;
    overlay[i].x = vecx[0].elements[i]+0.5;
    overlay[i].y = vecy[0].elements[i]+0.5;
    overlay[i].dx = dx;
    overlay[i].dy = dy;
    overlay[i].angle = 0.0;
  }

  KiiLoadOverlay (kapa, overlay, Noverlay, argv[1]);
  free (overlay);
  return (TRUE);
}

