77 lines
1.4 KiB
Rust
77 lines
1.4 KiB
Rust
//#[derive(Copy, Clone)]
|
|
struct RGB {
|
|
red: u8,
|
|
green: u8,
|
|
blue: u8,
|
|
}
|
|
|
|
impl RGB {
|
|
fn new(red: u8, green: u8, blue: u8) -> RGB {
|
|
RGB { red, green, blue }
|
|
}
|
|
|
|
fn black() -> RGB {
|
|
RGB::new(0, 0, 0)
|
|
}
|
|
|
|
fn white() -> RGB {
|
|
RGB::new(255, 255, 255)
|
|
}
|
|
}
|
|
|
|
struct Image {
|
|
width: usize,
|
|
height: usize,
|
|
data: Vec<RGB>,
|
|
}
|
|
|
|
impl Image {
|
|
fn new(width: usize, height: usize, data: Vec<RGB>) -> Image {
|
|
if width * height != data.len() {
|
|
panic!("Width and height of image does not match amount of data.");
|
|
}
|
|
|
|
let img = Image {
|
|
width,
|
|
height,
|
|
data,
|
|
};
|
|
|
|
img
|
|
}
|
|
|
|
fn white_image(width: usize, height: usize) -> Image {
|
|
let mut data = Vec::new();
|
|
|
|
for i in 0..width * height {
|
|
data.push(RGB::white());
|
|
}
|
|
|
|
Image::new(width, height, data)
|
|
}
|
|
|
|
fn get(&self, x: usize, y: usize) -> &RGB {
|
|
self.data.get(y * self.width + x).unwrap()
|
|
}
|
|
|
|
fn get_mut(&mut self, x: usize, y: usize) -> &mut RGB {
|
|
self.data.get_mut(y * self.width + x).unwrap()
|
|
}
|
|
}
|
|
|
|
fn manipulate_first_pixel(image: &mut Image) {
|
|
let c = image.get_mut(0, 0);
|
|
|
|
c.red = 0;
|
|
}
|
|
|
|
fn main() {
|
|
let mut image = Image::white_image(640, 480);
|
|
|
|
manipulate_first_pixel(&mut image);
|
|
|
|
let c = image.get(0, 0);
|
|
|
|
println!("red = {}, green = {}, blue = {}", c.red, c.green, c.blue)
|
|
}
|