pub struct Raster<P: Pixel> { /* private fields */ }Expand description
Image arranged as a rectangular array of pixels. Rows are ordered top to bottom, and pixels within rows are left to right.
A Raster can be constructed using one of the with_ methods:
§Working with byte buffers
To allow interoperability with other crates, images can be created from byte buffers, and converted back again.
use pix::rgb::Rgba8;
use pix::Raster;
let buf = vec![0; 200 * 200 * std::mem::size_of::<Rgba8>()];
let mut raster = Raster::<Rgba8>::with_u8_buffer(200, 200, buf);
// ... manipulate the image
let slice: Box<[u8]> = raster.into();
// A boxed slice can be turned back into Vec
let v: Vec<u8> = slice.into();Implementations§
Source§impl<P: Pixel> Raster<P>
impl<P: Pixel> Raster<P>
Sourcepub fn with_clear(width: u32, height: u32) -> Self
pub fn with_clear(width: u32, height: u32) -> Self
Construct a Raster with all pixels set to the default value.
§Panics
Panics if width or height is greater than std::i32::MAX.
§Examples
use pix::gray::{SGray8, SGraya32};
use pix::matte::Matte8;
use pix::rgb::SRgb16;
use pix::Raster;
let r1 = Raster::<SGray8>::with_clear(20, 20);
let r2 = Raster::<Matte8>::with_clear(64, 64);
let r3 = Raster::<SRgb16>::with_clear(10, 10);
let r4 = Raster::<SGraya32>::with_clear(100, 250);Sourcepub fn with_color(width: u32, height: u32, clr: P) -> Self
pub fn with_color(width: u32, height: u32, clr: P) -> Self
Sourcepub fn with_raster<S>(src: &Raster<S>) -> Self
pub fn with_raster<S>(src: &Raster<S>) -> Self
Construct a Raster with another Raster.
The pixel format can be converted using this method.
SPixelformat of sourceRaster.
§Convert from SRgb8 to Rgba16
use pix::rgb::{Rgba16, SRgb8};
use pix::Raster;
let mut r0 = Raster::<SRgb8>::with_clear(50, 50);
// load pixels into raster
let r1 = Raster::<Rgba16>::with_raster(&r0);Sourcepub fn with_pixels<B>(width: u32, height: u32, pixels: B) -> Self
pub fn with_pixels<B>(width: u32, height: u32, pixels: B) -> Self
Construct a Raster with owned pixel data. You can get ownership of
the pixel data back from the Raster as either a Vec<P> or a
Box<[P]> by calling into().
BOwned pixed type (Vecor boxed slice).widthWidth ofRaster.heightHeight ofRaster.pixelsPixel data.
§Panics
- If
widthorheightis greater thanstd::i32::MAX - If
pixelslength is not equal towidth*height
§Example
use pix::ops::Src;
use pix::rgb::Rgb8;
use pix::Raster;
let p = vec![Rgb8::new(255, 0, 255); 16]; // vec of magenta pix
let mut r = Raster::with_pixels(4, 4, p); // convert to raster
let clr = Rgb8::new(0x00, 0xFF, 0x00); // green
r.copy_color((2, 0, 1, 3), clr); // make stripe
let p2 = Into::<Vec<Rgb8>>::into(r); // back to vecSourcepub fn with_u8_buffer<B>(width: u32, height: u32, buffer: B) -> Self
pub fn with_u8_buffer<B>(width: u32, height: u32, buffer: B) -> Self
Construct a Raster from a u8 buffer.
BOwned pixed type (Vecor boxed slice).widthWidth ofRaster.heightHeight ofRaster.bufferBuffer of pixel data.
§Panics
- If
widthorheightis greater thanstd::i32::MAX - If
bufferlength is not equal towidth*height*std::mem::size_of::<P>()
Sourcepub fn with_u16_buffer<B>(width: u32, height: u32, buffer: B) -> Self
pub fn with_u16_buffer<B>(width: u32, height: u32, buffer: B) -> Self
Construct a Raster from a u16 buffer.
BOwned pixed type (Vecor boxed slice).widthWidth ofRaster.heightHeight ofRaster.bufferBuffer of pixel data (in native-endian byte order).
§Panics
- If
widthorheightis greater thanstd::i32::MAX - If
bufferlength is not equal towidth*height*std::mem::size_of::<P>()
Sourcepub fn pixels_mut(&mut self) -> &mut [P]
pub fn pixels_mut(&mut self) -> &mut [P]
Get a mutable slice of all pixels.
Sourcepub fn rows<R>(&self, reg: R) -> Rows<'_, P> ⓘ
pub fn rows<R>(&self, reg: R) -> Rows<'_, P> ⓘ
Get an Iterator of rows within a Raster.
regRegion of the Raster to iterate.
Sourcepub fn rows_mut<R>(&mut self, reg: R) -> RowsMut<'_, P> ⓘ
pub fn rows_mut<R>(&mut self, reg: R) -> RowsMut<'_, P> ⓘ
Get an Iterator of mutable rows within a Raster.
regRegion of the Raster to iterate.
Sourcepub fn intersection<R>(&self, reg: R) -> Region
pub fn intersection<R>(&self, reg: R) -> Region
Get intersection with a Region.
Sourcepub fn copy_color<R>(&mut self, reg: R, clr: P)
pub fn copy_color<R>(&mut self, reg: R, clr: P)
Copy a color to a region of the Raster.
regRegion withinself. It can be aRegionstruct, tuple of (x, y, width, height) or the unit type(). Using()has the same result asRaster::region().clrSourcePixelcolor.
§Copy a color to a rectangle region
use pix::rgb::SRgb8;
use pix::Raster;
let mut r = Raster::with_clear(100, 100);
let clr = SRgb8::new(0xDD, 0x96, 0x70);
r.copy_color((20, 40, 25, 50), clr);Sourcepub fn copy_raster<R0, R1>(&mut self, to: R0, src: &Raster<P>, from: R1)
pub fn copy_raster<R0, R1>(&mut self, to: R0, src: &Raster<P>, from: R1)
Copy from a source Raster.
toRegion withinself(destination).srcSourceRaster.fromRegion within sourceRaster.
to / from can be Region structs, tuples of (x, y, width,
height) or the unit type (). Using () has the same result as
Raster::region().
*------------+ *-------------+
| | | *------+ |
| *------+ | | | | |
| | | | | | from | |
| | to | | <--- | +------+ |
| +------+ | | |
| | | src |
| self | +-------------+
+------------+The copied Region is clamped to the smaller of to and from in
both X and Y dimensions. Also, to and from are clipped to
their respective Raster dimensions.
§Copy part of one Raster to another
use pix::rgb::SRgb8;
use pix::Raster;
let mut r0 = Raster::with_clear(100, 100);
let r1 = Raster::with_color(5, 5, SRgb8::new(80, 0, 80));
// ... load image data
r0.copy_raster((40, 40, 5, 5), &r1, ());Sourcepub fn as_u8_slice(&self) -> &[u8] ⓘ
pub fn as_u8_slice(&self) -> &[u8] ⓘ
Get view of pixels as a u8 slice.
Sourcepub fn as_u8_slice_mut(&mut self) -> &mut [u8] ⓘ
pub fn as_u8_slice_mut(&mut self) -> &mut [u8] ⓘ
Get view of pixels as a mutable u8 slice.
Source§impl<P> Raster<P>
impl<P> Raster<P>
Sourcepub fn composite_color<R, O>(&mut self, reg: R, clr: P, op: O)
pub fn composite_color<R, O>(&mut self, reg: R, clr: P, op: O)
Composite a source color to a region of the Raster.
regRegion withinself. It can be aRegionstruct, tuple of (x, y, width, height) or the unit type(). Using()has the same result asRaster::region().clrSourcePixelcolor.opCompositing operation.
§Example
use pix::ops::SrcOver;
use pix::bgr::Bgra8p;
use pix::Raster;
let mut r = Raster::with_color(100, 100, Bgra8p::new(99, 0, 99, 255));
let clr = Bgra8p::new(200, 200, 0, 128);
r.composite_color((20, 40, 25, 50), clr, SrcOver);Sourcepub fn composite_matte<R0, R1, M, O>(
&mut self,
to: R0,
src: &Raster<M>,
from: R1,
clr: P,
op: O,
)
pub fn composite_matte<R0, R1, M, O>( &mut self, to: R0, src: &Raster<M>, from: R1, clr: P, op: O, )
Composite from a matte Raster and color.
toRegion withinself(destination).srcSourceRastermatte.fromRegion within sourceRaster.clrColor to apply to the matte.opCompositing operation.
to / from can be Region structs, tuples of (x, y, width,
height) or the unit type (). Using () has the same result as
Raster::region().
§Example
use pix::matte::Matte8;
use pix::ops::SrcOver;
use pix::rgb::Rgba8p;
use pix::Raster;
let mut r0 = Raster::with_clear(100, 100);
let r1 = Raster::with_color(10, 10, Matte8::new(37));
// ... load image data
let clr = Rgba8p::new(50, 100, 150, 200);
r0.composite_matte((30, 50, 10, 10), &r1, (), clr, SrcOver);Sourcepub fn composite_raster<R0, R1, O>(
&mut self,
to: R0,
src: &Raster<P>,
from: R1,
op: O,
)
pub fn composite_raster<R0, R1, O>( &mut self, to: R0, src: &Raster<P>, from: R1, op: O, )
Composite from a source Raster.
toRegion withinself(destination).srcSourceRaster.fromRegion within sourceRaster.opCompositing operation.
to / from can be Region structs, tuples of (x, y, width,
height) or the unit type (). Using () has the same result as
Raster::region().
*------------+ *-------------+
| | | *------+ |
| *------+ | | | | |
| | | | | | from | |
| | to | | <--- | +------+ |
| +------+ | | |
| | | src |
| self | +-------------+
+------------+The composited Region is clamped to the smaller of to and from in
both X and Y dimensions. Also, to and from are clipped to
their respective Raster dimensions.
§Blend one Raster onto another
use pix::ops::SrcOver;
use pix::rgb::Rgba8p;
use pix::Raster;
let mut r0 = Raster::with_clear(100, 100);
let r1 = Raster::with_color(5, 5, Rgba8p::new(80, 0, 80, 200));
// ... load image data
r0.composite_raster((40, 40), &r1, (), SrcOver);