/****************************************************************
 * Filename  :	Video.c
 * Functions :	
 *		Video_New
 *		Video_Delete
 *		Video_Initialize
 *		Video_OpenFile
 *		Video_ReadHeader
 *		Video_ReadColumn
 *		Video_AddOverlay
 *		Video_CloseFile
 *
 * Purpose   : 	Functions for the Video class
 $Revision: 1.1 $
 ***************************************************************/

#include <stdioLib.h>                  /* VxWorks standard I/O library        */
#include "dy4std.h"                     /* DY4 standard definitions           */
#include "dmvideo.h"                    /* data manager definitions           */
#include "mygraphics.h"                 /* graphics alignment definitions     */
#include "OVtypes.h"                    /* video overlay type definitions     */
#include "Alarm.h" 			/* definitions for Alarm class        */
#include "Condition.h" 			/* definitions for Condition class    */
#include "Config.h" 			/* definitions for Config class       */
#include "Overlay.h" 			/* definitions for Overlay class      */
#include "Video.h" 			/* definitions for Video class        */

/*---------------------------------------------------------------
 | Function  :	Video_New
 | Purpose   : 	object construction 
 | Inputs    :	name of resource file
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    OV_Video*
Video_New(OV_char *file_name)
{
    OV_Video 	*p_video;		/* an instance of video */
    OV_int	nb_char;		/* number of characters */

    p_video = (OV_Video *) malloc ( sizeof (OV_Video));

/* allocate and initalize string for file name */
    nb_char = My_get_length ( file_name);
    p_video->file_name = (char *) malloc ( nb_char+1);
    strncpy ( p_video->file_name, file_name, nb_char);
    *(p_video->file_name+nb_char) = NULL;

    return (p_video);

} /* Video_New */

/*---------------------------------------------------------------
 | Function  :	Video_Delete
 | Purpose   : 	object destruction
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 | Modified  :  tarrant 8/7/96
 |                to free all malloced components of structure
 ---------------------------------------------------------------*/
    void
Video_Delete(OV_Video *p_video)
{
    OV_int i_overlay;			/* overlay loop counter               */

    if (p_video->file_name != NULL)
	free(p_video->file_name);

    for (i_overlay = 0; i_overlay < p_video->nb_overlays_in_list; i_overlay++)
	if (p_video->p_overlays_list[i_overlay] != NULL)
	    Overlay_Delete(p_video->p_overlays_list[i_overlay]);

    free ( p_video);

    return;

} /* Video_Delete */

/*---------------------------------------------------------------
 | Function  :	Video_Initialize
 | Purpose   : 	object initialization
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Video_Initialize(OV_Video *p_video)
{
	p_video->header_length = 2;
	p_video->nb_overlays = 0;
	p_video->nb_overlays_in_list = 0;
	p_video->fd_file = NULL;

        return;

} /* Video_Initialize */


/*---------------------------------------------------------------
 | Function  :	Video_OpenFile
 | Purpose   : 	open the configuration file
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	fill attributes
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 | Modified  :  tarrant 10/30/96
 |                commented out all the French routines because
 |                they would bus error if the file did not exist
 ---------------------------------------------------------------*/
	uint32
Video_OpenFile(OV_Video *p_video)
{
	OV_int	 kerr;		/* error code */
	FILE	 *fd_file;		/* pointer on file descriptor */

	kerr = 0;

	/* check file existence */
/*	kerr = ctrasc ( p_video->file_name);*/

	/* open it or print error message */
/*
	if (kerr == 0) {
	  printf ("--> opening file %s ...\n", p_video->file_name);
	  kerr = clsasc ( fd_file) ;
	  kerr = opiasc ( &fd_file, p_video->file_name) ;
	  p_video->fd_file = fd_file;
	} else {
	  printf ("--> %d: non existent file %s ...\n", kerr,
		  p_video->file_name) ;
	}
*/

	if ((fd_file = fopen(p_video->file_name, "r")) == NULL)
	{
	    kerr = -1;
	    printf("--> non existent file %s ...\n", p_video->file_name) ;
	}
	else
	{
	    p_video->fd_file = fd_file;
	    printf ("--> opening file %s ...\n", p_video->file_name);
	}

        return(kerr);

} /* Video_OpenFile */


/*---------------------------------------------------------------
 | Function  :	Video_ReadHeader
 | Purpose   : 	read information in file header
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	fill attributes with header information
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    uint32
Video_ReadHeader(OV_Video *p_video)
{
	OV_float rentt[LENTM];		/* array to store header */
	OV_int 	 lent;			/* header length */
	OV_int	 ient;			/* current header index */
	OV_int	 kerr;			/* error code */

	/* ....................... */
	/* ... Initializations ... */
	/* ....................... */

	kerr = 0;
	lent = p_video->header_length;

	/* read header as a float array */
	kerr = etlasc ( p_video->fd_file, rentt, &lent);

	if ( kerr < 0) {
	   printf ("\n *** error n0 %d in reading header ***\n", kerr);
           return(kerr);
	}

/*
for ( ient = 0 ; ient <  lent; ient++) {
   printf (" [%d] = %f\n", ient + 1, rentt[ient] ) ;
}
*/

	/* fill corresponding attributes */
	p_video->nb_overlays   = (OV_int) rentt[0];
	p_video->unique_output = (OV_int) rentt[1];

	/* ............. */
	/* ...  End  ... */
	/* ............. */

        return(kerr);

} /* Video_ReadHeader */


/*---------------------------------------------------------------
 | Function  :	Video_ReadColumn
 | Purpose   : 	read a column of values
 | Inputs    :	Pointer to video structure, number of the matrix to be read,
 |           :  number of the column to be read, array to store column values
 |           :  and array to store column strings.
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    uint32
Video_ReadColumn(OV_Video *p_video, OV_int matrix, OV_int column,
		 OV_float values_a[], OV_char *strings_a[])
{
	OV_int	 kerr;		/* error code */
	OV_int 	 kser;		/* matrix index */
	OV_int	 iechd;		/* index of the first value */
	OV_int	 ival;		/* current value index */
	OV_int	 istr;		/* current string index */
	OV_int	 ivec;		/* current vector index */
	OV_int	 nser;		/* number of matrix */
	OV_int	 nvec;		/* max number of vectors */
	OV_int	 nvallu;	/* number of values read in a line */
	OV_int	 nstrlu;	/* number of stringa read in a line */
	OV_float rvalt[20];	/* array to store line values */
	OV_char  *zstrt[20];	/* array to store line strings */
	OV_int	 nb_lines;	/* number of lines to be read */
	OV_int	 i_line;	/* line index */

	/* ....................... */
	/* ... Initializations ... */
	/* ....................... */

	kerr   = 0;
	kser   = matrix;
	iechd  = 1;
	nb_lines = p_video->nb_overlays;

	/* ............. */
	/* ... Begin ... */
	/* ............. */

	/* loop on lines */
	for ( i_line=0; i_line<nb_lines; i_line++);
	{

	   kerr = ligasc ( p_video->fd_file, 
			&kser,  
			&iechd,  
			&nvec,
			rvalt,  
			zstrt,  
			&nvallu,
			&nstrlu);

	   if ( kerr < 0)
	   {
	      printf ("\n *** error n0 %d in reading line ***\n", kerr);
	   } 
	   else
	   {
	      values_a[i_line]  = rvalt[column];
	      strings_a[i_line] = zstrt[column];
	   }

	} /* endfor */

/*debug
printf ( " matrix = %d \n", kser);
for ( i_line=0; i_line<nb_lines; i_line++);
{
   printf ("[%d] = %f \n", i_line + 1, values_a[i_line]) ;
   printf ("[%d] = %s \n", i_line + 1, strings_a[i_line]) ;
}
enddebug*/

	/* ............. */
	/* ...  End  ... */
	/* ............. */

        return(kerr);

} /* Video_ReadColumn */



/*---------------------------------------------------------------
 | Function  :	Video_ReadLine
 | Purpose   : 	read a line of information (text and values)
 | Inputs    :	Pointer to video structure, number of the matrix to be read,
 |           :  number of the line to be read, array to store line values
 |           :  and array to store line strings.
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    uint32
Video_ReadLine(OV_Video *p_video, OV_int matrix, OV_int line, OV_float rvalt[],
	       OV_char *zstrt[])
{
	OV_int	 kerr;			/* error code */
	OV_int 	 kser;			/* matrix index */
	OV_int	 iechd;			/* index of the first value */
	OV_int	 ival;			/* current value index */
	OV_int	 istr;			/* current string index */
	OV_int	 ivec;			/* current vector index */
	OV_int	 nser;			/* number of matrix */
	OV_int	 nvec;			/* max number of vectors */
	OV_int	 nvallu;		/* number of values read */
	OV_int	 nstrlu;		/* number of string read */

	/* ... Initializations ... */

	kerr  = 0;
	kser  = matrix;
	iechd = line+1;
	nvec  = NVECM;

	/* ... Begin ... */

	kerr = ligasc ( p_video->fd_file, 
			&kser,  
			&iechd,  
			&nvec,
			rvalt,  
			zstrt,  
			&nvallu,
			&nstrlu);

	if ( kerr >= 0) {
/*debug
	   printf ( " matrix = %d \n", matrix);
	   for( ival = 0; ival < nvallu; ival ++) {
		   printf ("[%d] = %f \n", ival + 1, rvalt[ival]) ;
	   }
	   for( istr = 0; istr < nstrlu; istr ++) {
		   printf ("[%d] = %s \n", istr + 1, zstrt[istr]) ;
	   }
enddebug*/

	}
	else {
	   printf ("\n *** error n0 %d in reading line ***\n", kerr);
	}

	/* ... End ... */

        return(kerr);

} /* Video_ReadLine */


/*---------------------------------------------------------------
 | Function  :	Video_AddOverlay
 | Purpose   : 	Add an overlay object to the list
 | Inputs    :	pointer on new overlay object
 | Outputs   :	none
 | Side FX   :	attributes nb_overlays,p_overlays_list modified
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    void
Video_AddOverlay(OV_Video *p_video, OV_Overlay *p_overlay)
{
	OV_int	nb_overlays_in_list;

	/* ... add an overlay to the list */

	nb_overlays_in_list = p_video->nb_overlays_in_list;
	p_video->p_overlays_list[nb_overlays_in_list] = p_overlay;
	nb_overlays_in_list = nb_overlays_in_list + 1;
	p_video->nb_overlays_in_list = nb_overlays_in_list;

        return;

} /* Video_AddOverlay */

/*---------------------------------------------------------------
 | Function  :	Video_CloseFile
 | Purpose   :  Close resource file	
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/
    uint32
Video_CloseFile(OV_Video *p_video)
{
	OV_int	kerr;	/* error code */

	kerr = clsasc ( p_video->fd_file);

        return (kerr);

} /* Video_CloseFile */


/*---------------------------------------------------------------
 | Function  :	Video_cccccc
 | Purpose   : 	
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 ---------------------------------------------------------------*/

	void
Video_cccccc ()
{

	/* ... Local Variables */
	/* ... Initializations */
	/* ... Begin */


	/* ... End */

        return;

} /* Video_cccccc */


