/* @file pmSubtraction.h
 *
 * PSF-matched image subtraction, based on the Alard & Lupton (1998) and Alard (2000) methods.
 *
 * @author Paul Price, IfA
 * @author GLG, MHPCC
 *
 * @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
 * @date $Date: 2007-05-11 02:21:01 $
 * Copyright 2004-207 Institute for Astronomy, University of Hawaii
 */

#ifndef PM_SUBTRACTION_H
#define PM_SUBTRACTION_H

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

/// @addtogroup imcombine Image Combinations
/// @{

/// Calculate the least-squares equation to match the image quality
bool pmSubtractionCalculateEquation(psArray *stamps, ///< The stamps for which to calculate the equation,
                                    const psImage *reference, ///< Reference image
                                    const psImage *input, ///< Input image
                                    const psImage *weight, ///< Weight image, or NULL
                                    const pmSubtractionKernels *kernels, ///< Kernel parameters
                                    int footprint ///< Half-size of region over which to calculate equation
                                    );

/// Solve the least-squares equation to match the image quality
psVector *pmSubtractionSolveEquation(psVector *solution, ///< Solution vector, or NULL
                                     const psArray *stamps ///< Array of stamps
                                     );

/// Reject stamps
int pmSubtractionRejectStamps(psArray *stamps, ///< Array of stamps to check for rejection
                              const psImage *refImage, ///< Reference image
                              psImage *inImage, ///< Input image
                              psImage *mask, ///< Mask image
                              psMaskType badStampMaskVal, ///< Value to use in mask for bad stamp
                              const psVector *solution, ///< Solution vector
                              int footprint, ///< Region to mask if stamp is bad
                              float sigmaRej, ///< Number of RMS deviations above zero at which to reject
                              const pmSubtractionKernels *kernels ///< Kernel parameters
    );

/// Generate an image of the convolution kernel
psImage *pmSubtractionKernelImage(const psVector *solution, ///< Solution vector
                                  const pmSubtractionKernels *kernels, ///< Kernel parameters
                                  float x, float y ///< Normalised position [-1,1] for which to generate image
                                  );

/// Convolve image in preparation for subtraction
bool pmSubtractionConvolve(psImage **outImage, ///< Output image
                           psImage **outWeight, ///< Output weight map (or NULL)
                           psImage **outMask, ///< Output mask (or NULL)
                           const psImage *inImage, ///< Input image
                           const psImage *inWeight, ///< Input weight map (or NULL)
                           const psImage *inMask, ///< Input mask (or NULL)
                           psMaskType maskVal, ///< Value to mask
                           psMaskType blank, ///< Mask value for blank regions
                           const psVector *solution, ///< The solution vector
                           const pmSubtractionKernels *kernels ///< Kernel parameters
    );

/// @}
#endif
