Struct BMP

Source
pub struct BMP {
    pub contents: Vec<u8>,
    /* private fields */
}
Expand description

Represents loaded BMP file, the contents is a vector of the file bytes.

Also contains many useful utility functions.

Fields§

§contents: Vec<u8>

Implementations§

Source§

impl BMP

Source

pub fn new(height: i32, width: u32, default_color: Option<[u8; 4]>) -> BMP

Source

pub fn new_from_file(file_path: &str) -> Result<BMP, ErrorKind>

Load BMP from file.

Source

pub fn num_bytes_to_kilobytes(bytes: u32) -> u32

Converts bytes to kilobytes (1024 bytes per kilobyte).

Source

pub fn point_in_enclosure(point: [u16; 2], enclosure: &Vec<[u16; 2]>) -> bool

Check if a point is in an enclosure of points, using the even-odd rule.

Source

pub fn rgba_to_hex(rgba: [u8; 4]) -> String

Converts RGBA to hexadecimal string.

Source

pub fn hex_to_rgba(hex: String) -> [u8; 4]

Converts hexadecimal string to RGBA.

Source

pub fn hsl_to_rgb(hsl: [f64; 3]) -> Result<[u8; 3], ErrorKind>

Convert HSL (hue, saturation, lightness) to RGB.

Source

pub fn composite_colors(color1: [u8; 4], color2: [u8; 4]) -> [u8; 4]

Source

pub fn rgb_to_grayscale(rgba: [u8; 4]) -> [u8; 4]

Source

pub fn get_header(&self) -> BITMAPFILEHEADER

Gets the file header. The file header contains the size of the file and offset in bytes to pixel array.

Source

pub fn get_offset(&self) -> u32

Get the offset in bytes to pixel array from the file header.

Source

pub fn get_size(&self, use_header: bool) -> u32

Get file size, either from the file header or by actually checking the length in bytes of the file

Source

pub fn diff(bmp1: &BMP, bmp2: &BMP) -> Result<ImageDiff, ErrorKind>

Returns the difference between two loaded BMP files. Works even if the two files are different height and width.

Source

pub fn is_from_file(&self) -> bool

See if BMP was originally loaded from file or not

Source

pub fn get_dib_header(&self) -> Result<DIBHEADER, ErrorKind>

Get the DIB header for the BMP file. There are several different versions of the DIB header, documented here.

The BITMAPV5HEADER is the most recent and most common version, and the documentation for that can be found here.

Source

pub fn get_pixel_data(&self) -> Result<VecDeque<Vec<Vec<u8>>>, ErrorKind>

Get the pixel array. Returned as a VecDeque<Vec<Vec<u8>>. The VecDeque stores rows of pixels. The outer Vec contains one row of pixels, and the inner Vec represents the actual pixel color.

Source

pub fn get_color_of_pixel( &self, x: usize, y: usize, ) -> Result<[u8; 4], ErrorKind>

Given x and y coordinates ((0, 0) is the upper left corner of the image), get the RGB/RGBA color at that location.

Source

pub fn get_color_of_px(&self, x: usize, y: usize) -> Result<[u8; 4], ErrorKind>

Source

pub fn get_color_of_pixel_efficient( &self, x: usize, y: usize, dib_header: &DIBHEADER, pixel_data: &VecDeque<Vec<Vec<u8>>>, ) -> Result<[u8; 4], ErrorKind>

More efficient version of get_color_of_pixel that accepts the DIB header and pixel array as references to prevent unnecessary and slow copying of data.

Source

pub fn get_color_of_px_efficient( &self, x: usize, y: usize, dib_header: &DIBHEADER, pixel_data: &VecDeque<Vec<Vec<u8>>>, ) -> Result<[u8; 4], ErrorKind>

Source

pub fn get_format(&self) -> String

See what color format the pixels are stored in. Will return one of the following: rgb, bgr, rgba, bgra, argb, abgr, color table.

Source

pub fn change_color_of_pixel( &mut self, x: u16, y: u16, new_color: [u8; 4], ) -> Result<(), ErrorKind>

Given x and y coordinates ((0, 0) is the upper left corner of the image), change the RGB/RGBA color at that location.

Note: Currently only supports files where pixel color is stored as 24 or 32 bits, which should be most BMP files.

Source

pub fn change_color_of_pixel_efficient( &mut self, x: u16, y: u16, new_color: [u8; 4], dib_header: &DIBHEADER, header: &BITMAPFILEHEADER, ) -> Result<(), ErrorKind>

More efficient version of change_color_of_pixel that accepts the DIB header and file header as references to prevent unnecessary and slow copying of data.

Source

pub fn change_color_of_pixels( &mut self, pixels: Vec<[u16; 2]>, new_color: [u8; 4], ) -> Result<(), ErrorKind>

Batch change color of pixels. Pass in a vector of coordinates, and a new color. Useful if wanting to changing many pixels to the same color.

Source

pub fn draw_image(&mut self, x: u16, y: u16, bmp2: BMP) -> Result<(), ErrorKind>

Draw another loaded BMP file on the current BMP file, with upper left corner of the drawn on file at the given x and y coordinates.

Source

pub fn change_opacity(&mut self, opacity: u8) -> Result<(), ErrorKind>

Change opacity of the whole image.

Source

pub fn invert(&mut self, invert_alpha: Option<bool>) -> Result<(), ErrorKind>

Invert the colors of the image.

Source

pub fn translate(&mut self, x: i16, y: i16) -> Result<(), ErrorKind>

Translate the image in the horizontal and/or vertical directions.

Source

pub fn rotate( &mut self, deg: f64, center_option: Option<[u16; 2]>, ) -> Result<(), ErrorKind>

Rotate the image. If no center coordinate provided, defaults to (0, 0) (top left corner of image).

Source

pub fn separable_blur( &mut self, radius: u8, gen_distribution: impl Fn(u8, u8) -> u16, horizontal: Option<bool>, vertical: Option<bool>, ) -> Result<(), ErrorKind>

Source

pub fn box_blur(&mut self, radius: u8) -> Result<(), ErrorKind>

Apply box blur with given radius. The larger the radius, the more it will blur, since the radius determines how many neighbouring pixels will be considered.

A box blur essentially averages the color value of every pixel with the neighbours’ color values.

Since a box blur is a separable filter, unlike the mean filter, but has the same effect, it is recommended to use box_blur instead of mean_filter.

Source

pub fn gaussian_blur(&mut self, radius: u8) -> Result<(), ErrorKind>

Apply gaussian blur with given radius. The larger the radius, the more it will blur, since the radius determines how many neighbouring pixels will be considered.

Learn more about gaussian blurs here.

Source

pub fn grayscale(&mut self) -> Result<(), ErrorKind>

Turn the image into grayscale

Source

pub fn greyscale(&mut self) -> Result<(), ErrorKind>

Source

pub fn channel_grayscale( &mut self, channel: RGBAChannel, ) -> Result<(), ErrorKind>

Only considers the value of a specific channel (red, green, alpha) when turning the image grayscale.

Source

pub fn surround_filter( &mut self, radius: u8, get_new_pixel: impl Fn(Vec<[u8; 4]>) -> [u8; 4], ) -> Result<(), ErrorKind>

Source

pub fn median_filter(&mut self, radius: u8) -> Result<(), ErrorKind>

A median filter changes the color a value of a pixel to the median color value of all of the pixel’s neighbours. The larger the radius, the more neighbouring pixels will be considered.

Median filters are great at filtering out noise.

Source

pub fn mean_filter(&mut self, radius: u8) -> Result<(), ErrorKind>

A less efficient version of the box_blur, use that instead.

Source

pub fn draw_line( &mut self, fill: [u8; 4], p1: [u16; 2], p2: [u16; 2], ) -> Result<(), ErrorKind>

Draw a line specifying color fill, start point and end point.

Source

pub fn draw_rectangle( &mut self, fill: Option<[u8; 4]>, stroke: Option<[u8; 4]>, p1: [u16; 2], p2: [u16; 2], ) -> Result<(), ErrorKind>

Draw rectangle, specifying fill color and/or stroke color, and upper left and lower right corners.

Source

pub fn draw_ellipse( &mut self, center: [u16; 2], xlength: u16, ylength: u16, stroke: [u8; 4], fill: Option<[u8; 4]>, guess: bool, ) -> Result<(), ErrorKind>

Draw an ellipse specifying the center coordinates, horizontal radius, vertical radius, stroke color and optionally fill color.

If guess is true, the function will guess missing parts of the ellipse, so there are no gaps in the ellipse outline.

Source

pub fn fill_bucket( &mut self, fill: [u8; 4], x: usize, y: usize, ) -> Result<Vec<[u16; 2]>, ErrorKind>

Equivalent to the bucket fill tool in many image editors. Specify the starting coordinates and the new fill color.

Source

pub fn save_to_new(self, file_path: &str) -> Result<(), ErrorKind>

Save the image to a new file.

Trait Implementations§

Source§

impl Clone for BMP

Source§

fn clone(&self) -> BMP

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl PartialEq for BMP

Source§

fn eq(&self, other: &BMP) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl Freeze for BMP

§

impl RefUnwindSafe for BMP

§

impl Send for BMP

§

impl Sync for BMP

§

impl Unpin for BMP

§

impl UnwindSafe for BMP

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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

Source§

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.