Skip to content

Commit e299a46

Browse files
Mavrick-Fclaude
andcommitted
fix(raster): write 8-bit bands as bytes only when every value is whole
An 8-bit raster can hold fractions after resampling (a block mean of 1.5). write_all wrote such bands as bytes and truncated them; it now writes bytes only when every valid value is whole, and floats otherwise. The mosaic's band-role error no longer says the alpha band is the last one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 995aaf1 commit e299a46

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

‎crates/data/src/raster/geotiff.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,9 @@ fn encode(r: &Raster, band: usize, every: bool) -> Result<Vec<u8>> {
17491749
// so an 8-bit image reads back as one; otherwise 32-bit floats.
17501750
let n = (w as usize) * (h as usize);
17511751
let samples = n.checked_mul(all.len()).ok_or_else(|| err("the raster is too large to write"))?;
1752-
let as_bytes = all.iter().all(|b| fits_bytes(r, *b)) && (r.nodata.is_some() || r.bands.iter().all(|b| b.stats.count == n));
1752+
// Only whole values: an 8-bit raster can hold fractions after resampling, kept as floats.
1753+
let whole = r.bands.iter().all(|b| b.data.iter().all(|v| !r.is_valid(*v) || v.fract() == 0.0));
1754+
let as_bytes = whole && all.iter().all(|b| fits_bytes(r, *b)) && (r.nodata.is_some() || r.bands.iter().all(|b| b.stats.count == n));
17531755
macro_rules! wide {
17541756
($colour:ty, $sample:expr) => {{
17551757
let mut data = super::room(samples, w as usize, h as usize)?;
@@ -2069,6 +2071,11 @@ mod tests {
20692071
let back = read(std::io::Cursor::new(write_all(&rgbi).unwrap()), "b").unwrap();
20702072
assert_eq!((back.band_count(), &back.color, back.data_type.as_str()), (5, &rgbi.color, "UInt8"));
20712073
assert!((0..5).all(|b| back.bands[b].data == rgbi.bands[b].data));
2074+
// An 8-bit raster holding a fraction (a resampled mean) keeps it: written as floats.
2075+
let mean = vec![vec![0.0, 1.5, 7.0, 9.0, 0.0, 3.0], vec![1.0; 6]];
2076+
let means = Raster::new("m", 3, 2, mean, gt, Crs::LonLat, Some(0.0), ColorHint::Gray, "UInt8").unwrap();
2077+
let back = read(std::io::Cursor::new(write_all(&means).unwrap()), "m").unwrap();
2078+
assert_eq!(back.value(0, 1, 0), Some(1.5));
20722079
// What `write` keeps whole is written as `write` writes it.
20732080
let rgb = raster(3, ColorHint::Rgb { alpha: false });
20742081
assert_eq!(write_all(&rgb).unwrap(), write(&rgb, 0).unwrap());

‎crates/engine/src/cmd/imagery.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,12 +1376,12 @@ fn mosaic_matches(first: &Raster, r: &Raster) -> Result<(), String> {
13761376
if r.band_count() != first.band_count() {
13771377
return Err(format!("{} has {} bands, {} {}", r.name, r.band_count(), first.name, first.band_count()));
13781378
}
1379-
// The mosaic takes the first input's colour, so a last band that is alpha in one input and
1380-
// data in another (RGBA and RGB with near-infrared) would take one role for both.
1379+
// The mosaic takes the first input's colour, so a band that is alpha in one input and data in
1380+
// another (RGBA and RGB with near-infrared) would take one role for both.
13811381
let alpha = |r: &Raster| matches!(r.color, ColorHint::Rgb { alpha: true } | ColorHint::GrayAlpha);
13821382
if alpha(r) != alpha(first) {
13831383
let (with, without) = if alpha(r) { (r, first) } else { (first, r) };
1384-
return Err(format!("{}'s last band is transparency, {}'s is data: use rasters with matching band roles", with.name, without.name));
1384+
return Err(format!("{} has a transparency band and {} doesn't: use rasters with matching band roles", with.name, without.name));
13851385
}
13861386
Ok(())
13871387
}

0 commit comments

Comments
 (0)