#include <malloc.h>
#include <stdio.h>


extern void read_sal_file_content();
void read_sal_input_file_wh(), read_sal_input_file_whKs();


/*   read a saliency input file.
 *   input:
 *     i_file - input file name.
 *   output:
 *     &xs, &ys, &ks, &sals, &neighs - the arrays of x,y,k,sal,neigh values.
 *         (the area for these arrays is allocated inside.)
 *     &n - number of elements in the arrays.
 *     &w - image width
 *     &h - image height
 */
void
read_sal_input_file_wh(i_file, xs, ys, ks, sals, neighs, n, w, h)
char *i_file;
int **xs, **ys, **ks, **neighs, *n, *w, *h;
double **sals;
{
  FILE *f_in;
  char line[100];
  int m_line = 100;
  int dim;

  printf(" Read file %s\n",i_file);

  f_in = fopen(i_file,"r");
  if (!f_in) {
    fprintf(stderr, "\n Couldn't open %s... aborting.\n", i_file);
    exit(3);
  }

  fgets(line, m_line, f_in);
  sscanf(line, "%*s %d %*s %*s %d %*s %*s %d %*s %*s %d", n, &dim, w, h);
  printf(" File contains %d points and %d columns. w=%d h=%d\n",
	 *n, dim, *w, *h);

  *xs = (int *) malloc(*n * sizeof(int));
  *ys = (int *) malloc(*n * sizeof(int));
  *ks = (int *) malloc(*n * sizeof(int));
  *sals = (double *) malloc(*n * sizeof(double));
  if (dim >= 5)
    *neighs = (int *) malloc(*n * sizeof(int));
  else
    *neighs = NULL;

  read_sal_file_content (f_in, *xs, *ys, *ks, *sals, *neighs, *n);

  fclose(f_in);
}


void
read_sal_input_file_whKs(i_file, xs, ys, ks, sals, neighs, n, w, h, K, scale)
char *i_file;
int **xs, **ys, **ks, **neighs, *n, *w, *h, *K, *scale;
double **sals;
{
  FILE *f_in;
  char line[100];
  int m_line = 100;
  int dim;

  printf(" Read file %s\n",i_file);

  f_in = fopen(i_file,"r");
  if (!f_in) {
    fprintf(stderr, "\n Couldn't open %s... aborting.\n", i_file);
    exit(3);
  }

  fgets(line, m_line, f_in);
  sscanf(line, "%*s %d %*s %*s %d %*s %*s %d %*s %*s %d %*s %*s %d %*s %*s %d",
	 n, &dim, w, h, K, scale);
  printf(" File contains %d points and %d columns. w=%d h=%d K=%d scale=%d\n",
	 *n, dim, *w, *h, *K, *scale);

  *xs = (int *) malloc(*n * sizeof(int));
  *ys = (int *) malloc(*n * sizeof(int));
  *ks = (int *) malloc(*n * sizeof(int));
  *sals = (double *) malloc(*n * sizeof(double));
  if (dim >= 5)
    *neighs = (int *) malloc(*n * sizeof(int));
  else
    *neighs = NULL;

  read_sal_file_content (f_in, *xs, *ys, *ks, *sals, *neighs, *n);

  fclose(f_in);
}
