From a472b1cdaa8a1f72b96988009b3d4d9820e7128f Mon Sep 17 00:00:00 2001 From: Dylan Date: Sun, 21 Jun 2026 15:01:28 +1200 Subject: feat: Added bitmap writing --- src/image.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/image.rs (limited to 'src/image.rs') diff --git a/src/image.rs b/src/image.rs new file mode 100644 index 0000000..ab5dc99 --- /dev/null +++ b/src/image.rs @@ -0,0 +1,89 @@ +use std::ops; + +use crate::{reader::FileReader, writer::FileWriter}; + +pub trait ColorDepth: Clone +{ + const DEFAULT: Self; +} +impl ColorDepth for u8 { + const DEFAULT: Self = 0; +} +impl ColorDepth for u16 { + const DEFAULT: Self = 0; +} +impl ColorDepth for u32 { + const DEFAULT: Self = 0; +} + +#[derive(Debug, Clone, Copy)] +pub struct RGBA +{ + pub r: T, + pub g: T, + pub b: T, + pub a: T +} +#[derive(Debug, Clone, Copy)] +pub struct RGB +{ + pub r: T, + pub g: T, + pub b: T, +} + +pub trait ColorType: Clone +{ + const DEFAULT: Self; +} +impl ColorType for RGBA +{ + const DEFAULT: Self = Self { r: T::DEFAULT, g: T::DEFAULT, b: T::DEFAULT, a: T::DEFAULT }; +} +impl ColorType for RGB +{ + const DEFAULT: Self = Self { r: T::DEFAULT, g: T::DEFAULT, b: T::DEFAULT}; +} + +#[derive(Debug)] +pub struct PixelArr +{ + pub width: u32, + pub height: u32, + raw: Vec +} +impl PixelArr { + pub fn new(width: u32, height: u32) -> Self { + let raw = vec![T::DEFAULT;(width*height) as usize]; + Self { width, height, raw } + } +} +impl ops::Index<(usize, usize)> for PixelArr +{ + type Output = T; + + fn index(&self, index: (usize, usize)) -> &Self::Output { + let (x, y) = index; + &self.raw[x + y*(self.width as usize)] + } +} +impl ops::IndexMut<(usize, usize)> for PixelArr +{ + fn index_mut(&mut self, index: (usize, usize)) -> &mut T { + let (x, y) = index; + &mut self.raw[x + y*(self.width as usize)] + } +} + +#[derive(Debug)] +pub struct ImageBase +{ + pub bit_depth: u8, + pub pixels: PixelArr> +} + +pub trait Image +{ + fn read_image(reader: &mut FileReader) -> Result; + fn write_image(image: &ImageBase, writer: &mut FileWriter)-> (); +} -- cgit v1.3.1