Skip to main content

PixelMode

Enum PixelMode 

Source
#[repr(u8)]
pub enum PixelMode { Mono1 = 0, Mono8 = 1, Rgb8 = 2, Bgr8 = 3, Xbgr8 = 4, Cmyk8 = 5, DeviceN8 = 6, }
Expand description

Re-export every public type from the color crate, including all arithmetic helpers from color::convert.

Downstream modules within this crate can import everything they need with a single use crate::types::*. Pixel color mode, describing the memory layout of one pixel in a bitmap row.

The discriminant values mirror SplashColorMode in SplashTypes.h so that raw FFI conversions remain stable.

§Packed-bits exception

Mono1 stores one bit per pixel and cannot be expressed as a whole-number byte count per pixel. See PixelMode::is_packed_bits and PixelMode::bits_per_pixel.

Variants§

§

Mono1 = 0

1 bit/pixel, MSB-first packed; row stride is (width + 7) / 8 bytes.

bytes_per_pixel() returns 0 for this variant — guard with is_packed_bits before using it.

§

Mono8 = 1

1 byte/pixel grayscale (8 bits, luminance only).

§

Rgb8 = 2

3 bytes/pixel, channel order: R G B.

§

Bgr8 = 3

3 bytes/pixel, channel order: B G R.

§

Xbgr8 = 4

4 bytes/pixel, channel order: X B G R, where X = 255. Used by the Cairo and Qt/QImage backends.

§

Cmyk8 = 5

4 bytes/pixel: C M Y K.

§

DeviceN8 = 6

8 bytes/pixel: C M Y K + 4 spot channels. Corresponds to SPOT_NCOMPS = 4 in SplashTypes.h.

Implementations§

Source§

impl PixelMode

Source

pub const fn bytes_per_pixel(self) -> usize

Returns the number of bytes occupied by a single pixel in this mode.

§Packed-bits exception

Returns 0 for Mono1. Callers must check is_packed_bits before dividing or multiplying by this value, or use pixel_count_to_bytes which handles the case safely.

§Exhaustiveness

The implementation uses an exhaustive match, so adding a new variant without updating this function is a compile error.

Source

pub const fn bits_per_pixel(self) -> usize

Returns the number of bits per pixel for this mode.

Unlike bytes_per_pixel, this method always returns a meaningful positive value, making it safe to use for Mono1 without a prior guard.

ModeBits
Mono11
Mono88
Rgb824
Bgr824
Xbgr832
Cmyk832
DeviceN864
Source

pub const fn is_packed_bits(self) -> bool

Returns true if pixels are packed at sub-byte granularity.

Currently only Mono1 is packed. For packed modes, bytes_per_pixel returns 0 and must not be used for per-pixel arithmetic; use the row-stride formula (width + 7) / 8 instead.

Source

pub const fn from_u8(v: u8) -> Option<PixelMode>

Converts a raw discriminant byte into a PixelMode, or None if the value does not correspond to any variant.

Prefer this over unsafe transmute or unchecked as casts when parsing mode values from untrusted sources (e.g. C FFI, file headers).

assert_eq!(PixelMode::from_u8(2), Some(PixelMode::Rgb8));
assert_eq!(PixelMode::from_u8(99), None);
Source

pub const fn pixel_count_to_bytes(self, count: usize) -> Option<usize>

Multiplies count pixels by bytes_per_pixel, returning None on overflow or when the mode is Mono1 (packed bits have no per-pixel byte count).

Use this instead of count * mode.bytes_per_pixel() to avoid panics or silent overflow in release builds.

assert_eq!(PixelMode::Rgb8.pixel_count_to_bytes(10), Some(30));
assert_eq!(PixelMode::Mono1.pixel_count_to_bytes(10), None);
assert_eq!(PixelMode::Rgb8.pixel_count_to_bytes(usize::MAX), None);

Trait Implementations§

Source§

impl Clone for PixelMode

Source§

fn clone(&self) -> PixelMode

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 PixelMode

Source§

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

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

impl Default for PixelMode

Source§

fn default() -> PixelMode

Returns the “default value” for a type. Read more
Source§

impl Hash for PixelMode

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for PixelMode

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for PixelMode

Source§

impl Eq for PixelMode

Source§

impl StructuralPartialEq for PixelMode

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