Merge pull request #3017 from FredApps/refactor/shared-bmp-density

Images: use the shared BMP density probe
This commit is contained in:
FredApps authored and GitHub committed 2026-11-19 23:19:53 +07:00
commit 129abd936b
2 files changed
+26 -9

No files matched your search

+2 -9
View File
@@ -232,13 +232,6 @@ fn webp_resolution(bytes: &[u8]) -> Option<(f64, f64)> {
/// BITMAPINFOHEADER pixels-per-meter (offset 38/42): meters → dpi.
fn bmp_resolution(bytes: &[u8]) -> Option<(f64, f64)> {
if bytes.len() < 46 || u32::from_le_bytes(bytes.get(14..18)?.try_into().ok()?) < 40 {
return None;
}
let ppm = |i: usize| -> Option<i32> { Some(i32::from_le_bytes(bytes.get(i..i + 4)?.try_into().ok()?)) };
let (x, y) = (ppm(38)?, ppm(42)?);
if x > 0 && y > 0 {
return Some((x as f64 * 0.0254, y as f64 * 0.0254));
}
None
let (x, y) = craft_image_codecs::bmp_probe::pixels_per_meter(bytes)?;
Some((f64::from(x) * 0.0254, f64::from(y) * 0.0254))
}
+24
View File
@@ -853,6 +853,30 @@ fn bmp_density_sets_natural_size() {
assert_eq!(natural_size(&flat), Some((600.0, 900.0)));
}
#[test]
fn bmp_density_preserves_unrounded_axes_and_placement_policy() {
let mut bmp = bmp_with_ppm(11811);
bmp[42..46].copy_from_slice(&23622i32.to_le_bytes());
let (x, y) = raster_resolution(&bmp).unwrap();
assert!((x - 299.9994).abs() < 1e-9, "{x}");
assert!((y - 599.9988).abs() < 1e-9, "{y}");
let (w, h) = natural_size(&bmp).unwrap();
assert!((w - 144.000288000576).abs() < 1e-9, "{w}");
assert!((h - 108.000216000432).abs() < 1e-9, "{h}");
for (x, y) in [(1, 11811), (11811, 1), (i32::MAX, 11811), (11811, i32::MAX)] {
bmp[38..42].copy_from_slice(&x.to_le_bytes());
bmp[42..46].copy_from_slice(&y.to_le_bytes());
assert!(raster_resolution(&bmp).is_some());
assert_eq!(natural_size(&bmp), Some((600.0, 900.0)));
}
for (x, y) in [(0i32, 11811i32), (-1, 11811), (11811, 0), (11811, -1)] {
bmp[38..42].copy_from_slice(&x.to_le_bytes());
bmp[42..46].copy_from_slice(&y.to_le_bytes());
assert_eq!(raster_resolution(&bmp), None);
assert_eq!(natural_size(&bmp), Some((600.0, 900.0)));
}
}
#[test]
fn tiff_resolution_tags_set_natural_size() {
use tiff::encoder::colortype::Gray8;