Struct fimg::Image

source ·
pub struct Image<T, const CHANNELS: usize> { /* private fields */ }
Expand description

A image with a variable number of channels, and a nonzero size.

Implementations§

source§

impl<const CHANNELS: usize, T: AsMut<[u8]> + AsRef<[u8]>> Image<T, CHANNELS>

source

pub fn flip_v(&mut self)

Flip an image vertically.

source

pub fn flip_h(&mut self)

Flip an image horizontally.

source§

impl<const CHANNELS: usize, T: AsMut<[u8]> + AsRef<[u8]>> Image<T, CHANNELS>

source

pub fn rot_180(&mut self)

Rotate an image 180 degrees clockwise.

source

pub unsafe fn rot_90(&mut self)

Rotate an image 90 degrees clockwise.

Safety

UB if the image is not square

source

pub unsafe fn rot_270(&mut self)

Rotate an image 270 degrees clockwise, or 90 degrees anti clockwise.

Safety

UB if the image is not square

source§

impl<B: Buffer, const C: usize> Image<B, C>

source

pub const fn build(w: u32, h: u32) -> Builder<B, C>

creates a builder

source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS>

source

pub fn box( &mut self, (x1, y1): (u32, u32), width: u32, height: u32, c: [u8; CHANNELS] )

Draw a bordered box

let mut b = Image::alloc(10, 9);
b.as_mut().r#box((1, 1), 7, 6, [255]);
source

pub fn filled_box( &mut self, (x1, y1): (u32, u32), width: u32, height: u32, c: [u8; CHANNELS] )

Draw a filled box.

let mut b = Image::alloc(10, 9);
b.as_mut().filled_box((1, 1), 7, 6, [255]);
source

pub fn stroked_box( &mut self, (x1, y1): (u32, u32), width: u32, height: u32, stroke: u32, c: [u8; CHANNELS] )

Draw a stroked box

let mut b = Image::alloc(11, 11);
b.as_mut().stroked_box((2, 2), 6, 6, 2, [255]);
source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS>

source

pub fn border_circle( &mut self, (xc, yc): (i32, i32), radius: i32, c: [u8; CHANNELS] )

Draws a circle, using the Bresenham’s circle algorithm.

let mut i = Image::alloc(50, 50);
i.border_circle((25, 25), 20, [255]);
source

pub fn circle(&mut self, (xc, yc): (i32, i32), radius: i32, c: [u8; CHANNELS])

Draw a filled circle.

let mut i = Image::alloc(50, 50);
i.circle((25, 25), 20, [255]);
source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS>

source

pub fn line(&mut self, a: (i32, i32), b: (i32, i32), color: [u8; CHANNELS])

Draw a line from point a to point b.

Points not in bounds will not be included.

Uses bresenshams line algorithm.

source

pub fn thick_line( &mut self, a: impl Into<Vec2>, b: impl Into<Vec2>, stroke: f32, color: [u8; CHANNELS] )

Draw a thick line from point a to point b. Prefer Image::line when possible.

Points not in bounds will not be included.

Uses Image::quad.

let mut i = Image::alloc(10, 10);
i.thick_line((2.0, 2.0), (8.0, 8.0), 2.0, [255]);
source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS>

source

pub fn points(&mut self, poly: &[(i32, i32)], c: [u8; CHANNELS])

Draws a filled polygon from a slice of points. Please close your poly. (first == last)

Borrowed from imageproc, modified for less allocations.

let mut i = Image::alloc(10, 10);
i.points(&[(1, 8), (3, 1), (8, 1), (6, 6), (8, 8), (1, 8)], [255]);
source

pub fn quad( &mut self, a: (i32, i32), b: (i32, i32), c: (i32, i32), d: (i32, i32), col: [u8; CHANNELS] )

Draws a filled quadrilateral. This currently just uses Image::points, but in the future this may change.

source

pub fn poly( &mut self, pos: impl Into<Vec2>, sides: usize, radius: f32, rotation: f32, c: [u8; CHANNELS] )

Draws a regular convex polygon with a specified number of sides, a radius, and a rotation (radians). Prefer Image::circle over poly(.., 600, ..). Calls into Image::tri and Image::quad.

let mut i = Image::alloc(300, 300);
//          draw a enneagon
// at  x150, y150    │  unrotated   white
// with a radius of ─┼──╮      │      │
i.poly((150., 150.), 9, 100.0, 0.0, [255]);
source

pub fn border_poly( &mut self, pos: impl Into<Vec2>, sides: usize, radius: f32, rotation: f32, stroke: f32, c: [u8; CHANNELS] )

Draw a bordered polygon. Prefer Image::border_circle to draw circles. See also Image::poly.

let mut i = fimg::Image::alloc(100, 100);
i.border_poly((50., 50.), 5, 25., 0., 7., [255]);
source§

impl<const N: usize, T: AsMut<[u8]> + AsRef<[u8]>> Image<T, N>

source

pub fn text( &mut self, x: u32, y: u32, size: f32, font: &Font, text: &str, color: [u8; N] )

Available on crate feature text only.

Draw text.

let font = fontdue::Font::from_bytes(
    &include_bytes!("../../tdata/CascadiaCode.ttf")[..],
    fontdue::FontSettings {
        scale: 200.0,
        ..Default::default()
    },
).unwrap();
let mut i: Image<_, 4> = Image::alloc(750, 250).boxed();
i.text(50, 10, 200.0, &font, "hello", [0, 0, 0, 255]);
source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS>

source

pub fn tri( &mut self, b: impl Into<Vec2>, a: impl Into<Vec2>, c: impl Into<Vec2>, col: [u8; CHANNELS] )

Draw a (filled) triangle

let mut a = Image::alloc(10, 10);
// draw a triangle
a.as_mut().tri(
  (3.0, 2.0), // point a
  (8.0, 7.0), // point b
  (1.0, 8.0), // point c
  [255] // white
);
source§

impl<T: AsMut<[u8]> + AsRef<[u8]>> Image<T, 1>

source

pub fn scale<A: ScalingAlgorithm>( &self, width: u32, height: u32 ) -> Image<Box<[u8]>, 1>

Available on crate feature scale only.

Scale a Y image with a given scaling algorithm.

source§

impl<T: AsMut<[u8]> + AsRef<[u8]>> Image<T, 2>

source

pub fn scale<A: ScalingAlgorithm>( &mut self, width: u32, height: u32 ) -> Image<Box<[u8]>, 2>

Available on crate feature scale only.

Scale a YA image with a given scaling algorithm.

source§

impl<T: AsMut<[u8]> + AsRef<[u8]>> Image<T, 3>

source

pub fn scale<A: ScalingAlgorithm>( &self, width: u32, height: u32 ) -> Image<Box<[u8]>, 3>

Available on crate feature scale only.

Scale a RGB image with a given scaling algorithm.

source§

impl<T: AsMut<[u8]> + AsRef<[u8]>> Image<T, 4>

source

pub fn scale<A: ScalingAlgorithm>( &mut self, width: u32, height: u32 ) -> Image<Box<[u8]>, 4>

Available on crate feature scale only.

Scale a RGBA image with a given scaling algorithm.

source§

impl Image<&[u8], 3>

source

pub unsafe fn repeated( &self, out_width: u32, out_height: u32 ) -> Image<Vec<u8>, 3>

Tile self till it fills a new image of size x, y

Safety

UB if self’s width is not a multiple of x, or self’s height is not a multiple of y

let x: Image<&[u8], 3> = Image::build(8, 8).buf(include_bytes!("../benches/3_8x8.imgbuf"));
let tiled = unsafe { x.repeated(48, 48) }; // repeat 6 times
source§

impl<T, const CHANNELS: usize> Image<T, CHANNELS>

source

pub fn height(&self) -> u32

get the height as a u32

source

pub fn width(&self) -> u32

get the width as a u32

source

pub const unsafe fn new( width: NonZeroU32, height: NonZeroU32, buffer: T ) -> Self

create a new image

Safety

does not check that buffer.len() == w * h * C

using this with invalid values may result in future UB

source

pub fn take_buffer(self) -> T

consumes the image, returning the image buffer

source

pub const fn buffer(&self) -> &T

returns a immutable reference to the backing buffer

source

pub unsafe fn buffer_mut(&mut self) -> &mut T

returns a mutable(!) reference to the backing buffer

Safety

please do not change buffer size.

source§

impl<const CHANNELS: usize, T: Clone> Image<&[T], CHANNELS>

source

pub fn to_owned(&self) -> Image<Vec<T>, CHANNELS>

Allocate a new Image<Vec<T>> from this imageref.

source§

impl<const CHANNELS: usize, T: Clone> Image<&mut [T], CHANNELS>

source

pub fn to_owned(&self) -> Image<Vec<T>, CHANNELS>

Allocate a new Image<Vec<T>> from this mutable imageref.

source§

impl<const CHANNELS: usize> Image<&[u8], CHANNELS>

source

pub const fn copy(&self) -> Self

Copy this ref image

source

pub const fn make<'a, const WIDTH: u32, const HEIGHT: u32>( ) -> Image<&'a [u8], CHANNELS>

Create a new immutable image of width x, y.

Panics

if width || height == 0

let img = Image::make::<5, 5>();
source§

impl<const CHANNELS: usize, const N: usize> Image<[u8; N], CHANNELS>

source

pub fn boxed(self) -> Image<Box<[u8]>, CHANNELS>

Box this array image.

source§

impl<const CHANNELS: usize> Image<&[u8], CHANNELS>

source

pub fn boxed(self) -> Image<Box<[u8]>, CHANNELS>

Box this image.

source§

impl<const CHANNELS: usize> Image<Vec<u8>, CHANNELS>

source

pub fn boxed(self) -> Image<Box<[u8]>, CHANNELS>

Box this owned image.

source§

impl<T: AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS>

source

pub fn len(&self) -> usize

The size of the underlying buffer.

source

pub fn cloner(&self) -> ImageCloner<'_, CHANNELS>

Procure a ImageCloner.

source

pub fn as_ref(&self) -> Image<&[u8], CHANNELS>

Reference this image.

source

pub fn chunked(&self) -> impl DoubleEndedIterator<Item = &[u8; CHANNELS]>

Returns a iterator over every pixel

source

pub fn flatten(&self) -> &[[u8; CHANNELS]]

Flatten the chunks of this image into a slice of slices.

source

pub unsafe fn pixel(&self, x: u32, y: u32) -> [u8; CHANNELS]

Return a pixel at (x, y).

Safety
  • UB if x, y is out of bounds
  • UB if buffer is too small
source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS>

source

pub unsafe fn pixel_mut(&mut self, x: u32, y: u32) -> &mut [u8]

Return a mutable reference to a pixel at (x, y).

Safety
  • UB if x, y is out of bounds
  • UB if buffer is too small
source

pub fn chunked_mut(&mut self) -> impl Iterator<Item = &mut [u8; CHANNELS]>

Returns a iterator over every pixel, mutably

source

pub fn as_mut(&mut self) -> Image<&mut [u8], CHANNELS>

Create a mutref to this image

source

pub fn flatten_mut(&mut self) -> &mut [[u8; CHANNELS]]

Flatten the chunks of this image into a mutable slice of slices.

source

pub unsafe fn set_pixel(&mut self, x: u32, y: u32, px: [u8; CHANNELS])

Set the pixel at x, y

Safety

UB if x, y is out of bounds.

source§

impl<const CHANNELS: usize> Image<&mut [u8], CHANNELS>

source

pub fn copy(&mut self) -> Image<&mut [u8], CHANNELS>

Copy this ref image

source§

impl<const CHANNELS: usize> Image<Vec<u8>, CHANNELS>

source

pub fn alloc(width: u32, height: u32) -> Self

Allocates a new image. If width and height are constant, try using make.

Panics

if width || height == 0

source

pub fn leak(self) -> Image<&'static mut [u8], CHANNELS>

Consumes and leaks this image, returning a reference to the image.

source§

impl<const CHANNELS: usize, T: ?Sized> Image<Box<T>, CHANNELS>

source

pub fn leak(self) -> Image<&'static mut T, CHANNELS>

Consumes and leaks this image, returning a reference to the image.

source§

impl<const CHANNELS: usize> Image<Vec<u8>, CHANNELS>

source

pub fn open(f: impl AsRef<Path>) -> Self

Available on crate feature save only.

Open a PNG image

source§

impl<T: AsRef<[u8]>> Image<T, 3>

source

pub fn save(&self, f: impl AsRef<Path>)

Available on crate feature save only.

Save this RGB image.

source§

impl<T: AsRef<[u8]>> Image<T, 4>

source

pub fn save(&self, f: impl AsRef<Path>)

Available on crate feature save only.

Save this RGBA image.

source§

impl<T: AsRef<[u8]>> Image<T, 2>

source

pub fn save(&self, f: impl AsRef<Path>)

Available on crate feature save only.

Save this YA image.

source§

impl<T: AsRef<[u8]>> Image<T, 1>

source

pub fn save(&self, f: impl AsRef<Path>)

Available on crate feature save only.

Save this Y image.

Trait Implementations§

source§

impl BlendingOverlay<Image<&[u8], 4>> for Image<&mut [u8], 4>

source§

unsafe fn overlay_blended(&mut self, with: &Image<&[u8], 4>) -> &mut Self

Overlay with => self, blending. You probably do not need this, unless your images make much usage of alpha. If you only have 2 alpha states, 0 | 255 (transparent | opaque), please use Overlay, as it is much faster. Read more
source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, U: AsRef<[u8]>> BlendingOverlay<Image<U, 4>> for Image<T, 3>

source§

unsafe fn overlay_blended(&mut self, with: &Image<U, 4>) -> &mut Self

Overlay with => self, blending. You probably do not need this, unless your images make much usage of alpha. If you only have 2 alpha states, 0 | 255 (transparent | opaque), please use Overlay, as it is much faster. Read more
source§

impl<T: Clone, const CHANNELS: usize> Clone for Image<T, CHANNELS>

source§

fn clone(&self) -> Self

Returns a duplicate of this image.

let new_i = i.clone();

If you find yourself in the pattern of

let mut i = i.clone();
unsafe { i.rot_90() };

STOP!

Instead use

let i = unsafe { i.cloner().rot_90() };
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug, const CHANNELS: usize> Debug for Image<T, CHANNELS>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, U: AsRef<[u8]>> Overlay<Image<U, 4>> for Image<T, 3>

source§

unsafe fn overlay(&mut self, with: &Image<U, 4>) -> &mut Self

Overlay with => self (does not blend) Read more
source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, U: AsRef<[u8]>> Overlay<Image<U, 4>> for Image<T, 4>

source§

unsafe fn overlay(&mut self, with: &Image<U, 4>) -> &mut Self

Overlay with => self (does not blend) Read more
source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, U: AsRef<[u8]>> OverlayAt<Image<U, 3>> for Image<T, 3>

source§

unsafe fn overlay_at(&mut self, with: &Image<U, 3>, x: u32, y: u32) -> &mut Self

Overlay a RGB image(with) => self at coordinates x, y. As this is a RGBxRGB operation, blending is unnecessary, and this is simply a copy.

Safety

UB if x, y is out of bounds

source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, U: AsRef<[u8]>> OverlayAt<Image<U, 4>> for Image<T, 3>

source§

unsafe fn overlay_at(&mut self, with: &Image<U, 4>, x: u32, y: u32) -> &mut Self

Overlay with => self at coordinates x, y, without blending Read more
source§

impl<T: AsMut<[u8]> + AsRef<[u8]>, U: AsRef<[u8]>> OverlayAt<Image<U, 4>> for Image<T, 4>

source§

unsafe fn overlay_at(&mut self, with: &Image<U, 4>, x: u32, y: u32) -> &mut Self

Overlay with => self at coordinates x, y, without blending Read more
source§

impl<T: PartialEq, const CHANNELS: usize> PartialEq for Image<T, CHANNELS>

source§

fn eq(&self, other: &Image<T, CHANNELS>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const CHANNELS: usize> Copy for Image<&[u8], CHANNELS>

source§

impl<T: Eq, const CHANNELS: usize> Eq for Image<T, CHANNELS>

source§

impl<T, const CHANNELS: usize> StructuralEq for Image<T, CHANNELS>

source§

impl<T, const CHANNELS: usize> StructuralPartialEq for Image<T, CHANNELS>

Auto Trait Implementations§

§

impl<T, const CHANNELS: usize> RefUnwindSafe for Image<T, CHANNELS>where T: RefUnwindSafe,

§

impl<T, const CHANNELS: usize> Send for Image<T, CHANNELS>where T: Send,

§

impl<T, const CHANNELS: usize> Sync for Image<T, CHANNELS>where T: Sync,

§

impl<T, const CHANNELS: usize> Unpin for Image<T, CHANNELS>where T: Unpin,

§

impl<T, const CHANNELS: usize> UnwindSafe for Image<T, CHANNELS>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.