diff examples/common.rs @ 705:ed65f9412bc0

anmrenderer: split common loading functions in another module.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 19:29:00 +0200
parents
children bca515da9047
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/examples/common.rs
@@ -0,0 +1,93 @@
+use image::{GenericImageView, DynamicImage};
+use luminance::pixel::{NormRGB8UI, NormRGBA8UI};
+use luminance::texture::{Dim2, Flat, Sampler, Texture, GenMipmaps};
+use luminance_glfw::surface::GlfwSurface;
+use std::fs::File;
+use std::io::{BufReader, Read};
+use std::path::Path;
+
+pub fn load_file_into_vec(filename: &Path) -> Vec<u8> {
+    let file = File::open(filename).unwrap();
+    let mut file = BufReader::new(file);
+    let mut buf = vec![];
+    file.read_to_end(&mut buf).unwrap();
+    buf
+}
+
+pub enum LoadedTexture {
+    Rgba(Texture<Flat, Dim2, NormRGBA8UI>),
+    Rgb(Texture<Flat, Dim2, NormRGB8UI>),
+}
+
+pub fn load_rgb_png(surface: &mut GlfwSurface, path: &Path) -> Option<LoadedTexture> {
+    // 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<_>>();
+
+            // 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);
+
+            Some(LoadedTexture::Rgb(tex))
+        }
+
+        Err(e) => {
+            eprintln!("cannot open image {}: {}", path.display(), e);
+            None
+        }
+    }
+}
+
+pub fn load_rgb_a_pngs(surface: &mut GlfwSurface, rgb: &Path, alpha: &Path) -> Option<LoadedTexture> {
+    // 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,
+                _ => {
+                    eprintln!("cannot convert alpha image {} to grayscale", alpha.display());
+                    return None;
+                }
+            };
+            let img = match image::open(&rgb) {
+                Ok(img) => img,
+                Err(e) => {
+                    eprintln!("cannot open rgb image {}: {}", rgb.display(), e);
+                    return None;
+                },
+            };
+            let texels = img
+                .pixels()
+                .zip(alpha.pixels())
+                .map(|((_x, _y, rgb), luma)| (rgb[0], rgb[1], rgb[1], 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");
+
+            // the first argument disables mipmap generation (we don’t care so far)
+            tex.upload(GenMipmaps::No, &texels);
+
+            Some(LoadedTexture::Rgba(tex))
+        }
+
+        Err(e) => {
+            eprintln!("cannot open alpha image {}: {}", alpha.display(), e);
+            None
+        }
+    }
+}