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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use super::utils::*;
pub struct Header {
file_type: String,
file_size: u32,
res1: u8,
res2: u8,
pixel_data_offset: u8,
header_size: u8,
pub image_width: u32,
pub image_height: u32,
planes: u8,
bits_per_pixel: u8,
compression: u8,
image_size: u32,
x_per_meter: u8,
y_per_meter: u8,
color_map_entries_used: u8,
sig_colors: u8
}
impl Header {
pub fn new(
image_width: u32, image_height: u32, image_size: u32) -> Header {
Header {
file_type: String::from("BM"), file_size: 0, res1: 0, res2: 0,
pixel_data_offset: 54, header_size: 40, image_width: image_width,
image_height: image_height, planes: 1, bits_per_pixel: 24,
compression: 0, image_size: image_size, x_per_meter: 0,
y_per_meter: 0, color_map_entries_used: 0, sig_colors: 0
}
}
pub fn get_binary_data(&self) -> Vec<u8>{
let mut bdata: Vec<u8> = vec!();
bdata.push(self.file_type.chars().nth(0).unwrap() as u8);
bdata.push(self.file_type.chars().nth(1).unwrap() as u8);
for _i in 0..4 {
bdata.push(self.file_size as u8);
}
for _i in 0..2 {
bdata.push(self.res1);
}
for _i in 0..2 {
bdata.push(self.res2);
}
for i in 0..4 {
if i == 0 {
bdata.push(self.pixel_data_offset);
}
else {
bdata.push(0);
}
}
for i in 0..4 {
if i == 0 {
bdata.push(self.header_size);
}
else {
bdata.push(0);
}
}
let width = transform_u32_to_array_of_u8(self.image_width);
for i in 0..4 {
bdata.push(width[i]);
}
let height = transform_u32_to_array_of_u8(self.image_height);
for i in 0..4 {
bdata.push(height[i]);
}
for i in 0..2 {
if i == 0 {
bdata.push(self.planes);
}
else {
bdata.push(0);
}
}
for i in 0..2 {
if i == 0 {
bdata.push(self.bits_per_pixel);
}
else {
bdata.push(0);
}
}
for i in 0..4 {
if i == 0 {
bdata.push(self.compression);
}
else {
bdata.push(0);
}
}
let image_size = transform_u32_to_array_of_u8(self.image_size);
for i in 0..4 {
bdata.push(image_size[i]);
}
for i in 0..4 {
if i == 0 {
bdata.push(self.x_per_meter);
}
else {
bdata.push(0);
}
}
for i in 0..4 {
if i == 0 {
bdata.push(self.y_per_meter);
}
else {
bdata.push(0);
}
}
for i in 0..4 {
if i == 0 {
bdata.push(self.color_map_entries_used);
}
else {
bdata.push(0);
}
}
for i in 0..4 {
if i == 0 {
bdata.push(self.sig_colors);
}
else {
bdata.push(0);
}
}
bdata
}
}