add ImageMut trait

png-branch
an 2019-03-18 08:18:46 -04:00
parent 7d02f6460f
commit 422a7989f5
3 changed files with 58 additions and 3 deletions

View File

@ -388,6 +388,7 @@ Map tags:
| `ambi` | Array of Ambient Sound |
| `bonk` | Array of Random Sound |
| `term` | Array of Terminal |
| `NAME` | Unknown |
| `påth` | Unused, supposed to be guardpaths (å is $8C) |
| `door` | Unused, supposed to be extra door data |

View File

@ -65,6 +65,8 @@ pub trait Image
/// Returns the color of the pixel at column `x` at row `y`.
///
/// # Panics
///
/// Panics if `x` is greater than the width of the image or `y` is greater
/// than the height of the image.
fn index(&self, x: usize, y: usize) -> &Self::Output;
@ -80,6 +82,28 @@ pub trait Image
}
}
/// A generic color matrix image, which may be mutated.
pub trait ImageMut: Image
{
/// Returns the color of the pixel at column `x` at row `y`.
///
/// # Panics
///
/// Panics if `x` is greater than the width of the image or `y` is greater
/// than the height of the image.
fn index_mut(&mut self, x: usize, y: usize) -> &mut Self::Output;
/// The same as `index_mut`, but will not panic if out of bounds.
fn get_mut(&mut self, x: usize, y: usize) -> Option<&mut Self::Output>
{
if x < self.w() && y < self.h() {
Some(self.index_mut(x, y))
} else {
None
}
}
}
/// Any color which may be represented as RGBA16.
pub trait Color: Sized + Copy + Clone + Eq + PartialEq
{
@ -98,11 +122,17 @@ pub trait Color: Sized + Copy + Clone + Eq + PartialEq
impl Image16
{
/// Creates a new Image16.
/// Creates a new Image16 with no canvas.
pub fn new(w: usize, h: usize) -> Self
{
Self{w, h, cr: Vec::with_capacity(w * h)}
}
/// Creates a new Image16 with an empty canvas.
pub fn new_empty(w: usize, h: usize) -> Self
{
Self{w, h, cr: vec![Color16::new(0, 0, 0); w * h]}
}
}
impl Image for Image16
@ -118,13 +148,27 @@ impl Image for Image16
}
}
impl ImageMut for Image16
{
fn index_mut(&mut self, x: usize, y: usize) -> &mut Self::Output
{
&mut self.cr[x + y * self.w]
}
}
impl Image8
{
/// Creates a new Image8.
/// Creates a new Image8 with no canvas.
pub fn new(w: usize, h: usize) -> Self
{
Self{w, h, cr: Vec::with_capacity(w * h)}
}
/// Creates a new Image8 with an empty canvas.
pub fn new_empty(w: usize, h: usize) -> Self
{
Self{w, h, cr: vec![Color8::new(0, 0, 0); w * h]}
}
}
impl Image for Image8
@ -140,6 +184,14 @@ impl Image for Image8
}
}
impl ImageMut for Image8
{
fn index_mut(&mut self, x: usize, y: usize) -> &mut Self::Output
{
&mut self.cr[x + y * self.w]
}
}
impl Color16
{
pub const fn new(r: u16, g: u16, b: u16) -> Self {Self(r, g, b)}

View File

@ -11,7 +11,9 @@ pub trait Sound
/// Returns the `n`th sample.
///
/// May panic if `n` exceeds the length of this sound.
/// # Panics
///
/// Panics if `n` exceeds the length of this sound.
fn index(&self, n: usize) -> i16;
/// Returns the number of the first sample of the loop, or `0`.