use blurhash_update::{Components, ImageBounds};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use image::EncodableLayout;

pub fn criterion_benchmark(c: &mut Criterion) {
    let mut group = c.benchmark_group("blurhash");

    let inputs = [
        "data/19dd1c444d1c7939.png",
        "data/f73d2ee39133d871.jpg",
        "data/shenzi.png",
    ];

    for input in inputs {
        group.bench_with_input(BenchmarkId::from_parameter(input), input, |b, i| {
            let img = image::open(i).unwrap();
            let width = img.width();
            let height = img.height();
            let rgba = img.to_rgba8();
            let bytes = rgba.as_bytes();

            b.iter(|| {
                let _bhash =
                    black_box(blurhash::encode(4, 3, width, height, black_box(bytes)).unwrap());
            });
        });
    }

    group.finish();

    let mut group = c.benchmark_group("blurhash-update");

    let inputs = [
        "data/19dd1c444d1c7939.png",
        "data/f73d2ee39133d871.jpg",
        "data/shenzi.png",
    ];

    for input in inputs {
        group.bench_with_input(BenchmarkId::from_parameter(input), input, |b, i| {
            let img = image::open(i).unwrap();
            let width = img.width();
            let height = img.height();
            let rgba = img.to_rgba8();
            let bytes = rgba.as_bytes();

            b.iter(|| {
                let _bhash = black_box(
                    blurhash_update::encode(
                        Components { x: 4, y: 3 },
                        ImageBounds { width, height },
                        bytes,
                    )
                    .unwrap(),
                );
            });
        });
    }

    group.finish();
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
