diff options
Diffstat (limited to 'src/decode.rs')
-rw-r--r-- | src/decode.rs | 69 |
1 files changed, 53 insertions, 16 deletions
diff --git a/src/decode.rs b/src/decode.rs index a9abd37..9c6a065 100644 --- a/src/decode.rs +++ b/src/decode.rs | |||
@@ -4,7 +4,7 @@ use bitvec::prelude::*; | |||
4 | use byteorder::{LittleEndian, ReadBytesExt}; | 4 | use byteorder::{LittleEndian, ReadBytesExt}; |
5 | 5 | ||
6 | use crate::error::{OBIError, OBIResult}; | 6 | use crate::error::{OBIError, OBIResult}; |
7 | use crate::{FileHeader, Image, ImageInfoHeader}; | 7 | use crate::{CompressionType, FileHeader, Image, ImageInfoHeader}; |
8 | 8 | ||
9 | pub fn decode_image(obi_data: &mut Cursor<Vec<u8>>) -> OBIResult<Image> { | 9 | pub fn decode_image(obi_data: &mut Cursor<Vec<u8>>) -> OBIResult<Image> { |
10 | // file header | 10 | // file header |
@@ -43,22 +43,59 @@ pub fn decode_image(obi_data: &mut Cursor<Vec<u8>>) -> OBIResult<Image> { | |||
43 | post_compression_size, | 43 | post_compression_size, |
44 | }; | 44 | }; |
45 | 45 | ||
46 | // pixmap data | 46 | let data: Vec<bool> = match CompressionType::from_u32(compression_type) { |
47 | let mut data_bytes = vec![]; | 47 | CompressionType::RLE => { |
48 | obi_data | 48 | let mut rest = vec![]; |
49 | .read_to_end(&mut data_bytes) | 49 | let mut lengths = vec![]; |
50 | .map_err(|_| OBIError::Decode)?; | 50 | loop { |
51 | let data = data_bytes | 51 | let l = obi_data |
52 | .iter() | 52 | .read_u32::<LittleEndian>() |
53 | .map(|&b| { | 53 | .map_err(|_| OBIError::Encode)?; |
54 | BitVec::<Lsb0, u8>::from_element(b) | 54 | if l == 0 { |
55 | .into_iter() | 55 | break; |
56 | .map(|e| e as bool) | 56 | } |
57 | .collect::<Vec<bool>>() | 57 | lengths.push(l); |
58 | }) | 58 | } |
59 | .flatten() | 59 | obi_data |
60 | .collect::<Vec<_>>(); | 60 | .read_to_end(&mut rest) |
61 | .map_err(|_| OBIError::Decode)?; | ||
62 | let data_points = rest | ||
63 | .iter() | ||
64 | .map(|&b| { | ||
65 | BitVec::<Lsb0, u8>::from_element(b) | ||
66 | .into_iter() | ||
67 | .map(|e| e as bool) | ||
68 | .collect::<Vec<bool>>() | ||
69 | }) | ||
70 | .flatten() | ||
71 | .collect::<Vec<bool>>(); | ||
61 | 72 | ||
73 | let data = data_points | ||
74 | .into_iter() | ||
75 | .zip(lengths) | ||
76 | .map(|(d, l)| vec![d; l as usize]) | ||
77 | .flatten() | ||
78 | .collect::<Vec<bool>>(); | ||
79 | data | ||
80 | } | ||
81 | _ => { | ||
82 | let mut rest = vec![]; | ||
83 | obi_data | ||
84 | .read_to_end(&mut rest) | ||
85 | .map_err(|_| OBIError::Decode)?; | ||
86 | let data_points = rest | ||
87 | .iter() | ||
88 | .map(|&b| { | ||
89 | BitVec::<Lsb0, u8>::from_element(b) | ||
90 | .into_iter() | ||
91 | .map(|e| e as bool) | ||
92 | .collect::<Vec<bool>>() | ||
93 | }) | ||
94 | .flatten() | ||
95 | .collect::<Vec<_>>(); | ||
96 | data_points | ||
97 | } | ||
98 | }; | ||
62 | return Ok(Image { | 99 | return Ok(Image { |
63 | file_header, | 100 | file_header, |
64 | image_info_header, | 101 | image_info_header, |