pub struct AlphaMask { /* private fields */ }Expand description
An 8-bit coverage plane: one byte per pixel, 255 fully opaque.
This is both a soft mask (ISO 32000 §11.6.5) and a clip mask. It is always
sized and aligned to the device it applies to, because a mismatched mask
is silently ignored by vello_cpu and merely warned about by
tiny-skia — it fails open.
use pdfrum_render::AlphaMask;
let mask = AlphaMask::filled(2, 2, 128);
assert_eq!((mask.width(), mask.height()), (2, 2));
// One byte per pixel; 255 is fully opaque.
assert_eq!(mask.data(), &[128; 4]);Implementations§
Source§impl AlphaMask
impl AlphaMask
Sourcepub fn new(width: u32, height: u32) -> Self
pub fn new(width: u32, height: u32) -> Self
A fully transparent (all-zero) mask.
use pdfrum_render::AlphaMask;
// Fully transparent: all zero.
assert_eq!(AlphaMask::new(2, 1).data(), &[0, 0]);Sourcepub fn filled(width: u32, height: u32, value: u8) -> Self
pub fn filled(width: u32, height: u32, value: u8) -> Self
A mask with every byte set to value.
use pdfrum_render::AlphaMask;
assert_eq!(AlphaMask::filled(2, 1, 255).data(), &[255, 255]);Sourcepub fn from_vec(width: u32, height: u32, data: Vec<u8>) -> Option<Self>
pub fn from_vec(width: u32, height: u32, data: Vec<u8>) -> Option<Self>
Wrap an existing coverage plane, or None on a size mismatch.
use pdfrum_render::AlphaMask;
assert!(AlphaMask::from_vec(2, 1, vec![0, 255]).is_some());
// Not exactly `width * height` bytes.
assert!(AlphaMask::from_vec(2, 1, vec![0]).is_none());Sourcepub fn width(&self) -> u32
pub fn width(&self) -> u32
The mask’s width in pixels.
use pdfrum_render::AlphaMask;
assert_eq!(AlphaMask::new(3, 2).width(), 3);Sourcepub fn height(&self) -> u32
pub fn height(&self) -> u32
The mask’s height in pixels.
use pdfrum_render::AlphaMask;
assert_eq!(AlphaMask::new(3, 2).height(), 2);Sourcepub fn data(&self) -> &[u8] ⓘ
pub fn data(&self) -> &[u8] ⓘ
The coverage bytes, row-major.
use pdfrum_render::AlphaMask;
// One byte per pixel, row-major.
assert_eq!(AlphaMask::new(2, 2).data().len(), 4);Sourcepub fn data_mut(&mut self) -> &mut [u8] ⓘ
pub fn data_mut(&mut self) -> &mut [u8] ⓘ
The coverage bytes, mutably.
use pdfrum_render::AlphaMask;
let mut mask = AlphaMask::new(1, 1);
mask.data_mut()[0] = 255;
assert_eq!(mask.data(), &[255]);Sourcepub fn into_data(self) -> Vec<u8> ⓘ
pub fn into_data(self) -> Vec<u8> ⓘ
Consume the mask, yielding its coverage bytes.
use pdfrum_render::AlphaMask;
assert_eq!(AlphaMask::filled(1, 1, 200).into_data(), vec![200]);Sourcepub fn intersect(&mut self, other: &Self)
pub fn intersect(&mut self, other: &Self)
Intersect with another mask of the same size: the truncating integer
product old * new / 255.
use pdfrum_render::AlphaMask;
let mut mask = AlphaMask::filled(1, 1, 255);
// The truncating integer product `old * new / 255`.
mask.intersect(&AlphaMask::filled(1, 1, 128));
assert_eq!(mask.data(), &[128]);Sourcepub fn apply_transfer(&mut self, table: &[u8; 256])
pub fn apply_transfer(&mut self, table: &[u8; 256])
Map every byte through a 256-entry lookup — a soft mask’s /TR.
use pdfrum_render::AlphaMask;
let mut mask = AlphaMask::filled(1, 1, 10);
// A soft mask's `/TR`, as a 256-entry lookup.
let mut table = [0u8; 256];
table[10] = 200;
mask.apply_transfer(&table);
assert_eq!(mask.data(), &[200]);Sourcepub fn placed_in(&self, width: u32, height: u32, x: i32, y: i32) -> Self
pub fn placed_in(&self, width: u32, height: u32, x: i32, y: i32) -> Self
Place src at (x, y) in a device-sized mask, zero everywhere else.
This is E8’s padding: a soft mask is produced at the clip rect’s size
and must be handed to push_layer device-sized.
use pdfrum_render::AlphaMask;
// A mask produced at a clip rect's size, padded out to the device.
let small = AlphaMask::filled(1, 1, 255);
let placed = small.placed_in(2, 2, 1, 1);
assert_eq!(placed.data(), &[0, 0, 0, 255]);