windows file format device independent bitmap dib / bmp decoding and encoding
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..96c2cc0 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,66 @@ +//! # [DIB, also known as BMP](https://en.wikipedia.org/wiki/BMP_file_format) is a image format for bitmap storage. +//! +//! this crate implements [encoding](encode) and [decoding](decode) most types of BMPs. +#![allow(incomplete_features, mixed_script_confusables, internal_features)] +#![warn(missing_docs)] +#![feature( + maybe_uninit_uninit_array_transpose, + maybe_uninit_write_slice, + generic_const_exprs, + array_try_from_fn, + slice_as_chunks, + core_intrinsics, + slice_flatten, + effects, + test +)] +mod until; + +mod decode; +mod encode; + +pub use decode::{decode, decode_into}; +pub use encode::{encode, size as encoded_size}; + +pub use Color::*; +#[derive(Copy, Debug, Clone, PartialEq, Eq)] +#[repr(u8)] +/// Color types. +pub enum Color { + /// Grayscale + Y = 1, + /// Grayscale with alpha + YA, + /// Red, green, blue + RGB, + /// RGB with alpha + RGBA, +} + +impl Color { + /// Bits per pixel ([`depth`](Color::depth) [*](std::ops::Mul) [`8`](https://en.wikipedia.org/wiki/8)). + #[must_use] + pub const fn bpp(self) -> u8 { + self.depth() * 8 + } + + /// Color depth. + #[must_use] + pub const fn depth(self) -> u8 { + self as u8 + } +} + +#[test] +fn encode_decode() { + for elem in std::fs::read_dir("tdata").unwrap().map(Result::unwrap) { + if elem.path().extension().unwrap() == "imgbuf" { + let (dat, c, dim) = + decode(&mut std::fs::File::open(elem.path().with_extension("bmp")).unwrap()) + .unwrap(); + let mut v = vec![]; + encode(c, dim, &dat, &mut v).unwrap(); + assert_eq!(decode(&mut &v[..]).unwrap().0, dat); + } + } +} |