comparison examples/common.rs @ 706:bca515da9047

examples: use common module.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 19:46:47 +0200
parents ed65f9412bc0
children 987409d48991
comparison
equal deleted inserted replaced
705:ed65f9412bc0 706:bca515da9047
1 use image::{GenericImageView, DynamicImage}; 1 use image::{GenericImageView, DynamicImage};
2 use luminance::pixel::{NormRGB8UI, NormRGBA8UI}; 2 use luminance::pixel::{NormRGB8UI, NormRGBA8UI};
3 use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps}; 3 use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps};
4 use luminance_glfw::surface::GlfwSurface; 4 use luminance_glfw::surface::GlfwSurface;
5 use touhou::th06::anm0::Anm0;
5 use std::fs::File; 6 use std::fs::File;
6 use std::io::{BufReader, Read}; 7 use std::io::{BufReader, Read};
7 use std::path::Path; 8 use std::path::Path;
8 9
9 pub fn load_file_into_vec(filename: &Path) -> Vec<u8> { 10 pub fn load_file_into_vec(filename: &Path) -> Vec<u8> {
17 pub enum LoadedTexture { 18 pub enum LoadedTexture {
18 Rgba(Texture<Flat, Dim2, NormRGBA8UI>), 19 Rgba(Texture<Flat, Dim2, NormRGBA8UI>),
19 Rgb(Texture<Flat, Dim2, NormRGB8UI>), 20 Rgb(Texture<Flat, Dim2, NormRGB8UI>),
20 } 21 }
21 22
22 pub fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Option<LoadedTexture> { 23 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 // load the texture into memory as a whole bloc (i.e. no streaming)
24 match image::open(&path) { 25 match image::open(&path) {
25 Ok(img) => { 26 Ok(img) => {
26 let (width, height) = img.dimensions(); 27 let (width, height) = img.dimensions();
27 let texels = img 28 let texels = img
46 None 47 None
47 } 48 }
48 } 49 }
49 } 50 }
50 51
51 pub fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Option<LoadedTexture> { 52 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 // load the texture into memory as a whole bloc (i.e. no streaming)
53 match image::open(&alpha) { 54 match image::open(&alpha) {
54 Ok(img) => { 55 Ok(img) => {
55 let (width, height) = img.dimensions(); 56 let (width, height) = img.dimensions();
56 let alpha = match img.grayscale() { 57 let alpha = match img.grayscale() {
68 }, 69 },
69 }; 70 };
70 let texels = img 71 let texels = img
71 .pixels() 72 .pixels()
72 .zip(alpha.pixels()) 73 .zip(alpha.pixels())
73 .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[1], luma[0])) 74 .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[2], luma[0]))
74 .collect::<Vec<_>>(); 75 .collect::<Vec<_>>();
75 76
76 // create the luminance texture; the third argument is the number of mipmaps we want (leave it 77 // 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 // 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 // shader (we’ll just use the default one)
89 eprintln!("cannot open alpha image {}: {}", alpha.display(), e); 90 eprintln!("cannot open alpha image {}: {}", alpha.display(), e);
90 None 91 None
91 } 92 }
92 } 93 }
93 } 94 }
95
96 pub fn load_anm_image(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: &Path) -> LoadedTexture {
97 let png_filename = anm_filename.with_file_name(Path::new(&anm0.png_filename).file_name().unwrap());
98 match anm0.alpha_filename {
99 Some(ref filename) => {
100 let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap());
101 println!("alpha {:?}", alpha_filename);
102 load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename).expect("texture loading")
103 },
104 None => {
105 load_rgb_png(&mut surface, &png_filename).expect("texture loading")
106 }
107 }
108 }