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
impl Image
Sourcepub fn from_premultiplied_rgba(
width: u32,
height: u32,
pixels: Vec<u8>,
) -> Result<Image, ImageError>
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,
}
));Sourcepub fn from_straight_rgba(
width: u32,
height: u32,
pixels: Vec<u8>,
) -> Result<Image, ImageError>
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]);