diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/convert.rs | 26 | ||||
-rw-r--r-- | src/lib.rs | 5 |
2 files changed, 31 insertions, 0 deletions
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 @@ | |||
1 | use crate::{ | ||
2 | error::{OBIError, OBIResult}, | ||
3 | CompressionType, Image, | ||
4 | }; | ||
5 | |||
6 | use std::io::Write; | ||
7 | |||
8 | use png::{BitDepth, ColorType, Encoder, Writer}; | ||
9 | |||
10 | pub 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 | } | ||
@@ -4,6 +4,7 @@ | |||
4 | 4 | ||
5 | use std::io; | 5 | use std::io; |
6 | 6 | ||
7 | pub mod convert; | ||
7 | mod decode; | 8 | mod decode; |
8 | mod encode; | 9 | mod encode; |
9 | pub mod error; | 10 | pub 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 | } |