Struct pix::Raster

source · []
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

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);

Construct a Raster with all pixels set to one color.

Panics

Panics if width or height is greater than std::i32::MAX.

Example
use pix::rgb::SRgb8;
use pix::Raster;

let clr = SRgb8::new(0x40, 0xAA, 0xBB);
let r = Raster::<SRgb8>::with_color(15, 15, clr);

Construct a Raster with another Raster.

The pixel format can be converted using this method.

  • S Pixel format of source Raster.
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);

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().

  • B Owned pixed type (Vec or boxed slice).
  • width Width of Raster.
  • height Height of Raster.
  • pixels Pixel data.
Panics
  • If width or height is greater than std::i32::MAX
  • If pixels length is not equal to width * 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 vec

Construct a Raster from a u8 buffer.

  • B Owned pixed type (Vec or boxed slice).
  • width Width of Raster.
  • height Height of Raster.
  • buffer Buffer of pixel data.
Panics
  • If width or height is greater than std::i32::MAX
  • If buffer length is not equal to width * height * std::mem::size_of::<P>()

Construct a Raster from a u16 buffer.

  • B Owned pixed type (Vec or boxed slice).
  • width Width of Raster.
  • height Height of Raster.
  • buffer Buffer of pixel data (in native-endian byte order).
Panics
  • If width or height is greater than std::i32::MAX
  • If buffer length is not equal to width * height * std::mem::size_of::<P>()

Get width of Raster.

Get height of Raster.

Clear all pixels to default value.

Get one pixel.

Get a mutable pixel.

Get a slice of all pixels.

Get a mutable slice of all pixels.

Get an Iterator of rows within a Raster.

  • reg Region of the Raster to iterate.

Get an Iterator of mutable rows within a Raster.

  • reg Region of the Raster to iterate.

Get Region of entire Raster.

Get intersection with a Region.

Copy a color to a region of the Raster.

  • reg Region within self. It can be a Region struct, tuple of (x, y, width, height) or the unit type (). Using () has the same result as Raster::region().
  • clr Source Pixel color.
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);

Copy from a source Raster.

  • to Region within self (destination).
  • src Source Raster.
  • from Region within source Raster.

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, ());

Get view of pixels as a u8 slice.

Get view of pixels as a mutable u8 slice.

Composite a source color to a region of the Raster.

  • reg Region within self. It can be a Region struct, tuple of (x, y, width, height) or the unit type (). Using () has the same result as Raster::region().
  • clr Source Pixel color.
  • op Compositing 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);

Composite from a matte Raster and color.

  • to Region within self (destination).
  • src Source Raster matte.
  • from Region within source Raster.
  • clr Color to apply to the matte.
  • op Compositing 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);

Composite from a source Raster.

  • to Region within self (destination).
  • src Source Raster.
  • from Region within source Raster.
  • op Compositing 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);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Get internal pixel data as boxed slice.

Get internal pixel data as Vec of pixels.

Get internal pixel data as boxed slice of u8.

Get internal pixel data as boxed slice of u16.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.