oxygengine_composite_renderer/
png_image_asset_protocol.rs

1use crate::core::assets::protocol::{AssetLoadResult, AssetProtocol};
2use std::io::Cursor;
3
4pub struct PngImageAsset {
5    bytes: Vec<u8>,
6    width: usize,
7    height: usize,
8}
9
10impl PngImageAsset {
11    pub fn bytes(&self) -> &[u8] {
12        &self.bytes
13    }
14
15    pub fn width(&self) -> usize {
16        self.width
17    }
18
19    pub fn height(&self) -> usize {
20        self.height
21    }
22}
23
24pub struct PngImageAssetProtocol;
25
26impl AssetProtocol for PngImageAssetProtocol {
27    fn name(&self) -> &str {
28        "png"
29    }
30
31    fn on_load(&mut self, data: Vec<u8>) -> AssetLoadResult {
32        let stream = Cursor::new(&data);
33        let decoder = png::Decoder::new(stream);
34        let reader = decoder.read_info().unwrap();
35        let info = reader.info();
36        let width = info.width as usize;
37        let height = info.height as usize;
38        AssetLoadResult::Data(Box::new(PngImageAsset {
39            bytes: data,
40            width,
41            height,
42        }))
43    }
44}