Skip to content
Merged
20 changes: 16 additions & 4 deletions crates/data/src/raster/cogwrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
//!
//! The untiled GeoTIFF ([`super::geotiff::write`]) is written here too, by [`write_strips`]: one
//! image in strips of rows, with the same predictors, Deflate effort and threads, and no overviews.
//!
//! Alpha: a float file's alpha is opaque at 1, so the alpha of an integer raster written as floats
//! is divided by its opaque value (255 for bytes), as the untiled writer does.

use std::io::{Seek, SeekFrom, Write};
use std::ops::Range;
Expand Down Expand Up @@ -137,10 +140,11 @@ fn fits_shorts(r: &Raster, b: usize) -> bool {
r.data_type == "UInt16" && nodata_ok && ((s.min >= 0.0 && s.max <= 65_535.0) || s.count == 0)
}

/// Chooses the file's samples like [`super::geotiff::write`] does: a colour raster's colour bands
/// (as bytes when they are bytes, alpha kept), otherwise `band` alone. 16-bit rasters stay 16-bit,
/// and a grey byte raster is written as bytes when no pixel needs a no-data value the bytes can't
/// hold. Grey with alpha keeps both bands; a palette raster keeps its colour table.
/// Chooses the file's samples like [`super::geotiff::write`] does, except for a colour cell with
/// no value under an opaque alpha: it is written see-through here (its alpha 0), not as floats
/// there. A colour raster's colour bands are written (as bytes when they are bytes, alpha kept),
/// otherwise `band` alone. 16-bit rasters stay 16-bit, and a grey byte raster is written as bytes
/// when no pixel needs a no-data value the bytes can't hold. Grey with alpha keeps both bands; a palette raster keeps its colour table.
fn plan(r: &Raster, band: usize) -> Result<Plan> {
let int_nodata = |max: f64| r.nodata.filter(|d| (0.0..=max).contains(d) && d.fract() == 0.0).map(|d| d as u16);
// The type of a set of bands: bytes, else 16-bit when that is what they are, else floats.
Expand Down Expand Up @@ -325,6 +329,7 @@ fn tile_samples(r: &Raster, plan: &Plan, lv: &Level, tx: u32, ty: u32) -> Vec<u8
let mut raw = Vec::with_capacity(t * t * spp * bps);
let float_pad = r.nodata.map_or(f32::NAN, |d| d as f32);
let int_pad = plan.nodata_int.unwrap_or(0);
let opaque = super::alpha_max(&r.data_type);
let mut px = vec![f32::NAN; spp];
for y in 0..t {
let oy = ty as usize * t + y;
Expand All @@ -342,6 +347,13 @@ fn tile_samples(r: &Raster, plan: &Plan, lv: &Level, tx: u32, ty: u32) -> Vec<u8
{
*alpha = 0.0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cloud-optimised writer now makes a NaN colour cell under an opaque alpha see-through (alpha 0) without any notice, where the GeoTIFF writers in the same PR report nothing either. The cell's opacity changes from 255 to 0 in the file. AGENTS.md's "Never silent" rule asks a writer that approximates data to say what, how much and where, and the docs paragraph is not a notice through Session::notify or ToolResult::messages. The see-through result may well be the right representation of a cell with no value, but it needs a count-based notice, or an explicit reason in the doc comment that the rule is met another way.

Category: agents-md

}
// A float file's alpha is opaque at 1: an integer raster's is divided by its opaque value.
// (No no-data tag goes with an alpha band, so none of its cells is a no-data one.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says no cell of the alpha band is a no-data one, but the source raster can still have a no-data value that equals an alpha value (for example an Int16 RGBA with no-data 32767 and alpha 32767). The COG file has no nodata tag, so after the division that cell is 1.0, an opaque pixel. The map draws the source cell see-through, because Raster::is_valid is false for it. The GeoTIFF path keeps it as no value, so the two files now differ. The visible result is the same as before this PR (the raw 32767 also read as opaque), so this is not a regression, but the comment is wrong and the division should skip the cells the source marks as no-data, the way float_alpha does. Confidence is moderate: I did not run the case.

Category: bug

if plan.sample == Sample::Float
&& let Some(alpha) = plan.alpha.and_then(|a| px.get_mut(a))
{
*alpha /= opaque;
}
for v in &px {
match plan.sample {
Sample::Byte => raw.push(if v.is_nan() { int_pad as u8 } else { v.round().clamp(0.0, 255.0) as u8 }),
Expand Down
Loading
Loading