texture_synthesis/
errors.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub struct InvalidRange {
5    pub(crate) min: f32,
6    pub(crate) max: f32,
7    pub(crate) value: f32,
8    pub(crate) name: &'static str,
9}
10
11impl fmt::Display for InvalidRange {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        write!(
14            f,
15            "parameter '{}' - value '{}' is outside the range of {}-{}",
16            self.name, self.value, self.min, self.max
17        )
18    }
19}
20
21#[derive(Debug)]
22pub struct SizeMismatch {
23    pub(crate) input: (u32, u32),
24    pub(crate) output: (u32, u32),
25}
26
27impl fmt::Display for SizeMismatch {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(
30            f,
31            "the input size ({}x{}) must match the output size ({}x{}) when using an inpaint mask",
32            self.input.0, self.input.1, self.output.0, self.output.1
33        )
34    }
35}
36
37#[derive(Debug)]
38pub enum Error {
39    /// An error in the image library occurred, eg failed to load/save
40    Image(image::ImageError),
41    /// An input parameter had an invalid range specified
42    InvalidRange(InvalidRange),
43    /// When using inpaint, the input and output sizes must match
44    SizeMismatch(SizeMismatch),
45    /// If more than 1 example guide is provided, then **all** examples must have
46    /// a guide
47    ExampleGuideMismatch(u32, u32),
48    /// Io is notoriously error free with no problems, but we cover it just in case!
49    Io(std::io::Error),
50    /// The user specified an image format we don't support as the output
51    UnsupportedOutputFormat(String),
52    /// There are no examples to source pixels from, either because no examples
53    /// were added, or all of them used SampleMethod::Ignore
54    NoExamples,
55    ///
56    MapsCountMismatch(u32, u32),
57}
58
59impl std::error::Error for Error {
60    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
61        match self {
62            Self::Image(err) => Some(err),
63            Self::Io(err) => Some(err),
64            _ => None,
65        }
66    }
67}
68
69impl fmt::Display for Error {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match self {
72            Self::Image(ie) => write!(f, "{}", ie),
73            Self::InvalidRange(ir) => write!(f, "{}", ir),
74            Self::SizeMismatch(sm) => write!(f, "{}", sm),
75            Self::ExampleGuideMismatch(examples, guides) => {
76                if examples > guides {
77                    write!(
78                        f,
79                        "{} examples were provided, but only {} guides were",
80                        examples, guides
81                    )
82                } else {
83                    write!(
84                        f,
85                        "{} examples were provided, but {} guides were",
86                        examples, guides
87                    )
88                }
89            }
90            Self::Io(io) => write!(f, "{}", io),
91            Self::UnsupportedOutputFormat(fmt) => {
92                write!(f, "the output format '{}' is not supported", fmt)
93            }
94            Self::NoExamples => write!(
95                f,
96                "at least 1 example must be available as a sampling source"
97            ),
98            Self::MapsCountMismatch(input, required) => write!(
99                f,
100                "{} map(s) were provided, but {} is/are required",
101                input, required
102            ),
103        }
104    }
105}
106
107impl From<image::ImageError> for Error {
108    fn from(ie: image::ImageError) -> Self {
109        Self::Image(ie)
110    }
111}
112
113impl From<std::io::Error> for Error {
114    fn from(io: std::io::Error) -> Self {
115        Self::Io(io)
116    }
117}