Use Array2D as storage for ImageRgbF

This commit is contained in:
Matthew Gordon 2020-09-01 22:44:55 -04:00
parent ef0fb96f9d
commit 7c8baf57da
3 changed files with 10 additions and 31 deletions

View File

@ -2,7 +2,7 @@ use crate::math::Vec3;
use std::ops::{Add, Mul}; use std::ops::{Add, Mul};
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug, Default)]
pub struct ColourRgbF { pub struct ColourRgbF {
values: Vec3, values: Vec3,
} }

View File

@ -4,7 +4,6 @@ use std::io::BufWriter;
use std::path::Path; use std::path::Path;
use crate::colour::{ColourRgbF, ColourRgbU8}; use crate::colour::{ColourRgbF, ColourRgbU8};
use crate::math::Vec3;
use crate::util::Array2D; use crate::util::Array2D;
pub struct ImageRgbU8 { pub struct ImageRgbU8 {
@ -68,59 +67,39 @@ impl ImageRgbU8 {
} }
pub struct ImageRgbF { pub struct ImageRgbF {
pixel_data: Vec<f64>, data: Array2D<ColourRgbF>,
width: usize,
height: usize,
} }
impl ImageRgbF { impl ImageRgbF {
pub fn new(width: usize, height: usize) -> ImageRgbF { pub fn new(width: usize, height: usize) -> ImageRgbF {
ImageRgbF { ImageRgbF {
width, data: Array2D::new(height, width),
height,
pixel_data: vec![0.0; width * height * 3 as usize],
} }
} }
pub fn clear(&mut self) -> &mut ImageRgbF { pub fn clear(&mut self) {
for elem in self.pixel_data.iter_mut() { self.data.clear();
*elem = 0.0;
}
self
} }
pub fn get_colour(&self, row: usize, column: usize) -> ColourRgbF { pub fn get_colour(&self, row: usize, column: usize) -> ColourRgbF {
assert!(row < self.height && column < self.width); self.data[row][column]
let index = self.calculate_index(row, column);
ColourRgbF::from_vec3(&Vec3::from_slice(&self.pixel_data[index..index + 3]))
} }
pub fn set_colour(&mut self, row: usize, column: usize, colour: ColourRgbF) { pub fn set_colour(&mut self, row: usize, column: usize, colour: ColourRgbF) {
assert!(row < self.height && column < self.width); self.data[row][column] = colour;
let index = self.calculate_index(row, column);
self.pixel_data[index..index + 3].copy_from_slice(&colour.as_vec3().as_slice());
}
pub fn get_pixel_data(&self) -> &Vec<f64> {
&self.pixel_data
} }
pub fn get_width(&self) -> usize { pub fn get_width(&self) -> usize {
self.width self.data.get_width()
} }
pub fn get_height(&self) -> usize { pub fn get_height(&self) -> usize {
self.height self.data.get_height()
} }
pub fn num_channels() -> usize { pub fn num_channels() -> usize {
3 3
} }
fn calculate_index(&self, row: usize, column: usize) -> usize {
assert!(row < self.height && column < self.width);
(((self.height - (row + 1)) * self.width + column) * Self::num_channels()) as usize
}
} }
pub trait NormalizedAsByte { pub trait NormalizedAsByte {

View File

@ -4,7 +4,7 @@ use itertools::izip;
use std::ops::{Add, AddAssign, Index, IndexMut, Mul, MulAssign, Neg, Sub, SubAssign}; use std::ops::{Add, AddAssign, Index, IndexMut, Mul, MulAssign, Neg, Sub, SubAssign};
#[derive(Copy, Clone, PartialEq, Debug)] #[derive(Copy, Clone, PartialEq, Debug, Default)]
pub struct Vec3 { pub struct Vec3 {
pub coords: [f64; 3], pub coords: [f64; 3],
} }