Struct cosmic::iced_winit::graphics::image::image_rs::FlatSamples

source ·
pub struct FlatSamples<Buffer> {
    pub samples: Buffer,
    pub layout: SampleLayout,
    pub color_hint: Option<ColorType>,
}
Expand description

A flat buffer over a (multi channel) image.

In contrast to ImageBuffer, this representation of a sample collection is much more lenient in the layout thereof. It also allows grouping by color planes instead of by pixel as long as the strides of each extent are constant. This struct itself has no invariants on the strides but not every possible configuration can be interpreted as a GenericImageView or GenericImage. The methods as_view and as_view_mut construct the actual implementors of these traits and perform necessary checks. To manually perform this and other layout checks use is_normal or has_aliased_samples.

Instances can be constructed not only by hand. The buffer instances returned by library functions such as ImageBuffer::as_flat_samples guarantee that the conversion to a generic image or generic view succeeds. A very different constructor is with_monocolor. It uses a single pixel as the backing storage for an arbitrarily sized read-only raster by mapping each pixel to the same samples by setting some strides to 0.

Fields§

§samples: Buffer

Underlying linear container holding sample values.

§layout: SampleLayout

A repr(C) description of the layout of buffer samples.

§color_hint: Option<ColorType>

Supplementary color information.

You may keep this as None in most cases. This is NOT checked in View or other converters. It is intended mainly as a way for types that convert to this buffer type to attach their otherwise static color information. A dynamic image representation could however use this to resolve representational ambiguities such as the order of RGB channels.

Implementations§

source§

impl<Buffer> FlatSamples<Buffer>

source

pub fn strides_cwh(&self) -> (usize, usize, usize)

Get the strides for indexing matrix-like [(c, w, h)].

For a row-major layout with grouped samples, this tuple is strictly increasing.

source

pub fn extents(&self) -> (usize, usize, usize)

Get the dimensions (channels, width, height).

The interface is optimized for use with strides_cwh instead. The channel extent will be before width and height.

source

pub fn bounds(&self) -> (u8, u32, u32)

Tuple of bounds in the order of coordinate inputs.

This function should be used whenever working with image coordinates opposed to buffer coordinates. The only difference compared to extents is the output type.

source

pub fn as_ref<T>(&self) -> FlatSamples<&[T]>
where Buffer: AsRef<[T]>,

Get a reference based version.

source

pub fn as_mut<T>(&mut self) -> FlatSamples<&mut [T]>
where Buffer: AsMut<[T]>,

Get a mutable reference based version.

source

pub fn to_vec<T>(&self) -> FlatSamples<Vec<T>>
where T: Clone, Buffer: AsRef<[T]>,

Copy the data into an owned vector.

source

pub fn get_sample<T>(&self, channel: u8, x: u32, y: u32) -> Option<&T>
where Buffer: AsRef<[T]>,

Get a reference to a single sample.

This more restrictive than the method based on std::ops::Index but guarantees to properly check all bounds and not panic as long as Buffer::as_ref does not do so.

let flat = RgbImage::new(480, 640).into_flat_samples();

// Get the blue channel at (10, 10).
assert!(flat.get_sample(1, 10, 10).is_some());

// There is no alpha channel.
assert!(flat.get_sample(3, 10, 10).is_none());

For cases where a special buffer does not provide AsRef<[T]>, consider encapsulating bounds checks with min_length in a type similar to View. Then you may use in_bounds_index as a small speedup over the index calculation of this method which relies on index_ignoring_bounds since it can not have a-priori knowledge that the sample coordinate is in fact backed by any memory buffer.

source

pub fn get_mut_sample<T>( &mut self, channel: u8, x: u32, y: u32, ) -> Option<&mut T>
where Buffer: AsMut<[T]>,

Get a mutable reference to a single sample.

This more restrictive than the method based on std::ops::IndexMut but guarantees to properly check all bounds and not panic as long as Buffer::as_ref does not do so. Contrary to conversion to ViewMut, this does not require that samples are packed since it does not need to convert samples to a color representation.

WARNING: Note that of course samples may alias, so that the mutable reference returned here can in fact modify more than the coordinate in the argument.

let mut flat = RgbImage::new(480, 640).into_flat_samples();

// Assign some new color to the blue channel at (10, 10).
*flat.get_mut_sample(1, 10, 10).unwrap() = 255;

// There is no alpha channel.
assert!(flat.get_mut_sample(3, 10, 10).is_none());

For cases where a special buffer does not provide AsRef<[T]>, consider encapsulating bounds checks with min_length in a type similar to View. Then you may use in_bounds_index as a small speedup over the index calculation of this method which relies on index_ignoring_bounds since it can not have a-priori knowledge that the sample coordinate is in fact backed by any memory buffer.

source

pub fn as_view<P>(&self) -> Result<View<&[<P as Pixel>::Subpixel], P>, Error>
where P: Pixel, Buffer: AsRef<[<P as Pixel>::Subpixel]>,

View this buffer as an image over some type of pixel.

This first ensures that all in-bounds coordinates refer to valid indices in the sample buffer. It also checks that the specified pixel format expects the same number of channels that are present in this buffer. Neither are larger nor a smaller number will be accepted. There is no automatic conversion.

source

pub fn as_view_with_mut_samples<P>( &mut self, ) -> Result<View<&mut [<P as Pixel>::Subpixel], P>, Error>
where P: Pixel, Buffer: AsMut<[<P as Pixel>::Subpixel]>,

View this buffer but keep mutability at a sample level.

This is similar to as_view but subtly different from as_view_mut. The resulting type can be used as a GenericImage with the same prior invariants needed as for as_view. It can not be used as a mutable GenericImage but does not need channels to be packed in their pixel representation.

This first ensures that all in-bounds coordinates refer to valid indices in the sample buffer. It also checks that the specified pixel format expects the same number of channels that are present in this buffer. Neither are larger nor a smaller number will be accepted. There is no automatic conversion.

WARNING: Note that of course samples may alias, so that the mutable reference returned for one sample can in fact modify other samples as well. Sometimes exactly this is intended.

source

pub fn as_view_mut<P>( &mut self, ) -> Result<ViewMut<&mut [<P as Pixel>::Subpixel], P>, Error>
where P: Pixel, Buffer: AsMut<[<P as Pixel>::Subpixel]>,

Interpret this buffer as a mutable image.

To succeed, the pixels in this buffer may not alias each other and the samples of each pixel must be packed (i.e. channel_stride is 1). The number of channels must be consistent with the channel count expected by the pixel format.

This is similar to an ImageBuffer except it is a temporary view that is not normalized as strongly. To get an owning version, consider copying the data into an ImageBuffer. This provides many more operations, is possibly faster (if not you may want to open an issue) is generally polished. You can also try to convert this buffer inline, see ImageBuffer::from_raw.

source

pub fn as_slice<T>(&self) -> &[T]
where Buffer: AsRef<[T]>,

View the samples as a slice.

The slice is not limited to the region of the image and not all sample indices are valid indices into this buffer. See image_mut_slice as an alternative.

source

pub fn as_mut_slice<T>(&mut self) -> &mut [T]
where Buffer: AsMut<[T]>,

View the samples as a slice.

The slice is not limited to the region of the image and not all sample indices are valid indices into this buffer. See image_mut_slice as an alternative.

source

pub fn image_slice<T>(&self) -> Option<&[T]>
where Buffer: AsRef<[T]>,

Return the portion of the buffer that holds sample values.

This may fail when the coordinates in this image are either out-of-bounds of the underlying buffer or can not be represented. Note that the slice may have holes that do not correspond to any sample in the image represented by it.

source

pub fn image_mut_slice<T>(&mut self) -> Option<&mut [T]>
where Buffer: AsMut<[T]>,

Mutable portion of the buffer that holds sample values.

source

pub fn try_into_buffer<P>( self, ) -> Result<ImageBuffer<P, Buffer>, (Error, FlatSamples<Buffer>)>
where P: Pixel + 'static, <P as Pixel>::Subpixel: 'static, Buffer: Deref<Target = [<P as Pixel>::Subpixel]>,

Move the data into an image buffer.

This does not convert the sample layout. The buffer needs to be in packed row-major form before calling this function. In case of an error, returns the buffer again so that it does not release any allocation.

source

pub fn min_length(&self) -> Option<usize>

Get the minimum length of a buffer such that all in-bounds samples have valid indices.

This method will allow zero strides, allowing compact representations of monochrome images. To check that no aliasing occurs, try check_alias_invariants. For compact images (no aliasing and no unindexed samples) this is width*height*channels. But for both of the other cases, the reasoning is slightly more involved.

§Explanation

Note that there is a difference between min_length and the index of the sample ’one-past-the-end`. This is due to strides that may be larger than the dimension below.

§Example with holes

Let’s look at an example of a grayscale image with

  • width_stride = 1
  • width = 2
  • height_stride = 3
  • height = 2
| x x   | x x m | $
 min_length m ^
                  ^ one-past-the-end $

The difference is also extreme for empty images with large strides. The one-past-the-end sample index is still as large as the largest of these strides while min_length = 0.

§Example with aliasing

The concept gets even more important when you allow samples to alias each other. Here we have the buffer of a small grayscale image where this is the case, this time we will first show the buffer and then the individual rows below.

  • width_stride = 1
  • width = 3
  • height_stride = 2
  • height = 2
 1 2 3 4 5 m
|1 2 3| row one
    |3 4 5| row two
           ^ m min_length
         ^ ??? one-past-the-end

This time ‘one-past-the-end’ is not even simply the largest stride times the extent of its dimension. That still points inside the image because height*height_stride = 4 but also index_of(1, 2) = 4.

source

pub fn fits(&self, len: usize) -> bool

Check if a buffer of length len is large enough.

source

pub fn has_aliased_samples(&self) -> bool

If there are any samples aliasing each other.

If this is not the case, it would always be safe to allow mutable access to two different samples at the same time. Otherwise, this operation would need additional checks. When one dimension overflows usize with its stride we also consider this aliasing.

source

pub fn is_normal(&self, form: NormalForm) -> bool

Check if a buffer fulfills the requirements of a normal form.

Certain conversions have preconditions on the structure of the sample buffer that are not captured (by design) by the type system. These are then checked before the conversion. Such checks can all be done in constant time and will not inspect the buffer content. You can perform these checks yourself when the conversion is not required at this moment but maybe still performed later.

source

pub fn in_bounds(&self, channel: u8, x: u32, y: u32) -> bool

Check that the pixel and the channel index are in bounds.

An in-bound coordinate does not yet guarantee that the corresponding calculation of a buffer index does not overflow. However, if such a buffer large enough to hold all samples actually exists in memory, this property of course follows.

source

pub fn index(&self, channel: u8, x: u32, y: u32) -> Option<usize>

Resolve the index of a particular sample.

None if the index is outside the bounds or does not fit into a usize.

source

pub fn index_ignoring_bounds( &self, channel: usize, x: usize, y: usize, ) -> Option<usize>

Get the theoretical position of sample (x, y, channel).

The ‘check’ is for overflow during index calculation, not that it is contained in the image. Two samples may return the same index, even when one of them is out of bounds. This happens when all strides are 0, i.e. the image is an arbitrarily large monochrome image.

source

pub fn in_bounds_index(&self, channel: u8, x: u32, y: u32) -> usize

Get an index provided it is inbouds.

Assumes that the image is backed by some sufficiently large buffer. Then computation can not overflow as we could represent the maximum coordinate. Since overflow is defined either way, this method can not be unsafe.

source

pub fn shrink_to(&mut self, channels: u8, width: u32, height: u32)

Shrink the image to the minimum of current and given extents.

This does not modify the strides, so that the resulting sample buffer may have holes created by the shrinking operation. Shrinking could also lead to an non-aliasing image when samples had aliased each other before.

source§

impl<'buf, Subpixel> FlatSamples<&'buf [Subpixel]>

source

pub fn with_monocolor<P>( pixel: &'buf P, width: u32, height: u32, ) -> FlatSamples<&'buf [Subpixel]>
where P: Pixel<Subpixel = Subpixel>, Subpixel: Primitive,

Create a monocolor image from a single pixel.

This can be used as a very cheap source of a GenericImageView with an arbitrary number of pixels of a single color, without any dynamic allocation.

§Examples
use image::{flat::FlatSamples, GenericImage, RgbImage, Rgb};

let background = Rgb([20, 20, 20]);
let bg = FlatSamples::with_monocolor(&background, 200, 200);;

let mut image = RgbImage::new(200, 200);
paint_something(&mut image);

// Reset the canvas
image.copy_from(&bg.as_view().unwrap(), 0, 0);

Trait Implementations§

source§

impl<Buffer> Clone for FlatSamples<Buffer>
where Buffer: Clone,

source§

fn clone(&self) -> FlatSamples<Buffer>

Returns a copy 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<Buffer> Debug for FlatSamples<Buffer>
where Buffer: Debug,

source§

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

Formats the value using the given formatter. Read more
source§

impl<Buffer> Index<(u8, u32, u32)> for FlatSamples<Buffer>
where Buffer: Index<usize>,

source§

fn index( &self, _: (u8, u32, u32), ) -> &<FlatSamples<Buffer> as Index<(u8, u32, u32)>>::Output

Return a reference to a single sample at specified coordinates.

§Panics

When the coordinates are out of bounds or the index calculation fails.

source§

type Output = <Buffer as Index<usize>>::Output

The returned type after indexing.
source§

impl<Buffer> IndexMut<(u8, u32, u32)> for FlatSamples<Buffer>
where Buffer: IndexMut<usize>,

source§

fn index_mut( &mut self, _: (u8, u32, u32), ) -> &mut <FlatSamples<Buffer> as Index<(u8, u32, u32)>>::Output

Return a mutable reference to a single sample at specified coordinates.

§Panics

When the coordinates are out of bounds or the index calculation fails.

Auto Trait Implementations§

§

impl<Buffer> Freeze for FlatSamples<Buffer>
where Buffer: Freeze,

§

impl<Buffer> RefUnwindSafe for FlatSamples<Buffer>
where Buffer: RefUnwindSafe,

§

impl<Buffer> Send for FlatSamples<Buffer>
where Buffer: Send,

§

impl<Buffer> Sync for FlatSamples<Buffer>
where Buffer: Sync,

§

impl<Buffer> Unpin for FlatSamples<Buffer>
where Buffer: Unpin,

§

impl<Buffer> UnwindSafe for FlatSamples<Buffer>
where Buffer: UnwindSafe,

Blanket Implementations§

source§

impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where T: Real + Zero + Arithmetics + Clone, Swp: WhitePoint<T>, Dwp: WhitePoint<T>, D: AdaptFrom<S, Swp, Dwp, T>,

source§

fn adapt_into_using<M>(self, method: M) -> D
where M: TransformMatrix<T>,

Convert the source color to the destination color using the specified method.
source§

fn adapt_into(self) -> D

Convert the source color to the destination color using the bradford method by default.
source§

impl<T> Also for T

source§

fn also<F>(self, block: F) -> Self
where F: FnOnce(&mut Self),

Apply a function to this value and return the (possibly) modified value.
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, Res> Apply<Res> for T
where T: ?Sized,

source§

fn apply<F>(self, f: F) -> Res
where F: FnOnce(Self) -> Res, Self: Sized,

Apply a function which takes the parameter by value.
source§

fn apply_ref<F>(&self, f: F) -> Res
where F: FnOnce(&Self) -> Res,

Apply a function which takes the parameter by reference.
source§

fn apply_mut<F>(&mut self, f: F) -> Res
where F: FnOnce(&mut Self) -> Res,

Apply a function which takes the parameter by mutable reference.
source§

impl<T, C> ArraysFrom<C> for T
where C: IntoArrays<T>,

source§

fn arrays_from(colors: C) -> T

Cast a collection of colors into a collection of arrays.
source§

impl<T, C> ArraysInto<C> for T
where C: FromArrays<T>,

source§

fn arrays_into(self) -> C

Cast this collection of arrays into a collection of colors.
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<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for U
where T: FromCam16Unclamped<WpParam, U>,

source§

type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
source§

fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
source§

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

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

impl<T, C> ComponentsFrom<C> for T
where C: IntoComponents<T>,

source§

fn components_from(colors: C) -> T

Cast a collection of colors into a collection of color components.
source§

impl<T> Downcast for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromAngle<T> for T

source§

fn from_angle(angle: T) -> T

Performs a conversion from angle.
source§

impl<T, U> FromStimulus<U> for T
where U: IntoStimulus<T>,

source§

fn from_stimulus(other: U) -> T

Converts other into Self, while performing the appropriate scaling, rounding and clamping.
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> IntoAngle<U> for T
where U: FromAngle<T>,

source§

fn into_angle(self) -> U

Performs a conversion into T.
source§

impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for U
where T: Cam16FromUnclamped<WpParam, U>,

source§

type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
source§

fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
source§

impl<T, U> IntoColor<U> for T
where U: FromColor<T>,

source§

fn into_color(self) -> U

Convert into T with values clamped to the color defined bounds Read more
source§

impl<T, U> IntoColorUnclamped<U> for T
where U: FromColorUnclamped<T>,

source§

fn into_color_unclamped(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
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> IntoStimulus<T> for T

source§

fn into_stimulus(self) -> T

Converts self into T, while performing the appropriate scaling, rounding and clamping.
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<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, C> TryComponentsInto<C> for T
where C: TryFromComponents<T>,

source§

type Error = <C as TryFromComponents<T>>::Error

The error for when try_into_colors fails to cast.
source§

fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>

Try to cast this collection of color components into a collection of colors. 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<T, U> TryIntoColor<U> for T
where U: TryFromColor<T>,

source§

fn try_into_color(self) -> Result<U, OutOfBounds<U>>

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more
source§

impl<C, U> UintsFrom<C> for U
where C: IntoUints<U>,

source§

fn uints_from(colors: C) -> U

Cast a collection of colors into a collection of unsigned integers.
source§

impl<C, U> UintsInto<C> for U
where C: FromUints<U>,

source§

fn uints_into(self) -> C

Cast this collection of unsigned integers into a collection of colors.
source§

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

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSend for T
where T: Send,

source§

impl<T> MaybeSendSync for T

source§

impl<T> MaybeSync for T
where T: Sync,