Maraiah/src/durandal/image.rs

76 lines
1.2 KiB
Rust
Raw Normal View History

2018-09-06 09:01:52 -07:00
//! Image and color representations.
use std::ops::{Index, IndexMut};
/// RGBA8 color.
2019-02-04 21:12:10 -08:00
#[derive(Clone, Debug, PartialEq)]
2018-09-06 09:01:52 -07:00
pub struct Color
{
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
/// Image with width and height.
pub struct Image
{
2019-02-08 21:53:27 -08:00
w: usize,
h: usize,
2018-09-09 15:17:36 -07:00
pub cr: Vec<Color>,
2018-09-06 09:01:52 -07:00
}
impl Color
{
/// Converts a R5G5B5 format color to RGBA8.
pub fn from_r5g5b5(rgb: u16) -> Color
{
2018-09-09 15:17:36 -07:00
let r = rgb >> 10 & 0x1f;
2019-02-08 21:53:27 -08:00
let g = rgb >> 5 & 0x1f;
let b = rgb & 0x1f;
2018-09-09 15:17:36 -07:00
Color{r: (r << 3 | r >> 2) as u8,
g: (g << 3 | g >> 2) as u8,
b: (b << 3 | b >> 2) as u8,
2018-09-06 09:01:52 -07:00
a: 255}
}
}
impl Image
{
/// Creates a new Image structure.
pub fn new(w: usize, h: usize) -> Image
2019-02-08 21:53:27 -08:00
{
Image{w, h, cr: Vec::with_capacity(w * h)}
}
pub fn w(&self) -> usize
{
self.w
}
2018-09-06 09:01:52 -07:00
2019-02-08 21:53:27 -08:00
pub fn h(&self) -> usize
{
self.h
}
2018-09-06 09:01:52 -07:00
}
impl Index<(usize, usize)> for Image
{
type Output = Color;
fn index(&self, (x, y): (usize, usize)) -> &Color
2019-02-08 21:53:27 -08:00
{
&self.cr[x + y * self.w]
}
2018-09-06 09:01:52 -07:00
}
impl IndexMut<(usize, usize)> for Image
{
fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Color
2019-02-08 21:53:27 -08:00
{
&mut self.cr[x + y * self.w]
}
2018-09-06 09:01:52 -07:00
}
// EOF