kornelski / lodepng-rust

All-in-one PNG image encoder/decoder in pure Rust
https://lib.rs/lodepng
zlib License
100 stars 23 forks source link

examples provided in crate lodepng fail greatly underestimate its abilities. Here's just one example. #47

Open pwplus7 opened 3 years ago

pwplus7 commented 3 years ago
#![allow(non_snake_case)]

use lodepng::* ;
use std::process::Command; // used to execute OS commands that calls a PNG image viewer

fn main() {

  // PNG encode a simple tri-color image

  const W: usize = 300;  const H: usize = 600;
  let mut img = [[0_u8; 4]; W*H ];

  for frame in 1..6
  { let f8 : u8 = 30*frame as u8;
    for h in 0..H
    { for w in 0..W
      {  img[h*W +w ] =
            match h/200
              {   0  => [   f8, 0_u8, 0_u8, 255_u8],
                  1  => [ 0_u8,   f8, 0_u8, 255_u8],
                  _  => [ 0_u8, 0_u8,   f8, 255_u8],
              } ;
    }  }
    //Save image:  the buffer can be a slice of any type that has 4 bytes per element e.g. struct RGBA or [u8; 4]
    let _result =encode32_file("out.png", &img, W, H) ;
    // set up  cmd that calls The OS  PNG viewer...below the Linux feh viewer is called
    // let result =
    Command:: new("feh")
        .arg("out.png")
        .status() //.output()
        .expect("failed to execute command") ;
    }
}
kornelski commented 3 years ago

I'm not sure what you mean. There is an example for encoding. I don't have any example for viewing images, but that's out of scope of this library.