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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::{io, result};

use skia_bindings as sb;

use super::{decode_stream, Codec, Decoder, Result};

#[derive(Debug)]
pub struct BmpDecoder;

impl Decoder for BmpDecoder {
    const ID: &'static str = "bmp";

    fn is_format(data: &[u8]) -> bool {
        unsafe { sb::C_SkBmpDecoder_IsBmp(data.as_ptr() as _, data.len()) }
    }

    fn decode_stream(stream: &mut impl io::Read) -> result::Result<Codec, Result> {
        decode_stream(stream, sb::C_SkBmpDecoder_Decode)
    }
}

#[derive(Debug)]
pub struct GifDecoder;

impl Decoder for GifDecoder {
    const ID: &'static str = "gif";

    fn is_format(data: &[u8]) -> bool {
        unsafe { sb::C_SkGifDecoder_IsGif(data.as_ptr() as _, data.len()) }
    }

    fn decode_stream(stream: &mut impl io::Read) -> result::Result<Codec, Result> {
        decode_stream(stream, sb::C_SkGifDecoder_Decode)
    }
}

#[derive(Debug)]
pub struct IcoDecoder;

impl Decoder for IcoDecoder {
    const ID: &'static str = "ico";

    fn is_format(data: &[u8]) -> bool {
        unsafe { sb::C_SkIcoDecoder_IsIco(data.as_ptr() as _, data.len()) }
    }

    fn decode_stream(stream: &mut impl io::Read) -> result::Result<Codec, Result> {
        decode_stream(stream, sb::C_SkIcoDecoder_Decode)
    }
}

#[derive(Debug)]
pub struct JpegDecoder;

impl Decoder for JpegDecoder {
    const ID: &'static str = "jpeg";

    fn is_format(data: &[u8]) -> bool {
        unsafe { sb::C_SkJpegDecoder_IsJpeg(data.as_ptr() as _, data.len()) }
    }

    fn decode_stream(stream: &mut impl io::Read) -> result::Result<Codec, Result> {
        decode_stream(stream, sb::C_SkJpegDecoder_Decode)
    }
}

#[derive(Debug)]
pub struct PngDecoder;

impl Decoder for PngDecoder {
    const ID: &'static str = "png";

    fn is_format(data: &[u8]) -> bool {
        unsafe { sb::C_SkPngDecoder_IsPng(data.as_ptr() as _, data.len()) }
    }

    fn decode_stream(stream: &mut impl io::Read) -> result::Result<Codec, Result> {
        decode_stream(stream, sb::C_SkPngDecoder_Decode)
    }
}

#[derive(Debug)]
pub struct WbmpDecoder;

impl Decoder for WbmpDecoder {
    const ID: &'static str = "Wbmp";

    fn is_format(data: &[u8]) -> bool {
        unsafe { sb::C_SkWbmpDecoder_IsWbmp(data.as_ptr() as _, data.len()) }
    }

    fn decode_stream(stream: &mut impl io::Read) -> result::Result<Codec, Result> {
        decode_stream(stream, sb::C_SkWbmpDecoder_Decode)
    }
}

#[cfg(feature = "webp-decode")]
#[derive(Debug)]
pub struct WebpDecoder;

#[cfg(feature = "webp-decode")]
impl Decoder for WebpDecoder {
    const ID: &'static str = "Webp";

    fn is_format(data: &[u8]) -> bool {
        unsafe { sb::C_SkWebpDecoder_IsWebp(data.as_ptr() as _, data.len()) }
    }

    fn decode_stream(stream: &mut impl io::Read) -> result::Result<Codec, Result> {
        decode_stream(stream, sb::C_SkWebpDecoder_Decode)
    }
}