#include <stdlib.h>
#include "stack-c.h"
#include "api_scilab.h"
#include "Scierror.h"
#include "sciprint.h"

#include "FloatRaster.h"
#include "Erreur.h"

/********************************************
 * SCILAB function : CreateFloatRaster      *
 ********************************************/

int gtw_CreateFloatRaster ( char * fname )
{
	char * Nom;
	
	int m, n, l;
	int un = 1;
    int nrows,ncols,LL,HandleRaster;
    double cellsize,xll,yll;
	float NODATA_value;

	CheckRhs(8,8);
	CheckLhs(1,1);

	/* Variable Nom */

	GetRhsVar(1,"c",&m,&n,&l);
	Nom = cstk(l);

	/* Variable ncols */

	GetRhsVar(2,"i",&m,&n,&l);
	CheckScalar(2,m,n);
	ncols = *istk(l);

	/* Variable nrows */

	GetRhsVar(3,"i",&m,&n,&l);
	CheckScalar(3,m,n);
	nrows = *istk(l);

	/* Variable cellsize */

	GetRhsVar(4,"d",&m,&n,&l);
	CheckScalar(4,m,n);
	cellsize = *stk(l);

	/* Variable xll */

	GetRhsVar(5,"d",&m,&n,&l);
	CheckScalar(5,m,n);
	xll = *stk(l);

	/* Variable yll */

	GetRhsVar(6,"d",&m,&n,&l);
	CheckScalar(6,m,n);
	yll = *stk(l);

	/* Variable NODATA_value */

	GetRhsVar(7,"r",&m,&n,&l);
	CheckScalar(7,m,n);
	NODATA_value = *sstk(l);

	/* Variable LL */

	GetRhsVar(8,"i",&m,&n,&l);
	CheckScalar(8,m,n);
	LL = *istk(l);

	/* Appel � l'API */

	HandleRaster = CreateFloatRaster ( 	Nom,
						ncols,
						nrows,
						cellsize,
						xll,
						yll,
						NODATA_value,
						LL
					);

	if ( HandleRaster < 0 ) Scierror (999, GetErreur());
	
	/* Variable HandleModele */
	
	CreateVar(Rhs + 1,"i",&un,&un,&l);
	*istk(l) = HandleRaster ;
	LhsVar(1) = Rhs +1 ;

	return(0);
}

/********************************************
 * SCILAB function : DetruireFloatRaster    *
 ********************************************/

int gtw_DestroyFloatRaster ( char * fname )
{
	int HandleRaster;
	int m, n, l;

	CheckRhs(1,1);
	CheckLhs(0,1);

	/* Variable HandleRaster */

	GetRhsVar(1,"i",&m,&n,&l);
	CheckScalar(1, m, n);
	HandleRaster = *istk(l);

	if (DestroyFloatRaster(HandleRaster)) Scierror(999,GetErreur());

	return(0);
}

/********************************************************
 * SCILAB function : CreateFloatRasterFromBinaryFile    *
 ********************************************************/

int gtw_CreateFloatRasterFromBinaryFile (   char * fname )
{
    	char * Nom , * binfile , * hdrfile ;

	int m, n, l;
	int un = 1;
        int TYPE_SRC,HandleRaster;

	CheckRhs(4,4);
	CheckLhs(1,1);

	/* Variable Nom */

	GetRhsVar(1,"c",&m,&n,&l);
	Nom = cstk(l);

    /* Variable binfile */

    GetRhsVar(2,"c",&m,&n,&l);
	binfile = cstk(l);

    /* Variable hdrfile */

    GetRhsVar(3,"c",&m,&n,&l);
	hdrfile = cstk(l);

    /* Variable TYPE_SRC */

    GetRhsVar(4,"i",&m,&n,&l);
    CheckScalar(4,m,n);
    TYPE_SRC = *istk(l);

    /* Appel � l'API */

	HandleRaster = CreateFloatRasterFromBinaryFile ( 	Nom,
                                                        binfile,
                                                        hdrfile,
                                                        TYPE_SRC
                                                    );

	if ( HandleRaster < 0 ) Scierror (999, GetErreur());

	/* Variable HandleModele */

	CreateVar(Rhs + 1,"i",&un,&un,&l);
	*istk(l) = HandleRaster ;
	LhsVar(1) = Rhs +1 ;

	return(0);
}

/*******************************************
 * SCILAB function : GetFloatRasterHeader  *
 *******************************************/

int gtw_GetFloatRasterHeader ( char * fname )
{
	int HandleRaster;
	int m, n, l;
    int ncols,nrows,LL;
        double cellsize,xll,yll;
		float NODATA_value;
        int headerlength=7;
        int un=1;

        double * Header;

	CheckRhs(1,1);
	CheckLhs(1,1);

	/* Variable HandleRaster */

	GetRhsVar(1,"i",&m,&n,&l);
	CheckScalar(1, m, n);
	HandleRaster = *istk(l);

        if ( GetFloatRasterHeader ( HandleRaster ,
                                    & ncols ,
                                    & nrows ,
                                    & cellsize ,
                                    & xll ,
                                    & yll ,
                                    & NODATA_value ,
                                    & LL ) )
            Scierror(999,GetErreur());

		Header = (double *)malloc(headerlength*sizeof(double));	
			
        Header[0] = (double)ncols;
        Header[1] = (double)nrows;
        Header[2] = cellsize;
        Header[3] = xll;
        Header[4] = yll;
        Header[5] = (double)NODATA_value;
        Header[6] = (double)LL;

        CreateVarFromPtr ( Rhs + 1 , "d", & headerlength, & un, & Header );
		LhsVar(1) = Rhs + 1;

		free(Header);
	
        return(0);
}

int gtw_GetFloatRasterData ( char * fname )
{
	int HandleRaster;
	int m, n, l;
        int ncols,nrows,LL;
        double cellsize,xll,yll;
		float NODATA_value;
        int headerlength=7;
        int un=1;

        double * Header;
        float * Data;

	CheckRhs(1,1);
//	CheckLhs(2,2);
        CheckLhs(1,1);

	/* Variable HandleRaster */

	GetRhsVar(1,"i",&m,&n,&l);
	CheckScalar(1, m, n);
	HandleRaster = *istk(l);

        if ( GetFloatRasterHeader ( HandleRaster ,
                                    & ncols ,
                                    & nrows ,
                                    & cellsize ,
                                    & xll ,
                                    & yll ,
                                    & NODATA_value ,
                                    & LL ) )
            Scierror(999,GetErreur());

		Header = (double *)malloc(headerlength*sizeof(double));	
			
        Header[0] = (double)ncols;
        Header[1] = (double)nrows;
        Header[2] = cellsize;
        Header[3] = xll;
        Header[4] = yll;
        Header[5] = (double)NODATA_value;
        Header[6] = (double)LL;

        /* Dimensionnement du tableau contenant les données */

        if( !(Data = (float *) malloc(nrows*ncols*sizeof(float))) )
            Scierror(999,"%s : echec de l allocation du bloc de donnees\n",fname);

        if ( GetFloatRasterData ( HandleRaster , & Data ) )
                    Scierror(999,GetErreur());

        CreateVarFromPtr ( Rhs + 1 , "r", & nrows, & ncols, & Data );
	LhsVar(1) = Rhs + 1;

/*        CreateVarFromPtr ( Rhs + 2 , "d", & headerlength, & un, & Header );
	LhsVar(2) = Rhs + 2;*/

        free(Data);
		free(Header);

        return(0);
}

/********************************************
 * SCILAB function : SetRasterToNODATA      *
 ********************************************/

int gtw_SetRasterToNODATA ( char * fname )
{
	int HandleRaster;
	int m, n, l;

        CheckRhs(1,1);
        CheckLhs(0,1);

	/* Variable HandleRaster */

	GetRhsVar(1,"i",&m,&n,&l);
	CheckScalar(1, m, n);
	HandleRaster = *istk(l);

        /* Appel à l'API */

        if ( SetRasterToNODATA ( HandleRaster ) )
            Scierror(999,GetErreur());

        return(0);
}

/********************************************
 * SCILAB function : SetRasterToNODATA      *
 ********************************************/

int gtw_SetRasterToConstant ( char * fname )
{
	int HandleRaster;
        float constant;
	int m, n, l;

        CheckRhs(2,2);
        CheckLhs(0,1);

	/* Variable HandleRaster */

	GetRhsVar(1,"i",&m,&n,&l);
	CheckScalar(1, m, n);
	HandleRaster = *istk(l);

        /* Variable constant */

	GetRhsVar(2,"r",&m,&n,&l);
	CheckScalar(2, m, n);
	constant = *sstk(l);

        /* Appel à l'API */

        if ( SetRasterToConstant ( HandleRaster , constant ) )
            Scierror(999,GetErreur());

        return(0);
}

/********************************************************
 * SCILAB function : WriteRasterToFile                  *
 ********************************************************/

int gtw_WriteRasterToFile (   char * fname )
{
    	char * binfile;
	int m, n, l;
        int TYPE_DST,HandleRaster,SWITCH_ENDIANNESS;

	CheckRhs(4,4);
	CheckLhs(0,1);

	/* Variable HandleRaster */

	GetRhsVar(1,"i",&m,&n,&l);
	HandleRaster = *istk(l);

        /* Variable binfile */

        GetRhsVar(2,"c",&m,&n,&l);
	binfile = cstk(l);

        /* Variable TYPE_DST */

        GetRhsVar(3,"i",&m,&n,&l);
        CheckScalar(3,m,n);
        TYPE_DST = *istk(l);

        /* Variable SWITCH_ENDIANNESS */

        GetRhsVar(4,"i",&m,&n,&l);
        CheckScalar(4,m,n);
        SWITCH_ENDIANNESS = *istk(l);

        /* Appel a l'API */

        if ( WriteFloatRasterToFile ( HandleRaster, binfile, TYPE_DST, SWITCH_ENDIANNESS) )
            Scierror(999,GetErreur());

	return(0);
}

/********************************************************
 * SCILAB function : sci_CropRaster                     *
 ********************************************************/

int gtw_CropRaster (   char * fname )
{
	char * Nom ;
    int m, n, l;
	int un = 1;
	int startrow, startcol, endrow, endcol ;
    int HandleRaster,HandleCroppedRaster ;

	CheckRhs(6,6);
	CheckLhs(0,1);

	/* Variable HandleRaster */

	GetRhsVar(1,"i",&m,&n,&l);
	HandleRaster = *istk(l);

    /* Variable startrow */

    GetRhsVar(2,"i",&m,&n,&l);
	CheckScalar(2,m,n);
	startrow = *istk(l);

    /* Variable startcol */

    GetRhsVar(3,"i",&m,&n,&l);
	CheckScalar(3,m,n);
	startcol = *istk(l);

    /* Variable endrow */

    GetRhsVar(4,"i",&m,&n,&l);
	CheckScalar(4,m,n);
	endrow = *istk(l);

    /* Variable endcol */

    GetRhsVar(5,"i",&m,&n,&l);
	CheckScalar(5,m,n);
	endcol = *istk(l);
	
	/* Variable Nom */

	GetRhsVar(6,"c",&m,&n,&l);
	Nom = cstk(l);
	
    /* Appel a l'API */

	HandleCroppedRaster = CropRaster ( HandleRaster , startrow , startcol , endrow , endcol , Nom ) ;
	
    if ( HandleCroppedRaster < 0 )
		Scierror(999,GetErreur());

	/* Variable HandleCroppedRaster */
	
	CreateVar(Rhs + 1,"i",&un,&un,&l);
	*istk(l) = HandleCroppedRaster ;
	LhsVar(1) = Rhs +1 ;

	return(0);
}
