summaryrefslogtreecommitdiff
path: root/src/graphics/image.rs
blob: 26833c0d430ae569915ab96f0cb76720f8757d49 (plain)
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
// TODO: このファイルは好きな設計ではないので破壊する。

use super::texture::TextureWrapper;
use png::{BitDepth, ColorType, Decoder};
use std::io::Cursor;
use wgpu::{Device, Queue, TextureFormat, TextureUsages};

#[derive(Clone, Copy)]
pub enum ImageId {
    Image,
}

static IMAGE_DATAS: [&[u8]; 1] = [include_bytes!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/assets/image.png"
))];

pub fn create_textures(device: &Device, queue: &Queue) -> Vec<TextureWrapper> {
    IMAGE_DATAS
        .iter()
        .map(|data| create_texture(device, queue, data))
        .collect()
}

fn create_texture(device: &Device, queue: &Queue, data: &[u8]) -> TextureWrapper {
    let mut reader = Decoder::new(Cursor::new(data))
        .read_info()
        .expect("fatal: failed to load image");
    let mut buf = vec![
        0x00;
        reader
            .output_buffer_size()
            .expect("fatal: image is too large")
    ];
    let info = reader
        .next_frame(&mut buf)
        .expect("fatal: failed to decode image");
    if info.color_type != ColorType::Rgba || info.bit_depth != BitDepth::Eight {
        panic!("fatal: image is not 8-bit RGBA");
    }

    let texture = TextureWrapper::new(
        device,
        info.width,
        info.height,
        TextureFormat::Rgba8UnormSrgb,
        TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
    );
    texture.write(queue, &buf[..info.buffer_size()]);
    texture
}