Mercurial > touhou
annotate examples/common.rs @ 745:90e907859bae
examples: Simplify PNG load functions.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 18 Jan 2020 16:27:57 +0100 |
parents | 8d29dac12219 |
children | 0ebf6467e4ff |
rev | line source |
---|---|
745
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
1 use image::{GenericImageView, DynamicImage, GrayImage, ImageError}; |
705
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}; |
707
987409d48991
Switch to versioned luminance now that it got released, not hardcoded paths.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
706
diff
changeset
|
4 use luminance_glfw::GlfwSurface; |
706
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
5 use touhou::th06::anm0::Anm0; |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 use std::fs::File; |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 use std::io::{BufReader, Read}; |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 use std::path::Path; |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 |
740
8d29dac12219
examples: Make the common functions take AsRef<Path>.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
717
diff
changeset
|
10 pub fn load_file_into_vec<P: AsRef<Path>>(filename: P) -> Vec<u8> { |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 let file = File::open(filename).unwrap(); |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 let mut file = BufReader::new(file); |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 let mut buf = vec![]; |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 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
|
15 buf |
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 |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 pub enum LoadedTexture { |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 Rgba(Texture<Flat, Dim2, NormRGBA8UI>), |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 Rgb(Texture<Flat, Dim2, NormRGB8UI>), |
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 |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
23 #[derive(Debug)] |
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
24 pub enum TextureLoadError { |
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
25 CannotOpenRgb(String, ImageError), |
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
26 CannotOpenAlpha(String, ImageError), |
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
27 AlphaToGrayscale(String), |
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
28 } |
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
29 |
745
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
30 fn open_rgb_png(path: &Path) -> Result<DynamicImage, TextureLoadError> { |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
31 // load the texture into memory as a whole bloc (i.e. no streaming) |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
32 image::open(&path).map_err(|e| TextureLoadError::CannotOpenRgb(path.to_str().unwrap().to_owned(), e)) |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
33 } |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
34 |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
35 fn open_alpha_png(path: &Path) -> Result<DynamicImage, TextureLoadError> { |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 // load the texture into memory as a whole bloc (i.e. no streaming) |
745
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
37 image::open(&path).map_err(|e| TextureLoadError::CannotOpenAlpha(path.to_str().unwrap().to_owned(), e)) |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
38 } |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
39 |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
40 fn merge_rgb_alpha(rgb: &DynamicImage, alpha: &GrayImage) -> Vec<(u8, u8, u8, u8)> { |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
41 rgb |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
42 .pixels() |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
43 .zip(alpha.pixels()) |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
44 .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[2], luma[0])) |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
45 .collect::<Vec<_>>() |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
46 } |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 |
745
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
48 fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Result<LoadedTexture, TextureLoadError> { |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
49 let img = open_rgb_png(&path)?; |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
50 let (width, height) = img.dimensions(); |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
51 let texels = img |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
52 .pixels() |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
53 .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2])) |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
54 .collect::<Vec<_>>(); |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 |
745
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
56 // create the luminance texture; the third argument is the number of mipmaps we want (leave it |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
57 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
58 // shader (we’ll just use the default one) |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
59 let tex = |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
60 Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation"); |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 |
745
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
62 // the first argument disables mipmap generation (we don’t care so far) |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
63 tex.upload(GenMipmaps::No, &texels).unwrap(); |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 |
745
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
65 Ok(LoadedTexture::Rgb(tex)) |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
66 } |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
68 fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Result<LoadedTexture, TextureLoadError> { |
745
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
69 let img = open_alpha_png(&alpha)?; |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
70 let alpha = match img.grayscale() { |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
71 DynamicImage::ImageLuma8(img) => img, |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
72 _ => { |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
73 return Err(TextureLoadError::AlphaToGrayscale(alpha.to_str().unwrap().to_owned())) |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
74 } |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
75 }; |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
76 let (width, height) = img.dimensions(); |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
77 let img = open_rgb_png(&rgb)?; |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
78 assert_eq!((width, height), img.dimensions()); |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
79 let texels = merge_rgb_alpha(&img, &alpha); |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 |
745
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
81 // create the luminance texture; the third argument is the number of mipmaps we want (leave it |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
82 // to 0 for now) and the latest is a the sampler to use when sampling the texels in the |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
83 // shader (we’ll just use the default one) |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
84 let tex = |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
85 Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation"); |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
86 |
745
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
87 // the first argument disables mipmap generation (we don’t care so far) |
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
88 tex.upload(GenMipmaps::No, &texels).unwrap(); |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
89 |
745
90e907859bae
examples: Simplify PNG load functions.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
740
diff
changeset
|
90 Ok(LoadedTexture::Rgba(tex)) |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 } |
706
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
92 |
740
8d29dac12219
examples: Make the common functions take AsRef<Path>.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
717
diff
changeset
|
93 pub fn load_anm_image<P: AsRef<Path>>(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: P) -> Result<LoadedTexture, TextureLoadError> { |
8d29dac12219
examples: Make the common functions take AsRef<Path>.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
717
diff
changeset
|
94 let anm_filename = anm_filename.as_ref(); |
706
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
95 let png_filename = anm_filename.with_file_name(Path::new(&anm0.png_filename).file_name().unwrap()); |
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
96 match anm0.alpha_filename { |
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
97 Some(ref filename) => { |
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
98 let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap()); |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
99 load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename) |
706
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
100 }, |
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
101 None => { |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
102 load_rgb_png(&mut surface, &png_filename) |
706
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
103 } |
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
104 } |
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
105 } |