diff options
author | Akshay <[email protected]> | 2021-02-26 05:39:31 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-02-26 05:39:31 +0000 |
commit | fd47159bd3ec220c63b497ad659507ce5c54ca54 (patch) | |
tree | b026e48f13b85d2068d1e44cbf70a7d6452f439e /src | |
parent | 7979dd9cc3d2c3c2782cdabc2411b3acf7b7d5f2 (diff) |
add basic types
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 99 |
1 files changed, 99 insertions, 0 deletions
@@ -1,3 +1,102 @@ | |||
1 | #![allow(unreachable_patterns)] | ||
2 | #![allow(non_snake_case)] | ||
3 | #[non_exhaustive] | ||
4 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
5 | pub enum Pixel { | ||
6 | On, | ||
7 | Off, | ||
8 | } | ||
9 | |||
10 | #[non_exhaustive] | ||
11 | #[derive(Copy, Clone, Debug, PartialEq)] | ||
12 | pub enum OBIVersion { | ||
13 | One, | ||
14 | } | ||
15 | |||
16 | impl OBIVersion { | ||
17 | pub fn header_size(&self) -> u32 { | ||
18 | match self { | ||
19 | One => 26, | ||
20 | } | ||
21 | } | ||
22 | } | ||
23 | |||
24 | #[non_exhaustive] | ||
25 | pub struct FileHeader { | ||
26 | pub version: u16, | ||
27 | pub file_size: u32, | ||
28 | pub data_offset: u32, | ||
29 | } | ||
30 | |||
31 | impl FileHeader { | ||
32 | pub fn new(version: OBIVersion, data_size: u32) -> Self { | ||
33 | let header_size = version.header_size(); | ||
34 | Self { | ||
35 | version: match version { | ||
36 | OBIVersion::One => 1u16, | ||
37 | _ => unreachable!("New version has been added!"), | ||
38 | }, | ||
39 | file_size: header_size + data_size, | ||
40 | data_offset: header_size, | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | |||
45 | #[non_exhaustive] | ||
46 | pub struct ImageInfoHeader { | ||
47 | pub width: u32, | ||
48 | pub height: u32, | ||
49 | pub compression_type: u32, | ||
50 | pub post_compression_size: u32, | ||
51 | } | ||
52 | |||
53 | impl ImageInfoHeader { | ||
54 | pub fn new(width: u32, height: u32) -> Self { | ||
55 | Self { | ||
56 | width, | ||
57 | height, | ||
58 | compression_type: 0u32, | ||
59 | post_compression_size: 0u32, | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | |||
64 | #[non_exhaustive] | ||
65 | pub enum CompressionType { | ||
66 | RLE, | ||
67 | Kosinki, | ||
68 | None, | ||
69 | } | ||
70 | |||
71 | impl CompressionType { | ||
72 | pub fn from_u32(kind: u32) -> Self { | ||
73 | match kind { | ||
74 | 0 => CompressionType::None, | ||
75 | 1 => CompressionType::RLE, | ||
76 | 10 => CompressionType::Kosinki, | ||
77 | _ => panic!("Invalid compression algorithm"), | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | |||
82 | pub struct Image { | ||
83 | pub file_header: FileHeader, | ||
84 | pub image_info_header: ImageInfoHeader, | ||
85 | pub data: Vec<Pixel>, | ||
86 | } | ||
87 | |||
88 | impl Image { | ||
89 | pub fn new(width: u32, height: u32) -> Self { | ||
90 | let data_size = width * height; | ||
91 | let data = vec![Pixel::Off; data_size as usize]; | ||
92 | Self { | ||
93 | file_header: FileHeader::new(OBIVersion::One, data_size), | ||
94 | image_info_header: ImageInfoHeader::new(width, height), | ||
95 | data, | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | |||
1 | #[cfg(test)] | 100 | #[cfg(test)] |
2 | mod tests { | 101 | mod tests { |
3 | #[test] | 102 | #[test] |