Skip to main content

openreadout_ometiff/
lib.rs

1//! OME-TIFF writer: `BigTIFF` container (via the `tiff` crate) plus an OME-XML 2016-06
2//! document in the first IFD's `ImageDescription`. Every export is written to a temporary
3//! file, read back plane by plane and hash-compared to the source before it is renamed
4//! into place. The source file is never opened for writing.
5//!
6//! OME-TIFF is the open microscopy exchange format: an ordinary (here: Big)TIFF file with one
7//! page per plane and the full acquisition metadata as OME-XML, readable by Fiji, napari,
8//! QuPath, Bio-Formats and most image software.
9//!
10//! # Example
11//!
12//! ```
13//! use std::path::Path;
14//!
15//! use openreadout_core::{Dataset, Result};
16//! use openreadout_ometiff::{Codec, ExportOptions, export_ome_tiff};
17//!
18//! /// Write channel 0 of every image of an opened file as LZW-compressed OME-TIFF.
19//! fn channel0_to_ome_tiff(dataset: &mut dyn Dataset, input: &Path) -> Result<()> {
20//!     let mut options = ExportOptions::default();
21//!     options.select = vec!["c=0".into()];
22//!     options.codec = Codec::Lzw;
23//!     let report = export_ome_tiff(dataset, input, Path::new("channel0.ome.tiff"), &options)?;
24//!     assert!(report.verified); // every plane was read back and hash-compared
25//!     Ok(())
26//! }
27//! ```
28#![forbid(unsafe_code)]
29#![warn(missing_docs)]
30
31pub mod omexml;
32pub mod pyramid;
33mod tiled;
34mod tiled_export;
35
36use std::fs::File;
37use std::io::BufWriter;
38use std::path::Path;
39
40use openreadout_core::parallel::{PlaneRequest, ReadContext, read_in_order};
41use openreadout_core::reader::PlaneIndex;
42use openreadout_core::select::Selection;
43use openreadout_core::{Dataset, Error, PixelType, Plane, Result};
44use rayon::prelude::*;
45use schemars::JsonSchema;
46use serde::{Deserialize, Serialize};
47use tiff::encoder::{TiffEncoder, TiffKindBig};
48use tiff::tags::Tag;
49
50pub use omexml::{WrittenImage, build_ome_xml, build_ome_xml_metadata_only};
51pub use pyramid::PyramidMode;
52pub use tiled_export::WrittenLevels;
53
54/// Compression codec for the TIFF strips.
55#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
56#[serde(rename_all = "lowercase")]
57#[non_exhaustive]
58pub enum Codec {
59    /// No compression.
60    None,
61    /// Deflate (zlib), lossless; the default.
62    Deflate,
63    /// LZW, lossless; readable by older TIFF software.
64    Lzw,
65}
66
67/// Export options.
68#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
69#[non_exhaustive]
70pub struct ExportOptions {
71    /// Only this image index (default: all).
72    pub image: Option<u32>,
73    /// Plane selection strings (`c=0`, `z=1-3`, `t=0,2`).
74    pub select: Vec<String>,
75    /// Compression of the pixel data.
76    pub codec: Codec,
77    /// Replace an existing output file.
78    pub overwrite: bool,
79    /// Embed the vendor metadata tree (as JSON) in a `StructuredAnnotation`.
80    pub embed_vendor: bool,
81    /// Export this source pyramid level as the full resolution (0 = the source's full
82    /// resolution).
83    pub level: u32,
84    /// Export only this rectangle of every plane, in the pixel coordinates of `level`.
85    pub region: Option<openreadout_core::Region>,
86    /// Reduced-resolution levels (`SubIFDs`): `Auto` copies the source's own levels when the
87    /// whole image is exported from level 0 and the source has a pyramid; else none.
88    pub pyramid: PyramidMode,
89    /// Levels including full resolution: exactly (mean pyramids) or at most (source pyramids).
90    pub levels: Option<u32>,
91    /// Tile edge of tiled output in pixels (a multiple of 16; default 512). Tiled output is
92    /// written for pyramids, regions, levels and planes above 4 GiB.
93    pub tile: u32,
94}
95
96impl Default for ExportOptions {
97    /// Every image and plane, deflate compression, no overwrite, no vendor tree, full
98    /// resolution, the source's own pyramid levels when it has them.
99    fn default() -> Self {
100        ExportOptions {
101            image: None,
102            select: Vec::new(),
103            codec: Codec::Deflate,
104            overwrite: false,
105            embed_vendor: false,
106            level: 0,
107            region: None,
108            pyramid: PyramidMode::Auto,
109            levels: None,
110            tile: DEFAULT_TILE,
111        }
112    }
113}
114
115/// Default tile edge of tiled OME-TIFF output.
116pub const DEFAULT_TILE: u32 = 512;
117
118/// What `export` reports.
119#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
120pub struct ExportReport {
121    /// The file that was read.
122    pub input: String,
123    /// The OME-TIFF (or OME-Zarr) written.
124    pub output: String,
125    /// `ome-tiff` or `ome-zarr`.
126    pub format: String,
127    /// Images written.
128    pub images_written: u32,
129    /// Planes written, over all images.
130    pub planes_written: u64,
131    /// Size of the output in bytes.
132    pub bytes_written: u64,
133    /// True when every written plane was read back and hashed equal to the source plane.
134    pub verified: bool,
135    /// Compression used.
136    pub codec: Codec,
137    /// Size of the embedded OME-XML in bytes.
138    pub ome_xml_bytes: u64,
139    /// Per image, the resolution levels written (tiled exports: pyramids, regions, levels);
140    /// absent for single-resolution exports of whole planes.
141    #[serde(default, skip_serializing_if = "Vec::is_empty")]
142    pub resolutions: Vec<WrittenLevels>,
143    /// OME-Zarr: `plate` when the store is an OME-NGFF high-content screening plate (row,
144    /// column and field groups); absent for single images and collections.
145    #[serde(default, skip_serializing_if = "Option::is_none")]
146    pub layout: Option<String>,
147    /// Images left out: plane files they need are missing (`--skip-incomplete`), or the reader
148    /// does not decode their pixel data (raw FLIM photon histograms; `info` notes say which).
149    #[serde(default, skip_serializing_if = "Vec::is_empty")]
150    pub images_skipped: Vec<u32>,
151}
152
153pub(crate) const CREATOR: &str = concat!("openreadout ", env!("CARGO_PKG_VERSION"));
154
155fn tiff_err(e: tiff::TiffError, path: &Path) -> Error {
156    Error::Other(format!("TIFF write error on {}: {e}", path.display()))
157}
158
159/// TIFF description of a plane's samples.
160#[derive(Debug, Clone, Copy)]
161pub(crate) struct SampleLayout {
162    bits: u16,
163    /// TIFF `SampleFormat`: 1 unsigned integer, 2 signed integer, 3 IEEE floating point.
164    format: u16,
165    /// TIFF `PhotometricInterpretation`: 1 black is zero, 2 RGB.
166    photometric: u16,
167}
168
169fn sample_layout(plane: &Plane) -> Result<SampleLayout> {
170    layout_of(plane.pixel_type, plane.samples_per_pixel)
171}
172
173/// The TIFF sample description of `samples_per_pixel` samples of `pixel_type`.
174pub(crate) fn layout_of(pixel_type: PixelType, samples_per_pixel: u32) -> Result<SampleLayout> {
175    let (bits, format) = match (pixel_type, samples_per_pixel) {
176        (PixelType::Uint8, 1 | 3) => (8, 1),
177        (PixelType::Uint16, 1 | 3) => (16, 1),
178        (PixelType::Uint32, 1) => (32, 1),
179        (PixelType::Int8, 1) => (8, 2),
180        (PixelType::Int16, 1) => (16, 2),
181        (PixelType::Int32, 1) => (32, 2),
182        (PixelType::Float, 1 | 3) => (32, 3),
183        (PixelType::Double, 1) => (64, 3),
184        (pt, spp) => {
185            // OME-Zarr takes any sample count of these types, but no 64-bit integer or
186            // complex samples either.
187            let zarr_takes_it = matches!(
188                pt,
189                PixelType::Uint8
190                    | PixelType::Uint16
191                    | PixelType::Uint32
192                    | PixelType::Int8
193                    | PixelType::Int16
194                    | PixelType::Int32
195                    | PixelType::Float
196                    | PixelType::Double
197            );
198            let hint = if zarr_takes_it {
199                "OME-TIFF export supports 1-sample int8/16/32, uint8/16/32, float and double planes and 3-sample uint8/16/float planes. Export with `--format ome-zarr` instead: it writes each sample as a channel."
200            } else {
201                "Neither OME-TIFF nor OME-Zarr export writes 64-bit integer or complex samples. `openreadout planes FILE --dump-dir DIR` writes each plane's raw little-endian samples instead."
202            };
203            return Err(Error::unsupported(
204                "ome-tiff",
205                format!("{} samples of {} per pixel", spp, pt.ome_name()),
206                hint,
207            ));
208        }
209    };
210    Ok(SampleLayout {
211        bits,
212        format,
213        photometric: if samples_per_pixel == 3 { 2 } else { 1 },
214    })
215}
216
217/// A plane made ready for the sequential TIFF writer on a worker thread: samples in the file's
218/// byte order and, for deflate and LZW, every strip already compressed. Compressing here rather
219/// than in the writer is what lets `--threads` speed up compressed exports.
220struct EncodedPlane {
221    width: u32,
222    height: u32,
223    samples_per_pixel: u16,
224    layout: SampleLayout,
225    rows_per_strip: u32,
226    strips: Strips,
227}
228
229enum Strips {
230    /// Uncompressed: the plane's bytes, cut into strips of `strip_bytes` when written.
231    Raw { data: Vec<u8>, strip_bytes: usize },
232    /// One compressed buffer per strip.
233    Compressed(Vec<Vec<u8>>),
234}
235
236/// TIFF `Compression` tag value.
237pub(crate) fn compression_tag(codec: Codec) -> u16 {
238    match codec {
239        Codec::None => 1,
240        Codec::Lzw => 5,
241        Codec::Deflate => 8,
242    }
243}
244
245/// Strips of at most 1 MiB of whole rows (at least one row).
246fn rows_per_strip(plane: &Plane) -> u32 {
247    let row_bytes = plane.row_bytes().max(1);
248    ((1usize << 20) / row_bytes).clamp(1, plane.height.max(1) as usize) as u32
249}
250
251fn encode_plane(plane: Plane, codec: Codec) -> Result<EncodedPlane> {
252    let layout = sample_layout(&plane)?;
253    let rows = rows_per_strip(&plane);
254    let strip_bytes = plane.row_bytes().max(1) * rows as usize;
255    let sample_bytes = plane.pixel_type.bytes_per_sample();
256    let (width, height, spp) = (plane.width, plane.height, plane.samples_per_pixel);
257    let mut data = plane.data;
258    // Plane samples are little-endian; the `tiff` encoder writes the host's byte order.
259    if cfg!(target_endian = "big") && sample_bytes > 1 {
260        for s in data.chunks_mut(sample_bytes) {
261            s.reverse();
262        }
263    }
264    let strips = match codec {
265        Codec::None => Strips::Raw { data, strip_bytes },
266        Codec::Deflate => Strips::Compressed(
267            data.chunks(strip_bytes)
268                .map(|s| {
269                    use std::io::Write;
270                    // Same codec and level as the `tiff` crate's default deflate.
271                    let mut z = flate2::write::ZlibEncoder::new(
272                        Vec::with_capacity(s.len() / 2),
273                        flate2::Compression::new(6),
274                    );
275                    z.write_all(s)?;
276                    z.finish()
277                })
278                .collect::<std::io::Result<_>>()
279                .map_err(|e| Error::Other(format!("deflate compression failed: {e}")))?,
280        ),
281        Codec::Lzw => Strips::Compressed(
282            data.chunks(strip_bytes)
283                .map(|s| {
284                    weezl::encode::Encoder::with_tiff_size_switch(weezl::BitOrder::Msb, 8).encode(s)
285                })
286                .collect::<std::result::Result<_, _>>()
287                .map_err(|e| Error::Other(format!("LZW compression failed: {e}")))?,
288        ),
289    };
290    Ok(EncodedPlane {
291        width,
292        height,
293        samples_per_pixel: spp as u16,
294        layout,
295        rows_per_strip: rows,
296        strips,
297    })
298}
299
300/// Write one encoded plane as the next IFD. `description` is attached when `Some`. Tags are
301/// written in the order the `tiff` crate's `ImageEncoder` uses, so the file layout is the same
302/// as before strips were compressed on worker threads.
303fn write_plane<W: std::io::Write + std::io::Seek>(
304    enc: &mut TiffEncoder<W, TiffKindBig>,
305    plane: &EncodedPlane,
306    codec: Codec,
307    description: Option<&str>,
308    out_path: &Path,
309) -> Result<()> {
310    use tiff::encoder::Rational;
311    use tiff::tags::ResolutionUnit;
312    let te = |e| tiff_err(e, out_path);
313    if plane.width == 0 || plane.height == 0 {
314        return Err(Error::Other(format!(
315            "cannot write a {}x{} plane to TIFF",
316            plane.width, plane.height
317        )));
318    }
319    let spp = usize::from(plane.samples_per_pixel);
320    let mut dir = enc.image_directory().map_err(te)?;
321    dir.write_tag(Tag::ImageWidth, plane.width).map_err(te)?;
322    dir.write_tag(Tag::ImageLength, plane.height).map_err(te)?;
323    dir.write_tag(Tag::Compression, compression_tag(codec))
324        .map_err(te)?;
325    dir.write_tag(Tag::Predictor, 1u16).map_err(te)?;
326    dir.write_tag(Tag::PhotometricInterpretation, plane.layout.photometric)
327        .map_err(te)?;
328    dir.write_tag(Tag::RowsPerStrip, plane.rows_per_strip)
329        .map_err(te)?;
330    dir.write_tag(Tag::SamplesPerPixel, plane.samples_per_pixel)
331        .map_err(te)?;
332    dir.write_tag(Tag::XResolution, Rational { n: 1, d: 1 })
333        .map_err(te)?;
334    dir.write_tag(Tag::YResolution, Rational { n: 1, d: 1 })
335        .map_err(te)?;
336    dir.write_tag(Tag::ResolutionUnit, ResolutionUnit::None)
337        .map_err(te)?;
338    dir.write_tag(Tag::Software, CREATOR).map_err(te)?;
339    if let Some(d) = description {
340        dir.write_tag(Tag::ImageDescription, d).map_err(te)?;
341    }
342    let mut offsets: Vec<u64> = Vec::new();
343    let mut counts: Vec<u64> = Vec::new();
344    let mut put = |s: &[u8]| -> Result<()> {
345        offsets.push(dir.write_data(s).map_err(te)?);
346        counts.push(s.len() as u64);
347        Ok(())
348    };
349    match &plane.strips {
350        Strips::Raw { data, strip_bytes } => data.chunks(*strip_bytes).try_for_each(&mut put)?,
351        Strips::Compressed(strips) => strips.iter().map(Vec::as_slice).try_for_each(&mut put)?,
352    }
353    dir.write_tag(Tag::BitsPerSample, &vec![plane.layout.bits; spp][..])
354        .map_err(te)?;
355    dir.write_tag(Tag::SampleFormat, &vec![plane.layout.format; spp][..])
356        .map_err(te)?;
357    dir.write_tag(Tag::StripOffsets, &offsets[..]).map_err(te)?;
358    dir.write_tag(Tag::StripByteCounts, &counts[..])
359        .map_err(te)?;
360    dir.finish().map_err(te)
361}
362
363/// Read every IFD of the written file back and return the xxh3 of each plane's samples
364/// (little-endian bytes, as [`Plane::xxh3_hex`] hashes them). `expected` is the number of
365/// planes written. The IFDs are split into contiguous ranges read by parallel workers (the
366/// `--threads` pool), each with its own decoder; the last range keeps reading past `expected`
367/// if the file has more IFDs, so a surplus shows up as a count mismatch.
368fn read_back_hashes(path: &Path, expected: usize, plane_bytes: u64) -> Result<Vec<String>> {
369    let expected = expected.max(1);
370    // As many workers as planes may be in flight during decoding (threads, memory-bounded).
371    let workers = openreadout_core::parallel::window(plane_bytes).clamp(1, expected);
372    let per = expected.div_ceil(workers);
373    let ranges: Vec<(usize, usize)> = (0..expected)
374        .step_by(per)
375        .map(|s| (s, (s + per).min(expected)))
376        .collect();
377    let last = ranges.len() - 1;
378    let parts: Vec<Result<Vec<String>>> = ranges
379        .par_iter()
380        .enumerate()
381        .map(|(i, &(start, end))| read_back_range(path, start, end, i == last))
382        .collect();
383    let mut out = Vec::with_capacity(expected);
384    for p in parts {
385        out.extend(p?);
386    }
387    Ok(out)
388}
389
390fn read_back_range(path: &Path, start: usize, end: usize, to_last: bool) -> Result<Vec<String>> {
391    use tiff::decoder::{Decoder, DecodingResult};
392    let tiff_err =
393        |e: tiff::TiffError| Error::Other(format!("read-back of {} failed: {e}", path.display()));
394    let f = File::open(path).map_err(|e| Error::io(path, e))?;
395    // We wrote these planes ourselves from planes already held in memory, so the decoder's
396    // default 256 MiB buffer limit (hit by whole-slide RGB scans) protects nothing here.
397    let mut dec = Decoder::new(std::io::BufReader::new(f))
398        .map_err(tiff_err)?
399        .with_limits(tiff::decoder::Limits::unlimited());
400    if start > 0 {
401        dec.seek_to_image(start).map_err(tiff_err)?;
402    }
403    let mut out = Vec::with_capacity(end - start);
404    let mut i = start;
405    loop {
406        // A fresh buffer per plane: `read_image_to_buffer` allocates the new one before it
407        // drops what the buffer held, so reusing it would keep two planes alive at once.
408        let mut buf = DecodingResult::U8(Vec::new());
409        dec.read_image_to_buffer(&mut buf).map_err(tiff_err)?;
410        out.push(format!("{:032x}", hash_le(&mut buf)));
411        i += 1;
412        if (i >= end && !to_last) || !dec.more_images() {
413            break;
414        }
415        dec.next_image().map_err(tiff_err)?;
416    }
417    Ok(out)
418}
419
420/// xxh3-128 of a decoded buffer's samples in little-endian byte order, without first copying
421/// the plane into a byte vector.
422fn hash_le(buf: &mut tiff::decoder::DecodingResult) -> u128 {
423    use tiff::decoder::DecodingResult as D;
424    let width = match buf {
425        D::U8(_) | D::I8(_) => 1,
426        D::U16(_) | D::I16(_) | D::F16(_) => 2,
427        D::U32(_) | D::I32(_) | D::F32(_) => 4,
428        D::U64(_) | D::I64(_) | D::F64(_) => 8,
429    };
430    let view = buf.as_buffer(0);
431    let bytes = view.as_bytes();
432    if cfg!(target_endian = "little") || width == 1 {
433        return xxhash_rust::xxh3::xxh3_128(bytes);
434    }
435    let mut h = xxhash_rust::xxh3::Xxh3::new();
436    let mut tmp = Vec::with_capacity(1 << 16);
437    for chunk in bytes.chunks(1 << 16) {
438        tmp.clear();
439        tmp.extend_from_slice(chunk);
440        for s in tmp.chunks_mut(width) {
441            s.reverse();
442        }
443        h.update(&tmp);
444    }
445    h.digest128()
446}
447
448/// The error for an image export of a file that holds no images (a VSI without its folder of
449/// `.ets` pixel files, for example). `writer` names the export, as in [`Error::unsupported`].
450/// The file's first note usually says why, so it goes in the message.
451pub fn no_images_error(info: &openreadout_core::model::FileInfo, writer: &'static str) -> Error {
452    let why = info
453        .notes
454        .first()
455        .map_or_else(String::new, |n| format!(" ({n})"));
456    Error::unsupported(
457        writer,
458        format!("a file with no images{why}"),
459        "The file holds metadata but no image planes, so there is nothing to export. `openreadout info FILE` shows what it does hold.",
460    )
461}
462
463/// Export `ds` (already opened from `input`) to an OME-TIFF at `output`.
464pub fn export_ome_tiff(
465    ds: &mut dyn Dataset,
466    input: &Path,
467    output: &Path,
468    opts: &ExportOptions,
469) -> Result<ExportReport> {
470    export_ome_tiff_with(ds, input, output, opts, &ReadContext::default())
471}
472
473/// [`export_ome_tiff`] with parallel plane decoding and progress reporting: planes are
474/// decoded by worker threads (extra handles from `ctx.opener`) and written sequentially in
475/// the same order, so the file is byte-identical whatever the number of threads.
476pub fn export_ome_tiff_with(
477    ds: &mut dyn Dataset,
478    input: &Path,
479    output: &Path,
480    opts: &ExportOptions,
481    ctx: &ReadContext<'_>,
482) -> Result<ExportReport> {
483    openreadout_core::output::refuse_dataset(output, input, &*ds)?;
484    if output.exists() && !opts.overwrite {
485        return Err(Error::Usage(format!(
486            "{} exists; pass --overwrite to replace it",
487            output.display()
488        )));
489    }
490    let info = ds.info()?;
491    if info.images.is_empty() {
492        return Err(no_images_error(&info, "ome-tiff"));
493    }
494    let sel = Selection::parse(&opts.select)?;
495    let images_skipped = refused_images(ds, &info, opts.image);
496    let mut written: Vec<WrittenImage<'_>> = Vec::new();
497    let mut next_ifd = 0u32;
498    for im in &info.images {
499        if opts.image.is_some_and(|i| i != im.index) || images_skipped.contains(&im.index) {
500            continue;
501        }
502        // OME-Zarr label images (`extra.label`) have no OME-TIFF counterpart: written only
503        // when asked for by `--image`.
504        if opts.image.is_none()
505            && im.extra.get("label").and_then(serde_json::Value::as_bool) == Some(true)
506        {
507            continue;
508        }
509        // Per-plane lists below are sized by the declared dimensions; refuse absurd ones.
510        openreadout_core::limits::checked_plane_count("ome-tiff", im.size_c, im.size_z, im.size_t)?;
511        let c_map: Vec<u32> = (0..im.size_c)
512            .filter(|&c| sel.c.is_empty() || sel.c.contains(&c))
513            .collect();
514        let z_map: Vec<u32> = (0..im.size_z)
515            .filter(|&z| sel.z.is_empty() || sel.z.contains(&z))
516            .collect();
517        let t_map: Vec<u32> = (0..im.size_t)
518            .filter(|&t| sel.t.is_empty() || sel.t.contains(&t))
519            .collect();
520        if c_map.is_empty() || z_map.is_empty() || t_map.is_empty() {
521            continue;
522        }
523        let mut planes = Vec::new();
524        for &t in &t_map {
525            for &z in &z_map {
526                for &c in &c_map {
527                    planes.push((c, z, t));
528                }
529            }
530        }
531        let n = planes.len() as u32;
532        // Per-frame records feed the `Plane` elements; they are metadata, so a reader that
533        // cannot produce them does not stop the export.
534        let frames = ds
535            .frames(im.index, None)
536            .map(|(_, r)| r)
537            .unwrap_or_default();
538        written.push(WrittenImage {
539            info: im,
540            first_ifd: next_ifd,
541            planes,
542            size_c: c_map.len() as u32,
543            size_z: z_map.len() as u32,
544            size_t: t_map.len() as u32,
545            c_map,
546            source_planes: Vec::new(),
547            frames,
548        });
549        next_ifd += n;
550    }
551    if written.is_empty() {
552        return Err(Error::Usage("selection matches no planes".into()));
553    }
554    if written
555        .iter()
556        .any(|w| tiled_export::needs_tiled(w.info, opts))
557    {
558        return export_tiled(ds, &info, &written, input, output, opts, ctx).map(|mut r| {
559            r.images_skipped = images_skipped;
560            r
561        });
562    }
563    // Fail before writing anything when a plane could not be held in memory.
564    let mut max_plane = 0u64;
565    for w in &written {
566        let n = openreadout_core::pixel::plane_bytes_checked(
567            "ome-tiff",
568            w.info.size_x,
569            w.info.size_y,
570            w.info.samples_per_pixel.max(1) as usize * w.info.pixel_type.bytes_per_sample(),
571        )?;
572        max_plane = max_plane.max(n as u64);
573    }
574    // Re-index the selected planes to 0..n for the OME-XML (TiffData FirstC/Z/T are in the exported image's own index space).
575    let xml_images: Vec<WrittenImage<'_>> = written
576        .iter()
577        .map(|w| {
578            let mut w2 = w.clone();
579            w2.source_planes.clone_from(&w.planes);
580            let cpos = |c: u32| w.c_map.iter().position(|&x| x == c).unwrap_or(0) as u32;
581            let zs: Vec<u32> = {
582                let mut v: Vec<u32> = w.planes.iter().map(|p| p.1).collect();
583                v.sort_unstable();
584                v.dedup();
585                v
586            };
587            let ts: Vec<u32> = {
588                let mut v: Vec<u32> = w.planes.iter().map(|p| p.2).collect();
589                v.sort_unstable();
590                v.dedup();
591                v
592            };
593            w2.planes = w
594                .planes
595                .iter()
596                .map(|&(c, z, t)| {
597                    (
598                        cpos(c),
599                        zs.iter().position(|&x| x == z).unwrap_or(0) as u32,
600                        ts.iter().position(|&x| x == t).unwrap_or(0) as u32,
601                    )
602                })
603                .collect();
604            w2
605        })
606        .collect();
607    let vendor_json = if opts.embed_vendor {
608        ds.vendor_metadata()
609            .ok()
610            .filter(|v| !v.is_null())
611            .and_then(|v| serde_json::to_string(&v).ok())
612            .filter(|s| s.len() < 8 << 20)
613    } else {
614        None
615    };
616    let xml =
617        build_ome_xml(&info, &xml_images, CREATOR, vendor_json.as_deref()).map_err(Error::Other)?;
618
619    let tmp = output.with_file_name(format!(
620        ".{}.partial-{}",
621        output
622            .file_name()
623            .map_or_else(|| "export".into(), |s| s.to_string_lossy().to_string()),
624        std::process::id()
625    ));
626    let result = (|| -> Result<(u64, Vec<String>)> {
627        let f = File::create(&tmp).map_err(|e| Error::io(&tmp, e))?;
628        let mut enc = TiffEncoder::new_big(BufWriter::new(f)).map_err(|e| tiff_err(e, &tmp))?;
629        let mut hashes = Vec::new();
630        let mut first = true;
631        let mut count = 0u64;
632        let mut requests = Vec::new();
633        let mut plane_bytes = 0u64;
634        for w in &written {
635            plane_bytes = plane_bytes.max(
636                u64::from(w.info.size_x)
637                    * u64::from(w.info.size_y)
638                    * u64::from(w.info.samples_per_pixel.max(1))
639                    * w.info.pixel_type.bytes_per_sample() as u64,
640            );
641            for &(c, z, t) in &w.planes {
642                requests.push(PlaneRequest {
643                    image: w.info.index,
644                    index: PlaneIndex { c, z, t },
645                    level: 0,
646                    region: None,
647                });
648            }
649        }
650        let total = requests.len() as u64;
651        let format_id: &'static str = Box::leak(info.format.id.clone().into_boxed_str());
652        read_in_order(
653            ds,
654            ctx,
655            &requests,
656            plane_bytes,
657            &|r, plane| {
658                if plane.data.len() != plane.expected_len() {
659                    let PlaneIndex { c, z, t } = r.index;
660                    return Err(Error::corrupt(
661                        format_id,
662                        format!(
663                            "plane c={c} z={z} t={t} has {} bytes, expected {}",
664                            plane.data.len(),
665                            plane.expected_len()
666                        ),
667                    ));
668                }
669                let h = plane.xxh3_hex();
670                Ok((encode_plane(plane, opts.codec)?, h))
671            },
672            &mut |_, (plane, h)| {
673                hashes.push(h);
674                write_plane(
675                    &mut enc,
676                    &plane,
677                    opts.codec,
678                    first.then_some(xml.as_str()),
679                    &tmp,
680                )?;
681                first = false;
682                count += 1;
683                ctx.report(count, total);
684                Ok(())
685            },
686        )?;
687        drop(enc);
688        Ok((count, hashes))
689    })();
690    let (planes_written, source_hashes) = match result {
691        Ok(v) => v,
692        Err(e) => {
693            let _ = std::fs::remove_file(&tmp);
694            return Err(e);
695        }
696    };
697    let back = match read_back_hashes(&tmp, source_hashes.len(), max_plane) {
698        Ok(h) => h,
699        Err(e) => {
700            if std::env::var_os("OPENREADOUT_KEEP_PARTIAL").is_none() {
701                let _ = std::fs::remove_file(&tmp);
702            }
703            return Err(e);
704        }
705    };
706    let verified = back == source_hashes;
707    if !verified {
708        let _ = std::fs::remove_file(&tmp);
709        return Err(Error::Other(format!(
710            "read-back verification failed: {} of {} planes differ after writing; output discarded",
711            back.iter()
712                .zip(&source_hashes)
713                .filter(|(a, b)| a != b)
714                .count()
715                + back.len().abs_diff(source_hashes.len()),
716            source_hashes.len()
717        )));
718    }
719    let bytes_written = std::fs::metadata(&tmp).map_or(0, |m| m.len());
720    if output.exists() {
721        std::fs::remove_file(output).map_err(|e| Error::io(output, e))?;
722    }
723    std::fs::rename(&tmp, output).map_err(|e| Error::io(output, e))?;
724    Ok(ExportReport {
725        input: input.display().to_string(),
726        output: output.display().to_string(),
727        format: "ome-tiff".into(),
728        images_written: written.len() as u32,
729        planes_written,
730        bytes_written,
731        verified,
732        codec: opts.codec,
733        ome_xml_bytes: xml.len() as u64,
734        resolutions: Vec::new(),
735        layout: None,
736        images_skipped,
737    })
738}
739
740/// Without `--image`, the images whose planes the reader refuses
741/// ([`Dataset::pixels_refused`]): they are left out of the export and listed. When that would
742/// leave nothing, none are left out, so the reader's error says why.
743pub fn refused_images(
744    ds: &dyn Dataset,
745    info: &openreadout_core::FileInfo,
746    image: Option<u32>,
747) -> Vec<u32> {
748    if image.is_some() {
749        return Vec::new();
750    }
751    let refused: Vec<u32> = info
752        .images
753        .iter()
754        .filter(|im| ds.pixels_refused(im.index).is_some())
755        .map(|im| im.index)
756        .collect();
757    if refused.len() == info.images.len() {
758        Vec::new()
759    } else {
760        refused
761    }
762}
763
764/// The tiled path of [`export_ome_tiff_with`]: temporary file, stream, verify, rename.
765fn export_tiled(
766    ds: &mut dyn Dataset,
767    info: &openreadout_core::FileInfo,
768    written: &[WrittenImage<'_>],
769    input: &Path,
770    output: &Path,
771    opts: &ExportOptions,
772    ctx: &ReadContext<'_>,
773) -> Result<ExportReport> {
774    let vendor_json = if opts.embed_vendor {
775        ds.vendor_metadata()
776            .ok()
777            .filter(|v| !v.is_null())
778            .and_then(|v| serde_json::to_string(&v).ok())
779            .filter(|s| s.len() < 8 << 20)
780    } else {
781        None
782    };
783    let tmp = output.with_file_name(format!(
784        ".{}.partial-{}",
785        output
786            .file_name()
787            .map_or_else(|| "export".into(), |s| s.to_string_lossy().to_string()),
788        std::process::id()
789    ));
790    let res = tiled_export::export(ds, info, written, &tmp, opts, ctx, vendor_json.as_deref());
791    let (planes_written, bytes_written, resolutions) = match res {
792        Ok(v) => v,
793        Err(e) => {
794            if std::env::var_os("OPENREADOUT_KEEP_PARTIAL").is_none() {
795                let _ = std::fs::remove_file(&tmp);
796            }
797            return Err(e);
798        }
799    };
800    if output.exists() {
801        std::fs::remove_file(output).map_err(|e| Error::io(output, e))?;
802    }
803    std::fs::rename(&tmp, output).map_err(|e| Error::io(output, e))?;
804    let xml_bytes = read_description_len(output);
805    Ok(ExportReport {
806        input: input.display().to_string(),
807        output: output.display().to_string(),
808        format: "ome-tiff".into(),
809        images_written: written.len() as u32,
810        planes_written,
811        bytes_written,
812        verified: true,
813        codec: opts.codec,
814        ome_xml_bytes: xml_bytes,
815        resolutions,
816        layout: None,
817        images_skipped: Vec::new(),
818    })
819}
820
821/// Length of the first IFD's `ImageDescription` (the OME-XML) of a TIFF, or 0.
822fn read_description_len(path: &Path) -> u64 {
823    use tiff::decoder::Decoder;
824    let Ok(f) = File::open(path) else {
825        return 0;
826    };
827    Decoder::new(std::io::BufReader::new(f))
828        .ok()
829        .and_then(|mut d| d.get_tag_ascii_string(Tag::ImageDescription).ok())
830        .map_or(0, |s| s.len() as u64)
831}