use ggstd::image::{self, color, png, Image};
use ggstd::os;
fn main() {
example_encode();
}
fn example_encode() {
let (width, height) = (256, 256);
let mut img = image::Img::new_nrgba(&image::rect(0, 0, width, height));
for y in 0..height {
for x in 0..width {
let c = color::Color::new_rgba(
((x + y) & 255) as u8,
(((x + y) << 1) & 255) as u8,
(((x + y) << 2) & 255) as u8,
255,
);
img.set(x, y, &c);
}
}
let mut f = os::create("image.png").unwrap();
png::encode(&mut f, &img).unwrap();
f.sync_all().unwrap();
{
let mut f = std::fs::File::open("image.png").unwrap();
let mut br = std::io::BufReader::new(&mut f);
let config = png::decode_config(&mut br).unwrap();
println!("size: {}x{}", config.width, config.height);
println!("color model: {:?}", config.color_model);
}
{
let mut f = std::fs::File::open("image.png").unwrap();
let mut br = std::io::BufReader::new(&mut f);
let img = png::decode(&mut br).unwrap();
let width = img.bounds().dx();
let height = img.bounds().dy();
println!("size: {}x{}", width, height);
println!("color model: {:?}", img.color_model());
for y in 0..height {
for x in 0..width {
let c = img.at(x as isize, y as isize);
let expect = color::Color::new_rgba(
((x + y) & 255) as u8,
(((x + y) << 1) & 255) as u8,
(((x + y) << 2) & 255) as u8,
255,
);
if c != expect {
println!(
"unexpected color at ({}, {}): expecting {:?}, got {:?}",
x, y, expect, c
);
}
}
}
}
}