diff options
| author | Dylan <boss@tehbox.org> | 2026-06-21 15:01:28 +1200 |
|---|---|---|
| committer | Dylan <boss@tehbox.org> | 2026-06-21 15:03:02 +1200 |
| commit | a472b1cdaa8a1f72b96988009b3d4d9820e7128f (patch) | |
| tree | 833959761c3c1f60c60425bdc7553162cacb3f2d /src/writer.rs | |
| parent | 14de6db28fb689cdb066c5b01a2885b6a6a35157 (diff) | |
| download | tehimage-rs-a472b1cdaa8a1f72b96988009b3d4d9820e7128f.tar.gz tehimage-rs-a472b1cdaa8a1f72b96988009b3d4d9820e7128f.zip | |
feat: Added bitmap writing
Diffstat (limited to 'src/writer.rs')
| -rw-r--r-- | src/writer.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/writer.rs b/src/writer.rs new file mode 100644 index 0000000..dc1ac85 --- /dev/null +++ b/src/writer.rs @@ -0,0 +1,69 @@ +use std::io::Write; +use std::{fs::File, io::BufWriter}; + +pub struct FileWriter +{ + buf_writer: BufWriter<File> +} + +impl FileWriter +{ + pub fn new(buf_writer: BufWriter<File>) -> Self + { + Self + { + buf_writer + } + } + + pub fn write_zeros(&mut self, c: usize) + { + for _ in 0..c + { + self.write(0 as u8); + } + // self.buf_writer.write_all(&[0u8; c]).expect("Could not write 0s"); + } + + pub fn write_array<const N: usize, T: ToBytes<N>, const C: usize>(&mut self, xs: [T; C]) + { + for x in xs + { + self.write(x); + } + } + pub fn write<const N: usize, T: ToBytes<N>>(&mut self, x: T) + { + let data = x.to_le_bytes(); + self.buf_writer.write_all(&data).expect("Could not write"); + } + + pub fn flush(&mut self) + { + self.buf_writer.flush().expect("Could not flush"); + } +} + +pub trait ToBytes<const N: usize>: Sized + Copy +{ + fn to_le_bytes(&self) -> [u8; N]; +} + +impl ToBytes<4> for u32 +{ + fn to_le_bytes(&self) -> [u8; 4] { + u32::to_le_bytes(*self) + } +} +impl ToBytes<2> for u16 +{ + fn to_le_bytes(&self) -> [u8; 2] { + u16::to_le_bytes(*self) + } +} +impl ToBytes<1> for u8 +{ + fn to_le_bytes(&self) -> [u8; 1] { + u8::to_le_bytes(*self) + } +} |
