#image #frame #interoperability #stride

imgref

A trivial struct for interchange of pixel buffers with width, height & stride

12 stable releases

1.3.5 Jun 17, 2018
1.3.4 Apr 15, 2018
1.3.3 Dec 9, 2017
1.3.0 Nov 7, 2017
1.0.0 Apr 1, 2017

#12 in Images

Download history 114/week @ 2018-07-01 88/week @ 2018-07-08 161/week @ 2018-07-15 126/week @ 2018-07-22 165/week @ 2018-07-29 169/week @ 2018-08-05 317/week @ 2018-08-12 185/week @ 2018-08-19 152/week @ 2018-08-26 198/week @ 2018-09-02 290/week @ 2018-09-09 225/week @ 2018-09-16 134/week @ 2018-09-23

507 downloads per month
Used in 10 crates (9 directly)

CC0 license

55KB
456 lines

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

extern crate imgref;
use imgref::*;

fn main() {
    let img = Img::new(vec![0; 4], 2, 2);

    let new_image = some_image_processing_function(img.as_ref());

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

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

Type aliases

Illustration: stride is width of the whole buffer.

These are described in more detail in the reference.

ImgVec

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

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.

No runtime deps