// Aplica cada filtro a una copia de la imagen, la dibuja y guarda los resultados en build/.
#include <stdio.h>

#include "filters.h"

static int show_and_save(const char *title, const struct image *img, const char *name) {
    printf("%s\n", title);
    image_show(img);
    if (!image_save(img, name)) {
        fprintf(stderr, "No se pudo escribir %s\n", name);
        return 0;
    }
    return 1;
}

int main(void) {
    struct image img;
    if (!image_load(&img, "lab/images/sun.pgm")) {
        fprintf(stderr, "No se pudo leer lab/images/sun.pgm\n");
        return 1;
    }

    int min = 0;
    int max = 0;
    double mean = 0;
    image_stats(&img, &min, &max, &mean);
    printf("mínimo %d, máximo %d, promedio %.2f\n", min, max, mean);

    int ok = show_and_save("Original:", &img, "build/sun_original.pgm");

    // Los filtros cambian la imagen que reciben: cada uno trabaja sobre su propia copia.
    struct image copy = img;
    invert(&copy);
    ok = show_and_save("invert:", &copy, "build/sun_invert.pgm") && ok;

    copy = img;
    threshold(&copy, 100);
    ok = show_and_save("threshold 100:", &copy, "build/sun_threshold.pgm") && ok;

    copy = img;
    flip_horizontal(&copy);
    ok = show_and_save("flip_horizontal:", &copy, "build/sun_flip.pgm") && ok;

    return ok ? 0 : 1;
}
