/* * * * * * * * * * * * * * * * * * *  * * * * * * * * * * * * * * * * * *
 *                                                                        *
 * Poly_Data:  Reports training and test pixels: class, row, column       *
 *                                                                        *
 *         -   Input parameters (at command line)are:                     *
 *                 -        Dataset filename                              *
 *                 -        Rows                                          *
 *                 -        Columns and                                   *
 *                 -        Header size                                   *
 *         -   Output parameters in two files                             *
 *             training.txt and test.txt are:                             *
 *                 -        Class number                                  *
 *                 -        Pixel row position and                        *
 *                 -        Pixel column position                         *
 *         -   Works up to 255 classes (8 bit images)                     *
 *                                                                        *
 * * * * * * * * * * * * * * * * * * * *  * * * * * * * * * * * * * * * * *
 *                                                                        *
 * Poly_Data    by Christos Iossifides   (c) December 2005 NaTUReS Lab.   *
 *                                                                        *
 * * * * * * * * * * * * * * * * * * *  * * * * * * * * * * * * * * * * * */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[]){
  FILE *fp, *fp1, *fp2;
  int row, col, hea;
  unsigned char *p;
  register int r, c;

  if(argc!=5){
    puts("Usage: >Poly_data filename rows columns header.");
    exit(1);
    }
  if((fp=fopen(argv[1],"rb"))==NULL){
    printf("Poly_data reports: Bad filename <%s>.\n",argv[1]);
    exit(1);
    }
  if((fp1=fopen("training.txt","w"))==NULL){
    printf("Poly_data reports: Bad filename <training.txt>.\n");
    exit(1);
    }
  if((fp2=fopen("test.txt","w"))==NULL){
    printf("Poly_data reports: Bad filename <test.txt>.\n");
    exit(1);
    }
  row=atoi(argv[2]);
  col=atoi(argv[3]);
  hea=atoi(argv[4]);
  if (row<1 ||col<1|| hea<0|| hea>=row*col*sizeof(char)){
    printf("Poly_data reports: Bad data <%d %d %d>.\n",row,col,hea);
    exit(1);
    }
  if(hea&&fseek(fp,hea,SEEK_SET)){
    puts("Poly_data reports: Bad seek.");
    exit(1);
    }
  if((p=(unsigned char *)malloc(col*sizeof(char)))==NULL){
    puts("Poly_data reports: Bad memory allocation.");
    exit(1);
    }

  for (r=0;r<row;r++){
    if(fread(p,sizeof(char),col,fp)!=col){
      puts("Poly_data reports: EOF encountered.");
      exit(1);
    }
    for (c=0;c<col;c++)
      if (p[c])
        fprintf(((r%2)?fp1:fp2),"%2d %4d %4d\n",(int)p[c],r+1,c+1);
  }

  fclose(fp);
  fclose(fp1);
  fclose(fp2);
  printf("\n\nPoly_Data by Christos Iossifides (c)Dec.2005 NaTUReS Lab.\n");

  return 0;
}

