1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
pub mod bmp_header;
pub mod bmp_info_header;
pub mod bmp_colour_table;
pub mod bmp_pixel_data;
mod utils;
//standard library imports
use std::fs::File;
use std::io;
//bmp file section imports
use bmp_header::BmpHeader;
use bmp_info_header::BmpInfoHeader;
use bmp_colour_table::BmpColourTable;
use bmp_pixel_data::BmpPixelData;
//import utils
use utils::{round_up_to_multiple_of_four, rgb_to_greyscale};
/// A clonable struct representing a .bmp file. Top level abstraction of bitmap file. Currently only supports 24-bit .bmp files.
pub struct Bmp {
pub header: BmpHeader,
pub info_header: BmpInfoHeader,
pub colour_table: BmpColourTable,
pub pixel_data: BmpPixelData
}
impl Bmp {
/// Builds a `Bmp` struct instance.
///
/// # Arguments
///
/// * `width` - The width of the image in pixels.
/// * `height` - The height of the image in pixels.
///
/// # Examples
///
/// ```
/// use bumpy::bmp::Bmp;
///
/// let bmp = Bmp::new(100, 100);
/// ```
pub fn new(width: u32, height: u32) -> Self {
let header = BmpHeader::new(width, height);
let info_header = BmpInfoHeader::new(width, height);
let colour_table = BmpColourTable::new();
let pixel_data = BmpPixelData::new(width, height);
Bmp {
header,
info_header,
colour_table,
pixel_data
}
}
/// Builds a Bmp struct instance from a `File` object.
///
/// # Arguments
///
/// * `file` - A mutable reference to a `File` object.
///
/// # Returns
///
/// Returns a `Result` containing the `BmpHeader` if successful, or an `io::Error` if an error occurred.
///
/// # Examples
///
/// ```
/// use std::fs::File;
/// use std::io;
/// use bumpy::bmp::Bmp;
///
/// fn main() -> io::Result<()> {
/// let mut file = File::open("sample.bmp")?;
/// let bmp = Bmp::build_from_file(&mut file)?;
///
/// Ok(())
/// }
/// ```
pub fn build_from_file(file: &mut File) -> io::Result<Self> {
let header = BmpHeader::build_from_file(file)?;
let info_header = BmpInfoHeader::build_from_file(file)?;
let colour_table = BmpColourTable::build_from_file(file, &info_header)?;
let pixel_data = BmpPixelData::build_from_file(file, &header.data_offset)?;
let bits_per_px = u16::from_le_bytes(info_header.bits_per_px);
if bits_per_px != 24 && bits_per_px != 8{
return Err(io::Error::new(io::ErrorKind::InvalidData, "Only 24-bit .bmp files are supported"));
}
Ok(Bmp {
header,
info_header,
colour_table,
pixel_data
})
}
/// Prints the contents of the `Bmp` struct to the console.
///
/// # Arguments
///
/// * `with_color_table` - A boolean value indicating whether to print the color table.
/// * `with_pixel_data` - A boolean value indicating whether to print the pixel data.
///
/// # Examples
///
/// ```
/// use std::fs::File;
/// use std::io;
/// use bumpy::bmp::Bmp;
///
/// fn main() -> io::Result<()> {
/// let mut file = File::open("sample.bmp")?;
/// let bmp = Bmp::build_from_file(&mut file)?;
///
/// bmp.print_all(false, false);
///
/// Ok(())
/// }
/// ```
pub fn print_all(&self, with_color_table: bool, with_pixel_data: bool) {
println!("BMP Header:");
println!("Signature: {}", String::from_utf8_lossy(&self.header.signature));
println!("File size: {}", u32::from_le_bytes(self.header.file_size));
println!("Reserved: {}", u32::from_le_bytes(self.header.reserved));
println!("Data offset: {}", u32::from_le_bytes(self.header.data_offset));
println!("");
println!("BMP Info Header:");
println!("Size: {}", u32::from_le_bytes(self.info_header.size));
println!("Width: {:?}", u32::from_le_bytes(self.info_header.width));
println!("Height: {:?}", u32::from_le_bytes(self.info_header.height));
println!("Planes: {}", u16::from_le_bytes(self.info_header.planes));
println!("Bits per pixel: {}", u16::from_le_bytes(self.info_header.bits_per_px));
println!("Compression: {}", u32::from_le_bytes(self.info_header.compression));
println!("Image size: {}", u32::from_le_bytes(self.info_header.image_size));
println!("X pixels per meter: {}", u32::from_le_bytes(self.info_header.x_per_m));
println!("Y pixels per meter: {}", u32::from_le_bytes(self.info_header.y_per_m));
println!("Colours used: {}", u32::from_le_bytes(self.info_header.colours_used));
println!("Important colours: {}", u32::from_le_bytes(self.info_header.important_colours));
println!("");
if with_color_table {
println!("BMP Color Table:");
for (i, color) in self.colour_table.data.iter().enumerate() {
println!("Color {}: {:?}", i, color);
}
println!("");
}
if with_pixel_data {
println!("BMP Pixel Data:");
for (i, pixel) in self.pixel_data.data.iter().enumerate() {
println!("Entry {}: {:?}", i, pixel);
}
}
}
/// Prints the contents of the `Bmp` struct to the console without converting the bytes to their corresponding integer values.
///
/// # Arguments
///
/// * `with_color_table` - A boolean value indicating whether to print the color table.
/// * `with_pixel_data` - A boolean value indicating whether to print the pixel data.
///
/// # Examples
///
/// ```
/// use std::fs::File;
/// use std::io;
/// use bumpy::bmp::Bmp;
///
/// fn main() -> io::Result<()> {
/// let mut file = File::open("sample.bmp")?;
/// let bmp = Bmp::build_from_file(&mut file)?;
///
/// bmp.print_all_raw(false, false);
///
/// Ok(())
/// }
/// ```
pub fn print_all_raw (&self, with_color_table: bool, with_pixel_data: bool) {
println!("BMP Header:");
println!("Signature: {:?}", self.header.signature);
println!("File size: {:?}", self.header.file_size);
println!("Reserved: {:?}", self.header.reserved);
println!("Data offset: {:?}", self.header.data_offset);
println!("");
println!("BMP Info Header:");
println!("Size: {:?}", self.info_header.size);
println!("Width: {:?}", self.info_header.width);
println!("Height: {:?}", self.info_header.height);
println!("Planes: {:?}", self.info_header.planes);
println!("Bits per pixel: {:?}", self.info_header.bits_per_px);
println!("Compression: {:?}", self.info_header.compression);
println!("Image size: {:?}", self.info_header.image_size);
println!("X pixels per meter: {:?}", self.info_header.x_per_m);
println!("Y pixels per meter: {:?}", self.info_header.y_per_m);
println!("Colours used: {:?}", self.info_header.colours_used);
println!("Important colours: {:?}", self.info_header.important_colours);
println!("");
if with_color_table {
println!("BMP Color Table:");
for (i, color) in self.colour_table.data.iter().enumerate() {
println!("Color {}: {:?}", i, color);
}
println!("");
}
if with_pixel_data {
println!("BMP Pixel Data:");
}
}
/// Writes the contents of the `Bmp` struct to a file.
///
/// # Arguments
///
/// * `file_name` - A string slice containing the name of the file to write to.
///
/// # Examples
///
/// ```
/// use std::fs::File;
/// use std::io;
/// use bumpy::bmp::Bmp;
///
/// fn main() -> io::Result<()> {
/// let mut file = File::open("sample.bmp")?;
/// let bmp = Bmp::build_from_file(&mut file)?;
///
/// bmp.write_to_file("test")?;
///
/// Ok(())
/// }
/// ```
pub fn write_to_file(&self, file_name: &str) -> io::Result<()> {
let mut file = File::create(format!("{}.bmp", file_name))?;
self.header.write_to_file(&mut file)?;
self.info_header.write_to_file(&mut file)?;
self.colour_table.write_to_file(&mut file)?;
self.pixel_data.write_to_file(&mut file)?;
Ok(())
}
/// Converts the image to greyscale.
///
/// # Examples
///
/// ```
/// use std::fs::File;
/// use std::io;
/// use bumpy::bmp::Bmp;
///
/// fn main() -> io::Result<()> {
/// let mut file = File::open("sample.bmp")?;
/// let mut bmp = Bmp::build_from_file(&mut file)?;
///
/// bmp.to_greyscale();
///
/// bmp.write_to_file("test")?;
///
/// Ok(())
/// }
/// ```
pub fn to_greyscale(&mut self) {
let bits_per_px = u16::from_le_bytes(self.info_header.bits_per_px);
if bits_per_px == 24 {
self.to_greyscale_24();
}
else {
panic!("Only 24-bit .bmp files are supported");
}
}
// 24 bit function definition for greyscale conversion
fn to_greyscale_24(&mut self){
let width = u32::from_le_bytes(self.info_header.width);
let height = u32::from_le_bytes(self.info_header.height);
for i in (0..=width - 1).rev(){
for j in 0..=height - 1 {
let index = (i + width * j) as usize;
let (b, g, r) = rgb_to_greyscale((self.pixel_data.data[index * 3], self.pixel_data.data[index * 3 + 1], self.pixel_data.data[index * 3 + 2]));
self.pixel_data.data[index * 3] = b;
self.pixel_data.data[index * 3 + 1] = g;
self.pixel_data.data[index * 3 + 2] = r;
}
}
}
/// Rotates image 90 degrees clockwise.
///
/// # Examples
///
/// ```
/// use std::fs::File;
/// use std::io;
/// use bumpy::bmp::Bmp;
///
/// fn main() -> io::Result<()> {
/// let mut file = File::open("sample.bmp")?;
/// let mut bmp = Bmp::build_from_file(&mut file)?;
///
/// bmp.rotate_90();
///
/// bmp.write_to_file("test")?;
///
/// Ok(())
/// }
/// ```
pub fn rotate_90(&mut self){
let bits_per_px = u16::from_le_bytes(self.info_header.bits_per_px);
if bits_per_px == 8 {
self.rotate_90_8();
}
else if bits_per_px == 24 {
self.rotate_90_24();
}
else {
panic!("Only 24-bit .bmp files are supported");
}
}
fn rotate_90_8(&mut self){
let width = u32::from_le_bytes(self.info_header.width);
let height = u32::from_le_bytes(self.info_header.height);
let mut new_pixel_data = Vec::new();
let curr_padding = round_up_to_multiple_of_four(width) - (width);
let new_padding = round_up_to_multiple_of_four(height) - (height);
for i in (0..=width - 1).rev() {
for j in 0..=(height - 1) {
let index = (i + width * j) as usize;
new_pixel_data.push(self.pixel_data.data[index + (curr_padding * j) as usize]);
}
for _j in 0..new_padding {
new_pixel_data.push(0);
}
}
// swap width and height
let width = self.info_header.width;
let height = self.info_header.height;
self.info_header.width = height;
self.info_header.height = width;
self.pixel_data.data = new_pixel_data;
}
//24 bit function definition for 90 degree rotation
fn rotate_90_24(&mut self){
let width = u32::from_le_bytes(self.info_header.width);
let height = u32::from_le_bytes(self.info_header.height);
let mut new_pixel_data = Vec::new();
let curr_padding = round_up_to_multiple_of_four(width * 3) - (width * 3);
let new_padding = round_up_to_multiple_of_four(height * 3) - (height * 3);
for i in (0..=width - 1).rev() {
for j in 0..=(height - 1) {
let index = (i + width * j) as usize;
new_pixel_data.push(self.pixel_data.data[index * 3 + (curr_padding * j) as usize]);
new_pixel_data.push(self.pixel_data.data[index * 3 + 1 + (curr_padding * j) as usize]);
new_pixel_data.push(self.pixel_data.data[index * 3 + 2 + (curr_padding * j) as usize]);
}
for _j in 0..new_padding {
new_pixel_data.push(0);
}
}
// swap width and height
let width = self.info_header.width;
let height = self.info_header.height;
self.info_header.width = height;
self.info_header.height = width;
self.pixel_data.data = new_pixel_data;
}
/// Rotates image 180 degrees
///
/// # Examples
///
/// ```
/// use std::fs::File;
/// use std::io;
/// use bumpy::bmp::Bmp;
///
/// fn main() -> io::Result<()> {
/// let mut file = File::open("sample.bmp")?;
/// let mut bmp = Bmp::build_from_file(&mut file)?;
///
/// bmp.rotate_180();
///
/// bmp.write_to_file("test")?;
///
/// Ok(())
/// }
/// ```
pub fn rotate_180(&mut self){
let bits_per_px = u16::from_le_bytes(self.info_header.bits_per_px);
if bits_per_px == 8 {
self.rotate_90_8();
self.rotate_90_8();
}
else if bits_per_px == 24 {
self.rotate_90_24();
self.rotate_90_24();
}
else {
panic!("Only 24-bit .bmp files are supported");
}
}
/// Rotates image 270 degrees clockwise.
///
/// # Examples
///
/// ```
/// use std::fs::File;
/// use std::io;
/// use bumpy::bmp::Bmp;
///
/// fn main() -> io::Result<()> {
/// let mut file = File::open("sample.bmp")?;
/// let mut bmp = Bmp::build_from_file(&mut file)?;
///
/// bmp.rotate_270();
///
/// bmp.write_to_file("test")?;
///
/// Ok(())
/// }
/// ```
pub fn rotate_270(&mut self){
let bits_per_px = u16::from_le_bytes(self.info_header.bits_per_px);
if bits_per_px == 8 {
self.rotate_90_8();
self.rotate_90_8();
self.rotate_90_8();
}
else if bits_per_px == 24 {
self.rotate_90_24();
self.rotate_90_24();
self.rotate_90_24();
}
else {
panic!("Only 24-bit .bmp files are supported");
}
}
/// Mirrors image along horizontal axis
///
/// # Examples
///
/// ```
/// use std::fs::File;
/// use std::io;
/// use bumpy::bmp::Bmp;
///
/// fn main() -> io::Result<()> {
/// let mut file = File::open("sample.bmp")?;
/// let mut bmp = Bmp::build_from_file(&mut file)?;
///
/// bmp.flip_hor();
///
/// bmp.write_to_file("test")?;
///
/// Ok(())
/// }
/// ```
pub fn flip_hor(&mut self){
let width = u32::from_le_bytes(self.info_header.width);
let height = u32::from_le_bytes(self.info_header.height);
let mut new_pixel_data = Vec::new();
let curr_padding = round_up_to_multiple_of_four(width * 3) - (width * 3);
for i in 0..=height - 1 {
for j in (1..=width).rev() {
let index = ((j - 1) + width * i) as usize;
new_pixel_data.push(self.pixel_data.data[index * 3 + (curr_padding * i) as usize]);
new_pixel_data.push(self.pixel_data.data[index * 3 + 1 + (curr_padding * i) as usize]);
new_pixel_data.push(self.pixel_data.data[index * 3 + 2 + (curr_padding * i) as usize]);
}
for _j in 0..curr_padding {
new_pixel_data.push(0);
}
}
self.pixel_data.data = new_pixel_data;
}
}
impl Clone for Bmp {
fn clone(&self) -> Self {
Bmp {
header: self.header.clone(),
info_header: self.info_header.clone(),
colour_table: self.colour_table.clone(),
pixel_data: self.pixel_data.clone()
}
}
}