Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------------
- // Pixel
- //------------------------------------------
- pub trait Pixel {
- fn to_u32(&self) -> u32;
- }
- impl Pixel for u32 {
- fn to_u32(&self) -> u32 {
- *self
- }
- }
- //------------------------------------------
- // Rgb
- //------------------------------------------
- pub struct Rgb {
- r: u8,
- g: u8,
- b: u8
- }
- impl Rgb {
- pub fn new(r: u8, g: u8, b: u8) -> Rgb {
- Rgb {
- r: r,
- g: g,
- b: b
- }
- }
- }
- impl Pixel for Rgb {
- fn to_u32(&self) -> u32 {
- ((self.r as u32) << 24) +
- ((self.g as u32) << 16) +
- ((self.b as u32) << 8)
- }
- }
- //------------------------------------------
- // Rgba
- //------------------------------------------
- pub struct Rgba {
- r: u8,
- g: u8,
- b: u8,
- a: u8
- }
- impl Rgba {
- pub fn new(r: u8, g: u8, b: u8, a: u8) -> Rgba {
- Rgba {
- r: r,
- g: g,
- b: b,
- a: a
- }
- }
- }
- impl Pixel for Rgba {
- fn to_u32(&self) -> u32 {
- ((self.r as u32) << 24) +
- ((self.g as u32) << 16) +
- ((self.b as u32) << 8) +
- self.a as u32
- }
- }
- //------------------------------------------
- // PixelBuffer
- //------------------------------------------
- pub struct PixelBuffer {
- pub _width : usize,
- pub _height : usize,
- pub _pixels : Vec<u32>,
- pub _depth : Vec<f32>,
- pub _stencil : Vec<u8>
- }
- impl PixelBuffer {
- pub fn new(width: usize, height: usize) -> PixelBuffer {
- PixelBuffer {
- _width : width,
- _height : height,
- _pixels : vec![0_u32; width * height],
- _depth : vec![0_f32; width * height],
- _stencil : vec![0_u8; width * height]
- }
- }
- pub fn width(&self) -> usize {
- self._width
- }
- pub fn height(&self) -> usize {
- self._height
- }
- pub fn clear<P: Pixel> (&mut self, p: P) {
- for i in 0..self._pixels.len() {
- self._pixels[i] = p.to_u32()
- }
- }
- pub fn set<P: Pixel>(&mut self, x: usize, y: usize, p: P) {
- self._pixels[x + self._width * y] = p.to_u32();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement