# HG changeset patch # User Emmanuel Gil Peyrot # Date 1569240095 -7200 # Node ID d5d5496e4e53d5aa4d2c34156eec08ba5abd763a # Parent 5016c09e5d7c55f3e64cb9ca227f287694cb86f7 examples: Propagate image loading errors. diff --git a/examples/anmrenderer.rs b/examples/anmrenderer.rs --- a/examples/anmrenderer.rs +++ b/examples/anmrenderer.rs @@ -132,7 +132,7 @@ fn main() { let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap(); // Open the image atlas matching this ANM. - let tex = load_anm_image(&mut surface, &anm0, anm_filename); + let tex = load_anm_image(&mut surface, &anm0, anm_filename).expect("image loading"); // set the uniform interface to our type so that we can read textures from the shader let program = diff --git a/examples/common.rs b/examples/common.rs --- 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), } -fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Option { +#[derive(Debug)] +pub enum TextureLoadError { + CannotOpenRgb(String, ImageError), + CannotOpenAlpha(String, ImageError), + AlphaToGrayscale(String), +} + +fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Result { // 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 { +fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Result { // 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 { 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) } } } diff --git a/examples/eclrenderer.rs b/examples/eclrenderer.rs --- a/examples/eclrenderer.rs +++ b/examples/eclrenderer.rs @@ -133,7 +133,7 @@ fn main() { let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap(); // Open the image atlas matching this ANM. - let tex = load_anm_image(&mut surface, &anm0.borrow(), anm_filename); + let tex = load_anm_image(&mut surface, &anm0.borrow(), anm_filename).expect("image loading"); // set the uniform interface to our type so that we can read textures from the shader let program = diff --git a/examples/stdrenderer.rs b/examples/stdrenderer.rs --- a/examples/stdrenderer.rs +++ b/examples/stdrenderer.rs @@ -159,7 +159,7 @@ fn main() { let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap(); // Open the image atlas matching this ANM. - let tex = load_anm_image(&mut surface, &anm0, anm_filename); + let tex = load_anm_image(&mut surface, &anm0, anm_filename).expect("image loading"); // set the uniform interface to our type so that we can read textures from the shader let program =