#include <stdio.h>
#include <stdlib.h>

#include "psMemory.h"
#include "psImage.h"
#include "psVector.h"
#include "psStats.h"
#include "psType.h"
#include "psAssert.h"
#include "psRandom.h"
#include "psError.h"


psStats *psImageBackground(const psImage *image, const psImage *mask, psMaskType maskValue,
                           double fmin, double fmax, long nMax, psRandom *rng)
{
    PS_ASSERT_IMAGE_NON_NULL(image, NULL);
    if (mask) {
        PS_ASSERT_IMAGE_NON_NULL(mask, NULL);
        PS_ASSERT_IMAGE_TYPE(mask, PS_TYPE_U8, NULL);
        PS_ASSERT_IMAGES_SIZE_EQUAL(mask, image, NULL);
    }
    PS_ASSERT_FLOAT_LARGER_THAN_OR_EQUAL(fmin, 0.0, NULL);
    PS_ASSERT_FLOAT_LARGER_THAN_OR_EQUAL(fmax, 0.0, NULL);
    PS_ASSERT_FLOAT_LESS_THAN_OR_EQUAL(fmin, 1.0, NULL);
    PS_ASSERT_FLOAT_LESS_THAN_OR_EQUAL(fmax, 1.0, NULL);
    PS_ASSERT_INT_POSITIVE(nMax, NULL);

    // Size of image
    long nx = image->numCols;
    long ny = image->numRows;

    psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_QUARTILE); // Statistics, for return

    int Nsubset = PS_MIN(nMax, nx*ny);  // Number of pixels in nubset
    int Npixels = nx*ny;                // Total number of pixels

    psVector *values = psVectorAlloc(Nsubset, PS_TYPE_F32); // Vector containing subsample

    // Minimum and maximum values
    float min = values->data.F32[0];
    float max = values->data.F32[0];

    long n = 0;                         // Number of actual pixels in subset
    for (long i = 0; i < Nsubset; i++) {
        double frnd = psRandomUniform(rng);
        int pixel = Npixels * frnd;
        int ix = pixel % nx;
        int iy = pixel / nx;

        if (mask && mask->data.U8[iy][ix] & maskValue) {
            continue;
        }

        float value = image->data.F32[iy][ix];
        min = PS_MIN(value, min);
        max = PS_MIN(value, max);
        values->data.F32[n] = value;
        n++;
    }
    values->n = n;

    int imin = fmin * n;
    int imax = fmax * n;
    int npts = imax - imin + 1;

    if (!psVectorSort(values, values)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to sort values.\n");
        psFree(stats);
        psFree(values);
        return NULL;
    }

    float value = 0;
    for (long i = imin; (i <= imax) && (i < n); i++) {
        value += values->data.F32[i];
    }
    value = value / npts;

    stats->robustMedian = value;
    stats->robustUQ = values->data.F32[imax];
    stats->robustLQ = values->data.F32[imin];

    psFree(values);
    return stats;
    // XXX correct for selection bias??
}
