/** @file pmResiduals.c
 *
 * Functions to manipulate the residual tables (data - model).
 *
 * @author EAM, IfA
 * @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
 * @date $Date: 2007-11-10 01:09:20 $
 * Copyright 2004 IfA, University of Hawaii
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <pslib.h>
#include "pmResiduals.h"

static void pmResidualsFree (pmResiduals *resid) {

    if (resid == NULL) return;

    psFree (resid->Ro);
    psFree (resid->Rx);
    psFree (resid->Ry);
    psFree (resid->weight);
    psFree (resid->mask);
    return;
}

pmResiduals *pmResidualsAlloc (int xSize, int ySize, int xBin, int yBin) {

    pmResiduals *resid = (pmResiduals *) psAlloc(sizeof(pmResiduals));
    psMemSetDeallocator(resid, (psFreeFunc) pmResidualsFree);

    int nX = xSize * xBin;
    int nY = ySize * yBin;

    nX = (nX % 2) ? nX : nX + 1;
    nY = (nY % 2) ? nY : nY + 1;

    resid->Ro  = psImageAlloc (nX, nY, PS_TYPE_F32);
    resid->Rx  = psImageAlloc (nX, nY, PS_TYPE_F32);
    resid->Ry  = psImageAlloc (nX, nY, PS_TYPE_F32);
    resid->weight = psImageAlloc (nX, nY, PS_TYPE_F32);
    resid->mask   = psImageAlloc (nX, nY, PS_TYPE_U8);

    resid->xBin = xBin;
    resid->yBin = yBin;
    resid->xCenter = 0.5*(nX - 1); 
    resid->yCenter = 0.5*(nY - 1);
    return resid;
}

bool psMemCheckResiduals(psPtr ptr)
{
    PS_ASSERT_PTR(ptr, false);
    return ( psMemGetDeallocator(ptr) == (psFreeFunc) pmResidualsFree);
}

