1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use image::GenericImageView;
use pixelmatch::pixelmatch;

use super::{Error, Result};

pub async fn diff(user_image: &[u8], answer_image: &[u8]) -> Result<f32> {
    let (width, height) = image::load_from_memory(user_image)?.dimensions();
    let _buffer: Option<&mut Vec<u8>> = None;

    let diff = pixelmatch(
        user_image,
        answer_image,
        _buffer,
        Some(width),
        Some(height),
        Some(pixelmatch::Options {
            threshold: 0.1,
            ..Default::default()
        }),
    )
    .map_err(|error| Error::DiffFailed(error.to_string()))?;

    let match_percent = 1. - (diff as f32) / ((width * height) as f32);

    Ok(match_percent)
}