pub trait ReadableImage<P>: Index<(usize, usize), Output = P> {
// Required methods
fn width(&self) -> usize;
fn height(&self) -> usize;
fn pitch(&self) -> isize;
fn as_ptr(&self) -> *const P;
// Provided methods
fn get(&self, loc: (usize, usize)) -> Option<&P> { ... }
fn slice(&self, r: Range<(usize, usize)>) -> ImageSlice<'_, P> { ... }
fn iter(&self) -> ImageRefIter<'_, P> ⓘ { ... }
fn to_vecimage(&self) -> VecImage<P>
where Self: Sized,
P: Default + Clone { ... }
fn upscale(&self, scale: usize) -> VecImage<P>
where P: Copy + Default { ... }
}
Expand description
Trait for anything that can be read as if it was an image.
An image is here defined as being a height
long series of “pixel” slices
(each width
in length) that are evenly spaced out by a particular pitch
value (the offset from the start of one slice to the start of the next
slice). The type of data held in a pixel often doesn’t matter, most
operations don’t examine the data itself. Things that do examine the data
use “where” bounds so that they are always appropriate to the pixel type.
Required Methods§
Provided Methods§
Sourcefn get(&self, loc: (usize, usize)) -> Option<&P>
fn get(&self, loc: (usize, usize)) -> Option<&P>
Performs an optional indexing by reference, gives None
for out of bounds.
Sourcefn slice(&self, r: Range<(usize, usize)>) -> ImageSlice<'_, P>
fn slice(&self, r: Range<(usize, usize)>) -> ImageSlice<'_, P>
Grabs out a sub-slice of the data.
§Panics
If either end of the requested portion is out of bounds.
use retro_pixel::*;
let vi: VecImage<u8> = VecImage::new(10,15);
let whole_slice = vi.slice( (0,0) .. (vi.width(),vi.height()) );
assert_eq!(whole_slice.width(), vi.width());
assert_eq!(whole_slice.height(), vi.height());
assert_eq!(whole_slice.pitch(), vi.pitch());
assert_eq!(whole_slice.as_ptr(), vi.as_ptr());
let partial_slice = whole_slice.slice( (3,3) .. (7,8) );
assert_eq!(partial_slice.width(), 4);
assert_eq!(partial_slice.height(), 5);
assert_eq!(partial_slice.pitch(), vi.pitch());
assert_eq!(partial_slice.as_ptr(), unsafe { vi.as_ptr().offset(3 + (3 * vi.pitch())) } );
Sourcefn iter(&self) -> ImageRefIter<'_, P> ⓘ
fn iter(&self) -> ImageRefIter<'_, P> ⓘ
Lets you iterate any image by reference.
Sourcefn to_vecimage(&self) -> VecImage<P>
fn to_vecimage(&self) -> VecImage<P>
This is like to_owned
, you get your own version of the data.
The actual ToOwned
trait has some conflicts because we’re not using
normal references, so we just have this instead.
use retro_pixel::*;
let mut source: VecImage<u8> = VecImage::new(5,5);
let a = source.to_vecimage();
let b = source.slice((0,0)..(5,5)).to_vecimage();
let c = source.slice_mut((0,0)..(5,5)).to_vecimage();
assert_eq!(a, b);
assert_eq!(a, c);
Sourcefn upscale(&self, scale: usize) -> VecImage<P>
fn upscale(&self, scale: usize) -> VecImage<P>
Scales up into a new VecImage
by the given amount.
use retro_pixel::*;
let mut source = VecImage::new(2,2);
source[(0,1)] = 5u8;
let scaled = source.upscale(2);
for (x,y,&p) in scaled.iter() {
match (x,y) {
(0,2) | (0,3) | (1,2) | (1,3) => assert_eq!(p, 5),
_ => assert_eq!(p, 0),
}
}