From 7773bfb4c63ab3ea49b428e20ef0946b713fd5f5 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 23 Apr 2021 17:16:33 +0530 Subject: add basic obi to png conversions --- src/convert.rs | 26 ++++++++++++++++++++++++++ src/lib.rs | 5 +++++ 2 files changed, 31 insertions(+) create mode 100644 src/convert.rs (limited to 'src') diff --git a/src/convert.rs b/src/convert.rs new file mode 100644 index 0000000..368b2ef --- /dev/null +++ b/src/convert.rs @@ -0,0 +1,26 @@ +use crate::{ + error::{OBIError, OBIResult}, + CompressionType, Image, +}; + +use std::io::Write; + +use png::{BitDepth, ColorType, Encoder, Writer}; + +pub fn to_png(writer: W, img: &Image) -> Writer { + let mut encoder = Encoder::new(writer, img.width(), img.height()); + encoder.set_color(ColorType::RGBA); + encoder.set_depth(BitDepth::Eight); + let mut writer = encoder.write_header().unwrap(); + writer + .write_image_data( + &img.data + .iter() + .take((img.width() * img.height()) as usize) + .map(|x| if *x { vec![255; 4] } else { vec![0, 0, 0, 255] }) + .flatten() + .collect::>()[..], + ) + .unwrap(); + writer +} diff --git a/src/lib.rs b/src/lib.rs index 05070cb..b6cab75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ use std::io; +pub mod convert; mod decode; mod encode; pub mod error; @@ -165,4 +166,8 @@ impl Image { pub fn decode(data: &mut io::Cursor>) -> OBIResult { decode::decode_image(data) } + + pub fn write_png(&self, writer: W) -> png::Writer { + convert::to_png(writer, self) + } } -- cgit v1.2.3