Struct raster::Image
[−]
[src]
pub struct Image { pub width: i32, pub height: i32, pub bytes: Vec<u8>, }
A struct for easily representing a raster image.
Fields
width: i32
Width of image in pixels.
height: i32
Height of image in pixels.
bytes: Vec<u8>
Vector containing sequence of bytes in RGBA format.
Methods
impl<'a> Image
[src]
fn blank(w: i32, h: i32) -> Image
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);
fn check_pixel(&self, x: i32, y: i32) -> bool
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);
fn histogram(&self) -> RasterResult<Histogram>
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:
fn get_pixel(&self, x: i32, y: i32) -> RasterResult<Color>
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);
fn set_pixel(&mut self, x: i32, y: i32, color: Color) -> RasterResult<()>
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 Debug for Image
[src]
impl Clone for Image
[src]
fn clone(&self) -> Image
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more