////////////////////////////////////////////////////////////////////////////
// Fast Track example I
//
// Usage: file2FT image_filename
//
// This program loads an image from a file and runs FastTrack on
// it. It actually runs FastTrack twice. First HVEdgeCross and then
// VHEdgeCross.
//
// This program appears in the FastTrack documentation.
//
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2004  Joao Hespanha
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Modification history:
//
// Before August 27, 2006 
// No idea about what happened before this date...
//
// August 27, 2006
// GNU GPL added
// 
///////////////////////////////////////////////////////////////////////////

#include "../lib/FastTrack.h"

#define TEMPLATE_LONG_SIZE  3   // in the direction of derivative
#define TEMPLATE_SHORT_SIZE 2   // for averaging

#define EDGE_THRESHOLD 100      // threshold for edge detection

#define CRATIO_H .25            // Cross-rations being searched for
#define CRATIO_V .25            // (horizontal and vertical)

#define CRATIO_THRESHOLD .02    // Threshold to reject the cross ratio

FT_16GrayImage_T Image,VHOutput,HVOutput;
FT_8GrayImage_T  Image8,VHOutput8,HVOutput8;

int main(int argc, char *argv[])
{
  float vm[4],vb[4],hm[5],hb[4],px[8],py[8];
  struct timeval t0,t1;

  ////// initialize gtk /////////////////////////////////////////////
  gtk_init(&argc, &argv);
  gdk_rgb_init();

  ////// process argv to get filename ///////////////////////////////
  if (argc != 2) {
    fprintf(stderr,"%s : wrong number of arguments (1 expected, %d found)\n",argv[0],argc-1);
    exit(1); }

  ////// LoadImage() ////////////////////////////////////////////////
  LoadImage(argv[1],&Image);

  fprintf(stderr,"filename \"%s\", width = %d, height = %d\n",
          argv[1],Image.width,Image.height);

  ////// call HVEdgeCross() /////////////////////////////////////////
  //  memset(HVOutput.pixels,0,Image.pixelslength);
  memcpy(HVOutput.pixels,Image.pixels,Image.pixelslength);
  fprintf(stderr,"Horizontal-Vertical detection (HVEdgeCross)\n");
  gettimeofday(&t0,NULL);
  if (!HVEdgeCross(&Image,&HVOutput,0,0,Image.width-1,Image.height-1,
                   TEMPLATE_LONG_SIZE,TEMPLATE_SHORT_SIZE,EDGE_THRESHOLD,
                   CRATIO_H,CRATIO_THRESHOLD,
                   CRATIO_V,CRATIO_THRESHOLD,
                   vm,vb,hm,hb)) {
    Lines2Points(vm,vb,hm,hb,px,py);
    gettimeofday(&t1,NULL);
    printf("(%6.2f,%6.2f)-------------------------------(%6.2f,%6.2f)\n",px[0],py[0],px[1],py[1]);
    printf("       |      (%6.2f,%6.2f)---(%6.2f,%6.2f)      |\n",px[4],py[4],px[5],py[5]);
    printf("       |      (%6.2f,%6.2f)---(%6.2f,%6.2f)      |\n",px[7],py[7],px[6],py[6]);
    printf("(%6.2f,%6.2f)-------------------------------(%6.2f,%6.2f)\n",px[3],py[3],px[2],py[2]);
  } else {
    gettimeofday(&t1,NULL);
    fprintf(stderr,"Cross ratio not found!\n"); }
  printf("\t time elapse = %6.3f ms\n",
	  1000*(t1.tv_sec-t0.tv_sec)+.001*(t1.tv_usec-t0.tv_usec));

  ////// call VHEdgeCross() ////////////////////////////////////////
  memset(VHOutput.pixels,0,Image.pixelslength);
  //  memcpy(VHOutput.pixels,Image.pixels,Image.pixelslength);
  fprintf(stderr,"Vertical-Horizontal detection (VHEdgeCross)\n");
  gettimeofday(&t0,NULL);
  if (!VHEdgeCross(&Image,&VHOutput,0,0,Image.width-1,Image.height-1,
                   TEMPLATE_LONG_SIZE,TEMPLATE_SHORT_SIZE,EDGE_THRESHOLD,
                   CRATIO_H,CRATIO_THRESHOLD,
                   CRATIO_V,CRATIO_THRESHOLD,
                   vm,vb,hm,hb)) {
    Lines2Points(vm,vb,hm,hb,px,py);
    gettimeofday(&t1,NULL);
    printf("(%6.2f,%6.2f)-------------------------------(%6.2f,%6.2f)\n",px[0],py[0],px[1],py[1]);
    printf("       |      (%6.2f,%6.2f)---(%6.2f,%6.2f)      |\n",px[4],py[4],px[5],py[5]);
    printf("       |      (%6.2f,%6.2f)---(%6.2f,%6.2f)      |\n",px[7],py[7],px[6],py[6]);
    printf("(%6.2f,%6.2f)-------------------------------(%6.2f,%6.2f)\n",px[3],py[3],px[2],py[2]);
  } else {
    gettimeofday(&t1,NULL);
    fprintf(stderr,"Cross ratio not found!\n"); }
  printf("\t time elapse = %6.3f ms\n",
	  1000*(t1.tv_sec-t0.tv_sec)+.001*(t1.tv_usec-t0.tv_usec));

  ////// Display all images ////////////////////////////////////////
  Convert16to8(&Image,&Image8);
  DisplayNewImage(&Image8,"Original Image");

  Convert16to8(&HVOutput,&HVOutput8);
  DisplayNewImage(&HVOutput8,"HVEdgeCross (full image)");

  Convert16to8(&VHOutput,&VHOutput8);
  DisplayNewImage(&VHOutput8,"VHEdgeCross (full image)");

  ////// gtk_main() loop ///////////////////////////////////////////
  gtk_main();
}


