Mercurial > touhou
annotate examples/common.rs @ 717:d5d5496e4e53
examples: Propagate image loading errors.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Mon, 23 Sep 2019 14:01:35 +0200 |
parents | 2b2376811f46 |
children | 8d29dac12219 |
rev | line source |
---|---|
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
1 use image::{GenericImageView, DynamicImage, 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 |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 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
|
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 |
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
30 fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Result<LoadedTexture, TextureLoadError> { |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 // 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
|
32 match image::open(&path) { |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 Ok(img) => { |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 let (width, height) = img.dimensions(); |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 let texels = img |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 .pixels() |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 .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
|
38 .collect::<Vec<_>>(); |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 // 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
|
41 // 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
|
42 // 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
|
43 let tex = |
715
2b2376811f46
examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
708
diff
changeset
|
44 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
|
45 |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 // the first argument disables mipmap generation (we don’t care so far) |
708
3954801b6299
examples: Update to luminance 0.32.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
707
diff
changeset
|
47 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
|
48 |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
49 Ok(LoadedTexture::Rgb(tex)) |
705
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 |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 Err(e) => { |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
53 Err(TextureLoadError::CannotOpenRgb(path.to_str().unwrap().to_owned(), e)) |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 } |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 } |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
56 } |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
58 fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Result<LoadedTexture, TextureLoadError> { |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 // 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
|
60 match image::open(&alpha) { |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 Ok(img) => { |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
62 let (width, height) = img.dimensions(); |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
63 let alpha = match img.grayscale() { |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 DynamicImage::ImageLuma8(img) => img, |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
65 _ => { |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
66 return Err(TextureLoadError::AlphaToGrayscale(alpha.to_str().unwrap().to_owned())) |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 } |
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 let img = match image::open(&rgb) { |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 Ok(img) => img, |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 Err(e) => { |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
72 return Err(TextureLoadError::CannotOpenRgb(rgb.to_str().unwrap().to_owned(), e)) |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 }, |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 }; |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 let texels = img |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 .pixels() |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 .zip(alpha.pixels()) |
706
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
78 .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[2], luma[0])) |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
79 .collect::<Vec<_>>(); |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 // 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
|
82 // 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
|
83 // 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
|
84 let tex = |
715
2b2376811f46
examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
708
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 |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
87 // the first argument disables mipmap generation (we don’t care so far) |
708
3954801b6299
examples: Update to luminance 0.32.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
707
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 |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
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 } |
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 Err(e) => { |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
94 Err(TextureLoadError::CannotOpenAlpha(alpha.to_str().unwrap().to_owned(), e)) |
705
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 } |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
96 } |
ed65f9412bc0
anmrenderer: split common loading functions in another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
97 } |
706
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
98 |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
99 pub fn load_anm_image(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: &Path) -> Result<LoadedTexture, TextureLoadError> { |
706
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
100 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
|
101 match anm0.alpha_filename { |
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
102 Some(ref filename) => { |
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
103 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
|
104 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
|
105 }, |
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
106 None => { |
717
d5d5496e4e53
examples: Propagate image loading errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
715
diff
changeset
|
107 load_rgb_png(&mut surface, &png_filename) |
706
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
108 } |
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
109 } |
bca515da9047
examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
705
diff
changeset
|
110 } |