windows file format device independent bitmap dib / bmp decoding and encoding
use lib
bendn 2024-04-28
parent 8528e5e · commit 0e6994e
-rw-r--r--Cargo.toml1
-rw-r--r--src/decode.rs36
-rw-r--r--src/encode.rs31
3 files changed, 7 insertions, 61 deletions
diff --git a/Cargo.toml b/Cargo.toml
index e905887..5ae809a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,3 +14,4 @@ categories = ["multimedia::images", "graphics", "encoding"]
atools = "0.1.4"
bites = "0.0.0"
car = "0.1.0"
+raad = "0.1.0"
diff --git a/src/decode.rs b/src/decode.rs
index 36f91bd..7ef5932 100644
--- a/src/decode.rs
+++ b/src/decode.rs
@@ -1,47 +1,13 @@
use crate::until::Until;
use crate::Color;
use atools::prelude::*;
+use raad::le::*;
use std::intrinsics::unlikely;
use std::io::{self, Read};
use std::iter::{repeat, repeat_with};
use std::mem::MaybeUninit as MU;
use std::{error, fmt};
-trait R {
- fn r<T: Readable>(&mut self) -> std::io::Result<T>;
-}
-
-impl<D: Read> R for D {
- fn r<T: Readable>(&mut self) -> std::io::Result<T> {
- T::r(self)
- }
-}
-
-trait Readable
-where
- Self: Sized,
-{
- fn r(from: &mut impl Read) -> std::io::Result<Self>;
-}
-
-impl<const N: usize> Readable for [u8; N] {
- fn r(from: &mut impl Read) -> std::io::Result<[u8; N]> {
- let mut buf = [0; N];
- from.read_exact(&mut buf).map(|()| buf)
- }
-}
-
-macro_rules! n {
- ($($n:ident)+) => {
- $(impl Readable for $n {
- fn r(from: &mut impl Read) -> std::io::Result<$n> {
- <[u8; { std::mem::size_of::<$n>() }]>::r(from).map($n::from_le_bytes)
- }
- })+
- };
-}
-n![u8 u16 u32 u64 u128 i8 i16 i32 i64 i128];
-
pub const CORE_HEADER_SIZE: u32 = 12;
pub const DIB_HEADER_SIZE: u32 = 40;
pub const DIB_HEADER_V2_SIZE: u32 = 52;
diff --git a/src/encode.rs b/src/encode.rs
index 31957c3..93fae03 100644
--- a/src/encode.rs
+++ b/src/encode.rs
@@ -4,28 +4,7 @@ use super::Color;
use super::Color::*;
use atools::prelude::*;
use bites::*;
-
-trait W<T>: Write {
- fn w(&mut self, x: T) -> io::Result<()>;
-}
-
-impl<T: Write, const N: usize> W<[u8; N]> for T {
- fn w(&mut self, x: [u8; N]) -> io::Result<()> {
- self.write_all(&x)
- }
-}
-
-impl<T: Write> W<u8> for T {
- fn w(&mut self, x: u8) -> io::Result<()> {
- self.w([x])
- }
-}
-
-impl<T: Write> W<u32> for T {
- fn w(&mut self, x: u32) -> io::Result<()> {
- self.w(x.to_le_bytes())
- }
-}
+use raad::le::*;
/// uses the so-called `BITMAPINFOHEADER`
const DIB_HEADER_SIZE: u32 = 40;
@@ -141,7 +120,7 @@ pub fn encode(
.rev()
.try_for_each(|mut x| {
x.try_for_each(|x| to.w(x))?;
- to.write_all(&[0; 4][..width as usize % 4])
+ to.w(&[0; 4][..width as usize % 4])
})?;
Ok(())
@@ -156,7 +135,7 @@ pub fn encode(
.rev()
.try_for_each(|mut x| {
x.try_for_each(|x| to.w(x))?;
- to.write_all(&[0; 4][..width as usize % 4])
+ to.w(&[0; 4][..width as usize % 4])
})?;
Ok(())
@@ -165,8 +144,8 @@ pub fn encode(
fn y(width: u32, data: &[u8], to: &mut impl Write) -> io::Result<()> {
to.w(GRAY)?;
data.chunks_exact(width as _).rev().try_for_each(|row| {
- to.write_all(row)?;
- to.write_all(&[0; 4][..width as usize % 4])
+ to.w(row)?;
+ to.w(&[0; 4][..width as usize % 4])
})?;
Ok(())
}