changeset 745:90e907859bae

examples: Simplify PNG load functions.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 18 Jan 2020 16:27:57 +0100
parents 3687205fe620
children 0ebf6467e4ff
files examples/common.rs
diffstat 1 files changed, 51 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/examples/common.rs
+++ b/examples/common.rs
@@ -1,4 +1,4 @@
-use image::{GenericImageView, DynamicImage, ImageError};
+use image::{GenericImageView, DynamicImage, GrayImage, ImageError};
 use luminance::pixel::{NormRGB8UI, NormRGBA8UI};
 use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps};
 use luminance_glfw::GlfwSurface;
@@ -27,73 +27,67 @@ pub enum TextureLoadError {
     AlphaToGrayscale(String),
 }
 
-fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Result<LoadedTexture, TextureLoadError> {
+fn open_rgb_png(path: &Path) -> Result<DynamicImage, TextureLoadError> {
+    // load the texture into memory as a whole bloc (i.e. no streaming)
+    image::open(&path).map_err(|e| TextureLoadError::CannotOpenRgb(path.to_str().unwrap().to_owned(), e))
+}
+
+fn open_alpha_png(path: &Path) -> Result<DynamicImage, TextureLoadError> {
     // load the texture into memory as a whole bloc (i.e. no streaming)
-    match image::open(&path) {
-        Ok(img) => {
-            let (width, height) = img.dimensions();
-            let texels = img
-                .pixels()
-                .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2]))
-                .collect::<Vec<_>>();
+    image::open(&path).map_err(|e| TextureLoadError::CannotOpenAlpha(path.to_str().unwrap().to_owned(), e))
+}
+
+fn merge_rgb_alpha(rgb: &DynamicImage, alpha: &GrayImage) -> Vec<(u8, u8, u8, u8)> {
+    rgb
+        .pixels()
+        .zip(alpha.pixels())
+        .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[2], luma[0]))
+        .collect::<Vec<_>>()
+}
 
-            // create the luminance texture; the third argument is the number of mipmaps we want (leave it
-            // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
-            // shader (we’ll just use the default one)
-            let tex =
-                Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation");
+fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Result<LoadedTexture, TextureLoadError> {
+    let img = open_rgb_png(&path)?;
+    let (width, height) = img.dimensions();
+    let texels = img
+        .pixels()
+        .map(|(_x, _y, rgb)| (rgb[0], rgb[1], rgb[2]))
+        .collect::<Vec<_>>();
 
-            // the first argument disables mipmap generation (we don’t care so far)
-            tex.upload(GenMipmaps::No, &texels).unwrap();
+    // create the luminance texture; the third argument is the number of mipmaps we want (leave it
+    // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
+    // shader (we’ll just use the default one)
+    let tex =
+        Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation");
 
-            Ok(LoadedTexture::Rgb(tex))
-        }
+    // the first argument disables mipmap generation (we don’t care so far)
+    tex.upload(GenMipmaps::No, &texels).unwrap();
 
-        Err(e) => {
-            Err(TextureLoadError::CannotOpenRgb(path.to_str().unwrap().to_owned(), e))
-        }
-    }
+    Ok(LoadedTexture::Rgb(tex))
 }
 
 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) => {
-            let (width, height) = img.dimensions();
-            let alpha = match img.grayscale() {
-                DynamicImage::ImageLuma8(img) => img,
-                _ => {
-                    return Err(TextureLoadError::AlphaToGrayscale(alpha.to_str().unwrap().to_owned()))
-                }
-            };
-            let img = match image::open(&rgb) {
-                Ok(img) => img,
-                Err(e) => {
-                    return Err(TextureLoadError::CannotOpenRgb(rgb.to_str().unwrap().to_owned(), e))
-                },
-            };
-            let texels = img
-                .pixels()
-                .zip(alpha.pixels())
-                .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[2], luma[0]))
-                .collect::<Vec<_>>();
+    let img = open_alpha_png(&alpha)?;
+    let alpha = match img.grayscale() {
+        DynamicImage::ImageLuma8(img) => img,
+        _ => {
+            return Err(TextureLoadError::AlphaToGrayscale(alpha.to_str().unwrap().to_owned()))
+        }
+    };
+    let (width, height) = img.dimensions();
+    let img = open_rgb_png(&rgb)?;
+    assert_eq!((width, height), img.dimensions());
+    let texels = merge_rgb_alpha(&img, &alpha);
 
-            // create the luminance texture; the third argument is the number of mipmaps we want (leave it
-            // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
-            // shader (we’ll just use the default one)
-            let tex =
-                Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation");
+    // create the luminance texture; the third argument is the number of mipmaps we want (leave it
+    // to 0 for now) and the latest is a the sampler to use when sampling the texels in the
+    // shader (we’ll just use the default one)
+    let tex =
+        Texture::new(surface, [width, height], 0, Sampler::default()).expect("luminance texture creation");
 
-            // the first argument disables mipmap generation (we don’t care so far)
-            tex.upload(GenMipmaps::No, &texels).unwrap();
+    // the first argument disables mipmap generation (we don’t care so far)
+    tex.upload(GenMipmaps::No, &texels).unwrap();
 
-            Ok(LoadedTexture::Rgba(tex))
-        }
-
-        Err(e) => {
-            Err(TextureLoadError::CannotOpenAlpha(alpha.to_str().unwrap().to_owned(), e))
-        }
-    }
+    Ok(LoadedTexture::Rgba(tex))
 }
 
 pub fn load_anm_image<P: AsRef<Path>>(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: P) -> Result<LoadedTexture, TextureLoadError> {