/* @file psImageCovariance.h
 *
 * @brief Calculations involving image covariance
 *
 * @author Paul Price, IfA
 *
 * @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
 * @date $Date: 2009-02-13 21:47:42 $
 * Copyright 2009 Institute for Astronomy, University of Hawaii
 */

#ifndef PS_IMAGE_COVARIANCE_H
#define PS_IMAGE_COVARIANCE_H

/// @addtogroup ImageOps Image Operations
/// @{

#include <psImageConvolve.h>

// We don't carry the entire covariance matrix for an image (the size goes as N^2, for N pixels, which makes
// storage difficult; and if that's not enough, the time to do the calculation is definitely impractical).
// Since there are (generally) lots of zeros in the covariance matrix, and the same basic pattern repeats (for
// background pixels), we can just carry that pattern ("covariance pseudo-matrix").  We carry this in a
// psKernel, since the values are the covariance between the pixel of consideration (at 0,0 in the kernel) and
// neighbouring pixels.  Note that this may not be strictly correct near sources, but this is the best we can
// do (and much better than most currently do).

/// Allocate a covariance pseudo-matrix with no covariance
psKernel *psImageCovarianceNone(void);

/// Calculate the covariance pseudo-matrix for a convolution kernel
psKernel *psImageCovarianceCalculate(
    const psKernel *kernel,             ///< Convolution kernel
    const psKernel *covariance          ///< Current covariance pseudo-matrix
    );

/// Return the pixel-to-pixel covariance factor
float psImageCovarianceFactor(
    const psKernel *covariance          ///< Covariance pseudo-matrix
    );

/// Sum multiple covariance pseudo-matrices
psKernel *psImageCovarianceSum(
    const psArray *array                ///< Array of covariance pseudo-matrices
    );

/// Average multiple covariance pseudo-matrices
psKernel *psImageCovarianceAverage(
    const psArray *array                ///< Array of covariance pseudo-matrices
    );

/// Truncate covariance pseudo-matrix
///
/// The covariance pseudo-matrix is truncated by removing the outer regions that contribute less than the
/// nominated fraction of the total.
psKernel *psImageCovarianceTruncate(
    const psKernel *covar,              ///< Covariance pseudo-matrix
    float frac                          ///< Fraction of covariance to truncate
    );

/// Transfer covariance factor from covariance pseudo-matrix to variance map
bool psImageCovarianceTransfer(
    psImage *variance,                  ///< Variance map to which to transfer
    psKernel *covar                     ///< Covariance pseudo-matrix from which to transfer
    );


/// @}
#endif // #ifndef PS_IMAGE_COVARIANCE_H
