#include <stdio.h>
#include <ctype.h>


#include "/usr/include/opencv/highgui.h"
#include "/usr/include/opencv/cxtypes.h"
#include "/usr/include/opencv/cv.h"
//#include "/usr/include/opencv/cxmisc.h"
#include "/usr/include/opencv/cxcore.h"
#include "/usr/include/opencv/cvaux.h"

#include <dirent.h>

#include <sys/stat.h>

#define DEFAULT_IMAGE_SUFFIX "tif"
const char CO_prefix = 'c';
const char BW_prefix = 'I';

int init_viewer_window( int logtodisk )
{
      cvNamedWindow( "Black and White", 0 );
      cvNamedWindow( "Color", 0 );

      return 0;
}


int dest_viewer_window( int logtodisk )
{
  if (logtodisk)
    {
      cvDestroyWindow( "Black and White" );
      cvDestroyWindow( "Color" );
      return 0;
    }
  else
    return -1;
}

int parse_timestamp(char* timestamp, char*  filename, size_t timestamp_len )
{
  memset( timestamp, 0, timestamp_len);

  if ( filename[0]==BW_prefix )
  {  
      timestamp[0] = filename[13];
      timestamp[1] = filename[14];
      timestamp[2] = ':';
      timestamp[3] = filename[15];
      timestamp[4] = filename[16];
      timestamp[5] = ':';
      timestamp[6] = filename[17];
      timestamp[7] = filename[18];
      //    printf( "bw image timestamp: %s\n",timestamp );
    return 0;
  }
  else if ( filename[0]==CO_prefix )
    {
      timestamp[0] = filename[14];
      timestamp[1] = filename[15];
      timestamp[2] = ':';
      timestamp[3] = filename[16];
      timestamp[4] = filename[17];
      timestamp[5] = ':';
      timestamp[6] = filename[18];
      timestamp[7] = filename[19];
      
      //printf( "color image timestamp: %s\n",timestamp );
      return 0;
    }
  else
    {
      fprintf(stderr,"ERROR: parse_time_stamp: A %s file in the folder is not in the correct format\n", DEFAULT_IMAGE_SUFFIX);
      return -1;
    }
}

int cat_image_path( char* im_full_path,
		    size_t im_full_path_len,
		    char* log_dir,
		    char* filename )
{
  memset( im_full_path, 0, im_full_path_len);
  strcpy( im_full_path,log_dir );
  strcat( im_full_path,"/" );
  strcat( im_full_path,filename );
 
  return 0;
}


int view_image( char* im_full_path, 
		char* filename,
		char* timestamp )
{
   
  IplImage* im = cvvLoadImage( im_full_path );

   if (!im)
     {
        fprintf(stderr, 
	      "ERROR cvLoadImage: Could not load image file %s\n", 
	      im_full_path ) ;
        return -1;
     }

     CvFont font_timestamp;
     cvInitFont( &font_timestamp,
		CV_FONT_VECTOR0,
		3.0,    //scale
		3.0,       //italicness
		0,
		13,
		CV_AA );      //thickness
		
      
     cvPutText( im, 
	       timestamp, 
	       cvPoint(10,100),
	       &font_timestamp,
		cvScalarAll(200) );
     
     /*     cvPutText( im, 
	       framecount, 
	       cvPoint(900,100),
	       &font_timestamp,
	       cvScalarAll(200) ); */
    
     if ( filename[0] == CO_prefix )
       {
	 cvNamedWindow( "Color", 0 );
	 cvMoveWindow( "Color", 500, 0);
         cvShowImage( "Color", im );
       }
     if ( filename[0] == BW_prefix )
       {
         cvNamedWindow( "Black and White", 0 );
	 cvMoveWindow( "Black and White", 0, 0);
         cvShowImage( "Black and White", im );
       }
    	 
   
       cvWaitKey(1000);
       cvReleaseImage( &im );
 
    if ( filename[0] == CO_prefix )
       {
	 cvDestroyWindow( "Color" );
       }
     if ( filename[0] == BW_prefix )
       {
         cvDestroyWindow( "Black and White" );
       }

    return 0;
}
	 
   
  
   
