Skip to main content

Image

Struct Image 

Source
pub struct Image { /* private fields */ }
Expand description

Premultiplied RGBA8 pixel data, shared by every element that shows it.

The pixels are row-major with no padding, four bytes per pixel in red, green, blue, alpha order, with the color channels already multiplied by the alpha. Two images compare equal when they came from the same Image::from_premultiplied_rgba call, so a display list that re-emits the same image damages nothing new.

§Examples

A two by two image, fully opaque red and blue:

use smudge_core::image::Image;

let image = Image::from_premultiplied_rgba(
    2,
    2,
    vec![
        255, 0, 0, 255, 255, 0, 0, 255,
        0, 0, 255, 255, 0, 0, 255, 255,
    ],
)
.expect("the buffer holds four pixels");
assert_eq!(image.width(), 2);

Implementations§

Source§

impl Image

Source

pub fn from_premultiplied_rgba( width: u32, height: u32, pixels: Vec<u8>, ) -> Result<Image, ImageError>

Builds an image from premultiplied RGBA8 pixels, row-major with no padding, four bytes per pixel in red, green, blue, alpha order, with the color channels already multiplied by the alpha.

A color channel above its pixel’s alpha cannot come out of a multiplication by that alpha, so it is clamped to the alpha. Both renderers then blend the same valid pixel. A buffer that holds straight alpha belongs in Image::from_straight_rgba instead.

§Errors

Returns ImageError::ZeroSize when width or height is zero, and ImageError::PixelCount when pixels does not hold exactly width * height * 4 bytes.

§Examples
use smudge_core::image::{Image, ImageError};

let error = Image::from_premultiplied_rgba(2, 2, vec![0; 7])
    .expect_err("the buffer is too short");
assert!(matches!(
    error,
    ImageError::PixelCount {
        expected: 16,
        found: 7,
    }
));
Source

pub fn from_straight_rgba( width: u32, height: u32, pixels: Vec<u8>, ) -> Result<Image, ImageError>

Builds an image from straight-alpha RGBA8 pixels, the form most decoders produce, by multiplying each color channel by its pixel’s alpha and rounding to the nearest value.

The layout is the one Image::from_premultiplied_rgba takes: row major with no padding, four bytes per pixel in red, green, blue, alpha order. The conversion happens in place, without a second buffer.

§Errors

Returns ImageError::ZeroSize when width or height is zero, and ImageError::PixelCount when pixels does not hold exactly width * height * 4 bytes.

§Examples

White at half alpha becomes half-intensity white:

use smudge_core::image::Image;

let image = Image::from_straight_rgba(1, 1, vec![255, 255, 255, 128])
    .expect("one pixel fills the buffer");
assert_eq!(image.pixels(), &[128, 128, 128, 128]);
Source

pub fn width(&self) -> u32

The image’s width in pixels.

Source

pub fn height(&self) -> u32

The image’s height in pixels.

Source

pub fn pixels(&self) -> &[u8] ⓘ

The premultiplied RGBA8 pixels, row-major with no padding.

Source

pub fn id(&self) -> u64

The identifier that names this image’s data for the lifetime of the process.

Renderers use it as an upload cache key without hashing the pixels.

Trait Implementations§

Source§

impl Clone for Image

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Image

Source§

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

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

impl PartialEq for Image

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl Forget for Image

§

impl Freeze for Image

§

impl Move for Image

§

impl RefUnwindSafe for Image

§

impl Send for Image

§

impl Sync for Image

§

impl Unpin for Image

§

impl UnsafeUnpin for Image

§

impl UnwindSafe for Image

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> 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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.