aboutsummaryrefslogtreecommitdiff
path: root/src/encode.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/encode.rs')
-rw-r--r--src/encode.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/encode.rs b/src/encode.rs
new file mode 100644
index 0000000..24d3792
--- /dev/null
+++ b/src/encode.rs
@@ -0,0 +1,34 @@
1use std::io;
2
3use bitvec::prelude::*;
4use bitvec::slice::BitSlice;
5use byteorder::{LittleEndian, WriteBytesExt};
6
7use crate::Image;
8
9pub fn encode_image(obi_image: Image) -> io::Result<Vec<u8>> {
10 let mut obi_data = Vec::with_capacity(obi_image.file_header.file_size as usize);
11
12 // file header
13 let file_header = obi_image.file_header;
14 obi_data.write_u16::<LittleEndian>(file_header.version)?;
15 obi_data.write_u32::<LittleEndian>(file_header.file_size)?;
16 obi_data.write_u32::<LittleEndian>(file_header.data_offset)?;
17
18 // image info header
19 let image_info_header = obi_image.image_info_header;
20 obi_data.write_u32::<LittleEndian>(image_info_header.width)?;
21 obi_data.write_u32::<LittleEndian>(image_info_header.height)?;
22 obi_data.write_u32::<LittleEndian>(image_info_header.compression_type)?;
23 obi_data.write_u32::<LittleEndian>(image_info_header.post_compression_size)?;
24
25 // pixmap data
26 let pixmap = obi_image.data;
27 for byte in pixmap.chunks(8) {
28 let bits_as_u8 = byte.iter().map(|&e| e as u8).collect::<Vec<_>>();
29 let slice = BitSlice::<Lsb0, _>::from_slice(&bits_as_u8).unwrap();
30 obi_data.write_u8(slice.load::<u8>())?;
31 }
32
33 return Ok(obi_data);
34}