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
impl BMP
pub fn new(height: i32, width: u32, default_color: Option<[u8; 4]>) -> BMP
sourcepub fn new_from_file(file_path: &str) -> BMP
pub fn new_from_file(file_path: &str) -> BMP
Load BMP from file.
sourcepub fn num_bytes_to_kilobytes(bytes: u32) -> u32
pub fn num_bytes_to_kilobytes(bytes: u32) -> u32
Converts bytes to kilobytes (1024 bytes per kilobyte).
sourcepub fn point_in_enclosure(point: [u16; 2], enclosure: &Vec<[u16; 2]>) -> bool
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.
sourcepub fn rgba_to_hex(rgba: [u8; 4]) -> String
pub fn rgba_to_hex(rgba: [u8; 4]) -> String
Converts RGBA to hexadecimal string.
sourcepub fn hex_to_rgba(hex: String) -> [u8; 4]
pub fn hex_to_rgba(hex: String) -> [u8; 4]
Converts hexadecimal string to RGBA.
sourcepub fn hsl_to_rgb(hsl: [f64; 3]) -> Result<[u8; 3], ErrorKind>
pub fn hsl_to_rgb(hsl: [f64; 3]) -> Result<[u8; 3], ErrorKind>
Convert HSL (hue, saturation, lightness) to RGB.
pub fn composite_colors(color1: [u8; 4], color2: [u8; 4]) -> [u8; 4]
pub fn rgb_to_grayscale(rgba: [u8; 4]) -> [u8; 4]
sourcepub fn get_header(&self) -> BITMAPFILEHEADER
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.
sourcepub fn get_offset(&self) -> u32
pub fn get_offset(&self) -> u32
Get the offset in bytes to pixel array from the file header.
sourcepub fn get_size(&self, use_header: bool) -> u32
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
sourcepub fn diff(bmp1: &BMP, bmp2: &BMP) -> Result<ImageDiff, ErrorKind>
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.
sourcepub fn is_from_file(&self) -> bool
pub fn is_from_file(&self) -> bool
See if BMP was originally loaded from file or not
sourcepub fn get_dib_header(&self) -> Result<DIBHEADER, ErrorKind>
pub fn get_dib_header(&self) -> Result<DIBHEADER, ErrorKind>
sourcepub fn get_pixel_data(&self) -> Result<VecDeque<Vec<Vec<u8>>>, ErrorKind>
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.
sourcepub fn get_color_of_pixel(
&self,
x: usize,
y: usize
) -> Result<[u8; 4], ErrorKind>
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.
pub fn get_color_of_px(&self, x: usize, y: usize) -> Result<[u8; 4], ErrorKind>
sourcepub fn get_color_of_pixel_efficient(
&self,
x: usize,
y: usize,
dib_header: &DIBHEADER,
pixel_data: &VecDeque<Vec<Vec<u8>>>
) -> Result<[u8; 4], ErrorKind>
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.
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>
sourcepub fn get_format(&self) -> String
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.
sourcepub fn change_color_of_pixel(
&mut self,
x: u16,
y: u16,
new_color: [u8; 4]
) -> Result<(), ErrorKind>
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.
sourcepub fn change_color_of_pixel_efficient(
&mut self,
x: u16,
y: u16,
new_color: [u8; 4],
dib_header: &DIBHEADER,
header: &BITMAPFILEHEADER
) -> Result<(), ErrorKind>
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.
sourcepub fn change_color_of_pixels(
&mut self,
pixels: Vec<[u16; 2]>,
new_color: [u8; 4]
) -> Result<(), ErrorKind>
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.
sourcepub fn draw_image(&mut self, x: u16, y: u16, bmp2: BMP) -> Result<(), ErrorKind>
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.
sourcepub fn change_opacity(&mut self, opacity: u8) -> Result<(), ErrorKind>
pub fn change_opacity(&mut self, opacity: u8) -> Result<(), ErrorKind>
Change opacity of the whole image.
sourcepub fn invert(&mut self, invert_alpha: Option<bool>) -> Result<(), ErrorKind>
pub fn invert(&mut self, invert_alpha: Option<bool>) -> Result<(), ErrorKind>
Invert the colors of the image.
sourcepub fn translate(&mut self, x: i16, y: i16) -> Result<(), ErrorKind>
pub fn translate(&mut self, x: i16, y: i16) -> Result<(), ErrorKind>
Translate the image in the horizontal and/or vertical directions.
sourcepub fn rotate(
&mut self,
deg: f64,
center_option: Option<[u16; 2]>
) -> Result<(), ErrorKind>
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).
pub fn separable_blur( &mut self, radius: u8, gen_distribution: impl Fn(u8, u8) -> u16, horizontal: Option<bool>, vertical: Option<bool> ) -> Result<(), ErrorKind>
sourcepub fn box_blur(&mut self, radius: u8) -> Result<(), ErrorKind>
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
.
sourcepub fn gaussian_blur(&mut self, radius: u8) -> Result<(), ErrorKind>
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.
pub fn greyscale(&mut self) -> Result<(), ErrorKind>
sourcepub fn channel_grayscale(
&mut self,
channel: RGBAChannel
) -> Result<(), ErrorKind>
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.
pub fn surround_filter( &mut self, radius: u8, get_new_pixel: impl Fn(Vec<[u8; 4]>) -> [u8; 4] ) -> Result<(), ErrorKind>
sourcepub fn median_filter(&mut self, radius: u8) -> Result<(), ErrorKind>
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.
sourcepub fn mean_filter(&mut self, radius: u8) -> Result<(), ErrorKind>
pub fn mean_filter(&mut self, radius: u8) -> Result<(), ErrorKind>
A less efficient version of the box_blur
, use that instead.
sourcepub fn draw_line(
&mut self,
fill: [u8; 4],
p1: [u16; 2],
p2: [u16; 2]
) -> Result<(), ErrorKind>
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.
sourcepub fn draw_rectangle(
&mut self,
fill: Option<[u8; 4]>,
stroke: Option<[u8; 4]>,
p1: [u16; 2],
p2: [u16; 2]
) -> Result<(), ErrorKind>
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.
sourcepub fn draw_ellipse(
&mut self,
center: [u16; 2],
xlength: u16,
ylength: u16,
stroke: [u8; 4],
fill: Option<[u8; 4]>,
guess: bool
) -> Result<(), ErrorKind>
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.