1use std::collections::HashMap;
13
14use openreadout_core::model::{CheckReport, FileInfo, ImageInfo, LsEntry};
15use openreadout_core::provenance::ProvenanceMap;
16use openreadout_core::reader::{Dataset, PlaneIndex};
17use openreadout_core::{Error, PixelType, Plane, Result};
18use schemars::JsonSchema;
19use serde::Deserialize;
20
21use crate::annotate::Annotation;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, JsonSchema)]
25#[serde(rename_all = "lowercase")]
26#[non_exhaustive]
27pub enum View {
28 Native,
30 Axial,
32 Coronal,
34 Sagittal,
36 Orthogonal,
38 Xz,
40 Yz,
42}
43
44impl std::str::FromStr for View {
45 type Err = Error;
46 fn from_str(s: &str) -> Result<Self> {
47 Ok(match s.trim().to_ascii_lowercase().as_str() {
48 "native" | "xy" => View::Native,
49 "axial" | "transverse" => View::Axial,
50 "coronal" => View::Coronal,
51 "sagittal" => View::Sagittal,
52 "orthogonal" | "ortho" | "mpr" => View::Orthogonal,
53 "xz" => View::Xz,
54 "yz" => View::Yz,
55 other => {
56 return Err(Error::Usage(format!(
57 "bad view '{other}': use native, axial, coronal, sagittal, orthogonal, xz or yz"
58 )));
59 }
60 })
61 }
62}
63
64impl View {
65 fn name(self) -> &'static str {
66 match self {
67 View::Native => "native",
68 View::Axial => "axial",
69 View::Coronal => "coronal",
70 View::Sagittal => "sagittal",
71 View::Orthogonal => "orthogonal",
72 View::Xz => "xz",
73 View::Yz => "yz",
74 }
75 }
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80struct Ax {
81 data: usize,
82 flip: bool,
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87struct Frame3 {
88 u: Ax,
89 v: Ax,
90 w: Ax,
91}
92
93const IDENTITY: Frame3 = Frame3 {
94 u: Ax {
95 data: 0,
96 flip: false,
97 },
98 v: Ax {
99 data: 1,
100 flip: false,
101 },
102 w: Ax {
103 data: 2,
104 flip: false,
105 },
106};
107
108fn letter(c: &str) -> Option<(usize, bool)> {
111 Some(match c {
112 "L" => (0, true),
113 "R" => (0, false),
114 "P" => (1, true),
115 "A" => (1, false),
116 "S" => (2, true),
117 "I" => (2, false),
118 _ => return None,
119 })
120}
121
122fn anatomy(im: &ImageInfo) -> Option<[(usize, bool); 3]> {
125 let o = im.extra.get("orientation")?;
126 let get = |k: &str| o.get(k).and_then(|v| v.as_str()).and_then(letter);
127 let x = get("columns_toward")?;
128 let y = get("rows_toward")?;
129 let z = match get("slices_toward") {
130 Some(z) => z,
131 None => (3 - x.0 - y.0, true),
133 };
134 let mut seen = [x.0, y.0, z.0];
135 seen.sort_unstable();
136 (seen == [0, 1, 2]).then_some([x, y, z])
137}
138
139fn anatomical_frame(view: View, an: [(usize, bool); 3]) -> Option<Frame3> {
142 let (h, vv, n) = match view {
144 View::Axial => ((0, true), (1, true), 2),
146 View::Coronal => ((0, true), (2, false), 1),
148 View::Sagittal => ((1, true), (2, false), 0),
150 _ => return None,
151 };
152 let data_of = |a: usize| an.iter().position(|(x, _)| *x == a);
153 let axis = |(a, pos_right): (usize, bool)| -> Option<Ax> {
154 let d = data_of(a)?;
155 let advances_positive = an[d].1;
157 Some(Ax {
158 data: d,
159 flip: advances_positive != pos_right,
160 })
161 };
162 let u = axis(h)?;
163 let v = axis(vv)?;
164 let w = Ax {
165 data: data_of(n)?,
166 flip: false,
167 };
168 Some(Frame3 { u, v, w })
169}
170
171fn acquisition_view(im: &ImageInfo) -> Option<View> {
173 let p = im.extra.get("orientation")?.get("plane")?.as_str()?;
174 Some(match p {
175 "axial" => View::Axial,
176 "coronal" => View::Coronal,
177 "sagittal" => View::Sagittal,
178 _ => return None,
179 })
180}
181
182pub(crate) fn effective(info: &FileInfo, im: &ImageInfo, req: Option<View>) -> Option<View> {
186 match req {
187 Some(View::Native) => None,
188 Some(v) => Some(v),
189 None if info.format.family == "medical-imaging" && im.pyramid_levels <= 1 => {
190 let v = acquisition_view(im)?;
191 let an = anatomy(im)?;
192 (anatomical_frame(v, an)? != IDENTITY).then_some(v)
194 }
195 None => None,
196 }
197}
198
199fn steps(im: &ImageInfo) -> [Option<f64>; 3] {
201 [im.physical_size.x, im.physical_size.y, im.physical_size.z]
202}
203
204fn sizes(im: &ImageInfo) -> [u32; 3] {
205 [im.size_x, im.size_y, im.size_z]
206}
207
208const MAX_VOLUME_BYTES: u64 = 2 << 30;
210
211struct Volume {
213 planes: Vec<Vec<u8>>,
214 pixel_type: PixelType,
215 spp: u32,
216}
217
218pub(crate) struct ViewDataset<'a> {
220 inner: &'a mut dyn Dataset,
221 image: u32,
222 src: ImageInfo,
223 frame: Frame3,
224 view: View,
225 cache: HashMap<(u32, u32), Volume>,
226 ortho: Option<Ortho>,
228}
229
230#[derive(Debug, Clone)]
232struct Ortho {
233 point: [u32; 3],
234 step: f64,
236 panels: Vec<(View, Frame3, u32, u32, u32)>,
238 width: u32,
239 height: u32,
240}
241
242const GAP: u32 = 8;
243
244impl<'a> ViewDataset<'a> {
245 pub(crate) fn new(
247 inner: &'a mut dyn Dataset,
248 info: &FileInfo,
249 image: u32,
250 view: View,
251 select_z: Option<u32>,
252 ) -> Result<(ViewDataset<'a>, ImageInfo, Vec<Annotation>, Vec<String>)> {
253 let src = info
254 .images
255 .iter()
256 .find(|i| i.index == image)
257 .cloned()
258 .ok_or_else(|| Error::Usage(format!("image {image} not found")))?;
259 let mut notes = Vec::new();
260 let an = anatomy(&src);
261 let frame_for = |v: View, notes: &mut Vec<String>| -> Result<Frame3> {
262 Ok(match v {
263 View::Native | View::Orthogonal => IDENTITY,
264 View::Xz => Frame3 {
265 u: Ax {
266 data: 0,
267 flip: false,
268 },
269 v: Ax {
270 data: 2,
271 flip: false,
272 },
273 w: Ax {
274 data: 1,
275 flip: false,
276 },
277 },
278 View::Yz => Frame3 {
279 u: Ax {
280 data: 1,
281 flip: false,
282 },
283 v: Ax {
284 data: 2,
285 flip: false,
286 },
287 w: Ax {
288 data: 0,
289 flip: false,
290 },
291 },
292 View::Axial | View::Coronal | View::Sagittal => {
293 if let Some(f) = an.and_then(|a| anatomical_frame(v, a)) {
294 f
295 } else {
296 {
297 notes.push(format!(
299 "the image records no patient orientation: the {} view is the stack's {} plane",
300 v.name(),
301 match v {
302 View::Axial => "xy",
303 View::Coronal => "xz",
304 _ => "yz",
305 }
306 ));
307 match v {
308 View::Axial => IDENTITY,
309 View::Coronal => Frame3 {
310 u: Ax {
311 data: 0,
312 flip: false,
313 },
314 v: Ax {
315 data: 2,
316 flip: true,
317 },
318 w: Ax {
319 data: 1,
320 flip: false,
321 },
322 },
323 _ => Frame3 {
324 u: Ax {
325 data: 1,
326 flip: false,
327 },
328 v: Ax {
329 data: 2,
330 flip: true,
331 },
332 w: Ax {
333 data: 0,
334 flip: false,
335 },
336 },
337 }
338 }
339 }
340 }
341 })
342 };
343 let n = sizes(&src);
344 let needs_stack = |f: &Frame3| f.w.data != 2;
345 let frame = frame_for(view, &mut notes)?;
346 if (needs_stack(&frame) || view == View::Orthogonal) && n[2] < 2 {
347 return Err(Error::Usage(format!(
348 "the {} view needs a volume; image {image} has a single z plane",
349 view.name()
350 )));
351 }
352 if src.samples_per_pixel > 1 && (needs_stack(&frame) || view == View::Orthogonal) {
353 return Err(Error::Usage(
354 "views across the stack are drawn for single-sample (grayscale) images".into(),
355 ));
356 }
357 let st = steps(&src);
358 let mut out = src.clone();
359 out.resolution_levels.clear();
360 out.pyramid_levels = 1;
361 let mut annotations = Vec::new();
362 let mut ortho = None;
363 if view == View::Orthogonal {
364 let s = st.iter().flatten().copied().fold(f64::INFINITY, f64::min);
366 let s = if s.is_finite() && s > 0.0 { s } else { 1.0 };
367 let ext = |d: usize| f64::from(n[d]) * st[d].unwrap_or(s);
368 let mut step = s;
369 let total = |step: f64| -> f64 {
371 [View::Axial, View::Coronal, View::Sagittal]
372 .iter()
373 .filter_map(|v| frame_for(*v, &mut Vec::new()).ok())
374 .map(|f| ext(f.u.data) / step)
375 .sum::<f64>()
376 };
377 while total(step) > 2048.0 {
378 step *= 1.25;
379 }
380 let point = [
381 n[0] / 2,
382 n[1] / 2,
383 select_z.unwrap_or(n[2] / 2).min(n[2] - 1),
384 ];
385 let mut panels = Vec::new();
386 let mut x = 0u32;
387 let mut height = 0u32;
388 for v in [View::Axial, View::Coronal, View::Sagittal] {
389 let f = frame_for(v, &mut notes)?;
390 let w = (ext(f.u.data) / step).round().max(1.0) as u32;
391 let h = (ext(f.v.data) / step).round().max(1.0) as u32;
392 panels.push((v, f, x, w, h));
393 x += w + GAP;
394 height = height.max(h);
395 }
396 let width = x - GAP;
397 out.size_x = width;
398 out.size_y = height;
399 out.size_z = 1;
400 out.physical_size.x = st[0].is_some().then_some(step);
401 out.physical_size.y = out.physical_size.x;
402 out.physical_size.z = None;
403 out.pixel_type = PixelType::Float;
404 for (v, f, x0, w, h) in &panels {
406 let y0 = f64::from((height - h) / 2);
408 annotations.push(outline(f64::from(*x0), y0, f64::from(*w), f64::from(*h)));
409 annotations.push(panel_name(
410 f64::from(*x0) + f64::from(*w) / 2.0,
411 y0 + 14.0,
412 v.name(),
413 ));
414 let pos = |a: Ax, len: u32| -> f64 {
415 let i = f64::from(point[a.data]) + 0.5;
416 let frac = i / f64::from(n[a.data]);
417 let frac = if a.flip { 1.0 - frac } else { frac };
418 frac * f64::from(len)
419 };
420 let cx = f64::from(*x0) + pos(f.u, *w);
421 let cy = y0 + pos(f.v, *h);
422 annotations.push(Annotation::segment(cx, y0, cx, y0 + f64::from(*h)));
423 annotations.push(Annotation::segment(
424 f64::from(*x0),
425 cy,
426 f64::from(*x0 + w),
427 cy,
428 ));
429 }
430 ortho = Some(Ortho {
431 point,
432 step,
433 panels,
434 width,
435 height,
436 });
437 } else {
438 out.size_x = n[frame.u.data];
439 out.size_y = n[frame.v.data];
440 out.size_z = n[frame.w.data];
441 out.physical_size.x = st[frame.u.data];
442 out.physical_size.y = st[frame.v.data];
443 out.physical_size.z = st[frame.w.data];
444 if view != View::Native && needs_stack(&frame) && st[2].is_none() {
445 notes.push("the z step is not recorded: z is drawn with the xy pixel size".into());
446 let fill = st[0].or(st[1]);
447 if frame.u.data == 2 {
448 out.physical_size.x = fill;
449 }
450 if frame.v.data == 2 {
451 out.physical_size.y = fill;
452 }
453 }
454 }
455 out.plane_count = u64::from(out.size_z) * u64::from(out.size_c) * u64::from(out.size_t);
456 out.extra
457 .insert("view".into(), serde_json::Value::from(view.name()));
458 Ok((
459 ViewDataset {
460 inner,
461 image,
462 src,
463 frame,
464 view,
465 cache: HashMap::new(),
466 ortho,
467 },
468 out,
469 annotations,
470 notes,
471 ))
472 }
473
474 fn volume(&mut self, c: u32, t: u32) -> Result<&Volume> {
475 if !self.cache.contains_key(&(c, t)) {
476 let n = sizes(&self.src);
477 let bpp = self.src.pixel_type.bytes_per_sample() as u64
478 * u64::from(self.src.samples_per_pixel.max(1));
479 let bytes = u64::from(n[0]) * u64::from(n[1]) * u64::from(n[2]) * bpp;
480 if bytes > MAX_VOLUME_BYTES {
481 return Err(Error::unsupported(
482 "preview",
483 format!("a view across a {bytes}-byte volume"),
484 "Cuts through the stack hold the whole volume in memory (2 GiB at most); preview the stored planes (view: native) or a region.",
485 ));
486 }
487 self.cache.clear();
489 let mut planes = Vec::with_capacity(n[2] as usize);
490 let mut pt = self.src.pixel_type;
491 let mut spp = self.src.samples_per_pixel.max(1);
492 for z in 0..n[2] {
493 let p = self.inner.read_plane(self.image, PlaneIndex { c, z, t })?;
494 pt = p.pixel_type;
495 spp = p.samples_per_pixel.max(1);
496 planes.push(p.data);
497 }
498 self.cache.insert(
499 (c, t),
500 Volume {
501 planes,
502 pixel_type: pt,
503 spp,
504 },
505 );
506 }
507 self.cache
508 .get(&(c, t))
509 .ok_or_else(|| Error::Other("volume cache".into()))
510 }
511
512 fn cut(&mut self, f: Frame3, k: u32, c: u32, t: u32) -> Result<Plane> {
514 let n = sizes(&self.src);
515 let (nu, nv) = (n[f.u.data], n[f.v.data]);
516 if f.w.data == 2 && f.u.data == 0 && f.v.data == 1 {
517 let p = self
519 .inner
520 .read_plane(self.image, PlaneIndex { c, z: k, t })?;
521 if !f.u.flip && !f.v.flip {
522 return Ok(p);
523 }
524 let bpp = p.pixel_type.bytes_per_sample() * p.samples_per_pixel.max(1) as usize;
525 let (w, h) = (p.width as usize, p.height as usize);
526 let mut out = vec![0u8; p.data.len()];
527 for y in 0..h {
528 let sy = if f.v.flip { h - 1 - y } else { y };
529 for x in 0..w {
530 let sx = if f.u.flip { w - 1 - x } else { x };
531 let s = (sy * w + sx) * bpp;
532 let d = (y * w + x) * bpp;
533 if let (Some(src), Some(dst)) =
534 (p.data.get(s..s + bpp), out.get_mut(d..d + bpp))
535 {
536 dst.copy_from_slice(src);
537 }
538 }
539 }
540 return Ok(Plane { data: out, ..p });
541 }
542 let vol = self.volume(c, t)?;
543 let bpp = vol.pixel_type.bytes_per_sample() * vol.spp as usize;
544 let (pt, spp) = (vol.pixel_type, vol.spp);
545 let mut out = vec![0u8; nu as usize * nv as usize * bpp];
546 let mut idx = [0u32; 3];
547 idx[f.w.data] = if f.w.flip { n[f.w.data] - 1 - k } else { k };
548 for v in 0..nv {
549 idx[f.v.data] = if f.v.flip { nv - 1 - v } else { v };
550 for u in 0..nu {
551 idx[f.u.data] = if f.u.flip { nu - 1 - u } else { u };
552 let plane = &vol.planes[idx[2] as usize];
553 let s = (idx[1] as usize * n[0] as usize + idx[0] as usize) * bpp;
554 let d = (v as usize * nu as usize + u as usize) * bpp;
555 if let (Some(src), Some(dst)) = (plane.get(s..s + bpp), out.get_mut(d..d + bpp)) {
556 dst.copy_from_slice(src);
557 }
558 }
559 }
560 Ok(Plane {
561 width: nu,
562 height: nv,
563 pixel_type: pt,
564 samples_per_pixel: spp,
565 data: out,
566 })
567 }
568
569 fn composite(&mut self, c: u32, t: u32) -> Result<Plane> {
571 let o = self
572 .ortho
573 .clone()
574 .ok_or_else(|| Error::Other("not orthogonal".into()))?;
575 let n = sizes(&self.src);
576 let mut out = vec![f32::NAN; o.width as usize * o.height as usize];
577 for (_, f, x0, w, h) in &o.panels {
578 let k = o.point[f.w.data];
579 let p = self.cut(*f, k, c, t)?;
580 let vals: Vec<f32> = p
581 .pixel_type
582 .samples_f64(&p.data)
583 .map(|v| v as f32)
584 .collect();
585 let (pw, ph) = (p.width as usize, p.height as usize);
586 let y_off = ((o.height - h) / 2) as usize;
587 for y in 0..*h as usize {
589 let fy = ((y as f64 + 0.5) / f64::from(*h) * ph as f64 - 0.5)
590 .clamp(0.0, (ph - 1) as f64);
591 let (y0, ty) = (fy.floor() as usize, (fy - fy.floor()) as f32);
592 let y1 = (y0 + 1).min(ph - 1);
593 for x in 0..*w as usize {
594 let fx = ((x as f64 + 0.5) / f64::from(*w) * pw as f64 - 0.5)
595 .clamp(0.0, (pw - 1) as f64);
596 let (x0i, tx) = (fx.floor() as usize, (fx - fx.floor()) as f32);
597 let x1 = (x0i + 1).min(pw - 1);
598 let g =
599 |yy: usize, xx: usize| vals.get(yy * pw + xx).copied().unwrap_or(f32::NAN);
600 let top = g(y0, x0i) * (1.0 - tx) + g(y0, x1) * tx;
601 let bottom = g(y1, x0i) * (1.0 - tx) + g(y1, x1) * tx;
602 out[(y + y_off) * o.width as usize + *x0 as usize + x] =
603 top * (1.0 - ty) + bottom * ty;
604 }
605 }
606 }
607 let _ = n;
608 Ok(Plane {
609 width: o.width,
610 height: o.height,
611 pixel_type: PixelType::Float,
612 samples_per_pixel: 1,
613 data: out.iter().flat_map(|v| v.to_le_bytes()).collect(),
614 })
615 }
616
617 pub(crate) fn view(&self) -> View {
619 self.view
620 }
621
622 pub(crate) fn point(&self) -> Option<[u32; 3]> {
624 self.ortho.as_ref().map(|o| o.point)
625 }
626
627 pub(crate) fn ortho_step(&self) -> Option<f64> {
629 self.ortho.as_ref().map(|o| o.step)
630 }
631}
632
633impl Dataset for ViewDataset<'_> {
634 fn info(&self) -> Result<FileInfo> {
635 self.inner.info()
636 }
637 fn vendor_metadata(&self) -> Result<serde_json::Value> {
638 self.inner.vendor_metadata()
639 }
640 fn provenance(&self) -> ProvenanceMap {
641 self.inner.provenance()
642 }
643 fn entries(&self) -> Result<Vec<LsEntry>> {
644 self.inner.entries()
645 }
646 fn read_plane(&mut self, image: u32, index: PlaneIndex) -> Result<Plane> {
647 if image != self.image {
648 return self.inner.read_plane(image, index);
649 }
650 if self.ortho.is_some() {
651 return self.composite(index.c, index.t);
652 }
653 let f = self.frame;
654 self.cut(f, index.z, index.c, index.t)
655 }
656 fn check(&mut self) -> Result<CheckReport> {
657 self.inner.check()
658 }
659}
660
661fn panel_name(x: f64, y: f64, label: &str) -> Annotation {
663 Annotation {
664 color: Some("white".into()),
665 ..Annotation::text(x, y, label)
666 }
667}
668
669fn outline(x: f64, y: f64, w: f64, h: f64) -> Annotation {
671 Annotation {
672 color: Some("muted".into()),
673 ..Annotation::rect(x, y, w, h)
674 }
675}
676
677#[cfg(test)]
678mod tests {
679 use super::*;
680
681 fn letters(c: &str, r: &str, s: &str) -> [(usize, bool); 3] {
682 [letter(c).unwrap(), letter(r).unwrap(), letter(s).unwrap()]
683 }
684
685 #[test]
686 fn dicom_axial_needs_no_change_and_nifti_ras_is_flipped() {
687 let f = anatomical_frame(View::Axial, letters("L", "P", "S")).unwrap();
689 assert_eq!(f, IDENTITY);
690 let f = anatomical_frame(View::Axial, letters("R", "A", "S")).unwrap();
692 assert!(f.u.flip && f.v.flip && f.w.data == 2);
693 let f = anatomical_frame(View::Coronal, letters("L", "P", "S")).unwrap();
695 assert_eq!((f.u.data, f.v.data, f.v.flip, f.w.data), (0, 2, true, 1));
696 let f = anatomical_frame(View::Sagittal, letters("L", "P", "S")).unwrap();
698 assert_eq!((f.u.data, f.u.flip, f.v.data, f.w.data), (1, false, 2, 0));
699 }
700}