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
use std::io::Write;
pub mod pixel;
pub mod header;
use header::Header;
use pixel::Pixel;
pub mod utils;
use utils::*;
mod rotate;
use rotate::rotate;
pub struct Img {
header: Header,
pub pixels: Vec<Vec<Pixel>>
}
impl Img {
pub fn new(pixels: Vec<Vec<Pixel>>) -> Img {
let height = pixels.len() as u32;
let width = pixels[0].len() as u32;
let header = Header::new(width, height, width * height);
Img {
header: header, pixels: pixels
}
}
pub fn get_binary_data(&self) -> Vec<u8>{
let mut data = self.header.get_binary_data();
for i in (0..self.pixels.len()).rev(){
for pixel in self.pixels[i].iter() {
data.push(pixel.b);
data.push(pixel.g);
data.push(pixel.r);
}
for _i in 0..(4 - (self.pixels[0].len() as u32 * 3) % 4){
if (self.pixels[0].len() as u32 * 3) % 4 == 0 {
break;
}
data.push(0);
}
}
let mut size = transform_u32_to_array_of_u8(data.len() as u32);
for i in 0..4 {
data[i+2] = size[i];
}
size = transform_u32_to_array_of_u8(self.pixels.len() as u32 * ((self.pixels[0].len() as u32 * 3 + 7) & !7) as u32 / 16);
for i in 0..4 {
data[i+34] = size[i];
}
data
}
pub fn write_image(&self, path: &str){
if !path.contains(".bmp") {
panic!("I am not a bmp image!");
}
let width = self.pixels[0].len();
for row in self.pixels.iter(){
if row.len() != width {
panic!("The pixel array does not have equal row sizes!")
}
}
let mut data = std::fs::File::create(path).unwrap();
let img = Img::new(self.pixels.clone());
let bdata = img.get_binary_data();
data.write(&bdata).unwrap();
}
pub fn rotate(&mut self){
self.pixels = rotate(&mut self.pixels);
}
}