#[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
impl PixelMode
Sourcepub const fn bytes_per_pixel(self) -> usize
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.
Sourcepub const fn bits_per_pixel(self) -> usize
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.
| Mode | Bits |
|---|---|
Mono1 | 1 |
Mono8 | 8 |
Rgb8 | 24 |
Bgr8 | 24 |
Xbgr8 | 32 |
Cmyk8 | 32 |
DeviceN8 | 64 |
Sourcepub const fn is_packed_bits(self) -> bool
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.
Sourcepub const fn from_u8(v: u8) -> Option<PixelMode>
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);Sourcepub const fn pixel_count_to_bytes(self, count: usize) -> Option<usize>
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);