PineappleBuffer

Struct PineappleBuffer 

Source
pub struct PineappleBuffer<T, Container> {
    pub buffer: Container,
    /* private fields */
}
Expand description

A row-major container storing an image buffer or grid of pixels.

The struct is generic over the data type T and over the container that holds raw pixel/subpixel data as a slice ([T]) or vector (Vec<T>). The container holding the pixel data must implement Deref<Target = [T]> to allow for slice-like access to the data. The length of the container must also be equal to the product of w * h * c.

§Examples

use pineapple_core::im::PineappleBuffer;

let width = 10;
let height = 10;
let channels = 3; // RGB
let data = vec![0u8; (width * height * channels) as usize];

let buffer = PineappleBuffer::new(width, height, channels, data);

assert_eq!(buffer.unwrap().len(), (width * height * channels) as usize);
use pineapple_core::im::PineappleBuffer;

let width = 10;
let height = 10;
let channels = 3; // RGB
let data = vec![0u8; (width * height * 3 * channels) as usize];

let buffer = PineappleBuffer::new(width, height, channels, data);

assert!(buffer.is_err()); // Buffer size does not match dimensions

Fields§

§buffer: Container

Implementations§

Source§

impl<T, Container> PineappleBuffer<T, Container>
where T: ToPrimitive + FromPrimitive, Container: Deref<Target = [T]>,

Source

pub fn new( width: u32, height: u32, channels: u32, buffer: Container, ) -> Result<PineappleBuffer<T, Container>, PineappleError>

Initializes a buffer from a generic data container

§Arguments
  • width - Image width
  • height - Image height
  • channels - Number of image channels (e.g. 1 for grayscale)
  • buffer - A generic container (e.g. Vec or slice)
§Examples
use pineapple_core::im::PineappleBuffer;
let buffer = [0, 1, 2, 3, 4];
let buffer = PineappleBuffer::new(2, 2, 1, buffer.as_slice());
Source§

impl<T, Container> PineappleBuffer<T, Container>
where T: ToPrimitive + FromPrimitive, Container: Deref<Target = [T]>,

Source

pub fn width(&self) -> u32

Width of the image

Source

pub fn height(&self) -> u32

Height of the image

Source

pub fn channels(&self) -> u32

Number of channels in the image

Source

pub fn shape(&self) -> (u32, u32, u32)

Shape/dimensions of the image

Source

pub fn len(&self) -> usize

Length of the raw image

Source

pub fn is_empty(&self) -> bool

Check if buffer is empty

Source§

impl<T, Container> PineappleBuffer<T, Container>
where T: ToPrimitive + FromPrimitive, Container: Deref<Target = [T]>,

Source

pub fn into_raw(self) -> Container

Returns the raw image

Source

pub fn as_raw(&self) -> &Container

Returns a reference to the raw image

Source

pub fn to_u8(&self) -> Vec<u8>

Cast subpixels to u8 and return the buffer

Source

pub fn to_u16(&self) -> Vec<u16>

Cast subpixels to u16 and return the buffer

Source

pub fn to_u32(&self) -> Vec<u32>

Cast subpixels to u16 and return the buffer

Source

pub fn to_f32(&self) -> Vec<f32>

Cast subpixels to f32 and return the buffer

Source

pub fn to_f64(&self) -> Vec<f64>

Cast subpixels to f64 and return the buffer

Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Source

pub fn iter_channel( &self, channel: u32, ) -> Result<impl Iterator<Item = &T>, PineappleError>
where Container: Deref<Target = [T]>,

Source

pub fn iter_pixels(&self) -> ChunksExact<'_, T>

Source§

impl<T, Container> PineappleBuffer<T, Container>
where Container: Deref<Target = [T]> + FromIterator<T>, T: Clone + ToPrimitive + FromPrimitive,

Source

pub fn crop_view( &self, x: u32, y: u32, w: u32, h: u32, ) -> PineappleViewBuffer<'_, T, Container>

Generate a zero-copy crop of an image subregion

§Arguments
  • x - Minimum x-coordinate (left)
  • y - Minimum y-coordinate (bottom)
  • w - Width of crop
  • h - Height of crop
Source

pub fn crop( &self, x: u32, y: u32, w: u32, h: u32, ) -> Result<PineappleBuffer<T, Container>, PineappleError>

Create a new buffer with copied cropped contents

§Arguments
  • x - Minimum x-coordinate (left)
  • y - Minimum y-coordinate (bottom)
  • w - Width of crop
  • h - Height of crop
Source

pub fn crop_masked( &self, x: u32, y: u32, w: u32, h: u32, mask: &PineappleMaskView<'_>, mask_style: MaskingStyle, ) -> Result<PineappleBuffer<T, Container>, PineappleError>

Crops the buffer while applying a mask to either foreground or background pixels

§Arguments
  • x - Minimum x-coordinate (left)
  • y - Minimum y-coordinate (bottom)
  • w - Width of crop
  • h - Height of crop
  • mask - A cropped mask view
  • mask_style - Foreground or background masking style
Source§

impl PineappleBuffer<u32, Vec<u32>>

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<PineappleMask, PineappleError>

Open a new mask from a provided path

§Arguments
  • path - A path to an image with a valid extension
use pineapple_core::im::PineappleMask;
let image = PineappleMask::open("mask.png");
Source

pub fn new_from_dynamic( mask: DynamicImage, ) -> Result<PineappleMask, PineappleError>

Initialize a new mask from a DynamicImage

§Arguments
  • image - An 8 or 16-bit grayscale DynamicImage
§Examples
use image::{GrayImage, DynamicImage};
use pineapple_core::im::PineappleMask;

let gray = GrayImage::new(10, 10);
let dynamic = DynamicImage::ImageLuma8(gray);
let image = PineappleMask::new_from_dynamic(dynamic);
Source

pub fn new_from_numpy( npy: NpyFile<&[u8]>, ) -> Result<PineappleMask, PineappleError>

Initialize a new image from a numpy array buffer

§Arguments
  • npy - A (height, width, channel) shaped numpy array buffer
§Examples
use npyz::NpyFile;
use pineapple_core::im::PineappleMask;

let bytes = std::fs::read("mask.npy").unwrap();
let npy = NpyFile::new(&bytes[..]).unwrap();
let image = PineappleMask::new_from_numpy(npy);
Source§

impl PineappleBuffer<u32, Vec<u32>>

Source

pub fn label(&mut self) -> Vec<u32>

Re-label the mask using connected components and return unique labels

§Notes

Re-labelling is guaranteed to assign the correct number of labels when assuming 8-connectivity. However, the labels are not guaranteed to be incremental (e.g. 1, 2, 3, ..). This should be taken into account when iterating over objects.

Source

pub fn polygons(&mut self) -> Result<(Vec<u32>, Polygons), PineappleError>

Extract polygons from a segmentation mask

Source

pub fn crop_binary( &self, x: u32, y: u32, w: u32, h: u32, label: u32, ) -> Result<PineappleMask, PineappleError>

Crops image while only including pixels with a specified label

§Arguments
  • x - Minimum x-coordinate (left)
  • y - Minimum y-coordinate (bottom)
  • w - Width of crop
  • h - Height of crop
  • label - Only include mask pixels equal to this label

Trait Implementations§

Source§

impl<T: Clone, Container: Clone> Clone for PineappleBuffer<T, Container>

Source§

fn clone(&self) -> PineappleBuffer<T, Container>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, Container: Debug> Debug for PineappleBuffer<T, Container>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, Container> Freeze for PineappleBuffer<T, Container>
where Container: Freeze,

§

impl<T, Container> RefUnwindSafe for PineappleBuffer<T, Container>
where Container: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, Container> Send for PineappleBuffer<T, Container>
where Container: Send, T: Send,

§

impl<T, Container> Sync for PineappleBuffer<T, Container>
where Container: Sync, T: Sync,

§

impl<T, Container> Unpin for PineappleBuffer<T, Container>
where Container: Unpin, T: Unpin,

§

impl<T, Container> UnwindSafe for PineappleBuffer<T, Container>
where Container: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V