Struct Pixmap

Source
#[repr(align(4))]
pub struct Pixmap { /* private fields */ }
Expand description

A struct representing a pixelmap for pixelflut.

This struct holds the data for each pixel, and can be concidered a bitmap. For each pixel, a u32 (DWORD) value is used containing 4 bytes that define the value for each of the 4 color channels.

This data structure is focussed on performance and multithreaded use with multiple readers and writers. This structure does not use any kind of locks. Instead, it is assumed that the operations done on the internal map are atomic (on a pixel basis). This is perfectly fine for what this pixelmap is used for.

Because this structure is aligned to 4 bytes in memory, each raw color value (u32) is also aligned to 4 bytes. This makes direct reads and writes on these values on most CPUs (but not all!). The fact that this may not be atomic in some cases is accepted for this structure. The speed of not using locks is preferred over the minor side effect of seldom rendering artifact on some systems.

More info: https://stackoverflow.com/a/5002256/1000145

Important: this data structure is considered unsafe, but is perfectly usable for pixelflut applications.

Implementations§

Source§

impl Pixmap

Source

pub fn new(width: usize, height: usize) -> Self

Construct a new

Source

pub fn width(&self) -> usize

Get the width of the pixel map.

Source

pub fn height(&self) -> usize

Get the height of the pixel map.

Source

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

Get the dimensions of the pixel map.

Source

pub fn pixel(&self, x: usize, y: usize) -> Result<Color, PixmapErr<'_>>

Get the pixel at the given coordinate, as color.

Source

pub fn pixel_raw(&self, x: usize, y: usize) -> Result<u32, PixmapErr<'_>>

Get the pixel at the given coordinate, as raw color value.

Source

pub fn set_pixel( &self, x: usize, y: usize, color: Color, ) -> Result<(), PixmapErr<'_>>

Set the pixel at the given coordinate, to the given color.

Source

pub fn set_pixel_raw( &self, x: usize, y: usize, raw: u32, ) -> Result<(), PixmapErr<'_>>

Set the pixel at the given coordinate, to the given raw color value.

Source

pub fn as_slice(&self) -> &[u32]

Get the pixelmap data, as slice with the raw color value of each pixel.

Note: this method returns a single u32 for each pixel, instead of 4 u8 bytes for each pixel as the as_bytes() method does.

Source

pub fn as_bytes(&self) -> &[u8]

Get the pixelmap data, as a slice of bytes.

Each pixel consumes a sequence of 4 bytes, each defining the value of a different color channel.

This data may be used to send to the GPU, as raw texture buffer, for rendering.

Trait Implementations§

Auto Trait Implementations§

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> 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, 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.