Struct Framebuffer

Source
pub struct Framebuffer { /* private fields */ }

Implementations§

Source§

impl Framebuffer

Source

pub fn crop( &self, x: u32, y: u32, width: u32, height: u32, ) -> Result<Framebuffer, ImageRegionError>

Copies a color rectangle into a new, tightly packed framebuffer.

(x, y) is the top-left pixel; rows proceed downward. Width and height must be nonzero, and the entire rectangle must fit; there is no clipping. Every RGBA byte is copied verbatim, including RGB under zero alpha, with no blending, premultiplication or color-space conversion. The source is unchanged and the new framebuffer’s depth values start at positive infinity. Allocation is bounded by the existing framebuffer dimension/pixel limits.

§Errors

Returns ImageRegionError::InvalidDimensions for an empty rectangle, or ImageRegionError::OutOfBounds if it extends outside the source. Invalid rectangles are rejected before allocation.

use ferravex_graphics::Framebuffer;

let mut atlas = Framebuffer::new(4, 3).unwrap();
atlas.pixels_mut()[6] = [200, 100, 50, 0];
let tile = atlas.crop(2, 1, 2, 2).unwrap();
assert_eq!((tile.width(), tile.height()), (2, 2));
assert_eq!(tile.pixel(0, 0), Some([200, 100, 50, 0]));
assert!(tile.depths().iter().all(|&depth| depth == f32::INFINITY));
assert!(atlas.crop(u32::MAX, 0, 2, 2).is_err());
Source

pub fn blit_rgba( &mut self, x: u32, y: u32, width: u32, height: u32, source: &[[u8; 4]], source_stride: usize, ) -> Result<(), ImageRegionError>

Replaces a color rectangle from row-major RGBA pixels without allocating.

(x, y) is the destination’s top-left pixel; rows proceed downward. Width and height must be nonzero and fit completely in the destination. source[0] is the source region’s top-left pixel. source_stride is the distance between source row starts in pixels, not bytes, and must be at least width. Only (height - 1) * source_stride + width pixels are required: padding after the final row is optional. Padding and trailing pixels are ignored; a subslice can select a region of a larger source.

All four channels replace the destination verbatim, even at zero alpha. There is no blending, premultiplication or color-space conversion; either straight or premultiplied input retains its original representation. Destination depth values and pixels outside the rectangle are unchanged. Safe borrowing requires separate source and destination storage.

§Errors

Validates nonzero dimensions, destination bounds, source stride, checked layout arithmetic and source length, in that order. Any error leaves the entire framebuffer unchanged, including when the final source row is short.

use ferravex_graphics::Framebuffer;

let mut source = Framebuffer::new(4, 3).unwrap();
source.pixels_mut()[5] = [120, 60, 30, 128];
let mut atlas = Framebuffer::new(6, 4).unwrap();
// Select a 2x2 region at source (1, 1) without allocating a crop.
atlas.blit_rgba(3, 2, 2, 2, &source.pixels()[5..], 4).unwrap();
assert_eq!(atlas.pixel(3, 2), Some([120, 60, 30, 128]));
let before = atlas.pixels().to_vec();
assert!(atlas.blit_rgba(0, 0, 2, 2, &[[0; 4]; 3], 2).is_err());
assert_eq!(atlas.pixels(), before);
Source

pub fn to_ppm(&self) -> Vec<u8> ⓘ

Binary RGB PPM, portable and intentionally uncompressed.

Source

pub fn to_png(&self) -> Vec<u8> ⓘ

RGBA PNG with original CRC/Adler and uncompressed DEFLATE blocks. This trades file size for a small dependency-free, browser-viewable export.

Source

pub fn save_png(&self, path: impl AsRef<Path>) -> Result<(), Error>

Source

pub fn save_ppm(&self, path: impl AsRef<Path>) -> Result<(), Error>

Source§

impl Framebuffer

Source

pub fn new(width: u32, height: u32) -> Result<Framebuffer, RenderError>

Source

pub const fn width(&self) -> u32

Source

pub const fn height(&self) -> u32

Source

pub fn pixels_mut(&mut self) -> &mut [[u8; 4]]

Mutable color storage for 2D rasterizers; dimensions and depth storage remain fixed.

Source

pub fn pixels(&self) -> &[[u8; 4]]

Source

pub fn depths(&self) -> &[f32]

Source

pub fn pixel(&self, x: u32, y: u32) -> Option<[u8; 4]>

Source

pub fn clear(&mut self, color: Color)

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.