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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::importer::{ImportResult, Importer};
use image::{self, DynamicImage};
use std::path::Path;

/// Importer type for images.
#[derive(Copy, Clone)]
pub struct ImageImporter;

impl ImageImporter {
    /// Import an image from a path.
    pub fn import_from_file(file: &Path) -> ImportResult<DynamicImage> {
        <Self as Importer<&Path>>::import(file)
    }

    /// Import an image from memory.
    pub fn import_from_memory(buf: &[u8]) -> ImportResult<DynamicImage> {
        <Self as Importer<&[u8]>>::import(buf)
    }
}

impl<'a> Importer<&'a Path> for ImageImporter {
    type Texture = DynamicImage;

    fn import(input: &Path) -> ImportResult<DynamicImage> {
        match image::open(input) {
            Ok(image) => Ok(image),
            Err(e) => Err(format!("{}", e)),
        }
    }
}

impl<'a> Importer<&'a [u8]> for ImageImporter {
    type Texture = DynamicImage;

    fn import(input: &[u8]) -> ImportResult<DynamicImage> {
        match image::load_from_memory(input) {
            Ok(image) => Ok(image),
            Err(e) => Err(format!("{}", e)),
        }
    }
}