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
use image::GenericImageView;
use std::sync::Arc;
use std::ffi::CStr;
use std::path::PathBuf;

pub mod timer;
pub mod vertex;

pub fn load_raw(filename: String) -> (Vec<u8>, (u32,u32)) {

    let project_root =
        std::env::current_dir()
            .expect("failed to get root directory");

    let mut compute_path = project_root.clone();
    compute_path.push(PathBuf::from("resources/images/"));
    compute_path.push(PathBuf::from(filename.clone()));

    let img = image::open(compute_path).expect("Couldn't find image");

    let xy = img.dimensions();

    let data_length = xy.0 * xy.1 * 4;
    let pixel_count = img.raw_pixels().len();

    let mut image_buffer = Vec::new();

    if pixel_count != data_length as usize {
        println!("Creating apha channel...");
        for i in img.raw_pixels().iter() {
            if (image_buffer.len() + 1) % 4 == 0 {
                image_buffer.push(255);
            }
            image_buffer.push(*i);
        }
        image_buffer.push(255);
    } else {
        image_buffer = img.raw_pixels();
    }

    (image_buffer, xy)
}