[][src]Struct raster::Image

pub struct Image {
    pub width: i32,
    pub height: i32,
    pub bytes: Vec<u8>,
}

A struct for easily representing a raster image.

Fields

Width of image in pixels.

Height of image in pixels.

Vector containing sequence of bytes in RGBA format.

Methods

impl<'a> Image
[src]

Create a blank image. Default color is black.

Examples

use raster::Image;

let image = Image::blank(2, 2);

println!("{:?}", image.bytes);

assert_eq!(image.width, 2);
assert_eq!(image.height, 2);

Check if there is a pixel at this location given by x and y.

Examples

use raster::Image;

let image = Image::blank(2, 2);

assert_eq!(image.check_pixel(0, 0), true);
assert_eq!(image.check_pixel(3, 3), false);

Get the histogram of the image.

Examples

Visualizing the histogram of the red channel of this image:

Image:

Code:

use raster::Image;
use raster::Color;

let image = raster::open("tests/in/sample.png").unwrap();

let (r_bin, _, _, _) = image.histogram().unwrap();

let mut max_r_bin = 0;
for (_, count) in &r_bin {
    if *count > max_r_bin {
        max_r_bin = *count;
    }
}

let canvas_w = 256;
let canvas_h: i32 = 100;
let mut image = Image::blank(canvas_w, canvas_h);
raster::editor::fill(&mut image, Color::rgb(214, 214, 214)).unwrap();

for x in 0..256 as i32 { // 0-255
    let key = x as u8;
    match r_bin.get(&key) {
        Some(count) => {
            let height = (canvas_h as f32 * (*count as f32 / max_r_bin as f32)).round() as i32;

            for y in canvas_h-height..canvas_h {
                image.set_pixel(x, y, &Color::hex("#e22d11").unwrap()).unwrap();
            }
        },
        None => {}
    }
}

raster::save(&image, "tests/out/histogram.png").unwrap();

Histogram:

Photoshop's result:

Get pixel in a given x and y location of an image.

Errors

If either the x or y coordinate falls out of bounds, this will fail with RasterError::PixelOutOfBounds.

Examples

use raster::Image;
use raster::Color;

let mut image = Image::blank(2, 2); // Creates a 2x2 black image.

let pixel = image.get_pixel(0, 0).unwrap();

assert_eq!(0, pixel.r);
assert_eq!(0, pixel.g);
assert_eq!(0, pixel.b);
assert_eq!(255, pixel.a);

Set pixel in a given x and y location of an image.

Errors

If either the x or y coordinate falls out of bounds, this will fail with RasterError::PixelOutOfBounds.

If the calculated byte start index is less than 0, this will fail with RasterError::InvalidStartIndex.

Examples

use raster::Image;
use raster::Color;

let mut image = Image::blank(2, 2); // Creates a 2x2 black image.

let _ = image.set_pixel(0, 0, &Color::rgba(255, 0, 0, 255)); // Set first pixel to red

let pixel = image.get_pixel(0, 0).unwrap();

assert_eq!(255, pixel.r);
assert_eq!(0, pixel.g);
assert_eq!(0, pixel.b);
assert_eq!(255, pixel.a);

Trait Implementations

impl Clone for Image
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for Image
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Image

impl Sync for Image

Blanket Implementations

impl<T> From for T
[src]

Performs the conversion.

impl<T, U> Into for T where
    U: From<T>, 
[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Borrow for T where
    T: ?Sized
[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut for T where
    T: ?Sized
[src]

Mutably borrows from an owned value. Read more

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

🔬 This is a nightly-only experimental API. (get_type_id)

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more

impl<T> SetParameter for T
[src]

Sets value as a parameter of self.