1#![forbid(unsafe_code)]
56#![warn(missing_docs)]
57
58mod assurance;
59#[doc(hidden)]
60pub mod dataset;
61#[doc(hidden)]
62pub mod ets;
63#[doc(hidden)]
64pub mod meta;
65#[doc(hidden)]
66pub mod tiff;
67#[doc(hidden)]
68pub mod tree;
69
70use std::path::Path;
71
72use openreadout_core::model::{DetectConfidence, FormatDescriptor};
73use openreadout_core::reader::{Dataset, Detection, FormatReader, has_extension};
74use openreadout_core::source::Input;
75use openreadout_core::{Error, Result};
76
77#[doc(hidden)]
78pub use dataset::VsiDataset;
79#[doc(hidden)]
80pub use ets::{ETS_MAGIC, EtsCompression, EtsFile, EtsHeader, SIS_MAGIC, Tile, looks_like_ets};
81#[doc(hidden)]
82pub use meta::{
83 ChannelMeta, DimKind, DimMeta, ObjectiveMeta, StackMeta, VsiMeta, read_meta, unit_factor,
84};
85#[doc(hidden)]
86pub use tiff::{PreviewInfo, looks_like_vsi, preview_info, preview_jpeg};
87#[doc(hidden)]
88pub use tree::{TREE_OFFSET, TagRecord, TagTree, TagValue, VOLUME_MAGIC};
89
90pub const FORMAT_ID: &str = "vsi";
92
93#[derive(Debug, Default, Clone, Copy)]
95pub struct VsiReader;
96
97impl FormatReader for VsiReader {
98 fn assurance(&self) -> Option<&'static openreadout_core::assurance::AssuranceProfile> {
99 Some(&assurance::VSI)
100 }
101
102 fn descriptor(&self) -> FormatDescriptor {
103 FormatDescriptor {
104 id: FORMAT_ID.into(),
105 name: "Olympus/Evident cellSens VSI".into(),
106 vendor: "Evident (Olympus)".into(),
107 extensions: vec!["vsi".into(), "ets".into()],
108 family: "microscopy".into(),
109 can_read: true,
110 can_write: false,
111 confidence: assurance::VSI.confidence,
112 known_gaps: vec![
113 "No specification: the .vsi record tree and ETS layout are derived from 27 corpus files (11 with pixel data: cellSens Dimension 1.18/3.2/3.x, VS200 ASW, VS120 dotSlide 2.5, Stream Essentials 1.7; ETS versions 0x00030003/5/6); names, calibration, channels, exposure, objective, camera, times and dimension kinds come from records identified by comparison with Bio-Formats; everything else is only in `vendor` under numeric tags".into(),
114 "Dimension kinds other than Z (1), T (2) and channel (4) never occurred in the corpus; one would be exposed as T with a note and a `check` warning".into(),
115 "ETS sample types other than 8-bit (2) and 16-bit (4) unsigned, and compressions other than raw (0), JPEG (2), JPEG 2000 (3) and lossless JPEG (5), are not decoded (exit 6)".into(),
116 "Unstored whole-slide tiles are filled with the ETS background value (as Bio-Formats does); JPEG tiles are decoded with jpeg-decoder and may differ by a few levels from other decoders".into(),
117 "Where the tile grid is offset from the image corner (record 2410), coarser pyramid levels place it at the nearest pixel of offset/2^L; Bio-Formats truncates, so such levels can differ from it by one pixel".into(),
118 "Planes larger than 4 GiB (full-resolution whole slides) are read by region (`--region`) or at a pyramid level (`--level`), not whole".into(),
119 "Stacks without an ETS file (focus maps, sample masks, measurement/ROI layers) and blob_*.ets focus data are listed, not decoded; a stack directory holding several frame_t*.ets files is read from the first only".into(),
120 "The excitation wavelength (record 2474) is inferred from plausibility across 9 channels (Bio-Formats reports only emission)".into(),
121 ],
122 }
123 }
124
125 fn sniff(&self, head: &[u8], path: &Path) -> Option<Detection> {
126 if tiff::looks_like_vsi(head) {
127 return Some(Detection {
128 format_id: FORMAT_ID,
129 confidence: DetectConfidence::Definite,
130 note: None,
131 });
132 }
133 if ets::looks_like_ets(head) {
134 return Some(Detection {
135 format_id: FORMAT_ID,
136 confidence: DetectConfidence::Definite,
137 note: Some("cellSens ETS tile file; open the .vsi next to its _<name>_ directory for names and calibration".into()),
138 });
139 }
140 if has_extension(path, &["vsi", "ets"]) {
141 return Some(Detection {
142 format_id: FORMAT_ID,
143 confidence: DetectConfidence::ExtensionOnly,
144 note: Some(
145 "`.vsi`/`.ets` extension, but the content does not match a cellSens header"
146 .into(),
147 ),
148 });
149 }
150 None
151 }
152
153 fn open(&self, path: &Path) -> Result<Box<dyn Dataset>> {
154 self.open_input(&Input::local(path))
155 }
156
157 fn reads_any_source(&self) -> bool {
158 true
159 }
160
161 fn open_input(&self, input: &Input) -> Result<Box<dyn Dataset>> {
162 let path = input.path();
163 if !input.fs().is_file(path) {
164 return Err(Error::io(
165 path,
166 std::io::Error::new(std::io::ErrorKind::NotFound, "not a file"),
167 ));
168 }
169 Ok(Box::new(dataset::VsiDataset::open(input)?))
170 }
171}