aboutsummaryrefslogtreecommitdiff
path: root/src/encode.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/encode.rs')
-rw-r--r--src/encode.rs47
1 files changed, 34 insertions, 13 deletions
diff --git a/src/encode.rs b/src/encode.rs
index e5760a8..a8c58e0 100644
--- a/src/encode.rs
+++ b/src/encode.rs
@@ -1,34 +1,55 @@
1use std::io; 1use std::borrow::Borrow;
2 2
3use bitvec::{prelude::*, vec::BitVec}; 3use bitvec::{prelude::*, vec::BitVec};
4use byteorder::{LittleEndian, WriteBytesExt}; 4use byteorder::{LittleEndian, WriteBytesExt};
5 5
6use crate::error::{OBIError, OBIResult};
6use crate::Image; 7use crate::Image;
7 8
8pub fn encode_image(obi_image: Image) -> io::Result<Vec<u8>> { 9pub fn encode_image<I>(obi_image: I) -> OBIResult<Vec<u8>>
10where
11 I: Borrow<Image>,
12{
13 let obi_image = obi_image.borrow();
9 let mut obi_data = Vec::with_capacity(obi_image.file_header.file_size as usize); 14 let mut obi_data = Vec::with_capacity(obi_image.file_header.file_size as usize);
10 15
11 // file header 16 // file header
12 let file_header = obi_image.file_header; 17 let file_header = &obi_image.file_header;
13 obi_data.write_u16::<LittleEndian>(file_header.version)?; 18 obi_data
14 obi_data.write_u32::<LittleEndian>(file_header.file_size)?; 19 .write_u16::<LittleEndian>(file_header.version)
15 obi_data.write_u32::<LittleEndian>(file_header.data_offset)?; 20 .map_err(|_| OBIError::Encode)?;
21 obi_data
22 .write_u32::<LittleEndian>(file_header.file_size)
23 .map_err(|_| OBIError::Encode)?;
24 obi_data
25 .write_u32::<LittleEndian>(file_header.data_offset)
26 .map_err(|_| OBIError::Encode)?;
16 27
17 // image info header 28 // image info header
18 let image_info_header = obi_image.image_info_header; 29 let image_info_header = &obi_image.image_info_header;
19 obi_data.write_u32::<LittleEndian>(image_info_header.width)?; 30 obi_data
20 obi_data.write_u32::<LittleEndian>(image_info_header.height)?; 31 .write_u32::<LittleEndian>(image_info_header.width)
21 obi_data.write_u32::<LittleEndian>(image_info_header.compression_type)?; 32 .map_err(|_| OBIError::Encode)?;
22 obi_data.write_u32::<LittleEndian>(image_info_header.post_compression_size)?; 33 obi_data
34 .write_u32::<LittleEndian>(image_info_header.height)
35 .map_err(|_| OBIError::Encode)?;
36 obi_data
37 .write_u32::<LittleEndian>(image_info_header.compression_type)
38 .map_err(|_| OBIError::Encode)?;
39 obi_data
40 .write_u32::<LittleEndian>(image_info_header.post_compression_size)
41 .map_err(|_| OBIError::Encode)?;
23 42
24 // pixmap data 43 // pixmap data
25 let pixmap = obi_image.data; 44 let pixmap = &obi_image.data;
26 for byte in pixmap.chunks(8) { 45 for byte in pixmap.chunks(8) {
27 let mut bv = BitVec::<Lsb0, u8>::new(); 46 let mut bv = BitVec::<Lsb0, u8>::new();
28 for &b in byte { 47 for &b in byte {
29 bv.push(b); 48 bv.push(b);
30 } 49 }
31 obi_data.write_u8(bv.load::<u8>())?; 50 obi_data
51 .write_u8(bv.load::<u8>())
52 .map_err(|_| OBIError::Encode)?;
32 } 53 }
33 54
34 return Ok(obi_data); 55 return Ok(obi_data);