#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "test_common.h"
#include "stb_image.h"
#include "stb_image_write.h"
#include <math.h>
#include <sys/time.h>

uint8_t *load_image(const char *path, int *width, int *height, int *channels) {
    return stbi_load(path, width, height, channels, 4);
}

int save_image(const char *path, uint8_t *data, int width, int height, int channels) {
    return stbi_write_png(path, width, height, channels, data, width * channels);
}

uint8_t *downsample_image(uint8_t *src, int sw, int sh, int sc, int *dw, int *dh, float scale) {
    if (!src || sw <= 0 || sh <= 0 || sc <= 0 || scale <= 0) {
        if (dw) *dw = 0;
        if (dh) *dh = 0;
        return NULL;
    }

    int tw = (int)(sw * scale);
    int th = (int)(sh * scale);

    if (tw < 1) tw = 1;
    if (th < 1) th = 1;

    uint8_t *dst = malloc(tw * th * sc);
    if (!dst) return NULL;

    float inv_scale = 1.0f / scale;
    for (int y = 0; y < th; y++) {
        for (int x = 0; x < tw; x++) {
            float sx = (x + 0.5f) * inv_scale;
            float sy = (y + 0.5f) * inv_scale;

            int ix = (int)sx;
            int iy = (int)sy;

            if (sw > 1) {
                ix = (ix < sw - 2) ? ix : sw - 2;
            } else {
                ix = 0;
            }
            if (sh > 1) {
                iy = (iy < sh - 2) ? iy : sh - 2;
            } else {
                iy = 0;
            }

            if (ix < 0) ix = 0;
            if (iy < 0) iy = 0;

            float fx = sx - ix;
            float fy = sy - iy;

            if (sw == 1) fx = 0.0f;
            if (sh == 1) fy = 0.0f;

            for (int c = 0; c < sc; c++) {
                uint8_t p00 = src[(iy * sw + ix) * sc + c];
                uint8_t p01 = (sw > 1) ? src[(iy * sw + ix + 1) * sc + c] : p00;
                uint8_t p10 = (sh > 1) ? src[((iy + 1) * sw + ix) * sc + c] : p00;
                uint8_t p11 = (sw > 1 && sh > 1) ? src[((iy + 1) * sw + ix + 1) * sc + c] : p00;

                float interp = p00 * (1 - fx) * (1 - fy) +
                              p01 * fx * (1 - fy) +
                              p10 * (1 - fx) * fy +
                              p11 * fx * fy;

                dst[(y * tw + x) * sc + c] = (uint8_t)(interp + 0.5f);
            }
        }
    }

    *dw = tw;
    *dh = th;
    return dst;
}

double get_time_ms(void) {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
}

double run_benchmark(double (*fn)(void), int iterations) {
    double warmup = fn();
    (void)warmup;

    double total = 0.0;
    for (int i = 0; i < iterations; i++) {
        total += fn();
    }
    return total / iterations;
}

int compare_images(uint8_t *a, uint8_t *b, int w, int h, int ch, float threshold) {
    int max_diff = 0;
    for (int i = 0; i < w * h * ch; i++) {
        int diff = abs((int)a[i] - (int)b[i]);
        if (diff > max_diff) max_diff = diff;
        if (diff > (int)(threshold * 255.0f)) {
            return 0;
        }
    }
    return 1;
}

float compute_psnr(uint8_t *a, uint8_t *b, int w, int h, int ch) {
    double mse = 0.0;
    int total = w * h * ch;

    for (int i = 0; i < total; i++) {
        double diff = (double)a[i] - (double)b[i];
        mse += diff * diff;
    }

    mse /= total;

    if (mse < 1e-10) return 99.99f;

    return (float)(10.0 * log10(255.0 * 255.0 / mse));
}

uint8_t *create_gradient_image(int w, int h) {
    uint8_t *img = malloc(w * h * 4);
    if (!img) return NULL;

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int idx = (y * w + x) * 4;
            img[idx + 0] = (uint8_t)((float)x / w * 255);
            img[idx + 1] = (uint8_t)((float)y / h * 255);
            img[idx + 2] = 128;
            img[idx + 3] = 255;
        }
    }
    return img;
}

uint8_t *create_noise_image(int w, int h, unsigned int seed) {
    uint8_t *img = malloc(w * h * 4);
    if (!img) return NULL;

    srand(seed);
    for (int i = 0; i < w * h * 4; i++) {
        img[i] = (uint8_t)(rand() % 256);
    }
    return img;
}

uint8_t *create_uniform_image(int w, int h, uint8_t r, uint8_t g, uint8_t b) {
    uint8_t *img = malloc(w * h * 4);
    if (!img) return NULL;

    for (int i = 0; i < w * h; i++) {
        img[i * 4 + 0] = r;
        img[i * 4 + 1] = g;
        img[i * 4 + 2] = b;
        img[i * 4 + 3] = 255;
    }
    return img;
}

uint8_t *create_checkerboard(int w, int h, int check_size) {
    uint8_t *img = malloc(w * h * 4);
    if (!img) return NULL;

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int idx = (y * w + x) * 4;
            int cx = x / check_size;
            int cy = y / check_size;
            if ((cx + cy) % 2 == 0) {
                img[idx + 0] = 255;
                img[idx + 1] = 255;
                img[idx + 2] = 255;
            } else {
                img[idx + 0] = 0;
                img[idx + 1] = 0;
                img[idx + 2] = 0;
            }
            img[idx + 3] = 255;
        }
    }
    return img;
}
