Trait retro_pixel::WritableImage [−][src]
pub trait WritableImage<P>: ReadableImage<P> + IndexMut<(usize, usize), Output = P> { fn as_mut_ptr(&mut self) -> *mut P; fn get_mut(&mut self, loc: (usize, usize)) -> Option<&mut P> { ... } fn slice_mut(&mut self, r: Range<(usize, usize)>) -> ImageMutSlice<P> { ... } fn iter_mut(&mut self) -> ImageMutRefIter<P> { ... } fn set_all(&mut self, pixel: P)
where
P: Clone, { ... } fn direct_copy<S>(&mut self, src: &S, offset: (isize, isize))
where
S: ReadableImage<P>,
P: Copy, { ... } fn flip_vertical(&mut self) { ... } fn flip_horizontal(&mut self) { ... } fn inplace_counterclockwise90_square(&mut self) -> Option<()> { ... } fn blit_generic<RI, F>(&mut self, src: &RI, offset: (isize, isize), op: F)
where
RI: ReadableImage<P>,
F: FnMut(P, P) -> P,
P: Copy, { ... } }
The trait for anything that can be written to as if it was an image.
Required Methods
fn as_mut_ptr(&mut self) -> *mut P
Raw mut pointer to the data.
Provided Methods
fn get_mut(&mut self, loc: (usize, usize)) -> Option<&mut P>
Performs an optional indexing by mut reference, gives None
for out of bounds.
fn slice_mut(&mut self, r: Range<(usize, usize)>) -> ImageMutSlice<P>
Grabs out a mutable sub-slice of the data.
Panics
If either end of the requested portion is out of bounds.
use retro_pixel::*; let width = 10; let height = 15; let mut vi: VecImage<u8> = VecImage::new(width,height); let base_ptr = vi.as_ptr(); let base_pitch = vi.pitch(); let mut whole_slice = vi.slice_mut( (0,0) .. (width,height) ); assert_eq!(whole_slice.width(), width); assert_eq!(whole_slice.height(), height); assert_eq!(whole_slice.pitch(), base_pitch); assert_eq!(whole_slice.as_ptr(), base_ptr); let partial_slice = whole_slice.slice_mut( (3,3) .. (7,8) ); assert_eq!(partial_slice.width(), 4); assert_eq!(partial_slice.height(), 5); assert_eq!(partial_slice.pitch(), base_pitch); assert_eq!(partial_slice.as_ptr(), unsafe { base_ptr.offset(3 + (3 * base_pitch)) } );
fn iter_mut(&mut self) -> ImageMutRefIter<P>
Lets you mutably iterate over any writable form of image.
fn set_all(&mut self, pixel: P) where
P: Clone,
P: Clone,
Assigns all locations to be the given pixel value.
fn direct_copy<S>(&mut self, src: &S, offset: (isize, isize)) where
S: ReadableImage<P>,
P: Copy,
S: ReadableImage<P>,
P: Copy,
Directly copies the data from the source image into this image.
The source image is copied in at the offset given. The resulting region is automatically clipped to stay within bounds, and you can even specify a negative offset if you like.
Because of rust's lifetime rules, and because absolutely no special
processing happens when moving the data between the two images, this just
does a byte-wise copy_nonoverlapping
operation, so it's pretty zippy.
use retro_pixel::*; let mut dest = VecImage::new(5,5); let mut src = VecImage::new(2,3); src.set_all(3u8); src[(0,2)] = 5; src[(1,2)] = 5; dest.direct_copy(&src, (1,-1)); for y in 0 .. dest.height() { for x in 0 .. dest.width() { match (x,y) { (1,0) | (2,0) => assert_eq!(dest[(x,y)], 3), (1,1) | (2,1) => assert_eq!(dest[(x,y)], 5), _ => assert_eq!(dest[(x,y)], 0), } } }
fn flip_vertical(&mut self)
Flips the image vertically.
use retro_pixel::*; let mut im = VecImage::new(2,2); im[(0,0)] = 5u8; im[(1,0)] = 5; im.flip_vertical(); assert_eq!(im[(0,0)], 0); assert_eq!(im[(1,0)], 0); assert_eq!(im[(0,1)], 5); assert_eq!(im[(1,1)], 5);
fn flip_horizontal(&mut self)
Flips the image horizontally.
use retro_pixel::*; let mut im = VecImage::new(2,2); im[(0,0)] = 5u8; im[(0,1)] = 5; im.flip_horizontal(); assert_eq!(im[(0,0)], 0); assert_eq!(im[(1,0)], 5); assert_eq!(im[(0,1)], 0); assert_eq!(im[(1,1)], 5);
fn inplace_counterclockwise90_square(&mut self) -> Option<()>
Performs a 90 degrees counter-clockwise rotation, in place.
Failure
The method simply fails with a None
return if the image isn't square.
use retro_pixel::*; let mut im = VecImage::new(3,3); im[(0,0)] = 5u8; im[(2,0)] = 6; im[(2,2)] = 7; im[(0,2)] = 8; assert!(im.inplace_counterclockwise90_square().is_some()); assert_eq!(im[(0,0)], 8); assert_eq!(im[(2,0)], 5); assert_eq!(im[(2,2)], 6); assert_eq!(im[(0,2)], 7); assert!(im.slice_mut((0,0)..(3,2)).inplace_counterclockwise90_square().is_none());
fn blit_generic<RI, F>(&mut self, src: &RI, offset: (isize, isize), op: F) where
RI: ReadableImage<P>,
F: FnMut(P, P) -> P,
P: Copy,
RI: ReadableImage<P>,
F: FnMut(P, P) -> P,
P: Copy,
Modifies this image by overlaying the source image at the offset given.
For each pixel of overlap between src
and dest
, the closure is called
with |src, dest|
as parameters, and the closure should return the result
to write as the new value for dest
.
use retro_pixel::*; let mut dest = VecImage::new(5,5); let mut src = VecImage::new(2,3); dest.set_all(1u8); src.set_all(3u8); src[(0,2)] = 5; src[(1,2)] = 5; dest.blit_generic(&src, (1,-1), |src,dest| src+dest ); for y in 0 .. dest.height() { for x in 0 .. dest.width() { match (x,y) { (1,0) | (2,0) => assert_eq!(dest[(x,y)], 4), (1,1) | (2,1) => assert_eq!(dest[(x,y)], 6), _ => assert_eq!(dest[(x,y)], 1), } } }
Implementors
impl<'a, P: 'a> WritableImage<P> for ImageMutSlice<'a, P>
impl<P> WritableImage<P> for VecImage<P>
impl WritableImage<u8> for NESImage