Mercurial > touhou
diff 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 |
line wrap: on
line diff
--- a/examples/common.rs +++ b/examples/common.rs @@ -1,4 +1,4 @@ -use image::{GenericImageView, DynamicImage}; +use image::{GenericImageView, DynamicImage, ImageError}; use luminance::pixel::{NormRGB8UI, NormRGBA8UI}; use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps}; use luminance_glfw::GlfwSurface; @@ -20,7 +20,14 @@ pub enum LoadedTexture { Rgb(Texture<Flat, Dim2, NormRGB8UI>), } -fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Option<LoadedTexture> { +#[derive(Debug)] +pub enum TextureLoadError { + CannotOpenRgb(String, ImageError), + CannotOpenAlpha(String, ImageError), + AlphaToGrayscale(String), +} + +fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Result<LoadedTexture, TextureLoadError> { // load the texture into memory as a whole bloc (i.e. no streaming) match image::open(&path) { Ok(img) => { @@ -39,17 +46,16 @@ fn load_rgb_png(surface: &mut GlfwSurfac // the first argument disables mipmap generation (we don’t care so far) tex.upload(GenMipmaps::No, &texels).unwrap(); - Some(LoadedTexture::Rgb(tex)) + Ok(LoadedTexture::Rgb(tex)) } Err(e) => { - eprintln!("cannot open image {}: {}", path.display(), e); - None + Err(TextureLoadError::CannotOpenRgb(path.to_str().unwrap().to_owned(), e)) } } } -fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Option<LoadedTexture> { +fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Result<LoadedTexture, TextureLoadError> { // load the texture into memory as a whole bloc (i.e. no streaming) match image::open(&alpha) { Ok(img) => { @@ -57,15 +63,13 @@ fn load_rgb_a_pngs(surface: &mut GlfwSur let alpha = match img.grayscale() { DynamicImage::ImageLuma8(img) => img, _ => { - eprintln!("cannot convert alpha image {} to grayscale", alpha.display()); - return None; + return Err(TextureLoadError::AlphaToGrayscale(alpha.to_str().unwrap().to_owned())) } }; let img = match image::open(&rgb) { Ok(img) => img, Err(e) => { - eprintln!("cannot open rgb image {}: {}", rgb.display(), e); - return None; + return Err(TextureLoadError::CannotOpenRgb(rgb.to_str().unwrap().to_owned(), e)) }, }; let texels = img @@ -83,26 +87,24 @@ fn load_rgb_a_pngs(surface: &mut GlfwSur // the first argument disables mipmap generation (we don’t care so far) tex.upload(GenMipmaps::No, &texels).unwrap(); - Some(LoadedTexture::Rgba(tex)) + Ok(LoadedTexture::Rgba(tex)) } Err(e) => { - eprintln!("cannot open alpha image {}: {}", alpha.display(), e); - None + Err(TextureLoadError::CannotOpenAlpha(alpha.to_str().unwrap().to_owned(), e)) } } } -pub fn load_anm_image(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: &Path) -> LoadedTexture { +pub fn load_anm_image(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: &Path) -> Result<LoadedTexture, TextureLoadError> { let png_filename = anm_filename.with_file_name(Path::new(&anm0.png_filename).file_name().unwrap()); match anm0.alpha_filename { Some(ref filename) => { let alpha_filename = anm_filename.with_file_name(Path::new(filename).file_name().unwrap()); - println!("alpha {:?}", alpha_filename); - load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename).expect("texture loading") + load_rgb_a_pngs(&mut surface, &png_filename, &alpha_filename) }, None => { - load_rgb_png(&mut surface, &png_filename).expect("texture loading") + load_rgb_png(&mut surface, &png_filename) } } }