Mercurial > touhou
comparison examples/common.rs @ 705:ed65f9412bc0
anmrenderer: split common loading functions in another module.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 23 Aug 2019 19:29:00 +0200 |
parents | |
children | bca515da9047 |
comparison
equal
deleted
inserted
replaced
704:84af5bedbde4 | 705:ed65f9412bc0 |
---|---|
1 use image::{GenericImageView, DynamicImage}; | |
2 use luminance::pixel::{NormRGB8UI, NormRGBA8UI}; | |
3 use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps}; | |
4 use luminance_glfw::surface::GlfwSurface; | |
5 use std::fs::File; | |
6 use std::io::{BufReader, Read}; | |
7 use std::path::Path; | |
8 | |
9 pub fn load_file_into_vec(filename: &Path) -> Vec<u8> { | |
10 let file = File::open(filename).unwrap(); | |
11 let mut file = BufReader::new(file); | |
12 let mut buf = vec![]; | |
13 file.read_to_end(&mut buf).unwrap(); | |
14 buf | |
15 } | |
16 | |
17 pub enum LoadedTexture { | |
18 Rgba(Texture<Flat, Dim2, NormRGBA8UI>), | |
19 Rgb(Texture<Flat, Dim2, NormRGB8UI>), | |
20 } | |
21 | |
22 pub fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Option<LoadedTexture> { | |
23 // load the texture into memory as a whole bloc (i.e. no streaming) | |
24 match image::open(&path) { | |
25 Ok(img) => { | |
26 let (width, height) = img.dimensions(); | |
27 let texels = img | |
28 .pixels() | |
29 .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2])) | |
30 .collect::<Vec<_>>(); | |
31 | |
32 // create the luminance texture; the third argument is the number of mipmaps we want (leave it | |
33 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the | |
34 // shader (we’ll just use the default one) | |
35 let tex = | |
36 Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation"); | |
37 | |
38 // the first argument disables mipmap generation (we don’t care so far) | |
39 tex.upload(GenMipmaps::No, &texels); | |
40 | |
41 Some(LoadedTexture::Rgb(tex)) | |
42 } | |
43 | |
44 Err(e) => { | |
45 eprintln!("cannot open image {}: {}", path.display(), e); | |
46 None | |
47 } | |
48 } | |
49 } | |
50 | |
51 pub fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Option<LoadedTexture> { | |
52 // load the texture into memory as a whole bloc (i.e. no streaming) | |
53 match image::open(&alpha) { | |
54 Ok(img) => { | |
55 let (width, height) = img.dimensions(); | |
56 let alpha = match img.grayscale() { | |
57 DynamicImage::ImageLuma8(img) => img, | |
58 _ => { | |
59 eprintln!("cannot convert alpha image {} to grayscale", alpha.display()); | |
60 return None; | |
61 } | |
62 }; | |
63 let img = match image::open(&rgb) { | |
64 Ok(img) => img, | |
65 Err(e) => { | |
66 eprintln!("cannot open rgb image {}: {}", rgb.display(), e); | |
67 return None; | |
68 }, | |
69 }; | |
70 let texels = img | |
71 .pixels() | |
72 .zip(alpha.pixels()) | |
73 .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[1], luma[0])) | |
74 .collect::<Vec<_>>(); | |
75 | |
76 // create the luminance texture; the third argument is the number of mipmaps we want (leave it | |
77 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the | |
78 // shader (we’ll just use the default one) | |
79 let tex = | |
80 Texture::new(surface, [width, height], 0, &Sampler::default()).expect("luminance texture creation"); | |
81 | |
82 // the first argument disables mipmap generation (we don’t care so far) | |
83 tex.upload(GenMipmaps::No, &texels); | |
84 | |
85 Some(LoadedTexture::Rgba(tex)) | |
86 } | |
87 | |
88 Err(e) => { | |
89 eprintln!("cannot open alpha image {}: {}", alpha.display(), e); | |
90 None | |
91 } | |
92 } | |
93 } |