aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--src/convert.rs26
-rw-r--r--src/lib.rs5
3 files changed, 32 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 96ef6c0..95dfa36 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
1/target 1/target
2Cargo.lock 2Cargo.lock
3*.png
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 @@
1use crate::{
2 error::{OBIError, OBIResult},
3 CompressionType, Image,
4};
5
6use std::io::Write;
7
8use png::{BitDepth, ColorType, Encoder, Writer};
9
10pub fn to_png<W: Write>(writer: W, img: &Image) -> Writer<W> {
11 let mut encoder = Encoder::new(writer, img.width(), img.height());
12 encoder.set_color(ColorType::RGBA);
13 encoder.set_depth(BitDepth::Eight);
14 let mut writer = encoder.write_header().unwrap();
15 writer
16 .write_image_data(
17 &img.data
18 .iter()
19 .take((img.width() * img.height()) as usize)
20 .map(|x| if *x { vec![255; 4] } else { vec![0, 0, 0, 255] })
21 .flatten()
22 .collect::<Vec<_>>()[..],
23 )
24 .unwrap();
25 writer
26}
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 @@
4 4
5use std::io; 5use std::io;
6 6
7pub mod convert;
7mod decode; 8mod decode;
8mod encode; 9mod encode;
9pub mod error; 10pub mod error;
@@ -165,4 +166,8 @@ impl Image {
165 pub fn decode(data: &mut io::Cursor<Vec<u8>>) -> OBIResult<Image> { 166 pub fn decode(data: &mut io::Cursor<Vec<u8>>) -> OBIResult<Image> {
166 decode::decode_image(data) 167 decode::decode_image(data)
167 } 168 }
169
170 pub fn write_png<W: io::Write>(&self, writer: W) -> png::Writer<W> {
171 convert::to_png(writer, self)
172 }
168} 173}