#interoperability #image #frame #stride #vec2d


A basic 2-dimensional slice for safe and convenient handling of pixel buffers with width, height & stride

10 stable releases

1.10.0 Oct 17, 2023
1.9.4 Sep 18, 2022
1.9.3 Aug 21, 2022
1.9.2 Jun 12, 2022
1.2.0 Jul 28, 2017

#5 in Images

Download history 8582/week @ 2023-08-11 7640/week @ 2023-08-18 5505/week @ 2023-08-25 4965/week @ 2023-09-01 5950/week @ 2023-09-08 6344/week @ 2023-09-15 6659/week @ 2023-09-22 62906/week @ 2023-09-29 98586/week @ 2023-10-06 90009/week @ 2023-10-13 94511/week @ 2023-10-20 103020/week @ 2023-10-27 96224/week @ 2023-11-03 107497/week @ 2023-11-10 40016/week @ 2023-11-17 23159/week @ 2023-11-24

287,573 downloads per month
Used in 97 crates (34 directly)

CC0-1.0 OR Apache-2.0


2D slice of a Vec

This is a lowest common denominator struct for working with image fragments in Rust code. It represents a 2-dimensional vector and rectangular slices of it.

In graphics code it's very common to pass width and height along with a Vec of pixels — all as separate arguments. This gets very repetitive, and can lead to errors.

This crate is a simple struct that adds dimensions to the underlying buffer. This makes it easier to correctly keep track of the image size and allows passing images with just one function argument instead three or four.

Additionally, it has a concept of a stride, which allows defining sub-regions of images without copying, as well as padding (e.g. buffers for video frames may require to be a multiple of 8, regardless of logical image size).

For convenience, it implements iterators for pixels/rows and indexing with img[(x,y)].

use imgref::*;

fn main() {
    let img = Img::new(vec![0; 1000], 50, 20); // 1000 pixels of a 50×20 image

    let new_image = some_image_processing_function(img.as_ref()); // Use imgvec.as_ref() instead of &imgvec for better efficiency

    println!("New size is {}×{}", new_image.width(), new_image.height());
    println!("And the top left pixel is {:?}", new_image[(0u32,0u32)]);

    let first_row_slice = &new_image[0];

    for row in new_image.rows() {}
    for px in new_image.pixels() {}

    // slice (x, y, width, height) by reference - no copy!
    let fragment = img.sub_image(5, 5, 15, 15);

    // create a vec of pixels without stride, for compatibility with software
    // that expects pixels without any "gaps"
    let (vec, width, height) = fragment.to_contiguous_buf();

Type aliases

Illustration: stride is width of the whole buffer.

These are described in more detail in the reference.


It owns its pixels (held in a Vec). It's analogous to a 2-dimensional Vec. Use this type to create and return new images from functions.

Don't use &ImgVec. Instead call ImgVec.as_ref() to get a reference (ImgRef) from it (explicit .as_ref() call is required, because Rust doesn't support custom conversions yet.)


ImgRef is a reference to pixels owned by some other ImgVec or a slice. It's analogous to a 2-dimensional &[].

Use this type to accept read-only images as arguments in functions. Note that ImgRef is a Copy type. Pass ImgRef, and not &ImgRef.


  • Latest stable Rust (1.42+)

No runtime deps