60 lines
1.1 KiB
Rust
60 lines
1.1 KiB
Rust
//! Image and color representations.
|
|
|
|
use std::ops::{Index, IndexMut};
|
|
|
|
/// RGBA8 color.
|
|
#[derive(Clone)]
|
|
pub struct Color
|
|
{
|
|
pub r: u8,
|
|
pub g: u8,
|
|
pub b: u8,
|
|
pub a: u8,
|
|
}
|
|
|
|
/// Image with width and height.
|
|
pub struct Image
|
|
{
|
|
w: usize,
|
|
h: usize,
|
|
cr: Vec<Color>,
|
|
}
|
|
|
|
impl Color
|
|
{
|
|
/// Converts a R5G5B5 format color to RGBA8.
|
|
pub fn from_r5g5b5(rgb: u16) -> Color
|
|
{
|
|
Color{r: (rgb >> 10 ) as u8 * 8,
|
|
g: (rgb >> 5 & 31) as u8 * 8,
|
|
b: (rgb & 31) as u8 * 8,
|
|
a: 255}
|
|
}
|
|
}
|
|
|
|
impl Image
|
|
{
|
|
/// Creates a new Image structure.
|
|
pub fn new(w: usize, h: usize) -> Image
|
|
{Image{w, h, cr: Vec::with_capacity(w as usize * h as usize)}}
|
|
|
|
pub fn w(&self) -> usize {self.w}
|
|
pub fn h(&self) -> usize {self.h}
|
|
}
|
|
|
|
impl Index<(usize, usize)> for Image
|
|
{
|
|
type Output = Color;
|
|
|
|
fn index(&self, (x, y): (usize, usize)) -> &Color
|
|
{&self.cr[x + y * self.w]}
|
|
}
|
|
|
|
impl IndexMut<(usize, usize)> for Image
|
|
{
|
|
fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Color
|
|
{&mut self.cr[x + y * self.w]}
|
|
}
|
|
|
|
// EOF
|