// Copyright (C) 2006  Paul A. Price (price@ifa.hawaii.edu)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

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

#include <stdio.h>
#include "pslib.h"
#include "pois.h"

#define MAXLINE 128                     // Maximum length of a line in the stamps file


psArray *poisReadStamps(psArray *stamps,// Stamps
                        FILE **stampFP, // File pointer for stamps file
                        const psImage *image, // Image for which to find stamps
                        const psImage *mask, // Mask image
                        const poisConfig *config // Configuration
    )
{
    FILE *fp = *stampFP;                // File pointer

    if (stamps == NULL) {
        stamps = psArrayAlloc(config->nsx);
        // Initialise
        for (int i = 0; i < config->nsx; i++) {
            stamps->data[i] = poisStampAlloc();
        }
    }

    for (int i = 0; i < stamps->n; i++) {
        poisStamp *stamp = stamps->data[i];

        if (stamp->status == POIS_STAMP_RESET) {
            if (fp) {
                float x, y;             // Position of stamp
                int xPixel, yPixel;     // Integer position
                int result = 0;         // Result of fscanf
                char line[MAXLINE];     // The line from the stamps file
                char *lineBack;         // The line back from fgets
                do {
                    lineBack = fgets(line, MAXLINE, fp);
                    result = sscanf(line, "%f %f", &x, &y);

                    // Add 0.5 for rounding, and subtract 1.0 for unit-offset to zero-offset
                    xPixel = (int)(x - 0.5);
                    yPixel = (int)(y - 0.5);
                } while (result == 2 && lineBack && ! poisCheckStamp(image, mask, xPixel, yPixel, config));
                if (result == 2 && lineBack) {
                    stamp->x = xPixel;
                    stamp->y = yPixel;
                    stamp->status = POIS_STAMP_RECALC;
                    psTrace("pois.readStamps", 7, "Stamp %d: %d,%d\n", i, stamp->x, stamp->y);
                } else {
                    // We've run out of stamps in the file --- probably got EOF
                    stamp->status = POIS_STAMP_BAD;
                }
            } else {
                // Stamp file isn't open!
                stamp->status = POIS_STAMP_BAD;
            }
        }
    }

    return stamps;
}

