 /*
  * Khoros: $Id: dmlatt.c,v 1.2 1992/03/20 23:31:50 dkhoros Exp $
  */

#if !defined(lint) && !defined(SABER)
static char rcsid[] = "Khoros: $Id: dmlatt.c,v 1.2 1992/03/20 23:31:50 dkhoros Exp $";
#endif

 /*
  * $Log: dmlatt.c,v $
 * Revision 1.2  1992/03/20  23:31:50  dkhoros
 * VirtualPatch5
 *
  */

/*
 *----------------------------------------------------------------------
 *
 * Copyright 1991, University of New Mexico.  All rights reserved.
 * Permission to copy and modify this software and its documen-
 * tation only for internal use in your organization is hereby
 * granted, provided that this notice is retained thereon and
 * on all copies.  UNM makes no representations as to the sui-
 * tability and operability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 * 
 * UNM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
 * NESS.  IN NO EVENT SHALL UNM BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY OTHER DAMAGES WHAT-
 * SOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PER-
 * FORMANCE OF THIS SOFTWARE.
 * 
 * No other rights, including, for example, the right to redis-
 * tribute this software and its documentation or the right to
 * prepare derivative works, are granted unless specifically
 * provided in a separate license agreement.
 *---------------------------------------------------------------------
 */

#include "unmcopyright.h"        /* Copyright 1991 by UNM */

/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 >>>>
 >>>>         File Name: dmlatt.c
 >>>>
 >>>>      Program Name: dmlatt
 >>>>
 >>>>      Program Desc: 
 >>>>	
 >>>>	1D MLMS Lattice Predictor Filter
 >>>>	
 >>>>	

 >>>>            Author: Ramiro Jordan
 >>>>
 >>>> Date Last Updated: Sat Feb 15 16:27:32 1992
 >>>>
 >>>>          Routines: main- the main program for dmlatt
 >>>> 		 gw_usage - gives usage of the program
 >>>> 		 gw_args  - gets arguments of program from command line
 >>>>
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/


#include "dmlatt.h"


/****************************************************************
 *
 * Routine Name:  main program for dmlatt
 *
 *       Input:  
 *        -i  input file (xviff file) 
 *        -o  resulting output file (xviff file) 
 *
 *
 *        [-s] number of stages for the filter   (value > 0)
 *        [-c] specify the convergence parameter  (0.000 < value < 1.000)
 *        [-b] specify the smoothing parameter  (0.000 < value < 1.000)
 *        [-j] scalar processing (0) or vector processing (1)
 *        [-d] down vectors(0) or across bands(1)
 *        [-g] type of output file to be created
 *		0 (choose error sequence ),
 *		1 (choose signal estimate sequence),
 *		or 2 (choose lattice filter coefficients)
 *		   [default = 0] 
 *
 *
 *
 ****************************************************************/


main(argc, argv)
int argc;
char *argv[];
{

/* -main_variable_list */
struct xvimage *image, *readimage();
int     stages, out_option, arith_type, procdir;
float   alpha, beta;
char    *malloc();

/* -main_variable_list_end */

	program = VStrcpy(argv[0]);
	ac = argc;
	av = argv;

	khoros_init();

	gw_get_args();

/* -main_before_lib_call */
if (check_args()) exit(1);
READIMAGE(image);
CHECKTYPE(program,image);
CHECKTYPE(program,image);

alpha = dmlatt->c_float;
beta = dmlatt->b_float;
stages = dmlatt->s_int;
arith_type = dmlatt->j_logic;
out_option = dmlatt->g_toggle;
procdir = dmlatt->d_logic;

/* -main_before_lib_call_end */

/* -main_library_call */

if(! ldmlatt ( image, alpha, beta, stages, arith_type, out_option, procdir ) )
        {
            (void) fprintf(stderr, "ldmlatt Failed\n");
            exit(1);
        }

/* -main_library_call_end */

/* -main_after_lib_call */
    writeimage(dmlatt->o_file,image);
/* -main_after_lib_call_end */


	khoros_close();
}


/****************************************************************
*
* Routine Name:  gw_usage 
*
*
* Purpose:  prints out the usage for dmlatt 
*
* Input:  none 
*
* Output:  none 
*
* Written By:  automatically generated by ghostwriter
*
****************************************************************/


gw_usage()
{
	fprintf(stderr, "dmlatt :\n");
	fprintf(stderr, "\t\n");
	fprintf(stderr, "\t1D MLMS Lattice Predictor Filter\n");
	fprintf(stderr, "\t\n");
	fprintf(stderr, "\t\n");

/* -usage_additions */
(void) fprintf(stderr,"where \"g\" is the output file to be created:\n");
(void) fprintf(stderr,"\t\"g=0\" error sequence (default)\n");
(void) fprintf(stderr,"\t\"g=1\" signal estimate sequence\n");
(void) fprintf(stderr,"\t\"g=2\" lattice filter coefficient sequence\n");
(void) fprintf(stderr,"where \"j\" type of arithmetic to be used:\n");
(void) fprintf(stderr,"\t\"j=0\" scalar arithmetic \n");
(void) fprintf(stderr,"\t\"j=1\" vector arithmetic (default)\n\n");
(void) fprintf(stderr,"where \"d\" direction of processing:\n");
(void) fprintf(stderr,"\t\"d=0\" down vectors \n");
(void) fprintf(stderr,"\t\"d=1\" across bands (default)\n\n");
/* -usage_additions_end */

	fprintf(stderr,"\t-i  input file (xviff file)  (infile)\n");
	fprintf(stderr,"\t-o  resulting output file (xviff file)  (outfile)\n");
	fprintf(stderr, "\n");
	fprintf(stderr, "\n");
	fprintf(stderr,"\t[-s]  number of stages for the filter  (integer, > 0) [1]\n");
	fprintf(stderr,"\t[-c]  specify the convergence parameter (float,  0.000 to 1.000) [0.01]\n");
	fprintf(stderr,"\t[-b]  specify the smoothing parameter (float,  0.000 to 1.000) [0.99]\n");
	fprintf(stderr,"\t[-j]  scalar processing (0) or vector processing (1) (boolean) [true]\n");
	fprintf(stderr,"\t[-d]  down vectors(0) or across bands(1) (boolean) [true]\n");
	fprintf(stderr, "\t[-g]  type of output file to be created\n");
	fprintf(stderr, "\t\t0  (choose error sequence ),\n");
	fprintf(stderr, "\t\t1  (choose signal estimate sequence),\n");
	fprintf(stderr, "\t\tor 2  (choose lattice filter coefficients)\n");
	fprintf(stderr, "\t\t[default = 0]\n");
	fprintf(stderr, "\n");	fprintf(stderr, " \n");
	fprintf(stderr, "\n");
	fprintf(stderr, "\t[-V] Gives the version for dmlatt\n");
	fprintf(stderr, "\t[-U] Gives the usage for dmlatt\n");
	fprintf(stderr, "\t[-P] Prompts for command line options\n");
	fprintf(stderr, "\t[-A [file1]] Creates the answer file called dmlatt.ans or file1 \n");
	fprintf(stderr, "\t[-a [file1]] Uses dmlatt.ans or file1 as the answer file \n\n\n");

}


/****************************************************************
*
* Routine Name:  gw_getargs 
*
*
* Purpose:  gets arguments off command line for dmlatt
*
* Input:  none 
*
* Output:  none 
*
* Written By:  automatically generated by ghostwriter
*
****************************************************************/


gw_get_args()
{

char	*i_pstr = 
	"\nEnter: (i) input file (xviff file) \n        {infile}: "; 
char	*o_pstr = 
	"\nEnter: (o) resulting output file (xviff file) \n        {outfile}: "; 
char	*s_pstr = 
	"\nEnter: (s) number of stages for the filter  \n        {integer, > 0 [1] }: "; 
char	*c_pstr = 
	"\nEnter: (c) specify the convergence parameter \n        {float,  0.000 to 1.000 [0.010000] }: "; 
char	*b_pstr = 
	"\nEnter: (b) specify the smoothing parameter \n        {float,  0.000 to 1.000 [0.990000] }: "; 
char	*j_pstr = 
	"\nEnter: (j) scalar processing (0) or vector processing (1) \n        {boolean, (y/n)  [yes] }: "; 
char	*d_pstr = 
	"\nEnter: (d) down vectors(0) or across bands(1) \n        {boolean, (y/n)  [yes] }: "; 
char	*g_pstr = "\nEnter: (g) type of output file to be created -- \n      \t\t0 (choose error sequence )\n\t\t1 (choose signal estimate sequence)\n\t\tor 2 (choose lattice filter coefficients)\n  [0]:  "; 
char  answer_file[512], temp[512];



int	fid = -1,
	error, query, ok, count;
	int prompt_flag = FALSE;

	/*
	 * print version if necessary
	 */
	vgparml(fid, "-V", "noprompt", &query, 0, 0, &error);
	if (error == 0)
	{
	    printf ("\n%s: %s: Version %d.%d\n\n",
	    "dmlatt", RELEASE_NAME, RELEASE_MAJOR, RELEASE_MINOR);
	    exit(1);
	}

	/*
	 * print usage if necessary
	 */
	vgparml(fid, "-U", "noprompt", &query, 0, 0, &error);
	if (error == 0)
	{
	    gw_usage();
	    exit(1);
	}

	/*
	 * see if interactive prompting is desired
	 */
	vgparml(fid, "-P", "noprompt", &query, 0, 0, &error);
	if (error == 0)
	    prompt_flag = TRUE;

	/*
	 * see if answer file is to be used
	 */
	vgparms(fid, "-a", "noprompt", answer_file, 0, 0, &error);
	if (error == 0)
	{
	     if (VStrlen(answer_file) == 0)
		sprintf(answer_file, "dmlatt.ans");
	     while (fid < 0)
	     {
		if ((fid = open(answer_file, O_RDONLY, 0666))== -1)
		{
		    fprintf(stderr, "dmlatt: can't open answer file \"%s\"",
			    answer_file); 
		    fprintf(stderr, "please re-enter: ");
		    (void) gets(answer_file);
		    if (VStrlen(answer_file) == 0)
			sprintf(answer_file, "dmlatt.ans");
		}

	     }

	}

	/*
	 * allocate the dmlatt structure
	 */
	dmlatt = (dmlatt_struct *) 
	       calloc((unsigned)1, (unsigned) sizeof (dmlatt_struct));

	/*
	 * get required arguments for dmlatt
	 */
	if (prompt_flag == TRUE)
	    fprintf(stderr,"\nRequired Arguments:\n\n");

	if (!(dmlatt->i_flag = vget_infile(fid, prompt_flag,
			"-i", i_pstr, &(dmlatt->i_file))
))
	{
	    if (prompt_flag == TRUE)
	    {
		while(!(dmlatt->i_flag))
		{
		    fprintf(stderr, "\t\ti is required, please re-enter: \n");
		    dmlatt->i_flag = vget_infile(fid, prompt_flag,
		    "-i", i_pstr, &(dmlatt->i_file))
;

		}
	    }
	    else
	    {
		fprintf(stderr, "Error: '-i' is a required argument\n");
		gw_usage();
		exit(1);
	    }
	}

	if (!(dmlatt->o_flag = vget_outfile(fid, prompt_flag,
			"-o", o_pstr, &(dmlatt->o_file))
))
	{
	    if (prompt_flag == TRUE)
	    {
		while(!(dmlatt->o_flag))
		{
		    fprintf(stderr, "\t\to is required, please re-enter: \n");
		    dmlatt->o_flag = vget_outfile(fid, prompt_flag,
		    "-o", o_pstr, &(dmlatt->o_file))
;

		}
	    }
	    else
	    {
		fprintf(stderr, "Error: '-o' is a required argument\n");
		gw_usage();
		exit(1);
	    }
	}

	/*
	 * get optional arguments for dmlatt
	 */
	if (prompt_flag == TRUE)
	   fprintf(stderr,"\nOptional Arguments:\n\n");

	dmlatt->s_flag = vget_int(fid, prompt_flag,
	"-s", s_pstr, &(dmlatt->s_int),
	1, 2, 2);

	dmlatt->c_flag = vget_float(fid, prompt_flag,
	"-c", c_pstr, &(dmlatt->c_float),
	0.010000, 0.000000, 1.000000);

	dmlatt->b_flag = vget_float(fid, prompt_flag,
	"-b", b_pstr, &(dmlatt->b_float),
	0.990000, 0.000000, 1.000000);

	dmlatt->j_flag = vget_logic(fid, prompt_flag,
	"-j", j_pstr, &(dmlatt->j_logic), 1);

	dmlatt->d_flag = vget_logic(fid, prompt_flag,
	"-d", d_pstr, &(dmlatt->d_logic), 1);


	/*
	 * get optional toggles for dmlatt
	 */
	if (prompt_flag == TRUE)
	{
	    ok = FALSE;
	    while(!ok)
	    {
		dmlatt->g_flag = vget_int(fid, prompt_flag,
		"-g", g_pstr, &(dmlatt->g_toggle),
		0, 0, 0);

		if ((dmlatt->g_toggle == 0 ) || 
		    (dmlatt->g_toggle == 1 ) || 
		    (dmlatt->g_toggle == 2 ))
		      ok = TRUE;
		else
		{
		}
	    }
	}
	else
	{
		dmlatt->g_flag = vget_int(fid, prompt_flag,
		"-g", g_pstr, &(dmlatt->g_toggle),
		0, 0, 0);

		if ((dmlatt->g_toggle != 0 ) && 
		    (dmlatt->g_toggle != 1 ) && 
		    (dmlatt->g_toggle != 2 ))
		{
		    fprintf(stderr, "\n");
		    gw_usage();
		    exit(0);
		}
	}


}



