#include<stdio.h>

int get_num_gcps(int *);
void affine_trans(void);

int main(int argc, char **argv)
{
  int poly_lev;
  
  if (argc != 2) {
    printf("%s [polynomial level]\n", argv[0]);
    return 0;
  }
 
  poly_lev = atoi(argv[1]);

  printf("You need %d GCPs to apply a %d level polynomial transformation\n", get_num_gcps(&poly_lev),poly_lev);

  affine_trans();

  return 0;
}

int get_num_gcps(int *ppoly_lev)
{
  return (*ppoly_lev + 1) * (*ppoly_lev + 2) / 2;
}

/*
get_poly_lev(int *pnum_gcps)
{
  return ;
}
*/
void affine_trans()
{
  float x1,y1;
  int x,y;
  float A,B,C,D,E,F;
  int numrows, numcols;
  
  printf("Enter the x pixel spacing: ");
  scanf("%f", &A);
  printf("Enter the negative y pixel spacing: ");
  scanf("%f", &E);
  printf("Enter the x rotation: ");
  scanf("%f", &B);
  printf("Enter the y rotation: ");
  scanf("%f", &D);
  printf("Enter the upper-left x real-world coordinate: ");
  scanf("%f", &C);
  printf("Enter the upper-left y real-world coordinate: ");
  scanf("%f", &F);

  printf("Enter the number of rows of the image: ");
  scanf("%d", &numrows);
  printf("Enter the number of columns of the image: ");
  scanf("%d", &numcols);
  
  for(x=0; x < numrows; x++) {
    for(y=0; y < numcols; y++) {
      printf("%f %f %f %f %f %f %d %d\n", A,B,C,D,E,F,numrows,numcols);
      x1 = (A*x) + (B*y) + (C);
      y1 = (D*x) + (E*y) + (F);
      printf("row %d, col %d are: \nx1: %f\ny1: %f\n\n",x,y,x1,y1);
    }
  }
} 

