Mercurial > touhou
comparison 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 |
comparison
equal
deleted
inserted
replaced
716:5016c09e5d7c | 717:d5d5496e4e53 |
---|---|
1 use image::{GenericImageView, DynamicImage}; | 1 use image::{GenericImageView, DynamicImage, ImageError}; |
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::GlfwSurface; | 4 use luminance_glfw::GlfwSurface; |
5 use touhou::th06::anm0::Anm0; | 5 use touhou::th06::anm0::Anm0; |
6 use std::fs::File; | 6 use std::fs::File; |
18 pub enum LoadedTexture { | 18 pub enum LoadedTexture { |
19 Rgba(Texture<Flat, Dim2, NormRGBA8UI>), | 19 Rgba(Texture<Flat, Dim2, NormRGBA8UI>), |
20 Rgb(Texture<Flat, Dim2, NormRGB8UI>), | 20 Rgb(Texture<Flat, Dim2, NormRGB8UI>), |
21 } | 21 } |
22 | 22 |
23 fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Option<LoadedTexture> { | 23 #[derive(Debug)] |
24 pub enum TextureLoadError { | |
25 CannotOpenRgb(String, ImageError), | |
26 CannotOpenAlpha(String, ImageError), | |
27 AlphaToGrayscale(String), | |
28 } | |
29 | |
30 fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Result<LoadedTexture, TextureLoadError> { | |
24 // load the texture into memory as a whole bloc (i.e. no streaming) | 31 // load the texture into memory as a whole bloc (i.e. no streaming) |
25 match image::open(&path) { | 32 match image::open(&path) { |
26 Ok(img) => { | 33 Ok(img) => { |
27 let (width, height) = img.dimensions(); | 34 let (width, height) = img.dimensions(); |
28 let texels = img | 35 let texels = img |
37 Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation"); | 44 Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation"); |
38 | 45 |
39 // the first argument disables mipmap generation (we don’t care so far) | 46 // the first argument disables mipmap generation (we don’t care so far) |
40 tex.upload(GenMipmaps::No, &texels).unwrap(); | 47 tex.upload(GenMipmaps::No, &texels).unwrap(); |
41 | 48 |
42 Some(LoadedTexture::Rgb(tex)) | 49 Ok(LoadedTexture::Rgb(tex)) |
43 } | 50 } |
44 | 51 |
45 Err(e) => { | 52 Err(e) => { |
46 eprintln!("cannot open image {}: {}", path.display(), e); | 53 Err(TextureLoadError::CannotOpenRgb(path.to_str().unwrap().to_owned(), e)) |
47 None | |
48 } | 54 } |
49 } | 55 } |
50 } | 56 } |
51 | 57 |
52 fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Option<LoadedTexture> { | 58 fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Result<LoadedTexture, TextureLoadError> { |
53 // load the texture into memory as a whole bloc (i.e. no streaming) | 59 // load the texture into memory as a whole bloc (i.e. no streaming) |
54 match image::open(&alpha) { | 60 match image::open(&alpha) { |
55 Ok(img) => { | 61 Ok(img) => { |
56 let (width, height) = img.dimensions(); | 62 let (width, height) = img.dimensions(); |
57 let alpha = match img.grayscale() { | 63 let alpha = match img.grayscale() { |
58 DynamicImage::ImageLuma8(img) => img, | 64 DynamicImage::ImageLuma8(img) => img, |
59 _ => { | 65 _ => { |
60 eprintln!("cannot convert alpha image {} to grayscale", alpha.display()); | 66 return Err(TextureLoadError::AlphaToGrayscale(alpha.to_str().unwrap().to_owned())) |
61 return None; | |
62 } | 67 } |
63 }; | 68 }; |
64 let img = match image::open(&rgb) { | 69 let img = match image::open(&rgb) { |
65 Ok(img) => img, | 70 Ok(img) => img, |
66 Err(e) => { | 71 Err(e) => { |
67 eprintln!("cannot open rgb image {}: {}", rgb.display(), e); | 72 return Err(TextureLoadError::CannotOpenRgb(rgb.to_str().unwrap().to_owned(), e)) |
68 return None; | |
69 }, | 73 }, |
70 }; | 74 }; |
71 let texels = img | 75 let texels = img |
72 .pixels() | 76 .pixels() |
73 .zip(alpha.pixels()) | 77 .zip(alpha.pixels()) |
81 Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation"); | 85 Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation"); |
82 | 86 |
83 // the first argument disables mipmap generation (we don’t care so far) | 87 // the first argument disables mipmap generation (we don’t care so far) |
84 tex.upload(GenMipmaps::No, &texels).unwrap(); | 88 tex.upload(GenMipmaps::No, &texels).unwrap(); |
85 | 89 |
86 Some(LoadedTexture::Rgba(tex)) | 90 Ok(LoadedTexture::Rgba(tex)) |
87 } | 91 } |
88 | 92 |
89 Err(e) => { | 93 Err(e) => { |
90 eprintln!("cannot open alpha image {}: {}", alpha.display(), e); | 94 Err(TextureLoadError::CannotOpenAlpha(alpha.to_str().unwrap().to_owned(), e)) |
91 None | |
92 } | 95 } |
93 } | 96 } |
94 } | 97 } |
95 | 98 |
96 pub fn load_anm_image(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: &Path) -> LoadedTexture { | 99 pub fn load_anm_image(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: &Path) -> Result<LoadedTexture, TextureLoadError> { |
97 let png_filename = anm_filename.with_file_name(Path::new(&anm0.png_filename).file_name().unwrap()); | 100 let png_filename = anm_filename.with_file_name(Path::new(&anm0.png_filename).file_name().unwrap()); |
98 match anm0.alpha_filename { | 101 match anm0.alpha_filename { |
99 Some(ref filename) => { | 102 Some(ref filename) => { |
100 let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap()); | 103 let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap()); |
101 println!("alpha {:?}", alpha_filename); | 104 load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename) |
102 load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename).expect("texture loading") | |
103 }, | 105 }, |
104 None => { | 106 None => { |
105 load_rgb_png(&mut surface, &png_filename).expect("texture loading") | 107 load_rgb_png(&mut surface, &png_filename) |
106 } | 108 } |
107 } | 109 } |
108 } | 110 } |