use openreadout_core::model::{FileInfo, ImageInfo};
use openreadout_core::pixel::{PixelType, Plane};
use openreadout_core::reader::{Dataset, PlaneIndex};
use openreadout_core::region::Region;
use openreadout_core::select::Selection;
use openreadout_core::{Error, Result};
use crate::color::{self, Rgb};
use crate::diffraction::{self as dif, Diffraction};
use crate::figure_frame::{self, FigureLayout};
use crate::rulers::{self, Layout, Mapping};
use crate::{
Axis, Contrast, ImagePreview, Lut, PreviewChannel, PreviewOutput, PreviewRequest, SourcePoint,
split_select,
};
use openreadout_figure::{Figure, Raster, Rect};
const MAX_COMPOSITE_CHANNELS: usize = 16;
const AUTO_PERCENTILES: (f64, f64) = (0.1, 99.9);
struct Band {
w: usize,
h: usize,
v: Vec<f32>,
}
fn samples(p: &Plane) -> Result<Vec<f32>> {
let n = p.width as usize * p.height as usize * p.samples_per_pixel as usize;
let bps = p.pixel_type.bytes_per_sample();
if p.data.len() < n * bps {
return Err(Error::Other(format!(
"plane holds {} bytes, expected {}",
p.data.len(),
n * bps
)));
}
let d = &p.data[..n * bps];
Ok(match p.pixel_type {
PixelType::Uint8 => d.iter().map(|&b| f32::from(b)).collect(),
PixelType::Int8 => d.iter().map(|&b| f32::from(b as i8)).collect(),
PixelType::Uint16 => d
.as_chunks::<2>()
.0
.iter()
.map(|c| f32::from(u16::from_le_bytes([c[0], c[1]])))
.collect(),
PixelType::Int16 => d
.as_chunks::<2>()
.0
.iter()
.map(|c| f32::from(i16::from_le_bytes([c[0], c[1]])))
.collect(),
PixelType::Uint32 => d
.as_chunks::<4>()
.0
.iter()
.map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]) as f32)
.collect(),
PixelType::Int32 => d
.as_chunks::<4>()
.0
.iter()
.map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]]) as f32)
.collect(),
PixelType::Float => d
.as_chunks::<4>()
.0
.iter()
.map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
.collect(),
PixelType::Double => d
.as_chunks::<8>()
.0
.iter()
.map(|c| f64::from_le_bytes([c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]]) as f32)
.collect(),
other => d
.chunks_exact(bps)
.map(|c| other.sample_f64(c) as f32)
.collect(),
})
}
fn weights(n_in: usize, n_out: usize) -> Vec<Vec<(usize, f32)>> {
let scale = n_in as f64 / n_out.max(1) as f64;
(0..n_out)
.map(|o| {
if scale <= 1.0 {
let c = (((o as f64 + 0.5) * scale) as usize).min(n_in.saturating_sub(1));
return vec![(c, 1.0)];
}
let (a, b) = (o as f64 * scale, ((o + 1) as f64 * scale).min(n_in as f64));
let mut v = Vec::with_capacity(scale.ceil() as usize + 1);
let mut i = a.floor() as usize;
while (i as f64) < b && i < n_in {
let lo = a.max(i as f64);
let hi = b.min((i + 1) as f64);
if hi > lo {
v.push((i, (hi - lo) as f32));
}
i += 1;
}
v
})
.collect()
}
fn resample(src: &[f32], w: usize, h: usize, spp: usize, s: usize, ow: usize, oh: usize) -> Band {
if ow == w && oh == h {
return Band {
w,
h,
v: (0..w * h).map(|i| src[i * spp + s]).collect(),
};
}
let wx = weights(w, ow);
let wy = weights(h, oh);
let mut rows = vec![(0f32, 0f32); h * ow];
for y in 0..h {
let line = &src[y * w * spp..(y + 1) * w * spp];
for (ox, ws) in wx.iter().enumerate() {
let (mut sum, mut wt) = (0f32, 0f32);
for &(x, k) in ws {
let v = line[x * spp + s];
if v.is_finite() {
sum += v * k;
wt += k;
}
}
rows[y * ow + ox] = (sum, wt);
}
}
let mut v = vec![f32::NAN; ow * oh];
for (oy, ws) in wy.iter().enumerate() {
for ox in 0..ow {
let (mut sum, mut wt) = (0f32, 0f32);
for &(y, k) in ws {
let (rs, rw) = rows[y * ow + ox];
sum += rs * k;
wt += rw * k;
}
if wt > 0.0 {
v[oy * ow + ox] = sum / wt;
}
}
}
Band { w: ow, h: oh, v }
}
fn fitted(w: u32, h: u32, fit: u32) -> (usize, usize) {
let long = w.max(h).max(1);
let k = f64::from(fit.max(1)) / f64::from(long);
let side = |n: u32| ((f64::from(n) * k).round() as usize).max(1);
(side(w), side(h))
}
fn aspect_height(im: &ImageInfo, h: u32) -> u32 {
match (im.physical_size.x, im.physical_size.y) {
(Some(x), Some(y)) if x > 0.0 && y > 0.0 && ((y / x) - 1.0).abs() > 0.01 => {
((f64::from(h) * y / x).round() as u32).clamp(1, 1 << 16)
}
_ => h,
}
}
fn pyramid(im: &ImageInfo) -> Vec<(u32, u32, u32)> {
if !im.resolution_levels.is_empty() {
return im
.resolution_levels
.iter()
.filter(|l| l.level < im.pyramid_levels.max(1))
.filter(|l| l.size_z.is_none_or(|z| z == im.size_z))
.map(|l| (l.level, l.size_x, l.size_y))
.collect();
}
let mut out = vec![(0, im.size_x, im.size_y)];
if let Some(levels) = im.extra.get("pyramid").and_then(|v| v.as_array()) {
for l in levels {
let get = |k: &str| l.get(k).and_then(serde_json::Value::as_u64);
if let (Some(level), Some(w), Some(h)) = (get("level"), get("size_x"), get("size_y"))
&& level > 0
&& level < u64::from(im.pyramid_levels.max(1))
{
out.push((level as u32, w as u32, h as u32));
}
}
}
out
}
fn choose_level(im: &ImageInfo, max_size: u32) -> u32 {
pyramid(im)
.into_iter()
.filter(|&(_, w, h)| w.max(h) >= max_size)
.min_by_key(|&(l, w, h)| (w.max(h), l))
.map_or(0, |(l, _, _)| l)
}
fn choose_region_level(im: &ImageInfo, region: Region, max_size: u32) -> (u32, Region) {
let base = (im.size_x, im.size_y);
pyramid(im)
.into_iter()
.map(|(l, w, h)| (l, region.rescale(base, (w, h))))
.filter(|(l, r)| *l == 0 || r.width.max(r.height) >= max_size)
.min_by_key(|(l, r)| (r.width.max(r.height), *l))
.unwrap_or((0, region))
}
fn level_dims(im: &ImageInfo, level: u32) -> Option<(u32, u32)> {
pyramid(im)
.into_iter()
.find(|&(l, _, _)| l == level)
.map(|(_, w, h)| (w, h))
}
fn stored_windows(im: &ImageInfo) -> Vec<(f64, f64)> {
im.extra
.get("windows")
.and_then(serde_json::Value::as_array)
.map(|v| {
v.iter()
.filter_map(|w| {
let c = w.get("center")?.as_f64()?;
let wd = w.get("width")?.as_f64()?;
(c.is_finite() && wd.is_finite() && wd > 0.0).then_some((c, wd))
})
.collect()
})
.unwrap_or_default()
}
fn resolve_contrast(
c: Contrast,
im: &ImageInfo,
rgb: bool,
notes: &mut Vec<String>,
) -> Result<Contrast> {
let pt = im.pixel_type;
let hu = im
.extra
.get("value_unit")
.and_then(serde_json::Value::as_str)
== Some("HU");
Ok(match c {
Contrast::Auto if rgb && pt == PixelType::Uint8 => Contrast::Raw,
Contrast::Auto if !rgb => match stored_windows(im).first() {
Some(&(center, width)) => {
notes.push(format!(
"contrast: the file's display window (center {center}, width {width}); contrast window:brain, window:bone, ... for presets"
));
Contrast::Window(center, width)
}
None if hu => {
notes.push("contrast: CT soft-tissue window (center 40 HU, width 400 HU); contrast window:bone, window:brain, window:lung, ... for others".into());
Contrast::Window(40.0, 400.0)
}
None => Contrast::Percentile(AUTO_PERCENTILES.0, AUTO_PERCENTILES.1),
},
Contrast::Auto => Contrast::Percentile(AUTO_PERCENTILES.0, AUTO_PERCENTILES.1),
Contrast::StoredWindow(n) => {
let w = stored_windows(im);
let &(center, width) = w.get(n).ok_or_else(|| {
Error::Usage(format!(
"window:file:{n}: the image stores {} display windows (images[].extra.windows)",
w.len()
))
})?;
Contrast::Window(center, width)
}
other => other,
})
}
fn raw_range(pt: PixelType) -> (f64, f64) {
match pt {
PixelType::Uint8 => (0.0, 255.0),
PixelType::Int8 => (-128.0, 127.0),
PixelType::Uint16 => (0.0, 65_535.0),
PixelType::Int16 => (-32_768.0, 32_767.0),
PixelType::Uint32 => (0.0, f64::from(u32::MAX)),
PixelType::Int32 => (f64::from(i32::MIN), f64::from(i32::MAX)),
_ => (0.0, 1.0),
}
}
fn percentile(sorted: &[f64], p: f64) -> f64 {
let pos = p / 100.0 * (sorted.len() - 1) as f64;
let lo = pos.floor() as usize;
let hi = (lo + 1).min(sorted.len() - 1);
sorted[lo] + (sorted[hi] - sorted[lo]) * (pos - lo as f64)
}
#[derive(Debug, Default, Clone, Copy, PartialEq)]
struct Screened {
ceiling: Option<f64>,
skip: Option<f64>,
}
const MAX_OVER_RANGE_PERCENT: usize = 1;
fn type_max(pt: PixelType) -> Option<f32> {
match pt {
PixelType::Uint8 => Some(f32::from(u8::MAX)),
PixelType::Uint16 => Some(f32::from(u16::MAX)),
PixelType::Uint32 => Some(u32::MAX as f32),
_ => None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum FindingKind {
Fill,
Blended,
AtMaximum,
}
#[derive(Debug, Clone, Copy, PartialEq)]
struct Finding {
kind: FindingKind,
share: f64,
at_top: f64,
}
fn share(n: usize, of: usize) -> f64 {
100.0 * n as f64 / of.max(1) as f64
}
#[allow(clippy::float_cmp)] fn screen_samples(acc: &mut [f32], im: &ImageInfo, auto: bool) -> (Screened, Option<Finding>) {
let none = (Screened::default(), None);
let Some(top) = type_max(im.pixel_type) else {
return none;
};
let finite = acc.iter().filter(|v| v.is_finite()).count();
if finite == 0 {
return none;
}
if let Some((ceiling, _)) = openreadout_core::stats::recorded_saturation(im) {
let ceil = ceiling as f32;
let above = acc.iter().filter(|&&v| v > ceil).count();
if above > 0 {
let at_top = acc.iter().filter(|&&v| v == top).count();
if at_top * 2 >= above {
for v in acc.iter_mut().filter(|v| **v > ceil) {
*v = f32::NAN;
}
let f = Finding {
kind: FindingKind::Fill,
share: share(above, finite),
at_top: share(at_top, above),
};
return (Screened::default(), Some(f));
}
if above * 100 <= finite * MAX_OVER_RANGE_PERCENT {
if !auto {
return none;
}
let f = Finding {
kind: FindingKind::Blended,
share: share(above, finite),
at_top: 0.0,
};
let s = Screened {
ceiling: Some(ceiling),
skip: None,
};
return (s, Some(f));
}
}
}
if auto {
let at_top = acc.iter().filter(|&&v| v == top).count();
let allowed = (100.0 - AUTO_PERCENTILES.1) / 100.0 * finite as f64;
if at_top < finite && at_top as f64 > allowed {
let f = Finding {
kind: FindingKind::AtMaximum,
share: share(at_top, finite),
at_top: 100.0,
};
let s = Screened {
ceiling: None,
skip: Some(f64::from(top)),
};
return (s, Some(f));
}
}
none
}
fn percent_range(v: &[f64]) -> String {
let lo = v.iter().copied().fold(f64::INFINITY, f64::min);
let hi = v.iter().copied().fold(f64::NEG_INFINITY, f64::max);
let (a, b) = (format!("{lo:.2}"), format!("{hi:.2}"));
if a == b {
format!("{a}%")
} else {
format!("{a}-{b}%")
}
}
fn finding_notes(im: &ImageInfo, found: &[(u32, Finding)]) -> Vec<String> {
let top = type_max(im.pixel_type).unwrap_or(f32::MAX);
let (bits, field) = openreadout_core::stats::recorded_saturation(im).map_or_else(
|| (String::new(), ""),
|(ceiling, field)| (format!("{}-bit", (ceiling + 1.0).log2().round()), field),
);
let mut kinds: Vec<FindingKind> = found.iter().map(|(_, f)| f.kind).collect();
kinds.sort_unstable();
kinds.dedup();
kinds
.into_iter()
.map(|kind| {
let of: Vec<(u32, Finding)> = found.iter().copied().filter(|(_, f)| f.kind == kind).collect();
let chans = of
.iter()
.map(|(c, _)| format!("c={c}"))
.collect::<Vec<_>>()
.join(", ");
let shares = percent_range(&of.iter().map(|(_, f)| f.share).collect::<Vec<_>>());
match kind {
FindingKind::Fill => format!(
"{chans}: {shares} of the samples lie above the {bits} range the file records ({field}), {} of those at {top}: the fill written where nothing was acquired (pyramid tiles past the scanned tiles); drawn as background, like uncovered canvas, and left out of the contrast",
percent_range(&of.iter().map(|(_, f)| f.at_top).collect::<Vec<_>>()),
),
FindingKind::Blended => format!(
"{chans}: {shares} of the samples lie above the {bits} range the file records ({field}) (tile edges blended with fill): drawn, but left out of the contrast"
),
FindingKind::AtMaximum => format!(
"{chans}: {shares} of the samples are {top}, the pixel type's maximum (fill where nothing was acquired, or clipping): the contrast is computed without them"
),
}
})
.collect()
}
#[allow(clippy::float_cmp)] fn display_range(bands: &[&Band], c: Contrast, pt: PixelType, screened: Screened) -> (f64, f64) {
let (lo, hi) = match c {
Contrast::Raw => raw_range(pt),
Contrast::Window(c, w) => (c - w / 2.0, c + w / 2.0),
Contrast::StoredWindow(_) => raw_range(pt),
Contrast::Auto | Contrast::MinMax | Contrast::Percentile(..) | Contrast::Log(..) => {
let finite = || {
bands
.iter()
.flat_map(|b| b.v.iter())
.filter(|v| v.is_finite())
.map(|&v| f64::from(v))
};
let counts = |v: &f64| {
screened.ceiling.is_none_or(|m| *v <= m) && screened.skip.is_none_or(|s| *v != s)
};
let mut vals: Vec<f64> = finite().filter(counts).collect();
if vals.is_empty() {
vals = finite().collect();
}
if vals.is_empty() {
return (0.0, 1.0);
}
vals.sort_by(f64::total_cmp);
match c {
Contrast::Percentile(a, b) | Contrast::Log(a, b) => {
(percentile(&vals, a), percentile(&vals, b))
}
_ => (vals[0], vals[vals.len() - 1]),
}
}
};
if hi > lo { (lo, hi) } else { (lo, lo + 1.0) }
}
fn level_of(v: f32, lo: f64, hi: f64) -> f32 {
if !v.is_finite() {
return 0.0;
}
((f64::from(v) - lo) / (hi - lo)).clamp(0.0, 1.0) as f32
}
fn log_level_of(v: f32, lo: f64, hi: f64) -> f32 {
if !v.is_finite() {
return 0.0;
}
let x = (f64::from(v) - lo).max(0.0);
((x.ln_1p()) / (hi - lo).max(1e-12).ln_1p()).clamp(0.0, 1.0) as f32
}
fn dif_note(g: &Diffraction, contrast: Contrast, lut: Lut) -> String {
let mut parts = vec![
"diffraction image: shrunk by the maximum of each block so single-pixel spots stay visible"
.to_string(),
];
if matches!(contrast, Contrast::Log(..)) {
parts.push(format!("{contrast} contrast"));
}
if lut == Lut::Inverted {
parts.push("dark spots on a light ground".into());
}
let geo = &g.geometry;
if geo.negative_is_masked || !geo.masked_values.is_empty() {
parts.push(
"pixels without a measurement (module gaps, bad pixels) in pale blue-grey".into(),
);
}
if let Some(o) = geo.overload {
parts.push(format!("overloads (≥ {o}) in red"));
}
parts.join("; ")
}
fn axis_indices(
name: &str,
sel: &[u32],
size: u32,
default: u32,
project: bool,
) -> Result<Vec<u32>> {
for &i in sel {
if i >= size {
return Err(Error::Usage(format!(
"{name}={i} out of range (image has {size}, 0..{size})"
)));
}
}
if project {
return Ok(if sel.is_empty() {
(0..size).collect()
} else {
sel.to_vec()
});
}
match sel {
[] => Ok(vec![default]),
[one] => Ok(vec![*one]),
_ => Err(Error::Usage(format!(
"a preview shows one {name} at a time; pass --mip {name} (maximum) or --sum {name} to project several"
))),
}
}
fn plane_bytes(im: &ImageInfo, level: u32, region: Option<Region>) -> u64 {
let (w, h) = match region {
Some(r) => (r.width, r.height),
None if level == 0 => (im.size_x, im.size_y),
None => level_dims(im, level).unwrap_or((im.size_x, im.size_y)),
};
u64::from(w)
.saturating_mul(u64::from(h))
.saturating_mul(u64::from(im.samples_per_pixel.max(1)))
.saturating_mul(im.pixel_type.bytes_per_sample() as u64)
}
#[derive(Clone, Copy)]
struct Budget<'a> {
im: &'a ImageInfo,
per_plane: u64,
planes: u64,
}
impl Budget<'_> {
fn check(&self, level: u32, region: Option<Region>) -> Result<()> {
let need = plane_bytes(self.im, level, region);
if need <= self.per_plane {
return Ok(());
}
let mib = |b: u64| b.div_ceil(1 << 20);
Err(Error::unsupported(
"preview",
format!(
"a quick look decoding {} MiB of pixels ({} plane(s) at level {level}; budget {} MiB)",
mib(need.saturating_mul(self.planes)),
self.planes,
mib(self.per_plane.saturating_mul(self.planes))
),
"Preview a region (full-resolution pixels) or a smaller pyramid level to look at part of the image.",
))
}
}
#[allow(clippy::too_many_arguments)]
fn read_plane(
ds: &mut dyn Dataset,
image: u32,
idx: PlaneIndex,
level: &mut u32,
region: &mut Option<Region>,
region0: Option<Region>,
auto: bool,
budget: Option<Budget<'_>>,
notes: &mut Vec<String>,
) -> Result<Plane> {
if let Some(b) = budget {
b.check(*level, *region)?;
}
let got = match *region {
Some(r) => ds.read_region(image, idx, *level, r),
None => ds.read_plane_level(image, idx, *level),
};
match got {
Err(Error::Unsupported { .. }) if auto && *level > 0 => {
notes.push(format!(
"pyramid level {level} is listed but not readable by this reader; used level 0",
level = *level
));
if let Some(b) = budget {
b.check(0, region0)?;
}
*level = 0;
*region = region0;
match region0 {
Some(r) => ds.read_region(image, idx, 0, r),
None => ds.read_plane(image, idx),
}
}
other => other,
}
}
fn blank_unstored(s: &mut [f32], w: usize, h: usize, spp: usize, areas: &[Region]) {
let mut stored = vec![false; w * h];
for a in areas {
let (x0, y0) = (a.x as usize, a.y as usize);
let x1 = (x0 + a.width as usize).min(w);
let y1 = (y0 + a.height as usize).min(h);
for y in y0.min(y1)..y1 {
stored[y * w + x0.min(x1)..y * w + x1].fill(true);
}
}
for (px, _) in s.chunks_exact_mut(spp).zip(&stored).filter(|(_, k)| !**k) {
px.fill(f32::NAN);
}
}
enum Framing {
Rulers(Layout),
Figure(FigureLayout),
}
impl Framing {
fn h(&self) -> u32 {
match self {
Framing::Rulers(l) => l.h(),
Framing::Figure(f) => f.h(),
}
}
fn v(&self) -> u32 {
match self {
Framing::Rulers(l) => l.v(),
Framing::Figure(f) => f.v(),
}
}
}
pub(crate) fn render(
ds: &mut dyn Dataset,
info: &FileInfo,
req: &PreviewRequest,
out: &mut PreviewOutput,
) -> Result<(Figure, crate::Frame)> {
let image = req.image.unwrap_or(0);
let im = info
.images
.iter()
.find(|i| i.index == image)
.ok_or_else(|| {
Error::Usage(format!(
"image {image} not found (file has {} images)",
info.images.len()
))
})?
.clone();
if im.size_c == 0 || im.size_z == 0 || im.size_t == 0 || im.size_x == 0 || im.size_y == 0 {
return Err(Error::Usage(format!("image {image} has no pixels")));
}
let sel = Selection::parse(&split_select(&req.select))?;
let rgb = im.samples_per_pixel == 3;
if req.mip.is_some() && req.sum.is_some() {
return Err(Error::Usage("pass mip (maximum) or sum, not both".into()));
}
let projection = req.mip.or(req.sum);
let mut composite = req.composite;
if sel.c.len() > 1 && !composite {
composite = true;
out.notes
.push("several channels selected: rendered as a composite".into());
}
if rgb && composite {
return Err(Error::Usage(
"composites blend single-sample channels; this image stores RGB planes, preview one channel (c=N) at a time".into(),
));
}
let geo = (!rgb && !composite).then(|| Diffraction::of(&im)).flatten();
let lut = req.lut.unwrap_or(if composite {
Lut::ChannelColor
} else if geo.is_some() {
Lut::Inverted
} else {
Lut::Gray
});
if composite && matches!(lut, Lut::Gray | Lut::Inverted) {
return Err(Error::Usage(
"--lut gray and inverted cannot show a composite; drop --lut or preview one channel"
.into(),
));
}
let c_list: Vec<u32> = if composite {
for &c in &sel.c {
if c >= im.size_c {
return Err(Error::Usage(format!(
"c={c} out of range (image has {}, 0..{})",
im.size_c, im.size_c
)));
}
}
let mut all: Vec<u32> = if sel.c.is_empty() {
(0..im.size_c).collect()
} else {
sel.c.clone()
};
if all.len() > MAX_COMPOSITE_CHANNELS {
out.notes.push(format!(
"composite limited to the first {MAX_COMPOSITE_CHANNELS} of {} channels",
all.len()
));
all.truncate(MAX_COMPOSITE_CHANNELS);
}
all
} else {
axis_indices("c", &sel.c, im.size_c, 0, false)?
};
let z_list = axis_indices(
"z",
&sel.z,
im.size_z,
im.size_z / 2,
projection == Some(Axis::Z),
)?;
let t_list = axis_indices("t", &sel.t, im.size_t, 0, projection == Some(Axis::T))?;
let has_bar = im.physical_size.x.is_some_and(|v| v.is_finite() && v > 0.0);
let titled = req.title.is_some() || req.caption.is_some();
let mut layout = if req.axes && !req.figure {
Some(Framing::Rulers(Layout::new(
req.max_size,
im.size_x,
im.size_y,
)))
} else if (req.axes && req.figure) || titled {
Some(Framing::Figure(FigureLayout::new(
req.max_size,
has_bar && req.axes,
req.title.as_deref(),
req.caption.as_deref(),
)))
} else {
None
};
if let Some(l) = &layout {
let room = req.max_size.saturating_sub(l.h().max(l.v()));
if room < 16 || room < req.max_size / 2 {
out.notes.push(format!(
"max size {} is too small for the frame; drawn without it",
req.max_size
));
layout = None;
}
}
if req.grid && !matches!(layout, Some(Framing::Rulers(_))) {
out.notes
.push("grid lines are drawn only with the rulers (axes)".into());
}
if titled && !matches!(layout, Some(Framing::Figure(_))) {
out.notes.push(
"title and caption are drawn with axes scale-bar or none, not with rulers".into(),
);
}
if req.axes && layout.is_some() && !has_bar {
out.notes.push(
"the file records no physical pixel size: no scale bar, and sizes are in pixels".into(),
);
}
let fit = layout
.as_ref()
.map_or(req.max_size, |l| req.max_size - l.h().max(l.v()));
let planes = (c_list.len() * z_list.len() * t_list.len()) as u64;
let budget = req.max_read_bytes.map(|b| Budget {
im: &im,
per_plane: b / planes.max(1),
planes,
});
let auto_level = req.level.is_none();
let (mut level, mut region) = match (req.level, req.region) {
(Some(l), r) => (l, r),
(None, Some(r)) => {
r.check_within(im.size_x, im.size_y, &format!("image {image} (level 0)"))?;
let (l, rl) = choose_region_level(&im, r, req.max_size);
(l, Some(rl))
}
(None, None) => (choose_level(&im, req.max_size), None),
};
let region0 = if auto_level { req.region } else { None };
let spp = im.samples_per_pixel.max(1) as usize;
let mut first_dims: Option<(u32, u32)> = None;
let mut out_dims = (1usize, 1usize);
let contrast = match (req.contrast, &geo) {
(Contrast::Auto, Some(_)) => Contrast::Log(dif::AUTO_LOG.0, dif::AUTO_LOG.1),
(c, _) => resolve_contrast(c, &im, rgb, &mut out.notes)?,
};
let display_inverted = im
.extra
.get("display_inverted")
.and_then(serde_json::Value::as_bool)
== Some(true);
let auto_contrast = req.contrast == Contrast::Auto && contrast != Contrast::Raw;
let mut per_channel: Vec<(u32, Vec<Band>, Screened)> = Vec::new();
let mut spectrum: Option<(usize, Vec<f32>)> = None;
let mut found: Vec<(u32, Finding)> = Vec::new();
for &c in &c_list {
let mut acc: Option<Vec<f32>> = None;
let mut dims = (0u32, 0u32);
let reader_sum = req.sum == Some(Axis::T)
&& t_list.len() > 1
&& region.is_none()
&& level == 0
&& spp == 1
&& budget.is_none();
let t_groups: Vec<Vec<u32>> = if reader_sum {
vec![t_list.clone()]
} else {
t_list.iter().map(|&t| vec![t]).collect()
};
for &z in &z_list {
for group in &t_groups {
let t = group[0];
let p = if group.len() > 1 {
ds.sum_planes(image, c, z, group)?
} else {
read_plane(
ds,
image,
PlaneIndex { c, z, t },
&mut level,
&mut region,
region0,
auto_level,
budget,
&mut out.notes,
)?
};
let mut s = samples(&p)?;
if let Some(g) = &geo {
g.mask(&mut s);
}
if let Some(areas) =
ds.stored_areas(image, PlaneIndex { c, z, t }, level, region)?
{
blank_unstored(
&mut s,
p.width as usize,
p.height as usize,
p.samples_per_pixel.max(1) as usize,
&areas,
);
}
dims = (p.width, p.height);
if p.samples_per_pixel as usize != spp {
return Err(Error::Other(format!(
"plane has {} samples per pixel, image declares {spp}",
p.samples_per_pixel
)));
}
acc = Some(match acc {
None => s,
Some(mut a) => {
if a.len() != s.len() {
return Err(Error::Other(
"planes of one image differ in size; cannot project".into(),
));
}
if req.sum.is_some() {
for (x, y) in a.iter_mut().zip(s) {
*x += y;
}
} else {
for (x, y) in a.iter_mut().zip(s) {
*x = x.max(y);
}
}
a
}
});
}
}
let mut acc = acc.unwrap_or_default();
if req.fft && spectrum.is_none() && spp == 1 && !composite {
let k = level_dims(&im, level)
.map_or(1.0, |(lw, _)| f64::from(im.size_x) / f64::from(lw.max(1)));
let pixel_a = im.physical_size.x.map(|um| um * 1e4 * k);
let bin = crate::fft_inset::bin_for(dims.0.min(dims.1) as usize, pixel_a);
spectrum =
crate::fft_inset::power_spectrum(&acc, dims.0 as usize, dims.1 as usize, bin);
if spectrum.is_none() {
out.notes.push(format!(
"no power-spectrum inset: the plane read is smaller than {} px",
crate::fft_inset::MIN_SIDE
));
}
}
let (screened, finding) = screen_samples(&mut acc, &im, auto_contrast);
found.extend(finding.map(|f| (c, f)));
match first_dims {
None => {
first_dims = Some(dims);
let shown = (dims.0, aspect_height(&im, dims.1));
let fit = if let Some(Framing::Figure(f)) = &layout {
f.fit(req.max_size, shown.0, shown.1)
} else if layout.is_some() {
fit
} else {
fit.min(shown.0.max(shown.1))
};
out_dims = fitted(shown.0, shown.1, fit);
}
Some(d) if d != dims => {
return Err(Error::Other(format!(
"channels of image {image} differ in size ({}x{} vs {}x{})",
d.0, d.1, dims.0, dims.1
)));
}
Some(_) => {}
}
let (w, h) = (dims.0 as usize, dims.1 as usize);
let bands = (0..spp)
.map(|s| {
if geo.is_some() {
let (w2, h2, v) = dif::resample_max(&acc, w, h, out_dims.0, out_dims.1);
Band { w: w2, h: h2, v }
} else {
resample(&acc, w, h, spp, s, out_dims.0, out_dims.1)
}
})
.collect();
per_channel.push((c, bands, screened));
}
out.notes.extend(finding_notes(&im, &found));
let (sw, sh) = match region {
Some(_) => level_dims(&im, level).unwrap_or((0, 0)),
None => first_dims.unwrap_or((0, 0)),
};
let (ow, oh) = per_channel
.first()
.and_then(|(_, b, _)| b.first())
.map_or((0, 0), |b| (b.w, b.h));
let mut canvas = Raster::new(ow as u32, oh as u32, [0, 0, 0]);
let mut channels = Vec::new();
let mut acc = vec![0f32; ow * oh * 3];
for (pos, (c, bands, screened)) in per_channel.iter().enumerate() {
let ch = im.channels.iter().find(|x| x.index == *c);
let refs: Vec<&Band> = bands.iter().collect();
let (lo, hi) = display_range(&refs, contrast, im.pixel_type, *screened);
let (col, source): (Rgb, &str) = if rgb {
([255, 255, 255], "rgb")
} else if matches!(lut, Lut::Gray | Lut::Inverted) {
([255, 255, 255], "lut")
} else {
let (c, s) = color::channel_color(ch, pos);
(c, s.as_str())
};
if rgb {
for i in 0..ow * oh {
let blank = bands.iter().take(3).any(|b| !b.v[i].is_finite());
for (k, band) in bands.iter().take(3).enumerate() {
acc[i * 3 + k] = if blank {
255.0
} else {
255.0 * level_of(band.v[i], lo, hi)
};
}
}
} else {
let band = &bands[0];
let log = matches!(contrast, Contrast::Log(..));
let inverted = lut == Lut::Inverted;
for i in 0..ow * oh {
let v = band.v[i];
let mut l = if log {
log_level_of(v, lo, hi)
} else {
level_of(v, lo, hi)
};
if inverted {
l = 1.0 - l;
}
if display_inverted && v.is_finite() {
l = 1.0 - l;
}
let fixed = match &geo {
Some(_) if !v.is_finite() => Some(dif::NO_DATA),
Some(g) if g.overloaded(v) => Some(dif::OVERLOAD),
_ => None,
};
for k in 0..3 {
acc[i * 3 + k] += fixed.map_or(f32::from(col[k]) * l, |c| f32::from(c[k]));
}
}
}
channels.push(PreviewChannel {
index: *c,
name: ch.and_then(|x| x.name.clone()),
color: color::to_hex(col),
color_source: source.into(),
display_min: lo,
display_max: hi,
});
}
for (o, a) in canvas.rgb.iter_mut().zip(&acc) {
*o = a.round().clamp(0.0, 255.0) as u8;
}
if req.fft && (rgb || composite) {
out.notes
.push("the power-spectrum inset is drawn on single-channel images".into());
}
let inset = spectrum.and_then(|(side, ps)| paint_inset(&mut canvas, side, &ps));
if let Some(r) = inset {
out.notes.push(format!(
"power-spectrum inset (top right, {}×{} px): the averaged log power spectrum of tiles from the middle of the plane read, zero frequency in its middle; it covers part of the picture",
r.width, r.height
));
}
let level_size = if level == 0 {
Some((im.size_x, im.size_y))
} else {
level_dims(&im, level).or(if region.is_none() { first_dims } else { None })
};
let (kx, ky) = match level_size {
Some((w, h)) if w > 0 && h > 0 => (
f64::from(im.size_x) / f64::from(w),
f64::from(im.size_y) / f64::from(h),
),
_ => {
out.notes.push(format!(
"the size of level {level} is not known: rulers and source_* fields count level-{level} pixels"
));
(1.0, 1.0)
}
};
let step = first_dims.map_or((1.0, 1.0), |(w, h)| {
(
f64::from(w) / out_dims.0.max(1) as f64,
f64::from(h) / out_dims.1.max(1) as f64,
)
});
let map = Mapping {
origin: region.map_or((0.0, 0.0), |r| (f64::from(r.x) * kx, f64::from(r.y) * ky)),
per_pixel: (step.0 * kx, step.1 * ky),
};
let read_end = match (region, level_size) {
(Some(r), Some(ls)) => {
let r0 = r.rescale(ls, (im.size_x, im.size_y));
(r0.x + r0.width, r0.y + r0.height)
}
_ => (im.size_x, im.size_y),
};
let full_res_region = map.covered(ow as u32, oh as u32, read_end);
let rulers_drawn = matches!(layout, Some(Framing::Rulers(_)));
let (mut figure, plot_area, scale_bar) = if let Some(l) = &layout {
let theme = crate::theme(req);
let d = match l {
Framing::Rulers(l) => {
rulers::decorate(canvas, *l, map, im.physical_size.x, req.grid, &theme)
}
Framing::Figure(f) => {
figure_frame::decorate(canvas, f, map, im.physical_size.x, &theme)
}
};
(d.figure, d.plot_area, d.scale_bar)
} else {
let (w, h) = (ow as f32, oh as f32);
let mut f = Figure::new(w, h, None);
f.image(
Rect::new(0.0, 0.0, w, h),
std::sync::Arc::new(canvas),
false,
);
(f, Region::new(0, 0, ow as u32, oh as u32), None)
};
if let Some(g) = &geo {
out.notes.push(dif_note(g, contrast, lut));
if matches!(req.marks, crate::Marks::Auto | crate::Marks::Rings) {
let shown = full_res_region;
let marks = dif::draw_rings(
&mut figure,
plot_area,
map,
shown,
&g.geometry,
&crate::theme(req),
);
if marks.iter().all(|m| m.kind != "resolution_ring") && req.marks == crate::Marks::Rings
{
out.notes.push("no resolution rings: they need the wavelength, the detector distance, the beam centre and square pixels, on a detector normal to the beam".into());
}
out.marks.extend(marks);
}
} else if req.marks == crate::Marks::Rings {
out.notes
.push("resolution rings are drawn on diffraction images (extra.diffraction)".into());
}
let fft_inset = inset.map(|r| {
let r = Region::new(plot_area.x + r.x, plot_area.y + r.y, r.width, r.height);
let theme = crate::theme(req);
let st = openreadout_figure::TextStyle::new(
theme.small_size(),
openreadout_figure::Color::rgb(255, 255, 255),
)
.face(openreadout_figure::Face::SansMedium)
.halo(openreadout_figure::Color::rgb(29, 31, 36), 2.0)
.align(
openreadout_figure::Anchor::Start,
openreadout_figure::Baseline::Top,
);
figure.text(r.x as f32 + 4.0, r.y as f32 + 3.0, "FFT", &st);
r
});
let frame = crate::Frame::Image { plot_area, map };
out.image = Some(ImagePreview {
image,
level,
source_width: sw,
source_height: sh,
region,
downsample: step.0,
c: c_list,
z: z_list,
t: t_list,
projection: projection.map(|a| {
let op = if req.sum.is_some() { "sum" } else { "max" };
match a {
Axis::Z => format!("{op}-z"),
Axis::T => format!("{op}-t"),
}
}),
composite,
rgb,
contrast: contrast.to_string(),
lut: if rgb {
"rgb".into()
} else if lut == Lut::Gray {
"gray".into()
} else if lut == Lut::Inverted {
"inverted".into()
} else {
"channel-color".into()
},
channels,
axes: rulers_drawn,
grid: rulers_drawn && req.grid,
plot_area,
source_origin: SourcePoint {
x: map.origin.0,
y: map.origin.1,
},
source_per_pixel: map.per_pixel.0,
source_per_pixel_y: map.per_pixel.1,
full_res_region,
scale_bar,
view: None,
crosshair: None,
view_slices: None,
fft_inset,
});
Ok((figure, frame))
}
fn paint_inset(canvas: &mut Raster, side: usize, ps: &[f32]) -> Option<Region> {
let (cw, ch) = (canvas.width as usize, canvas.height as usize);
let s = ((cw.min(ch) as f32 * 0.3) as usize).clamp(48, 360);
let m = (cw.min(ch) / 80).max(3);
if s + 2 * m + 2 > cw.min(ch) {
return None;
}
let band = resample(ps, side, side, 1, 0, s, s);
let (x0, y0) = (cw - m - s, m);
for y in y0.saturating_sub(1)..(y0 + s + 1).min(ch) {
for x in x0.saturating_sub(1)..(x0 + s + 1).min(cw) {
let i = (y * cw + x) * 3;
canvas.rgb[i..i + 3].copy_from_slice(&[247, 245, 240]);
}
}
for y in 0..s {
for x in 0..s {
let v = band.v[y * s + x];
let g = if v.is_finite() {
(v * 255.0).round().clamp(0.0, 255.0) as u8
} else {
0
};
let i = ((y0 + y) * cw + x0 + x) * 3;
canvas.rgb[i..i + 3].copy_from_slice(&[g, g, g]);
}
}
Some(Region::new(x0 as u32, y0 as u32, s as u32, s as u32))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resampling_averages_areas_and_skips_nan() {
let src = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
let b = resample(&src, 4, 2, 1, 0, 2, 1);
assert_eq!((b.w, b.h), (2, 1));
assert_eq!(b.v, vec![3.5, 5.5]);
let b = resample(&[0.0, 3.0, 6.0], 3, 1, 1, 0, 2, 1);
assert_eq!(b.v, vec![1.0, 5.0]);
let b = resample(&[f32::NAN, 2.0], 2, 1, 1, 0, 1, 1);
assert_eq!(b.v, vec![2.0]);
let b = resample(&[1.0, 2.0], 2, 1, 1, 0, 4, 2);
assert_eq!(b.v, vec![1.0, 1.0, 2.0, 2.0, 1.0, 1.0, 2.0, 2.0]);
assert_eq!(fitted(4000, 3000, 768), (768, 576));
}
#[test]
fn percentiles_and_ranges() {
let s: Vec<f64> = (0..=100).map(f64::from).collect();
assert!((percentile(&s, 1.0) - 1.0).abs() < 1e-12);
assert!((percentile(&s, 99.5) - 99.5).abs() < 1e-12);
let b = Band {
w: 2,
h: 1,
v: vec![5.0, 5.0],
};
assert_eq!(
display_range(
&[&b],
Contrast::MinMax,
PixelType::Uint16,
Screened::default()
),
(5.0, 6.0)
);
assert_eq!(
display_range(&[&b], Contrast::Raw, PixelType::Uint8, Screened::default()),
(0.0, 255.0)
);
}
fn twelve_bit() -> ImageInfo {
let mut im = ImageInfo::new(0, 10, 10, PixelType::Uint16);
im.extra
.insert("component_bit_count".into(), serde_json::json!(12));
im
}
#[test]
fn fill_above_the_recorded_depth_becomes_no_data() {
let mut acc: Vec<f32> = (0..60).map(|i| 100.0 + i as f32).collect();
acc.extend([65535.0; 30]);
acc.extend([30_000.0, 9_000.0]);
acc.extend([0.0; 8]);
let (s, f) = screen_samples(&mut acc, &twelve_bit(), true);
assert_eq!(s, Screened::default());
let f = f.unwrap();
assert_eq!(f.kind, FindingKind::Fill);
assert!((f.share - 32.0).abs() < 1e-9 && (f.at_top - 93.75).abs() < 1e-9);
assert_eq!(acc.iter().filter(|v| v.is_nan()).count(), 32);
assert!(acc.iter().all(|v| v.is_nan() || *v <= 4095.0));
let mut acc2 = vec![100.0, 65535.0];
let (_, f) = screen_samples(&mut acc2, &twelve_bit(), false);
assert!(acc2[1].is_nan() && f.is_some());
}
#[test]
fn blended_edges_leave_the_contrast_only() {
let mut acc: Vec<f32> = (0..995).map(|i| 1000.0 + (i % 100) as f32).collect();
acc.extend([20_000.0, 18_000.0, 9_000.0, 5_000.0, 30_000.0]);
let before = acc.clone();
let (s, f) = screen_samples(&mut acc, &twelve_bit(), true);
assert_eq!(acc, before, "blends stay drawn");
assert_eq!(s.ceiling, Some(4095.0));
assert_eq!(f.map(|f| f.kind), Some(FindingKind::Blended));
let band = Band {
w: 1000,
h: 1,
v: acc,
};
let p = Contrast::Percentile(0.1, 99.9);
let (_, hi) = display_range(&[&band], p, PixelType::Uint16, s);
assert!(
hi <= 1099.0,
"white point {hi} from the data, not the blends"
);
let (_, hi) = display_range(&[&band], p, PixelType::Uint16, Screened::default());
assert!(hi > 4095.0, "unscreened, the blends set the white point");
let (s, f) = screen_samples(&mut before.clone(), &twelve_bit(), false);
assert_eq!((s, f), (Screened::default(), None));
}
#[test]
fn a_wrong_recorded_depth_screens_nothing() {
let mut acc: Vec<f32> = (0..100).map(|i| (i * 160) as f32).collect();
let before = acc.clone();
let (s, f) = screen_samples(&mut acc, &twelve_bit(), true);
assert_eq!((acc, s, f), (before, Screened::default(), None));
}
#[test]
fn dominant_type_maximum_is_left_out_of_auto_contrast() {
let im = ImageInfo::new(0, 10, 10, PixelType::Uint16);
let mut acc: Vec<f32> = (0..80).map(|i| 200.0 + i as f32).collect();
acc.extend([65535.0; 20]);
let (s, f) = screen_samples(&mut acc, &im, true);
assert_eq!(s.skip, Some(65535.0));
assert_eq!(f.map(|f| f.kind), Some(FindingKind::AtMaximum));
let band = Band {
w: 100,
h: 1,
v: acc.clone(),
};
let p = Contrast::Percentile(0.1, 99.9);
let (lo, hi) = display_range(&[&band], p, PixelType::Uint16, s);
assert!(lo >= 200.0 && hi < 280.0, "{lo}..{hi}");
let mut few: Vec<f32> = (0..10_000).map(|i| (i % 500) as f32).collect();
few[0] = 65535.0;
assert_eq!(screen_samples(&mut few, &im, true).1, None);
let mut all = vec![65535.0f32; 10];
assert_eq!(screen_samples(&mut all, &im, true).1, None);
let only = Band {
w: 2,
h: 1,
v: vec![65535.0, 65535.0],
};
assert_eq!(
display_range(&[&only], p, PixelType::Uint16, s),
(65535.0, 65536.0)
);
let fim = ImageInfo::new(0, 10, 10, PixelType::Float);
assert_eq!(screen_samples(&mut acc, &fim, true).1, None);
}
#[test]
fn findings_make_one_note_per_kind() {
let fill = |share, at_top| Finding {
kind: FindingKind::Fill,
share,
at_top,
};
let blended = Finding {
kind: FindingKind::Blended,
share: 0.29,
at_top: 0.0,
};
let found = [
(0, fill(13.27, 97.23)),
(1, blended),
(3, fill(13.29, 97.07)),
];
let notes = finding_notes(&twelve_bit(), &found);
assert_eq!(notes.len(), 2, "{notes:?}");
assert!(
notes[0].starts_with("c=0, c=3: 13.27-13.29% of the samples lie above the 12-bit range the file records (images[].extra.component_bit_count), 97.07-97.23% of those at 65535"),
"{}",
notes[0]
);
assert!(notes[1].starts_with("c=1: 0.29% "), "{}", notes[1]);
}
#[test]
fn level_choice_prefers_smallest_sufficient() {
let mut im = ImageInfo::new(0, 8000, 6000, PixelType::Uint8);
im.pyramid_levels = 3;
im.extra.insert(
"pyramid".into(),
serde_json::json!([
{"level": 1, "size_x": 2000, "size_y": 1500},
{"level": 2, "size_x": 500, "size_y": 375}
]),
);
assert_eq!(choose_level(&im, 1024), 1);
assert_eq!(choose_level(&im, 256), 2);
assert_eq!(choose_level(&im, 9000), 0);
let r = Region::new(4000, 3000, 2000, 1000);
assert_eq!(
choose_region_level(&im, r, 500),
(1, Region::new(1000, 750, 500, 250))
);
assert_eq!(choose_region_level(&im, r, 1024), (0, r));
im.resolution_levels = vec![
openreadout_core::ResolutionLevel::new(0, 8000, 6000, 8000, 6000),
openreadout_core::ResolutionLevel::new(1, 4000, 3000, 8000, 6000),
openreadout_core::ResolutionLevel::new(2, 2000, 1500, 8000, 6000),
];
assert_eq!(choose_level(&im, 1024), 2);
assert_eq!(level_dims(&im, 1), Some((4000, 3000)));
im.size_z = 219;
im.resolution_levels[2].size_z = Some(109);
assert_eq!(choose_level(&im, 1024), 1);
}
}