# HG changeset patch # User Emmanuel Gil Peyrot # Date 1579361277 -3600 # Node ID 90e907859bae1b37fbe5c110b9c709600ffbebe3 # Parent 3687205fe620117ee1e195f0996c0a26269533d2 examples: Simplify PNG load functions. 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, 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 { +fn open_rgb_png(path: &Path) -> Result { + // 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 { // 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::>(); + 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::>() +} - // 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 { + 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::>(); - // 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 { - // 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::>(); + 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>(mut surface: &mut GlfwSurface, anm0: &Anm0, anm_filename: P) -> Result {