pub struct Framebuffer { /* private fields */ }Implementations§
Source§impl Framebuffer
impl Framebuffer
Sourcepub fn crop(
&self,
x: u32,
y: u32,
width: u32,
height: u32,
) -> Result<Framebuffer, ImageRegionError>
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());Sourcepub fn blit_rgba(
&mut self,
x: u32,
y: u32,
width: u32,
height: u32,
source: &[[u8; 4]],
source_stride: usize,
) -> Result<(), ImageRegionError>
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);Sourcepub fn to_png(&self) -> Vec<u8> ⓘ
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.
pub fn save_png(&self, path: impl AsRef<Path>) -> Result<(), Error>
pub fn save_ppm(&self, path: impl AsRef<Path>) -> Result<(), Error>
Source§impl Framebuffer
impl Framebuffer
pub fn new(width: u32, height: u32) -> Result<Framebuffer, RenderError>
pub const fn width(&self) -> u32
pub const fn height(&self) -> u32
Sourcepub fn pixels_mut(&mut self) -> &mut [[u8; 4]]
pub fn pixels_mut(&mut self) -> &mut [[u8; 4]]
Mutable color storage for 2D rasterizers; dimensions and depth storage remain fixed.