aboutsummaryrefslogtreecommitdiff
path: root/src/encode.rs
blob: 58c455e937d7f1ac31ec4c714cd67ea6e6c370d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::borrow::Borrow;

use bitvec::{prelude::*, vec::BitVec};
use byteorder::{LittleEndian, WriteBytesExt};

use crate::{
    error::{OBIError, OBIResult},
    rle::RLE,
    CompressionType, Image,
};

pub fn encode_image<I>(obi_image: I) -> OBIResult<Vec<u8>>
where
    I: Borrow<Image>,
{
    let obi_image = obi_image.borrow();
    let mut obi_data = Vec::with_capacity(obi_image.file_header.file_size as usize);

    // file header
    let file_header = &obi_image.file_header;
    obi_data
        .write_u16::<LittleEndian>(file_header.version)
        .map_err(|_| OBIError::Encode)?;
    obi_data
        .write_u32::<LittleEndian>(file_header.file_size)
        .map_err(|_| OBIError::Encode)?;
    obi_data
        .write_u32::<LittleEndian>(file_header.data_offset)
        .map_err(|_| OBIError::Encode)?;

    // image info header
    let image_info_header = &obi_image.image_info_header;
    obi_data
        .write_u32::<LittleEndian>(image_info_header.width)
        .map_err(|_| OBIError::Encode)?;
    obi_data
        .write_u32::<LittleEndian>(image_info_header.height)
        .map_err(|_| OBIError::Encode)?;
    obi_data
        .write_u32::<LittleEndian>(image_info_header.compression_type)
        .map_err(|_| OBIError::Encode)?;
    obi_data
        .write_u32::<LittleEndian>(image_info_header.post_compression_size)
        .map_err(|_| OBIError::Encode)?;

    let write_pixel_data = |pixels: &Vec<bool>, obi_data: &mut Vec<u8>| -> OBIResult<()> {
        for byte in pixels.chunks(8) {
            let mut bv = BitVec::<u8>::new();
            for &b in byte {
                bv.push(b)
            }
            obi_data
                .write_u8(bv.load::<u8>())
                .map_err(|_| OBIError::Encode)?;
        }
        Ok(())
    };

    // pixmap data
    let pixmap = &obi_image.data;
    match CompressionType::from_u32(obi_image.image_info_header.compression_type) {
        CompressionType::RLE => {
            let (data_points, lengths): (Vec<_>, Vec<_>) = pixmap.compress().into_iter().unzip();
            for l in lengths {
                obi_data
                    .write_u32::<LittleEndian>(l as u32)
                    .map_err(|_| OBIError::Encode)?;
            }
            // end length sequence with zero
            obi_data
                .write_u32::<LittleEndian>(0)
                .map_err(|_| OBIError::Encode)?;
            // begin data point sequence
            write_pixel_data(&data_points, &mut obi_data)?;
        }
        _ => write_pixel_data(pixmap, &mut obi_data)?,
    };

    Ok(obi_data)
}