Trait WritableImage

Source
pub trait WritableImage<P>: ReadableImage<P> + IndexMut<(usize, usize), Output = P> {
    // Required method
    fn as_mut_ptr(&mut self) -> *mut P;

    // Provided methods
    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 { ... }
}
Expand description

The trait for anything that can be written to as if it was an image.

Required Methods§

Source

fn as_mut_ptr(&mut self) -> *mut P

Raw mut pointer to the data.

Provided Methods§

Source

fn get_mut(&mut self, loc: (usize, usize)) -> Option<&mut P>

Performs an optional indexing by mut reference, gives None for out of bounds.

Source

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)) } );
Source

fn iter_mut(&mut self) -> ImageMutRefIter<'_, P>

Lets you mutably iterate over any writable form of image.

Source

fn set_all(&mut self, pixel: P)
where P: Clone,

Assigns all locations to be the given pixel value.

Source

fn direct_copy<S>(&mut self, src: &S, offset: (isize, isize))
where 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),
    }
  }
}
Source

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);
Source

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);
Source

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());
Source

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,

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),
    }
  }
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§