Skip to main content

blazediff_ssim/
error.rs

1//! What a metric refuses to do, and why.
2
3/// Why a metric could not produce a score.
4///
5/// The messages are the ones blazediff's front-ends have always printed, so a
6/// caller wrapping this in its own error type can forward them verbatim.
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub enum SsimError {
9    /// The two inputs are not the same size.
10    SizeMismatch {
11        img1_width: u32,
12        img1_height: u32,
13        img2_width: u32,
14        img2_height: u32,
15    },
16    /// The input is smaller than the chosen metric can work on.
17    InputTooSmall {
18        width: u32,
19        height: u32,
20        minimum: u32,
21    },
22    /// The metric was configured with values it cannot honour.
23    Options(String),
24}
25
26impl std::fmt::Display for SsimError {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        match self {
29            SsimError::SizeMismatch {
30                img1_width,
31                img1_height,
32                img2_width,
33                img2_height,
34            } => write!(
35                f,
36                "Image sizes do not match: {}x{} vs {}x{}",
37                img1_width, img1_height, img2_width, img2_height
38            ),
39            SsimError::InputTooSmall {
40                width,
41                height,
42                minimum,
43            } => write!(
44                f,
45                "Image {}x{} is too small for this metric: needs at least {}x{}",
46                width, height, minimum, minimum
47            ),
48            SsimError::Options(e) => write!(f, "Invalid metric options: {}", e),
49        }
50    }
51}
52
53impl std::error::Error for SsimError {}