diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 32 |
1 files changed, 18 insertions, 14 deletions
@@ -1,11 +1,7 @@ | |||
1 | #![allow(unreachable_patterns)] | 1 | #![allow(unreachable_patterns)] |
2 | #![allow(non_snake_case)] | 2 | #![allow(non_snake_case)] |
3 | #[non_exhaustive] | 3 | |
4 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | 4 | pub mod encode; |
5 | pub enum Pixel { | ||
6 | On, | ||
7 | Off, | ||
8 | } | ||
9 | 5 | ||
10 | #[non_exhaustive] | 6 | #[non_exhaustive] |
11 | #[derive(Copy, Clone, Debug, PartialEq)] | 7 | #[derive(Copy, Clone, Debug, PartialEq)] |
@@ -16,16 +12,16 @@ pub enum OBIVersion { | |||
16 | impl OBIVersion { | 12 | impl OBIVersion { |
17 | pub fn header_size(&self) -> u32 { | 13 | pub fn header_size(&self) -> u32 { |
18 | match self { | 14 | match self { |
19 | One => 26, | 15 | OBIVersion::One => 26, |
20 | } | 16 | } |
21 | } | 17 | } |
22 | } | 18 | } |
23 | 19 | ||
24 | #[non_exhaustive] | 20 | #[non_exhaustive] |
25 | pub struct FileHeader { | 21 | pub struct FileHeader { |
26 | pub version: u16, | ||
27 | pub file_size: u32, | 22 | pub file_size: u32, |
28 | pub data_offset: u32, | 23 | pub data_offset: u32, |
24 | pub version: u16, | ||
29 | } | 25 | } |
30 | 26 | ||
31 | impl FileHeader { | 27 | impl FileHeader { |
@@ -82,15 +78,19 @@ impl CompressionType { | |||
82 | pub struct Image { | 78 | pub struct Image { |
83 | pub file_header: FileHeader, | 79 | pub file_header: FileHeader, |
84 | pub image_info_header: ImageInfoHeader, | 80 | pub image_info_header: ImageInfoHeader, |
85 | pub data: Vec<Pixel>, | 81 | pub data: Vec<bool>, |
86 | } | 82 | } |
87 | 83 | ||
88 | impl Image { | 84 | impl Image { |
89 | pub fn new(width: u32, height: u32) -> Self { | 85 | pub fn new(width: u32, height: u32) -> Self { |
90 | let data_size = width * height; | 86 | // round to the nearest multiple of 8 |
91 | let data = vec![Pixel::Off; data_size as usize]; | 87 | // convert to number of bytes by dividing by 8 |
88 | let mut data_size = width * height + 7; | ||
89 | data_size = data_size - (data_size % 8); | ||
90 | let data = vec![false; data_size as usize]; | ||
91 | |||
92 | Self { | 92 | Self { |
93 | file_header: FileHeader::new(OBIVersion::One, data_size), | 93 | file_header: FileHeader::new(OBIVersion::One, data_size / 8), |
94 | image_info_header: ImageInfoHeader::new(width, height), | 94 | image_info_header: ImageInfoHeader::new(width, height), |
95 | data, | 95 | data, |
96 | } | 96 | } |
@@ -99,8 +99,12 @@ impl Image { | |||
99 | 99 | ||
100 | #[cfg(test)] | 100 | #[cfg(test)] |
101 | mod tests { | 101 | mod tests { |
102 | use super::*; | ||
103 | use std::mem::size_of; | ||
104 | |||
102 | #[test] | 105 | #[test] |
103 | fn it_works() { | 106 | fn size_of_image_info_header() { |
104 | assert_eq!(2 + 2, 4); | 107 | let file_header_size = size_of::<ImageInfoHeader>(); |
108 | assert_eq!(16, file_header_size); | ||
105 | } | 109 | } |
106 | } | 110 | } |