/*****************************************************************************
    This routine must ensure that the psImagePixelInterpolation() routine
    works correctly.
 *****************************************************************************/
#include <stdio.h>
#include "pslib.h"
#include "psTest.h"
#include "psMemory.h"
#include "psImage.h"
#include "psImageStats.h"
#include <float.h>
#include <math.h>
#define IMAGE_SIZE 10

psS32 main()
{
    psImage *tmpImage   = NULL;
    psS32 testStatus      = true;
    psS32 memLeaks        = 0;
    psS32 i               = 0;
    psS32 j               = 0;
    float pixel         = 0.0;
    float x             = 0.0;
    float y             = 0.0;
    psS32 currentId       = psMemGetId();

    /*************************************************************************/
    /*  Allocate and initialize data structures                      */
    /*************************************************************************/
    printPositiveTestHeader(stdout,
                            "psImageStats functions",
                            "psImagePixelInterpolation()");

    tmpImage = psImageAlloc(IMAGE_SIZE, IMAGE_SIZE, PS_TYPE_F32);
    for (i=0;i<IMAGE_SIZE;i++) {
        for (j=0;j<IMAGE_SIZE;j++) {
            tmpImage->data.F32[i][j] = 4.0;
            tmpImage->data.F32[i][j] = (float) (i + j) + (4.0 * (float) i);
            tmpImage->data.F32[i][j] = (float) (i + j);
            printf("%.1f ", tmpImage->data.F32[i][j]);
        }
        printf("\n");
    }
    for (i=0;i<IMAGE_SIZE-1;i++) {
        for (j=0;j<IMAGE_SIZE-1;j++) {
            x = 0.2 + (float) i;
            y = 0.2 + (float) j;
            pixel = psImagePixelInterpolate(tmpImage,
                                            x, y,
                                            NULL, 0,
                                            0,
                                            PS_INTERPOLATE_BILINEAR);
            printf("%.1f ", pixel);
        }
        printf("\n");
    }

    for (i=0;i<IMAGE_SIZE-1;i++) {
        for (j=0;j<IMAGE_SIZE-1;j++) {
            x = 0.2 + (float) i;
            y = 0.2 + (float) j;
            pixel = psImagePixelInterpolate(tmpImage,
                                            x, y,
                                            NULL,0,
                                            0,
                                            PS_INTERPOLATE_BILINEAR);
            printf("image[%.1f][%.1f] is interpolated at %.1f\n", x, y, pixel);
        }
    }


    psFree(tmpImage);
    psMemCheckCorruption(1);
    printFooter(stdout,
                "psImageStats functions",
                "psImagePixelInterpolation()",
                testStatus);

    /*************************************************************************/
    /*  Deallocate data structures                                   */
    /*************************************************************************/
    // GUS: replace with psFree()?
    //    psImageFree(tmpImage);

    psMemCheckCorruption(1);
    memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    if (0 != memLeaks) {
        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    }

    return (!testStatus);
}
