/* ------------------------------------------------------------------------- */
/* Filename     : wavelet.c                                                  */
/* Author       : Daniel Sage                                                */
/* Email        :                                                            */
/* Organization : EPFL/DMT/IOA/BIG                                           */
/* Date         : 15 June 1999                                               */
/* Version      :                                                            */
/* Online Help  :                                                            */
/* Demo File    :                                                            */
/* Doc File     :                                                            */
/* URL          :                                                            */
/* ------------------------------------------------------------------------- */
/* Note         : This file is automatically generated                       */
/* Sat 02 Sep 2000 17:20:07 GMT-07:00                                        */
/* ------------------------------------------------------------------------- */


#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "configs.h"
#include "messagedisplay.h"
#include "debug.h"
#include "error.h"
#include "wavelet.h"
#include "wavelettools.h"


/* ------------------------------------------------------------------------- */
/* Declaration of static procedures                                          */
/* ------------------------------------------------------------------------- */
/* None */

/* ------------------------------------------------------------------------- */
/* Definition of extern procedures                                           */
/* ------------------------------------------------------------------------- */


/* ------------------------------------------------------------------------- */
/* 	Function:	WaveletCheck                                                   */
/* ------------------------------------------------------------------------- */
extern int WaveletCheck( struct TWaveletStruct *Data)
{

	if (Data == (struct TWaveletStruct *)NULL) {
		WRITE_ERROR( WaveletCheck, "Invalid data pointer");
		return(ERROR);
	}

	if (Data->Input == (double *)NULL) {
		WRITE_ERROR( WaveletCheck, "Input is null pointer");
		return(ERROR);
	}
	
	if (Data->NxInput <= 1L) {
		WRITE_ERROR( WaveletCheck, "Input size should be greater than 1");
		return(ERROR);
	}
	
	if ((Data->NxInput/2L)*2L != Data->NxInput) {  /* The input length should be even */
		WRITE_ERROR( WaveletCheck, "Input size should be even");
		return(ERROR);
	}

	if (Data->NyInput > 1L) 
	if ((Data->NyInput/2L)*2L != Data->NyInput) {  /* The input length should be even */
		WRITE_ERROR( WaveletCheck, "Input size should be even");
		return(ERROR);
	}
	
	if (Data->NzInput > 1L) 
	if ((Data->NzInput/2L)*2L != Data->NzInput) {  /* The input length should be even */
		WRITE_ERROR( WaveletCheck, "Input size should be even");
		return(ERROR);
	}
	if (Data->Output == (double *)NULL) {
		WRITE_ERROR( WaveletCheck, "Output is null pointer");
		return(ERROR);
	}
	
	if ( (strcmp(Data->Operation, "Analysis")) &&
		 (strcmp(Data->Operation, "Synthesis")))  {
		WRITE_ERROR( WaveletCheck, "The Operation paramater is unknown.");
		return(ERROR);
	}

	if ( (strcmp(Data->Filter, "B-Spline")) &&
		 (strcmp(Data->Filter, "Cardinal Spline")) &&
		 (strcmp(Data->Filter, "Dual Spline")) &&
		 (strcmp(Data->Filter, "Orthogonal Spline")) &&
		 (strcmp(Data->Filter, "Orthonormal Spline")) &&
		 (strcmp(Data->Filter, "Fractional Orthonormal Spline")))  {
		WRITE_ERROR( WaveletCheck, "The Filter paramater is unknown.");
		return(ERROR);
	}
	
	if ( (strcmp(Data->BoundaryConditions, "Periodic")) &&
		 (strcmp(Data->BoundaryConditions, "Mirror")))  {
		WRITE_ERROR( WaveletCheck,"The BoundaryConditions parameter is unknown.");
		return(ERROR);
	}
	
	if ( (strcmp(Data->Order, "0")) &&
		 (strcmp(Data->Order, "1")) &&
		 (strcmp(Data->Order, "3")) &&
		 (strcmp(Data->Order, "5")))  {
		WRITE_ERROR( WaveletCheck, "The Order parameter is unknown.");
		return(ERROR);
	}

	return(!ERROR);
	return(!ERROR);

}

/* ------------------------------------------------------------------------- */
/* 	Function:	WaveletSize                                                    */
/* ------------------------------------------------------------------------- */
extern int WaveletSize( struct TWaveletStruct *Data)
{

	if (Data == (struct TWaveletStruct *)NULL) {
		WRITE_ERROR( WaveletSize, "Invalid data pointer");
		return(ERROR);
	}

	Data->NxOutput  = Data->NxInput;
	Data->NyOutput  = Data->NyInput;
	Data->NzOutput  = Data->NzInput;
	return(!ERROR);

}

/* ------------------------------------------------------------------------- */
/* 	Function:	Wavelet                                                        */
/* ------------------------------------------------------------------------- */
extern int  Wavelet( struct TWaveletStruct *Data)
{
int  	Status = !ERROR;
short	Order, Filter, BoundaryConditions, Operation;

	if (Data == (struct TWaveletStruct *)NULL) {
		WRITE_ERROR( Wavelet, "Invalid data pointer");
		return(ERROR);
	}

	/* Convert the Order from string to short format */
	Order = -1;
	if (!strcmp(Data->Order, "0")) Order = 0;
	if (!strcmp(Data->Order, "1")) Order = 1;
	if (!strcmp(Data->Order, "3")) Order = 3;
	if (!strcmp(Data->Order, "5")) Order = 5;
	if (Order == -1) {
		MessageDisplay("ERROR - Unexpected Order"); 
		return( ERROR);
	}

	/* Convert the Filter form string to short format */
	Filter = -1;
	if (!strcmp(Data->Filter, "B-Spline")) 			Filter = 1;
	if (!strcmp(Data->Filter, "Cardinal Spline")) 	Filter = 2;
	if (!strcmp(Data->Filter, "Dual Spline")) 		Filter = 3;
	if (!strcmp(Data->Filter, "Orthogonal Spline")) Filter = 4;
	if (!strcmp(Data->Filter, "Orthonormal Spline")) Filter = 6;
	if (!strcmp(Data->Filter, "Fractional Orthonormal Spline")) Filter = 5;
	if (Filter == -1) {
		MessageDisplay("ERROR - Unexpected Filter"); 
		return( ERROR);
	}

	/* Convert the BoundaryConditions form string to short format */
	BoundaryConditions = -1;
	if (!strcmp(Data->BoundaryConditions, "Mirror")) 	BoundaryConditions = 1;
	if (!strcmp(Data->BoundaryConditions, "Periodic")) 	BoundaryConditions = 2;
	if (BoundaryConditions == -1) {
		MessageDisplay("ERROR - Unexpected BoundaryConditions"); 
		return( ERROR);
	}
	
	/* Convert the Operation form string to short format */
	Operation = -1;
	if (!strcmp(Data->Operation, "Analysis")) 	Operation = 1;
	if (!strcmp(Data->Operation, "Synthesis")) 	Operation = 2;
	if (Operation == -1) {
		MessageDisplay("ERROR - Unexpected Operation"); 
		return( ERROR);
	}
	
	/* Call the wavelet operation */
	if (Operation == 1) {
		WaveletSplit_3D(	Data->Input, 
							Data->Output,
							Data->NxInput, Data->NyInput, Data->NzInput,
							Filter,
							Order,
							Data->Alpha,
							BoundaryConditions,
							&Status);
		if ( Status == ERROR) {					
			MessageDisplay("ERROR - In Wavelet analysis"); 
			return( ERROR);
		}
	}
							
	if (Operation == 2) {
		WaveletMerge_3D(	Data->Input, 
							Data->Output,
							Data->NxInput, Data->NyInput, Data->NzInput,
							Filter,
							Order,
							Data->Alpha,
							BoundaryConditions,
							&Status);
		if ( Status == ERROR) {					
			MessageDisplay("ERROR - In Wavelet synthesis"); 
			return( ERROR);
		}
	}

	return(!ERROR);


}

/* ------------------------------------------------------------------------- */
/* Definition of static procedures                                           */
/* ------------------------------------------------------------------------- */
/* None */

