changeset 717:d5d5496e4e53

examples: Propagate image loading errors.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 23 Sep 2019 14:01:35 +0200
parents 5016c09e5d7c
children c187e0a6b751
files examples/anmrenderer.rs examples/common.rs examples/eclrenderer.rs examples/stdrenderer.rs
diffstat 4 files changed, 22 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- 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 =
--- 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)
         }
     }
 }
--- 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 =
--- 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 =