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