use crate::wmf::imports::*;
#[derive(Clone, Debug)]
pub struct BitmapInfoHeaderInfo {
pub header_size: u32,
pub width: i32,
pub height: i32,
pub planes: u16,
pub bit_count: crate::wmf::parser::BitCount,
pub compression: crate::wmf::parser::Compression,
pub image_size: u32,
pub x_pels_per_meter: i32,
pub y_pels_per_meter: i32,
pub color_used: u32,
pub color_important: u32,
}
impl BitmapInfoHeaderInfo {
#[cfg_attr(feature = "tracing", tracing::instrument(
level = tracing::Level::TRACE,
skip_all,
err(level = tracing::Level::ERROR, Display),
))]
pub(super) fn parse<R: crate::wmf::Read>(
buf: &mut R,
header_size: u32,
) -> Result<(Self, usize), crate::wmf::parser::ParseError> {
let (
(width, width_bytes),
(height, height_bytes),
(planes, planes_bytes),
(bit_count, bit_count_bytes),
(compression, compression_bytes),
(image_size, image_size_bytes),
(x_pels_per_meter, x_pels_per_meter_bytes),
(y_pels_per_meter, y_pels_per_meter_bytes),
(color_used, color_used_bytes),
(color_important, color_important_bytes),
) = (
crate::wmf::parser::read_i32_from_le_bytes(buf)?,
crate::wmf::parser::read_i32_from_le_bytes(buf)?,
crate::wmf::parser::read_u16_from_le_bytes(buf)?,
crate::wmf::parser::BitCount::parse(buf)?,
crate::wmf::parser::Compression::parse(buf)?,
crate::wmf::parser::read_u32_from_le_bytes(buf)?,
crate::wmf::parser::read_i32_from_le_bytes(buf)?,
crate::wmf::parser::read_i32_from_le_bytes(buf)?,
crate::wmf::parser::read_u32_from_le_bytes(buf)?,
crate::wmf::parser::read_u32_from_le_bytes(buf)?,
);
let consumed_bytes = width_bytes
+ height_bytes
+ planes_bytes
+ image_size_bytes
+ x_pels_per_meter_bytes
+ y_pels_per_meter_bytes
+ color_used_bytes
+ color_important_bytes
+ bit_count_bytes
+ compression_bytes;
if width <= 0 {
return Err(crate::wmf::parser::ParseError::UnexpectedPattern {
cause: "The width field must be positive".to_owned(),
});
}
if height == 0 {
return Err(crate::wmf::parser::ParseError::UnexpectedPattern {
cause: "The height field must not be zero".to_owned(),
});
}
if planes != 0x0001 {
return Err(crate::wmf::parser::ParseError::UnexpectedPattern {
cause: "The planes field must be 0x01".to_owned(),
});
}
Ok((
Self {
header_size,
width,
height,
planes,
bit_count,
compression,
image_size,
x_pels_per_meter,
y_pels_per_meter,
color_used,
color_important,
},
consumed_bytes,
))
}
}
pub(super) fn dib_image_size(width: u64, height: u64, planes: u64, bit_count: u64) -> usize {
let stride = (width
.saturating_mul(planes)
.saturating_mul(bit_count)
.saturating_add(31)
& !31)
/ 8;
usize::try_from(stride.saturating_mul(height)).unwrap_or(usize::MAX)
}
#[cfg(test)]
mod tests {
use super::dib_image_size;
use crate::wmf::parser::{BitCount, BitmapInfoHeader, BitmapInfoHeaderCore};
#[test]
fn dib_image_size_matches_formula_on_valid_dimensions() {
assert_eq!(dib_image_size(640, 480, 1, 24), 921_600);
}
#[test]
fn dib_image_size_saturates_on_huge_dimensions() {
let v = dib_image_size(u64::from(u32::MAX), u64::from(u32::MAX), 1, 32);
assert!(v > 0);
}
#[test]
fn core_header_size_does_not_overflow() {
let header = BitmapInfoHeader::Core(BitmapInfoHeaderCore {
header_size: 12,
width: u16::MAX,
height: u16::MAX,
planes: 1,
bit_count: BitCount::BI_BITCOUNT_5,
});
assert!(header.size() > 0);
}
}