pub mod cog;
pub mod crs;
pub mod dem;
pub mod range;
pub use cog::Cog;
pub use crs::Crs;
pub use dem::Dem;
pub use range::tile_range_for_bounds;
const MAX_PIXELS_PER_FILE_BYTE: u64 = 8 * 1032;
const HEADER_SLACK_BYTES: u64 = 64 * 1024;
pub(crate) fn check_raster_size(
path: &std::path::Path,
file_len: u64,
width: u32,
height: u32,
) -> crate::error::Result<()> {
let pixels = u64::from(width) * u64::from(height);
let most = file_len
.saturating_add(HEADER_SLACK_BYTES)
.saturating_mul(MAX_PIXELS_PER_FILE_BYTE);
if pixels > most {
return Err(crate::error::GeolithError::GeoTiff(format!(
"{}: the header claims {width}x{height} pixels, more than a file of {file_len} bytes can hold",
path.display()
)));
}
Ok(())
}
pub(crate) fn check_chunk_size(
path: &std::path::Path,
chunk_width: u32,
chunk_height: u32,
) -> crate::error::Result<()> {
if chunk_width == 0 || chunk_height == 0 {
return Err(crate::error::GeolithError::GeoTiff(format!(
"{}: the header declares a {chunk_width}x{chunk_height} tile or strip",
path.display()
)));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
#[test]
fn a_tiny_file_cannot_claim_a_continent() {
let path = Path::new("dem.tif");
let err = check_raster_size(path, 200, 100_000, 100_000).expect_err("10^10 pixels");
let text = err.to_string();
assert!(
text.contains("dem.tif") && text.contains("100000x100000"),
"{text}"
);
assert!(check_raster_size(path, 200, u32::MAX, u32::MAX).is_err());
}
#[test]
fn a_plausible_header_passes() {
let path = Path::new("dem.tif");
assert!(check_raster_size(path, 25_934_402, 3601, 3601).is_ok());
assert!(check_raster_size(path, 14_000, 3601, 3601).is_ok());
let limit = (1_048_576 + HEADER_SLACK_BYTES) * MAX_PIXELS_PER_FILE_BYTE;
assert!(check_raster_size(path, 1_048_576, 65_536, (limit / 65_536) as u32).is_ok());
assert!(check_raster_size(path, 1_048_576, 65_536, (limit / 65_536) as u32 + 1).is_err());
}
#[test]
fn a_chunk_of_no_pixels_is_refused() {
let path = Path::new("cog.tif");
assert!(check_chunk_size(path, 0, 256).is_err());
assert!(check_chunk_size(path, 256, 0).is_err());
assert!(check_chunk_size(path, 256, 256).is_ok());
}
}