aboutsummaryrefslogtreecommitdiff
path: root/src/decode.rs
blob: 9c6a06514205a9c5ace3075d3a01f38fc9c64c77 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::io::{Cursor, Read};

use bitvec::prelude::*;
use byteorder::{LittleEndian, ReadBytesExt};

use crate::error::{OBIError, OBIResult};
use crate::{CompressionType, FileHeader, Image, ImageInfoHeader};

pub fn decode_image(obi_data: &mut Cursor<Vec<u8>>) -> OBIResult<Image> {
    // file header
    let version = obi_data
        .read_u16::<LittleEndian>()
        .map_err(|_| OBIError::Decode)?;
    let file_size = obi_data
        .read_u32::<LittleEndian>()
        .map_err(|_| OBIError::Decode)?;
    let data_offset = obi_data
        .read_u32::<LittleEndian>()
        .map_err(|_| OBIError::Decode)?;
    let file_header = FileHeader {
        version,
        file_size,
        data_offset,
    };

    // image info header
    let width = obi_data
        .read_u32::<LittleEndian>()
        .map_err(|_| OBIError::Decode)?;
    let height = obi_data
        .read_u32::<LittleEndian>()
        .map_err(|_| OBIError::Decode)?;
    let compression_type = obi_data
        .read_u32::<LittleEndian>()
        .map_err(|_| OBIError::Decode)?;
    let post_compression_size = obi_data
        .read_u32::<LittleEndian>()
        .map_err(|_| OBIError::Decode)?;
    let image_info_header = ImageInfoHeader {
        width,
        height,
        compression_type,
        post_compression_size,
    };

    let data: Vec<bool> = match CompressionType::from_u32(compression_type) {
        CompressionType::RLE => {
            let mut rest = vec![];
            let mut lengths = vec![];
            loop {
                let l = obi_data
                    .read_u32::<LittleEndian>()
                    .map_err(|_| OBIError::Encode)?;
                if l == 0 {
                    break;
                }
                lengths.push(l);
            }
            obi_data
                .read_to_end(&mut rest)
                .map_err(|_| OBIError::Decode)?;
            let data_points = rest
                .iter()
                .map(|&b| {
                    BitVec::<Lsb0, u8>::from_element(b)
                        .into_iter()
                        .map(|e| e as bool)
                        .collect::<Vec<bool>>()
                })
                .flatten()
                .collect::<Vec<bool>>();

            let data = data_points
                .into_iter()
                .zip(lengths)
                .map(|(d, l)| vec![d; l as usize])
                .flatten()
                .collect::<Vec<bool>>();
            data
        }
        _ => {
            let mut rest = vec![];
            obi_data
                .read_to_end(&mut rest)
                .map_err(|_| OBIError::Decode)?;
            let data_points = rest
                .iter()
                .map(|&b| {
                    BitVec::<Lsb0, u8>::from_element(b)
                        .into_iter()
                        .map(|e| e as bool)
                        .collect::<Vec<bool>>()
                })
                .flatten()
                .collect::<Vec<_>>();
            data_points
        }
    };
    return Ok(Image {
        file_header,
        image_info_header,
        data,
    });
}